diff --git a/src/filesystem/impls/filer/BlobUtils.js b/src/filesystem/impls/filer/BlobUtils.js index 5bf581c39..dbbd66ff0 100644 --- a/src/filesystem/impls/filer/BlobUtils.js +++ b/src/filesystem/impls/filer/BlobUtils.js @@ -28,11 +28,7 @@ define(function (require, exports, module) { paths[url] = filename; } - // Remove the cached BLOB URL for the given filename - function remove(filename) { - filename = decodePath(filename); - filename = Path.normalize(filename); - + function _remove(filename) { var url = blobURLs[filename]; // The first time a file is written, we won't have // a stale cache entry to clean up. @@ -46,6 +42,26 @@ define(function (require, exports, module) { URL.revokeObjectURL(url); } + // Remove the cached BLOB URL for the given file path, or files beneath + // this path if it is a dir. Returns all file paths that were removed. + function remove(path) { + path = decodePath(path); + path = Path.normalize(path); + var removed = []; + + // If this is a dir path, look for other paths entries below it + Object.keys(blobURLs).forEach(function(key) { + // If this filename matches exactly, or is a root path (i.e., other + // filenames begin with "/...", remove it. Otherwise just skip. + if(key === path || key.indexOf(path + "/") === 0) { + removed.push(key); + _remove(key); + } + }); + + return removed; + } + // Update the cached records for the given filename function rename(oldPath, newPath) { oldPath = decodePath(oldPath); diff --git a/src/filesystem/impls/filer/FilerFileSystem.js b/src/filesystem/impls/filer/FilerFileSystem.js index a458b8175..9a692fb13 100644 --- a/src/filesystem/impls/filer/FilerFileSystem.js +++ b/src/filesystem/impls/filer/FilerFileSystem.js @@ -347,35 +347,33 @@ define(function (require, exports, module) { }); } - function unlink(path, callback) { - path = decodePath(path); - - fs.stat(path, function(err, stats) { + function _rmfr(path, callback) { + // Regardless of whether we're passed a file or dir path, recursively delete it all. + fs.rm(path, {recursive: true}, function(err) { if (err) { callback(_mapError(err)); return; } - // Deal with dir vs. file - var fnName = stats.type === "DIRECTORY" ? 'rmdir' : 'unlink'; - fs[fnName](path, function(err) { - // TODO: deal with the symlink case (i.e., only remove cache - // item if file is really going away). - BlobUtils.remove(path); - - if(!err) { - BrambleEvents.triggerFileRemoved(path); - } - callback(_mapError(err)); + // TODO: deal with the symlink case (i.e., only remove cache + // item if file is really going away). + BlobUtils.remove(path).forEach(function(filename) { + BrambleEvents.triggerFileRemoved(filename); }); + + callback(); }); } + function unlink(path, callback) { + path = decodePath(path); + _rmfr(path, callback); + } + function moveToTrash(path, callback) { path = decodePath(path); - // TODO: do we want to support a .trash/ dir or the like? - unlink(path, callback); + _rmfr(path, callback); } function initWatchers(changeCallback, offlineCallback) {