Introduce a tempfile module
Subsystems (2)
v2 cover Introduce a tempfile module
This is a re-roll of the tempfile patch series [1]. I'm sorry for the long delay getting v2 out. Thanks to Junio and Johannes Sixt for their feedback about v1. I think I have addressed all of their points.
This version is very similar to v1 in spirit, though quite a few details have changed. The main difference is that I add some more wrapper functions for both lockfile and tempfile (a) to add some abstraction and (b) so that users of the former don't need to know that it is based on the latter:
* Add new lockfile wrappers around the corresponding tempfile functions:
* lockfile:
* fdopen_lock_file()
* close_lock_file()
* reopen_lock_file()
* Add accessors:
* lockfile:
* get_lock_file_path()
* get_lock_file_fd()
* get_lock_file_fp()
* tempfile:
* is_tempfile_active()
* get_tempfile_path()
* get_tempfile_fd()
* get_tempfile_fp()
Other changes in this version:
* Make some trivial wrapper functions inline.
* Change create_bundle() to dup() the file descriptor that it passes to write_pack_data() so that it doesn't have to tinker with lock->tempfile.fd to prevent the file from being closed twice.
* Move some docs about the implementation from tempfile.h to tempfile.c.
* Rename register_tempfile_object() to prepare_tempfile_object() to reduce confusion with register_tempfile(). Remove its "path" parameter and add a docstring.
* Simplify some die("BUG:...") error messages.
This series applies to the same commit as v1, namely v2.4.3-368-g7974889. There is one small conflict when merging to master or next or (pu minus gitster/mh/tempfile).
This patch series is also available from my GitHub fork [2] as branch "tempfile".
[1] http://thread.gmane.org/gmane.comp.version-control.git/270998
[2] https://github.com/mhagger/git
Michael Haggerty (16):
Move lockfile documentation to lockfile.h and lockfile.c create_bundle(): duplicate file descriptor to avoid closing it twice
lockfile: add accessors get_lock_file_fd() and get_lock_file_fp()
lockfile: add accessor get_lock_file_path() commit_lock_file(): use get_locked_file_path()
tempfile: a new module for handling temporary files prepare_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/commit.c | 15 +- builtin/gc.c | 32 +-- bundle.c | 26 ++- config.c | 14 +- credential-cache--daemon.c | 25 +-- credential-store.c | 2 +- diff.c | 29 +-- lockfile.c | 205 +++---------------- lockfile.h | 336 +++++++++++++++++++++++++------ read-cache.c | 40 +--- refs.c | 18 +- shallow.c | 41 +--- tempfile.c | 305 ++++++++++++++++++++++++++++ tempfile.h | 271 +++++++++++++++++++++++++ 16 files changed, 951 insertions(+), 629 deletions(-) delete mode 100644 Documentation/technical/api-lockfile.txt create mode 100644 tempfile.c create mode 100644 tempfile.h
Signature · 1 line
2.5.0
PATCH v2 1/16 Move lockfile documentation to lockfile.h and lockfile.c
Rearrange/rewrite it somewhat to fit its new environment.
--- Documentation/technical/api-lockfile.txt | 220 ----------------------- lockfile.c | 53 ++++++ lockfile.h | 290 ++++++++++++++++++++++++------- 3 files changed, 283 insertions(+), 280 deletions(-) delete mode 100644 Documentation/technical/api-lockfile.txt
Documentation/technical/api-lockfile.txt deleted +0 -220
@@ -1,220 +0,0 @@ -lockfile API -============ - -The lockfile API serves two purposes: - -* Mutual exclusion and atomic file updates. When we want to change a - file, we create a lockfile `<filename>.lock`, write the new file - contents into it, and then rename the lockfile to its final - destination `<filename>`. We create the `<filename>.lock` file with - `O_CREAT|O_EXCL` so that we can notice and fail if somebody else has - already locked the file, then atomically rename the lockfile to its - final destination to commit the changes and unlock the file. - -* Automatic cruft removal. If the program exits after we lock a file - but before the changes have been committed, we want to make sure - that we remove the lockfile. This is done by remembering the - lockfiles we have created in a linked list and setting up an - `atexit(3)` handler and a signal handler that clean up the - lockfiles. This mechanism ensures that outstanding lockfiles are - cleaned up if the program exits (including when `die()` is called) - or if the program dies on a signal. - -Please note that lockfiles only block other writers. Readers do not -block, but they are guaranteed to see either the old contents of the -file or the new contents of the file (assuming that the filesystem -implements `rename(2)` atomically). - - -Calling sequence ----------------- - -The caller: - -* Allocates a `struct lock_file` either as a static variable or on the - heap, initialized to zeros. Once you use the structure to call the - `hold_lock_file_*` family of functions, it belongs to the lockfile - subsystem and its storage must remain valid throughout the life of - the program (i.e. you cannot use an on-stack variable to hold this - structure). - -* Attempts to create a lockfile by passing that variable and the path - of the final destination (e.g. `$GIT_DIR/index`) to - `hold_lock_file_for_update` or `hold_lock_file_for_append`. - -* Writes new content for the destination file by either: - - * writing to the file descriptor returned by the `hold_lock_file_*` - functions (also available via `lock->fd`). - - * calling `fdopen_lock_file` to get a `FILE` pointer for the open - file and writing to the file using stdio. - -When finished writing, the caller can: - -* Close the file descriptor and rename the lockfile to its final - destination by calling `commit_lock_file` or `commit_lock_file_to`. - -* Close the file descriptor and remove the lockfile by calling - `rollback_lock_file`. - -* Close the file descriptor without removing or renaming the lockfile - by calling `close_lock_file`, and later call `commit_lock_file`, - `commit_lock_file_to`, `rollback_lock_file`, or `reopen_lock_file`. - -Even after the lockfile is committed or rolled back, the `lock_file` -object must not be freed or altered by the caller. However, it may be -reused; just pass it to another call of `hold_lock_file_for_update` or -`hold_lock_file_for_append`. - -If the program exits before you have called one of `commit_lock_file`, -`commit_lock_file_to`, `rollback_lock_file`, or `close_lock_file`, an -`atexit(3)` handler will close and remove the lockfile, rolling back -any uncommitted changes. - -If you need to close the file descriptor you obtained from a -`hold_lock_file_*` function yourself, do so by calling -`close_lock_file`. You should never call `close(2)` or `fclose(3)` -yourself! Otherwise the `struct lock_file` structure would still think -that the file descriptor needs to be closed, and a commit or rollback -would result in duplicate calls to `close(2)`. Worse yet, if you close -and then later open another file descriptor for a completely different -purpose, then a commit or rollback might close that unrelated file -descriptor. - - -Error handling --------------- - -The `hold_lock_file_*` functions return a file descriptor on success -or -1 on failure (unless `LOCK_DIE_ON_ERROR` is used; see below). On -errors, `errno` describes the reason for failure. Errors can be -reported by passing `errno` to one of the following helper functions: - -unable_to_lock_message:: - - Append an appropriate error message to a `strbuf`. - -unable_to_lock_error:: - - Emit an appropriate error message using `error()`. - -unable_to_lock_die:: - - Emit an appropriate error message and `die()`. - -Similarly, `commit_lock_file`, `commit_lock_file_to`, and -`close_lock_file` return 0 on success. On failure they set `errno` -appropriately, do their best to roll back the lockfile, and return -1. - - -Flags ------ - -The following flags can be passed to `hold_lock_file_for_update` or -`hold_lock_file_for_append`: - -LOCK_NO_DEREF:: - - Usually symbolic links in the destination path are resolved - and the lockfile is created by adding ".lock" to the resolved - path. If `LOCK_NO_DEREF` is set, then the lockfile is created - by adding ".lock" to the path argument itself. This option is - used, for example, when locking a symbolic reference, which - for backwards-compatibility reasons can be a symbolic link - containing the name of the referred-to-reference. - -LOCK_DIE_ON_ERROR:: - - If a lock is already taken for the file, `die()` with an error - message. If this option is not specified, trying to lock a - file that is already locked returns -1 to the caller. - - -The functions -------------- - -hold_lock_file_for_update:: - - Take a pointer to `struct lock_file`, the path of the file to - be locked (e.g. `$GIT_DIR/index`) and a flags argument (see - above). Attempt to create a lockfile for the destination and - return the file descriptor for writing to the file. - -hold_lock_file_for_append:: - - Like `hold_lock_file_for_update`, but before returning copy - the existing contents of the file (if any) to the lockfile and - position its write pointer at the end of the file. - -fdopen_lock_file:: - - Associate a stdio stream with the lockfile. Return NULL - (*without* rolling back the lockfile) on error. The stream is - closed automatically when `close_lock_file` is called or when - the file is committed or rolled back. - -get_locked_file_path:: - - Return the path of the file that is locked by the specified - lock_file object. The caller must free the memory. - -commit_lock_file:: - - Take a pointer to the `struct lock_file` initialized with an - earlier call to `hold_lock_file_for_update` or - `hold_lock_file_for_append`, close the file descriptor, and - rename the lockfile to its final destination. Return 0 upon - success. On failure, roll back the lock file and return -1, - with `errno` set to the value from the failing call to - `close(2)` or `rename(2)`. It is a bug to call - `commit_lock_file` for a `lock_file` object that is not - currently locked. - -commit_lock_file_to:: - - Like `commit_lock_file()`, except that it takes an explicit - `path` argument to which the lockfile should be renamed. The - `path` must be on the same filesystem as the lock file. - -rollback_lock_file:: - - Take a pointer to the `struct lock_file` initialized with an - earlier call to `hold_lock_file_for_update` or - `hold_lock_file_for_append`, close the file descriptor and - remove the lockfile. It is a NOOP to call - `rollback_lock_file()` for a `lock_file` object that has - already been committed or rolled back. - -close_lock_file:: - - Take a pointer to the `struct lock_file` initialized with an - earlier call to `hold_lock_file_for_update` or - `hold_lock_file_for_append`. Close the file descriptor (and - the file pointer if it has been opened using - `fdopen_lock_file`). Return 0 upon success. On failure to - `close(2)`, return a negative value and roll back the lock - file. Usually `commit_lock_file`, `commit_lock_file_to`, or - `rollback_lock_file` should eventually be called if - `close_lock_file` succeeds. - -reopen_lock_file:: - - Re-open a lockfile that has been closed (using - `close_lock_file`) but not yet committed or rolled back. This - can be used to implement a sequence of operations like the - following: - - * Lock file. - - * Write new contents to lockfile, then `close_lock_file` to - cause the contents to be written to disk. - - * Pass the name of the lockfile to another program to allow it - (and nobody else) to inspect the contents you wrote, while - still holding the lock yourself. - - * `reopen_lock_file` to reopen the lockfile. Make further - updates to the contents. - - * `commit_lock_file` to make the final version permanent.
lockfile.c +53 -0
59 lines not on this page · raw message
lockfile.h +230 -60
315 lines not on this page · raw message
Signature · 1 line
2.5.0
Re: [PATCH v2 01/16] Move lockfile documentation to lockfile.h and lockfile.c
Quoted 29 lines · Michael Haggerty
Rearrange/rewrite it somewhat to fit its new environment.
...lockfile.h +16
-0@@ -4,54 +4,103 @@@@ -68,16 +117,51 @@ struct lock_file {#define LOCK_SUFFIX ".lock"#define LOCK_SUFFIX_LEN 5 + +/* +* Flags +* ----- +* +* The following flags can be passed to `hold_lock_file_for_update()` +* or `hold_lock_file_for_append()`. +*/ + +/* +* If a lock is already taken for the file, `die()` with an error +* message. If this flag is not specified, trying to lock a file that +* is already locked returns -1 to the caller. +*/ #define LOCK_DIE_ON_ERROR 1 + +/*
lockfile.h struct lock_file { · line 145
Quoted 10 lines
lockfile.h +9
-0+* Usually symbolic links in the destination path are resolved. This +* means that (1) the lockfile is created by adding ".lock" to the +* resolved path, and (2) upon commit, the resolved path is +* overwritten. However, if `LOCK_NO_DEREF` is set, then the lockfile +* is created by adding ".lock" to the path argument itself. This +* option is used, for example, when detaching a symbolic reference, +* which for backwards-compatibility reasons, can be a symbolic link +* containing the name of the referred-to-reference. +*/
Thanks. I really like the way these per-item descriptions explain each item much better. The old documentation may have contained all the same info, but a better organization makes a big difference.
PATCH v2 2/16 create_bundle(): duplicate file descriptor to avoid closing it twice
In reply to Michael Haggertywrite_pack_data() passes bundle_fd to start_command() to be used as the stdout of pack-objects. But start_command() closes its stdout if it is > 1. This is a problem if bundle_fd is the fd of a lock_file, because commit_lock_file() will also try to close the fd.
So the old code suppressed commit_lock_file()'s usual behavior of closing the file descriptor by setting the lock_file object's fd field to -1.
But this is not really kosher. Code here shouldn't be mutating fields within the lock_file object.
Instead, duplicate the file descriptor before passing it to write_pack_data(). Then that function can close its copy without closing the copy held in the lock_file object.
---
This is new since v1. I like that it is better decoupled than the old code, but let me know if you think otherwise.
Actually, it seems to me that start_command()'s special case of not closing fd==0 is weird. I suppose that is because fd==0 is used to mean "no redirections" whereas 0 also happens to be the fd for stdin. But I don't want to dig into that now.
bundle.c | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-)
bundle.c +16 -10
@@ -235,7 +235,9 @@ out:return result; } -static int write_pack_data (int bundle_fd,struct lock_file *lock,struct rev_info *revs) + +/* Write the pack data to bundle_fd, then close it if it is > 1. */ +static int write_pack_data (int bundle_fd,struct rev_info *revs) {struct child_process pack_objects =CHILD_PROCESS_INIT ;int i;
@@ -250,13 +252,6 @@ static int write_pack_data(int bundle_fd, struct lock_file *lock, struct rev_infif (start_command (&pack_objects))return error (_ ("Could not spawn pack-objects" )); -/* -* start_command closed bundle_fd if it was > 1 -* so set the lock fd to -1 so commit_lock_file() -* won't fail trying to close it. -*/ - lock->fd =-1 ; -for (i =0 ; i < revs->pending.nr; i++) {struct object *object = revs->pending.objects[i].item;if (object->flags &UNINTERESTING )
@@ -416,10 +411,21 @@ int create_bundle(struct bundle_header *header, const char *path, bundle_to_stdout = !strcmp (path,"-" );if (bundle_to_stdout) bundle_fd =1 ; -else +else { bundle_fd =hold_lock_file_for_update (&lock, path,LOCK_DIE_ON_ERROR ); +/* +* write_pack_data() will close the fd passed to it, +* but commit_lock_file() will also try to close the +* lockfile's fd. So make a copy of the file +* descriptor to avoid trying to close it twice. +*/ + bundle_fd =dup (bundle_fd); +if (bundle_fd <0 ) +die_errno ("unable to dup file descriptor" ); + } +/* write signature */ write_or_die (bundle_fd, bundle_signature,strlen (bundle_signature));
@@ -445,7 +451,7 @@ int create_bundle(struct bundle_header *header, const char *path,return -1 ;/* write pack */ -if (write_pack_data (bundle_fd, &lock, &revs)) +if (write_pack_data (bundle_fd, &revs))return -1 ;if (!bundle_to_stdout) {
Signature · 1 line
2.5.0
PATCH v2 3/16 lockfile: add accessors get_lock_file_fd() and get_lock_file_fp()
In reply to Michael HaggertyWe are about to move those members, so change client code to read them through accessor functions.
--- credential-store.c | 2 +- lockfile.c | 14 ++++++++++++++ lockfile.h | 3 +++ read-cache.c | 2 +- refs.c | 12 +++++++----- 5 files changed, 26 insertions(+), 7 deletions(-)
credential-store.c +1 -1
@@ -52,7 +52,7 @@ static void print_entry(struct credential *c)static void print_line (struct strbuf *buf) {strbuf_addch (buf,'\n' ); -write_or_die (credential_lock.fd, buf->buf, buf->len); +write_or_die (get_lock_file_fd (&credential_lock), buf->buf, buf->len); } static void rewrite_credential_file(const char *fn, struct credential *c,
lockfile.c +14 -0
@@ -364,6 +364,20 @@ FILE *fdopen_lock_file(struct lock_file *lk, const char *mode)return lk->fp; } +int get_lock_file_fd (struct lock_file *lk) +{ +if (!lk->active) +die ("BUG: get_lock_file_fd() called for unlocked object" ); +return lk->fd; +} + +FILE *get_lock_file_fp (struct lock_file *lk) +{ +if (!lk->active) +die ("BUG: get_lock_file_fp() called for unlocked object" ); +return lk->fp; +} +char *get_locked_file_path (struct lock_file *lk) {if (!lk->active)
lockfile.h +3 -0
@@ -203,6 +203,9 @@ extern NORETURN void unable_to_lock_die(const char *path, int err);*/ extern FILE *fdopen_lock_file (struct lock_file *lk,const char *mode); +extern int get_lock_file_fd (struct lock_file *lk); +extern FILE *get_lock_file_fp (struct lock_file *lk); +/* * Return the path of the file that is locked by the specified * lock_file object. The caller must free the memory.
read-cache.c +1 -1
@@ -2112,7 +2112,7 @@ static int commit_locked_index(struct lock_file *lk)static int do_write_locked_index (struct index_state *istate,struct lock_file *lock,unsigned flags) { -int ret =do_write_index (istate, lock->fd,0 ); +int ret =do_write_index (istate,get_lock_file_fd (lock),0 );if (ret)return ret; assert((flags & (COMMIT_LOCK | CLOSE_LOCK)) !=
refs.c +7 -5
@@ -3162,6 +3162,7 @@ static int write_ref_to_lockfile(struct ref_lock *lock, {static char term ='\n' ;struct object *o; +int fd; o =parse_object (sha1);if (!o) {
@@ -3178,8 +3179,9 @@ static int write_ref_to_lockfile(struct ref_lock *lock, errno =EINVAL ;return -1 ; } -if (write_in_full (lock->lk->fd,sha1_to_hex (sha1),40 ) !=40 || -write_in_full (lock->lk->fd, &term,1 ) !=1 || + fd =get_lock_file_fd (lock->lk); +if (write_in_full (fd,sha1_to_hex (sha1),40 ) !=40 || +write_in_full (fd, &term,1 ) !=1 ||close_ref (lock) <0 ) {int save_errno = errno;error ("Couldn't write %s" , lock->lk->filename.buf);
@@ -4264,10 +4266,10 @@ int reflog_expire(const char *refname, const unsigned char *sha1, status |=error ("couldn't write %s: %s" , log_file,strerror (errno)); }else if (update && - (write_in_full (lock->lk->fd, + (write_in_full (get_lock_file_fd (lock->lk),sha1_to_hex (cb.last_kept_sha1),40 ) !=40 || -write_str_in_full (lock->lk->fd,"\n" ) !=1 || -close_ref (lock) <0 )) { +write_str_in_full (get_lock_file_fd (lock->lk),"\n" ) !=1 || +close_ref (lock) <0 )) { status |=error ("couldn't write %s" , lock->lk->filename.buf);rollback_lock_file (&reflog_lock);
Signature · 1 line
2.5.0
Re: [PATCH v2 03/16] lockfile: add accessors get_lock_file_fd() and get_lock_file_fp()
Quoted 2 lines · Michael Haggerty
We are about to move those members, so change client code to read them through accessor functions.
Hmph, _fp() variant does not seem to be used at all at this step, though.
PATCH v2 4/16 lockfile: add accessor get_lock_file_path()
In reply to Michael Haggerty--- builtin/commit.c | 15 ++++++++------- config.c | 14 +++++++------- lockfile.c | 7 +++++++ lockfile.h | 6 ++++++ refs.c | 6 +++--- shallow.c | 6 +++--- 6 files changed, 34 insertions(+), 20 deletions(-)
builtin/commit.c +8 -7
@@ -324,6 +324,7 @@ static const char *prepare_index(int argc, const char **argv, const char *prefixstruct string_list partial;struct pathspec pathspec;int refresh_flags =REFRESH_QUIET ; +const char *ret;if (is_status) refresh_flags |=REFRESH_UNMERGED ;
@@ -344,7 +345,7 @@ static const char *prepare_index(int argc, const char **argv, const char *prefixdie (_ ("unable to create temporary index" )); old_index_env =getenv (INDEX_ENVIRONMENT ); -setenv (INDEX_ENVIRONMENT , index_lock.filename.buf,1 ); +setenv (INDEX_ENVIRONMENT ,get_lock_file_path (&index_lock),1 );if (interactive_add (argc, argv, prefix, patch_interactive) !=0 )die (_ ("interactive add failed" ));
@@ -355,7 +356,7 @@ static const char *prepare_index(int argc, const char **argv, const char *prefixunsetenv (INDEX_ENVIRONMENT );discard_cache (); -read_cache_from (index_lock.filename.buf); +read_cache_from (get_lock_file_path (&index_lock));if (update_main_cache_tree (WRITE_TREE_SILENT ) ==0 ) {if (reopen_lock_file (&index_lock) <0 )die (_ ("unable to write index file" ));
@@ -365,7 +366,7 @@ static const char *prepare_index(int argc, const char **argv, const char *prefixwarning (_ ("Failed to update main cache tree" )); commit_style =COMMIT_NORMAL ; -return index_lock.filename.buf; +return get_lock_file_path (&index_lock); }/*
@@ -388,7 +389,7 @@ static const char *prepare_index(int argc, const char **argv, const char *prefixif (write_locked_index (&the_index, &index_lock,CLOSE_LOCK ))die (_ ("unable to write new_index file" )); commit_style =COMMIT_NORMAL ; -return index_lock.filename.buf; +return get_lock_file_path (&index_lock); }/*
@@ -475,9 +476,9 @@ static const char *prepare_index(int argc, const char **argv, const char *prefixdie (_ ("unable to write temporary index file" ));discard_cache (); -read_cache_from (false_lock.filename.buf); - -return false_lock.filename.buf; + ret =get_lock_file_path (&false_lock); +read_cache_from (ret); +return ret; } static int run_status(FILE *fp, const char *index_file, const char *prefix, int nowarn,
config.c +7 -7
@@ -2056,9 +2056,9 @@ int git_config_set_multivar_in_file(const char *config_filename, MAP_PRIVATE, in_fd, 0);close (in_fd); -if (chmod (lock->filename.buf, st.st_mode &07777 ) <0 ) { +if (chmod (get_lock_file_path (lock), st.st_mode &07777 ) <0 ) {error ("chmod on %s failed: %s" , - lock->filename.buf,strerror (errno)); +get_lock_file_path (lock),strerror (errno)); ret =CONFIG_NO_WRITE ; goto out_free; }
@@ -2138,7 +2138,7 @@ out_free:return ret; write_err_out: - ret =write_error (lock->filename.buf); + ret =write_error (get_lock_file_path (lock)); goto out_free; }
@@ -2239,9 +2239,9 @@ int git_config_rename_section_in_file(const char *config_filename,fstat (fileno (config_file), &st); -if (chmod (lock->filename.buf, st.st_mode &07777 ) <0 ) { +if (chmod (get_lock_file_path (lock), st.st_mode &07777 ) <0 ) { ret =error ("chmod on %s failed: %s" , - lock->filename.buf,strerror (errno)); +get_lock_file_path (lock),strerror (errno)); goto out; }
@@ -2262,7 +2262,7 @@ int git_config_rename_section_in_file(const char *config_filename, } store.baselen =strlen (new_name);if (!store_write_section (out_fd, new_name)) { - ret =write_error (lock->filename.buf); + ret =write_error (get_lock_file_path (lock)); goto out; }/*
@@ -2288,7 +2288,7 @@ int git_config_rename_section_in_file(const char *config_filename,continue ; length =strlen (output);if (write_in_full (out_fd, output, length) != length) { - ret =write_error (lock->filename.buf); + ret =write_error (get_lock_file_path (lock)); goto out; } }
lockfile.c +7 -0
@@ -364,6 +364,13 @@ FILE *fdopen_lock_file(struct lock_file *lk, const char *mode)return lk->fp; } +const char *get_lock_file_path (struct lock_file *lk) +{ +if (!lk->active) +die ("BUG: get_lock_file_path() called for unlocked object" ); +return lk->filename.buf; +} +int get_lock_file_fd (struct lock_file *lk) {if (!lk->active)
lockfile.h +6 -0
@@ -203,6 +203,12 @@ extern NORETURN void unable_to_lock_die(const char *path, int err);*/ extern FILE *fdopen_lock_file (struct lock_file *lk,const char *mode); +/* +* Return the path of the lockfile. The return value is a pointer to a +* field within the lock_file object and should not be freed. +*/ +extern const char *get_lock_file_path (struct lock_file *lk); +extern int get_lock_file_fd (struct lock_file *lk);extern FILE *get_lock_file_fp (struct lock_file *lk);
refs.c +3 -3
@@ -3184,7 +3184,7 @@ static int write_ref_to_lockfile(struct ref_lock *lock, write_in_full(fd, &term, 1) != 1 || close_ref(lock) < 0) {int save_errno = errno; -error ("Couldn't write %s" , lock->lk->filename.buf); +error ("Couldn't write %s" ,get_lock_file_path (lock->lk));unlock_ref (lock); errno = save_errno;return -1 ;
@@ -4241,7 +4241,7 @@ int reflog_expire(const char *refname, const unsigned char *sha1, cb.newlog =fdopen_lock_file (&reflog_lock,"w" );if (!cb.newlog) {error ("cannot fdopen %s (%s)" , - reflog_lock.filename.buf,strerror (errno)); +get_lock_file_path (&reflog_lock),strerror (errno)); goto failure; } }
@@ -4271,7 +4271,7 @@ int reflog_expire(const char *refname, const unsigned char *sha1, write_str_in_full(get_lock_file_fd(lock->lk), "\n") != 1 || close_ref(lock) < 0)) { status |=error ("couldn't write %s" , - lock->lk->filename.buf); +get_lock_file_path (lock->lk));rollback_lock_file (&reflog_lock); } else if (commit_lock_file(&reflog_lock)) { status |= error("unable to commit reflog '%s' (%s)",
shallow.c +3 -3
@@ -267,8 +267,8 @@ void setup_alternate_shallow(struct lock_file *shallow_lock,if (write_shallow_commits (&sb,0 , extra)) {if (write_in_full (fd, sb.buf, sb.len) != sb.len)die_errno ("failed to write to %s" , - shallow_lock->filename.buf); - *alternate_shallow_file = shallow_lock->filename.buf; +get_lock_file_path (shallow_lock)); + *alternate_shallow_file =get_lock_file_path (shallow_lock); }else /* * is_repository_shallow() sees empty string as "no
@@ -314,7 +314,7 @@ void prune_shallow(int show_only)if (write_shallow_commits_1 (&sb,0 ,NULL ,SEEN_ONLY )) {if (write_in_full (fd, sb.buf, sb.len) != sb.len)die_errno ("failed to write to %s" , - shallow_lock.filename.buf); +get_lock_file_path (&shallow_lock));commit_lock_file (&shallow_lock); }else {unlink (git_path ("shallow" ));
Signature · 1 line
2.5.0
Re: [PATCH v2 04/16] lockfile: add accessor get_lock_file_path()
Quoted 2 lines · Michael Haggerty
Signed-off-by Michael Haggerty <[email protected]>---
I was briefly confused by the similarity between get_locked_file_path() and this new helper ;-) but names of both make sense to me.
PATCH v2 5/16 commit_lock_file(): use get_locked_file_path()
In reply to Michael HaggertyFirst beef up the sanity checking in get_locked_file_path() to match that in commit_lock_file(). Then rewrite commit_lock_file() to use get_locked_file_path() for its pathname computation.
--- lockfile.c | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-)
lockfile.c +12 -16
@@ -389,8 +389,10 @@ char *get_locked_file_path(struct lock_file *lk) {if (!lk->active)die ("BUG: get_locked_file_path() called for unlocked object" ); -if (lk->filename.len <=LOCK_SUFFIX_LEN ) +if (lk->filename.len <=LOCK_SUFFIX_LEN || +strcmp (lk->filename.buf + lk->filename.len -LOCK_SUFFIX_LEN ,LOCK_SUFFIX ))die ("BUG: get_locked_file_path() called for malformed lock object" ); +/* remove ".lock": */ return xmemdupz (lk->filename.buf, lk->filename.len -LOCK_SUFFIX_LEN ); }
@@ -458,22 +460,16 @@ int commit_lock_file_to(struct lock_file *lk, const char *path)int commit_lock_file (struct lock_file *lk) { -static struct strbuf result_file =STRBUF_INIT ; -int err; +char *result_path =get_locked_file_path (lk); -if (!lk->active) -die ("BUG: attempt to commit unlocked object" ); - -if (lk->filename.len <=LOCK_SUFFIX_LEN || -strcmp (lk->filename.buf + lk->filename.len -LOCK_SUFFIX_LEN ,LOCK_SUFFIX )) -die ("BUG: lockfile filename corrupt" ); - -/* remove ".lock": */ -strbuf_add (&result_file, lk->filename.buf, - lk->filename.len -LOCK_SUFFIX_LEN ); - err =commit_lock_file_to (lk, result_file.buf); -strbuf_reset (&result_file); -return err; +if (commit_lock_file_to (lk, result_path)) { +int save_errno = errno; +free (result_path); + errno = save_errno; +return -1 ; + } +free (result_path); +return 0 ; }void rollback_lock_file (struct lock_file *lk)
Signature · 1 line
2.5.0
PATCH v2 6/16 tempfile: a new module for handling temporary files
In reply to Michael HaggertyA lot of work went into defining the state diagram for lockfiles and ensuring correct, race-resistant cleanup in all circumstances.
Most of that infrastructure can be applied directly to *any* temporary file. So extract a new "tempfile" module from the "lockfile" module. Reimplement lockfile on top of tempfile.
Subsequent commits will add more users of the new module.
--- Makefile | 1 + lockfile.c | 261 ++++--------------------------------------------------------- lockfile.h | 73 +++++++++++------ tempfile.c | 238 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ tempfile.h | 167 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 470 insertions(+), 270 deletions(-) create mode 100644 tempfile.c create mode 100644 tempfile.h
Makefile +1 -0
@@ -786,6 +786,7 @@ LIB_OBJS += string-list.o LIB_OBJS += submodule.o LIB_OBJS += symlinks.o LIB_OBJS += tag.o +LIB_OBJS += tempfile.o LIB_OBJS += trace.o LIB_OBJS += trailer.o LIB_OBJS += transport.o
lockfile.c +16 -245
290 lines not on this page · raw message
lockfile.h +48 -25
130 lines not on this page · raw message
tempfile.c new file +238 -0
238 lines not on this page · raw message
tempfile.h new file +167 -0
167 lines not on this page · raw message
Signature · 1 line
2.5.0
PATCH v2 7/16 prepare_tempfile_object(): new function, extracted from create_tempfile()
In reply to Michael HaggertyThis makes the next step easier.
The old code used to use "path" to set the initial length of tempfile->filename. This was not helpful because path was usually relative whereas the value stored to filename will be absolute. So just initialize the length to 0.
--- tempfile.c | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-)
tempfile.c +13 -7
@@ -85,11 +85,11 @@ static void remove_tempfiles_on_signal(int signo)raise (signo); } -/* Make sure errno contains a meaningful value on error */ -int create_tempfile (struct tempfile *tempfile,const char *path) +/* +* Initialize *tempfile if necessary and add it to tempfile_list. +*/ +static void prepare_tempfile_object (struct tempfile *tempfile) { -size_t pathlen =strlen (path); -if (!tempfile_list) {/* One-time initialization */ sigchain_push_common (remove_tempfiles_on_signal);
@@ -97,21 +97,27 @@ int create_tempfile(struct tempfile *tempfile, const char *path) }if (tempfile->active) -die ("BUG: create_tempfile called for active object" ); +die ("BUG: prepare_tempfile_object called for active object" );if (!tempfile->on_list) {/* Initialize *tempfile and add it to tempfile_list: */ tempfile->fd =-1 ; tempfile->fp =NULL ; tempfile->active =0 ; tempfile->owner =0 ; -strbuf_init (&tempfile->filename, pathlen); +strbuf_init (&tempfile->filename,0 ); tempfile->next = tempfile_list; tempfile_list = tempfile; tempfile->on_list =1 ; }else if (tempfile->filename.len) {/* This shouldn't happen, but better safe than sorry. */ -die ("BUG: create_tempfile called for improperly-reset object" ); +die ("BUG: prepare_tempfile_object called for improperly-reset object" ); } +} + +/* Make sure errno contains a meaningful value on error */ +int create_tempfile (struct tempfile *tempfile,const char *path) +{ +prepare_tempfile_object (tempfile);strbuf_add_absolute_path (&tempfile->filename, path); tempfile->fd =open (tempfile->filename.buf,O_RDWR |O_CREAT |O_EXCL ,0666 );
Signature · 1 line
2.5.0
Re: [PATCH v2 07/16] prepare_tempfile_object(): new function, extracted from create_tempfile()
Quoted 6 lines · Michael Haggerty
This makes the next step easier.
The old code used to use "path" to set the initial length of
tempfile->filename. This was not helpful because path was usually relative whereas the value stored to filename will be absolute. So just initialize the length to 0.
Makes sense.
PATCH v2 8/16 tempfile: add several functions for creating temporary files
In reply to Michael HaggertyAdd several functions for creating temporary files with automatically-generated names, analogous to mkstemps(), but also arranging for the files to be deleted on program exit.
The functions are named according to a pattern depending how they operate. They will be used to replace many places in the code where temporary files are created and cleaned up ad-hoc.
--- tempfile.c | 53 ++++++++++++++++++++++++++++++++++ tempfile.h | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 149 insertions(+)
tempfile.c +53 -0
@@ -137,6 +137,59 @@ int create_tempfile(struct tempfile *tempfile, const char *path)return tempfile->fd; } +int mks_tempfile_sm (struct tempfile *tempfile, +const char *template,int suffixlen,int mode) +{ +prepare_tempfile_object (tempfile); + +strbuf_add_absolute_path (&tempfile->filename, template); + tempfile->fd =git_mkstemps_mode (tempfile->filename.buf, suffixlen, mode); +if (tempfile->fd <0 ) { +strbuf_reset (&tempfile->filename); +return -1 ; + } + tempfile->owner =getpid (); + tempfile->active =1 ; +return tempfile->fd; +} + +int mks_tempfile_tsm (struct tempfile *tempfile, +const char *template,int suffixlen,int mode) +{ +const char *tmpdir; + +prepare_tempfile_object (tempfile); + + tmpdir =getenv ("TMPDIR" ); +if (!tmpdir) + tmpdir ="/tmp" ; + +strbuf_addf (&tempfile->filename,"%s/%s" , tmpdir, template); + tempfile->fd =git_mkstemps_mode (tempfile->filename.buf, suffixlen, mode); +if (tempfile->fd <0 ) { +strbuf_reset (&tempfile->filename); +return -1 ; + } + tempfile->owner =getpid (); + tempfile->active =1 ; +return tempfile->fd; +} + +int xmks_tempfile_m (struct tempfile *tempfile,const char *template,int mode) +{ +int fd; +struct strbuf full_template =STRBUF_INIT ; + +strbuf_add_absolute_path (&full_template, template); + fd =mks_tempfile_m (tempfile, full_template.buf, mode); +if (fd <0 ) +die_errno ("Unable to create temporary file '%s'" , + full_template.buf); + +strbuf_release (&full_template); +return fd; +} +FILE *fdopen_tempfile (struct tempfile *tempfile,const char *mode) {if (!tempfile->active)
tempfile.h +96 -0
102 lines not on this page · raw message
Signature · 1 line
2.5.0
PATCH v2 9/16 register_tempfile(): new function to handle an existing temporary file
In reply to Michael HaggertyAllow an existing file to be registered with the tempfile-handling infrastructure; in particular, arrange for it to be deleted on program exit. This can be used if the temporary file has to be created in a more complicated way than just open(). For example:
* If the file itself needs to be created via the lockfile API
* If it is not a regular file (e.g., a socket)
--- tempfile.c | 8 ++++++++ tempfile.h | 8 ++++++++ 2 files changed, 16 insertions(+)
tempfile.c +8 -0
@@ -137,6 +137,14 @@ int create_tempfile(struct tempfile *tempfile, const char *path)return tempfile->fd; } +void register_tempfile (struct tempfile *tempfile,const char *path) +{ +prepare_tempfile_object (tempfile); +strbuf_add_absolute_path (&tempfile->filename, path); + tempfile->owner =getpid (); + tempfile->active =1 ; +} +int mks_tempfile_sm (struct tempfile *tempfile,const char *template,int suffixlen,int mode) {
tempfile.h +8 -0
@@ -92,6 +92,14 @@ struct tempfile {*/ extern int create_tempfile (struct tempfile *tempfile,const char *path); +/* +* Register an existing file as a tempfile, meaning that it will be +* deleted when the program exits. The tempfile is considered closed, +* but it can be worked with like any other closed tempfile (for +* example, it can be opened using reopen_tempfile()). +*/ +extern void register_tempfile (struct tempfile *tempfile,const char *path); +/* * mks_tempfile functions
Signature · 1 line
2.5.0
PATCH v2 10/16 write_shared_index(): use tempfile module
In reply to Michael Haggerty--- read-cache.c | 38 ++++++-------------------------------- 1 file changed, 6 insertions(+), 32 deletions(-)
read-cache.c +6 -32
@@ -5,6 +5,7 @@*/ #define NO_THE_INDEX_COMPATIBILITY_MACROS #include "cache.h" +#include "tempfile.h" #include "lockfile.h" #include "cache-tree.h" #include "refs.h"
@@ -2136,54 +2137,27 @@ static int write_split_index(struct index_state *istate,return ret; } -static char *temporary_sharedindex; - -static void remove_temporary_sharedindex (void ) -{ -if (temporary_sharedindex) { -unlink_or_warn (temporary_sharedindex); -free (temporary_sharedindex); - temporary_sharedindex =NULL ; - } -} - -static void remove_temporary_sharedindex_on_signal (int signo) -{ -remove_temporary_sharedindex (); -sigchain_pop (signo); -raise (signo); -} +static struct tempfile temporary_sharedindex;static int write_shared_index (struct index_state *istate,struct lock_file *lock,unsigned flags) {struct split_index *si = istate->split_index; -static int installed_handler;int fd, ret; - temporary_sharedindex =git_pathdup ("sharedindex_XXXXXX" ); - fd =mkstemp (temporary_sharedindex); + fd =mks_tempfile (&temporary_sharedindex,git_path ("sharedindex_XXXXXX" ));if (fd <0 ) { -free (temporary_sharedindex); - temporary_sharedindex =NULL ;hashclr (si->base_sha1);return do_write_locked_index (istate, lock, flags); } -if (!installed_handler) { -atexit (remove_temporary_sharedindex); -sigchain_push_common (remove_temporary_sharedindex_on_signal); - }move_cache_to_base_index (istate); ret =do_write_index (si->base, fd,1 ); -close (fd);if (ret) { -remove_temporary_sharedindex (); +delete_tempfile (&temporary_sharedindex);return ret; } - ret =rename (temporary_sharedindex, -git_path ("sharedindex.%s" ,sha1_to_hex (si->base->sha1))); -free (temporary_sharedindex); - temporary_sharedindex =NULL ; + ret =rename_tempfile (&temporary_sharedindex, +git_path ("sharedindex.%s" ,sha1_to_hex (si->base->sha1)));if (!ret)hashcpy (si->base_sha1, si->base->sha1);return ret;
Signature · 1 line
2.5.0
PATCH v2 11/16 setup_temporary_shallow(): use tempfile module
In reply to Michael Haggerty--- shallow.c | 35 +++++++---------------------------- 1 file changed, 7 insertions(+), 28 deletions(-)
shallow.c +7 -28
@@ -1,4 +1,5 @@#include "cache.h" +#include "tempfile.h" #include "lockfile.h" #include "commit.h" #include "tag.h"
@@ -208,50 +209,28 @@ int write_shallow_commits(struct strbuf *out, int use_pack_protocol,return write_shallow_commits_1 (out, use_pack_protocol, extra,0 ); } -static struct strbuf temporary_shallow =STRBUF_INIT ; - -static void remove_temporary_shallow (void ) -{ -if (temporary_shallow.len) { -unlink_or_warn (temporary_shallow.buf); -strbuf_reset (&temporary_shallow); - } -} - -static void remove_temporary_shallow_on_signal (int signo) -{ -remove_temporary_shallow (); -sigchain_pop (signo); -raise (signo); -} +static struct tempfile temporary_shallow;const char *setup_temporary_shallow (const struct sha1_array *extra) {struct strbuf sb =STRBUF_INIT ;int fd; -if (temporary_shallow.len) -die ("BUG: attempt to create two temporary shallow files" ); -if (write_shallow_commits (&sb,0 , extra)) { -strbuf_addstr (&temporary_shallow,git_path ("shallow_XXXXXX" )); - fd =xmkstemp (temporary_shallow.buf); - -atexit (remove_temporary_shallow); -sigchain_push_common (remove_temporary_shallow_on_signal); + fd =xmks_tempfile (&temporary_shallow,git_path ("shallow_XXXXXX" ));if (write_in_full (fd, sb.buf, sb.len) != sb.len)die_errno ("failed to write to %s" , - temporary_shallow.buf); -close (fd); +get_tempfile_path (&temporary_shallow)); +close_tempfile (&temporary_shallow);strbuf_release (&sb); -return temporary_shallow.buf; +return get_tempfile_path (&temporary_shallow); }/* * is_repository_shallow() sees empty string as "no shallow * file". */ -return temporary_shallow.buf; +return get_tempfile_path (&temporary_shallow); } void setup_alternate_shallow(struct lock_file *shallow_lock,
Signature · 1 line
2.5.0
PATCH v2 12/16 diff: use tempfile module
In reply to Michael Haggerty--- diff.c | 29 +++++++---------------------- 1 file changed, 7 insertions(+), 22 deletions(-)
diff.c +7 -22
@@ -2,6 +2,7 @@* Copyright (C) 2005 Junio C Hamano */ #include "cache.h" +#include "tempfile.h" #include "quote.h" #include "diff.h" #include "diffcore.h"
@@ -312,7 +313,7 @@ static struct diff_tempfile {const char *name;/* filename external diff should read from */ char hex[41 ];char mode[10 ]; -char tmp_path[PATH_MAX ]; +struct tempfile tempfile; } diff_temp[2 ];typedef unsigned long (*sane_truncate_fn )(char *line,unsigned long len);
@@ -564,25 +565,16 @@ static struct diff_tempfile *claim_diff_tempfile(void) {die ("BUG: diff is failing to clean up its tempfiles" ); } -static int remove_tempfile_installed; -static void remove_tempfile (void ) {int i;for (i =0 ; i <ARRAY_SIZE (diff_temp); i++) { -if (diff_temp[i].name == diff_temp[i].tmp_path) -unlink_or_warn (diff_temp[i].name); +if (is_tempfile_active (&diff_temp[i].tempfile)) +delete_tempfile (&diff_temp[i].tempfile); diff_temp[i].name =NULL ; } } -static void remove_tempfile_on_signal (int signo) -{ -remove_tempfile (); -sigchain_pop (signo); -raise (signo); -} -static void print_line_count (FILE *file,int count) {switch (count) {
@@ -2817,8 +2809,7 @@ static void prep_temp_blob(const char *path, struct diff_tempfile *temp,strbuf_addstr (&template,"XXXXXX_" );strbuf_addstr (&template, base); - fd =git_mkstemps (temp->tmp_path,PATH_MAX , template.buf, -strlen (base) +1 ); + fd =mks_tempfile_ts (&temp->tempfile, template.buf,strlen (base) +1 );if (fd <0 )die_errno ("unable to create temp-file" ); if (convert_to_working_tree(path,
@@ -2828,8 +2819,8 @@ static void prep_temp_blob(const char *path, struct diff_tempfile *temp, }if (write_in_full (fd, blob, size) != size)die_errno ("unable to write temp-file" ); -close (fd); - temp->name = temp->tmp_path; +close_tempfile (&temp->tempfile); + temp->name =get_tempfile_path (&temp->tempfile);strcpy (temp->hex,sha1_to_hex (sha1)); temp->hex[40 ] =0 ;sprintf (temp->mode,"%06o" , mode);
@@ -2854,12 +2845,6 @@ static struct diff_tempfile *prepare_temp_file(const char *name,return temp; } -if (!remove_tempfile_installed) { -atexit (remove_tempfile); -sigchain_push_common (remove_tempfile_on_signal); - remove_tempfile_installed =1 ; - } -if (!S_ISGITLINK (one->mode) && (!one->sha1_valid ||reuse_worktree_file (name, one->sha1,1 ))) {
Signature · 1 line
2.5.0
Re: [PATCH v2 12/16] diff: use tempfile module
Quoted 4 lines · Michael Haggerty
Signed-off-by Michael Haggerty <[email protected]>--- diff.c | 29 +++++++---------------------- 1 file changed, 7 insertions(+), 22 deletions(-)
Nice code reduction.
Quoted 25 lines
diff.c +2
-1@@ -2,6 +2,7 @@* Copyright (C) 2005 Junio C Hamano */ #include "cache.h" +#include "tempfile.h" #include "quote.h" #include "diff.h" #include "diffcore.h" @@ -312,7 +313,7 @@ static struct diff_tempfile {const char *name;/* filename external diff should read from */ char hex[41 ];char mode[10 ]; -char tmp_path[PATH_MAX ]; +struct tempfile tempfile; } diff_temp[2 ];typedef unsigned long (*sane_truncate_fn )(char *line,unsigned long len);@@ -564,25 +565,16 @@ static struct diff_tempfile *claim_diff_tempfile(void) {die ("BUG: diff is failing to clean up its tempfiles" ); }
diff.c static struct diff_tempfile *claim_diff_tempfile(void) { · line 573
Quoted 10 lines
diff.c +2
-4-static int remove_tempfile_installed; -static void remove_tempfile (void ) {int i;for (i =0 ; i <ARRAY_SIZE (diff_temp); i++) { -if (diff_temp[i].name == diff_temp[i].tmp_path) -unlink_or_warn (diff_temp[i].name); +if (is_tempfile_active (&diff_temp[i].tempfile)) +delete_tempfile (&diff_temp[i].tempfile);
I suspect that this indicates that there is something iffy in the conversion. The original invariant, that is consistently used between claim_diff_tempfile() and remove_tempfile(), is that .name field points at .tmp_path for a slot in diff_temp[] that holds a temporary that is in use. Otherwise, .name is NULL and it can be claimed for your own use.
Here the updated code uses a different and new invariant: .tempfile satisfies is_tempfile_active() for a slot in use. But the check in claim_diff_tempfile() still relies on the original invariant.
The updated code may happen to always have an active tempfile in tempfile and always set NULL when it clears .name, but that would mean (1) future changes may easily violate one of invariants (we used to have only one, now we have two that have to be sync) by mistake, and (2) we are keeping track of two closely linked things as two invariants.
As the value that used to be in the .name field can now be obtained by calling get_tempfile_path() on the .tempfile field, perhaps we should drop .name (and its associated invariant) at the same time?
Re: [PATCH v2 12/16] diff: use tempfile module
Quoted 43 lines · Junio C Hamano
Quoted 4 lines · Michael Haggerty
Signed-off-by Michael Haggerty <[email protected]>--- diff.c | 29 +++++++---------------------- 1 file changed, 7 insertions(+), 22 deletions(-)Nice code reduction.
Quoted 34 lines
diff.c +3
-5@@ -2,6 +2,7 @@* Copyright (C) 2005 Junio C Hamano */ #include "cache.h" +#include "tempfile.h" #include "quote.h" #include "diff.h" #include "diffcore.h" @@ -312,7 +313,7 @@ static struct diff_tempfile {const char *name;/* filename external diff should read from */ char hex[41 ];char mode[10 ]; -char tmp_path[PATH_MAX ]; +struct tempfile tempfile; } diff_temp[2 ];typedef unsigned long (*sane_truncate_fn )(char *line,unsigned long len);@@ -564,25 +565,16 @@ static struct diff_tempfile *claim_diff_tempfile(void) {die ("BUG: diff is failing to clean up its tempfiles" ); } -static int remove_tempfile_installed; -static void remove_tempfile (void ) {int i;for (i =0 ; i <ARRAY_SIZE (diff_temp); i++) { -if (diff_temp[i].name == diff_temp[i].tmp_path) -unlink_or_warn (diff_temp[i].name); +if (is_tempfile_active (&diff_temp[i].tempfile))
Quoted 8 lines
diff.c static struct diff_tempfile *claim_diff_tempfile(void) { · line 573
Quoted 1 line
diff.c +1
-0+delete_tempfile (&diff_temp[i].tempfile);I suspect that this indicates that there is something iffy in the conversion. The original invariant, that is consistently used between
claim_diff_tempfile()andremove_tempfile(), is that .name field points at .tmp_path for a slot in diff_temp[] that holds a temporary that is in use. Otherwise, .name is NULL and it can be claimed for your own use.
No, prepare_temp_file() sometimes sets diff_tempfile::name to "/dev/null", and sometimes to point at its argument name. In either of these cases .tmp_path can hold anything, and the file is *not* cleaned up even though the diff_temp entry is considered by claim_diff_tempfile() to be in use.
If I'm not mistaken, the old invariant was:
* Iff diff_tempfile::name is NULL, the entry is not in use.
* Iff diff_tempfile::name == diff_tempfile::tmp_path, then the entry is in use and refers to a temporary file that needs to be cleaned up.
* Otherwise, the entry is in use but the corresponding file should *not* be cleaned up.
The new invariant is:
* Iff diff_tempfile::name is NULL, the entry is not in use. In these cases, is_tempfile_active() is always false.
* Iff is_tempfile_active(diff_tempfile::tempfile), then it refers to a file that needs to get cleaned up. In these cases name points at the tempfile object's filename.
* If neither of the above is true, then the entry is in use but the corresponding file should not be cleaned up.
Quoted 3 lines
Here the updated code uses a different and new invariant: .tempfile satisfies
is_tempfile_active()for a slot in use. But the check inclaim_diff_tempfile()still relies on the original invariant.
That is not true. The is_tempfile_active() check is only used in remove_tempfile() when deciding whether to clean up the file. The check in claim_diff_tempfile() wants to know whether the entry is in use, so it uses the other check.
Quoted 10 lines
The updated code may happen to always have an active tempfile in tempfile and always set NULL when it clears .name, but that would mean (1) future changes may easily violate one of invariants (we used to have only one, now we have two that have to be sync) by mistake, and (2) we are keeping track of two closely linked things as two invariants.
As the value that used to be in the .name field can now be obtained by calling
get_tempfile_path()on the .tempfile field, perhaps we should drop .name (and its associated invariant) at the same time?
This is also incorrect. See my first paragraph above.
I will change this patch to document the invariants.
Michael
Signature · 2 lines
Michael Haggerty [email protected]
Re: [PATCH v2 12/16] diff: use tempfile module
Quoted 2 lines · Michael Haggerty
No,
prepare_temp_file()sometimes setsdiff_tempfile::nameto "/dev/null", and sometimes to point at its argumentname.
That explains everything. Thanks. It's been a while since I wrote this part of the system ;-).
PATCH v2 12/16 diff: use tempfile module
Also add some code comments explaining how the fields in "struct diff_tempfile" are used.
---
This is a replacement for tempfile patch v2 12/16 that includes some extra code comments. It is also available from my GitHub repo [1] on branch "tempfile".
[1] https://github.com/mhagger/git
diff.c | 46 +++++++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 23 deletions(-)
diff.c +23 -23
@@ -2,6 +2,7 @@* Copyright (C) 2005 Junio C Hamano */ #include "cache.h" +#include "tempfile.h" #include "quote.h" #include "diff.h" #include "diffcore.h"
@@ -308,11 +309,26 @@ static const char *external_diff(void)return external_diff_cmd; } +/* +* Keep track of files used for diffing. Sometimes such an entry +* refers to a temporary file, sometimes to an existing file, and +* sometimes to "/dev/null". +*/ static struct diff_tempfile { -const char *name;/* filename external diff should read from */ +/* +* filename external diff should read from, or NULL if this +* entry is currently not in use: +*/ +const char *name; +char hex[41 ];char mode[10 ]; -char tmp_path[PATH_MAX ]; + +/* +* If this diff_tempfile instance refers to a temporary file, +* this tempfile object is used to manage its lifetime. +*/ +struct tempfile tempfile; } diff_temp[2 ];typedef unsigned long (*sane_truncate_fn )(char *line,unsigned long len);
@@ -564,25 +580,16 @@ static struct diff_tempfile *claim_diff_tempfile(void) {die ("BUG: diff is failing to clean up its tempfiles" ); } -static int remove_tempfile_installed; -static void remove_tempfile (void ) {int i;for (i =0 ; i <ARRAY_SIZE (diff_temp); i++) { -if (diff_temp[i].name == diff_temp[i].tmp_path) -unlink_or_warn (diff_temp[i].name); +if (is_tempfile_active (&diff_temp[i].tempfile)) +delete_tempfile (&diff_temp[i].tempfile); diff_temp[i].name =NULL ; } } -static void remove_tempfile_on_signal (int signo) -{ -remove_tempfile (); -sigchain_pop (signo); -raise (signo); -} -static void print_line_count (FILE *file,int count) {switch (count) {
@@ -2817,8 +2824,7 @@ static void prep_temp_blob(const char *path, struct diff_tempfile *temp,strbuf_addstr (&template,"XXXXXX_" );strbuf_addstr (&template, base); - fd =git_mkstemps (temp->tmp_path,PATH_MAX , template.buf, -strlen (base) +1 ); + fd =mks_tempfile_ts (&temp->tempfile, template.buf,strlen (base) +1 );if (fd <0 )die_errno ("unable to create temp-file" ); if (convert_to_working_tree(path,
@@ -2828,8 +2834,8 @@ static void prep_temp_blob(const char *path, struct diff_tempfile *temp, }if (write_in_full (fd, blob, size) != size)die_errno ("unable to write temp-file" ); -close (fd); - temp->name = temp->tmp_path; +close_tempfile (&temp->tempfile); + temp->name =get_tempfile_path (&temp->tempfile);strcpy (temp->hex,sha1_to_hex (sha1)); temp->hex[40 ] =0 ;sprintf (temp->mode,"%06o" , mode);
@@ -2854,12 +2860,6 @@ static struct diff_tempfile *prepare_temp_file(const char *name,return temp; } -if (!remove_tempfile_installed) { -atexit (remove_tempfile); -sigchain_push_common (remove_tempfile_on_signal); - remove_tempfile_installed =1 ; - } -if (!S_ISGITLINK (one->mode) && (!one->sha1_valid ||reuse_worktree_file (name, one->sha1,1 ))) {
Signature · 1 line
2.5.0
PATCH v2 13/16 lock_repo_for_gc(): compute the path to "gc.pid" only once
In reply to Michael Haggerty--- builtin/gc.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-)
builtin/gc.c +6 -3
@@ -199,6 +199,7 @@ static const char *lock_repo_for_gc(int force, pid_t* ret_pid)uintmax_t pid;FILE *fp;int fd; +char *pidfile_path;if (pidfile)/* already locked */
@@ -207,12 +208,13 @@ static const char *lock_repo_for_gc(int force, pid_t* ret_pid)if (gethostname (my_host,sizeof (my_host)))strcpy (my_host,"unknown" ); - fd =hold_lock_file_for_update (&lock,git_path ("gc.pid" ), + pidfile_path =git_pathdup ("gc.pid" ); + fd =hold_lock_file_for_update (&lock, pidfile_path,LOCK_DIE_ON_ERROR );if (!force) {static char locking_host[128 ];int should_exit; - fp =fopen (git_path ("gc.pid" ),"r" ); + fp =fopen (pidfile_path,"r" );memset (locking_host,0 ,sizeof (locking_host)); should_exit = fp != NULL &&
@@ -236,6 +238,7 @@ static const char *lock_repo_for_gc(int force, pid_t* ret_pid)if (fd >=0 )rollback_lock_file (&lock); *ret_pid = pid; +free (pidfile_path);return locking_host; } }
@@ -246,7 +249,7 @@ static const char *lock_repo_for_gc(int force, pid_t* ret_pid)strbuf_release (&sb);commit_lock_file (&lock); - pidfile =git_pathdup ("gc.pid" ); + pidfile = pidfile_path;sigchain_push_common (remove_pidfile_on_signal);atexit (remove_pidfile);
Signature · 1 line
2.5.0
Re: [PATCH v2 13/16] lock_repo_for_gc(): compute the path to "gc.pid" only once
Quoted 18 lines · Michael Haggerty
Signed-off-by Michael Haggerty <[email protected]>--- builtin/gc.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-)builtin/gc.c +1
-0@@ -199,6 +199,7 @@ static const char *lock_repo_for_gc(int force, pid_t* ret_pid)uintmax_t pid;FILE *fp;int fd; +char *pidfile_path;if (pidfile)/* already locked */
builtin/gc.c static const char *lock_repo_for_gc(int force, pid_t* ret_pid) · line 213
Quoted 7 lines
builtin/gc.c +2
-1@@ -207,12 +208,13 @@ static const char *lock_repo_for_gc(int force, pid_t* ret_pid)if (gethostname (my_host,sizeof (my_host)))strcpy (my_host,"unknown" ); - fd =hold_lock_file_for_update (&lock,git_path ("gc.pid" ), + pidfile_path =git_pathdup ("gc.pid" ); + fd =hold_lock_file_for_update (&lock, pidfile_path,LOCK_DIE_ON_ERROR );
Looks correct; somehow this reminded me of the other topic from Peff to reduce use of git_path() ;-)
builtin/gc.c static const char *lock_repo_for_gc(int force, pid_t* ret_pid) · line 254
Quoted 4 lines
builtin/gc.c +1
-1- pidfile =git_pathdup ("gc.pid" ); + pidfile = pidfile_path;sigchain_push_common (remove_pidfile_on_signal);atexit (remove_pidfile);
I wonder if you can reduce the atexit() here by registering this as a tempfile to be cleared?
Re: [PATCH v2 13/16] lock_repo_for_gc(): compute the path to "gc.pid" only once
Quoted 11 lines · Junio C Hamano
Looks correct; somehow this reminded me of the other topic from Peff to reduce use of
git_path();-)builtin/gc.c static const char *lock_repo_for_gc(int force, pid_t* ret_pid) · line 254
Quoted 4 lines
builtin/gc.c +1
-1- pidfile =git_pathdup ("gc.pid" ); + pidfile = pidfile_path;sigchain_push_common (remove_pidfile_on_signal);atexit (remove_pidfile);I wonder if you can reduce the
atexit()here by registering this as a tempfile to be cleared?
Heh, I should have been slightly more patient. That is what 14/16 is about ;-)
PATCH v2 14/16 gc: use tempfile module to handle gc.pid file
In reply to Michael Haggerty--- builtin/gc.c | 25 +++++-------------------- 1 file changed, 5 insertions(+), 20 deletions(-)
builtin/gc.c +5 -20
@@ -11,6 +11,7 @@*/ #include "builtin.h" +#include "tempfile.h" #include "lockfile.h" #include "parse-options.h" #include "run-command.h"
@@ -42,20 +43,7 @@ static struct argv_array prune = ARGV_ARRAY_INIT;static struct argv_array prune_worktrees =ARGV_ARRAY_INIT ;static struct argv_array rerere =ARGV_ARRAY_INIT ; -static char *pidfile; - -static void remove_pidfile (void ) -{ -if (pidfile) -unlink (pidfile); -} - -static void remove_pidfile_on_signal (int signo) -{ -remove_pidfile (); -sigchain_pop (signo); -raise (signo); -} +static struct tempfile pidfile;static void git_config_date_string (const char *key,const char **output) {
@@ -201,7 +189,7 @@ static const char *lock_repo_for_gc(int force, pid_t* ret_pid)int fd;char *pidfile_path; -if (pidfile) +if (is_tempfile_active (&pidfile))/* already locked */ return NULL ;
@@ -248,11 +236,8 @@ static const char *lock_repo_for_gc(int force, pid_t* ret_pid)write_in_full (fd, sb.buf, sb.len);strbuf_release (&sb);commit_lock_file (&lock); - - pidfile = pidfile_path; -sigchain_push_common (remove_pidfile_on_signal); -atexit (remove_pidfile); - +register_tempfile (&pidfile, pidfile_path); +free (pidfile_path);return NULL ; }
Signature · 1 line
2.5.0
PATCH v2 15/16 credential-cache--daemon: delete socket from main()
In reply to Michael Haggertymain() is responsible for cleaning up the socket in the case of errors, so it is reasonable to also make it responsible for cleaning it up when there are no errors. This change also makes the next step easier.
--- credential-cache--daemon.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
credential-cache--daemon.c +2 -1
@@ -221,7 +221,6 @@ static void serve_cache(const char *socket_path, int debug) ;/* nothing */ close (fd); -unlink (socket_path); } static const char permissions_advice[] =
@@ -280,5 +279,7 @@ int main(int argc, const char **argv)serve_cache (socket_path, debug); +unlink (socket_path); +return 0 ; }
Signature · 1 line
2.5.0
PATCH v2 16/16 credential-cache--daemon: use tempfile module
In reply to Michael HaggertyUse the tempfile module to ensure that the socket file gets deleted on program exit.
--- credential-cache--daemon.c | 26 ++++++-------------------- 1 file changed, 6 insertions(+), 20 deletions(-)
credential-cache--daemon.c +6 -20
@@ -1,23 +1,11 @@#include "cache.h" +#include "tempfile.h" #include "credential.h" #include "unix-socket.h" #include "sigchain.h" #include "parse-options.h" -static const char *socket_path; - -static void cleanup_socket (void ) -{ -if (socket_path) -unlink (socket_path); -} - -static void cleanup_socket_on_signal (int sig) -{ -cleanup_socket (); -sigchain_pop (sig); -raise (sig); -} +static struct tempfile socket_file;struct credential_cache_entry {struct credential item;
@@ -256,6 +244,7 @@ static void check_socket_directory(const char *path)int main (int argc,const char **argv) { +const char *socket_path;static const char *usage[] = {"git-credential-cache--daemon [opts] <socket_path>" ,NULL
@@ -272,14 +261,11 @@ int main(int argc, const char **argv)if (!socket_path)usage_with_options (usage, options); -check_socket_directory (socket_path); - -atexit (cleanup_socket); -sigchain_push_common (cleanup_socket_on_signal); +check_socket_directory (socket_path); +register_tempfile (&socket_file, socket_path);serve_cache (socket_path, debug); - -unlink (socket_path); +delete_tempfile (&socket_file);return 0 ; }
Signature · 1 line
2.5.0
Thanks for a pleasant read. All looked reasonable.
Quoted 1 line · Junio C Hamano
Thanks for a pleasant read. All looked reasonable.
Thanks for your review!
Michael
Signature · 2 lines
Michael Haggerty [email protected]