{"thread":{"id":"2972","subject":"use result of open(2) to check for presence","startedAt":"2006-01-05T11:43:34Z","lastAt":"2006-01-05T18:07:15Z","messageCount":5,"participants":["Alex Riesen","Morten Welinder"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"14174","messageId":"81b0412b0601050343w4c00ac86y30569c3babbf6728@mail.gmail.com","threadId":"2972","inReplyTo":null,"subject":"use result of open(2) to check for presence","fromName":"Alex Riesen","fromEmail":"raa.lkml@gmail.com","sentAt":"2006-01-05T11:43:34Z","receivedAt":"2006-01-05T11:43:34Z","isPatch":false,"sender":{"key":"raa.lkml@gmail.com","avatar":"https://avatars.githubusercontent.com/u/324101?v=4"},"body":"Not that the stat against open race would matter much in this context,\nbut that simplifies\nthe code a bit. Also some diagnostics added (why the open failed)\n\nSigned-off-by: Alex Riesen <raa.lkml@gmail.com>\n\n\nSubject: [PATCH] use result of open(2) to check for presence\n\nof the old config file\n\nSigned-off-by: Alex Riesen <raa.lkml@gmail.com>\n\n\n---\n\n config.c |   17 ++++++++++++-----\n 1 files changed, 12 insertions(+), 5 deletions(-)\n\nb74ea5a71504808112bd4781a69b17f6009104db\ndiff --git a/config.c b/config.c\nindex 992e988..8355224 100644\n--- a/config.c\n+++ b/config.c\n@@ -409,8 +409,7 @@ int git_config_set_multivar(const char* \n \tconst char* value_regex, int multi_replace)\n {\n \tint i;\n-\tstruct stat st;\n-\tint fd;\n+\tint fd, in_fd;\n \tchar* config_filename = strdup(git_path(\"config\"));\n \tchar* lock_file = strdup(git_path(\"config.lock\"));\n \tconst char* last_dot = strrchr(key, '.');\n@@ -457,9 +456,17 @@ int git_config_set_multivar(const char* \n \t/*\n \t * If .git/config does not exist yet, write a minimal version.\n \t */\n-\tif (stat(config_filename, &st)) {\n+\tin_fd = open(config_filename, O_RDONLY);\n+\tif ( in_fd < 0 ) {\n \t\tfree(store.key);\n \n+\t\tif ( ENOENT != errno ) {\n+\t\t\terror(\"opening %s: %s\", config_filename,\n+\t\t\t      strerror(errno));\n+\t\t\tclose(fd);\n+\t\t\tunlink(lock_file);\n+\t\t\treturn 3; /* same as \"invalid config file\" */\n+\t\t}\n \t\t/* if nothing to unset, error out */\n \t\tif (value == NULL) {\n \t\t\tclose(fd);\n@@ -471,7 +478,7 @@ int git_config_set_multivar(const char* \n \t\tstore_write_section(fd, key);\n \t\tstore_write_pair(fd, key, value);\n \t} else{\n-\t\tint in_fd;\n+\t\tstruct stat st;\n \t\tchar* contents;\n \t\tint i, copy_begin, copy_end, new_line = 0;\n \n@@ -528,7 +535,7 @@ int git_config_set_multivar(const char* \n \t\t\treturn 5;\n \t\t}\n \n-\t\tin_fd = open(config_filename, O_RDONLY, 0666);\n+\t\tfstat(in_fd, &st);\n \t\tcontents = mmap(NULL, st.st_size, PROT_READ,\n \t\t\tMAP_PRIVATE, in_fd, 0);\n \t\tclose(in_fd);\n-- \n1.0.GIT\n\n\n\n"},{"id":"14185","messageId":"118833cc0601050648t17f68bcch8fd7c541585e965c@mail.gmail.com","threadId":"2972","inReplyTo":"81b0412b0601050343w4c00ac86y30569c3babbf6728@mail.gmail.com","subject":"Re: use result of open(2) to check for presence","fromName":"Morten Welinder","fromEmail":"mwelinder@gmail.com","sentAt":"2006-01-05T14:48:16Z","receivedAt":"2006-01-05T14:48:16Z","isPatch":false,"sender":{"key":"mwelinder@gmail.com","avatar":null},"body":"Neat, but perhaps you should check the fstat return code.\nIt actually can fail, for example if the filesize cannot be represented in the\nstructure because it is too big.\n\nMorten\n"},{"id":"14186","messageId":"81b0412b0601050712q4c2123f7i8e9190c3b75b6fda@mail.gmail.com","threadId":"2972","inReplyTo":"118833cc0601050648t17f68bcch8fd7c541585e965c@mail.gmail.com","subject":"Re: use result of open(2) to check for presence","fromName":"Alex Riesen","fromEmail":"raa.lkml@gmail.com","sentAt":"2006-01-05T15:12:22Z","receivedAt":"2006-01-05T15:12:22Z","isPatch":false,"sender":{"key":"raa.lkml@gmail.com","avatar":"https://avatars.githubusercontent.com/u/324101?v=4"},"body":"On 1/5/06, Morten Welinder <mwelinder@gmail.com> wrote:\n> Neat, but perhaps you should check the fstat return code.\n> It actually can fail, for example if the filesize cannot be represented in the\n> structure because it is too big.\n\nYes, of course. It will be added along with the missing test for mmap\nreturning MAP_FAILED. Sometime.\n\nSarcasm aside, how do you get such a situation?\n"},{"id":"14191","messageId":"118833cc0601050845i40f93bbajdd70e77953d07014@mail.gmail.com","threadId":"2972","inReplyTo":"81b0412b0601050712q4c2123f7i8e9190c3b75b6fda@mail.gmail.com","subject":"Re: use result of open(2) to check for presence","fromName":"Morten Welinder","fromEmail":"mwelinder@gmail.com","sentAt":"2006-01-05T16:45:27Z","receivedAt":"2006-01-05T16:45:27Z","isPatch":false,"sender":{"key":"mwelinder@gmail.com","avatar":null},"body":"Config files always tend to grow, but I do agree that 4GB config files stretches\ncredibility a bit.\n\nHowever, empty ones are not unlikely and then mmap will fail.\n\nM.\n"},{"id":"14197","messageId":"20060105180715.GA6112@steel.home","threadId":"2972","inReplyTo":"118833cc0601050845i40f93bbajdd70e77953d07014@mail.gmail.com","subject":"Re: use result of open(2) to check for presence","fromName":"Alex Riesen","fromEmail":"raa.lkml@gmail.com","sentAt":"2006-01-05T18:07:15Z","receivedAt":"2006-01-05T18:07:15Z","isPatch":false,"sender":{"key":"raa.lkml@gmail.com","avatar":"https://avatars.githubusercontent.com/u/324101?v=4"},"body":"Morten Welinder, Thu, Jan 05, 2006 17:45:27 +0100:\n> Config files always tend to grow, but I do agree that 4GB config files stretches\n> credibility a bit.\n> \n> However, empty ones are not unlikely and then mmap will fail.\n> \n\nShouldn't be a problem: the code in question does not seem to do\nanything for empty files and wont even touch the result of mmap.\n"}]}