aboutsummaryrefslogtreecommitdiff
path: root/Utility/InodeCache.hs
diff options
context:
space:
mode:
authorGravatar Joey Hess <joey@kitenet.net>2013-02-14 16:17:40 -0400
committerGravatar Joey Hess <joey@kitenet.net>2013-02-14 16:17:40 -0400
commit5fc9ccdaa5c73ec424de175962f98cc8fd63eca0 (patch)
tree1030a6fe9d351709bcdada518d4a24fa4986ed18 /Utility/InodeCache.hs
parent177245deb6ee3271eb44d77c2b0cd722755b2c3f (diff)
split out Utility.InodeCache
Diffstat (limited to 'Utility/InodeCache.hs')
-rw-r--r--Utility/InodeCache.hs50
1 files changed, 50 insertions, 0 deletions
diff --git a/Utility/InodeCache.hs b/Utility/InodeCache.hs
new file mode 100644
index 000000000..023a203f8
--- /dev/null
+++ b/Utility/InodeCache.hs
@@ -0,0 +1,50 @@
+{- Caching a file's inode, size, and modification time to see when it's changed.
+ -
+ - Copyright 2013 Joey Hess <joey@kitenet.net>
+ -
+ - Licensed under the GNU GPL version 3 or higher.
+ -}
+
+module Utility.InodeCache where
+
+import Common
+import System.Posix.Types
+
+data InodeCache = InodeCache FileID FileOffset EpochTime
+ deriving (Eq, Show)
+
+showInodeCache :: InodeCache -> String
+showInodeCache (InodeCache inode size mtime) = unwords
+ [ show inode
+ , show size
+ , show mtime
+ ]
+
+readInodeCache :: String -> Maybe InodeCache
+readInodeCache s = case words s of
+ (inode:size:mtime:_) -> InodeCache
+ <$> readish inode
+ <*> readish size
+ <*> readish mtime
+ _ -> Nothing
+
+-- for quickcheck
+prop_read_show_inodecache :: InodeCache -> Bool
+prop_read_show_inodecache c = readInodeCache (showInodeCache c) == Just c
+
+genInodeCache :: FilePath -> IO (Maybe InodeCache)
+genInodeCache f = catchDefaultIO Nothing $ toInodeCache <$> getFileStatus f
+
+toInodeCache :: FileStatus -> Maybe InodeCache
+toInodeCache s
+ | isRegularFile s = Just $ InodeCache
+ (fileID s)
+ (fileSize s)
+ (modificationTime s)
+ | otherwise = Nothing
+
+{- Compares an inode cache with the current inode of file. -}
+compareInodeCache :: FilePath -> Maybe InodeCache -> IO Bool
+compareInodeCache file old = do
+ curr <- genInodeCache file
+ return $ isJust curr && curr == old