Skip to content

Commit b6ac6b7

Browse files
authored
fix(rename): don't accidentally file if new filename invalid (#26)
1 parent 34fac1b commit b6ac6b7

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

src/CacheFS.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -196,14 +196,16 @@ module.exports = class CacheFS {
196196
parent.delete(basename);
197197
}
198198
rename(oldFilepath, newFilepath) {
199-
// grab reference
199+
let basename = path.basename(newFilepath);
200+
// Note: do both lookups before making any changes
201+
// so if lookup throws, we don't lose data (issue #23)
202+
// grab references
200203
let entry = this._lookup(oldFilepath);
201-
// remove from parent directory
202-
this.unlink(oldFilepath)
204+
let destDir = this._lookup(path.dirname(newFilepath));
203205
// insert into new parent directory
204-
let dir = this._lookup(path.dirname(newFilepath));
205-
let basename = path.basename(newFilepath);
206-
dir.set(basename, entry);
206+
destDir.set(basename, entry);
207+
// remove from old parent directory
208+
this.unlink(oldFilepath)
207209
}
208210
stat(filepath) {
209211
return this._lookup(filepath).get(STAT);

0 commit comments

Comments
 (0)