VirtualBox

Ignore:
Timestamp:
Apr 27, 2009 1:56:07 PM (16 years ago)
Author:
vboxsync
Message:

IPRT: Function RTDirRemoveRecursive added. Only partially implemented yet (no symlinks etc).

File:
1 edited

Legend:

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

    r9358 r19211  
    4343
    4444#include <iprt/dir.h>
     45#include <iprt/file.h>
    4546#include <iprt/path.h>
    4647#include <iprt/alloc.h>
     
    715716}
    716717
     718
     719RTDECL(int) RTDirRemoveRecursive(const char *pszPath)
     720{
     721    int rc;
     722
     723    if (!RTDirExists(pszPath))
     724        return VINF_SUCCESS;
     725
     726    char szAbsPath[RTPATH_MAX];
     727    /** @todo use RTPathReal here instead? */
     728    rc = RTPathAbs(pszPath, szAbsPath, sizeof(szAbsPath));
     729    if (RT_FAILURE(rc))
     730        return rc;
     731
     732    PRTDIR pDir = NULL;
     733    rc = RTDirOpen(&pDir, szAbsPath);
     734    if (RT_SUCCESS(rc))
     735    {
     736        RTDIRENTRY dirEntry;
     737        size_t cbDirEntry = sizeof(dirEntry);
     738
     739        while ((rc = RTDirRead(pDir, &dirEntry, NULL /* Passing an argument won't work here yet. */)) == VINF_SUCCESS)
     740        {
     741            char* pszEntry = NULL;
     742            rc = RTStrAPrintf(&pszEntry, "%s/%s", szAbsPath, dirEntry.szName);
     743            if(    RT_SUCCESS(rc)
     744                && strcmp(dirEntry.szName, ".")
     745                && strcmp(dirEntry.szName, ".."))
     746            {
     747                switch (dirEntry.enmType)
     748                {
     749                    case RTDIRENTRYTYPE_FILE:
     750
     751                        rc = RTFileDelete(pszEntry);
     752                        break;
     753
     754                    case RTDIRENTRYTYPE_DIRECTORY:
     755
     756                        rc = RTDirRemoveRecursive(pszEntry);
     757                        break;
     758
     759                    default:
     760                        /** @todo not implemented yet. */
     761                        break;
     762                }
     763
     764                RTStrFree(pszEntry);
     765            }
     766
     767            if (RT_FAILURE(rc))
     768               break;
     769        }
     770        if (rc == VERR_NO_MORE_FILES)
     771            rc = VINF_SUCCESS;
     772
     773        RTDirClose(pDir);
     774        rc = RTDirRemove(szAbsPath);   
     775    }
     776
     777    LogFlow(("RTDirRemoveRecursive(%p:{%s}): returns %Rrc\n", pszPath, pszPath, rc));
     778    return rc;
     779}
     780
     781
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