isomorphic-git / lightning-fs

A lean and fast 'fs' for the browser
MIT License
476 stars 47 forks source link

pfs.rename deletes file if oldFilepath and newFilepath are equal #41

Open BrianHung opened 4 years ago

BrianHung commented 4 years ago

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;
    ...
  }