diff options
author | Roman Smrž <roman.smrz@seznam.cz> | 2024-06-29 22:36:25 +0200 |
---|---|---|
committer | Roman Smrž <roman.smrz@seznam.cz> | 2024-06-30 09:06:47 +0200 |
commit | a16efeb30c3d68479e609196e6e1320c89acc6a6 (patch) | |
tree | 213c07c577b540aae7a9e1a4219414d58d604a37 /src/Erebos/Storage.hs | |
parent | 414f810c7c1d9aedeaa45e0998537395e7f4a907 (diff) |
Check erebos-storage version from directory listing
Diffstat (limited to 'src/Erebos/Storage.hs')
-rw-r--r-- | src/Erebos/Storage.hs | 33 |
1 files changed, 19 insertions, 14 deletions
diff --git a/src/Erebos/Storage.hs b/src/Erebos/Storage.hs index 034ed04..95ef649 100644 --- a/src/Erebos/Storage.hs +++ b/src/Erebos/Storage.hs @@ -117,21 +117,26 @@ storageVersion = "0.1" openStorage :: FilePath -> IO Storage openStorage path = modifyIOError annotate $ do - let versionPath = path </> "erebos-storage" - doesFileExist versionPath >>= \case - True -> readFile versionPath >>= \case - content | (ver:_) <- lines content, ver == storageVersion -> return () - | otherwise -> fail "unsupported storage version" + let versionFileName = "erebos-storage" + let versionPath = path </> versionFileName + let writeVersionFile = writeFile versionPath $ storageVersion <> "\n" + + doesDirectoryExist path >>= \case + True -> do + listDirectory path >>= \case + files@(_:_) + | versionFileName `elem` files -> do + readFile versionPath >>= \case + content | (ver:_) <- lines content, ver == storageVersion -> return () + | otherwise -> fail "unsupported storage version" + + | "objects" `notElem` files || "heads" `notElem` files -> do + fail "directory is neither empty, nor an existing erebos storage" + + _ -> writeVersionFile False -> do - doesDirectoryExist path >>= \case - True -> do - listDirectory path >>= \case - contents@(_:_) | "objects" `notElem` contents || "heads" `notElem` contents - -> fail "directory is neither empty, nor an existing erebos storage" - _ -> return () - False -> do - createDirectoryIfMissing True $ path - writeFile versionPath $ storageVersion <> "\n" + createDirectoryIfMissing True $ path + writeVersionFile createDirectoryIfMissing True $ path </> "objects" createDirectoryIfMissing True $ path </> "heads" |