Skip to content

Commit fd5db55

Browse files
committed
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 <[email protected]>
1 parent f8c8abc commit fd5db55

File tree

1 file changed

+26
-17
lines changed

1 file changed

+26
-17
lines changed

entry.c

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,29 @@ static void *read_blob_entry(struct cache_entry *ce, unsigned long *size)
9191
return NULL;
9292
}
9393

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+
94117
static int write_entry(struct cache_entry *ce, char *path, const struct checkout *state, int to_tempfile)
95118
{
96119
unsigned int ce_mode_s_ifmt = ce->ce_mode & S_IFMT;
@@ -128,30 +151,16 @@ static int write_entry(struct cache_entry *ce, char *path, const struct checkout
128151
size = newsize;
129152
}
130153

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);
142155
if (fd < 0) {
143156
free(new);
144157
return error("unable to create file %s (%s)",
145158
path, strerror(errno));
146159
}
147160

148161
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);
155164
close(fd);
156165
free(new);
157166
if (wrote != size)

0 commit comments

Comments
 (0)