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 <gitster@pobox.com>

Junio C Hamano committed May 12, 2011 at 21:36 UTC fd5db55d8b6668a1ff9583a6636a4d54ad9519f2
1 file changed +26 -17
entry.c
+26 -17
@@ -91,6 +91,29 @@ static void *read_blob_entry(struct cache_entry *ce, unsigned long *size)
91 return NULL;
92 }
93
94 +static int open_output_fd(char *path, struct cache_entry *ce, int to_tempfile)
95 +{
96 + int symlink = (ce->ce_mode & S_IFMT) != S_IFREG;
97 + if (to_tempfile) {
98 + strcpy(path, symlink
99 + ? ".merge_link_XXXXXX" : ".merge_file_XXXXXX");
100 + return mkstemp(path);
101 + } else {
102 + return create_file(path, !symlink ? ce->ce_mode : 0666);
103 + }
104 +}
105 +
106 +static int fstat_output(int fd, const struct checkout *state, struct stat *st)
107 +{
108 + /* use fstat() only when path == ce->name */
109 + if (fstat_is_reliable() &&
110 + state->refresh_cache && !state->base_dir_len) {
111 + fstat(fd, st);
112 + return 1;
113 + }
114 + return 0;
115 +}
116 +
117 static int write_entry(struct cache_entry *ce, char *path, const struct checkout *state, int to_tempfile)
118 {
119 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
151 size = newsize;
152 }
153
131 - if (to_tempfile) {
132 - if (ce_mode_s_ifmt == S_IFREG)
133 - strcpy(path, ".merge_file_XXXXXX");
134 - else
135 - strcpy(path, ".merge_link_XXXXXX");
136 - fd = mkstemp(path);
137 - } else if (ce_mode_s_ifmt == S_IFREG) {
138 - fd = create_file(path, ce->ce_mode);
139 - } else {
140 - fd = create_file(path, 0666);
141 - }
154 + fd = open_output_fd(path, ce, to_tempfile);
155 if (fd < 0) {
156 free(new);
157 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
159 }
160
161 wrote = write_in_full(fd, new, size);
149 - /* use fstat() only when path == ce->name */
150 - if (fstat_is_reliable() &&
151 - state->refresh_cache && !to_tempfile && !state->base_dir_len) {
152 - fstat(fd, &st);
153 - fstat_done = 1;
154 - }
162 + if (!to_tempfile)
163 + fstat_done = fstat_output(fd, state, &st);
164 close(fd);
165 free(new);
166 if (wrote != size)