git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 00/14] Introduce a tempfile module

From
Michael Haggerty <mhagger@alum.mit.edu>
Date
Jun 8, 2015, 09:07 UTC
Message-ID
<cover.1433751986.git.mhagger@alum.mit.edu>

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
Next: Michael Haggerty
Message 1 of 26 in “Introduce a tempfile module”
  1. 00/14 Introduce a tempfile moduleMichael Haggerty, Jun 8, 2015
  2. 01/14 Move lockfile API documentation to lockfile.hMichael Haggerty, Jun 8, 2015
  3. 02/14 tempfile: a new module for handling temporary filesMichael Haggerty, Jun 8, 2015
  4. Junio C HamanoJun 10, 2015
  5. Michael HaggertyJun 10, 2015
  6. Junio C HamanoJun 10, 2015
  7. 03/14 lockfile: remove some redundant functionsMichael Haggerty, Jun 8, 2015
  8. Junio C HamanoJun 10, 2015
  9. Johannes SixtJun 10, 2015
  10. 04/14 commit_lock_file(): use get_locked_file_path()Michael Haggerty, Jun 8, 2015
  11. 05/14 register_tempfile_object(): new function, extracted from create_tempfile()Michael Haggerty, Jun 8, 2015
  12. 06/14 tempfile: add several functions for creating temporary filesMichael Haggerty, Jun 8, 2015
  13. Junio C HamanoJun 10, 2015
  14. Michael HaggertyAug 10, 2015
  15. 07/14 register_tempfile(): new function to handle an existing temporary fileMichael Haggerty, Jun 8, 2015
  16. Junio C HamanoJun 10, 2015
  17. Michael HaggertyAug 10, 2015
  18. 08/14 write_shared_index(): use tempfile moduleMichael Haggerty, Jun 8, 2015
  19. Junio C HamanoJun 10, 2015
  20. 09/14 setup_temporary_shallow(): use tempfile moduleMichael Haggerty, Jun 8, 2015
  21. 10/14 diff: use tempfile moduleMichael Haggerty, Jun 8, 2015
  22. 11/14 lock_repo_for_gc(): compute the path to "gc.pid" only onceMichael Haggerty, Jun 8, 2015
  23. 12/14 gc: use tempfile module to handle gc.pid fileMichael Haggerty, Jun 8, 2015
  24. 13/14 credential-cache--daemon: delete socket from main()Michael Haggerty, Jun 8, 2015
  25. 14/14 credential-cache--daemon: use tempfile moduleMichael Haggerty, Jun 8, 2015
  26. Junio C HamanoJun 10, 2015

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.