Question

I'm playing a bit with xv6, a modern implementation of Unix version 6.

For my first hack, I wanted to implement the simple getcwd syscall, but I'm a bit lost as to which level of abstraction I should use.

  • Should I use the struct file interface?
  • Or maybe the struct inode interface?
  • For what matters, it seems it could even be implemented purely in userland.

I started implementing it with struct inode manipulations. My naive idea was to retrieve the proc->cwd, then readi() its second entry (..), scan it to retrieve my previous inum, and so on recursively until I hit the root.

Doesn't seem very performant, but that will fit for a first hack.

My problem though is that I need fs.c:iget() to retrieve a struct inode from the inums I get in the dirents. I've noticed that iget() is static in fs.c and not declared in defs.h which annoys me a bit, but I can't find the reason why.

So, this is my question. Why is it that iget() was deliberately hidden from the rest of the kernel?

Was it helpful?

Solution

Seems to me they were just pragmatic.

iget is used only by the directory manipulation routines. The directory manipulation routines are in fs.c.

As for the getcwd implementation. It would be much better if you follow the chdir syscall code. The path is there. You just need to store it, probably in a new field in the proc structure. Of course, if the path given is relative, you should append it to the current stored path.

OTHER TIPS

in my opinion answer to Your question is:

  1. it was insecure and non generic. (if You could access directly file via inode, without traversing dirent's, how would security be mantained? You need permission for a file as well as execute for parent directory)
  2. it was inconclusive to access file by inode (file with same inode might be in several directories, and inode number is unique just for given FS)

but perhaps I misunderstood You ? Regarding how to get cwd, I'm pretty certain You'd prefer pointer to solution so maybe this will help:

  1. what is being kept in u.u_dent.u_name ? (take a closer look at user.h)

You might want to take a look at these too:

http://lwn.net/Articles/254486/

Why can't files be manipulated by inode?

how to get directory name by inode value in c?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top