0

So I am implementing a filesystem FUSE. I would like to know in which directory I am located when calling a function (specifically "mkdir"). Is there a way to know that? Or a way to keep track of directories when the user uses the "cd" command? The "getcwd" function always returns the root directory.

bobthesnob
  • 83
  • 5
  • The FUSE API doesn't include any way to know the user's current directory. I think you'd need to track that in your user space application. – GandhiGandhi Apr 19 '21 at 13:56
  • @GandhiGandhi I just dont see how one would do that. I would have to know when the user calls the "cd" command then, wouldn't I? – bobthesnob Apr 19 '21 at 13:59

1 Answers1

1

The libfuse deamon is operating as root, and not in the context of the user operating on the mounted filesystem. This is why getcwd in your fuse code always returns the root directory. However, you do have access to some of the calling user's attributes, in particular their PID number, with fuse_get_context()->pid (see linux how to intercept unauthorized process access to the file,base with FUSE( cryfs or gocryptfs ))

Using the PID you can fetch the user's current directory by calling readlink() on /proc/$pid/cwd (see https://unix.stackexchange.com/questions/509420/get-working-directory-of-logged-in-users)

Oren Kishon
  • 509
  • 6
  • 8
  • Thank you. I kind of understand and have managed to use readlink() from the terminal. But I'm a little slow witted it seems. So may I ask how one would use it from inside a function? – bobthesnob Apr 19 '21 at 14:38
  • https://stackoverflow.com/questions/5525668/how-to-implement-readlink-to-find-the-path – Oren Kishon Apr 19 '21 at 14:53