hpath-0.8.0: Support for well-typed paths

Copyright© 2016 Julian Ospald
LicenseBSD3
MaintainerJulian Ospald <hasufell@posteo.de>
Stabilityexperimental
Portabilityportable
Safe HaskellNone
LanguageHaskell2010

HPath.IO

Contents

Description

This module provides high-level IO related file operations like copy, delete, move and so on. It only operates on Path Abs which guarantees us well-typed paths which are absolute.

Some functions are just path-safe wrappers around unix functions, others have stricter exception handling and some implement functionality that doesn't have a unix counterpart (like copyDirRecursive).

Some of these operations are due to their nature not atomic, which means they may do multiple syscalls which form one context. Some of them also have to examine the filetypes explicitly before the syscalls, so a reasonable decision can be made. That means the result is undefined if another process changes that context while the non-atomic operation is still happening. However, where possible, as few syscalls as possible are used and the underlying exception handling is kept.

Note: BlockDevice, CharacterDevice, NamedPipe and Socket are ignored by some of the more high-level functions (like easyCopy). For other functions (like copyFile), the behavior on these file types is unreliable/unsafe. Check the documentation of those functions for details.

Synopsis

Types

data RecursiveErrorMode Source #

The error mode for any recursive operation.

On FailEarly the whole operation fails immediately if any of the recursive sub-operations fail, which is sort of the default for IO operations.

On CollectFailures skips errors in the recursion and keeps on recursing. However all errors are collected in the RecursiveFailure error type, which is raised finally if there was any error.

Constructors

FailEarly 
CollectFailures 

data CopyMode Source #

The mode for copy and file moves. Overwrite mode is usually not very well defined, but is a convenience shortcut.

Constructors

Strict

fail if any target exists

Overwrite

overwrite targets

File copying

copyDirRecursive Source #

Arguments

:: Path Abs

copy contents of this source dir

-> Path Abs

to this full destination (parent dirs are not automatically created)

-> CopyMode 
-> RecursiveErrorMode 
-> IO () 

Copies the contents of a directory recursively to the given destination. Does not follow symbolic links. This behaves more or less like:

  mkdir /destination/dir
  cp -R /source/dir/* /destination/dir/

For directory contents, this will ignore any file type that is not RegularFile, SymbolicLink or Directory.

For Overwrite copy mode this does not prune destination directory contents, so the destination might contain more files than the source after the operation has completed. Permissions of existing directories are fixed.

Note that there is no guaranteed ordering of the exceptions contained within RecursiveFailure in CollectFailures RecursiveErrorMode.

Safety/reliability concerns:

  • not atomic
  • examines filetypes explicitly
  • an explicit check throwDestinationInSource is carried out for the top directory for basic sanity, because otherwise we might end up with an infinite copy loop... however, this operation is not carried out recursively (because it's slow)

Throws:

Throws in FailEarly RecursiveErrorMode only:

  • PermissionDenied if output directory is not writable
  • InvalidArgument if source directory is wrong type (symlink)
  • InappropriateType if source directory is wrong type (regular file)

Throws in CollectFailures RecursiveErrorMode only:

Throws in Strict CopyMode only:

  • AlreadyExists if destination already exists

recreateSymlink Source #

Arguments

:: Path Abs

the old symlink file

-> Path Abs

destination file

-> CopyMode 
-> IO () 

Recreate a symlink.

In Overwrite copy mode only files and empty directories are deleted.

Safety/reliability concerns:

Throws:

  • InvalidArgument if source file is wrong type (not a symlink)
  • PermissionDenied if output directory cannot be written to
  • PermissionDenied if source directory cannot be opened
  • SameFile if source and destination are the same file (HPathIOException)

Throws in Strict mode only:

  • AlreadyExists if destination file already exists

Throws in Overwrite mode only:

  • UnsatisfiedConstraints if destination file is non-empty directory

Note: calls symlink

copyFile Source #

Arguments

:: Path Abs

source file

-> Path Abs

destination file

-> CopyMode 
-> IO () 

Copies the given regular file to the given destination. Neither follows symbolic links, nor accepts them. For "copying" symbolic links, use recreateSymlink instead.

Note that this is still sort of a low-level function and doesn't examine file types. For a more high-level version, use easyCopy instead.

In Overwrite copy mode only overwrites actual files, not directories.

Safety/reliability concerns:

  • Overwrite mode is not atomic
  • when used on CharacterDevice, reads the "contents" and copies them to a regular file, which might take indefinitely
  • when used on BlockDevice, may either read the "contents" and copy them to a regular file (potentially hanging indefinitely) or may create a regular empty destination file
  • when used on NamedPipe, will hang indefinitely

Throws:

  • NoSuchThing if source file does not exist
  • NoSuchThing if source file is a a Socket
  • PermissionDenied if output directory is not writable
  • PermissionDenied if source directory can't be opened
  • InvalidArgument if source file is wrong type (symlink or directory)
  • SameFile if source and destination are the same file (HPathIOException)

Throws in Strict mode only:

  • AlreadyExists if destination already exists

Note: calls sendfile and possibly read/write as fallback

easyCopy :: Path Abs -> Path Abs -> CopyMode -> RecursiveErrorMode -> IO () Source #

Copies a regular file, directory or symbolic link. In case of a symbolic link it is just recreated, even if it points to a directory. Any other file type is ignored.

Safety/reliability concerns:

File deletion

deleteFile :: Path Abs -> IO () Source #

Deletes the given file. Raises eISDIR if run on a directory. Does not follow symbolic links.

Throws:

  • InappropriateType for wrong file type (directory)
  • NoSuchThing if the file does not exist
  • PermissionDenied if the directory cannot be read

deleteDir :: Path Abs -> IO () Source #

Deletes the given directory, which must be empty, never symlinks.

Throws:

  • InappropriateType for wrong file type (symlink to directory)
  • InappropriateType for wrong file type (regular file)
  • NoSuchThing if directory does not exist
  • UnsatisfiedConstraints if directory is not empty
  • PermissionDenied if we can't open or write to parent directory

Notes: calls rmdir

deleteDirRecursive :: Path Abs -> IO () Source #

Deletes the given directory recursively. Does not follow symbolic links. Tries deleteDir first before attemtping a recursive deletion.

On directory contents this behaves like easyDelete and thus will ignore any file type that is not RegularFile, SymbolicLink or Directory.

Safety/reliability concerns:

  • not atomic
  • examines filetypes explicitly

Throws:

  • InappropriateType for wrong file type (symlink to directory)
  • InappropriateType for wrong file type (regular file)
  • NoSuchThing if directory does not exist
  • PermissionDenied if we can't open or write to parent directory

easyDelete :: Path Abs -> IO () Source #

Deletes a file, directory or symlink. In case of directory, performs recursive deletion. In case of a symlink, the symlink file is deleted. Any other file type is ignored.

Safety/reliability concerns:

File opening

openFile :: Path Abs -> IO ProcessID Source #

Opens a file appropriately by invoking xdg-open. The file type is not checked. This forks a process.

executeFile Source #

Arguments

:: Path Abs

program

-> [ByteString]

arguments

-> IO ProcessID 

Executes a program with the given arguments. This forks a process.

File creation

createRegularFile :: FileMode -> Path Abs -> IO () Source #

Create an empty regular file at the given directory with the given filename.

Throws:

  • PermissionDenied if output directory cannot be written to
  • AlreadyExists if destination file already exists

createDir :: FileMode -> Path Abs -> IO () Source #

Create an empty directory at the given directory with the given filename.

Throws:

  • PermissionDenied if output directory cannot be written to
  • AlreadyExists if destination directory already exists

createSymlink Source #

Arguments

:: Path Abs

destination file

-> ByteString

path the symlink points to

-> IO () 

Create a symlink.

Throws:

  • PermissionDenied if output directory cannot be written to
  • AlreadyExists if destination file already exists

Note: calls symlink

File renaming/moving

renameFile :: Path Abs -> Path Abs -> IO () Source #

Rename a given file with the provided filename. Destination and source must be on the same device, otherwise eXDEV will be raised.

Does not follow symbolic links, but renames the symbolic link file.

Safety/reliability concerns:

  • has a separate set of exception handling, apart from the syscall

Throws:

  • NoSuchThing if source file does not exist
  • PermissionDenied if output directory cannot be written to
  • PermissionDenied if source directory cannot be opened
  • UnsupportedOperation if source and destination are on different devices
  • FileDoesExist if destination file already exists (HPathIOException)
  • DirDoesExist if destination directory already exists (HPathIOException)
  • SameFile if destination and source are the same file (HPathIOException)

Note: calls rename (but does not allow to rename over existing files)

moveFile Source #

Arguments

:: Path Abs

file to move

-> Path Abs

destination

-> CopyMode 
-> IO () 

Move a file. This also works across devices by copy-delete fallback. And also works on directories.

Does not follow symbolic links, but renames the symbolic link file.

Safety/reliability concerns:

Throws:

  • NoSuchThing if source file does not exist
  • PermissionDenied if output directory cannot be written to
  • PermissionDenied if source directory cannot be opened
  • SameFile if destination and source are the same file (HPathIOException)

Throws in Strict mode only:

Note: calls rename (but does not allow to rename over existing files)

File permissions

newFilePerms :: FileMode Source #

Default permissions for a new file.

newDirPerms :: FileMode Source #

Default permissions for a new directory.

Directory reading

getDirsFiles Source #

Arguments

:: Path Abs

dir to read

-> IO [Path Abs] 

Gets all filenames of the given directory. This excludes "." and "..". This version does not follow symbolic links.

The contents are not sorted and there is no guarantee on the ordering.

Throws:

  • NoSuchThing if directory does not exist
  • InappropriateType if file type is wrong (file)
  • InappropriateType if file type is wrong (symlink to file)
  • InappropriateType if file type is wrong (symlink to dir)
  • PermissionDenied if directory cannot be opened

Filetype operations

getFileType :: Path Abs -> IO FileType Source #

Get the file type of the file located at the given path. Does not follow symbolic links.

Throws:

  • NoSuchThing if the file does not exist
  • PermissionDenied if any part of the path is not accessible

Others

canonicalizePath :: Path Abs -> IO (Path Abs) Source #

Applies realpath on the given absolute path.

Throws:

  • NoSuchThing if the file at the given path does not exist
  • NoSuchThing if the symlink is broken