Merge pull request #151 from detro/dev-commonjs_fileio

Last API
1.3
Ariya Hidayat 2011-09-04 21:19:15 -07:00
commit 7cd76309e6
6 changed files with 76 additions and 28 deletions

View File

@ -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.
*

View File

@ -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

View File

@ -72,33 +72,31 @@ 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;
// 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 "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;

View File

@ -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]);

View File

@ -4,7 +4,7 @@
<file>coffee-script.js</file>
<file>usage.txt</file>
<file>webpage-shim.js</file>
<file>fs-shim.js</file>
<file>filesystem-shim.js</file>
<file>configurator.js</file>
</qresource>
</RCC>

View File

@ -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();
});
});