Anonymous committed 88fb958

use result of open(2) to check for presence

Not that the stat against open race would matter much in this context,
but that simplifies
the code a bit. Also some diagnostics added (why the open failed)

Signed-off-by: Alex Riesen <>
Signed-off-by: Junio C Hamano <>

Comments (0)

Files changed (1)

 	const char* value_regex, int multi_replace)
 	int i;
-	struct stat st;
-	int fd;
+	int fd, in_fd;
 	char* config_filename = strdup(git_path("config"));
 	char* lock_file = strdup(git_path("config.lock"));
 	const char* last_dot = strrchr(key, '.');
 	 * If .git/config does not exist yet, write a minimal version.
-	if (stat(config_filename, &st)) {
+	in_fd = open(config_filename, O_RDONLY);
+	if ( in_fd < 0 ) {
+		if ( ENOENT != errno ) {
+			error("opening %s: %s", config_filename,
+			      strerror(errno));
+			close(fd);
+			unlink(lock_file);
+			return 3; /* same as "invalid config file" */
+		}
 		/* if nothing to unset, error out */
 		if (value == NULL) {
 		store_write_section(fd, key);
 		store_write_pair(fd, key, value);
 	} else{
-		int in_fd;
+		struct stat st;
 		char* contents;
 		int i, copy_begin, copy_end, new_line = 0;
 			return 5;
-		in_fd = open(config_filename, O_RDONLY, 0666);
+		fstat(in_fd, &st);
 		contents = mmap(NULL, st.st_size, PROT_READ,
 			MAP_PRIVATE, in_fd, 0);
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.