/u, one of our long-standing Unix customizations

July 3, 2010

One of Unix's small problems is that it has no simple and universal way to refer to people's home directories, especially other people's home directories. Some people are about to pipe up about ~user, but that's not a universal way; support for it has to be added to every program that will use it (and in every context where they resolve file names), and inevitably there are programs where it is not supported at all or is not supported everywhere. Beyond that, it's the wrong solution for the same reason that handling globbing in each program instead of in shells is the wrong solution.

(You can see how non-universal ~user is these days by trying it in various graphical file open dialogs. And in fact there are places where ~user support simply cannot be added, such as #! lines in shell scripts; no Unix kernel is ever going to support '#!~cks/bin/dog'.)

If you want a universal solution to a problem related to filenames and paths in Unix, there is only one way you can do it: you have to put something in the filesystem, because that's the only common thing used by absolutely every program that uses files. And you want to put it in the filesystem because this instantly makes it accessible to everything that uses files, which on a Unix system is a lot of things.

(This is the same core idea behind /proc and so on, and behind Plan 9 sticking everything in the filesystem. The filesystem is the common Unix namespace, so if you want to expose something widely, you put it in the filesystem.)

So our solution to this problem is to have a directory called /u, which is set up so that /u/<user> gives you the user's real home directory regardless of just where it is. Our implementation uses symlinks because that's the simple approach for us, but you could equally well use the automounter. The name /u is picked partly because it is short and partly because it is relatively memorable.

Having a real /u directory turns out to have a host of practical benefits, just as you'd expect on a Unix system. Here's a couple. First, it is truly universal; in anything that refers to files, in any context, you now have convenient short way of referring to anyone's home directory, yours included. You no longer have to worry about whether a particular program supports ~, ~user, or $HOME; you just use /u/<user>.

(And yes, this includes #! lines in shell scripts, so you can have scripts that do '#!/u/cks/bin/python3' if you need to.)

Second, we've wound up rewriting user home directories in /etc/passwd to use this form, because doing so persuades any number of programs to embed this form in their automatically created user configuration files, which in turn means that they no longer throw a spanner if we move a user from one filesystem to another (Firefox used to be infamous for this). Arguably any program doing this is buggy, but we're pragmatists; doing this eliminates the bug for any program that is not excessively clever.

(Excessively clever programs will try to determine the real path of the user's home directory, which can be done in our implementation.)

PS: if you are going to do this, it turns out to be convenient to do this for all entries in /etc/passwd (or at least all entries with real home directories), not just the accounts of actual users.

Written on 03 July 2010.
« Returning to the era of 'duplicated' Ethernet addresses
A thesis: noisy email addresses are dead »

Page tools: View Source, Add Comment.
Login: Password:
Atom Syndication: Recent Comments.

Last modified: Sat Jul 3 23:49:00 2010
This dinky wiki is brought to you by the Insane Hackers Guild, Python sub-branch.