makeRelative

Herbert Valerio Riedel hvr at gnu.org
Fri Sep 28 22:16:34 CEST 2012


Jon Fairbairn <jon.fairbairn at cl.cam.ac.uk> writes:

> So I want
>
> makeRelative "/var/www/this_server/foo/bar/" "/var/www/this_server/bob"
>
> to return "../../bob" 

[...]

> Is there any reason that System.FilePath.Posix.makeRelative should not
> do this? I don’t know enough about non-posix filesystems to know
> whether it even makes sense elsewhere.

I don't know whether this might the reason (or whether it is relevant at
all for the API at hand) but you can't implement this function properly
w/o turning it into an IO action, since you need to be able to
introspect the filesystem, because under POSIX at least, you can easily
construct a filesystem, which exhibits the following confusing
behaviour:

,----
| /tmp/xxx$ pwd
| /tmp/xxx
| /tmp/xxx$ ls -l bar
| total 0
| -rw-rw-r-- 1 hvr hvr 0 Sep 28 22:03 this_is_in_xxx
| /tmp/xxx$ cd bar
| /tmp/xxx/bar$ ls -l
| total 0
| -rw-rw-r-- 1 hvr hvr 0 Sep 28 22:03 this_is_in_xxx
| /tmp/xxx/bar$ ls -l ../bar
| total 0
| -rw-rw-r-- 1 hvr hvr 0 Sep 28 22:03 this_is_in_xxx
| /tmp/xxx/bar$ cd ..
| /tmp/xxx$ cd zoo
| /tmp/xxx/zoo$ pwd
| /tmp/xxx/zoo
| /tmp/xxx/zoo$ ls -l ../bar
| total 0
| -rw-rw-r-- 1 hvr hvr 0 Sep 28 22:03 this_is_in_foo
`----

this is simply because '/tmp/xxx/zoo' is actually a symlink pointing to
a different folder '/tmp/foo/zoo', whose parent contains a different
'bar' folder. I.e.:

,----
| $ ls -lR /tmp/foo /tmp/xxx
| /tmp/foo:
| total 0
| drwxrwxr-x 2 hvr hvr 60 Sep 28 22:03 bar
| drwxrwxr-x 2 hvr hvr 40 Sep 28 22:02 zoo
| 
| /tmp/foo/bar:
| total 0
| -rw-rw-r-- 1 hvr hvr 0 Sep 28 22:03 this_is_in_foo
| 
| /tmp/foo/zoo:
| total 0
| 
| /tmp/xxx:
| total 0
| drwxrwxr-x 2 hvr hvr 60 Sep 28 22:03 bar
| lrwxrwxrwx 1 hvr hvr 10 Sep 28 22:04 zoo -> ../foo/zoo
| 
| /tmp/xxx/bar:
| total 0
| -rw-rw-r-- 1 hvr hvr 0 Sep 28 22:03 this_is_in_xxx
`----

thus,

| makeRelative "/tmp/xxx/zoo" "/tmp/xxx/bar"

would return "../bar" which when passed to filesystem related
system-calls (assuming CWD is /tmp/xxx/zoo) wouldn't point to
"/tmp/xxx/bar" but rather to "/tmp/foo/bar" instead.

cheers,
  hvr



More information about the Libraries mailing list