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

Re: [PATCH 08/14] write_shared_index(): use tempfile module

From
Junio C Hamano <gitster@pobox.com>
Date
Jun 10, 2015, 17:56 UTC
Message-ID
<xmqqegljla5o.fsf@gitster.dls.corp.google.com>
In-Reply-To
<78ffe87f5dd9037ae16a77bf9c40dcfdd9e3b40f.1433751986.git.mhagger@alum.mit.edu>
Michael Haggerty <mhagger@alum.mit.edu> writes:
> Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
> ---
>  read-cache.c | 37 +++++--------------------------------
>  1 file changed, 5 insertions(+), 32 deletions(-)
Nicely done.
Show 65 quoted lines
>
> diff --git a/read-cache.c b/read-cache.c
> index 3e49c49..4f7b70f 100644
> --- a/read-cache.c
> +++ b/read-cache.c
> @@ -2137,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;
Previous: Michael HaggertyNext: Michael Haggerty
Message 19 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.