I find the following generalization of copyFile useful. Perhaps it could be added to System.Directory. {- |@'copyContents' old new@ copies the contents of /old/ to /new/, creating /new/ if it does not already exist. However the parent directory of /new/ must already exist. If /old/ is a plain file, 'copyContents' is equivalent to 'copyFile'. If /old/ is a directory, its contents are recursively copied into /new/, which, if it already exists, must be a directory. -} copyContents :: FilePath -> FilePath -> IO () copyContents fromFPath toFPath = do isFile <- doesFileExist fromFPath if isFile then copyFile fromFPath toFPath else do contents <- getDirectoryContents fromFPath createDirectoryIfMissing False toFPath sequence_ [copyContents (fromFPath `joinFileName` f) (toFPath `joinFileName` f) | f <- contents, f /= "." && f /= ".."]
Ross Paterson wrote:
I find the following generalization of copyFile useful. Perhaps it could be added to System.Directory.
{- |@'copyContents' old new@ copies the contents of /old/ to /new/, creating /new/ if it does not already exist.
Copying files and directories opens a huge can of worms, due to issues such as: 1. Should metadata (ownership, permissions, timestamps) be preserved? 2. What if the destination exists? What if the destination is a symlink? Should the destination be opened for writing, or should it be deleted and a new file created? 3. If the source is a symlink (or if the source tree contains symlinks), should the symlink be copied or should it be dereferenced and the contents copied to a new file? There is no one "correct" way to copy a file, and the situation is even more involved for directory trees (e.g. if parts of the destination hierarchy already exist). -- Glynn Clements <glynn@gclements.plus.com>
participants (2)
-
Glynn Clements -
Ross Paterson