use new wrapper write_file() for simple file writing
authorNguyễn Thái Ngọc Duy <pclouds@gmail.com>
Sun, 30 Nov 2014 08:24:46 +0000 (15:24 +0700)
committerJunio C Hamano <gitster@pobox.com>
Mon, 1 Dec 2014 19:00:16 +0000 (11:00 -0800)
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>
builtin/branch.c
builtin/init-db.c
daemon.c
submodule.c
transport.c
index 3b79c5087fbf1157d1bdfa2cccc99547f8abdb09..1a475d8e94a2ce98344e8732ace444e35ccd9bfb 100644 (file)
@@ -764,7 +764,6 @@ static const char edit_description[] = "BRANCH_DESCRIPTION";
 
 static int edit_branch_description(const char *branch_name)
 {
-       FILE *fp;
        int status;
        struct strbuf buf = STRBUF_INIT;
        struct strbuf name = STRBUF_INIT;
@@ -777,8 +776,7 @@ static int edit_branch_description(const char *branch_name)
                    "  %s\n"
                    "Lines starting with '%c' will be stripped.\n",
                    branch_name, comment_line_char);
-       fp = fopen(git_path(edit_description), "w");
-       if ((fwrite(buf.buf, 1, buf.len, fp) < buf.len) || fclose(fp)) {
+       if (write_file(git_path(edit_description), 0, "%s", buf.buf)) {
                strbuf_release(&buf);
                return error(_("could not write branch description template: %s"),
                             strerror(errno));
index 587a5055ed677c3541c85101d944ffb0a19b1962..6b7fa5f21ab524d2749099b5e7dd22ac6498cf02 100644 (file)
@@ -342,7 +342,6 @@ int set_git_dir_init(const char *git_dir, const char *real_git_dir,
 static void separate_git_dir(const char *git_dir)
 {
        struct stat st;
-       FILE *fp;
 
        if (!stat(git_link, &st)) {
                const char *src;
@@ -358,11 +357,7 @@ static void separate_git_dir(const char *git_dir)
                        die_errno(_("unable to move %s to %s"), src, git_dir);
        }
 
-       fp = fopen(git_link, "w");
-       if (!fp)
-               die(_("Could not create git link %s"), git_link);
-       fprintf(fp, "gitdir: %s\n", git_dir);
-       fclose(fp);
+       write_file(git_link, 1, "gitdir: %s\n", git_dir);
 }
 
 int init_db(const char *template_dir, unsigned int flags)
index 54a03bd527a81485b498e33225ed54c54c0dbff7..60bbab6bda0fa1e20ead36988afe444551ba6d64 100644 (file)
--- a/daemon.c
+++ b/daemon.c
@@ -1070,15 +1070,6 @@ static struct credentials *prepare_credentials(const char *user_name,
 }
 #endif
 
-static void store_pid(const char *path)
-{
-       FILE *f = fopen(path, "w");
-       if (!f)
-               die_errno("cannot open pid file '%s'", path);
-       if (fprintf(f, "%"PRIuMAX"\n", (uintmax_t) getpid()) < 0 || fclose(f) != 0)
-               die_errno("failed to write pid file '%s'", path);
-}
-
 static int serve(struct string_list *listen_addr, int listen_port,
     struct credentials *cred)
 {
@@ -1289,7 +1280,7 @@ int main(int argc, char **argv)
                sanitize_stdfds();
 
        if (pid_file)
-               store_pid(pid_file);
+               write_file(pid_file, 1, "%"PRIuMAX"\n", (uintmax_t) getpid());
 
        /* prepare argv for serving-processes */
        cld_argv = xmalloc(sizeof (char *) * (argc + 2));
index 0690dc50d07e9fd30242e41afe149476a34fc105..34094f5e97bbd63d6be2d697f656986c327c74ea 100644 (file)
@@ -1102,16 +1102,11 @@ void connect_work_tree_and_git_dir(const char *work_tree, const char *git_dir)
        struct strbuf file_name = STRBUF_INIT;
        struct strbuf rel_path = STRBUF_INIT;
        const char *real_work_tree = xstrdup(real_path(work_tree));
-       FILE *fp;
 
        /* Update gitfile */
        strbuf_addf(&file_name, "%s/.git", work_tree);
-       fp = fopen(file_name.buf, "w");
-       if (!fp)
-               die(_("Could not create git link %s"), file_name.buf);
-       fprintf(fp, "gitdir: %s\n", relative_path(git_dir, real_work_tree,
-                                                 &rel_path));
-       fclose(fp);
+       write_file(file_name.buf, 1, "gitdir: %s\n",
+                  relative_path(git_dir, real_work_tree, &rel_path));
 
        /* Update core.worktree setting */
        strbuf_reset(&file_name);
index 70d38e4c4b35c6207e226ab7cb54095c34bb4766..051b7ac175e4b1bb0d2e5c30528651fa59e381b0 100644 (file)
@@ -283,7 +283,6 @@ static int write_one_ref(const char *name, const unsigned char *sha1,
 {
        struct strbuf *buf = data;
        int len = buf->len;
-       FILE *f;
 
        /* when called via for_each_ref(), flags is non-zero */
        if (flags && !starts_with(name, "refs/heads/") &&
@@ -292,10 +291,9 @@ static int write_one_ref(const char *name, const unsigned char *sha1,
 
        strbuf_addstr(buf, name);
        if (safe_create_leading_directories(buf->buf) ||
-                       !(f = fopen(buf->buf, "w")) ||
-                       fprintf(f, "%s\n", sha1_to_hex(sha1)) < 0 ||
-                       fclose(f))
-               return error("problems writing temporary file %s", buf->buf);
+           write_file(buf->buf, 0, "%s\n", sha1_to_hex(sha1)))
+               return error("problems writing temporary file %s: %s",
+                            buf->buf, strerror(errno));
        strbuf_setlen(buf, len);
        return 0;
 }