// static void VFSNode::copyDirectory(const VFSNode& source, const VFSNode& dest, bool recursive) { if (recursive) { VString sourcePathWithTrailingSeparator = source.getPath() + (source.getPath().endsWith(PATH_SEPARATOR_CHAR) ? "" : PATH_SEPARATOR_CHARS); if (dest.getPath().startsWith(sourcePathWithTrailingSeparator)) { throw VException(VSTRING_FORMAT("Attempt to recursively copy '%s' into '%s'.", source.getPath().chars(), dest.getPath().chars())); } } if (!dest.exists()) { dest.mkdirs(); } VFSNodeCopyDirectoryCallback copyDirectoryCallback(dest, recursive); source.iterate(copyDirectoryCallback); }
void VFSNodeUnit::_testDirectoryIteration(const VFSNode& dir) { const int NUM_FILES_TO_CREATE = 5; const int NUM_FILES_TO_CHECK = NUM_FILES_TO_CREATE + 3; // we'll verify we don't have these extras // Test directory listing, iteration, find. // Create 5 files in the deep directory, then test that we can find them. for (int i = 0; i < NUM_FILES_TO_CREATE; ++i) { VString testIterFileName(VSTRING_ARGS("iter_test_%d.txt", i)); VFSNode testIterFileNode; dir.getChildNode(testIterFileName, testIterFileNode); VBufferedFileStream testIterStream(testIterFileNode); testIterStream.openWrite(); VTextIOStream out(testIterStream); out.writeLine(testIterFileName); } { // find() test VFSNode testIterNode; for (int i = 0; i < NUM_FILES_TO_CHECK; ++i) { VString testIterFileName(VSTRING_ARGS("iter_test_%d.txt", i)); if (i < NUM_FILES_TO_CREATE) VUNIT_ASSERT_TRUE_LABELED(dir.find(testIterFileName, testIterNode), VSTRING_FORMAT("find() found #%d", i)); // this file should exist else VUNIT_ASSERT_FALSE_LABELED(dir.find(testIterFileName, testIterNode), VSTRING_FORMAT("find() did not find #%d", i)); // this file should not exist } } // There is no guarantee that the list() and iterate() methods will return the directory // listing in any particular order. We either need to sort them ourself, or verify without // regard to order. Here, we'll sort the returned string list, since we know that our // file names are sortable because they are single-digit-number-based strings, e.g. "iter_test_3.txt". // Note that the result of sorting is ultimately dependent on strcmp(). { // list() names test int index = 0; VStringVector fileNames; dir.list(fileNames); std::sort(fileNames.begin(), fileNames.end()); VUNIT_ASSERT_EQUAL_LABELED(static_cast<int>(fileNames.size()), NUM_FILES_TO_CREATE, "list names size"); for (VStringVector::const_iterator i = fileNames.begin(); i != fileNames.end(); ++i, ++index) { VString testIterFileName(VSTRING_ARGS("iter_test_%d.txt", index)); VUNIT_ASSERT_EQUAL_LABELED(*i, testIterFileName, VSTRING_FORMAT("list names #%d", index)); } } { // list() nodes test int index = 0; VFSNodeVector fileNodes; dir.list(fileNodes); std::sort(fileNodes.begin(), fileNodes.end()); VUNIT_ASSERT_EQUAL_LABELED(static_cast<int>(fileNodes.size()), NUM_FILES_TO_CREATE, "list nodes size"); for (VFSNodeVector::const_iterator i = fileNodes.begin(); i != fileNodes.end(); ++i, ++index) { VString testIterFileName(VSTRING_ARGS("iter_test_%d.txt", index)); VString nodeFileName; i->getName(nodeFileName); VUNIT_ASSERT_EQUAL_LABELED(nodeFileName, testIterFileName, VSTRING_FORMAT("list nodes #%d", index)); } } { // iterate() test int index = 0; VFSNodeIterateTestCallback callback; dir.iterate(callback); std::sort(callback.mNodeNames.begin(), callback.mNodeNames.end()); VUNIT_ASSERT_EQUAL_LABELED(static_cast<int>(callback.mNodeNames.size()), NUM_FILES_TO_CREATE, "iterate size"); for (VStringVector::const_iterator i = callback.mNodeNames.begin(); i != callback.mNodeNames.end(); ++i, ++index) { VString testIterFileName(VSTRING_ARGS("iter_test_%d.txt", index)); VUNIT_ASSERT_EQUAL_LABELED(*i, testIterFileName, VSTRING_FORMAT("iterate nodes #%d", index)); } } }