VirtualBox

Changeset 102195 in vbox


Ignore:
Timestamp:
Nov 21, 2023 12:43:15 PM (12 months ago)
Author:
vboxsync
Message:

libs/xpcom/xpcom: Get rid of nsprpub based file I/O and convert to IPRT, bugref:10545

Location:
trunk/src/libs/xpcom18a4/xpcom/io
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/libs/xpcom18a4/xpcom/io/nsILocalFile.idl

    r101944 r102195  
    4242
    4343%{C++
    44 #include "prio.h"
    45 #include <stdio.h>
    46 
    4744#include <iprt/ldr.h>
    4845%}
    4946
    50 [ptr] native PRFileDescStar(PRFileDesc);
    5147[ptr] native RTLDRMOD(RTLDRMODINTERNAL);
    5248[ptr] native FILE(FILE);
     
    10298     */
    10399    attribute PRBool followLinks; 
    104 
    105     [noscript] PRFileDescStar openNSPRFileDesc(in long flags, in long mode);
    106     [noscript] FILE           openANSIFileDesc(in string mode);
    107100
    108101    [noscript] RTLDRMOD       load();
  • trunk/src/libs/xpcom18a4/xpcom/io/nsLocalFile.h

    r101804 r102195  
    6666// on this.
    6767
     68#include <iprt/err.h>
     69
    6870#include <errno.h>
     71
    6972#include "nsILocalFile.h"
    7073
     
    7780#endif
    7881
    79 #define NSRESULT_FOR_RETURN(ret) (((ret) < 0) ? NSRESULT_FOR_ERRNO() : NS_OK)
     82
     83#define NSRESULT_FOR_IPRT(ret) (RT_FAILURE((ret)) ? nsresultForIprt(ret) : NS_OK)
    8084
    8185inline nsresult
    82 nsresultForErrno(int err)
     86nsresultForIprt(int err)
    8387{
    8488    switch (err) {
    85       case 0:
     89      case VINF_SUCCESS:
    8690        return NS_OK;
    87       case ENOENT:
     91      case VERR_NOT_FOUND:
     92      case VERR_FILE_NOT_FOUND:
     93      case VERR_PATH_NOT_FOUND:
    8894        return NS_ERROR_FILE_TARGET_DOES_NOT_EXIST;
    89       case ENOTDIR:
     95      case VERR_NOT_A_DIRECTORY:
    9096        return NS_ERROR_FILE_DESTINATION_NOT_DIR;
    91 #ifdef ENOLINK
    92       case ENOLINK:
     97      case VERR_NOT_SYMLINK:
    9398        return NS_ERROR_FILE_UNRESOLVABLE_SYMLINK;
    94 #endif /* ENOLINK */
    95       case EEXIST:
     99      case VERR_ALREADY_EXISTS:
    96100        return NS_ERROR_FILE_ALREADY_EXISTS;
    97 #ifdef EPERM
    98       case EPERM:
    99 #endif /* EPERM */
    100       case EACCES:
     101      case VERR_ACCESS_DENIED:
    101102        return NS_ERROR_FILE_ACCESS_DENIED;
    102103      default:
     
    105106}
    106107
    107 #define NSRESULT_FOR_ERRNO() nsresultForErrno(errno)
     108#define NSRESULT_FOR_ERRNO() nsresultForIprt(RTErrConvertFromErrno(errno))
    108109
    109110void NS_StartupLocalFile();
  • trunk/src/libs/xpcom18a4/xpcom/io/nsLocalFileUnix.cpp

    r102050 r102195  
    7676#include "prerr.h"
    7777
    78 #include <iprt/errcore.h>
     78#include <iprt/err.h>
     79#include <iprt/dir.h>
     80#include <iprt/file.h>
     81#include <iprt/path.h>
     82#include <iprt/time.h>
    7983
    8084#define FILE_STRCMP strcmp
     
    97101
    98102NS_COM nsresult
    99 NS_ErrorAccordingToNSPR()
    100 {
    101     PRErrorCode err = PR_GetError();
    102     switch (err) {
    103       case PR_OUT_OF_MEMORY_ERROR:              return NS_ERROR_OUT_OF_MEMORY;
    104       case PR_WOULD_BLOCK_ERROR:                return NS_BASE_STREAM_WOULD_BLOCK;
    105       case PR_FILE_NOT_FOUND_ERROR:             return NS_ERROR_FILE_NOT_FOUND;
    106       case PR_READ_ONLY_FILESYSTEM_ERROR:       return NS_ERROR_FILE_READ_ONLY;
    107       case PR_NOT_DIRECTORY_ERROR:              return NS_ERROR_FILE_NOT_DIRECTORY;
    108       case PR_IS_DIRECTORY_ERROR:               return NS_ERROR_FILE_IS_DIRECTORY;
    109       case PR_LOOP_ERROR:                       return NS_ERROR_FILE_UNRESOLVABLE_SYMLINK;
    110       case PR_FILE_EXISTS_ERROR:                return NS_ERROR_FILE_ALREADY_EXISTS;
    111       case PR_FILE_IS_LOCKED_ERROR:             return NS_ERROR_FILE_IS_LOCKED;
    112       case PR_FILE_TOO_BIG_ERROR:               return NS_ERROR_FILE_TOO_BIG;
    113       case PR_NO_DEVICE_SPACE_ERROR:            return NS_ERROR_FILE_NO_DEVICE_SPACE;
    114       case PR_NAME_TOO_LONG_ERROR:              return NS_ERROR_FILE_NAME_TOO_LONG;
    115       case PR_DIRECTORY_NOT_EMPTY_ERROR:        return NS_ERROR_FILE_DIR_NOT_EMPTY;
    116       case PR_NO_ACCESS_RIGHTS_ERROR:           return NS_ERROR_FILE_ACCESS_DENIED;
     103NS_ErrorAccordingToIPRT(int vrc)
     104{
     105    switch (vrc) {
     106      case VERR_NO_MEMORY:                      return NS_ERROR_OUT_OF_MEMORY;
     107      case VERR_FILE_NOT_FOUND:                 return NS_ERROR_FILE_NOT_FOUND;
     108      case VERR_PATH_NOT_FOUND:                 return NS_ERROR_FILE_NOT_FOUND;
     109      case VERR_NOT_A_DIRECTORY:                return NS_ERROR_FILE_NOT_DIRECTORY;
     110      case VERR_IS_A_DIRECTORY:                 return NS_ERROR_FILE_IS_DIRECTORY;
     111      case VERR_ALREADY_EXISTS:                 return NS_ERROR_FILE_ALREADY_EXISTS;
     112      case VERR_FILE_LOCK_VIOLATION:            return NS_ERROR_FILE_IS_LOCKED;
     113      case VERR_FILE_TOO_BIG:                   return NS_ERROR_FILE_TOO_BIG;
     114      case VERR_FILENAME_TOO_LONG:              return NS_ERROR_FILE_NAME_TOO_LONG;
     115      case VERR_DIR_NOT_EMPTY:                  return NS_ERROR_FILE_DIR_NOT_EMPTY;
     116      case VERR_ACCESS_DENIED:                  return NS_ERROR_FILE_ACCESS_DENIED;
     117      case VERR_WRITE_PROTECT:                  return NS_ERROR_FILE_READ_ONLY;
     118      case VERR_DISK_FULL:                      return NS_ERROR_FILE_NO_DEVICE_SPACE;
     119
     120      /** @todo: No IPRT equivalent right now (but shouldn't be important). */
     121      //case PR_WOULD_BLOCK_ERROR:                return NS_BASE_STREAM_WOULD_BLOCK;
    117122      default:                                  return NS_ERROR_FAILURE;
    118123    }
     
    370375         */
    371376        if (mkdir_result == -1 && mkdir_errno != EEXIST)
    372             return nsresultForErrno(mkdir_errno);
     377            return nsresultForIprt(RTErrConvertFromErrno(mkdir_errno));
    373378    }
    374379
     
    380385}
    381386
    382 NS_IMETHODIMP
    383 nsLocalFile::OpenNSPRFileDesc(PRInt32 flags, PRInt32 mode, PRFileDesc **_retval)
    384 {
    385     *_retval = PR_Open(mPath.get(), flags, mode);
    386     if (! *_retval)
    387         return NS_ErrorAccordingToNSPR();
    388 
    389     return NS_OK;
    390 }
    391 
    392 NS_IMETHODIMP
    393 nsLocalFile::OpenANSIFileDesc(const char *mode, FILE **_retval)
    394 {
    395     *_retval = fopen(mPath.get(), mode);
    396     if (! *_retval)
    397         return NS_ERROR_FAILURE;
    398 
    399     return NS_OK;
    400 }
    401 
    402387static int
    403 do_create(const char *path, PRIntn flags, mode_t mode, PRFileDesc **_retval)
    404 {
    405     *_retval = PR_Open(path, flags, mode);
    406     return *_retval ? 0 : -1;
     388do_create(const char *path, uint32_t flags, mode_t mode, RTFILE *_retval)
     389{
     390    return RTFileOpen(_retval, path, flags | (mode << RTFILE_O_CREATE_MODE_SHIFT));
    407391}
    408392
    409393static int
    410 do_mkdir(const char *path, PRIntn flags, mode_t mode, PRFileDesc **_retval)
    411 {
    412     *_retval = nsnull;
    413     return mkdir(path, mode);
     394do_mkdir(const char *path, uint32_t flags, mode_t mode, RTFILE *_retval)
     395{
     396    *_retval = NULL;
     397    return RTDirCreate(path, mode, 0 /* fCreate*/);
    414398}
    415399
    416400nsresult
    417 nsLocalFile::CreateAndKeepOpen(PRUint32 type, PRIntn flags,
    418                                PRUint32 permissions, PRFileDesc **_retval)
     401nsLocalFile::CreateAndKeepOpen(PRUint32 type, uint32_t flags,
     402                               PRUint32 permissions, RTFILE *_retval)
    419403{
    420404    if (type != NORMAL_FILE_TYPE && type != DIRECTORY_TYPE)
    421405        return NS_ERROR_FILE_UNKNOWN_TYPE;
    422406
    423     int result;
    424     int (*createFunc)(const char *, PRIntn, mode_t, PRFileDesc **) =
     407    int (*createFunc)(const char *, uint32_t, mode_t, RTFILE *) =
    425408        (type == NORMAL_FILE_TYPE) ? do_create : do_mkdir;
    426409
    427     result = createFunc(mPath.get(), flags, permissions, _retval);
    428     if (result == -1 && errno == ENOENT) {
     410    int vrc = createFunc(mPath.get(), flags, permissions, _retval);
     411    if (vrc == VERR_PATH_NOT_FOUND || vrc == VERR_FILE_NOT_FOUND || vrc == VERR_NOT_FOUND) {
    429412        /*
    430413         * If we failed because of missing ancestor components, try to create
     
    455438        fprintf(stderr, "nsIFile: Create(\"%s\") again\n", mPath.get());
    456439#endif
    457         result = createFunc(mPath.get(), flags, permissions, _retval);
    458     }
    459 
    460     return NSRESULT_FOR_RETURN(result);
     440        vrc = createFunc(mPath.get(), flags, permissions, _retval);
     441    }
     442
     443    return NSRESULT_FOR_IPRT(vrc);
    461444}
    462445
     
    464447nsLocalFile::Create(PRUint32 type, PRUint32 permissions)
    465448{
    466     PRFileDesc *junk = nsnull;
     449    RTFILE junk = NULL;
    467450    nsresult rv = CreateAndKeepOpen(type,
    468                                     PR_WRONLY | PR_CREATE_FILE | PR_TRUNCATE |
    469                                     PR_EXCL,
     451                                    RTFILE_O_WRITE | RTFILE_O_CREATE | RTFILE_O_TRUNCATE |
     452                                    RTFILE_O_DENY_ALL,
    470453                                    permissions,
    471454                                    &junk);
    472455    if (junk)
    473         PR_Close(junk);
     456        RTFileClose(junk);
    474457    return rv;
    475458}
     
    793776        // open it successfully for writing.
    794777
    795         PRFileDesc *newFD;
     778        RTFILE newFD;
    796779        rv = newFile->CreateAndKeepOpen(NORMAL_FILE_TYPE,
    797                                         PR_WRONLY|PR_CREATE_FILE|PR_TRUNCATE,
     780                                        RTFILE_O_WRITE | RTFILE_O_CREATE | RTFILE_O_TRUNCATE | RTFILE_O_DENY_NONE,
    798781                                        myPerms,
    799782                                        &newFD);
     
    804787        PRBool specialFile;
    805788        if (NS_FAILED(rv = IsSpecial(&specialFile))) {
    806             PR_Close(newFD);
     789            RTFileClose(newFD);
    807790            return rv;
    808791        }
     
    812795#endif
    813796            // make sure to clean up properly
    814             PR_Close(newFD);
     797            RTFileClose(newFD);
    815798            return NS_OK;
    816799        }
    817800               
    818         PRFileDesc *oldFD;
    819         rv = OpenNSPRFileDesc(PR_RDONLY, myPerms, &oldFD);
     801        RTFILE oldFD = NIL_RTFILE;
     802        int vrc = RTFileOpen(&oldFD, mPath.get(), RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_DENY_NONE);
     803        if (RT_FAILURE(vrc))
     804            rv = NS_ErrorAccordingToIPRT(vrc);
    820805        if (NS_FAILED(rv)) {
    821806            // make sure to clean up properly
    822             PR_Close(newFD);
     807            RTFileClose(newFD);
    823808            return rv;
    824809        }
    825810
    826 #ifdef DEBUG_blizzard
    827         PRInt32 totalRead = 0;
    828         PRInt32 totalWritten = 0;
    829 #endif
    830         char buf[BUFSIZ];
    831         PRInt32 bytesRead;
    832        
    833         while ((bytesRead = PR_Read(oldFD, buf, BUFSIZ)) > 0) {
    834 #ifdef DEBUG_blizzard
    835             totalRead += bytesRead;
    836 #endif
    837 
    838             // PR_Write promises never to do a short write
    839             PRInt32 bytesWritten = PR_Write(newFD, buf, bytesRead);
    840             if (bytesWritten < 0) {
    841                 bytesRead = -1;
    842                 break;
    843             }
    844             NS_ASSERTION(bytesWritten == bytesRead, "short PR_Write?");
    845 
    846 #ifdef DEBUG_blizzard
    847             totalWritten += bytesWritten;
    848 #endif
    849         }
    850 
    851 #ifdef DEBUG_blizzard
    852         printf("read %d bytes, wrote %d bytes\n",
    853                  totalRead, totalWritten);
    854 #endif
     811        vrc = RTFileCopyByHandles(oldFD, newFD);
    855812
    856813        // close the files
    857         PR_Close(newFD);
    858         PR_Close(oldFD);
     814        RTFileClose(newFD);
     815        RTFileClose(oldFD);
    859816
    860817        // check for read (or write) error after cleaning up
    861         if (bytesRead < 0)
     818        if (RT_FAILURE(vrc))
    862819            return NS_ERROR_OUT_OF_MEMORY;
    863820    }
     
    915872
    916873    if (!recursive && isSymLink)
    917         return NSRESULT_FOR_RETURN(unlink(mPath.get()));
     874        return NSRESULT_FOR_IPRT(RTErrConvertFromErrno(unlink(mPath.get())));
    918875   
    919876    isDir = S_ISDIR(mCachedStat.st_mode);
     
    962919    NS_ENSURE_ARG(aLastModTime);
    963920
    964     PRFileInfo64 info;
    965     if (PR_GetFileInfo64(mPath.get(), &info) != PR_SUCCESS)
    966         return NSRESULT_FOR_ERRNO();
    967 
    968     // PRTime is a 64 bit value
     921    RTFSOBJINFO info;
     922    int vrc = RTPathQueryInfo(mPath.get(), &info, RTFSOBJATTRADD_NOTHING);
     923    if (RT_FAILURE(vrc))
     924        return NSRESULT_FOR_IPRT(vrc);
     925
    969926    // microseconds -> milliseconds
    970     PRInt64 usecPerMsec;
    971     LL_I2L(usecPerMsec, PR_USEC_PER_MSEC);
    972     LL_DIV(*aLastModTime, info.modifyTime, usecPerMsec);
     927    *aLastModTime = RTTimeSpecGetMilli(&info.ModificationTime);
    973928    return NS_OK;
    974929}
     
    994949    }
    995950    InvalidateCache();
    996     return NSRESULT_FOR_RETURN(result);
     951    return NSRESULT_FOR_IPRT(RTErrConvertFromErrno(result));
    997952}
    998953
  • trunk/src/libs/xpcom18a4/xpcom/io/nsLocalFileUnix.h

    r1 r102195  
    4444#ifndef _nsLocalFileUNIX_H_
    4545#define _nsLocalFileUNIX_H_
     46
     47#include <iprt/file.h>
    4648
    4749#include <sys/stat.h>
     
    126128    nsresult FillStatCache();
    127129
    128     nsresult CreateAndKeepOpen(PRUint32 type, PRIntn flags,
    129                                PRUint32 permissions, PRFileDesc **_retval);
     130    nsresult CreateAndKeepOpen(PRUint32 type, uint32_t flags,
     131                               PRUint32 permissions, RTFILE *_retval);
    130132};
    131133
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette