@matheusbsbs / matheus-bagliano / commits / 88fb958baa
Fork 0

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 <raa.lkml@gmail.com> Signed-off-by: Junio C Hamano <junkio@cox.net>

Alex Riesen committed Jan 5, 2006 at 12:43 UTC 88fb958baa1066af23348c44a78b812be2344088
1 file changed +12 -5
config.c
+12 -5
@@ -409,8 +409,7 @@ int git_config_set_multivar(const char* key, const char* value,
409 const char* value_regex, int multi_replace)
410 {
411 int i;
412 - struct stat st;
413 - int fd;
412 + int fd, in_fd;
413 char* config_filename = strdup(git_path("config"));
414 char* lock_file = strdup(git_path("config.lock"));
415 const char* last_dot = strrchr(key, '.');
@@ -457,9 +456,17 @@ int git_config_set_multivar(const char* key, const char* value,
456 /*
457 * If .git/config does not exist yet, write a minimal version.
458 */
460 - if (stat(config_filename, &st)) {
459 + in_fd = open(config_filename, O_RDONLY);
460 + if ( in_fd < 0 ) {
461 free(store.key);
462
463 + if ( ENOENT != errno ) {
464 + error("opening %s: %s", config_filename,
465 + strerror(errno));
466 + close(fd);
467 + unlink(lock_file);
468 + return 3; /* same as "invalid config file" */
469 + }
470 /* if nothing to unset, error out */
471 if (value == NULL) {
472 close(fd);
@@ -471,7 +478,7 @@ int git_config_set_multivar(const char* key, const char* value,
478 store_write_section(fd, key);
479 store_write_pair(fd, key, value);
480 } else{
474 - int in_fd;
481 + struct stat st;
482 char* contents;
483 int i, copy_begin, copy_end, new_line = 0;
484
@@ -528,7 +535,7 @@ int git_config_set_multivar(const char* key, const char* value,
535 return 5;
536 }
537
531 - in_fd = open(config_filename, O_RDONLY, 0666);
538 + fstat(in_fd, &st);
539 contents = mmap(NULL, st.st_size, PROT_READ,
540 MAP_PRIVATE, in_fd, 0);
541 close(in_fd);