Example #1
0
/*----------------------------------------------------------------------
|   TestRemoveEmptyDirectory
+---------------------------------------------------------------------*/
void
TestRemoveEmptyDirectory()
{
    NPT_DirectoryEntryInfo info;
    NPT_String             path;
    NPT_Result             res;

    // read path from environment variable
    res = NPT_GetEnvironment("NEPTUNE_TEST_ROOT", path);
    CHECK(NPT_SUCCEEDED(res));

    // find the next directory path that does not exist
    res = FindNextAvailableDirectory(path, path);
    CHECK(NPT_SUCCEEDED(res));

    // create directory object
    res = NPT_DirectoryCreate(path, true);
    CHECK(NPT_SUCCEEDED(res));

    // remove empty directory
    res = NPT_DirectoryRemove(path, false);
    CHECK(NPT_SUCCEEDED(res));

    // verify directory does not exist any more
    res = NPT_DirectoryEntry::GetInfo(path, &info);
    CHECK(res == NPT_ERROR_NO_SUCH_ITEM);
}
Example #2
0
/*----------------------------------------------------------------------
|   TestRemoveDirectory
+---------------------------------------------------------------------*/
void
TestRemoveDirectory()
{
    NPT_DirectoryEntryInfo info;
    NPT_String             path;
    NPT_Result             res;

    // read path from environment variable
    res = NPT_GetEnvironment("NEPTUNE_TEST_ROOT", path);
    CHECK(NPT_SUCCEEDED(res));

    // find the next directory path that does not exist
    res = FindNextAvailableDirectory(path, path);
    CHECK(NPT_SUCCEEDED(res));

    // create directory object
    res = NPT_DirectoryCreate(path, true);
    CHECK(NPT_SUCCEEDED(res));

    // create some subdirectories
    res = CreateEntry(path, "foof1", true);
    CHECK(NPT_SUCCEEDED(res));
    res = CreateEntry(path, "foof2", true);
    CHECK(NPT_SUCCEEDED(res));
    res = CreateEntry(path, "foof2.dir", true);
    CHECK(NPT_SUCCEEDED(res));

    // create some files
    res = CreateEntry(path, "foo1", false);
    CHECK(NPT_SUCCEEDED(res));
    res = CreateEntry(path, "foo2.", false);
    CHECK(NPT_SUCCEEDED(res));
    res = CreateEntry(path, "foo2.e", false);
    CHECK(NPT_SUCCEEDED(res));
    res = CreateEntry(path, "foo2.e3", false);
    CHECK(NPT_SUCCEEDED(res));
    res = CreateEntry(path, "foo2.gas", false);
    CHECK(NPT_SUCCEEDED(res));
    res = CreateEntry(path, "foo4.tee", false);
    CHECK(NPT_SUCCEEDED(res));

    // remove directory
    res = NPT_DirectoryRemove(path, true);
    CHECK(NPT_SUCCEEDED(res));

    // verify directory does not exist any more
    res = NPT_DirectoryEntry::GetInfo(path, &info);
    CHECK(res == NPT_ERROR_NO_SUCH_ITEM);
}
Example #3
0
/*----------------------------------------------------------------------
|   NPT_DirectoryRemove
+---------------------------------------------------------------------*/
NPT_Result 
NPT_DirectoryRemove(const char* path, bool recursively)
{
    NPT_Result             res;
    NPT_DirectoryEntryInfo info;
    NPT_String             root_path = path;

    // replace delimiters with the proper one for the platform
    root_path.Replace((NPT_DIR_DELIMITER_CHR == '/')?'\\':'/', NPT_DIR_DELIMITER_CHR);
    // remove excessive delimiters
    root_path.TrimRight(NPT_DIR_DELIMITER_CHR);

    NPT_CHECK_WARNING(NPT_DirectoryEntry::GetInfo(root_path, &info));
    if (info.type == NPT_FILE_TYPE || !recursively) {
        return NPT_Directory::Remove(root_path);
    }

    {
        // enumerate all entries
        NPT_Directory  dir(path);
        NPT_String     entry_path;
        NPT_String     name;

        do {
            res = dir.GetNextEntry(name, &info);
            if (NPT_SUCCEEDED(res)) {
                // build full path to entry
                entry_path = root_path;

                // append filename
                NPT_DirectoryAppendToPath(entry_path, name);

                // try to remove entry recursively
                res = NPT_DirectoryRemove(entry_path, true);
            }
        } while (NPT_SUCCEEDED(res));
    }

    // if no more items in directory, try to delete it
    if (res == NPT_ERROR_NO_SUCH_ITEM) {
        return NPT_Directory::Remove(path);
    }

    return res;
}