Skip to content

Commit

Permalink
removed rename syscall code
Browse files Browse the repository at this point in the history
  • Loading branch information
pranav-bhatt committed Aug 8, 2024
1 parent 0666f58 commit 7ea5a5c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 115 deletions.
90 changes: 22 additions & 68 deletions src/safeposix/syscalls/fs_calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4704,38 +4704,6 @@ impl Cage {
}

//------------------RENAME SYSCALL------------------
/// ### Description
///
/// `rename_syscall` renames a file or directory specified by `oldpath` to
/// `newpath`.
///
/// ### Arguments
///
/// It accepts two parameters:
/// * `oldpath` - A string slice that specifies the current name and
/// location of the file or directory.
/// * `newpath` - A string slice that specifies the new name and location
/// for the file or directory.
///
/// ### Returns
///
/// For a successful call, the return value will be 0, indicating that the
/// rename operation was successful. On error, a negative errno is returned
/// to indicate the error.
///
/// ### Errors
///
/// * `ENOENT` - The `oldpath` or `newpath` is null, i.e. not provided.
/// * `EEXIST` - The `oldpath` does not exist.
/// * `EBUSY` - Cannot rename the root directory.
/// * `EOPNOTSUPP` - Cannot move the file or directory to another directory.
///
/// ### Panics
///
/// * There are no known panics in this function.
///
/// For more detailed description of all the commands and return values,
/// refer to the rename syscall man page [here](https://man7.org/linux/man-pages/man2/rename.2.html).

pub fn rename_syscall(&self, oldpath: &str, newpath: &str) -> i32 {
if oldpath.len() == 0 {
Expand All @@ -4758,54 +4726,40 @@ impl Cage {
// make sure file is not moved to another dir
// get inodenum for parent of new path
let (_, new_par_inodenum) = metawalkandparent(true_newpath.as_path());
// check if old and new paths share parent
if new_par_inodenum != Some(parent_inodenum) {
return syscall_error(
Errno::EOPNOTSUPP,
"rename",
"Cannot move file to another directory",
);
}

// get parent directory inode object of old path
let old_pardir_inodeobj = FS_METADATA.inodetable.get_mut(&parent_inodenum).unwrap();
let pardir_inodeobj = FS_METADATA.inodetable.get_mut(&parent_inodenum).unwrap();
if let Inode::Dir(parent_dir) = &*pardir_inodeobj {
// add pair of new path and its inodenum to filename-inode dict
parent_dir.filename_to_inode_dict.insert(
true_newpath
.file_name()
.unwrap()
.to_str()
.unwrap()
.to_string(),
inodenum,
);

if let Inode::Dir(old_parent_dir) = &*old_pardir_inodeobj {
// remove entry of old path from filename-inode dict
old_parent_dir.filename_to_inode_dict.remove(
parent_dir.filename_to_inode_dict.remove(
&true_oldpath
.file_name()
.unwrap()
.to_str()
.unwrap()
.to_string(),
);

// // drop the ref to the old parent dir inode object
drop(old_pardir_inodeobj);

drop(pardir_inodeobj);
log_metadata(&FS_METADATA, parent_inodenum);

}

if let Some(new_parent_inodenum) = new_par_inodenum {
let new_pardir_inodeobj = FS_METADATA.inodetable.get_mut(&new_parent_inodenum).unwrap();

if let Inode::Dir(new_parent_dir) = &*new_pardir_inodeobj {
// add pair of new path and its inodenum to filename-inode dict
new_parent_dir.filename_to_inode_dict.insert(
true_newpath
.file_name()
.unwrap()
.to_str()
.unwrap()
.to_string(),
inodenum,
);

// drop the ref to the new parent dir inode object
drop(new_pardir_inodeobj);

// log and update metadata
log_metadata(&FS_METADATA, new_parent_inodenum);
}
} else {
return syscall_error(Errno::ENOENT, "rename", "New parent path does not exist");
}

// update the NET_METADATA
NET_METADATA.domsock_paths.insert(true_newpath);
NET_METADATA.domsock_paths.remove(&true_oldpath);
0 // success
Expand Down
47 changes: 0 additions & 47 deletions src/tests/fs_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4650,51 +4650,4 @@ pub mod fs_tests {
lindrustfinalize();
return;
}

#[test]
pub fn ut_lind_fs_rename_syscall_tests() {
// acquiring a lock on TESTMUTEX prevents other tests from running concurrently,
// and also performs clean env setup
let _thelock = setup::lock_and_init();

let cage = interface::cagetable_getref(1);

// test out whether an error is output for a non existent file path
// (EEXIST[-17])
assert_eq!(
cage.rename_syscall("non_existent_file_path", "non-existent-target"),
syscall_error(Errno::EEXIST, "rename", "test_failure")
);

// empty inputs for rename
assert_eq!(
cage.rename_syscall("", ""),
syscall_error(Errno::ENOENT, "rename", "empty inputs")
);

// root path provided
assert_eq!(
cage.rename_syscall("/", "rename-to-this"),
syscall_error(Errno::EBUSY, "rename", "cant deal with root")
);

// setting up inode object "/tmp/generic" for testing statfs_syscall
let generic_path = "/tmp/generic";
let creat_fd = cage.creat_syscall(generic_path, S_IRWXA);
assert!(creat_fd > 0);

// try to move to different parent
assert_eq!(cage.mkdir_syscall("/tmp2", S_IRWXA),0);
assert_eq!(
cage.rename_syscall("/tmp/generic", "/tmp2/generic"), 0
);
// assert_eq!(cage.access_syscall("/tmp2/generic", F_OK),0);

// // rename file
// assert_eq!(cage.rename_syscall("/tmp2/generic", "/tmp2/generic1"), 0);

lindrustfinalize();

return;
}
}

0 comments on commit 7ea5a5c

Please sign in to comment.