git-lfs/t/t-tempfile.sh
brian m. carlson a7f5200b9c
fs: be a little less aggressive with cleanup
When we invoke the file helper to download files from a remote system,
we use the repository's temporary directory in order to make sure we can
quickly and easily rename files into the object store without the need
for a copy, which might be necessary if we used the system temporary
directory.  In many cases, these files we generate are actually hard
links to the remote repository, which means we can cheaply and easily
copy files at maximum speed.  Note, however, that these files don't look
like a normal object ID; instead, they have a name generated by Go's
temporary file code.

However, our current code causes a problem if, during the pull, a file
is checked out and Git causes it to be uselessly cleaned, which happens
in some cases.  That's because our temporary file cleanup code will
remove all files in the temporary directory that don't look like normal
object ID components, and as a result, the clean operation can remove
files that are in use by the fetch.

Instead of immediately purging anything that looks like it's not a valid
object ID, let's wait an hour before pruning any temporary file unless
it's an object that we've already downloaded and verified is in our
object store.

We also need to do one more thing here, which is that we need to ignore
any file that lives in a directory younger than an hour old and adjust
our file helper to create a new directory for its temporary files.  This
is important because if we create a hard link to an object in a remote
repository, it may be older than one hour, and we don't want to prune
those if we're not done yet.

Note that we use a sync.Map to make this as efficient as possible and
avoid the need for many additional stat calls.  Ideally, we won't
actually see an increase in stat calls at all.
2021-04-30 14:36:06 +00:00

35 lines
785 B
Bash
Executable File

#!/usr/bin/env bash
. "$(dirname "$0")/testlib.sh"
begin_test "cleans only temp files and directories older than an hour"
(
set -e
reponame="$(basename "$0" ".sh")"
git init "$reponame"
cd "$reponame"
git lfs track '*.bin'
echo foo > abc.bin
git add abc.bin
git commit -m 'Add abc.bin'
tmpdir=.git/lfs/tmp
mkdir -p "$tmpdir"
mkdir "$tmpdir/dir-to-preserve"
touch "$tmpdir/to-preserve"
touch "$tmpdir/dir-to-preserve/file"
# git format-patch datestamp; arbitrary timestamp in the past.
TZ=UTC touch -t 200109170000.00 "$tmpdir/to-destroy"
TZ=UTC touch -t 200109170000.00 "$tmpdir/dir-to-preserve/file"
git lfs ls-files >/dev/null
[ -f "$tmpdir/to-preserve" ]
[ -f "$tmpdir/dir-to-preserve/file" ]
[ ! -f "$tmpdir/to-destroy" ]
)
end_test