From fd5db55d8b6668a1ff9583a6636a4d54ad9519f2 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Thu, 12 May 2011 21:36:42 -0700 Subject: [PATCH] write_entry(): separate two helper functions out In the write-out codepath, a block of code determines what file in the working tree to write to, and opens an output file descriptor to it. After writing the contents out to the file, another block of code runs fstat() on the file descriptor when appropriate. Separate these blocks out to open_output_fd() and fstat_output() helper functions. Signed-off-by: Junio C Hamano --- entry.c | 43 ++++++++++++++++++++++++++----------------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/entry.c b/entry.c index b017167f20..cc6502a548 100644 --- a/entry.c +++ b/entry.c @@ -91,6 +91,29 @@ static void *read_blob_entry(struct cache_entry *ce, unsigned long *size) return NULL; } +static int open_output_fd(char *path, struct cache_entry *ce, int to_tempfile) +{ + int symlink = (ce->ce_mode & S_IFMT) != S_IFREG; + if (to_tempfile) { + strcpy(path, symlink + ? ".merge_link_XXXXXX" : ".merge_file_XXXXXX"); + return mkstemp(path); + } else { + return create_file(path, !symlink ? ce->ce_mode : 0666); + } +} + +static int fstat_output(int fd, const struct checkout *state, struct stat *st) +{ + /* use fstat() only when path == ce->name */ + if (fstat_is_reliable() && + state->refresh_cache && !state->base_dir_len) { + fstat(fd, st); + return 1; + } + return 0; +} + static int write_entry(struct cache_entry *ce, char *path, const struct checkout *state, int to_tempfile) { unsigned int ce_mode_s_ifmt = ce->ce_mode & S_IFMT; @@ -128,17 +151,7 @@ static int write_entry(struct cache_entry *ce, char *path, const struct checkout size = newsize; } - if (to_tempfile) { - if (ce_mode_s_ifmt == S_IFREG) - strcpy(path, ".merge_file_XXXXXX"); - else - strcpy(path, ".merge_link_XXXXXX"); - fd = mkstemp(path); - } else if (ce_mode_s_ifmt == S_IFREG) { - fd = create_file(path, ce->ce_mode); - } else { - fd = create_file(path, 0666); - } + fd = open_output_fd(path, ce, to_tempfile); if (fd < 0) { free(new); return error("unable to create file %s (%s)", @@ -146,12 +159,8 @@ static int write_entry(struct cache_entry *ce, char *path, const struct checkout } wrote = write_in_full(fd, new, size); - /* use fstat() only when path == ce->name */ - if (fstat_is_reliable() && - state->refresh_cache && !to_tempfile && !state->base_dir_len) { - fstat(fd, &st); - fstat_done = 1; - } + if (!to_tempfile) + fstat_done = fstat_output(fd, state, &st); close(fd); free(new); if (wrote != size)