VirtualBox

Changeset 69757 in vbox


Ignore:
Timestamp:
Nov 19, 2017 3:15:36 PM (7 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
119157
Message:

iprt/dir.h: Added RTDirReadExA and RTDirReadExAFree for avoiding duplicating buffer management everywhere.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/iprt/dir.h

    r69756 r69757  
    448448
    449449/**
     450 * Wrapper around RTDirReadEx that does the directory entry buffer handling.
     451 *
     452 * Call RTDirReadExAFree to free the buffers allocated by this function.
     453 *
     454 * @returns IPRT status code, see RTDirReadEx() for details.
     455 *
     456 * @param   hDir        Handle to the open directory.
     457 * @param   ppDirEntry  Pointer to the directory entry pointer.  Initialize this
     458 *                      to NULL before the first call.
     459 * @param   pcbDirEntry Where the API caches the allocation size.  Set this to
     460 *                      zero before the first call.
     461 * @param   enmAddAttr  See RTDirReadEx.
     462 * @param   fFlags      See RTDirReadEx.
     463 */
     464RTDECL(int) RTDirReadExA(RTDIR hDir, PRTDIRENTRYEX *ppDirEntry, size_t *pcbDirEntry, RTFSOBJATTRADD enmAddAttr, uint32_t fFlags);
     465
     466/**
     467 * Frees the buffer allocated by RTDirReadExA.
     468 *
     469 * @param   ppDirEntry  Pointer to the directory entry pointer.
     470 * @param   pcbDirEntry Where the API caches the allocation size.
     471 */
     472RTDECL(void) RTDirReadExAFree(PRTDIRENTRYEX *ppDirEntry, size_t *pcbDirEntry);
     473
     474/**
    450475 * Resolves RTDIRENTRYTYPE_UNKNOWN values returned by RTDirRead.
    451476 *
  • trunk/include/iprt/mangling.h

    r69745 r69757  
    767767# define RTDirRead                                      RT_MANGLER(RTDirRead)
    768768# define RTDirReadEx                                    RT_MANGLER(RTDirReadEx)
     769# define RTDirReadExA                                   RT_MANGLER(RTDirReadExA)
     770# define RTDirReadExAFree                               RT_MANGLER(RTDirReadExAFree)
    769771# define RTDirRemove                                    RT_MANGLER(RTDirRemove)
    770772# define RTDirRemoveRecursive                           RT_MANGLER(RTDirRemoveRecursive)
  • trunk/src/VBox/Runtime/r3/dir.cpp

    r69753 r69757  
    795795}
    796796
     797
     798RTDECL(int) RTDirReadExA(RTDIR hDir, PRTDIRENTRYEX *ppDirEntry, size_t *pcbDirEntry, RTFSOBJATTRADD enmAddAttr, uint32_t fFlags)
     799{
     800    PRTDIRENTRYEX pDirEntry  = *ppDirEntry;
     801    size_t        cbDirEntry = *pcbDirEntry;
     802    if (pDirEntry != NULL && cbDirEntry >= sizeof(RTDIRENTRYEX))
     803    { /* likely */ }
     804    else
     805    {
     806        Assert(pDirEntry == NULL);
     807        Assert(cbDirEntry == 0);
     808
     809        cbDirEntry  = RT_ALIGN_Z(sizeof(RTDIRENTRYEX), 16);
     810        *ppDirEntry = pDirEntry = (PRTDIRENTRYEX)RTMemTmpAlloc(cbDirEntry);
     811        if (pDirEntry)
     812            *pcbDirEntry = cbDirEntry;
     813        else
     814        {
     815            *pcbDirEntry = 0;
     816            return VERR_NO_TMP_MEMORY;
     817        }
     818    }
     819
     820    for (;;)
     821    {
     822        int rc = RTDirReadEx(hDir, pDirEntry, &cbDirEntry, enmAddAttr, fFlags);
     823        if (rc != VERR_BUFFER_OVERFLOW)
     824            return rc;
     825
     826        /* Grow the buffer. */
     827        RTMemTmpFree(pDirEntry);
     828        cbDirEntry = RT_MAX(RT_ALIGN_Z(cbDirEntry, 64), *pcbDirEntry + 64);
     829        *ppDirEntry = pDirEntry = (PRTDIRENTRYEX)RTMemTmpAlloc(cbDirEntry);
     830        if (pDirEntry)
     831            *pcbDirEntry = cbDirEntry;
     832        else
     833        {
     834            *pcbDirEntry = 0;
     835            return VERR_NO_TMP_MEMORY;
     836        }
     837    }
     838}
     839
     840
     841RTDECL(void) RTDirReadExAFree(PRTDIRENTRYEX *ppDirEntry, size_t *pcbDirEntry)
     842{
     843    PRTDIRENTRYEX pDirEntry = *ppDirEntry;
     844    if (pDirEntry != NULL && *pcbDirEntry >= sizeof(*pcbDirEntry))
     845        RTMemTmpFree(pDirEntry);
     846    else
     847    {
     848        Assert(pDirEntry == NULL);
     849        Assert(*pcbDirEntry == 0);
     850    }
     851    *ppDirEntry  = NULL;
     852    *pcbDirEntry = 0;
     853}
     854
Note: See TracChangeset for help on using the changeset viewer.

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