]> git.ipfire.org Git - thirdparty/git.git/commit - builtin/clone.c
clone: initialize atexit cleanup handler earlier
authorJeff King <peff@peff.net>
Wed, 18 Mar 2015 18:55:32 +0000 (14:55 -0400)
committerJunio C Hamano <gitster@pobox.com>
Thu, 19 Mar 2015 20:38:07 +0000 (13:38 -0700)
commitee0e38727f1a67ddd3b8b7f6ecea34624b2a8b51
tree5e92dca24ac8b8a57659b5a6bc58ad2da8614024
parent282616c72d1d08a77ca4fe1186cb708c38408d87
clone: initialize atexit cleanup handler earlier

If clone fails, we generally try to clean up any directories
we've created. We do this by installing an atexit handler,
so that we don't have to manually trigger cleanup. However,
since we install this after touching the filesystem, any
errors between our initial mkdir() and our atexit() call
will result in us leaving a crufty directory around.

We can fix this by moving our atexit() call earlier. It's OK
to do it before the junk_work_tree variable is set, because
remove_junk makes sure the variable is initialized. This
means we "activate" the handler by assigning to the
junk_work_tree variable, which we now bump down to just
after we call mkdir(). We probably do not want to do it
before, because a plausible reason for mkdir() to fail is
EEXIST (i.e., we are racing with another "git init"), and we
would not want to remove their work.

OTOH, this is probably not that big a deal; we will allow
cloning into an empty directory (and skip the mkdir), which
is already racy (i.e., one clone may see the other's empty
dir and start writing into it). Still, it does not hurt to
err on the side of caution here.

Note that writing into junk_work_tree and junk_git_dir after
installing the handler is also technically racy, as we call
our handler on an async signal.  Depending on the platform,
we could see a sheared write to the variables. Traditionally
we have not worried about this, and indeed we already do
this later in the function. If we want to address that, it
can come as a separate topic.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/clone.c