VirtualBox

Changeset 6429 in vbox for trunk/src/VBox/Runtime


Ignore:
Timestamp:
Jan 21, 2008 10:22:11 PM (17 years ago)
Author:
vboxsync
Message:

r=bird: file sizes and offets are given as RTFOFF now (the uint64_t use is just legacy that will have to be removed sooner or later). Added a Ex version and filled in the missing documentation. Added a todo about the function *not* working on Windows (vista at least).

Location:
trunk/src/VBox/Runtime
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/r3/fileio.cpp

    r6421 r6429  
    219219
    220220/**
    221  * Determine the maximum file size. Tested on Windows and Linux.
    222  */
    223 RTR3DECL(uint64_t)  RTFileGetMaxSize(RTFILE File)
    224 {
     221 * Determine the maximum file size.
     222 * 
     223 * @returns The max size of the file.
     224 *          -1 on failure, the file position is undefined.
     225 * @param   File        Handle to the file.
     226 * @see     RTFileGetMaxSizeEx.
     227 */
     228RTR3DECL(RTFOFF) RTFileGetMaxSize(RTFILE File)
     229{
     230    RTFOFF cbMax;
     231    int rc = RTFileGetMaxSizeEx(File, &cbMax);
     232    return RT_SUCCESS(rc) ? cbMax : -1;
     233}
     234
     235
     236/**
     237 * Determine the maximum file size.
     238 * 
     239 * @returns IPRT status code.
     240 * @param   File        Handle to the file.
     241 * @param   pcbMax      Where to store the max file size.
     242 * @see     RTFileGetMaxSize.
     243 */
     244RTR3DECL(int) RTFileGetMaxSizeEx(RTFILE File, PRTFOFF pcbMax)
     245{
     246    /*
     247     * Save the current location
     248     */
     249    uint64_t offOld;
     250    int rc = RTFileSeek(File, 0, RTFILE_SEEK_CURRENT, &offOld);
     251    if (RT_FAILURE(rc))
     252        return rc;
     253
     254    /*
     255     * Perform a binary search for the max file size.
     256     */
    225257    uint64_t offLow  =       0;
    226258    uint64_t offHigh = 8 * _1T; /* we don't need bigger files */
    227     uint64_t offOld  = RTFileTell(File);
    228 
     259    /** @todo r=bird: This isn't doing the trick for windows (at least not vista).
     260     * Close to offHigh is returned regardless of NTFS or FAT32.
     261     * We might have to make this code OS specific... */
     262    //uint64_t offHigh = INT64_MAX;
    229263    for (;;)
    230264    {
    231         uint64_t interval = (offHigh - offLow) >> 1;
    232         if (interval == 0)
     265        uint64_t cbInterval = (offHigh - offLow) >> 1;
     266        if (cbInterval == 0)
    233267        {
    234             RTFileSeek(File, offOld, RTFILE_SEEK_BEGIN, NULL);
    235             return offLow;
     268            if (pcbMax)
     269                *pcbMax = offLow;
     270            return RTFileSeek(File, offOld, RTFILE_SEEK_BEGIN, NULL);
    236271        }
    237         if (RT_FAILURE(RTFileSeek(File, offLow + interval, RTFILE_SEEK_BEGIN, NULL)))
    238             offHigh = offLow + interval;
     272
     273        rc = RTFileSeek(File, offLow + cbInterval, RTFILE_SEEK_BEGIN, NULL);
     274        if (RT_FAILURE(rc))
     275            offHigh = offLow + cbInterval;
    239276        else
    240             offLow  = offLow + interval;
     277            offLow  = offLow + cbInterval;
    241278    }
    242279}
  • trunk/src/VBox/Runtime/testcase/tstFile.cpp

    r6421 r6429  
    5050    }
    5151
    52     RTPrintf("Maximum file size is %lld bytes.\n", RTFileGetMaxSize(File));
     52    RTFOFF cbMax = -2;
     53    rc = RTFileGetMaxSizeEx(File, &cbMax);
     54    if (RT_FAILURE(rc))
     55    {
     56        RTPrintf("tstFile: RTFileGetMaxSizeEx failed: %Rrc\n", rc);
     57        cErrors++;
     58    }
     59    else if (cbMax <= 0)
     60    {
     61        RTPrintf("tstFile: RTFileGetMaxSizeEx failed: cbMax=%RTfoff\n", cbMax);
     62        cErrors++;
     63    }
     64    else if (RTFileGetMaxSize(File) != cbMax)
     65    {
     66        RTPrintf("tstFile: RTFileGetMaxSize failed; returns %RTfoff instead of %RTfoff\n", RTFileGetMaxSize(File), cbMax);
     67        cErrors++;
     68    }
     69    else
     70        RTPrintf("Maximum file size is %RTfoff bytes.\n", cbMax);
     71
     72return 0;
    5373
    5474    /* grow file beyond 2G */
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