[commit: packages/directory] master: Add `makeAbsolute` function (87530fc)

git at git.haskell.org git at git.haskell.org
Thu Mar 19 11:38:33 UTC 2015


Repository : ssh://git@git.haskell.org/directory

On branch  : master
Link       : http://ghc.haskell.org/trac/ghc/changeset/87530fce17b5a5dc131af13aa0bedcd9eea793f5/directory

>---------------------------------------------------------------

commit 87530fce17b5a5dc131af13aa0bedcd9eea793f5
Author: Phil Ruffwind <rf at rufflewind.com>
Date:   Fri Mar 6 04:40:18 2015 -0500

    Add `makeAbsolute` function
    
    The function makes a path absolute by prepending the current directory
    and normalising the result.


>---------------------------------------------------------------

87530fce17b5a5dc131af13aa0bedcd9eea793f5
 System/Directory.hs | 13 +++++++++++++
 changelog.md        |  3 +++
 2 files changed, 16 insertions(+)

diff --git a/System/Directory.hs b/System/Directory.hs
index 0e2c071..32dc453 100644
--- a/System/Directory.hs
+++ b/System/Directory.hs
@@ -44,6 +44,7 @@ module System.Directory
     , copyFile
 
     , canonicalizePath
+    , makeAbsolute
     , makeRelativeToCurrentDirectory
     , findExecutable
     , findExecutables
@@ -778,6 +779,18 @@ foreign import ccall unsafe "realpath"
                               -> IO CString
 #endif
 
+-- | Make a path absolute by prepending the current directory (if it isn't
+-- already absolute) and applying @'normalise'@ to the result.
+--
+-- The operation may fail with the same exceptions as @'getCurrentDirectory'@.
+--
+-- /Since: 1.2.2.0/
+makeAbsolute :: FilePath -> IO FilePath
+makeAbsolute = fmap normalise . absolutize
+  where absolutize path -- avoid the call to `getCurrentDirectory` if we can
+          | isRelative path = fmap (</> path) getCurrentDirectory
+          | otherwise       = return path
+
 -- | 'makeRelative' the current directory.
 makeRelativeToCurrentDirectory :: FilePath -> IO FilePath
 makeRelativeToCurrentDirectory x = do
diff --git a/changelog.md b/changelog.md
index ecc6838..31c8393 100644
--- a/changelog.md
+++ b/changelog.md
@@ -29,6 +29,9 @@ Changelog for the [`directory`][1] package
     (`InappropriateType`) when the destination is a directory, as long as the
     filesystem is not being modified concurrently
 
+  * Add `makeAbsolute`, which should be preferred over `canonicalizePath`
+    unless one requires symbolic links to be resolved
+
 ## 1.2.1.0 (Mar 2014)
 
   * Bundled with GHC 7.8.1



More information about the ghc-commits mailing list