Subject: [PATCH 00/14] Introduce a tempfile module

From: Michael Haggerty <[email-masked]>
Date: Mon, 8 Jun 2015 11:07:31 +0200

We have spent a lot of effort defining the state diagram for lockfiles
and ensuring correct, race-resistant cleanup in all circumstances. Now
let's abstract out part of the lockfile module so that it can be used
to clean up arbitrary temporary files.

This patch series

* implements a new "tempfile" module

* re-implements the "lockfile" module on top of tempfile

* changes a number of places in the code that manage temporary files
  to use the new module.

This project was suggested by Peff as a 2014 GSoC project [1], but
nobody took it up. It was not suggested again as a project for GSoC
2015.

There are still a number of other call sites that could be rewritten
to use the new module. But I think it's better to get the new module
out there and rewrite the other call sites as time allows rather than
for me to keep sitting on these patches in the naive hope that I will
get around to rewriting all possible users.

Patch 06/14 adds a number of mkstemp()-like functions that work with
tempfile objects rather than just returning paths. Since wrapper.c
already contains many variants of mkstemp()-like functions, the new
module does as well. These functions basically have four switches that
can be turned on/off independently:

* Can the caller specify the file mode of the new file?

* Does the filename template include a suffix?

* Does the filename template include the full path to the file, or is
  the file created in a temporary directory?

* Does the function die on failure?

Hopefully the new module will be easier to use, not only because it
takes care of cleaning the temporary file up automatically, but also
because its functions are named more systematically. The following
table might help people trying to make sense of things:

| wrapper function  | die? | location | suffix? | mode? | tempfile function |
| ----------------- | ---- | -------- | ------- | ----- | ----------------- |
| mkstemp           |      |          |         |       | mks_tempfile      |
| git_mkstemp_mode  |      |          |         | yes   | mks_tempfile_m    |
| mkstemps          |      |          | yes     |       | mks_tempfile_s    |
| gitmkstemps (†)   |      |          | yes     |       | mks_tempfile_s    |
| git_mkstemps_mode |      |          | yes     | yes   | mks_tempfile_sm   |
| git_mkstemp       |      | $TMPDIR  |         |       | mks_tempfile_t    |
| N/A               |      | $TMPDIR  |         | yes   | mks_tempfile_tm   |
| git_mkstemps      |      | $TMPDIR  | yes     |       | mks_tempfile_ts   |
| N/A               |      | $TMPDIR  | yes     | yes   | mks_tempfile_tsm  |
| xmkstemp          | yes  |          |         |       | xmks_tempfile     |
| xmkstemp_mode     | yes  |          |         | yes   | xmks_tempfile_m   |

If the large number of new functions is too intimidating (even though
most of the functions are inline), it would be possible to decrease
the number. For example, we could add a "flags" argument that covers
"location" and "die?". We could also get rid of the no-suffix
variants, requiring all callers to use the suffix variant, setting
suffixlen to 0 if no suffix is desired.

These patches are also available from my GitHub repo [2], branch
"tempfile".

[1] http://git.github.io/SoC-2014-Ideas.html
[2] https://github.com/mhagger/git

Michael Haggerty (14):
  Move lockfile API documentation to lockfile.h
  tempfile: a new module for handling temporary files
  lockfile: remove some redundant functions
  commit_lock_file(): use get_locked_file_path()
  register_tempfile_object(): new function, extracted from
    create_tempfile()
  tempfile: add several functions for creating temporary files
  register_tempfile(): new function to handle an existing temporary file
  write_shared_index(): use tempfile module
  setup_temporary_shallow(): use tempfile module
  diff: use tempfile module
  lock_repo_for_gc(): compute the path to "gc.pid" only once
  gc: use tempfile module to handle gc.pid file
  credential-cache--daemon: delete socket from main()
  credential-cache--daemon: use tempfile module

 Documentation/technical/api-lockfile.txt | 220 ----------------------
 Makefile                                 |   1 +
 builtin/add.c                            |   1 +
 builtin/apply.c                          |   1 +
 builtin/checkout-index.c                 |   1 +
 builtin/checkout.c                       |   1 +
 builtin/clone.c                          |   1 +
 builtin/commit.c                         |  15 +-
 builtin/describe.c                       |   1 +
 builtin/diff.c                           |   1 +
 builtin/gc.c                             |  32 +---
 builtin/merge.c                          |   1 +
 builtin/mv.c                             |   1 +
 builtin/read-tree.c                      |   1 +
 builtin/receive-pack.c                   |   1 +
 builtin/reflog.c                         |   1 +
 builtin/reset.c                          |   1 +
 builtin/rm.c                             |   1 +
 builtin/update-index.c                   |   1 +
 bundle.c                                 |   3 +-
 cache-tree.c                             |   1 +
 config.c                                 |  15 +-
 credential-cache--daemon.c               |  25 +--
 credential-store.c                       |   3 +-
 diff.c                                   |  29 +--
 fast-import.c                            |   3 +-
 fetch-pack.c                             |   1 +
 lockfile.c                               | 198 +++-----------------
 lockfile.h                               | 260 +++++++++++++++++++-------
 merge-recursive.c                        |   1 +
 merge.c                                  |   1 +
 read-cache.c                             |  42 +----
 refs.c                                   |  23 +--
 rerere.c                                 |   1 +
 sequencer.c                              |   1 +
 sha1_file.c                              |   1 +
 shallow.c                                |  41 +---
 tempfile.c                               | 231 +++++++++++++++++++++++
 tempfile.h                               | 310 +++++++++++++++++++++++++++++++
 test-scrap-cache-tree.c                  |   1 +
 40 files changed, 857 insertions(+), 617 deletions(-)
 delete mode 100644 Documentation/technical/api-lockfile.txt
 create mode 100644 tempfile.c
 create mode 100644 tempfile.h

-- 
2.1.4