From a16efeb30c3d68479e609196e6e1320c89acc6a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roman=20Smr=C5=BE?= Date: Sat, 29 Jun 2024 22:36:25 +0200 Subject: Check erebos-storage version from directory listing --- src/Erebos/Storage.hs | 33 +++++++++++++++++++-------------- 1 file 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" -- cgit v1.2.3