From 8c15c80b1485af5673f282dfae1356da5d341e97 Mon Sep 17 00:00:00 2001 From: Ivan De Marino Date: Sun, 4 Sep 2011 21:26:11 +0100 Subject: [PATCH 1/2] Renamed "fs-shim.js" to "fileystem-shim.js" to ensure name consistency. --- src/{fs-shim.js => filesystem-shim.js} | 0 src/filesystem.h | 20 ++++++++++---------- src/phantom.cpp | 2 +- src/phantomjs.qrc | 2 +- 4 files changed, 12 insertions(+), 12 deletions(-) rename src/{fs-shim.js => filesystem-shim.js} (100%) diff --git a/src/fs-shim.js b/src/filesystem-shim.js similarity index 100% rename from src/fs-shim.js rename to src/filesystem-shim.js diff --git a/src/filesystem.h b/src/filesystem.h index e14fe0ec..7497bc6d 100644 --- a/src/filesystem.h +++ b/src/filesystem.h @@ -72,7 +72,7 @@ public: public slots: // Attributes - // 'size(path)' implemented in "fs-shim.js" using '_size(path)' + // 'size(path)' implemented in "filesystem-shim.js" using '_size(path)' int _size(const QString &path) const; QVariant lastModified(const QString &path) const; @@ -83,22 +83,22 @@ public slots: // symbolic links to directories. bool makeDirectory(const QString &path) const; bool makeTree(const QString &path) const; - // 'removeDirectory(path)' implemented in "fs-shim.js" using '_removeDirectory(path)' + // 'removeDirectory(path)' implemented in "filesystem-shim.js" using '_removeDirectory(path)' bool _removeDirectory(const QString &path) const; - // 'removeTree(path)' implemented in "fs-shim.js" using '_removeTree(path)' + // 'removeTree(path)' implemented in "filesystem-shim.js" using '_removeTree(path)' bool _removeTree(const QString &path) const; // Files - // 'open(path, mode)' implemented in "fs-shim.js" using '_open(path, mode)' + // 'open(path, mode)' implemented in "filesystem-shim.js" using '_open(path, mode)' QObject *_open(const QString &path, const QString &mode) const; - // 'read(path)' implemented in "fs-shim.js" - // 'write(path, mode)' implemented in the "fs-shim.js" - // 'remove(path)' implemented in "fs-shim.js" using '_remove(path)' + // 'read(path)' implemented in "filesystem-shim.js" + // 'write(path, mode)' implemented in the "filesystem-shim.js" + // 'remove(path)' implemented in "filesystem-shim.js" using '_remove(path)' bool _remove(const QString &path) const; - // 'copy(source, destination)' implemented in "fs-shim.js" using '_copy(source, destination)' + // 'copy(source, destination)' implemented in "filesystem-shim.js" using '_copy(source, destination)' bool _copy(const QString &source, const QString &destination) const; - // 'move(source, destination)' implemented in "fs-shim.js" - // 'touch(path)' implemented in "fs-shim.js" + // 'move(source, destination)' implemented in "filesystem-shim.js" + // 'touch(path)' implemented in "filesystem-shim.js" // Listing QStringList list(const QString &path) const; diff --git a/src/phantom.cpp b/src/phantom.cpp index c747a193..36a87485 100644 --- a/src/phantom.cpp +++ b/src/phantom.cpp @@ -114,7 +114,7 @@ Phantom::Phantom(QObject *parent) // Load all the required JavaScript 'shims' QString jsShims[2] = { ":/webpage-shim.js", - ":/fs-shim.js" + ":/filesystem-shim.js" }; for (int i = 0, len = 2; i < len; ++i) { QFile f(jsShims[i]); diff --git a/src/phantomjs.qrc b/src/phantomjs.qrc index 9c8dd5d7..b7a2ab13 100644 --- a/src/phantomjs.qrc +++ b/src/phantomjs.qrc @@ -4,7 +4,7 @@ coffee-script.js usage.txt webpage-shim.js - fs-shim.js + filesystem-shim.js configurator.js From 88991207c2e5ffc0e6c41f5d55759606839dbc5e Mon Sep 17 00:00:00 2001 From: Ivan De Marino Date: Sun, 4 Sep 2011 23:42:54 +0100 Subject: [PATCH 2/2] Added "copyTree(source, destination)" to the set of File API. * This is the last API * I added tests accordingly * Test could be improved with a proper recursive comparison * Or, if we introduce "phantom.exec", we could just run a "diff -rq" :) --- src/filesystem-shim.js | 12 ++++++++++ src/filesystem.cpp | 50 +++++++++++++++++++++++++++++++++--------- src/filesystem.h | 6 ++--- test/fs-spec-03.js | 12 ++++++++-- 4 files changed, 64 insertions(+), 16 deletions(-) diff --git a/src/filesystem-shim.js b/src/filesystem-shim.js index 9215474a..0a8a173c 100644 --- a/src/filesystem-shim.js +++ b/src/filesystem-shim.js @@ -102,6 +102,18 @@ window.fs.copy = function (source, destination) { } }; +/** Copy a directory tree. + * It will throw an exception if it fails. + * + * @param source Path of the source directory tree + * @param destination Path of the destination directory tree + */ +window.fs.copyTree = function (source, destination) { + if (!fs._copyTree(source, destination)) { + throw "Unable to copy directory tree '" + source + "' at '" + destination + "'"; + } +}; + /** Move a file. * It will throw an exception if it fails. * diff --git a/src/filesystem.cpp b/src/filesystem.cpp index f56f3f51..9d52a3c8 100644 --- a/src/filesystem.cpp +++ b/src/filesystem.cpp @@ -179,6 +179,34 @@ bool FileSystem::isWritable(const QString &path) const { } // Directory +bool FileSystem::_copyTree(const QString &source, const QString &destination) const { + QDir sourceDir(source); + QDir::Filters sourceDirFilter = QDir::NoDotAndDotDot | QDir::AllDirs | QDir::Files | QDir::NoSymLinks | QDir::Drives; + + if (sourceDir.exists()) { + // Make the destination directory if it doesn't exist already + if (!FileSystem::exists(destination) && !FileSystem::makeDirectory(destination)) { + return false; + } + + foreach(QFileInfo entry, sourceDir.entryInfoList(sourceDirFilter, QDir::DirsFirst)) { + if (entry.isDir()) { + if (!FileSystem::_copyTree(entry.absoluteFilePath(), + destination + "/" + entry.fileName())) { //< directory: recursive call + return false; + } + } else { + if (!FileSystem::_copy(entry.absoluteFilePath(), + destination + "/" + entry.fileName())) { //< file: copy + return false; + } + } + } + } + + return true; +} + bool FileSystem::makeDirectory(const QString &path) const { return QDir().mkdir(path); @@ -197,24 +225,26 @@ bool FileSystem::_removeDirectory(const QString &path) const bool FileSystem::_removeTree(const QString &path) const { QDir dir(path); - bool res = false; + QDir::Filters dirFilter = QDir::NoDotAndDotDot | QDir::System | QDir::Hidden | QDir::AllDirs | QDir::Files; if (dir.exists()) { - foreach(QFileInfo info, dir.entryInfoList(QDir::NoDotAndDotDot | QDir::System | QDir::Hidden | QDir::AllDirs | QDir::Files, QDir::DirsFirst)) { + foreach(QFileInfo info, dir.entryInfoList(dirFilter, QDir::DirsFirst)) { if (info.isDir()) { - res = _removeTree(info.absoluteFilePath()); + if (!FileSystem::_removeTree(info.absoluteFilePath())) { //< directory: recursive call + return false; + } } else { - res = _remove(info.absoluteFilePath()); - } - - if (!res) { - return res; + if (!FileSystem::_remove(info.absoluteFilePath())) { //< file: remove + return false; + } } } - res = _removeDirectory(path); + if (!FileSystem::_removeDirectory(path)) { //< delete the top tree directory + return false; + } } - return res; + return true; } QStringList FileSystem::list(const QString &path) const diff --git a/src/filesystem.h b/src/filesystem.h index 7497bc6d..19303e53 100644 --- a/src/filesystem.h +++ b/src/filesystem.h @@ -77,10 +77,8 @@ public slots: QVariant lastModified(const QString &path) const; // Directory - // - copyTree(source, target) //< copies files from a source path to a target path, - // copying the files of the source tree to the corresponding locations - // relative to the target, copying but not traversing into - // symbolic links to directories. + // 'copyTree(source, destination)' implemented in "filesystem-shim.js" using '_copyTree(source, destination)' + bool _copyTree(const QString &source, const QString &destination) const; bool makeDirectory(const QString &path) const; bool makeTree(const QString &path) const; // 'removeDirectory(path)' implemented in "filesystem-shim.js" using '_removeDirectory(path)' diff --git a/test/fs-spec-03.js b/test/fs-spec-03.js index f29378eb..4e10613a 100644 --- a/test/fs-spec-03.js +++ b/test/fs-spec-03.js @@ -1,4 +1,4 @@ -describe("Paths Files API", function() { +describe("Files and Directories API", function() { var TEST_DIR = "testdir", TEST_FILE = "testfile", START_CWD = null; @@ -18,8 +18,16 @@ describe("Paths Files API", function() { expect(lastIndex + suffix.length === abs.length); }); - it("shoudl return to previous Current Working Directory and remove temporary directory", function() { + it("should return to previous Current Working Directory and remove temporary directory", function() { expect(fs.changeWorkingDirectory(START_CWD)).toBeTruthy(); fs.removeTree(TEST_DIR); }); + + it("should copy Current Working Directory tree in a temporary directory, compare with the original and remove", function() { + expect(fs.changeWorkingDirectory("../")).toBeTruthy(); + fs.copyTree(START_CWD, TEST_DIR); + expect(fs.list(START_CWD).length).toEqual(fs.list(TEST_DIR).length); + fs.removeTree(TEST_DIR); + expect(fs.changeWorkingDirectory(START_CWD)).toBeTruthy(); + }); }); \ No newline at end of file