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

Haskell: copyFile: Copy file

System.Directory module provides copyFile function, used to copy file from old location to new location. If the new file exists, the old file replaces it. Following is the signature of copyFile function.

Prelude System.Directory> :t copyFile
copyFile :: FilePath -> FilePath -> IO ()


DirectoryUtil.hs
import System.IO
import System.Directory

main = do
    putStrLn "Enter file path to copy"
    oldFile <- getLine

    putStrLn "Enter new name"
    newFile <- getLine

    copyFile oldFile newFile
    putStrLn "File copied"

$ ls -l /Users/harikrishna_gurram/aca*log
-rw-r--r--  1 harikrishna_gurram  SYMC\Domain Users  1911 Apr  2 13:37 /Users/harikrishna_gurram/academyLogin.log
$
$ runghc DirectoryUtil.hs 
Enter file path to copy
/Users/harikrishna_gurram/academyLogin.log
Enter new name
/Users/harikrishna_gurram/academyLogin_backup.log 
File copied
$ 
$ ls -l /Users/harikrishna_gurram/aca*log
-rw-r--r--  1 harikrishna_gurram  SYMC\Domain Users  1911 Apr  2 13:37 /Users/harikrishna_gurram/academyLogin.log
-rw-r--r--  1 harikrishna_gurram  SYMC\Domain Users  1911 Apr  6 14:58 /Users/harikrishna_gurram/academyLogin_backup.log



Previous                                                 Next                                                 Home

Haskell: renameFile: Rename file

System.Directory module provides renameFile function, which is used to rename a file. Following is the signature of renameFile function.

Prelude System.Directory> :t renameFile
renameFile :: FilePath -> FilePath -> IO ()

Example
renameFile oldFile newFile
If the newFile already exists, it is atomically replaced by the oldFile.

You may get following errors while working with renameFile function.

Error
Description
HardwareFault
A physical I/O error has occurred.
InvalidArgument
Either operand is not a valid file name.
isDoesNotExistError / NoSuchThing
The original file does not exist, or there is no path to the target.
isPermissionError / PermissionDenied
The process has insufficient privileges to perform the operation.
ResourceExhausted
Insufficient resources are available to perform the operation.
UnsatisfiedConstraints
Implementation-dependent constraints are not satisfied.
UnsupportedOperation
The implementation does not support renaming in this situation.
InappropriateType
Either path refers to an existing directory.


DirectoryUtil.hs
import System.IO
import System.Directory

main = do
    putStrLn "Enter file path to rename"
    oldFile <- getLine

    putStrLn "Enter new name for the file"
    newFile <- getLine

    renameFile oldFile newFile
    putStrLn "File renamed"

$  ls -l /Users/harikrishna_gurram/admin.log
-rw-r--r--  1 harikrishna_gurram  ABC Users  2367 Apr  2 13:38 /Users/harikrishna_gurram/admin.log
$ 
$ runghc DirectoryUtil.hs 
Enter file path to rename
/Users/harikrishna_gurram/admin.log
Enter new name for the file
/Users/harikrishna_gurram/admin_tmp.log
File renamed
$ 
$  ls -l /Users/harikrishna_gurram/admin.log
ls: /Users/harikrishna_gurram/admin.log: No such file or directory
$
$  ls -l /Users/harikrishna_gurram/admin_tmp.log
-rw-r--r--  1 harikrishna_gurram  ABC Users  2367 Apr  2 13:38 /Users/harikrishna_gurram/admin_tmp.log




Previous                                                 Next                                                 Home