git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2 01/21] Use xfopen() in more places

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
May 3, 2017, 10:16 UTC
Message-ID
<20170503101706.9223-2-pclouds@gmail.com>
In-Reply-To
<20170503101706.9223-1-pclouds@gmail.com>
xfopen()
 - provides error details
 - explains error on reading, or writing, or whatever operation
 - has l10n support
 - prints file name in the error

Some of these are missing in the places that are replaced with xfopen(), which is a clear win. In some other places, it's just less code (not as clearly a win as the previous case but still is).

The only slight regresssion is in remote-testsvn, where we don't report the file class (marks files) in the error messages anymore. But since this is a _test_ svn remote transport, I'm not too concerned.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 bisect.c              | 5 +----
 builtin/am.c          | 8 ++------
 builtin/commit.c      | 5 +----
 builtin/fast-export.c | 4 +---
 builtin/fsck.c        | 3 +--
 builtin/merge.c       | 4 +---
 builtin/pull.c        | 3 +--
 diff.c                | 8 ++------
 fast-import.c         | 4 +---
 remote-testsvn.c      | 8 ++------
 10 files changed, 13 insertions(+), 39 deletions(-)
diff --git a/bisect.c b/bisect.c
index 08c9fb7266..469a3e9061 100644
--- a/bisect.c
+++ b/bisect.c
@@ -438,10 +438,7 @@ static void read_bisect_paths(struct argv_array *array)
 {
 	struct strbuf str = STRBUF_INIT;
 	const char *filename = git_path_bisect_names();
-	FILE *fp = fopen(filename, "r");
-
-	if (!fp)
-		die_errno(_("Could not open file '%s'"), filename);
+	FILE *fp = xfopen(filename, "r");
 
 	while (strbuf_getline_lf(&str, fp) != EOF) {
 		strbuf_trim(&str);
diff --git a/builtin/am.c b/builtin/am.c
index a95dd8b4e6..f5dac7783e 100644
--- a/builtin/am.c
+++ b/builtin/am.c
@@ -1275,12 +1275,8 @@ static int parse_mail(struct am_state *state, const char *mail)
 		die("BUG: invalid value for state->scissors");
 	}
 
-	mi.input = fopen(mail, "r");
-	if (!mi.input)
-		die("could not open input");
-	mi.output = fopen(am_path(state, "info"), "w");
-	if (!mi.output)
-		die("could not open output 'info'");
+	mi.input = xfopen(mail, "r");
+	mi.output = xfopen(am_path(state, "info"), "w");
 	if (mailinfo(&mi, am_path(state, "msg"), am_path(state, "patch")))
 		die("could not parse patch");
 
diff --git a/builtin/commit.c b/builtin/commit.c
index 1d805f5da8..eda0d32311 100644
--- a/builtin/commit.c
+++ b/builtin/commit.c
@@ -1695,10 +1695,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
 		if (!reflog_msg)
 			reflog_msg = "commit (merge)";
 		pptr = commit_list_append(current_head, pptr);
-		fp = fopen(git_path_merge_head(), "r");
-		if (fp == NULL)
-			die_errno(_("could not open '%s' for reading"),
-				  git_path_merge_head());
+		fp = xfopen(git_path_merge_head(), "r");
 		while (strbuf_getline_lf(&m, fp) != EOF) {
 			struct commit *parent;
 
diff --git a/builtin/fast-export.c b/builtin/fast-export.c
index e0220630d0..128b99e6da 100644
--- a/builtin/fast-export.c
+++ b/builtin/fast-export.c
@@ -905,9 +905,7 @@ static void export_marks(char *file)
 static void import_marks(char *input_file)
 {
 	char line[512];
-	FILE *f = fopen(input_file, "r");
-	if (!f)
-		die_errno("cannot read '%s'", input_file);
+	FILE *f = xfopen(input_file, "r");
 
 	while (fgets(line, sizeof(line), f)) {
 		uint32_t mark;
diff --git a/builtin/fsck.c b/builtin/fsck.c
index b5e13a4556..00beaaa4e6 100644
--- a/builtin/fsck.c
+++ b/builtin/fsck.c
@@ -280,8 +280,7 @@ static void check_unreachable_object(struct object *obj)
 				free(filename);
 				return;
 			}
-			if (!(f = fopen(filename, "w")))
-				die_errno("Could not open '%s'", filename);
+			f = xfopen(filename, "w");
 			if (obj->type == OBJ_BLOB) {
 				if (stream_blob_to_fd(fileno(f), &obj->oid, NULL, 1))
 					die_errno("Could not write '%s'", filename);
diff --git a/builtin/merge.c b/builtin/merge.c
index 703827f006..65a1501858 100644
--- a/builtin/merge.c
+++ b/builtin/merge.c
@@ -839,9 +839,7 @@ static int suggest_conflicts(void)
 	struct strbuf msgbuf = STRBUF_INIT;
 
 	filename = git_path_merge_msg();
-	fp = fopen(filename, "a");
-	if (!fp)
-		die_errno(_("Could not open '%s' for writing"), filename);
+	fp = xfopen(filename, "a");
 
 	append_conflicts_hint(&msgbuf);
 	fputs(msgbuf.buf, fp);
diff --git a/builtin/pull.c b/builtin/pull.c
index dd1a4a94e4..589c25becf 100644
--- a/builtin/pull.c
+++ b/builtin/pull.c
@@ -337,8 +337,7 @@ static void get_merge_heads(struct oid_array *merge_heads)
 	struct strbuf sb = STRBUF_INIT;
 	struct object_id oid;
 
-	if (!(fp = fopen(filename, "r")))
-		die_errno(_("could not open '%s' for reading"), filename);
+	fp = xfopen(filename, "r");
 	while (strbuf_getline_lf(&sb, fp) != EOF) {
 		if (get_oid_hex(sb.buf, &oid))
 			continue;  /* invalid line: does not start with SHA1 */
diff --git a/diff.c b/diff.c
index 11eef1c85d..b6597ce568 100644
--- a/diff.c
+++ b/diff.c
@@ -4071,9 +4071,7 @@ int diff_opt_parse(struct diff_options *options,
 		DIFF_OPT_CLR(options, FUNCCONTEXT);
 	else if ((argcount = parse_long_opt("output", av, &optarg))) {
 		char *path = prefix_filename(prefix, optarg);
-		options->file = fopen(path, "w");
-		if (!options->file)
-			die_errno("Could not open '%s'", path);
+		options->file = xfopen(path, "w");
 		options->close_file = 1;
 		if (options->use_color != GIT_COLOR_ALWAYS)
 			options->use_color = GIT_COLOR_NEVER;
@@ -4807,9 +4805,7 @@ void diff_flush(struct diff_options *options)
 		 */
 		if (options->close_file)
 			fclose(options->file);
-		options->file = fopen("/dev/null", "w");
-		if (!options->file)
-			die_errno("Could not open /dev/null");
+		options->file = xfopen("/dev/null", "w");
 		options->close_file = 1;
 		for (i = 0; i < q->nr; i++) {
 			struct diff_filepair *p = q->queue[i];
diff --git a/fast-import.c b/fast-import.c
index cf58f875b8..420b3a00d3 100644
--- a/fast-import.c
+++ b/fast-import.c
@@ -3274,9 +3274,7 @@ static void option_export_pack_edges(const char *edges)
 {
 	if (pack_edges)
 		fclose(pack_edges);
-	pack_edges = fopen(edges, "a");
-	if (!pack_edges)
-		die_errno("Cannot open '%s'", edges);
+	pack_edges = xfopen(edges, "a");
 }
 
 static int parse_one_option(const char *option)
diff --git a/remote-testsvn.c b/remote-testsvn.c
index f87bf851ba..50404ef343 100644
--- a/remote-testsvn.c
+++ b/remote-testsvn.c
@@ -124,10 +124,8 @@ static int note2mark_cb(const unsigned char *object_sha1,
 static void regenerate_marks(void)
 {
 	int ret;
-	FILE *marksfile = fopen(marksfilename, "w+");
+	FILE *marksfile = xfopen(marksfilename, "w+");
 
-	if (!marksfile)
-		die_errno("Couldn't create mark file %s.", marksfilename);
 	ret = for_each_note(NULL, 0, note2mark_cb, marksfile);
 	if (ret)
 		die("Regeneration of marks failed, returned %d.", ret);
@@ -148,9 +146,7 @@ static void check_or_regenerate_marks(int latestrev)
 	marksfile = fopen(marksfilename, "r");
 	if (!marksfile) {
 		regenerate_marks();
-		marksfile = fopen(marksfilename, "r");
-		if (!marksfile)
-			die_errno("cannot read marks file %s!", marksfilename);
+		marksfile = xfopen(marksfilename, "r");
 		fclose(marksfile);
 	} else {
 		strbuf_addf(&sb, ":%d ", latestrev);
-- 
2.11.0.157.gd943d85
Previous: Nguyễn Thái Ngọc DuyNext: Nguyễn Thái Ngọc Duy
Message 32 of 70 in “Handle fopen() errors”
  1. 00/15 Handle fopen() errorsNguyễn Thái Ngọc Duy, Apr 20, 2017
  2. 02/15 bisect: report on fopen() errorNguyễn Thái Ngọc Duy, Apr 20, 2017
  3. 03/15 blame: report error on open if graft_file is a directoryNguyễn Thái Ngọc Duy, Apr 20, 2017
  4. 01/15 config.mak.uname: set FREAD_READS_DIRECTORIES for Linux and FreeBSDNguyễn Thái Ngọc Duy, Apr 20, 2017
  5. 05/15 log: report errno on failure to fopen() a fileNguyễn Thái Ngọc Duy, Apr 20, 2017
  6. Jeff KingApr 21, 2017
  7. 06/15 commit.c: report error on failure to fopen() the graft fileNguyễn Thái Ngọc Duy, Apr 20, 2017
  8. 09/15 rerere.c: report correct errnoNguyễn Thái Ngọc Duy, Apr 20, 2017
  9. 04/15 clone: use xfopen() instead of fopen()Nguyễn Thái Ngọc Duy, Apr 20, 2017
  10. 10/15 sequencer.c: report error on failure to fopen()Nguyễn Thái Ngọc Duy, Apr 20, 2017
  11. 08/15 rerere.c: report error on failure to fopen()Nguyễn Thái Ngọc Duy, Apr 20, 2017
  12. 11/15 server-info: report error on failure to fopen()Nguyễn Thái Ngọc Duy, Apr 20, 2017
  13. 07/15 remote.c: report error on failure to fopen()Nguyễn Thái Ngọc Duy, Apr 20, 2017
  14. Johannes SixtApr 26, 2017
  15. Junio C HamanoApr 27, 2017
  16. Johannes SixtApr 27, 2017
  17. Duy NguyenApr 27, 2017
  18. Johannes SixtApr 27, 2017
  19. 12/15 wt-status.c: report error on failure to fopen()Nguyễn Thái Ngọc Duy, Apr 20, 2017
  20. 15/15 t1308: add a test case on open a config directoryNguyễn Thái Ngọc Duy, Apr 20, 2017
  21. 14/15 config.c: handle error on failing to fopen()Nguyễn Thái Ngọc Duy, Apr 20, 2017
  22. 13/15 xdiff-interface.c: report errno on failure to stat() or fopen()Nguyễn Thái Ngọc Duy, Apr 20, 2017
  23. Junio C HamanoApr 21, 2017
  24. Junio C HamanoApr 21, 2017
  25. Jeff KingApr 21, 2017
  26. Duy NguyenApr 21, 2017
  27. Junio C HamanoApr 21, 2017
  28. Duy NguyenApr 21, 2017
  29. Jeff KingApr 21, 2017
  30. Junio C HamanoApr 24, 2017
  31. 00/21 Nguyễn Thái Ngọc Duy, May 3, 2017
  32. 01/21 Use xfopen() in more placesNguyễn Thái Ngọc Duy, May 3, 2017
  33. 03/21 config.mak.uname: set FREAD_READS_DIRECTORIES for Linux and FreeBSDNguyễn Thái Ngọc Duy, May 3, 2017
  34. 02/21 clone: use xfopen() instead of fopen()Nguyễn Thái Ngọc Duy, May 3, 2017
  35. Johannes SchindelinMay 3, 2017
  36. 04/21 wrapper.c: add warn_on_fopen_errors()Nguyễn Thái Ngọc Duy, May 3, 2017
  37. Johannes SchindelinMay 3, 2017
  38. Junio C HamanoMay 4, 2017
  39. 05/21 wrapper.c: add fopen_or_warn()Nguyễn Thái Ngọc Duy, May 3, 2017
  40. 06/21 attr.c: use fopen_or_warn()Nguyễn Thái Ngọc Duy, May 3, 2017
  41. Johannes SchindelinMay 3, 2017
  42. 07/21 ident.c: use fopen_or_warn()Nguyễn Thái Ngọc Duy, May 3, 2017
  43. 08/21 bisect: report on fopen() errorNguyễn Thái Ngọc Duy, May 3, 2017
  44. Johannes SchindelinMay 3, 2017
  45. 09/21 blame: report error on open if graft_file is a directoryNguyễn Thái Ngọc Duy, May 3, 2017
  46. Johannes SchindelinMay 3, 2017
  47. 10/21 log: report errno on failure to fopen() a fileNguyễn Thái Ngọc Duy, May 3, 2017
  48. Johannes SchindelinMay 3, 2017
  49. 11/21 log: fix memory leak in open_next_file()Nguyễn Thái Ngọc Duy, May 3, 2017
  50. 12/21 commit.c: report error on failure to fopen() the graft fileNguyễn Thái Ngọc Duy, May 3, 2017
  51. 13/21 remote.c: report error on failure to fopen()Nguyễn Thái Ngọc Duy, May 3, 2017
  52. Johannes SchindelinMay 3, 2017
  53. Duy NguyenMay 9, 2017
  54. Johannes SchindelinMay 9, 2017
  55. 14/21 rerere.c: report error on failure to fopen()Nguyễn Thái Ngọc Duy, May 3, 2017
  56. 15/21 rerere.c: report correct errnoNguyễn Thái Ngọc Duy, May 3, 2017
  57. Johannes SchindelinMay 3, 2017
  58. 17/21 server-info: report error on failure to fopen()Nguyễn Thái Ngọc Duy, May 3, 2017
  59. 16/21 sequencer.c: report error on failure to fopen()Nguyễn Thái Ngọc Duy, May 3, 2017
  60. 18/21 wt-status.c: report error on failure to fopen()Nguyễn Thái Ngọc Duy, May 3, 2017
  61. 20/21 config.c: handle error on failing to fopen()Nguyễn Thái Ngọc Duy, May 3, 2017
  62. Johannes SchindelinMay 3, 2017
  63. 21/21 t1308: add a test case on open a config directoryNguyễn Thái Ngọc Duy, May 3, 2017
  64. Johannes SchindelinMay 3, 2017
  65. 19/21 xdiff-interface.c: report errno on failure to stat() or fopen()Nguyễn Thái Ngọc Duy, May 3, 2017
  66. Junio C HamanoMay 4, 2017
  67. Junio C HamanoMay 7, 2017
  68. Duy NguyenMay 9, 2017
  69. Junio C HamanoMay 9, 2017
  70. config.mak.uname: set FREAD_READS_DIRECTORIES for Darwin, tooJunio C Hamano, May 10, 2017

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.