Skip to content

Commit 91aacda

Browse files
pcloudsgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 316e53e commit 91aacda

File tree

5 files changed

+8
-31
lines changed

5 files changed

+8
-31
lines changed

builtin/branch.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -764,7 +764,6 @@ static const char edit_description[] = "BRANCH_DESCRIPTION";
764764

765765
static int edit_branch_description(const char *branch_name)
766766
{
767-
FILE *fp;
768767
int status;
769768
struct strbuf buf = STRBUF_INIT;
770769
struct strbuf name = STRBUF_INIT;
@@ -777,8 +776,7 @@ static int edit_branch_description(const char *branch_name)
777776
" %s\n"
778777
"Lines starting with '%c' will be stripped.\n",
779778
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)) {
782780
strbuf_release(&buf);
783781
return error(_("could not write branch description template: %s"),
784782
strerror(errno));

builtin/init-db.c

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,6 @@ int set_git_dir_init(const char *git_dir, const char *real_git_dir,
342342
static void separate_git_dir(const char *git_dir)
343343
{
344344
struct stat st;
345-
FILE *fp;
346345

347346
if (!stat(git_link, &st)) {
348347
const char *src;
@@ -358,11 +357,7 @@ static void separate_git_dir(const char *git_dir)
358357
die_errno(_("unable to move %s to %s"), src, git_dir);
359358
}
360359

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);
366361
}
367362

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

daemon.c

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,15 +1070,6 @@ static struct credentials *prepare_credentials(const char *user_name,
10701070
}
10711071
#endif
10721072

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-
10821073
static int serve(struct string_list *listen_addr, int listen_port,
10831074
struct credentials *cred)
10841075
{
@@ -1289,7 +1280,7 @@ int main(int argc, char **argv)
12891280
sanitize_stdfds();
12901281

12911282
if (pid_file)
1292-
store_pid(pid_file);
1283+
write_file(pid_file, 1, "%"PRIuMAX"\n", (uintmax_t) getpid());
12931284

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

submodule.c

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,16 +1102,11 @@ void connect_work_tree_and_git_dir(const char *work_tree, const char *git_dir)
11021102
struct strbuf file_name = STRBUF_INIT;
11031103
struct strbuf rel_path = STRBUF_INIT;
11041104
const char *real_work_tree = xstrdup(real_path(work_tree));
1105-
FILE *fp;
11061105

11071106
/* Update gitfile */
11081107
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));
11151110

11161111
/* Update core.worktree setting */
11171112
strbuf_reset(&file_name);

transport.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,6 @@ static int write_one_ref(const char *name, const unsigned char *sha1,
283283
{
284284
struct strbuf *buf = data;
285285
int len = buf->len;
286-
FILE *f;
287286

288287
/* when called via for_each_ref(), flags is non-zero */
289288
if (flags && !starts_with(name, "refs/heads/") &&
@@ -292,10 +291,9 @@ static int write_one_ref(const char *name, const unsigned char *sha1,
292291

293292
strbuf_addstr(buf, name);
294293
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));
299297
strbuf_setlen(buf, len);
300298
return 0;
301299
}

0 commit comments

Comments
 (0)