
Fix bug in handling of linked worktrees on filesystems not supporting symlinks, that caused annexed file content to be stored in the wrong location inside the git directory, and also caused pointer files to not get populated. This parameterizes functions in Annex.Locations with a GitLocationMaker. The uses of standardGitLocationMaker are in cases where the path returned by a function should not change when in a linked worktree. For example, gitAnnexLink uses standardGitLocationMaker because symlink targets should always be to ".git/annex/objects" paths, even when in a linked worktree. Hopefully I have gotten all uses of standardGitLocationMaker right. This also assumes that all path construction to the annex directory is done via the functions in Annex.Locations, and there is no other, ad-hoc construction elsewhere. Thankfully, Annex.Locations has been around since the beginning, and has been used consistently. I think. --- In fixupUnusualRepos, when symlinks are supported, the .git file is replaced with a symlink to the linked worktree git directory. And in that directory, an "annex" symlink points to the main annex directory. In that case, it's not necessary to set mainWorkTreePath. It would be ok to set it, but not setting it in that case allows an optimisation of avoiding reading the "commondir" file. The change to make fixupUnusualRepos set mainWorkTreePath when the repository is not initialized yet is done in case the initialization itself writes to the annex directory. If that were the case, without setting mainWorkTreePath, the annex symlink would not be set up yet, and so it might have created the annex directory in the wrong place. Currently that didn't happen, but now that mainWorkTreePath is available, using it here avoids any such later problem. --- This commit does not deal with the mess of a worktree that has experienced this bug before. In particular, if `git-annex get` were run in such a worktree, it would have stored the object files in the linked worktree's git directory, rather than in the main git directory. Such misplaced object files need to be dealt with; the plan is to make git-annex fsck notice and fix them. A worktree that has experienced this bug before will contain unpopulated pointer files. Those may eventually get fixed up in regular usage of git-annex, but git-annex fsck will also fix them up. --- Finally, this has me pondering if all of git-annex's state files should really be stored in one common place across all linked worktrees. Should perhaps state files that are specific to the worktree be stored per-worktree? That has not been the case when using git-annex on filesystems supporting symlinks, but it *has* been the case on filesystems not supporting symlinks. Perhaps this leads to some other buggy behavior in some cases. Or perhaps to extra work being done. For example, the keys database has an associated files table. Which depends on the worktree. But reconcileStaged updates that table, so when git-annex is used first in one worktree and then in another one, reconcileStaged will update the table to reflect the current worktree. Which is extra work each time a different worktree is used. But also, what if two git-annex processes are running at the same time, in separate worktrees? Probably this needs more thought and investigation. So there is a risk that this commit exposes such buggy behavior in a situation where it didn't happen before, due to the filesystem not supporting symlinks. But, given how much this bug crippled using linked worktrees in such a situation, I doubt that many people have been doing that.
156 lines
5.3 KiB
Haskell
156 lines
5.3 KiB
Haskell
{- git-annex assistant repository repair
|
|
-
|
|
- Copyright 2013 Joey Hess <id@joeyh.name>
|
|
-
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
|
-}
|
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
{-# LANGUAGE CPP #-}
|
|
|
|
module Assistant.Repair where
|
|
|
|
import Assistant.Common
|
|
import Command.Repair (repairAnnexBranch, trackingOrSyncBranch)
|
|
import Git.Fsck (FsckResults, foundBroken)
|
|
import Git.Repair (runRepairOf)
|
|
import qualified Git
|
|
import qualified Remote
|
|
import qualified Types.Remote as Remote
|
|
import Logs.FsckResults
|
|
import Annex.UUID
|
|
import Utility.Batch
|
|
import Annex.Path
|
|
import Assistant.Sync
|
|
import Assistant.Alert
|
|
import Assistant.DaemonStatus
|
|
import Assistant.Types.UrlRenderer
|
|
#ifdef WITH_WEBAPP
|
|
import Assistant.WebApp.Types
|
|
import qualified Data.Text as T
|
|
#endif
|
|
import qualified Utility.Lsof as Lsof
|
|
import Utility.ThreadScheduler
|
|
import qualified Utility.OsString as OS
|
|
|
|
import Control.Concurrent.Async
|
|
|
|
{- When the FsckResults require a repair, tries to do a non-destructive
|
|
- repair. If that fails, pops up an alert. -}
|
|
repairWhenNecessary :: UrlRenderer -> UUID -> Maybe Remote -> FsckResults -> Assistant Bool
|
|
repairWhenNecessary urlrenderer u mrmt fsckresults
|
|
| foundBroken fsckresults = do
|
|
liftAnnex $ writeFsckResults u fsckresults
|
|
repodesc <- liftAnnex $ Remote.prettyUUID u
|
|
ok <- alertWhile (repairingAlert repodesc)
|
|
(runRepair u mrmt False)
|
|
#ifdef WITH_WEBAPP
|
|
unless ok $ do
|
|
button <- mkAlertButton True (T.pack "Click Here") urlrenderer $
|
|
RepairRepositoryR u
|
|
void $ addAlert $ brokenRepositoryAlert [button]
|
|
#endif
|
|
return ok
|
|
| otherwise = return False
|
|
|
|
runRepair :: UUID -> Maybe Remote -> Bool -> Assistant Bool
|
|
runRepair u mrmt destructiverepair = do
|
|
fsckresults <- liftAnnex $ readFsckResults u
|
|
myu <- liftAnnex getUUID
|
|
ok <- if u == myu
|
|
then localrepair fsckresults
|
|
else remoterepair fsckresults
|
|
liftAnnex $ clearFsckResults u
|
|
debug [ "Repaired", show u, show ok ]
|
|
|
|
return ok
|
|
where
|
|
localrepair fsckresults = do
|
|
-- Stop the watcher from running while running repairs.
|
|
changeSyncable Nothing False
|
|
|
|
-- This intentionally runs the repair inside the Annex
|
|
-- monad, which is not strictly necessary, but keeps
|
|
-- other threads that might be trying to use the Annex
|
|
-- from running until it completes.
|
|
ok <- liftAnnex $ repair fsckresults Nothing
|
|
|
|
-- Run a background fast fsck if a destructive repair had
|
|
-- to be done, to ensure that the git-annex branch
|
|
-- reflects the current state of the repo.
|
|
when destructiverepair $
|
|
backgroundfsck [ Param "--fast" ]
|
|
|
|
-- Start the watcher running again. This also triggers it to
|
|
-- do a startup scan, which is especially important if the
|
|
-- git repo repair removed files from the index file. Those
|
|
-- files will be seen as new, and re-added to the repository.
|
|
when (ok || destructiverepair) $
|
|
changeSyncable Nothing True
|
|
|
|
return ok
|
|
|
|
remoterepair fsckresults = case Remote.repairRepo =<< mrmt of
|
|
Nothing -> return False
|
|
Just mkrepair -> do
|
|
thisrepopath <- liftIO . absPath
|
|
=<< liftAnnex (fromRepo Git.repoPath)
|
|
a <- liftAnnex $ mkrepair $
|
|
repair fsckresults (Just (fromOsPath thisrepopath))
|
|
liftIO $ catchBoolIO a
|
|
|
|
repair fsckresults referencerepo = do
|
|
(ok, modifiedbranches) <- inRepo $
|
|
runRepairOf fsckresults trackingOrSyncBranch destructiverepair referencerepo
|
|
when destructiverepair $
|
|
repairAnnexBranch modifiedbranches
|
|
return ok
|
|
|
|
backgroundfsck params = liftIO $ void $ async $ do
|
|
program <- programPath
|
|
batchCommand (fromOsPath program) (Param "fsck" : params)
|
|
|
|
{- Detect when a git lock file exists and has no git process currently
|
|
- writing to it. This strongly suggests it is a stale lock file.
|
|
-
|
|
- However, this could be on a network filesystem. Which is not very safe
|
|
- anyway (the assistant relies on being able to check when files have
|
|
- no writers to know when to commit them). Also, a few lock-file-ish
|
|
- things used by git are not kept open, particularly MERGE_HEAD.
|
|
-
|
|
- So, just in case, when the lock file appears stale, we delay for one
|
|
- minute, and check its size. If the size changed, delay for another
|
|
- minute, and so on. This will at work to detect when another machine
|
|
- is writing out a new index file, since git does so by writing the
|
|
- new content to index.lock.
|
|
-
|
|
- Returns true if locks were cleaned up.
|
|
-}
|
|
repairStaleGitLocks :: Git.Repo -> Assistant Bool
|
|
repairStaleGitLocks r = do
|
|
lockfiles <- liftIO $ filter islock
|
|
<$> emptyWhenDoesNotExist (findgitfiles r)
|
|
repairStaleLocks lockfiles
|
|
return $ not $ null lockfiles
|
|
where
|
|
findgitfiles = dirContentsRecursiveSkipping (== dropTrailingPathSeparator (annexDir standardGitLocationMaker)) True . Git.localGitDir
|
|
islock f
|
|
| literalOsPath "gc.pid" `OS.isInfixOf` f = False
|
|
| literalOsPath ".lock" `OS.isSuffixOf` f = True
|
|
| takeFileName f == literalOsPath "MERGE_HEAD" = True
|
|
| otherwise = False
|
|
|
|
repairStaleLocks :: [OsPath] -> Assistant ()
|
|
repairStaleLocks lockfiles = go =<< getsizes
|
|
where
|
|
getsize lf = catchMaybeIO $ (\s -> (lf, s))
|
|
<$> getFileSize lf
|
|
getsizes = liftIO $ catMaybes <$> mapM getsize lockfiles
|
|
go [] = return ()
|
|
go l = whenM (liftIO $ null <$> Lsof.query ("--" : map (fromOsPath . fst) l)) $ do
|
|
debug ["Waiting for 60 seconds to check stale git lock file"]
|
|
liftIO $ threadDelaySeconds $ Seconds 60
|
|
l' <- getsizes
|
|
if l' == l
|
|
then liftIO $ mapM_ (removeWhenExistsWith removeFile . fst) l
|
|
else go l'
|