2012-06-13 16:36:33 +00:00
|
|
|
{- git-annex assistant tree watcher
|
|
|
|
-
|
2015-12-21 22:41:15 +00:00
|
|
|
- Copyright 2012-2015 Joey Hess <id@joeyh.name>
|
2012-06-13 16:36:33 +00:00
|
|
|
-
|
2019-03-13 19:48:14 +00:00
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
2012-06-13 16:36:33 +00:00
|
|
|
-}
|
|
|
|
|
2023-04-10 21:03:41 +00:00
|
|
|
{-# LANGUAGE DeriveDataTypeable, OverloadedStrings, CPP #-}
|
2013-01-27 11:43:05 +00:00
|
|
|
|
2012-07-20 23:29:59 +00:00
|
|
|
module Assistant.Threads.Watcher (
|
|
|
|
watchThread,
|
2013-10-26 20:54:49 +00:00
|
|
|
WatcherControl(..),
|
2012-07-20 23:29:59 +00:00
|
|
|
checkCanWatch,
|
|
|
|
needLsof,
|
|
|
|
onAddSymlink,
|
|
|
|
runHandler,
|
|
|
|
) where
|
|
|
|
|
|
|
|
import Assistant.Common
|
2012-06-13 16:36:33 +00:00
|
|
|
import Assistant.DaemonStatus
|
2012-06-19 06:40:21 +00:00
|
|
|
import Assistant.Changes
|
2012-10-29 23:30:23 +00:00
|
|
|
import Assistant.Types.Changes
|
2012-07-29 15:31:06 +00:00
|
|
|
import Assistant.Alert
|
2012-06-19 03:47:48 +00:00
|
|
|
import Utility.DirWatcher
|
2013-03-11 02:24:13 +00:00
|
|
|
import Utility.DirWatcher.Types
|
2019-08-26 19:52:19 +00:00
|
|
|
import Utility.InodeCache
|
2020-11-04 18:20:37 +00:00
|
|
|
import qualified Utility.RawFilePath as R
|
2012-06-20 23:04:16 +00:00
|
|
|
import qualified Annex
|
2012-06-13 16:36:33 +00:00
|
|
|
import qualified Annex.Queue
|
2012-12-24 18:24:13 +00:00
|
|
|
import qualified Git
|
2012-06-13 16:36:33 +00:00
|
|
|
import qualified Git.UpdateIndex
|
2012-12-24 18:24:13 +00:00
|
|
|
import qualified Git.LsFiles as LsFiles
|
2015-12-15 19:34:28 +00:00
|
|
|
import Annex.WorkTree
|
2012-06-13 16:36:33 +00:00
|
|
|
import Annex.CatFile
|
gitignore support for the assistant and watcher
Requires git 1.8.4 or newer. When it's installed, a background
git check-ignore process is run, and used to efficiently check ignores
whenever a new file is added.
Thanks to Adam Spiers, for getting the necessary support into git for this.
A complication is what to do about files that are gitignored but have
been checked into git anyway. git commands assume the ignore has been
overridden in this case, and not need any more overriding to commit a
changed version.
However, for the assistant to do the same, it would have to run git ls-files
to check if the ignored file is in git. This is somewhat expensive. Or it
could use the running git-cat-file process to query the file that way,
but that requires transferring the whole file content over a pipe, so it
can be quite expensive too, for files that are not git-annex
symlinks.
Now imagine if the user knows that a file or directory tree will be getting
frequent changes, and doesn't want the assistant to sync it, so gitignores
it. The assistant could overload the system with repeated ls-files checks!
So, I've decided that the assistant will not automatically commit changes
to files that are gitignored. This is a tradeoff. Hopefully it won't be a
problem to adjust .gitignore settings to not ignore files you want the
assistant to autocommit, or to manually git annex add files that are listed
in .gitignore.
(This could be revisited if git-annex gets access to an interface to check
the content of the index w/o forking a git command. This could be libgit2,
or perhaps a separate git cat-file --batch-check process, so it wouldn't
need to ship over the whole file content.)
This commit was sponsored by Francois Marier. Thanks!
2013-08-02 23:31:55 +00:00
|
|
|
import Annex.CheckIgnore
|
fully support core.symlinks=false in all relevant symlink handling code
Refactored annex link code into nice clean new library.
Audited and dealt with calls to createSymbolicLink.
Remaining calls are all safe, because:
Annex/Link.hs: ( liftIO $ createSymbolicLink linktarget file
only when core.symlinks=true
Assistant/WebApp/Configurators/Local.hs: createSymbolicLink link link
test if symlinks can be made
Command/Fix.hs: liftIO $ createSymbolicLink link file
command only works in indirect mode
Command/FromKey.hs: liftIO $ createSymbolicLink link file
command only works in indirect mode
Command/Indirect.hs: liftIO $ createSymbolicLink l f
refuses to run if core.symlinks=false
Init.hs: createSymbolicLink f f2
test if symlinks can be made
Remote/Directory.hs: go [file] = catchBoolIO $ createSymbolicLink file f >> return True
fast key linking; catches failure to make symlink and falls back to copy
Remote/Git.hs: liftIO $ catchBoolIO $ createSymbolicLink loc file >> return True
ditto
Upgrade/V1.hs: liftIO $ createSymbolicLink link f
v1 repos could not be on a filesystem w/o symlinks
Audited and dealt with calls to readSymbolicLink.
Remaining calls are all safe, because:
Annex/Link.hs: ( liftIO $ catchMaybeIO $ readSymbolicLink file
only when core.symlinks=true
Assistant/Threads/Watcher.hs: ifM ((==) (Just link) <$> liftIO (catchMaybeIO $ readSymbolicLink file))
code that fixes real symlinks when inotify sees them
It's ok to not fix psdueo-symlinks.
Assistant/Threads/Watcher.hs: mlink <- liftIO (catchMaybeIO $ readSymbolicLink file)
ditto
Command/Fix.hs: stopUnless ((/=) (Just link) <$> liftIO (catchMaybeIO $ readSymbolicLink file)) $ do
command only works in indirect mode
Upgrade/V1.hs: getsymlink = takeFileName <$> readSymbolicLink file
v1 repos could not be on a filesystem w/o symlinks
Audited and dealt with calls to isSymbolicLink.
(Typically used with getSymbolicLinkStatus, but that is just used because
getFileStatus is not as robust; it also works on pseudolinks.)
Remaining calls are all safe, because:
Assistant/Threads/SanityChecker.hs: | isSymbolicLink s -> addsymlink file ms
only handles staging of symlinks that were somehow not staged
(might need to be updated to support pseudolinks, but this is
only a belt-and-suspenders check anyway, and I've never seen the code run)
Command/Add.hs: if isSymbolicLink s || not (isRegularFile s)
avoids adding symlinks to the annex, so not relevant
Command/Indirect.hs: | isSymbolicLink s -> void $ flip whenAnnexed f $
only allowed on systems that support symlinks
Command/Indirect.hs: whenM (liftIO $ not . isSymbolicLink <$> getSymbolicLinkStatus f) $ do
ditto
Seek.hs:notSymlink f = liftIO $ not . isSymbolicLink <$> getSymbolicLinkStatus f
used to find unlocked files, only relevant in indirect mode
Utility/FSEvents.hs: | Files.isSymbolicLink s = runhook addSymlinkHook $ Just s
Utility/FSEvents.hs: | Files.isSymbolicLink s ->
Utility/INotify.hs: | Files.isSymbolicLink s ->
Utility/INotify.hs: checkfiletype Files.isSymbolicLink addSymlinkHook f
Utility/Kqueue.hs: | Files.isSymbolicLink s = callhook addSymlinkHook (Just s) change
all above are lower-level, not relevant
Audited and dealt with calls to isSymLink.
Remaining calls are all safe, because:
Annex/Direct.hs: | isSymLink (getmode item) =
This is looking at git diff-tree objects, not files on disk
Command/Unused.hs: | isSymLink (LsTree.mode l) = do
This is looking at git ls-tree, not file on disk
Utility/FileMode.hs:isSymLink :: FileMode -> Bool
Utility/FileMode.hs:isSymLink = checkMode symbolicLinkMode
low-level
Done!!
2013-02-17 19:05:55 +00:00
|
|
|
import Annex.Link
|
2015-12-21 22:41:15 +00:00
|
|
|
import Annex.Content
|
2013-05-17 19:59:37 +00:00
|
|
|
import Annex.ReplaceFile
|
2015-12-21 22:41:15 +00:00
|
|
|
import Annex.InodeSentinal
|
2012-06-13 16:36:33 +00:00
|
|
|
import Git.Types
|
2016-01-05 21:22:19 +00:00
|
|
|
import Git.FilePath
|
2017-02-03 17:40:14 +00:00
|
|
|
import Config.GitConfig
|
2013-01-27 11:43:05 +00:00
|
|
|
import Utility.ThreadScheduler
|
2015-12-21 22:41:15 +00:00
|
|
|
import Logs.Location
|
|
|
|
import qualified Database.Keys
|
2014-02-25 18:09:39 +00:00
|
|
|
#ifndef mingw32_HOST_OS
|
|
|
|
import qualified Utility.Lsof as Lsof
|
|
|
|
#endif
|
2012-06-13 16:36:33 +00:00
|
|
|
|
2013-01-27 11:43:05 +00:00
|
|
|
import Data.Typeable
|
2012-06-13 16:36:33 +00:00
|
|
|
import qualified Data.ByteString.Lazy as L
|
2013-01-27 11:43:05 +00:00
|
|
|
import qualified Control.Exception as E
|
2013-03-11 19:14:42 +00:00
|
|
|
import Data.Time.Clock
|
2023-03-01 19:55:58 +00:00
|
|
|
import System.PosixCompat.Files (fileMode, statusChangeTime)
|
2012-06-13 16:36:33 +00:00
|
|
|
|
2012-06-17 18:02:58 +00:00
|
|
|
checkCanWatch :: Annex ()
|
2012-06-19 03:47:48 +00:00
|
|
|
checkCanWatch
|
2012-12-14 19:52:44 +00:00
|
|
|
| canWatch = do
|
2013-12-09 20:56:15 +00:00
|
|
|
#ifndef mingw32_HOST_OS
|
2013-10-03 20:57:21 +00:00
|
|
|
liftIO Lsof.setup
|
2022-06-28 19:28:14 +00:00
|
|
|
unlessM (liftIO (inSearchPath "lsof") <||> Annex.getRead Annex.force)
|
2012-06-19 03:47:48 +00:00
|
|
|
needLsof
|
2013-12-09 20:56:15 +00:00
|
|
|
#else
|
|
|
|
noop
|
|
|
|
#endif
|
2016-11-16 01:29:54 +00:00
|
|
|
| otherwise = giveup "watch mode is not available on this system"
|
2012-06-17 18:02:58 +00:00
|
|
|
|
|
|
|
needLsof :: Annex ()
|
2016-11-16 01:29:54 +00:00
|
|
|
needLsof = giveup $ unlines
|
2012-06-17 18:02:58 +00:00
|
|
|
[ "The lsof command is needed for watch mode to be safe, and is not in PATH."
|
|
|
|
, "To override lsof checks to ensure that files are not open for writing"
|
|
|
|
, "when added to the annex, you can use --force"
|
|
|
|
, "Be warned: This can corrupt data in the annex, and make fsck complain."
|
|
|
|
]
|
|
|
|
|
2013-01-27 11:43:05 +00:00
|
|
|
{- A special exception that can be thrown to pause or resume the watcher. -}
|
2013-10-26 20:54:49 +00:00
|
|
|
data WatcherControl = PauseWatcher | ResumeWatcher
|
2014-10-09 19:09:26 +00:00
|
|
|
deriving (Show, Eq, Typeable)
|
2013-01-27 11:43:05 +00:00
|
|
|
|
2013-10-26 20:54:49 +00:00
|
|
|
instance E.Exception WatcherControl
|
2013-01-27 11:43:05 +00:00
|
|
|
|
2012-10-29 13:55:40 +00:00
|
|
|
watchThread :: NamedThread
|
2013-01-27 11:43:05 +00:00
|
|
|
watchThread = namedThread "Watcher" $
|
2017-02-03 17:40:14 +00:00
|
|
|
ifM (liftAnnex $ getGitConfigVal annexAutoCommit)
|
2013-01-27 11:43:05 +00:00
|
|
|
( runWatcher
|
|
|
|
, waitFor ResumeWatcher runWatcher
|
|
|
|
)
|
|
|
|
|
|
|
|
runWatcher :: Assistant ()
|
|
|
|
runWatcher = do
|
2012-10-30 21:14:26 +00:00
|
|
|
startup <- asIO1 startupScan
|
2013-04-10 23:57:11 +00:00
|
|
|
symlinkssupported <- liftAnnex $ coreSymlinks <$> Annex.getGitConfig
|
2021-09-27 16:52:30 +00:00
|
|
|
addhook <- hook $ onAddFile symlinkssupported
|
2012-10-29 13:55:40 +00:00
|
|
|
delhook <- hook onDel
|
2019-08-26 19:52:19 +00:00
|
|
|
addsymlinkhook <- hook onAddSymlink
|
2012-10-29 13:55:40 +00:00
|
|
|
deldirhook <- hook onDelDir
|
|
|
|
errhook <- hook onErr
|
|
|
|
let hooks = mkWatchHooks
|
|
|
|
{ addHook = addhook
|
|
|
|
, delHook = delhook
|
|
|
|
, addSymlinkHook = addsymlinkhook
|
|
|
|
, delDirHook = deldirhook
|
|
|
|
, errHook = errhook
|
2012-10-29 04:15:43 +00:00
|
|
|
}
|
2014-03-05 21:44:14 +00:00
|
|
|
scanevents <- liftAnnex $ annexStartupScan <$> Annex.getGitConfig
|
unify exception handling into Utility.Exception
Removed old extensible-exceptions, only needed for very old ghc.
Made webdav use Utility.Exception, to work after some changes in DAV's
exception handling.
Removed Annex.Exception. Mostly this was trivial, but note that
tryAnnex is replaced with tryNonAsync and catchAnnex replaced with
catchNonAsync. In theory that could be a behavior change, since the former
caught all exceptions, and the latter don't catch async exceptions.
However, in practice, nothing in the Annex monad uses async exceptions.
Grepping for throwTo and killThread only find stuff in the assistant,
which does not seem related.
Command.Add.undo is changed to accept a SomeException, and things
that use it for rollback now catch non-async exceptions, rather than
only IOExceptions.
2014-08-08 01:55:44 +00:00
|
|
|
h <- liftIO $ watchDir "." ignored scanevents hooks startup
|
2012-10-29 13:55:40 +00:00
|
|
|
debug [ "watching", "."]
|
2013-01-27 11:43:05 +00:00
|
|
|
|
|
|
|
{- Let the DirWatcher thread run until signalled to pause it,
|
|
|
|
- then wait for a resume signal, and restart. -}
|
|
|
|
waitFor PauseWatcher $ do
|
unify exception handling into Utility.Exception
Removed old extensible-exceptions, only needed for very old ghc.
Made webdav use Utility.Exception, to work after some changes in DAV's
exception handling.
Removed Annex.Exception. Mostly this was trivial, but note that
tryAnnex is replaced with tryNonAsync and catchAnnex replaced with
catchNonAsync. In theory that could be a behavior change, since the former
caught all exceptions, and the latter don't catch async exceptions.
However, in practice, nothing in the Annex monad uses async exceptions.
Grepping for throwTo and killThread only find stuff in the assistant,
which does not seem related.
Command.Add.undo is changed to accept a SomeException, and things
that use it for rollback now catch non-async exceptions, rather than
only IOExceptions.
2014-08-08 01:55:44 +00:00
|
|
|
liftIO $ stopWatchDir h
|
2013-01-27 11:43:05 +00:00
|
|
|
waitFor ResumeWatcher runWatcher
|
2012-10-29 13:55:40 +00:00
|
|
|
where
|
|
|
|
hook a = Just <$> asIO2 (runHandler a)
|
2012-06-13 16:36:33 +00:00
|
|
|
|
2013-10-26 20:54:49 +00:00
|
|
|
waitFor :: WatcherControl -> Assistant () -> Assistant ()
|
2013-01-27 11:43:05 +00:00
|
|
|
waitFor sig next = do
|
2013-10-03 02:59:07 +00:00
|
|
|
r <- liftIO (E.try pause :: IO (Either E.SomeException ()))
|
2013-01-27 11:43:05 +00:00
|
|
|
case r of
|
|
|
|
Left e -> case E.fromException e of
|
|
|
|
Just s
|
|
|
|
| s == sig -> next
|
|
|
|
_ -> noop
|
|
|
|
_ -> noop
|
|
|
|
where
|
|
|
|
pause = runEvery (Seconds 86400) noop
|
|
|
|
|
2012-06-19 02:13:39 +00:00
|
|
|
{- Initial scartup scan. The action should return once the scan is complete. -}
|
2012-10-29 13:55:40 +00:00
|
|
|
startupScan :: IO a -> Assistant a
|
|
|
|
startupScan scanner = do
|
|
|
|
liftAnnex $ showAction "scanning"
|
2012-10-29 20:49:47 +00:00
|
|
|
alertWhile' startupScanAlert $ do
|
2013-10-03 02:59:07 +00:00
|
|
|
r <- liftIO scanner
|
2012-06-19 02:13:39 +00:00
|
|
|
|
2012-07-29 23:05:51 +00:00
|
|
|
-- Notice any files that were deleted before
|
|
|
|
-- watching was started.
|
2012-12-24 18:24:13 +00:00
|
|
|
top <- liftAnnex $ fromRepo Git.repoPath
|
2020-05-28 19:55:17 +00:00
|
|
|
(fs, cleanup) <- liftAnnex $ inRepo $ LsFiles.deleted [] [top]
|
2012-12-24 18:24:13 +00:00
|
|
|
forM_ fs $ \f -> do
|
2019-12-05 19:10:23 +00:00
|
|
|
let f' = fromRawFilePath f
|
|
|
|
liftAnnex $ onDel' f'
|
|
|
|
maybe noop recordChange =<< madeChange f' RmChange
|
2013-10-03 02:59:07 +00:00
|
|
|
void $ liftIO cleanup
|
2012-12-24 18:24:13 +00:00
|
|
|
|
|
|
|
liftAnnex $ showAction "started"
|
2013-01-15 17:34:59 +00:00
|
|
|
liftIO $ putStrLn ""
|
2012-07-30 06:07:02 +00:00
|
|
|
|
2012-10-30 19:39:15 +00:00
|
|
|
modifyDaemonStatus_ $ \s -> s { scanComplete = True }
|
2012-07-30 20:32:32 +00:00
|
|
|
|
2013-12-16 19:43:28 +00:00
|
|
|
-- Ensure that the Committer sees any changes
|
|
|
|
-- that it did not process, and acts on them now that
|
|
|
|
-- the scan is complete.
|
|
|
|
refillChanges =<< getAnyChanges
|
|
|
|
|
2012-07-30 20:32:32 +00:00
|
|
|
return (True, r)
|
2012-06-19 02:13:39 +00:00
|
|
|
|
gitignore support for the assistant and watcher
Requires git 1.8.4 or newer. When it's installed, a background
git check-ignore process is run, and used to efficiently check ignores
whenever a new file is added.
Thanks to Adam Spiers, for getting the necessary support into git for this.
A complication is what to do about files that are gitignored but have
been checked into git anyway. git commands assume the ignore has been
overridden in this case, and not need any more overriding to commit a
changed version.
However, for the assistant to do the same, it would have to run git ls-files
to check if the ignored file is in git. This is somewhat expensive. Or it
could use the running git-cat-file process to query the file that way,
but that requires transferring the whole file content over a pipe, so it
can be quite expensive too, for files that are not git-annex
symlinks.
Now imagine if the user knows that a file or directory tree will be getting
frequent changes, and doesn't want the assistant to sync it, so gitignores
it. The assistant could overload the system with repeated ls-files checks!
So, I've decided that the assistant will not automatically commit changes
to files that are gitignored. This is a tradeoff. Hopefully it won't be a
problem to adjust .gitignore settings to not ignore files you want the
assistant to autocommit, or to manually git annex add files that are listed
in .gitignore.
(This could be revisited if git-annex gets access to an interface to check
the content of the index w/o forking a git command. This could be libgit2,
or perhaps a separate git cat-file --batch-check process, so it wouldn't
need to ship over the whole file content.)
This commit was sponsored by Francois Marier. Thanks!
2013-08-02 23:31:55 +00:00
|
|
|
{- Hardcoded ignores, passed to the DirWatcher so it can avoid looking
|
|
|
|
- at the entire .git directory. Does not include .gitignores. -}
|
2012-06-13 16:36:33 +00:00
|
|
|
ignored :: FilePath -> Bool
|
2012-06-18 17:01:58 +00:00
|
|
|
ignored = ig . takeFileName
|
2012-10-29 04:15:43 +00:00
|
|
|
where
|
2012-06-18 17:01:58 +00:00
|
|
|
ig ".git" = True
|
|
|
|
ig ".gitignore" = True
|
|
|
|
ig ".gitattributes" = True
|
2013-01-28 04:13:22 +00:00
|
|
|
#ifdef darwin_HOST_OS
|
|
|
|
ig ".DS_Store" = True
|
|
|
|
#endif
|
2012-06-18 17:01:58 +00:00
|
|
|
ig _ = False
|
|
|
|
|
gitignore support for the assistant and watcher
Requires git 1.8.4 or newer. When it's installed, a background
git check-ignore process is run, and used to efficiently check ignores
whenever a new file is added.
Thanks to Adam Spiers, for getting the necessary support into git for this.
A complication is what to do about files that are gitignored but have
been checked into git anyway. git commands assume the ignore has been
overridden in this case, and not need any more overriding to commit a
changed version.
However, for the assistant to do the same, it would have to run git ls-files
to check if the ignored file is in git. This is somewhat expensive. Or it
could use the running git-cat-file process to query the file that way,
but that requires transferring the whole file content over a pipe, so it
can be quite expensive too, for files that are not git-annex
symlinks.
Now imagine if the user knows that a file or directory tree will be getting
frequent changes, and doesn't want the assistant to sync it, so gitignores
it. The assistant could overload the system with repeated ls-files checks!
So, I've decided that the assistant will not automatically commit changes
to files that are gitignored. This is a tradeoff. Hopefully it won't be a
problem to adjust .gitignore settings to not ignore files you want the
assistant to autocommit, or to manually git annex add files that are listed
in .gitignore.
(This could be revisited if git-annex gets access to an interface to check
the content of the index w/o forking a git command. This could be libgit2,
or perhaps a separate git cat-file --batch-check process, so it wouldn't
need to ship over the whole file content.)
This commit was sponsored by Francois Marier. Thanks!
2013-08-02 23:31:55 +00:00
|
|
|
unlessIgnored :: FilePath -> Assistant (Maybe Change) -> Assistant (Maybe Change)
|
2020-11-04 18:20:37 +00:00
|
|
|
unlessIgnored file a = ifM (liftAnnex $ checkIgnored (CheckGitIgnore True) (toRawFilePath file))
|
gitignore support for the assistant and watcher
Requires git 1.8.4 or newer. When it's installed, a background
git check-ignore process is run, and used to efficiently check ignores
whenever a new file is added.
Thanks to Adam Spiers, for getting the necessary support into git for this.
A complication is what to do about files that are gitignored but have
been checked into git anyway. git commands assume the ignore has been
overridden in this case, and not need any more overriding to commit a
changed version.
However, for the assistant to do the same, it would have to run git ls-files
to check if the ignored file is in git. This is somewhat expensive. Or it
could use the running git-cat-file process to query the file that way,
but that requires transferring the whole file content over a pipe, so it
can be quite expensive too, for files that are not git-annex
symlinks.
Now imagine if the user knows that a file or directory tree will be getting
frequent changes, and doesn't want the assistant to sync it, so gitignores
it. The assistant could overload the system with repeated ls-files checks!
So, I've decided that the assistant will not automatically commit changes
to files that are gitignored. This is a tradeoff. Hopefully it won't be a
problem to adjust .gitignore settings to not ignore files you want the
assistant to autocommit, or to manually git annex add files that are listed
in .gitignore.
(This could be revisited if git-annex gets access to an interface to check
the content of the index w/o forking a git command. This could be libgit2,
or perhaps a separate git cat-file --batch-check process, so it wouldn't
need to ship over the whole file content.)
This commit was sponsored by Francois Marier. Thanks!
2013-08-02 23:31:55 +00:00
|
|
|
( noChange
|
|
|
|
, a
|
|
|
|
)
|
|
|
|
|
2012-10-29 13:55:40 +00:00
|
|
|
type Handler = FilePath -> Maybe FileStatus -> Assistant (Maybe Change)
|
2012-06-13 16:36:33 +00:00
|
|
|
|
2012-10-29 13:55:40 +00:00
|
|
|
{- Runs an action handler, and if there was a change, adds it to the ChangeChan.
|
2012-06-13 16:36:33 +00:00
|
|
|
-
|
|
|
|
- Exceptions are ignored, otherwise a whole watcher thread could be crashed.
|
|
|
|
-}
|
2012-10-29 13:55:40 +00:00
|
|
|
runHandler :: Handler -> FilePath -> Maybe FileStatus -> Assistant ()
|
|
|
|
runHandler handler file filestatus = void $ do
|
2013-04-02 20:20:23 +00:00
|
|
|
r <- tryIO <~> handler (normalize file) filestatus
|
2012-06-13 16:36:33 +00:00
|
|
|
case r of
|
filter out control characters in warning messages
Converted warning and similar to use StringContainingQuotedPath. Most
warnings are static strings, some do refer to filepaths that need to be
quoted, and others don't need quoting.
Note that, since quote filters out control characters of even
UnquotedString, this makes all warnings safe, even when an attacker
sneaks in a control character in some other way.
When json is being output, no quoting is done, since json gets its own
quoting.
This does, as a side effect, make warning messages in json output not
be indented. The indentation is only needed to offset warning messages
underneath the display of the file they apply to, so that's ok.
Sponsored-by: Brett Eisenberg on Patreon
2023-04-10 18:47:32 +00:00
|
|
|
Left e -> liftAnnex $ warning $ UnquotedString $ show e
|
2012-06-13 16:36:33 +00:00
|
|
|
Right Nothing -> noop
|
2016-01-13 18:55:01 +00:00
|
|
|
Right (Just change) -> recordChange change
|
2013-04-02 20:20:23 +00:00
|
|
|
where
|
2014-10-09 18:53:13 +00:00
|
|
|
normalize f
|
2013-04-02 20:20:23 +00:00
|
|
|
| "./" `isPrefixOf` file = drop 2 f
|
|
|
|
| otherwise = f
|
2012-06-13 16:36:33 +00:00
|
|
|
|
2013-11-13 18:27:17 +00:00
|
|
|
shouldRestage :: DaemonStatus -> Bool
|
|
|
|
shouldRestage ds = scanComplete ds || forceRestage ds
|
|
|
|
|
2021-09-27 16:52:30 +00:00
|
|
|
onAddFile :: Bool -> Handler
|
|
|
|
onAddFile symlinkssupported f fs = do
|
2019-12-05 19:10:23 +00:00
|
|
|
mk <- liftIO $ isPointerFile $ toRawFilePath f
|
2016-05-16 17:36:36 +00:00
|
|
|
case mk of
|
2021-09-27 16:52:30 +00:00
|
|
|
Nothing -> onAddFile' contentchanged addassociatedfile addlink samefilestatus symlinkssupported f fs
|
2016-05-16 17:36:36 +00:00
|
|
|
Just k -> addlink f k
|
2015-12-21 22:41:15 +00:00
|
|
|
where
|
2016-01-05 21:22:19 +00:00
|
|
|
addassociatedfile key file =
|
|
|
|
Database.Keys.addAssociatedFile key
|
2019-12-09 17:49:05 +00:00
|
|
|
=<< inRepo (toTopFilePath (toRawFilePath file))
|
2015-12-21 22:41:15 +00:00
|
|
|
samefilestatus key file status = do
|
|
|
|
cache <- Database.Keys.getInodeCaches key
|
2020-11-05 15:26:34 +00:00
|
|
|
curr <- withTSDelta $ \delta ->
|
|
|
|
liftIO $ toInodeCache delta (toRawFilePath file) status
|
2015-12-21 22:41:15 +00:00
|
|
|
case (cache, curr) of
|
|
|
|
(_, Just c) -> elemInodeCaches c cache
|
|
|
|
([], Nothing) -> return True
|
|
|
|
_ -> return False
|
|
|
|
contentchanged oldkey file = do
|
2016-01-05 21:22:19 +00:00
|
|
|
Database.Keys.removeAssociatedFile oldkey
|
2019-12-09 17:49:05 +00:00
|
|
|
=<< inRepo (toTopFilePath (toRawFilePath file))
|
2015-12-21 22:41:15 +00:00
|
|
|
unlessM (inAnnex oldkey) $
|
|
|
|
logStatus oldkey InfoMissing
|
2016-05-16 17:19:02 +00:00
|
|
|
addlink file key = do
|
2023-03-01 19:55:58 +00:00
|
|
|
mode <- liftIO $ catchMaybeIO $ fileMode <$> R.getFileStatus (toRawFilePath file)
|
2019-12-05 19:10:23 +00:00
|
|
|
liftAnnex $ stagePointerFile (toRawFilePath file) mode =<< hashPointerFile key
|
2016-05-16 17:19:02 +00:00
|
|
|
madeChange file $ LinkChange (Just key)
|
2015-12-21 22:41:15 +00:00
|
|
|
|
2021-09-27 16:52:30 +00:00
|
|
|
onAddFile'
|
2019-08-26 19:52:19 +00:00
|
|
|
:: (Key -> FilePath -> Annex ())
|
2016-05-16 17:19:02 +00:00
|
|
|
-> (Key -> FilePath -> Annex ())
|
|
|
|
-> (FilePath -> Key -> Assistant (Maybe Change))
|
|
|
|
-> (Key -> FilePath -> FileStatus -> Annex Bool)
|
|
|
|
-> Bool
|
|
|
|
-> Handler
|
2021-09-27 16:52:30 +00:00
|
|
|
onAddFile' contentchanged addassociatedfile addlink samefilestatus symlinkssupported file fs = do
|
2019-12-05 19:10:23 +00:00
|
|
|
v <- liftAnnex $ catKeyFile (toRawFilePath file)
|
2012-12-25 19:48:15 +00:00
|
|
|
case (v, fs) of
|
|
|
|
(Just key, Just filestatus) ->
|
2015-12-21 22:41:15 +00:00
|
|
|
ifM (liftAnnex $ samefilestatus key file filestatus)
|
2013-04-02 16:58:56 +00:00
|
|
|
{- It's possible to get an add event for
|
|
|
|
- an existing file that is not
|
|
|
|
- really modified, but it might have
|
|
|
|
- just been deleted and been put back,
|
2016-05-16 17:19:02 +00:00
|
|
|
- so its annex link is restaged to make sure. -}
|
2013-11-13 18:27:17 +00:00
|
|
|
( ifM (shouldRestage <$> getDaemonStatus)
|
2016-05-16 17:19:02 +00:00
|
|
|
( addlink file key
|
2013-04-25 01:20:29 +00:00
|
|
|
, noChange
|
|
|
|
)
|
2013-04-10 23:57:11 +00:00
|
|
|
, guardSymlinkStandin (Just key) $ do
|
2015-12-21 22:41:15 +00:00
|
|
|
debug ["changed", file]
|
|
|
|
liftAnnex $ contentchanged key file
|
2021-07-13 16:15:40 +00:00
|
|
|
pendingAddChange file
|
2012-12-25 19:48:15 +00:00
|
|
|
)
|
gitignore support for the assistant and watcher
Requires git 1.8.4 or newer. When it's installed, a background
git check-ignore process is run, and used to efficiently check ignores
whenever a new file is added.
Thanks to Adam Spiers, for getting the necessary support into git for this.
A complication is what to do about files that are gitignored but have
been checked into git anyway. git commands assume the ignore has been
overridden in this case, and not need any more overriding to commit a
changed version.
However, for the assistant to do the same, it would have to run git ls-files
to check if the ignored file is in git. This is somewhat expensive. Or it
could use the running git-cat-file process to query the file that way,
but that requires transferring the whole file content over a pipe, so it
can be quite expensive too, for files that are not git-annex
symlinks.
Now imagine if the user knows that a file or directory tree will be getting
frequent changes, and doesn't want the assistant to sync it, so gitignores
it. The assistant could overload the system with repeated ls-files checks!
So, I've decided that the assistant will not automatically commit changes
to files that are gitignored. This is a tradeoff. Hopefully it won't be a
problem to adjust .gitignore settings to not ignore files you want the
assistant to autocommit, or to manually git annex add files that are listed
in .gitignore.
(This could be revisited if git-annex gets access to an interface to check
the content of the index w/o forking a git command. This could be libgit2,
or perhaps a separate git cat-file --batch-check process, so it wouldn't
need to ship over the whole file content.)
This commit was sponsored by Francois Marier. Thanks!
2013-08-02 23:31:55 +00:00
|
|
|
_ -> unlessIgnored file $
|
|
|
|
guardSymlinkStandin Nothing $ do
|
2015-12-21 22:41:15 +00:00
|
|
|
debug ["add", file]
|
2021-07-13 16:15:40 +00:00
|
|
|
pendingAddChange file
|
2013-04-10 23:57:11 +00:00
|
|
|
where
|
2014-10-09 18:53:13 +00:00
|
|
|
{- On a filesystem without symlinks, we'll get changes for regular
|
2013-04-10 23:57:11 +00:00
|
|
|
- files that git uses to stand-in for symlinks. Detect when
|
|
|
|
- this happens, and stage the symlink, rather than annexing the
|
|
|
|
- file. -}
|
|
|
|
guardSymlinkStandin mk a
|
|
|
|
| symlinkssupported = a
|
|
|
|
| otherwise = do
|
2019-12-05 19:10:23 +00:00
|
|
|
linktarget <- liftAnnex $ getAnnexLinkTarget $
|
|
|
|
toRawFilePath file
|
2013-04-10 23:57:11 +00:00
|
|
|
case linktarget of
|
|
|
|
Nothing -> a
|
|
|
|
Just lt -> do
|
2022-02-23 16:24:09 +00:00
|
|
|
case parseLinkTargetOrPointer lt of
|
2013-04-10 23:57:11 +00:00
|
|
|
Nothing -> noop
|
2015-12-21 22:41:15 +00:00
|
|
|
Just key -> liftAnnex $
|
|
|
|
addassociatedfile key file
|
2020-11-04 18:20:37 +00:00
|
|
|
onAddSymlink' (Just lt) mk file fs
|
2012-12-25 19:48:15 +00:00
|
|
|
|
2012-06-13 16:36:33 +00:00
|
|
|
{- A symlink might be an arbitrary symlink, which is just added.
|
|
|
|
- Or, if it is a git-annex symlink, ensure it points to the content
|
|
|
|
- before adding it.
|
|
|
|
-}
|
2019-08-26 19:52:19 +00:00
|
|
|
onAddSymlink :: Handler
|
|
|
|
onAddSymlink file filestatus = unlessIgnored file $ do
|
2020-11-04 18:20:37 +00:00
|
|
|
linktarget <- liftIO (catchMaybeIO $ R.readSymbolicLink file')
|
|
|
|
kv <- liftAnnex (lookupKey file')
|
2019-08-26 19:52:19 +00:00
|
|
|
onAddSymlink' linktarget kv file filestatus
|
2020-11-04 18:20:37 +00:00
|
|
|
where
|
|
|
|
file' = toRawFilePath file
|
2013-04-10 23:57:11 +00:00
|
|
|
|
2020-11-04 18:20:37 +00:00
|
|
|
onAddSymlink' :: Maybe LinkTarget -> Maybe Key -> Handler
|
2019-08-26 19:52:19 +00:00
|
|
|
onAddSymlink' linktarget mk file filestatus = go mk
|
2012-10-29 04:15:43 +00:00
|
|
|
where
|
2014-10-09 18:53:13 +00:00
|
|
|
go (Just key) = do
|
2020-11-04 18:20:37 +00:00
|
|
|
link <- liftAnnex $ calcRepo $ gitAnnexLink (toRawFilePath file) key
|
2013-04-10 23:57:11 +00:00
|
|
|
if linktarget == Just link
|
|
|
|
then ensurestaged (Just link) =<< getDaemonStatus
|
|
|
|
else do
|
2020-03-06 15:31:01 +00:00
|
|
|
liftAnnex $ replaceWorkTreeFile file $
|
2023-10-26 17:36:49 +00:00
|
|
|
makeAnnexLink link
|
2013-04-02 16:58:56 +00:00
|
|
|
addLink file link (Just key)
|
2013-04-10 23:57:11 +00:00
|
|
|
-- other symlink, not git-annex
|
|
|
|
go Nothing = ensurestaged linktarget =<< getDaemonStatus
|
2012-10-29 04:15:43 +00:00
|
|
|
|
|
|
|
{- This is often called on symlinks that are already
|
|
|
|
- staged correctly. A symlink may have been deleted
|
|
|
|
- and being re-added, or added when the watcher was
|
|
|
|
- not running. So they're normally restaged to make sure.
|
|
|
|
-
|
|
|
|
- As an optimisation, during the startup scan, avoid
|
|
|
|
- restaging everything. Only links that were created since
|
|
|
|
- the last time the daemon was running are staged.
|
|
|
|
- (If the daemon has never ran before, avoid staging
|
|
|
|
- links too.)
|
|
|
|
-}
|
2013-04-10 23:57:11 +00:00
|
|
|
ensurestaged (Just link) daemonstatus
|
2013-11-13 18:27:17 +00:00
|
|
|
| shouldRestage daemonstatus = addLink file link mk
|
2012-10-29 04:15:43 +00:00
|
|
|
| otherwise = case filestatus of
|
2012-10-29 15:58:29 +00:00
|
|
|
Just s
|
2012-10-29 23:30:23 +00:00
|
|
|
| not (afterLastDaemonRun (statusChangeTime s) daemonstatus) -> noChange
|
2013-04-02 16:58:56 +00:00
|
|
|
_ -> addLink file link mk
|
2013-04-10 23:57:11 +00:00
|
|
|
ensurestaged Nothing _ = noChange
|
2012-10-29 04:15:43 +00:00
|
|
|
|
2013-04-02 16:58:56 +00:00
|
|
|
{- For speed, tries to reuse the existing blob for symlink target. -}
|
2020-11-04 18:20:37 +00:00
|
|
|
addLink :: FilePath -> LinkTarget -> Maybe Key -> Assistant (Maybe Change)
|
2013-04-02 16:58:56 +00:00
|
|
|
addLink file link mk = do
|
|
|
|
debug ["add symlink", file]
|
|
|
|
liftAnnex $ do
|
2021-08-11 00:45:02 +00:00
|
|
|
v <- catObjectDetails $ Ref $ encodeBS $ ':':file
|
2013-04-02 16:58:56 +00:00
|
|
|
case v of
|
2013-10-20 21:50:51 +00:00
|
|
|
Just (currlink, sha, _type)
|
2020-11-04 18:20:37 +00:00
|
|
|
| L.fromStrict link == currlink ->
|
2019-12-05 19:10:23 +00:00
|
|
|
stageSymlink (toRawFilePath file) sha
|
2020-11-03 22:34:27 +00:00
|
|
|
_ -> stageSymlink (toRawFilePath file)
|
2020-11-04 18:20:37 +00:00
|
|
|
=<< hashSymlink link
|
2013-04-02 16:58:56 +00:00
|
|
|
madeChange file $ LinkChange mk
|
2012-07-05 16:58:49 +00:00
|
|
|
|
2012-06-13 16:36:33 +00:00
|
|
|
onDel :: Handler
|
2012-10-29 13:55:40 +00:00
|
|
|
onDel file _ = do
|
|
|
|
debug ["file deleted", file]
|
2013-04-24 21:46:46 +00:00
|
|
|
liftAnnex $ onDel' file
|
2012-10-29 23:30:23 +00:00
|
|
|
madeChange file RmChange
|
2012-06-13 16:36:33 +00:00
|
|
|
|
2013-04-24 21:46:46 +00:00
|
|
|
onDel' :: FilePath -> Annex ()
|
|
|
|
onDel' file = do
|
2019-12-09 17:49:05 +00:00
|
|
|
topfile <- inRepo (toTopFilePath (toRawFilePath file))
|
2019-08-30 17:54:57 +00:00
|
|
|
withkey $ flip Database.Keys.removeAssociatedFile topfile
|
2013-04-24 21:46:46 +00:00
|
|
|
Annex.Queue.addUpdateIndex =<<
|
2023-02-27 19:02:53 +00:00
|
|
|
inRepo (Git.UpdateIndex.unstageFile (toRawFilePath file))
|
2015-12-21 22:41:15 +00:00
|
|
|
where
|
2019-12-05 19:10:23 +00:00
|
|
|
withkey a = maybe noop a =<< catKeyFile (toRawFilePath file)
|
2013-04-24 21:46:46 +00:00
|
|
|
|
2012-06-13 16:36:33 +00:00
|
|
|
{- A directory has been deleted, or moved, so tell git to remove anything
|
|
|
|
- that was inside it from its cache. Since it could reappear at any time,
|
2013-03-11 19:14:42 +00:00
|
|
|
- use --cached to only delete it from the index.
|
2012-06-13 16:36:33 +00:00
|
|
|
-
|
2013-03-11 19:14:42 +00:00
|
|
|
- This queues up a lot of RmChanges, which assists the Committer in
|
|
|
|
- pairing up renamed files when the directory was renamed. -}
|
2012-06-13 16:36:33 +00:00
|
|
|
onDelDir :: Handler
|
2012-10-29 13:55:40 +00:00
|
|
|
onDelDir dir _ = do
|
|
|
|
debug ["directory deleted", dir]
|
2020-05-28 19:55:17 +00:00
|
|
|
(fs, clean) <- liftAnnex $ inRepo $ LsFiles.deleted [] [toRawFilePath dir]
|
2019-12-05 19:10:23 +00:00
|
|
|
let fs' = map fromRawFilePath fs
|
2013-03-11 19:14:42 +00:00
|
|
|
|
2019-12-05 19:10:23 +00:00
|
|
|
liftAnnex $ mapM_ onDel' fs'
|
2013-03-11 19:14:42 +00:00
|
|
|
|
|
|
|
-- Get the events queued up as fast as possible, so the
|
|
|
|
-- committer sees them all in one block.
|
|
|
|
now <- liftIO getCurrentTime
|
2019-12-05 19:10:23 +00:00
|
|
|
recordChanges $ map (\f -> Change now f RmChange) fs'
|
2013-03-11 19:14:42 +00:00
|
|
|
|
2013-10-03 02:59:07 +00:00
|
|
|
void $ liftIO clean
|
2013-03-11 19:14:42 +00:00
|
|
|
noChange
|
2012-06-13 16:36:33 +00:00
|
|
|
|
2012-09-06 17:56:23 +00:00
|
|
|
{- Called when there's an error with inotify or kqueue. -}
|
2012-06-13 16:36:33 +00:00
|
|
|
onErr :: Handler
|
2012-10-29 13:55:40 +00:00
|
|
|
onErr msg _ = do
|
filter out control characters in warning messages
Converted warning and similar to use StringContainingQuotedPath. Most
warnings are static strings, some do refer to filepaths that need to be
quoted, and others don't need quoting.
Note that, since quote filters out control characters of even
UnquotedString, this makes all warnings safe, even when an attacker
sneaks in a control character in some other way.
When json is being output, no quoting is done, since json gets its own
quoting.
This does, as a side effect, make warning messages in json output not
be indented. The indentation is only needed to offset warning messages
underneath the display of the file they apply to, so that's ok.
Sponsored-by: Brett Eisenberg on Patreon
2023-04-10 18:47:32 +00:00
|
|
|
liftAnnex $ warning (UnquotedString msg)
|
2012-10-30 19:39:15 +00:00
|
|
|
void $ addAlert $ warningAlert "watcher" msg
|
2012-10-29 23:30:23 +00:00
|
|
|
noChange
|