lightning-fs icon indicating copy to clipboard operation
lightning-fs copied to clipboard

pfs.rename deletes file if oldFilepath and newFilepath are equal

Open BrianHung opened this issue 5 years ago • 0 comments

This seems related to #23. If oldFilepath and newFilepath are equal, then the file is deleted; I'm not sure if this is the default node fs behavior, but we can see this is because the old file is deleted after the new file is inserted here:

  rename(oldFilepath, newFilepath) {
    let basename = path.basename(newFilepath);
    // Note: do both lookups before making any changes
    // so if lookup throws, we don't lose data (issue #23)
    // grab references
    let entry = this._lookup(oldFilepath);
    let destDir = this._lookup(path.dirname(newFilepath));
    // insert into new parent directory
    destDir.set(basename, entry);
    // remove from old parent directory
    this.unlink(oldFilepath)
  }

A simple fix would be to check for that condition, and return without doing anything:

  rename(oldFilepath, newFilepath) {
    if (oldFilepath == newFilepath)
      return;
    ...
  }

BrianHung avatar Mar 24 '20 21:03 BrianHung