{- git-annex file replacing
-
- - Copyright 2013-2021 Joey Hess <id@joeyh.name>
+ - Copyright 2013-2025 Joey Hess <id@joeyh.name>
-
- Licensed under the GNU AGPL version 3 or higher.
-}
-{-# LANGUAGE CPP #-}
-
module Annex.ReplaceFile (
replaceGitAnnexDirFile,
replaceGitDirFile,
import Annex.Tmp
import Annex.Perms
import Git
+import Utility.Tmp
import Utility.Tmp.Dir
import Utility.Directory.Create
-#ifndef mingw32_HOST_OS
-import Utility.Path.Max
-#endif
+
+import qualified System.FilePath.ByteString as P
{- replaceFile on a file located inside the gitAnnexDir. -}
replaceGitAnnexDirFile :: FilePath -> (RawFilePath -> Annex a) -> Annex a
replaceFile' :: (RawFilePath -> Annex ()) -> FilePath -> (a -> Bool) -> (RawFilePath -> Annex a) -> Annex a
replaceFile' createdirectory file checkres action = withOtherTmp $ \othertmpdir -> do
- let othertmpdir' = fromRawFilePath othertmpdir
-#ifndef mingw32_HOST_OS
- -- Use part of the filename as the template for the temp
- -- directory. This does not need to be unique, but it
- -- makes it more clear what this temp directory is for.
- filemax <- liftIO $ fileNameLengthLimit othertmpdir'
- let basetmp = take (filemax `div` 2) (takeFileName file)
-#else
- -- Windows has limits on the whole path length, so keep
- -- it short.
- let basetmp = "t"
-#endif
- withTmpDirIn othertmpdir' (toOsPath (toRawFilePath basetmp)) $ \tmpdir -> do
- let tmpfile = toRawFilePath (tmpdir </> basetmp)
+ let basetmp = relatedTemplate' (toRawFilePath file)
+ withTmpDirIn (fromRawFilePath othertmpdir) (toOsPath basetmp) $ \tmpdir -> do
+ let tmpfile = toRawFilePath tmpdir P.</> basetmp
r <- action tmpfile
when (checkres r) $
replaceFileFrom tmpfile (toRawFilePath file) createdirectory
viaTmp,
withTmpFile,
withTmpFileIn,
- relatedTemplate,
openTmpFileIn,
+ relatedTemplate,
+ relatedTemplate',
) where
import System.IO
- This generates a template that is never too long.
-}
relatedTemplate :: RawFilePath -> Template
-relatedTemplate f
+relatedTemplate = toOsPath . relatedTemplate'
+
+relatedTemplate' :: RawFilePath -> RawFilePath
+relatedTemplate' f
| len > templateAddedLength =
{- Some filesystems like FAT have issues with filenames
- ending in ".", so avoid truncating a filename to end
- that way. -}
- toOsPath $ B.dropWhileEnd (== dot) $
+ B.dropWhileEnd (== dot) $
truncateFilePath (len - templateAddedLength) f
- | otherwise = toOsPath f
+ | otherwise = f
where
len = B.length f
dot = fromIntegral (ord '.')