From: "Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
To: git@vger.kernel.org
Cc: "Junio C Hamano" <gitster@pobox.com>,
"Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
Subject: [PATCH 21/32] use new wrapper write_file() for simple file writing
Date: Sat, 30 Aug 2014 15:33:51 +0700 [thread overview]
Message-ID: <1409387642-24492-22-git-send-email-pclouds@gmail.com> (raw)
In-Reply-To: <1409387642-24492-1-git-send-email-pclouds@gmail.com>
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 | 4 +---
builtin/init-db.c | 7 +------
daemon.c | 11 +----------
submodule.c | 9 ++-------
transport.c | 8 +++-----
5 files changed, 8 insertions(+), 31 deletions(-)
diff --git a/builtin/branch.c b/builtin/branch.c
index 0591b22..e4265a1 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -754,7 +754,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;
@@ -767,8 +766,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));
diff --git a/builtin/init-db.c b/builtin/init-db.c
index 56f85e2..ce8416a 100644
--- a/builtin/init-db.c
+++ b/builtin/init-db.c
@@ -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)
diff --git a/daemon.c b/daemon.c
index 1eb6631..dd638a9 100644
--- a/daemon.c
+++ b/daemon.c
@@ -1094,15 +1094,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)
{
@@ -1313,7 +1304,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));
diff --git a/submodule.c b/submodule.c
index b80ecac..b7b6059 100644
--- a/submodule.c
+++ b/submodule.c
@@ -1112,16 +1112,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);
diff --git a/transport.c b/transport.c
index 59c9727..1b779bb 100644
--- a/transport.c
+++ b/transport.c
@@ -296,7 +296,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/") &&
@@ -305,10 +304,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;
}
--
2.1.0.rc0.78.gc0d8480
next prev parent reply other threads:[~2014-08-30 8:36 UTC|newest]
Thread overview: 134+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-08-30 8:33 [PATCH 00/32] nd/multiple-work-trees cleanup Nguyễn Thái Ngọc Duy
2014-08-30 8:33 ` [PATCH 01/32] path.c: make get_pathname() return strbuf instead of static buffer Nguyễn Thái Ngọc Duy
2014-08-30 8:33 ` [PATCH 02/32] path.c: make get_pathname() call sites return const char * Nguyễn Thái Ngọc Duy
2014-08-30 8:33 ` [PATCH 03/32] git_snpath(): retire and replace with strbuf_git_path() Nguyễn Thái Ngọc Duy
2014-08-30 8:33 ` [PATCH 04/32] path.c: rename vsnpath() to do_git_path() Nguyễn Thái Ngọc Duy
2014-08-30 8:33 ` [PATCH 05/32] path.c: group git_path(), git_pathdup() and strbuf_git_path() together Nguyễn Thái Ngọc Duy
2014-08-30 8:33 ` [PATCH 06/32] git_path(): be aware of file relocation in $GIT_DIR Nguyễn Thái Ngọc Duy
2014-08-30 8:33 ` [PATCH 07/32] *.sh: respect $GIT_INDEX_FILE Nguyễn Thái Ngọc Duy
2014-08-30 8:33 ` [PATCH 08/32] reflog: avoid constructing .lock path with git_path Nguyễn Thái Ngọc Duy
2014-08-30 8:33 ` [PATCH 09/32] fast-import: use git_path() for accessing .git dir instead of get_git_dir() Nguyễn Thái Ngọc Duy
2014-08-30 8:33 ` [PATCH 10/32] commit: use SEQ_DIR instead of hardcoding "sequencer" Nguyễn Thái Ngọc Duy
2014-08-30 8:33 ` [PATCH 11/32] $GIT_COMMON_DIR: a new environment variable Nguyễn Thái Ngọc Duy
2014-08-30 8:33 ` [PATCH 12/32] git-sh-setup.sh: use rev-parse --git-path to get $GIT_DIR/objects Nguyễn Thái Ngọc Duy
2014-08-30 8:33 ` [PATCH 13/32] *.sh: avoid hardcoding $GIT_DIR/hooks/ Nguyễn Thái Ngọc Duy
2014-08-30 8:33 ` [PATCH 14/32] git-stash: avoid hardcoding $GIT_DIR/logs/ Nguyễn Thái Ngọc Duy
2014-08-30 8:33 ` [PATCH 15/32] setup.c: convert is_git_directory() to use strbuf Nguyễn Thái Ngọc Duy
2014-08-30 8:33 ` [PATCH 16/32] setup.c: detect $GIT_COMMON_DIR in is_git_directory() Nguyễn Thái Ngọc Duy
2014-08-30 8:33 ` [PATCH 17/32] setup.c: convert check_repository_format_gently to use strbuf Nguyễn Thái Ngọc Duy
2014-08-30 8:33 ` [PATCH 18/32] setup.c: detect $GIT_COMMON_DIR check_repository_format_gently() Nguyễn Thái Ngọc Duy
2014-08-30 8:33 ` [PATCH 19/32] setup.c: support multi-checkout repo setup Nguyễn Thái Ngọc Duy
2014-08-30 8:33 ` [PATCH 20/32] wrapper.c: wrapper to open a file, fprintf then close Nguyễn Thái Ngọc Duy
2014-08-30 8:33 ` Nguyễn Thái Ngọc Duy [this message]
2014-08-30 8:33 ` [PATCH 22/32] checkout: support checking out into a new working directory Nguyễn Thái Ngọc Duy
2014-08-30 20:50 ` Philip Oakley
2014-08-31 4:49 ` Duy Nguyen
2014-08-31 4:52 ` Duy Nguyen
2014-08-31 11:08 ` Philip Oakley
2014-09-02 12:27 ` Duy Nguyen
2014-09-02 14:51 ` Marc Branchaud
2014-09-02 17:33 ` Junio C Hamano
2014-09-04 14:19 ` Duy Nguyen
2014-09-08 10:52 ` Duy Nguyen
2014-09-08 14:06 ` Marc Branchaud
2014-09-05 3:26 ` Scott Schmit
2014-09-05 13:42 ` Duy Nguyen
2014-08-30 8:33 ` [PATCH 23/32] checkout: clean up half-prepared directories in --to mode Nguyễn Thái Ngọc Duy
2014-08-30 8:33 ` [PATCH 24/32] checkout: reject if the branch is already checked out elsewhere Nguyễn Thái Ngọc Duy
2014-08-30 8:33 ` [PATCH 25/32] prune: strategies for linked checkouts Nguyễn Thái Ngọc Duy
2014-08-30 8:33 ` [PATCH 26/32] gc: style change -- no SP before closing parenthesis Nguyễn Thái Ngọc Duy
2014-08-30 8:33 ` [PATCH 27/32] gc: factor out gc.pruneexpire parsing code Nguyễn Thái Ngọc Duy
2014-08-30 8:33 ` [PATCH 28/32] gc: support prune --repos Nguyễn Thái Ngọc Duy
2014-08-30 8:33 ` [PATCH 29/32] count-objects: report unused files in $GIT_DIR/repos/ Nguyễn Thái Ngọc Duy
2014-08-30 8:34 ` [PATCH 30/32] git_path(): keep "info/sparse-checkout" per work-tree Nguyễn Thái Ngọc Duy
2014-08-30 8:34 ` [PATCH 31/32] checkout: don't require a work tree when checking out into a new one Nguyễn Thái Ngọc Duy
2014-08-30 8:34 ` [PATCH 32/32] t2025: add a test to make sure grafts is working from a linked checkout Nguyễn Thái Ngọc Duy
2014-08-30 11:11 ` [PATCH 00/32] nd/multiple-work-trees cleanup Eric Sunshine
2014-08-30 11:14 ` Duy Nguyen
2014-09-02 17:29 ` Junio C Hamano
2014-09-10 22:41 ` [PATCH v2 00/32] nd/multiple-work-trees Nguyễn Thái Ngọc Duy
2014-09-10 22:41 ` [PATCH v2 01/32] path.c: make get_pathname() return strbuf instead of static buffer Nguyễn Thái Ngọc Duy
2014-09-10 22:41 ` [PATCH v2 02/32] path.c: make get_pathname() call sites return const char * Nguyễn Thái Ngọc Duy
2014-09-10 22:41 ` [PATCH v2 03/32] git_snpath(): retire and replace with strbuf_git_path() Nguyễn Thái Ngọc Duy
2014-09-10 22:41 ` [PATCH v2 04/32] path.c: rename vsnpath() to do_git_path() Nguyễn Thái Ngọc Duy
2014-09-10 22:41 ` [PATCH v2 05/32] path.c: group git_path(), git_pathdup() and strbuf_git_path() together Nguyễn Thái Ngọc Duy
2014-09-10 22:41 ` [PATCH v2 06/32] git_path(): be aware of file relocation in $GIT_DIR Nguyễn Thái Ngọc Duy
2014-09-10 22:41 ` [PATCH v2 07/32] *.sh: respect $GIT_INDEX_FILE Nguyễn Thái Ngọc Duy
2014-09-10 22:41 ` [PATCH v2 08/32] reflog: avoid constructing .lock path with git_path Nguyễn Thái Ngọc Duy
2014-09-10 22:41 ` [PATCH v2 09/32] fast-import: use git_path() for accessing .git dir instead of get_git_dir() Nguyễn Thái Ngọc Duy
2014-09-10 22:41 ` [PATCH v2 10/32] commit: use SEQ_DIR instead of hardcoding "sequencer" Nguyễn Thái Ngọc Duy
2014-09-10 22:41 ` [PATCH v2 11/32] $GIT_COMMON_DIR: a new environment variable Nguyễn Thái Ngọc Duy
2014-09-10 22:41 ` [PATCH v2 12/32] git-sh-setup.sh: use rev-parse --git-path to get $GIT_DIR/objects Nguyễn Thái Ngọc Duy
2014-09-10 22:41 ` [PATCH v2 13/32] *.sh: avoid hardcoding $GIT_DIR/hooks/ Nguyễn Thái Ngọc Duy
2014-09-10 22:41 ` [PATCH v2 14/32] git-stash: avoid hardcoding $GIT_DIR/logs/ Nguyễn Thái Ngọc Duy
2014-09-10 22:41 ` [PATCH v2 15/32] setup.c: convert is_git_directory() to use strbuf Nguyễn Thái Ngọc Duy
2014-09-10 22:41 ` [PATCH v2 16/32] setup.c: detect $GIT_COMMON_DIR in is_git_directory() Nguyễn Thái Ngọc Duy
2014-09-10 22:41 ` [PATCH v2 17/32] setup.c: convert check_repository_format_gently to use strbuf Nguyễn Thái Ngọc Duy
2014-09-10 22:41 ` [PATCH v2 18/32] setup.c: detect $GIT_COMMON_DIR check_repository_format_gently() Nguyễn Thái Ngọc Duy
2014-09-10 22:41 ` [PATCH v2 19/32] setup.c: support multi-checkout repo setup Nguyễn Thái Ngọc Duy
2014-09-10 22:41 ` [PATCH v2 20/32] wrapper.c: wrapper to open a file, fprintf then close Nguyễn Thái Ngọc Duy
2014-09-10 22:41 ` [PATCH v2 21/32] use new wrapper write_file() for simple file writing Nguyễn Thái Ngọc Duy
2014-09-10 22:41 ` [PATCH v2 22/32] checkout: support checking out into a new working directory Nguyễn Thái Ngọc Duy
2014-09-11 15:02 ` Marc Branchaud
2014-09-21 2:41 ` Duy Nguyen
2014-09-21 3:10 ` Eric Sunshine
2014-09-21 9:50 ` Duy Nguyen
2014-09-22 21:00 ` Marc Branchaud
2014-09-22 23:01 ` Eric Sunshine
2014-09-10 22:41 ` [PATCH v2 23/32] prune: strategies for linked checkouts Nguyễn Thái Ngọc Duy
2014-09-11 15:36 ` Marc Branchaud
2014-09-12 3:06 ` Eric Sunshine
2014-09-12 19:08 ` Marc Branchaud
2014-09-21 2:54 ` Duy Nguyen
2014-09-21 3:01 ` Eric Sunshine
2014-09-21 10:29 ` Duy Nguyen
2014-09-22 21:06 ` Marc Branchaud
2014-09-22 22:19 ` Eric Sunshine
2014-09-10 22:42 ` [PATCH v2 24/32] checkout: reject if the branch is already checked out elsewhere Nguyễn Thái Ngọc Duy
2014-09-10 22:42 ` [PATCH v2 25/32] checkout: clean up half-prepared directories in --to mode Nguyễn Thái Ngọc Duy
2014-09-10 22:42 ` [PATCH v2 26/32] gc: style change -- no SP before closing parenthesis Nguyễn Thái Ngọc Duy
2014-09-10 22:42 ` [PATCH v2 27/32] gc: factor out gc.pruneexpire parsing code Nguyễn Thái Ngọc Duy
2014-09-10 22:42 ` [PATCH v2 28/32] gc: support prune --worktrees Nguyễn Thái Ngọc Duy
2014-09-21 10:43 ` Duy Nguyen
2014-09-22 21:06 ` Marc Branchaud
2014-09-10 22:42 ` [PATCH v2 29/32] count-objects: report unused files in $GIT_DIR/worktrees/ Nguyễn Thái Ngọc Duy
2014-09-10 22:42 ` [PATCH v2 30/32] git_path(): keep "info/sparse-checkout" per work-tree Nguyễn Thái Ngọc Duy
2014-09-10 22:42 ` [PATCH v2 31/32] checkout: don't require a work tree when checking out into a new one Nguyễn Thái Ngọc Duy
2014-09-10 22:42 ` [PATCH v2 32/32] t2025: add a test to make sure grafts is working from a linked checkout Nguyễn Thái Ngọc Duy
2014-09-25 21:20 ` [PATCH v2 00/32] nd/multiple-work-trees Junio C Hamano
2014-09-28 1:22 ` [PATCH v3 " Nguyễn Thái Ngọc Duy
2014-09-28 1:22 ` [PATCH v3 01/32] path.c: make get_pathname() return strbuf instead of static buffer Nguyễn Thái Ngọc Duy
2014-09-28 1:22 ` [PATCH v3 02/32] path.c: make get_pathname() call sites return const char * Nguyễn Thái Ngọc Duy
2014-09-28 1:22 ` [PATCH v3 03/32] git_snpath(): retire and replace with strbuf_git_path() Nguyễn Thái Ngọc Duy
2014-09-28 1:22 ` [PATCH v3 04/32] path.c: rename vsnpath() to do_git_path() Nguyễn Thái Ngọc Duy
2014-09-28 1:22 ` [PATCH v3 05/32] path.c: group git_path(), git_pathdup() and strbuf_git_path() together Nguyễn Thái Ngọc Duy
2014-09-28 1:22 ` [PATCH v3 06/32] git_path(): be aware of file relocation in $GIT_DIR Nguyễn Thái Ngọc Duy
2014-09-28 1:22 ` [PATCH v3 07/32] *.sh: respect $GIT_INDEX_FILE Nguyễn Thái Ngọc Duy
2014-09-28 1:22 ` [PATCH v3 08/32] reflog: avoid constructing .lock path with git_path Nguyễn Thái Ngọc Duy
2014-09-28 1:22 ` [PATCH v3 09/32] fast-import: use git_path() for accessing .git dir instead of get_git_dir() Nguyễn Thái Ngọc Duy
2014-09-28 1:22 ` [PATCH v3 10/32] commit: use SEQ_DIR instead of hardcoding "sequencer" Nguyễn Thái Ngọc Duy
2014-09-28 1:22 ` [PATCH v3 11/32] $GIT_COMMON_DIR: a new environment variable Nguyễn Thái Ngọc Duy
2014-09-28 1:22 ` [PATCH v3 12/32] git-sh-setup.sh: use rev-parse --git-path to get $GIT_DIR/objects Nguyễn Thái Ngọc Duy
2014-09-28 1:22 ` [PATCH v3 13/32] *.sh: avoid hardcoding $GIT_DIR/hooks/ Nguyễn Thái Ngọc Duy
2014-09-28 1:22 ` [PATCH v3 14/32] git-stash: avoid hardcoding $GIT_DIR/logs/ Nguyễn Thái Ngọc Duy
2014-09-28 1:22 ` [PATCH v3 15/32] setup.c: convert is_git_directory() to use strbuf Nguyễn Thái Ngọc Duy
2014-09-28 1:22 ` [PATCH v3 16/32] setup.c: detect $GIT_COMMON_DIR in is_git_directory() Nguyễn Thái Ngọc Duy
2014-09-28 1:22 ` [PATCH v3 17/32] setup.c: convert check_repository_format_gently to use strbuf Nguyễn Thái Ngọc Duy
2014-09-28 1:22 ` [PATCH v3 18/32] setup.c: detect $GIT_COMMON_DIR check_repository_format_gently() Nguyễn Thái Ngọc Duy
2014-09-28 1:22 ` [PATCH v3 19/32] setup.c: support multi-checkout repo setup Nguyễn Thái Ngọc Duy
2014-10-01 16:19 ` Johannes Sixt
2014-09-28 1:22 ` [PATCH v3 20/32] wrapper.c: wrapper to open a file, fprintf then close Nguyễn Thái Ngọc Duy
2014-09-28 1:22 ` [PATCH v3 21/32] use new wrapper write_file() for simple file writing Nguyễn Thái Ngọc Duy
2014-09-28 1:22 ` [PATCH v3 22/32] checkout: support checking out into a new working directory Nguyễn Thái Ngọc Duy
2014-09-28 1:22 ` [PATCH v3 23/32] prune: strategies for linked checkouts Nguyễn Thái Ngọc Duy
2014-10-01 16:20 ` Johannes Sixt
2014-09-28 1:22 ` [PATCH v3 24/32] checkout: reject if the branch is already checked out elsewhere Nguyễn Thái Ngọc Duy
2014-09-28 1:22 ` [PATCH v3 25/32] checkout: clean up half-prepared directories in --to mode Nguyễn Thái Ngọc Duy
2014-09-28 1:22 ` [PATCH v3 26/32] gc: style change -- no SP before closing parenthesis Nguyễn Thái Ngọc Duy
2014-09-28 1:22 ` [PATCH v3 27/32] gc: factor out gc.pruneexpire parsing code Nguyễn Thái Ngọc Duy
2014-09-28 1:22 ` [PATCH v3 28/32] gc: support prune --worktrees Nguyễn Thái Ngọc Duy
2014-09-28 1:22 ` [PATCH v3 29/32] count-objects: report unused files in $GIT_DIR/worktrees/ Nguyễn Thái Ngọc Duy
2014-09-28 1:22 ` [PATCH v3 30/32] git_path(): keep "info/sparse-checkout" per work-tree Nguyễn Thái Ngọc Duy
2014-09-28 1:22 ` [PATCH v3 31/32] checkout: don't require a work tree when checking out into a new one Nguyễn Thái Ngọc Duy
2014-09-28 1:22 ` [PATCH v3 32/32] t2025: add a test to make sure grafts is working from a linked checkout Nguyễn Thái Ngọc Duy
2014-09-28 6:35 ` [PATCH v3 00/32] nd/multiple-work-trees Junio C Hamano
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1409387642-24492-22-git-send-email-pclouds@gmail.com \
--to=pclouds@gmail.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).