0

As a part of an alignement at university I have to modify the function unlink_file located in /usr/src/minix/fs/mfs/link.c, so (under certain conditions) instead of removing files it just changes their name.

I have parent directory's inode, file's inode and its name passed to the function as parameters:

static int unlink_file(dirp, rip, file_name)
struct inode *dirp;     /* parent directory of file */
struct inode *rip;      /* inode of file, may be NULL too. */
char file_name[MFS_NAME_MAX];   /* name of file to be removed */

I thought of using the rename(2) syscall (which implementation is located in the same file in the function fs_rename), but I need an absolute path of the file in order to do so. Unfortunately, I don't know how to retrieve it from inode structure.

My question is: how can I retrieve an absolute path to the file by its inode? Or is there another way to rename a file from inside the unlink_file function?

Mysquff
  • 293
  • 4
  • 11

1 Answers1

1

notice what fs_rename does with message and that it acquires pointer to inodes.

unlink_file already has pointer to inode of file and pointer to directory where file is located. If you only have to rename it, you can check how fs_rename() acts, when both old_dirp and new_dirp are same

same_pdir == (old_dip == new_dirp); //somewhere in fs_rename()
(bunch of error checks..)
if(same_pdir){
r = search_dir(old_dirp, old_name, NULL, DELETE, IGN_PERM); // this deletes the file from directory
if(r == OK)
(void)search_dir(old_dirp, new_name, &numb, ENTER, IGN_PERM); //this creates file with new_name in the directory
}

Keep in mind this part of code assumes no file with name new_name currently exists in the directory (as in the error checks I've skipped such file gets removed)

Blomex
  • 305
  • 2
  • 12