git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: git@vger.kernel.org
Subject: [PATCH v3 05/12] write_entry(): separate two helper functions out
Date: Fri, 20 May 2011 23:56:28 -0700	[thread overview]
Message-ID: <1305960995-25738-6-git-send-email-gitster@pobox.com> (raw)
In-Reply-To: <1305960995-25738-1-git-send-email-gitster@pobox.com>

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 <gitster@pobox.com>
---
 entry.c |   43 ++++++++++++++++++++++++++-----------------
 1 files changed, 26 insertions(+), 17 deletions(-)

diff --git a/entry.c b/entry.c
index b017167..cc6502a 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)
-- 
1.7.5.2.369.g8fc017

  parent reply	other threads:[~2011-05-21  6:57 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-05-21  6:56 [PATCH v3 00/12] writing out a huge blob to working tree Junio C Hamano
2011-05-21  6:56 ` [PATCH v3 01/12] packed_object_info_detail(): do not return a string Junio C Hamano
2011-05-21  6:56 ` [PATCH v3 02/12] sha1_object_info_extended(): expose a bit more info Junio C Hamano
2011-05-21  6:56 ` [PATCH v3 03/12] sha1_object_info_extended(): hint about objects in delta-base cache Junio C Hamano
2011-05-21  6:56 ` [PATCH v3 04/12] unpack_object_header(): make it public Junio C Hamano
2011-05-21  6:56 ` Junio C Hamano [this message]
2011-05-21  6:56 ` [PATCH v3 06/12] streaming: a new API to read from the object store Junio C Hamano
2011-05-21  6:56 ` [PATCH v3 07/12] streaming_write_entry(): use streaming API in write_entry() Junio C Hamano
2011-05-21  6:56 ` [PATCH v3 08/12] convert: CRLF_INPUT is a no-op in the output codepath Junio C Hamano
2011-05-21  6:56 ` [PATCH v3 09/12] streaming_write_entry(): support files with holes Junio C Hamano
2011-05-21  6:56 ` [PATCH v3 10/12] streaming: read non-delta incrementally from a pack Junio C Hamano
2011-05-21  6:56 ` [PATCH v3 11/12] sha1_file.c: expose helpers to read loose objects Junio C Hamano
2011-05-21  6:56 ` [PATCH v3 12/12] streaming: read loose objects incrementally Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1305960995-25738-6-git-send-email-gitster@pobox.com \
    --to=gitster@pobox.com \
    --cc=git@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).