10

The shell can expand ~ to your home directory. $HOME usually has the same deal, but often you want to refer to the current users home directory from a context that may not support such expansion.

I have had config files where $HOME works but ~ doesn't and vice versa.

I would guess that fuse could provide something along these lines, something like /var/myself -> $HOME

With that I could place values in config files to point to things like /var/myself/backdrops/pornography/wtf/yarly.jpg

Is there something like this already? If not, are there good reasons for there not being something like this?

Michael Mrozek
  • 93,103
  • 40
  • 240
  • 233
Lerc
  • 203
  • 2
    This actually seems like a really good idea, and fuse could do this pretty trivially if I remember the interface correctly – Michael Mrozek Sep 09 '10 at 06:54

4 Answers4

6

I understand your concern but the answer is "no" there is not such thing.
The usual method is to ask the OS the user's home path, or get the $HOME variable.

All these options needs always some coding from the application. A lot of applications, like bash, offer the "alias" ~ (open(2) does not translate that).

Of course a vfs or a fuse module could be implemented to do this. Probably there is something to do that, I am going to ask that!

But is it really needed? You can use a workaround like:

Keymon
  • 763
  • I ask for the vfs tool: http://unix.stackexchange.com/questions/1811/is-there-a-linux-vfs-tool-that-allows-bind-a-directory-in-different-location-lik – Keymon Sep 09 '10 at 08:43
0

You can get the pid of requestin user and ask system of his/her home directory. So it is possible.

However I'm not sure if there is no SUID programs that would assume FS as static.

Edit:

struct fuse_context *ctx = fuse_get_context ();
struct passwd pwd, *ppwd;
char buffer[1024];
int status = getpwuid_r (ctx->ui, &pwd, buffer, sizeof(buffer), &ppwd);
if (status == 0) {
  if (ppwd == NULL) {
    // No record found
  } else {
    // Handle record
  }
} else {
    // Handle error
} 

The above code is not ideal but should do work for common cases.

manatwork
  • 31,277
0

One trick (on Linux, at least) would be to change directory to $HOME before running the app and then use /proc/self/cwd/... in the config file.

  • I like the hackishness of this but presumably it would run into problems it the process changes the current directory. I'd like a /proc/PID/iwd for initial working directory. – Lerc Sep 12 '10 at 11:26
  • ...and for that matter a /proc/PID/home – Lerc Sep 12 '10 at 11:32
  • @Lerc, if it's going in a config file it needs to be /proc/self not /proc/PID, since you can't know in advance what the PID is going to be. – Neil Mayhew Sep 12 '10 at 13:53
  • @Lerc, yes, if the process changes directory then it would fail, but typically programs read the config file once when they start up, and then cache the values in memory. It all depends on the program you are working with, and if the program doesn't offer enough flexibility, either on the command line or in the config file syntax, then you are stuck. – Neil Mayhew Sep 12 '10 at 13:56
  • Yeah, I meant /proc/PID/home to mean that all proc/PID dirs should support this and thus include /proc/self – Lerc Sep 13 '10 at 11:09
0

Most programs allow you to specify a path to the configuration file on the command line. So you could write a wrapper that takes a standard configuration file, filters it to substitute things like $HOME for the current user, and then passes the modified, temporary configuration file to the program.

  • If you do that then you've effectively taken the ability to specify a path to the config file away from the user. Might be doable if you get rally fancy with a wrapper exe. – Lerc Sep 13 '10 at 11:08