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>
Junio C Hamano committed
Aug 24, 2015 at 13:03 UTC
12d6ce1dba504dfc5279b8d24da3edb4865c2820
10 files changed
+40
-19
builtin/am.c
+2
-2
@@ -205,13 +205,13 @@ static int write_state_text(const struct am_state *state,
205
fmt = "%s\n";
206
else
207
fmt = "%s";
208
- return write_file(am_path(state, name), 1, fmt, string);
208
+ return write_file(am_path(state, name), fmt, string);
209
}
210
211
static int write_state_count(const struct am_state *state,
212
const char *name, int value)
213
{
214
- return write_file(am_path(state, name), 1, "%d\n", value);
214
+ return write_file(am_path(state, name), "%d\n", value);
215
}
216
217
static int write_state_bool(const struct am_state *state,
builtin/branch.c
+1
-1
@@ -776,7 +776,7 @@ static int edit_branch_description(const char *branch_name)
776
" %s\n"
777
"Lines starting with '%c' will be stripped.\n",
778
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)) {
780
strbuf_release(&buf);
781
return error(_("could not write branch description template: %s"),
782
strerror(errno));
builtin/init-db.c
+1
-1
@@ -378,7 +378,7 @@ static void separate_git_dir(const char *git_dir)
378
die_errno(_("unable to move %s to %s"), src, git_dir);
379
}
380
381
- write_file(git_link, 1, "gitdir: %s\n", git_dir);
381
+ write_file(git_link, "gitdir: %s\n", git_dir);
382
}
383
384
int init_db(const char *template_dir, unsigned int flags)
builtin/worktree.c
+5
-5
@@ -213,7 +213,7 @@ static int add_worktree(const char *path, const char **child_argv)
213
* after the preparation is over.
214
*/
215
strbuf_addf(&sb, "%s/locked", sb_repo.buf);
216
- write_file(sb.buf, 1, "initializing\n");
216
+ write_file(sb.buf, "initializing\n");
217
218
strbuf_addf(&sb_git, "%s/.git", path);
219
if (safe_create_leading_directories_const(sb_git.buf))
@@ -223,8 +223,8 @@ static int add_worktree(const char *path, const char **child_argv)
223
224
strbuf_reset(&sb);
225
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",
228
real_path(get_git_common_dir()), name);
229
/*
230
* 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)
241
die(_("unable to resolve HEAD"));
242
strbuf_reset(&sb);
243
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));
245
strbuf_reset(&sb);
246
strbuf_addf(&sb, "%s/commondir", sb_repo.buf);
247
- write_file(sb.buf, 1, "../..\n");
247
+ write_file(sb.buf, "../..\n");
248
249
fprintf_ln(stderr, _("Enter %s (identifier %s)"), path, name);
250
cache.h
+3
-2
@@ -1539,8 +1539,9 @@ static inline ssize_t write_str_in_full(int fd, const char *str)
1539
{
1540
return write_in_full(fd, str, strlen(str));
1541
}
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, ...);
1545
1546
/* pager.c */
1547
extern void setup_pager(void);
daemon.c
+1
-1
@@ -1376,7 +1376,7 @@ int main(int argc, char **argv)
1376
sanitize_stdfds();
1377
1378
if (pid_file)
1379
- write_file(pid_file, 1, "%"PRIuMAX"\n", (uintmax_t) getpid());
1379
+ write_file(pid_file, "%"PRIuMAX"\n", (uintmax_t) getpid());
1380
1381
/* prepare argv for serving-processes */
1382
cld_argv = xmalloc(sizeof (char *) * (argc + 2));
setup.c
+1
-1
@@ -404,7 +404,7 @@ static void update_linked_gitdir(const char *gitfile, const char *gitdir)
404
405
strbuf_addf(&path, "%s/gitfile", gitdir);
406
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);
408
strbuf_release(&path);
409
}
410
submodule.c
+1
-1
@@ -1103,7 +1103,7 @@ void connect_work_tree_and_git_dir(const char *work_tree, const char *git_dir)
1103
1104
/* Update gitfile */
1105
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",
1107
relative_path(git_dir, real_work_tree, &rel_path));
1108
1109
/* Update core.worktree setting */
transport.c
+1
-1
@@ -291,7 +291,7 @@ static int write_one_ref(const char *name, const struct object_id *oid,
291
292
strbuf_addstr(buf, name);
293
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)))
295
return error("problems writing temporary file %s: %s",
296
buf->buf, strerror(errno));
297
strbuf_setlen(buf, len);
wrapper.c
+24
-4
@@ -621,19 +621,17 @@ char *xgetcwd(void)
621
return strbuf_detach(&sb, NULL);
622
}
623
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)
626
{
627
struct strbuf sb = STRBUF_INIT;
627
- va_list params;
628
int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0666);
629
if (fd < 0) {
630
if (fatal)
631
die_errno(_("could not open %s for writing"), path);
632
return -1;
633
}
634
- va_start(params, fmt);
634
strbuf_vaddf(&sb, fmt, params);
636
- va_end(params);
635
if (write_in_full(fd, sb.buf, sb.len) != sb.len) {
636
int err = errno;
637
close(fd);
@@ -652,6 +650,28 @@ int write_file(const char *path, int fatal, const char *fmt, ...)
650
return 0;
651
}
652
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
+
675
void sleep_millisec(int millisec)
676
{
677
poll(NULL, 0, millisec);