Skip to content

Commit 12d6ce1

Browse files
committed
write_file(): drop "fatal" parameter
All callers except three passed 1 for the "fatal" parameter to ask this function to die upon error, but to a casual reader of the code, it was not all obvious what that 1 meant. Instead, split the function into two based on a common write_file_v() that takes the flag, introduce write_file_gently() as a new way to attempt creating a file without dying on error, and make three callers to call it. Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 57c867e commit 12d6ce1

File tree

10 files changed

+40
-19
lines changed

10 files changed

+40
-19
lines changed

builtin/am.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,13 +205,13 @@ static int write_state_text(const struct am_state *state,
205205
fmt = "%s\n";
206206
else
207207
fmt = "%s";
208-
return write_file(am_path(state, name), 1, fmt, string);
208+
return write_file(am_path(state, name), fmt, string);
209209
}
210210

211211
static int write_state_count(const struct am_state *state,
212212
const char *name, int value)
213213
{
214-
return write_file(am_path(state, name), 1, "%d\n", value);
214+
return write_file(am_path(state, name), "%d\n", value);
215215
}
216216

217217
static int write_state_bool(const struct am_state *state,

builtin/branch.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -776,7 +776,7 @@ static int edit_branch_description(const char *branch_name)
776776
" %s\n"
777777
"Lines starting with '%c' will be stripped.\n",
778778
branch_name, comment_line_char);
779-
if (write_file(git_path(edit_description), 0, "%s", buf.buf)) {
779+
if (write_file_gently(git_path(edit_description), "%s", buf.buf)) {
780780
strbuf_release(&buf);
781781
return error(_("could not write branch description template: %s"),
782782
strerror(errno));

builtin/init-db.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ static void separate_git_dir(const char *git_dir)
378378
die_errno(_("unable to move %s to %s"), src, git_dir);
379379
}
380380

381-
write_file(git_link, 1, "gitdir: %s\n", git_dir);
381+
write_file(git_link, "gitdir: %s\n", git_dir);
382382
}
383383

384384
int init_db(const char *template_dir, unsigned int flags)

builtin/worktree.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ static int add_worktree(const char *path, const char **child_argv)
213213
* after the preparation is over.
214214
*/
215215
strbuf_addf(&sb, "%s/locked", sb_repo.buf);
216-
write_file(sb.buf, 1, "initializing\n");
216+
write_file(sb.buf, "initializing\n");
217217

218218
strbuf_addf(&sb_git, "%s/.git", path);
219219
if (safe_create_leading_directories_const(sb_git.buf))
@@ -223,8 +223,8 @@ static int add_worktree(const char *path, const char **child_argv)
223223

224224
strbuf_reset(&sb);
225225
strbuf_addf(&sb, "%s/gitdir", sb_repo.buf);
226-
write_file(sb.buf, 1, "%s\n", real_path(sb_git.buf));
227-
write_file(sb_git.buf, 1, "gitdir: %s/worktrees/%s\n",
226+
write_file(sb.buf, "%s\n", real_path(sb_git.buf));
227+
write_file(sb_git.buf, "gitdir: %s/worktrees/%s\n",
228228
real_path(get_git_common_dir()), name);
229229
/*
230230
* This is to keep resolve_ref() happy. We need a valid HEAD
@@ -241,10 +241,10 @@ static int add_worktree(const char *path, const char **child_argv)
241241
die(_("unable to resolve HEAD"));
242242
strbuf_reset(&sb);
243243
strbuf_addf(&sb, "%s/HEAD", sb_repo.buf);
244-
write_file(sb.buf, 1, "%s\n", sha1_to_hex(rev));
244+
write_file(sb.buf, "%s\n", sha1_to_hex(rev));
245245
strbuf_reset(&sb);
246246
strbuf_addf(&sb, "%s/commondir", sb_repo.buf);
247-
write_file(sb.buf, 1, "../..\n");
247+
write_file(sb.buf, "../..\n");
248248

249249
fprintf_ln(stderr, _("Enter %s (identifier %s)"), path, name);
250250

cache.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1539,8 +1539,9 @@ static inline ssize_t write_str_in_full(int fd, const char *str)
15391539
{
15401540
return write_in_full(fd, str, strlen(str));
15411541
}
1542-
__attribute__((format (printf, 3, 4)))
1543-
extern int write_file(const char *path, int fatal, const char *fmt, ...);
1542+
1543+
extern int write_file(const char *path, const char *fmt, ...);
1544+
extern int write_file_gently(const char *path, const char *fmt, ...);
15441545

15451546
/* pager.c */
15461547
extern void setup_pager(void);

daemon.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1376,7 +1376,7 @@ int main(int argc, char **argv)
13761376
sanitize_stdfds();
13771377

13781378
if (pid_file)
1379-
write_file(pid_file, 1, "%"PRIuMAX"\n", (uintmax_t) getpid());
1379+
write_file(pid_file, "%"PRIuMAX"\n", (uintmax_t) getpid());
13801380

13811381
/* prepare argv for serving-processes */
13821382
cld_argv = xmalloc(sizeof (char *) * (argc + 2));

setup.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ static void update_linked_gitdir(const char *gitfile, const char *gitdir)
404404

405405
strbuf_addf(&path, "%s/gitfile", gitdir);
406406
if (stat(path.buf, &st) || st.st_mtime + 24 * 3600 < time(NULL))
407-
write_file(path.buf, 0, "%s\n", gitfile);
407+
write_file_gently(path.buf, "%s\n", gitfile);
408408
strbuf_release(&path);
409409
}
410410

submodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1103,7 +1103,7 @@ void connect_work_tree_and_git_dir(const char *work_tree, const char *git_dir)
11031103

11041104
/* Update gitfile */
11051105
strbuf_addf(&file_name, "%s/.git", work_tree);
1106-
write_file(file_name.buf, 1, "gitdir: %s\n",
1106+
write_file(file_name.buf, "gitdir: %s\n",
11071107
relative_path(git_dir, real_work_tree, &rel_path));
11081108

11091109
/* Update core.worktree setting */

transport.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ static int write_one_ref(const char *name, const struct object_id *oid,
291291

292292
strbuf_addstr(buf, name);
293293
if (safe_create_leading_directories(buf->buf) ||
294-
write_file(buf->buf, 0, "%s\n", oid_to_hex(oid)))
294+
write_file_gently(buf->buf, "%s\n", oid_to_hex(oid)))
295295
return error("problems writing temporary file %s: %s",
296296
buf->buf, strerror(errno));
297297
strbuf_setlen(buf, len);

wrapper.c

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -621,19 +621,17 @@ char *xgetcwd(void)
621621
return strbuf_detach(&sb, NULL);
622622
}
623623

624-
int write_file(const char *path, int fatal, const char *fmt, ...)
624+
static int write_file_v(const char *path, int fatal,
625+
const char *fmt, va_list params)
625626
{
626627
struct strbuf sb = STRBUF_INIT;
627-
va_list params;
628628
int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0666);
629629
if (fd < 0) {
630630
if (fatal)
631631
die_errno(_("could not open %s for writing"), path);
632632
return -1;
633633
}
634-
va_start(params, fmt);
635634
strbuf_vaddf(&sb, fmt, params);
636-
va_end(params);
637635
if (write_in_full(fd, sb.buf, sb.len) != sb.len) {
638636
int err = errno;
639637
close(fd);
@@ -652,6 +650,28 @@ int write_file(const char *path, int fatal, const char *fmt, ...)
652650
return 0;
653651
}
654652

653+
int write_file(const char *path, const char *fmt, ...)
654+
{
655+
int status;
656+
va_list params;
657+
658+
va_start(params, fmt);
659+
status = write_file_v(path, 1, fmt, params);
660+
va_end(params);
661+
return status;
662+
}
663+
664+
int write_file_gently(const char *path, const char *fmt, ...)
665+
{
666+
int status;
667+
va_list params;
668+
669+
va_start(params, fmt);
670+
status = write_file_v(path, 0, fmt, params);
671+
va_end(params);
672+
return status;
673+
}
674+
655675
void sleep_millisec(int millisec)
656676
{
657677
poll(NULL, 0, millisec);

0 commit comments

Comments
 (0)