use new wrapper write_file() for simple file writing
This fixes common problems in these code about error handling, forgetting to close the file handle after fprintf() fails, or not printing out the error string.. Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Nguyễn Thái Ngọc Duy committed
Nov 30, 2014 at 15:24 UTC
91aacda85a91192a9f66a0f175d739f77971412f
5 files changed
+8
-31
builtin/branch.c
+1
-3
@@ -764,7 +764,6 @@ static const char edit_description[] = "BRANCH_DESCRIPTION";
764
765
static int edit_branch_description(const char *branch_name)
766
{
767
- FILE *fp;
767
int status;
768
struct strbuf buf = STRBUF_INIT;
769
struct strbuf name = STRBUF_INIT;
@@ -777,8 +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);
780
- fp = fopen(git_path(edit_description), "w");
781
- if ((fwrite(buf.buf, 1, buf.len, fp) < buf.len) || fclose(fp)) {
779
+ if (write_file(git_path(edit_description), 0, "%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
-6
@@ -342,7 +342,6 @@ int set_git_dir_init(const char *git_dir, const char *real_git_dir,
342
static void separate_git_dir(const char *git_dir)
343
{
344
struct stat st;
345
- FILE *fp;
345
346
if (!stat(git_link, &st)) {
347
const char *src;
@@ -358,11 +357,7 @@ static void separate_git_dir(const char *git_dir)
357
die_errno(_("unable to move %s to %s"), src, git_dir);
358
}
359
361
- fp = fopen(git_link, "w");
362
- if (!fp)
363
- die(_("Could not create git link %s"), git_link);
364
- fprintf(fp, "gitdir: %s\n", git_dir);
365
- fclose(fp);
360
+ write_file(git_link, 1, "gitdir: %s\n", git_dir);
361
}
362
363
int init_db(const char *template_dir, unsigned int flags)
daemon.c
+1
-10
@@ -1070,15 +1070,6 @@ static struct credentials *prepare_credentials(const char *user_name,
1070
}
1071
#endif
1072
1073
-static void store_pid(const char *path)
1074
-{
1075
- FILE *f = fopen(path, "w");
1076
- if (!f)
1077
- die_errno("cannot open pid file '%s'", path);
1078
- if (fprintf(f, "%"PRIuMAX"\n", (uintmax_t) getpid()) < 0 || fclose(f) != 0)
1079
- die_errno("failed to write pid file '%s'", path);
1080
-}
1081
-
1073
static int serve(struct string_list *listen_addr, int listen_port,
1074
struct credentials *cred)
1075
{
@@ -1289,7 +1280,7 @@ int main(int argc, char **argv)
1280
sanitize_stdfds();
1281
1282
if (pid_file)
1292
- store_pid(pid_file);
1283
+ write_file(pid_file, 1, "%"PRIuMAX"\n", (uintmax_t) getpid());
1284
1285
/* prepare argv for serving-processes */
1286
cld_argv = xmalloc(sizeof (char *) * (argc + 2));
submodule.c
+2
-7
@@ -1102,16 +1102,11 @@ void connect_work_tree_and_git_dir(const char *work_tree, const char *git_dir)
1102
struct strbuf file_name = STRBUF_INIT;
1103
struct strbuf rel_path = STRBUF_INIT;
1104
const char *real_work_tree = xstrdup(real_path(work_tree));
1105
- FILE *fp;
1105
1106
/* Update gitfile */
1107
strbuf_addf(&file_name, "%s/.git", work_tree);
1109
- fp = fopen(file_name.buf, "w");
1110
- if (!fp)
1111
- die(_("Could not create git link %s"), file_name.buf);
1112
- fprintf(fp, "gitdir: %s\n", relative_path(git_dir, real_work_tree,
1113
- &rel_path));
1114
- fclose(fp);
1108
+ write_file(file_name.buf, 1, "gitdir: %s\n",
1109
+ relative_path(git_dir, real_work_tree, &rel_path));
1110
1111
/* Update core.worktree setting */
1112
strbuf_reset(&file_name);
transport.c
+3
-5
@@ -283,7 +283,6 @@ static int write_one_ref(const char *name, const unsigned char *sha1,
283
{
284
struct strbuf *buf = data;
285
int len = buf->len;
286
- FILE *f;
286
287
/* when called via for_each_ref(), flags is non-zero */
288
if (flags && !starts_with(name, "refs/heads/") &&
@@ -292,10 +291,9 @@ static int write_one_ref(const char *name, const unsigned char *sha1,
291
292
strbuf_addstr(buf, name);
293
if (safe_create_leading_directories(buf->buf) ||
295
- !(f = fopen(buf->buf, "w")) ||
296
- fprintf(f, "%s\n", sha1_to_hex(sha1)) < 0 ||
297
- fclose(f))
298
- return error("problems writing temporary file %s", buf->buf);
294
+ write_file(buf->buf, 0, "%s\n", sha1_to_hex(sha1)))
295
+ return error("problems writing temporary file %s: %s",
296
+ buf->buf, strerror(errno));
297
strbuf_setlen(buf, len);
298
return 0;
299
}