Monday, 30 May 2016

Haskell: canonicalizePath: Replace ., .. with actual path


Haskell provides canonicalizePath function, which removes the indirections (. Represents current directory, .. represents parent directory, symbolic links) from the path. Following is the signature of canonicalizePath.
Prelude System.Directory> :t canonicalizePath 
canonicalizePath :: FilePath -> IO FilePath
Prelude System.Directory> canonicalizePath "."
"/Users/harikrishna_gurram"
Prelude System.Directory> 
Prelude System.Directory> canonicalizePath ".."
"/Users"
Prelude System.Directory> 
Prelude System.Directory> canonicalizePath "./Shared"
"/Users/harikrishna_gurram/shared"


As I said it resolves symbolic links also.
$ ls -l /Users/harikrishna_gurram/playerLogin.log
-rw-r--r--  1 harikrishna_gurram  ABC Users  0 Mar 10 10:23 /Users/harikrishna_gurram/playerLogin.log
$
$ mkdir logs
$ 
$ ln -s /Users/harikrishna_gurram/playerLogin.log /Users/harikrishna_gurram/logs/playerLogin_tmp.log
$ 
$ ls -l /Users/harikrishna_gurram/playerLogin.log
-rw-r--r--  1 harikrishna_gurram  ABC Users  0 Mar 10 10:23 /Users/harikrishna_gurram/playerLogin.log
$ 
$ ls -l /Users/harikrishna_gurram/logs/playerLogin_tmp.log 
lrwxr-xr-x  1 harikrishna_gurram  ABC Users  41 Apr  6 16:56 /Users/harikrishna_gurram/logs/playerLogin_tmp.log -> /Users/harikrishna_gurram/playerLogin.log

Observe above snippet, “/Users/harikrishna_gurram/logs/playerLogin_tmp.log” is a symbolic link to /Users/harikrishna_gurram/playerLogin.log.

Prelude System.Directory> canonicalizePath "./logs/playerLogin_tmp.log"
"/Users/harikrishna_gurram/playerLogin.log"


Observe above snippet, canonicalizePath resolve the symbolic link.



Previous                                                 Next                                                 Home

No comments:

Post a Comment