2016-05-09 12:40:30 +00:00
|
|
|
-- |
|
|
|
|
-- Module : HPath.IO
|
|
|
|
-- Copyright : © 2016 Julian Ospald
|
2016-06-03 22:20:41 +00:00
|
|
|
-- License : BSD3
|
2016-05-09 12:40:30 +00:00
|
|
|
--
|
|
|
|
-- Maintainer : Julian Ospald <hasufell@posteo.de>
|
|
|
|
-- Stability : experimental
|
|
|
|
-- Portability : portable
|
|
|
|
--
|
2016-05-09 14:53:31 +00:00
|
|
|
-- This module provides high-level IO related file operations like
|
2018-04-10 22:44:47 +00:00
|
|
|
-- copy, delete, move and so on. It only operates on /Path x/ which
|
2020-01-26 20:49:34 +00:00
|
|
|
-- guarantees us well-typed paths. This is a thin wrapper over
|
|
|
|
-- System.Posix.RawFilePath.Directory in 'hpath-directory'. It's
|
|
|
|
-- encouraged to use this module.
|
2016-05-09 14:53:31 +00:00
|
|
|
--
|
2016-05-09 15:37:16 +00:00
|
|
|
-- Some of these operations are due to their nature __not atomic__, which
|
2016-05-09 14:53:31 +00:00
|
|
|
-- 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.
|
2016-05-09 15:37:16 +00:00
|
|
|
--
|
|
|
|
-- Note: `BlockDevice`, `CharacterDevice`, `NamedPipe` and `Socket`
|
2016-05-31 14:21:14 +00:00
|
|
|
-- 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.
|
2016-05-09 12:40:30 +00:00
|
|
|
|
2020-05-08 21:46:39 +00:00
|
|
|
{-# LANGUAGE FlexibleContexts #-} -- streamly
|
|
|
|
{-# LANGUAGE PackageImports #-}
|
2016-05-09 14:53:31 +00:00
|
|
|
|
2016-05-09 15:37:16 +00:00
|
|
|
module HPath.IO
|
|
|
|
(
|
|
|
|
-- * Types
|
|
|
|
FileType(..)
|
2016-06-05 14:07:46 +00:00
|
|
|
, RecursiveErrorMode(..)
|
2016-06-05 01:10:28 +00:00
|
|
|
, CopyMode(..)
|
2016-05-09 15:37:16 +00:00
|
|
|
-- * File copying
|
|
|
|
, copyDirRecursive
|
|
|
|
, recreateSymlink
|
|
|
|
, copyFile
|
|
|
|
, easyCopy
|
|
|
|
-- * File deletion
|
|
|
|
, deleteFile
|
|
|
|
, deleteDir
|
|
|
|
, deleteDirRecursive
|
|
|
|
, easyDelete
|
|
|
|
-- * File opening
|
|
|
|
, openFile
|
|
|
|
, executeFile
|
|
|
|
-- * File creation
|
|
|
|
, createRegularFile
|
|
|
|
, createDir
|
2020-01-18 17:45:17 +00:00
|
|
|
, createDirIfMissing
|
2016-06-12 23:28:55 +00:00
|
|
|
, createDirRecursive
|
2016-05-29 15:28:12 +00:00
|
|
|
, createSymlink
|
2016-05-09 15:37:16 +00:00
|
|
|
-- * File renaming/moving
|
|
|
|
, renameFile
|
|
|
|
, moveFile
|
2018-04-06 14:42:40 +00:00
|
|
|
-- * File reading
|
|
|
|
, readFile
|
2020-01-13 22:13:21 +00:00
|
|
|
, readFileStream
|
2018-04-06 15:22:22 +00:00
|
|
|
-- * File writing
|
|
|
|
, writeFile
|
2020-01-18 17:45:17 +00:00
|
|
|
, writeFileL
|
2018-04-06 15:22:22 +00:00
|
|
|
, appendFile
|
2016-05-09 15:37:16 +00:00
|
|
|
-- * File permissions
|
2020-01-26 21:40:03 +00:00
|
|
|
, RD.newFilePerms
|
|
|
|
, RD.newDirPerms
|
2020-01-13 22:12:55 +00:00
|
|
|
-- * File checks
|
|
|
|
, doesExist
|
|
|
|
, doesFileExist
|
|
|
|
, doesDirectoryExist
|
2020-01-18 17:45:17 +00:00
|
|
|
, isReadable
|
2020-01-13 22:12:55 +00:00
|
|
|
, isWritable
|
2020-01-18 17:45:17 +00:00
|
|
|
, isExecutable
|
2020-01-13 22:12:55 +00:00
|
|
|
, canOpenDirectory
|
2020-01-18 17:45:17 +00:00
|
|
|
-- * File times
|
|
|
|
, getModificationTime
|
|
|
|
, setModificationTime
|
|
|
|
, setModificationTimeHiRes
|
2016-05-09 15:37:16 +00:00
|
|
|
-- * Directory reading
|
|
|
|
, getDirsFiles
|
2020-01-18 17:45:17 +00:00
|
|
|
, getDirsFiles'
|
2020-05-08 21:46:39 +00:00
|
|
|
, getDirsFilesStream
|
2016-05-09 15:37:16 +00:00
|
|
|
-- * Filetype operations
|
|
|
|
, getFileType
|
|
|
|
-- * Others
|
|
|
|
, canonicalizePath
|
2018-04-10 22:44:47 +00:00
|
|
|
, toAbs
|
2020-01-18 17:45:17 +00:00
|
|
|
, withRawFilePath
|
|
|
|
, withHandle
|
2020-01-26 20:49:34 +00:00
|
|
|
, module System.Posix.RawFilePath.Directory.Errors
|
2016-05-09 15:37:16 +00:00
|
|
|
)
|
2020-01-26 21:40:03 +00:00
|
|
|
where
|
2016-05-09 14:53:31 +00:00
|
|
|
|
|
|
|
|
2020-05-08 21:46:39 +00:00
|
|
|
import Control.Exception.Safe ( MonadMask
|
|
|
|
, MonadCatch
|
|
|
|
, bracketOnError
|
2020-01-26 21:40:03 +00:00
|
|
|
, finally
|
|
|
|
)
|
|
|
|
import Control.Monad.Catch ( MonadThrow(..) )
|
2016-05-09 14:53:31 +00:00
|
|
|
|
2020-01-26 21:40:03 +00:00
|
|
|
import Data.ByteString ( ByteString )
|
|
|
|
import Data.Traversable ( for )
|
|
|
|
import qualified Data.ByteString.Lazy as L
|
|
|
|
import Data.Time.Clock
|
|
|
|
import Data.Time.Clock.POSIX ( POSIXTime )
|
|
|
|
import HPath
|
|
|
|
import Prelude hiding ( appendFile
|
|
|
|
, readFile
|
|
|
|
, writeFile
|
|
|
|
)
|
|
|
|
import Streamly
|
|
|
|
import qualified System.IO as SIO
|
|
|
|
import System.Posix.Directory.ByteString
|
|
|
|
( getWorkingDirectory )
|
|
|
|
import qualified "unix" System.Posix.IO.ByteString
|
|
|
|
as SPI
|
|
|
|
import System.Posix.FD ( openFd )
|
|
|
|
import System.Posix.RawFilePath.Directory.Errors
|
|
|
|
import System.Posix.Types ( FileMode
|
|
|
|
, ProcessID
|
|
|
|
, EpochTime
|
|
|
|
)
|
|
|
|
import qualified System.Posix.RawFilePath.Directory
|
|
|
|
as RD
|
|
|
|
import System.Posix.RawFilePath.Directory
|
|
|
|
( FileType
|
|
|
|
, RecursiveErrorMode
|
|
|
|
, CopyMode
|
|
|
|
)
|
2016-05-09 14:53:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
--------------------
|
|
|
|
--[ File Copying ]--
|
|
|
|
--------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-06-05 20:19:30 +00:00
|
|
|
-- |Copies the contents of a directory recursively to the given destination, while preserving permissions.
|
|
|
|
-- Does not follow symbolic links. This behaves more or less like
|
|
|
|
-- the following, without descending into the destination if it
|
|
|
|
-- already exists:
|
2016-06-05 14:31:08 +00:00
|
|
|
--
|
|
|
|
-- @
|
2016-06-05 20:19:30 +00:00
|
|
|
-- cp -a \/source\/dir \/destination\/somedir
|
2016-06-05 14:31:08 +00:00
|
|
|
-- @
|
2016-05-09 14:53:31 +00:00
|
|
|
--
|
2016-06-05 13:57:41 +00:00
|
|
|
-- For directory contents, this will ignore any file type that is not
|
|
|
|
-- `RegularFile`, `SymbolicLink` or `Directory`.
|
2016-05-31 14:21:14 +00:00
|
|
|
--
|
2016-06-05 14:38:54 +00:00
|
|
|
-- 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.
|
2016-06-05 01:10:28 +00:00
|
|
|
--
|
2016-05-09 14:53:31 +00:00
|
|
|
-- 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:
|
|
|
|
--
|
|
|
|
-- - `NoSuchThing` if source directory does not exist
|
|
|
|
-- - `PermissionDenied` if source directory can't be opened
|
2016-06-05 13:57:41 +00:00
|
|
|
-- - `SameFile` if source and destination are the same file
|
|
|
|
-- (`HPathIOException`)
|
|
|
|
-- - `DestinationInSource` if destination is contained in source
|
|
|
|
-- (`HPathIOException`)
|
2016-06-05 01:10:28 +00:00
|
|
|
--
|
2016-06-05 14:07:46 +00:00
|
|
|
-- Throws in `FailEarly` RecursiveErrorMode only:
|
2016-06-05 01:10:28 +00:00
|
|
|
--
|
2016-06-05 13:57:41 +00:00
|
|
|
-- - `PermissionDenied` if output directory is not writable
|
|
|
|
-- - `InvalidArgument` if source directory is wrong type (symlink)
|
|
|
|
-- - `InappropriateType` if source directory is wrong type (regular file)
|
2016-06-05 01:10:28 +00:00
|
|
|
--
|
2016-06-05 14:07:46 +00:00
|
|
|
-- Throws in `CollectFailures` RecursiveErrorMode only:
|
2016-06-05 01:10:28 +00:00
|
|
|
--
|
2016-06-05 13:57:41 +00:00
|
|
|
-- - `RecursiveFailure` if any of the recursive operations that are not
|
2016-06-05 14:00:15 +00:00
|
|
|
-- part of the top-directory sanity-checks fail (`HPathIOException`)
|
2016-06-05 13:57:41 +00:00
|
|
|
--
|
|
|
|
-- Throws in `Strict` CopyMode only:
|
|
|
|
--
|
2016-06-05 01:10:28 +00:00
|
|
|
-- - `AlreadyExists` if destination already exists
|
2018-04-12 12:28:37 +00:00
|
|
|
--
|
|
|
|
-- Note: may call `getcwd` (only if destination is a relative path)
|
2018-04-10 22:44:47 +00:00
|
|
|
copyDirRecursive :: Path b1 -- ^ source dir
|
|
|
|
-> Path b2 -- ^ destination (parent dirs
|
|
|
|
-- are not automatically created)
|
2016-06-05 01:10:28 +00:00
|
|
|
-> CopyMode
|
2016-06-05 14:07:46 +00:00
|
|
|
-> RecursiveErrorMode
|
2016-05-09 14:53:31 +00:00
|
|
|
-> IO ()
|
2020-01-26 21:40:03 +00:00
|
|
|
copyDirRecursive (Path fromp) (Path destdirp) cm rm =
|
|
|
|
RD.copyDirRecursive fromp destdirp cm rm
|
2016-05-09 14:53:31 +00:00
|
|
|
|
|
|
|
|
2016-06-05 01:10:28 +00:00
|
|
|
-- |Recreate a symlink.
|
2016-05-09 14:53:31 +00:00
|
|
|
--
|
2016-06-05 14:07:46 +00:00
|
|
|
-- In `Overwrite` copy mode only files and empty directories are deleted.
|
2016-05-31 14:21:14 +00:00
|
|
|
--
|
2016-06-05 01:10:28 +00:00
|
|
|
-- Safety/reliability concerns:
|
2016-05-09 14:53:31 +00:00
|
|
|
--
|
2016-06-05 01:10:28 +00:00
|
|
|
-- * `Overwrite` mode is inherently non-atomic
|
2016-05-09 14:53:31 +00:00
|
|
|
--
|
|
|
|
-- Throws:
|
|
|
|
--
|
2016-05-31 14:21:14 +00:00
|
|
|
-- - `InvalidArgument` if source file is wrong type (not a symlink)
|
2016-05-09 14:53:31 +00:00
|
|
|
-- - `PermissionDenied` if output directory cannot be written to
|
|
|
|
-- - `PermissionDenied` if source directory cannot be opened
|
2016-06-05 13:57:41 +00:00
|
|
|
-- - `SameFile` if source and destination are the same file
|
|
|
|
-- (`HPathIOException`)
|
2016-05-09 14:53:31 +00:00
|
|
|
--
|
2016-06-05 01:22:11 +00:00
|
|
|
--
|
|
|
|
-- Throws in `Strict` mode only:
|
|
|
|
--
|
2016-06-14 17:13:25 +00:00
|
|
|
-- - `AlreadyExists` if destination already exists
|
2016-06-05 01:22:11 +00:00
|
|
|
--
|
|
|
|
-- Throws in `Overwrite` mode only:
|
|
|
|
--
|
|
|
|
-- - `UnsatisfiedConstraints` if destination file is non-empty directory
|
|
|
|
--
|
2018-04-12 12:28:37 +00:00
|
|
|
-- Notes:
|
|
|
|
--
|
|
|
|
-- - calls `symlink`
|
|
|
|
-- - calls `getcwd` in Overwrite mode (if destination is a relative path)
|
2018-04-10 22:44:47 +00:00
|
|
|
recreateSymlink :: Path b1 -- ^ the old symlink file
|
|
|
|
-> Path b2 -- ^ destination file
|
2016-06-05 01:10:28 +00:00
|
|
|
-> CopyMode
|
2016-05-09 14:53:31 +00:00
|
|
|
-> IO ()
|
2020-01-26 21:40:03 +00:00
|
|
|
recreateSymlink (Path symsourceBS) (Path newsymBS) cm =
|
|
|
|
RD.recreateSymlink symsourceBS newsymBS cm
|
2016-05-09 14:53:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
-- |Copies the given regular file to the given destination.
|
|
|
|
-- Neither follows symbolic links, nor accepts them.
|
|
|
|
-- For "copying" symbolic links, use `recreateSymlink` instead.
|
|
|
|
--
|
2016-05-31 14:21:14 +00:00
|
|
|
-- 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.
|
|
|
|
--
|
2016-06-05 14:07:46 +00:00
|
|
|
-- In `Overwrite` copy mode only overwrites actual files, not directories.
|
2019-12-29 19:03:28 +00:00
|
|
|
-- In `Strict` mode the destination file must not exist.
|
2016-06-05 01:10:28 +00:00
|
|
|
--
|
2016-05-31 14:21:14 +00:00
|
|
|
-- Safety/reliability concerns:
|
|
|
|
--
|
2016-06-05 01:10:28 +00:00
|
|
|
-- * `Overwrite` mode is not atomic
|
2016-05-31 14:21:14 +00:00
|
|
|
-- * 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
|
|
|
|
--
|
2016-05-09 14:53:31 +00:00
|
|
|
-- Throws:
|
|
|
|
--
|
|
|
|
-- - `NoSuchThing` if source file does not exist
|
2016-05-31 14:21:14 +00:00
|
|
|
-- - `NoSuchThing` if source file is a a `Socket`
|
2016-05-09 14:53:31 +00:00
|
|
|
-- - `PermissionDenied` if output directory is not writable
|
|
|
|
-- - `PermissionDenied` if source directory can't be opened
|
2016-05-31 14:21:14 +00:00
|
|
|
-- - `InvalidArgument` if source file is wrong type (symlink or directory)
|
2016-06-05 13:57:41 +00:00
|
|
|
-- - `SameFile` if source and destination are the same file
|
|
|
|
-- (`HPathIOException`)
|
2016-05-09 14:53:31 +00:00
|
|
|
--
|
2016-06-05 01:22:11 +00:00
|
|
|
-- Throws in `Strict` mode only:
|
|
|
|
--
|
|
|
|
-- - `AlreadyExists` if destination already exists
|
|
|
|
--
|
2018-04-12 12:28:37 +00:00
|
|
|
-- Notes:
|
|
|
|
--
|
|
|
|
-- - may call `getcwd` in Overwrite mode (if destination is a relative path)
|
2018-04-10 22:44:47 +00:00
|
|
|
copyFile :: Path b1 -- ^ source file
|
|
|
|
-> Path b2 -- ^ destination file
|
2016-06-05 01:10:28 +00:00
|
|
|
-> CopyMode
|
2016-05-09 14:53:31 +00:00
|
|
|
-> IO ()
|
2020-01-26 20:49:34 +00:00
|
|
|
copyFile (Path from) (Path to) cm = RD.copyFile from to cm
|
2016-05-09 14:53:31 +00:00
|
|
|
|
2016-05-31 14:21:14 +00:00
|
|
|
-- |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.
|
2016-05-09 14:53:31 +00:00
|
|
|
--
|
|
|
|
-- Safety/reliability concerns:
|
|
|
|
--
|
|
|
|
-- * examines filetypes explicitly
|
|
|
|
-- * calls `copyDirRecursive` for directories
|
2018-04-12 12:28:37 +00:00
|
|
|
--
|
|
|
|
-- Note: may call `getcwd` in Overwrite mode (if destination is a relative path)
|
2020-01-26 21:40:03 +00:00
|
|
|
easyCopy :: Path b1 -> Path b2 -> CopyMode -> RecursiveErrorMode -> IO ()
|
2020-01-26 20:49:34 +00:00
|
|
|
easyCopy (Path from) (Path to) cm rm = RD.easyCopy from to cm rm
|
|
|
|
|
2016-05-09 14:53:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
---------------------
|
|
|
|
--[ File Deletion ]--
|
|
|
|
---------------------
|
|
|
|
|
|
|
|
|
2016-05-22 11:28:20 +00:00
|
|
|
-- |Deletes the given file. Raises `eISDIR`
|
2016-05-09 14:53:31 +00:00
|
|
|
-- 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
|
2018-04-10 22:44:47 +00:00
|
|
|
deleteFile :: Path b -> IO ()
|
2020-01-26 20:49:34 +00:00
|
|
|
deleteFile (Path p) = RD.deleteFile p
|
2016-05-09 14:53:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
-- |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`
|
2018-04-10 22:44:47 +00:00
|
|
|
deleteDir :: Path b -> IO ()
|
2020-01-26 20:49:34 +00:00
|
|
|
deleteDir (Path p) = RD.deleteDir p
|
2016-05-09 14:53:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
-- |Deletes the given directory recursively. Does not follow symbolic
|
|
|
|
-- links. Tries `deleteDir` first before attemtping a recursive
|
|
|
|
-- deletion.
|
|
|
|
--
|
2016-05-31 14:21:14 +00:00
|
|
|
-- On directory contents this behaves like `easyDelete`
|
|
|
|
-- and thus will ignore any file type that is not `RegularFile`,
|
|
|
|
-- `SymbolicLink` or `Directory`.
|
|
|
|
--
|
2016-05-09 14:53:31 +00:00
|
|
|
-- 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
|
2018-04-10 22:44:47 +00:00
|
|
|
deleteDirRecursive :: Path b -> IO ()
|
2020-01-26 20:49:34 +00:00
|
|
|
deleteDirRecursive (Path p) = RD.deleteDirRecursive p
|
|
|
|
|
2016-05-09 14:53:31 +00:00
|
|
|
|
|
|
|
|
2016-05-31 14:21:14 +00:00
|
|
|
-- |Deletes a file, directory or symlink.
|
2016-05-09 14:53:31 +00:00
|
|
|
-- In case of directory, performs recursive deletion. In case of
|
|
|
|
-- a symlink, the symlink file is deleted.
|
2016-05-31 14:21:14 +00:00
|
|
|
-- Any other file type is ignored.
|
2016-05-09 14:53:31 +00:00
|
|
|
--
|
|
|
|
-- Safety/reliability concerns:
|
|
|
|
--
|
|
|
|
-- * examines filetypes explicitly
|
|
|
|
-- * calls `deleteDirRecursive` for directories
|
2018-04-10 22:44:47 +00:00
|
|
|
easyDelete :: Path b -> IO ()
|
2020-01-26 20:49:34 +00:00
|
|
|
easyDelete (Path p) = RD.easyDelete p
|
2016-05-09 14:53:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
--------------------
|
|
|
|
--[ File Opening ]--
|
|
|
|
--------------------
|
|
|
|
|
|
|
|
|
|
|
|
-- |Opens a file appropriately by invoking xdg-open. The file type
|
2016-05-09 15:37:16 +00:00
|
|
|
-- is not checked. This forks a process.
|
2020-01-26 21:40:03 +00:00
|
|
|
openFile :: Path b -> IO ProcessID
|
2020-01-26 20:49:34 +00:00
|
|
|
openFile (Path fp) = RD.openFile fp
|
2016-05-09 14:53:31 +00:00
|
|
|
|
|
|
|
|
2016-05-09 15:37:16 +00:00
|
|
|
-- |Executes a program with the given arguments. This forks a process.
|
2018-04-10 22:44:47 +00:00
|
|
|
executeFile :: Path b -- ^ program
|
2016-05-09 14:53:31 +00:00
|
|
|
-> [ByteString] -- ^ arguments
|
|
|
|
-> IO ProcessID
|
2020-01-26 20:49:34 +00:00
|
|
|
executeFile (Path fp) args = RD.executeFile fp args
|
2016-05-09 14:53:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
---------------------
|
|
|
|
--[ File Creation ]--
|
|
|
|
---------------------
|
|
|
|
|
|
|
|
|
2016-06-05 13:57:41 +00:00
|
|
|
-- |Create an empty regular file at the given directory with the given
|
|
|
|
-- filename.
|
2016-05-09 14:53:31 +00:00
|
|
|
--
|
|
|
|
-- Throws:
|
|
|
|
--
|
|
|
|
-- - `PermissionDenied` if output directory cannot be written to
|
2016-06-14 17:13:25 +00:00
|
|
|
-- - `AlreadyExists` if destination already exists
|
2016-06-05 19:59:31 +00:00
|
|
|
-- - `NoSuchThing` if any of the parent components of the path
|
|
|
|
-- do not exist
|
2018-04-10 22:44:47 +00:00
|
|
|
createRegularFile :: FileMode -> Path b -> IO ()
|
2020-01-26 20:49:34 +00:00
|
|
|
createRegularFile fm (Path destBS) = RD.createRegularFile fm destBS
|
2016-05-09 14:53:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
-- |Create an empty directory at the given directory with the given filename.
|
|
|
|
--
|
|
|
|
-- Throws:
|
|
|
|
--
|
|
|
|
-- - `PermissionDenied` if output directory cannot be written to
|
2016-06-14 17:13:25 +00:00
|
|
|
-- - `AlreadyExists` if destination already exists
|
2016-06-05 19:59:31 +00:00
|
|
|
-- - `NoSuchThing` if any of the parent components of the path
|
|
|
|
-- do not exist
|
2018-04-10 22:44:47 +00:00
|
|
|
createDir :: FileMode -> Path b -> IO ()
|
2020-01-26 20:49:34 +00:00
|
|
|
createDir fm (Path destBS) = RD.createDir fm destBS
|
2016-05-09 14:53:31 +00:00
|
|
|
|
2020-01-18 17:45:17 +00:00
|
|
|
-- |Create an empty directory at the given directory with the given filename.
|
|
|
|
--
|
|
|
|
-- Throws:
|
|
|
|
--
|
|
|
|
-- - `PermissionDenied` if output directory cannot be written to
|
|
|
|
-- - `NoSuchThing` if any of the parent components of the path
|
|
|
|
-- do not exist
|
|
|
|
createDirIfMissing :: FileMode -> Path b -> IO ()
|
2020-01-26 20:49:34 +00:00
|
|
|
createDirIfMissing fm (Path destBS) = RD.createDirIfMissing fm destBS
|
2020-01-18 17:45:17 +00:00
|
|
|
|
2016-05-09 14:53:31 +00:00
|
|
|
|
2016-06-12 23:28:55 +00:00
|
|
|
-- |Create an empty directory at the given directory with the given filename.
|
|
|
|
-- All parent directories are created with the same filemode. This
|
|
|
|
-- basically behaves like:
|
|
|
|
--
|
|
|
|
-- @
|
2016-06-12 23:38:44 +00:00
|
|
|
-- mkdir -p \/some\/dir
|
2016-06-12 23:28:55 +00:00
|
|
|
-- @
|
|
|
|
--
|
|
|
|
-- Safety/reliability concerns:
|
|
|
|
--
|
|
|
|
-- * not atomic
|
|
|
|
--
|
|
|
|
-- Throws:
|
|
|
|
--
|
|
|
|
-- - `PermissionDenied` if any part of the path components do not
|
|
|
|
-- exist and cannot be written to
|
|
|
|
-- - `AlreadyExists` if destination already exists and
|
2020-01-18 17:45:17 +00:00
|
|
|
-- is *not* a directory
|
2018-04-12 12:28:37 +00:00
|
|
|
--
|
|
|
|
-- Note: calls `getcwd` if the input path is a relative path
|
2018-04-10 22:44:47 +00:00
|
|
|
createDirRecursive :: FileMode -> Path b -> IO ()
|
2020-01-26 20:49:34 +00:00
|
|
|
createDirRecursive fm (Path p) = RD.createDirRecursive fm p
|
|
|
|
|
2016-06-12 23:28:55 +00:00
|
|
|
|
|
|
|
|
2016-05-29 15:28:12 +00:00
|
|
|
-- |Create a symlink.
|
|
|
|
--
|
|
|
|
-- Throws:
|
|
|
|
--
|
|
|
|
-- - `PermissionDenied` if output directory cannot be written to
|
|
|
|
-- - `AlreadyExists` if destination file already exists
|
2016-06-05 19:59:31 +00:00
|
|
|
-- - `NoSuchThing` if any of the parent components of the path
|
|
|
|
-- do not exist
|
2016-05-29 15:28:12 +00:00
|
|
|
--
|
|
|
|
-- Note: calls `symlink`
|
2018-04-10 22:44:47 +00:00
|
|
|
createSymlink :: Path b -- ^ destination file
|
2016-05-29 15:28:12 +00:00
|
|
|
-> ByteString -- ^ path the symlink points to
|
|
|
|
-> IO ()
|
2020-01-26 20:49:34 +00:00
|
|
|
createSymlink (Path destBS) sympoint = RD.createSymlink destBS sympoint
|
2016-05-29 15:28:12 +00:00
|
|
|
|
2016-05-09 14:53:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
----------------------------
|
|
|
|
--[ File Renaming/Moving ]--
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
|
|
|
|
-- |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
|
2016-06-05 13:57:41 +00:00
|
|
|
-- - `UnsupportedOperation` if source and destination are on different
|
|
|
|
-- devices
|
2016-06-14 17:13:25 +00:00
|
|
|
-- - `AlreadyExists` if destination already exists
|
2016-06-05 13:57:41 +00:00
|
|
|
-- - `SameFile` if destination and source are the same file
|
|
|
|
-- (`HPathIOException`)
|
2016-05-09 14:53:31 +00:00
|
|
|
--
|
|
|
|
-- Note: calls `rename` (but does not allow to rename over existing files)
|
2018-04-10 22:44:47 +00:00
|
|
|
renameFile :: Path b1 -> Path b2 -> IO ()
|
2020-01-26 20:49:34 +00:00
|
|
|
renameFile (Path from) (Path to) = RD.renameFile from to
|
|
|
|
|
2016-05-09 14:53:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
-- |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.
|
|
|
|
--
|
2016-05-31 14:21:14 +00:00
|
|
|
--
|
2016-05-09 14:53:31 +00:00
|
|
|
-- Safety/reliability concerns:
|
|
|
|
--
|
2016-06-05 01:10:28 +00:00
|
|
|
-- * `Overwrite` mode is not atomic
|
2016-05-09 14:53:31 +00:00
|
|
|
-- * copy-delete fallback is inherently non-atomic
|
2016-05-31 14:21:14 +00:00
|
|
|
-- * since this function calls `easyCopy` and `easyDelete` as a fallback
|
|
|
|
-- to `renameFile`, file types that are not `RegularFile`, `SymbolicLink`
|
|
|
|
-- or `Directory` may be ignored
|
2016-06-05 01:10:28 +00:00
|
|
|
-- * for `Overwrite` mode, the destination will be deleted (not recursively)
|
|
|
|
-- before moving
|
2016-05-09 14:53:31 +00:00
|
|
|
--
|
|
|
|
-- Throws:
|
|
|
|
--
|
|
|
|
-- - `NoSuchThing` if source file does not exist
|
|
|
|
-- - `PermissionDenied` if output directory cannot be written to
|
|
|
|
-- - `PermissionDenied` if source directory cannot be opened
|
2016-06-05 13:57:41 +00:00
|
|
|
-- - `SameFile` if destination and source are the same file
|
|
|
|
-- (`HPathIOException`)
|
2016-05-09 14:53:31 +00:00
|
|
|
--
|
2016-06-05 01:22:11 +00:00
|
|
|
-- Throws in `Strict` mode only:
|
|
|
|
--
|
2016-06-14 17:13:25 +00:00
|
|
|
-- - `AlreadyExists` if destination already exists
|
2016-06-05 01:22:11 +00:00
|
|
|
--
|
2018-04-12 12:28:37 +00:00
|
|
|
-- Notes:
|
|
|
|
--
|
|
|
|
-- - calls `rename` (but does not allow to rename over existing files)
|
|
|
|
-- - calls `getcwd` in Overwrite mode if destination is a relative path
|
2018-04-10 22:44:47 +00:00
|
|
|
moveFile :: Path b1 -- ^ file to move
|
|
|
|
-> Path b2 -- ^ destination
|
2016-06-05 01:10:28 +00:00
|
|
|
-> CopyMode
|
2016-05-09 14:53:31 +00:00
|
|
|
-> IO ()
|
2020-01-26 20:49:34 +00:00
|
|
|
moveFile (Path from) (Path to) cm = RD.moveFile from to cm
|
2016-05-09 14:53:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-04-06 14:42:40 +00:00
|
|
|
--------------------
|
|
|
|
--[ File Reading ]--
|
|
|
|
--------------------
|
|
|
|
|
|
|
|
|
2020-01-13 22:13:21 +00:00
|
|
|
-- |Read the given file *at once* into memory as a lazy ByteString.
|
2018-04-06 14:42:40 +00:00
|
|
|
-- Symbolic links are followed, no sanity checks on file size
|
2020-01-13 22:13:21 +00:00
|
|
|
-- or file type. File must exist. Uses Builders under the hood
|
|
|
|
-- (hence lazy ByteString).
|
2018-04-06 14:42:40 +00:00
|
|
|
--
|
|
|
|
-- Safety/reliability concerns:
|
|
|
|
--
|
2020-01-13 22:13:21 +00:00
|
|
|
-- * the whole file is read into memory, this doesn't read lazily
|
2018-04-06 14:42:40 +00:00
|
|
|
--
|
|
|
|
-- Throws:
|
|
|
|
--
|
|
|
|
-- - `InappropriateType` if file is not a regular file or a symlink
|
|
|
|
-- - `PermissionDenied` if we cannot read the file or the directory
|
|
|
|
-- containting it
|
|
|
|
-- - `NoSuchThing` if the file does not exist
|
2020-01-13 22:13:21 +00:00
|
|
|
readFile :: Path b -> IO L.ByteString
|
2020-01-26 20:49:34 +00:00
|
|
|
readFile (Path path) = RD.readFile path
|
2018-04-06 14:42:40 +00:00
|
|
|
|
|
|
|
|
2020-01-13 22:13:21 +00:00
|
|
|
|
|
|
|
-- | Open the given file as a filestream. Once the filestream is
|
|
|
|
-- exits, the filehandle is cleaned up.
|
2018-04-06 14:42:40 +00:00
|
|
|
--
|
|
|
|
-- Throws:
|
|
|
|
--
|
|
|
|
-- - `InappropriateType` if file is not a regular file or a symlink
|
|
|
|
-- - `PermissionDenied` if we cannot read the file or the directory
|
|
|
|
-- containting it
|
|
|
|
-- - `NoSuchThing` if the file does not exist
|
2020-01-26 21:40:03 +00:00
|
|
|
readFileStream :: Path b -> IO (SerialT IO ByteString)
|
2020-01-26 20:49:34 +00:00
|
|
|
readFileStream (Path fp) = RD.readFileStream fp
|
2018-04-06 14:42:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-04-06 15:22:22 +00:00
|
|
|
--------------------
|
|
|
|
--[ File Writing ]--
|
|
|
|
--------------------
|
|
|
|
|
|
|
|
|
|
|
|
-- |Write a given ByteString to a file, truncating the file beforehand.
|
2020-01-18 17:45:17 +00:00
|
|
|
-- Follows symlinks.
|
2018-04-06 15:22:22 +00:00
|
|
|
--
|
|
|
|
-- Throws:
|
|
|
|
--
|
|
|
|
-- - `InappropriateType` if file is not a regular file or a symlink
|
|
|
|
-- - `PermissionDenied` if we cannot read the file or the directory
|
|
|
|
-- containting it
|
|
|
|
-- - `NoSuchThing` if the file does not exist
|
2020-01-18 17:45:17 +00:00
|
|
|
writeFile :: Path b
|
|
|
|
-> Maybe FileMode -- ^ if Nothing, file must exist
|
|
|
|
-> ByteString
|
|
|
|
-> IO ()
|
2020-01-26 20:49:34 +00:00
|
|
|
writeFile (Path fp) fmode bs = RD.writeFile fp fmode bs
|
2020-01-18 17:45:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
-- |Write a given lazy ByteString to a file, truncating the file beforehand.
|
|
|
|
-- Follows symlinks.
|
|
|
|
--
|
|
|
|
-- Throws:
|
|
|
|
--
|
|
|
|
-- - `InappropriateType` if file is not a regular file or a symlink
|
|
|
|
-- - `PermissionDenied` if we cannot read the file or the directory
|
|
|
|
-- containting it
|
|
|
|
-- - `NoSuchThing` if the file does not exist
|
|
|
|
--
|
|
|
|
-- Note: uses streamly under the hood
|
|
|
|
writeFileL :: Path b
|
|
|
|
-> Maybe FileMode -- ^ if Nothing, file must exist
|
|
|
|
-> L.ByteString
|
|
|
|
-> IO ()
|
2020-01-26 20:49:34 +00:00
|
|
|
writeFileL (Path fp) fmode lbs = RD.writeFileL fp fmode lbs
|
2018-04-06 15:22:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
-- |Append a given ByteString to a file.
|
|
|
|
-- The file must exist. Follows symlinks.
|
|
|
|
--
|
|
|
|
-- Throws:
|
|
|
|
--
|
|
|
|
-- - `InappropriateType` if file is not a regular file or a symlink
|
|
|
|
-- - `PermissionDenied` if we cannot read the file or the directory
|
|
|
|
-- containting it
|
|
|
|
-- - `NoSuchThing` if the file does not exist
|
2018-04-10 22:44:47 +00:00
|
|
|
appendFile :: Path b -> ByteString -> IO ()
|
2020-01-26 20:49:34 +00:00
|
|
|
appendFile (Path fp) bs = RD.appendFile fp bs
|
2018-04-06 15:22:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2016-05-09 14:53:31 +00:00
|
|
|
|
2020-01-13 22:12:55 +00:00
|
|
|
|
|
|
|
-------------------
|
|
|
|
--[ File checks ]--
|
|
|
|
-------------------
|
|
|
|
|
|
|
|
|
|
|
|
-- |Checks if the given file exists.
|
|
|
|
-- Does not follow symlinks.
|
|
|
|
--
|
|
|
|
-- Only eNOENT is catched (and returns False).
|
|
|
|
doesExist :: Path b -> IO Bool
|
2020-01-26 20:49:34 +00:00
|
|
|
doesExist (Path bs) = RD.doesExist bs
|
2020-01-13 22:12:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
-- |Checks if the given file exists and is not a directory.
|
|
|
|
-- Does not follow symlinks.
|
|
|
|
--
|
|
|
|
-- Only eNOENT is catched (and returns False).
|
|
|
|
doesFileExist :: Path b -> IO Bool
|
2020-01-26 20:49:34 +00:00
|
|
|
doesFileExist (Path bs) = RD.doesFileExist bs
|
2020-01-13 22:12:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
-- |Checks if the given file exists and is a directory.
|
|
|
|
-- Does not follow symlinks.
|
|
|
|
--
|
|
|
|
-- Only eNOENT is catched (and returns False).
|
|
|
|
doesDirectoryExist :: Path b -> IO Bool
|
2020-01-26 20:49:34 +00:00
|
|
|
doesDirectoryExist (Path bs) = RD.doesDirectoryExist bs
|
2020-01-13 22:12:55 +00:00
|
|
|
|
|
|
|
|
2020-01-18 17:45:17 +00:00
|
|
|
-- |Checks whether a file or folder is readable.
|
|
|
|
--
|
|
|
|
-- Only eACCES, eROFS, eTXTBSY, ePERM are catched (and return False).
|
|
|
|
--
|
|
|
|
-- Throws:
|
|
|
|
--
|
|
|
|
-- - `NoSuchThing` if the file does not exist
|
|
|
|
isReadable :: Path b -> IO Bool
|
2020-01-26 20:49:34 +00:00
|
|
|
isReadable (Path bs) = RD.isReadable bs
|
2020-01-18 17:45:17 +00:00
|
|
|
|
2020-01-13 22:12:55 +00:00
|
|
|
-- |Checks whether a file or folder is writable.
|
|
|
|
--
|
|
|
|
-- Only eACCES, eROFS, eTXTBSY, ePERM are catched (and return False).
|
|
|
|
--
|
|
|
|
-- Throws:
|
|
|
|
--
|
|
|
|
-- - `NoSuchThing` if the file does not exist
|
|
|
|
isWritable :: Path b -> IO Bool
|
2020-01-26 20:49:34 +00:00
|
|
|
isWritable (Path bs) = RD.isWritable bs
|
2020-01-13 22:12:55 +00:00
|
|
|
|
|
|
|
|
2020-01-18 17:45:17 +00:00
|
|
|
-- |Checks whether a file or folder is executable.
|
|
|
|
--
|
|
|
|
-- Only eACCES, eROFS, eTXTBSY, ePERM are catched (and return False).
|
|
|
|
--
|
|
|
|
-- Throws:
|
|
|
|
--
|
|
|
|
-- - `NoSuchThing` if the file does not exist
|
|
|
|
isExecutable :: Path b -> IO Bool
|
2020-01-26 20:49:34 +00:00
|
|
|
isExecutable (Path bs) = RD.isExecutable bs
|
2020-01-18 17:45:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2020-01-13 22:12:55 +00:00
|
|
|
-- |Checks whether the directory at the given path exists and can be
|
|
|
|
-- opened. This invokes `openDirStream` which follows symlinks.
|
|
|
|
canOpenDirectory :: Path b -> IO Bool
|
2020-01-26 20:49:34 +00:00
|
|
|
canOpenDirectory (Path bs) = RD.canOpenDirectory bs
|
2020-01-13 22:12:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-01-18 17:45:17 +00:00
|
|
|
------------------
|
|
|
|
--[ File times ]--
|
|
|
|
------------------
|
|
|
|
|
|
|
|
|
|
|
|
getModificationTime :: Path b -> IO UTCTime
|
2020-01-26 20:49:34 +00:00
|
|
|
getModificationTime (Path bs) = RD.getModificationTime bs
|
2020-01-18 17:45:17 +00:00
|
|
|
|
|
|
|
setModificationTime :: Path b -> EpochTime -> IO ()
|
2020-01-26 20:49:34 +00:00
|
|
|
setModificationTime (Path bs) t = RD.setModificationTime bs t
|
2020-01-18 17:45:17 +00:00
|
|
|
|
|
|
|
setModificationTimeHiRes :: Path b -> POSIXTime -> IO ()
|
2020-01-26 20:49:34 +00:00
|
|
|
setModificationTimeHiRes (Path bs) t = RD.setModificationTimeHiRes bs t
|
2020-01-18 17:45:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2016-05-09 14:53:31 +00:00
|
|
|
-------------------------
|
|
|
|
--[ Directory reading ]--
|
|
|
|
-------------------------
|
|
|
|
|
|
|
|
|
|
|
|
-- |Gets all filenames of the given directory. This excludes "." and "..".
|
|
|
|
-- This version does not follow symbolic links.
|
|
|
|
--
|
2016-06-05 13:57:41 +00:00
|
|
|
-- The contents are not sorted and there is no guarantee on the ordering.
|
|
|
|
--
|
2016-05-09 14:53:31 +00:00
|
|
|
-- 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
|
2020-01-18 17:45:17 +00:00
|
|
|
-- - `PathParseException` if a filename could not be parsed (should never happen)
|
2018-04-10 22:44:47 +00:00
|
|
|
getDirsFiles :: Path b -- ^ dir to read
|
|
|
|
-> IO [Path b]
|
2020-01-26 21:40:03 +00:00
|
|
|
getDirsFiles p = do
|
2020-01-18 17:45:17 +00:00
|
|
|
contents <- getDirsFiles' p
|
|
|
|
pure $ fmap (p </>) contents
|
|
|
|
|
|
|
|
|
|
|
|
-- | Like 'getDirsFiles', but returns the filename only, instead
|
|
|
|
-- of prepending the base path.
|
|
|
|
getDirsFiles' :: Path b -- ^ dir to read
|
2020-01-26 21:40:03 +00:00
|
|
|
-> IO [Path Rel]
|
2020-01-26 20:49:34 +00:00
|
|
|
getDirsFiles' (Path fp) = do
|
|
|
|
rawContents <- RD.getDirsFiles' fp
|
|
|
|
for rawContents $ \r -> parseRel r
|
2016-05-09 14:53:31 +00:00
|
|
|
|
2016-05-09 12:40:30 +00:00
|
|
|
|
2020-05-08 21:46:39 +00:00
|
|
|
-- | Like 'getDirsFiles'', except returning a Stream.
|
|
|
|
getDirsFilesStream :: (MonadCatch m, MonadAsync m, MonadMask m)
|
|
|
|
=> Path b
|
|
|
|
-> IO (SerialT m (Path Rel))
|
|
|
|
getDirsFilesStream (Path fp) = do
|
|
|
|
s <- RD.getDirsFilesStream fp
|
|
|
|
pure (s >>= parseRel)
|
|
|
|
|
|
|
|
|
2016-05-09 12:40:30 +00:00
|
|
|
|
|
|
|
|
2016-05-09 14:53:31 +00:00
|
|
|
---------------------------
|
|
|
|
--[ FileType operations ]--
|
|
|
|
---------------------------
|
2016-05-09 12:40:30 +00:00
|
|
|
|
|
|
|
|
2016-05-09 14:53:31 +00:00
|
|
|
-- |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
|
2018-04-10 22:44:47 +00:00
|
|
|
getFileType :: Path b -> IO FileType
|
2020-01-26 20:49:34 +00:00
|
|
|
getFileType (Path fp) = RD.getFileType fp
|
2016-05-09 14:53:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
--------------
|
|
|
|
--[ Others ]--
|
|
|
|
--------------
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-04-10 22:44:47 +00:00
|
|
|
-- |Applies `realpath` on the given path.
|
2016-05-09 14:53:31 +00:00
|
|
|
--
|
|
|
|
-- Throws:
|
|
|
|
--
|
|
|
|
-- - `NoSuchThing` if the file at the given path does not exist
|
|
|
|
-- - `NoSuchThing` if the symlink is broken
|
2020-01-20 18:50:44 +00:00
|
|
|
-- - `PathParseException` if realpath does not return an absolute path
|
2018-04-10 22:44:47 +00:00
|
|
|
canonicalizePath :: Path b -> IO (Path Abs)
|
2020-01-20 18:50:44 +00:00
|
|
|
canonicalizePath (Path l) = do
|
2020-01-26 20:49:34 +00:00
|
|
|
nl <- RD.canonicalizePath l
|
2020-01-20 18:50:44 +00:00
|
|
|
parseAbs nl
|
2018-04-10 22:44:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
-- |Converts any path to an absolute path.
|
|
|
|
-- This is done in the following way:
|
|
|
|
--
|
|
|
|
-- - if the path is already an absolute one, just return it
|
|
|
|
-- - if it's a relative path, prepend the current directory to it
|
|
|
|
toAbs :: Path b -> IO (Path Abs)
|
2020-01-20 18:50:44 +00:00
|
|
|
toAbs (Path bs) = do
|
2018-04-10 22:44:47 +00:00
|
|
|
let mabs = parseAbs bs :: Maybe (Path Abs)
|
|
|
|
case mabs of
|
2020-01-26 21:40:03 +00:00
|
|
|
Just a -> return a
|
|
|
|
Nothing -> do
|
2018-04-10 22:44:47 +00:00
|
|
|
cwd <- getWorkingDirectory >>= parseAbs
|
2020-01-26 21:40:03 +00:00
|
|
|
r <- parseRel bs -- we know it must be relative now
|
|
|
|
return $ cwd </> r
|
2020-01-18 17:45:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
-- | Helper function to use the Path library without
|
|
|
|
-- buying into the Path type too much. This uses 'parseAny'
|
|
|
|
-- under the hood and may throw `PathParseException`.
|
|
|
|
--
|
|
|
|
-- Throws:
|
|
|
|
--
|
|
|
|
-- - `PathParseException` if the bytestring could neither be parsed as
|
|
|
|
-- relative or absolute Path
|
2020-01-26 21:40:03 +00:00
|
|
|
withRawFilePath :: MonadThrow m
|
|
|
|
=> ByteString
|
|
|
|
-> (Either (Path Abs) (Path Rel) -> m b)
|
|
|
|
-> m b
|
2020-01-18 17:45:17 +00:00
|
|
|
withRawFilePath bs action = do
|
|
|
|
path <- parseAny bs
|
|
|
|
action path
|
|
|
|
|
|
|
|
|
|
|
|
-- | Convenience function to open the path as a handle.
|
|
|
|
--
|
|
|
|
-- If the file does not exist, it will be created with 'newFilePerms'.
|
|
|
|
--
|
|
|
|
-- Throws:
|
|
|
|
--
|
|
|
|
-- - `PathParseException` if the bytestring could neither be parsed as
|
|
|
|
-- relative or absolute Path
|
|
|
|
withHandle :: ByteString
|
|
|
|
-> SPI.OpenMode
|
2020-01-20 18:50:44 +00:00
|
|
|
-> ((SIO.Handle, Either (Path Abs) (Path Rel)) -> IO a)
|
2020-01-18 17:45:17 +00:00
|
|
|
-> IO a
|
|
|
|
withHandle bs mode action = do
|
|
|
|
path <- parseAny bs
|
|
|
|
handle <-
|
2020-01-26 21:40:03 +00:00
|
|
|
bracketOnError (openFd bs mode [] (Just RD.newFilePerms)) (SPI.closeFd)
|
2020-01-18 17:45:17 +00:00
|
|
|
$ SPI.fdToHandle
|
|
|
|
finally (action (handle, path)) (SIO.hClose handle)
|