summaryrefslogtreecommitdiff
path: root/Utility.hs
diff options
context:
space:
mode:
authorGravatar Joey Hess <joey@kitenet.net>2010-10-10 00:18:10 -0400
committerGravatar Joey Hess <joey@kitenet.net>2010-10-10 00:18:10 -0400
commitd0e82d0b9218a9ff3a693e066c4320c08d4d1c47 (patch)
treee183a3d4b61263f71606f75edbcab3f7be7618a0 /Utility.hs
parente64d1becf429489f8c6ded230e6e17b63a89c483 (diff)
add
Diffstat (limited to 'Utility.hs')
-rw-r--r--Utility.hs29
1 files changed, 29 insertions, 0 deletions
diff --git a/Utility.hs b/Utility.hs
new file mode 100644
index 000000000..05b06dea7
--- /dev/null
+++ b/Utility.hs
@@ -0,0 +1,29 @@
+{- git-annex utility functions
+ -}
+
+module Utility where
+
+import System.IO
+import System.Posix.IO
+import Data.String.Utils
+
+{- Let's just say that Haskell makes reading/writing a file with
+ - file locking excessively difficult. -}
+openLocked file mode = do
+ handle <- openFile file mode
+ lockfd <- handleToFd handle -- closes handle
+ waitToSetLock lockfd (lockType mode, AbsoluteSeek, 0, 0)
+ handle' <- fdToHandle lockfd
+ return handle'
+ where
+ lockType ReadMode = ReadLock
+ lockType _ = WriteLock
+
+{- Returns the parent directory of a path. Parent of / is "" -}
+parentDir :: String -> String
+parentDir dir =
+ if length dirs > 0
+ then "/" ++ (join "/" $ take ((length dirs) - 1) dirs)
+ else ""
+ where
+ dirs = filter (\x -> length x > 0) $ split "/" dir