use xfopen() in more places
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> Signed-off-by: Junio C Hamano <gitster@pobox.com>maint
							parent
							
								
									b0a642ac46
								
							
						
					
					
						commit
						23a9e0712d
					
				
							
								
								
									
										5
									
								
								bisect.c
								
								
								
								
							
							
						
						
									
										5
									
								
								bisect.c
								
								
								
								
							|  | @ -438,10 +438,7 @@ static void read_bisect_paths(struct argv_array *array) | ||||||
| { | { | ||||||
| 	struct strbuf str = STRBUF_INIT; | 	struct strbuf str = STRBUF_INIT; | ||||||
| 	const char *filename = git_path_bisect_names(); | 	const char *filename = git_path_bisect_names(); | ||||||
| 	FILE *fp = fopen(filename, "r"); | 	FILE *fp = xfopen(filename, "r"); | ||||||
|  |  | ||||||
| 	if (!fp) |  | ||||||
| 		die_errno(_("Could not open file '%s'"), filename); |  | ||||||
|  |  | ||||||
| 	while (strbuf_getline_lf(&str, fp) != EOF) { | 	while (strbuf_getline_lf(&str, fp) != EOF) { | ||||||
| 		strbuf_trim(&str); | 		strbuf_trim(&str); | ||||||
|  |  | ||||||
|  | @ -1275,12 +1275,8 @@ static int parse_mail(struct am_state *state, const char *mail) | ||||||
| 		die("BUG: invalid value for state->scissors"); | 		die("BUG: invalid value for state->scissors"); | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	mi.input = fopen(mail, "r"); | 	mi.input = xfopen(mail, "r"); | ||||||
| 	if (!mi.input) | 	mi.output = xfopen(am_path(state, "info"), "w"); | ||||||
| 		die("could not open input"); |  | ||||||
| 	mi.output = fopen(am_path(state, "info"), "w"); |  | ||||||
| 	if (!mi.output) |  | ||||||
| 		die("could not open output 'info'"); |  | ||||||
| 	if (mailinfo(&mi, am_path(state, "msg"), am_path(state, "patch"))) | 	if (mailinfo(&mi, am_path(state, "msg"), am_path(state, "patch"))) | ||||||
| 		die("could not parse patch"); | 		die("could not parse patch"); | ||||||
|  |  | ||||||
|  |  | ||||||
|  | @ -1695,10 +1695,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix) | ||||||
| 		if (!reflog_msg) | 		if (!reflog_msg) | ||||||
| 			reflog_msg = "commit (merge)"; | 			reflog_msg = "commit (merge)"; | ||||||
| 		pptr = commit_list_append(current_head, pptr); | 		pptr = commit_list_append(current_head, pptr); | ||||||
| 		fp = fopen(git_path_merge_head(), "r"); | 		fp = xfopen(git_path_merge_head(), "r"); | ||||||
| 		if (fp == NULL) |  | ||||||
| 			die_errno(_("could not open '%s' for reading"), |  | ||||||
| 				  git_path_merge_head()); |  | ||||||
| 		while (strbuf_getline_lf(&m, fp) != EOF) { | 		while (strbuf_getline_lf(&m, fp) != EOF) { | ||||||
| 			struct commit *parent; | 			struct commit *parent; | ||||||
|  |  | ||||||
|  |  | ||||||
|  | @ -905,9 +905,7 @@ static void export_marks(char *file) | ||||||
| static void import_marks(char *input_file) | static void import_marks(char *input_file) | ||||||
| { | { | ||||||
| 	char line[512]; | 	char line[512]; | ||||||
| 	FILE *f = fopen(input_file, "r"); | 	FILE *f = xfopen(input_file, "r"); | ||||||
| 	if (!f) |  | ||||||
| 		die_errno("cannot read '%s'", input_file); |  | ||||||
|  |  | ||||||
| 	while (fgets(line, sizeof(line), f)) { | 	while (fgets(line, sizeof(line), f)) { | ||||||
| 		uint32_t mark; | 		uint32_t mark; | ||||||
|  |  | ||||||
|  | @ -280,8 +280,7 @@ static void check_unreachable_object(struct object *obj) | ||||||
| 				free(filename); | 				free(filename); | ||||||
| 				return; | 				return; | ||||||
| 			} | 			} | ||||||
| 			if (!(f = fopen(filename, "w"))) | 			f = xfopen(filename, "w"); | ||||||
| 				die_errno("Could not open '%s'", filename); |  | ||||||
| 			if (obj->type == OBJ_BLOB) { | 			if (obj->type == OBJ_BLOB) { | ||||||
| 				if (stream_blob_to_fd(fileno(f), &obj->oid, NULL, 1)) | 				if (stream_blob_to_fd(fileno(f), &obj->oid, NULL, 1)) | ||||||
| 					die_errno("Could not write '%s'", filename); | 					die_errno("Could not write '%s'", filename); | ||||||
|  |  | ||||||
|  | @ -839,9 +839,7 @@ static int suggest_conflicts(void) | ||||||
| 	struct strbuf msgbuf = STRBUF_INIT; | 	struct strbuf msgbuf = STRBUF_INIT; | ||||||
|  |  | ||||||
| 	filename = git_path_merge_msg(); | 	filename = git_path_merge_msg(); | ||||||
| 	fp = fopen(filename, "a"); | 	fp = xfopen(filename, "a"); | ||||||
| 	if (!fp) |  | ||||||
| 		die_errno(_("Could not open '%s' for writing"), filename); |  | ||||||
|  |  | ||||||
| 	append_conflicts_hint(&msgbuf); | 	append_conflicts_hint(&msgbuf); | ||||||
| 	fputs(msgbuf.buf, fp); | 	fputs(msgbuf.buf, fp); | ||||||
|  |  | ||||||
|  | @ -337,8 +337,7 @@ static void get_merge_heads(struct oid_array *merge_heads) | ||||||
| 	struct strbuf sb = STRBUF_INIT; | 	struct strbuf sb = STRBUF_INIT; | ||||||
| 	struct object_id oid; | 	struct object_id oid; | ||||||
|  |  | ||||||
| 	if (!(fp = fopen(filename, "r"))) | 	fp = xfopen(filename, "r"); | ||||||
| 		die_errno(_("could not open '%s' for reading"), filename); |  | ||||||
| 	while (strbuf_getline_lf(&sb, fp) != EOF) { | 	while (strbuf_getline_lf(&sb, fp) != EOF) { | ||||||
| 		if (get_oid_hex(sb.buf, &oid)) | 		if (get_oid_hex(sb.buf, &oid)) | ||||||
| 			continue;  /* invalid line: does not start with SHA1 */ | 			continue;  /* invalid line: does not start with SHA1 */ | ||||||
|  |  | ||||||
							
								
								
									
										8
									
								
								diff.c
								
								
								
								
							
							
						
						
									
										8
									
								
								diff.c
								
								
								
								
							|  | @ -4071,9 +4071,7 @@ int diff_opt_parse(struct diff_options *options, | ||||||
| 		DIFF_OPT_CLR(options, FUNCCONTEXT); | 		DIFF_OPT_CLR(options, FUNCCONTEXT); | ||||||
| 	else if ((argcount = parse_long_opt("output", av, &optarg))) { | 	else if ((argcount = parse_long_opt("output", av, &optarg))) { | ||||||
| 		char *path = prefix_filename(prefix, optarg); | 		char *path = prefix_filename(prefix, optarg); | ||||||
| 		options->file = fopen(path, "w"); | 		options->file = xfopen(path, "w"); | ||||||
| 		if (!options->file) |  | ||||||
| 			die_errno("Could not open '%s'", path); |  | ||||||
| 		options->close_file = 1; | 		options->close_file = 1; | ||||||
| 		if (options->use_color != GIT_COLOR_ALWAYS) | 		if (options->use_color != GIT_COLOR_ALWAYS) | ||||||
| 			options->use_color = GIT_COLOR_NEVER; | 			options->use_color = GIT_COLOR_NEVER; | ||||||
|  | @ -4807,9 +4805,7 @@ void diff_flush(struct diff_options *options) | ||||||
| 		 */ | 		 */ | ||||||
| 		if (options->close_file) | 		if (options->close_file) | ||||||
| 			fclose(options->file); | 			fclose(options->file); | ||||||
| 		options->file = fopen("/dev/null", "w"); | 		options->file = xfopen("/dev/null", "w"); | ||||||
| 		if (!options->file) |  | ||||||
| 			die_errno("Could not open /dev/null"); |  | ||||||
| 		options->close_file = 1; | 		options->close_file = 1; | ||||||
| 		for (i = 0; i < q->nr; i++) { | 		for (i = 0; i < q->nr; i++) { | ||||||
| 			struct diff_filepair *p = q->queue[i]; | 			struct diff_filepair *p = q->queue[i]; | ||||||
|  |  | ||||||
|  | @ -3274,9 +3274,7 @@ static void option_export_pack_edges(const char *edges) | ||||||
| { | { | ||||||
| 	if (pack_edges) | 	if (pack_edges) | ||||||
| 		fclose(pack_edges); | 		fclose(pack_edges); | ||||||
| 	pack_edges = fopen(edges, "a"); | 	pack_edges = xfopen(edges, "a"); | ||||||
| 	if (!pack_edges) |  | ||||||
| 		die_errno("Cannot open '%s'", edges); |  | ||||||
| } | } | ||||||
|  |  | ||||||
| static int parse_one_option(const char *option) | static int parse_one_option(const char *option) | ||||||
|  |  | ||||||
|  | @ -124,10 +124,8 @@ static int note2mark_cb(const unsigned char *object_sha1, | ||||||
| static void regenerate_marks(void) | static void regenerate_marks(void) | ||||||
| { | { | ||||||
| 	int ret; | 	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); | 	ret = for_each_note(NULL, 0, note2mark_cb, marksfile); | ||||||
| 	if (ret) | 	if (ret) | ||||||
| 		die("Regeneration of marks failed, returned %d.", 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"); | 	marksfile = fopen(marksfilename, "r"); | ||||||
| 	if (!marksfile) { | 	if (!marksfile) { | ||||||
| 		regenerate_marks(); | 		regenerate_marks(); | ||||||
| 		marksfile = fopen(marksfilename, "r"); | 		marksfile = xfopen(marksfilename, "r"); | ||||||
| 		if (!marksfile) |  | ||||||
| 			die_errno("cannot read marks file %s!", marksfilename); |  | ||||||
| 		fclose(marksfile); | 		fclose(marksfile); | ||||||
| 	} else { | 	} else { | ||||||
| 		strbuf_addf(&sb, ":%d ", latestrev); | 		strbuf_addf(&sb, ":%d ", latestrev); | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue
	
	 Nguyễn Thái Ngọc Duy
						Nguyễn Thái Ngọc Duy