Commit f7fd161c authored by Ahmad Tarraf's avatar Ahmad Tarraf
Browse files

fix: rename overwrites existing destination (EPERM → remove + rename)

  gkfs_rename returned EPERM when the destination already existed,
  violating POSIX semantics that require rename(old, new) to atomically
  replace new. Fix: remove the destination first, then proceed with the
  rename.
parent 2e9fde42
Loading
Loading
Loading
Loading
Loading
+44 −42
Original line number Diff line number Diff line
@@ -610,17 +610,20 @@ gkfs_rename(const string& old_path, const string& new_path) {
            }
            return 0;
        }
        errno = EPERM;
        // Destination exists: remove it first (POSIX rename must atomically replace destination)
        auto is_dir_new = S_ISDIR(md_new->mode());
        auto rm_err = gkfs::rpc::forward_remove(new_path, is_dir_new,
                                                CTX->get_replicas(),
                                                md_new->size());
        if(rm_err) {
            errno = rm_err;
            return -1;
    } else {

        }
    }

    if(!md_old.value().target_path().empty()) {


        // the file is a renamed one, we need to get the metadata of the
        // original file. (There will be only one level)

        original_path = md_old.value().target_path();
        if(!S_ISLNK(md_old->mode())) {
            md_old = gkfs::utils::get_metadata(original_path, false);
@@ -656,7 +659,6 @@ gkfs_rename(const string& old_path, const string& new_path) {
                errno);
        return -1;
    }
    }
    fprintf(stderr, "GKFS_RENAME: success returning 0\n");
    return 0;
}