makeRelative

Jon Fairbairn jon.fairbairn at cl.cam.ac.uk
Sun Sep 30 11:36:18 CEST 2012


Herbert Valerio Riedel <hvr at gnu.org> writes:

> 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

Ah, well spotted. In my use case I happen to know the structure
of the directories, so hadn’t considered that symlinks can make
it impossible to do purely. Given that in System.Directory there
is

 makeRelativeToCurrentDirectory:: FilePath -> IO FilePath,

we should probably have a function in there 

 makeRelativeToPath:: FilePath -> FilePath -> IO FilePath.

and do it properly. Whether this should be

 makeRelativeToPath:: FilePath -> FilePath -> IO (Maybe
 FilePath)

(cf Ian Lynagh’s post) is another argument. I don’t like relying
on exceptions in general, but once we’re in the IO monad (where
exceptions seem to be used willy-nilly), throwing something when
no relative path is possible seems not entirely unreasonable.

-- 
Jón Fairbairn                                 Jon.Fairbairn at cl.cam.ac.uk





More information about the Libraries mailing list