{"thread":{"id":"3280","subject":"[PATCH] stat() for existence in safe_create_leading_directories()","startedAt":"2006-02-10T01:56:13Z","lastAt":"2006-02-10T02:30:36Z","messageCount":2,"participants":["Jason Riedy","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"15834","messageId":"15788.1139536573@lotus.CS.Berkeley.EDU","threadId":"3280","inReplyTo":null,"subject":"[PATCH] stat() for existence in safe_create_leading_directories()","fromName":"Jason Riedy","fromEmail":"ejr@eecs.berkeley.edu","sentAt":"2006-02-10T01:56:13Z","receivedAt":"2006-02-10T01:56:13Z","isPatch":true,"sender":{"key":"ejr@eecs.berkeley.edu","avatar":"https://gravatar.com/avatar/547fa56f887cab01599edab4e9f813c949c1269e02714f20e0496c56185d9837?d=mp&s=160"},"body":"Use stat() to explicitly check for existence rather than\nrelying on the non-portable EEXIST error in sha1_file.c's\nsafe_create_leading_directories().  There certainly are\noptimizations possible, but then the code becomes almost\nthe same as that in coreutil's lib/mkdir-p.c.\n\nOther uses of EEXIST seem ok.  Tested on Solaris 8, AIX 5.2L,\nand a few Linux versions.  AIX has some unrelated (I think)\nfailures right now; I haven't tried many recent gits there.\nAnyone have an old Ultrix box to break everything?  ;)\n\nAlso remove extraneous #includes.  Everything's already in\ngit-compat-util.h, included through cache.h.\n\nSigned-off-by: Jason Riedy <ejr@cs.berkeley.edu>\n\n---\n\n sha1_file.c |   15 ++++++++++-----\n 1 files changed, 10 insertions(+), 5 deletions(-)\n\n597de7026f284d7bb5ea5b929a70b3cf3077e4e7\ndiff --git a/sha1_file.c b/sha1_file.c\nindex 20f6419..3d11a9b 100644\n--- a/sha1_file.c\n+++ b/sha1_file.c\n@@ -6,8 +6,6 @@\n  * This handles basic git sha1 object files - packing, unpacking,\n  * creation etc.\n  */\n-#include <sys/types.h>\n-#include <dirent.h>\n #include \"cache.h\"\n #include \"delta.h\"\n #include \"pack.h\"\n@@ -74,6 +72,8 @@ int adjust_shared_perm(const char *path)\n int safe_create_leading_directories(char *path)\n {\n \tchar *pos = path;\n+\tstruct stat st;\n+\n \tif (*pos == '/')\n \t\tpos++;\n \n@@ -82,12 +82,17 @@ int safe_create_leading_directories(char\n \t\tif (!pos)\n \t\t\tbreak;\n \t\t*pos = 0;\n-\t\tif (mkdir(path, 0777) < 0) {\n-\t\t\tif (errno != EEXIST) {\n+\t\tif (!stat(path, &st)) {\n+\t\t\t/* path exists */\n+\t\t\tif (!S_ISDIR(st.st_mode)) {\n \t\t\t\t*pos = '/';\n-\t\t\t\treturn -1;\n+\t\t\t\treturn -3;\n \t\t\t}\n \t\t}\n+\t\telse if (mkdir(path, 0777)) {\n+\t\t\t*pos = '/';\n+\t\t\treturn -1;\n+\t\t}\n \t\telse if (adjust_shared_perm(path)) {\n \t\t\t*pos = '/';\n \t\t\treturn -2;\n-- \n1.1.5\n"},{"id":"15836","messageId":"7v7j840wtv.fsf@assigned-by-dhcp.cox.net","threadId":"3280","inReplyTo":"15788.1139536573@lotus.CS.Berkeley.EDU","subject":"Re: [PATCH] stat() for existence in safe_create_leading_directories()","fromName":"Junio C Hamano","fromEmail":"junkio@cox.net","sentAt":"2006-02-10T02:30:36Z","receivedAt":"2006-02-10T02:30:36Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Thanks.\n\nBTW, in an earlier message when I said this:\n\nJason Riedy <ejr@EECS.Berkeley.EDU> writes:\n\n> And Junio C Hamano writes:\n>\n>  - Somehow I started to trust your ability to code portably a lot\n>  - better than I trust myself, [...]\n>\n> eep.  ;)  I only have access to three major variations at the\n> moment (Linux, semi-old Solaris, recent AIX), so I'm not an\n> authority...  Solaris 8 is from before the general Linux/glibc \n> compatability movement, so it's pretty useful for testing.\n\nI was talking more about competence rather than access to\nvarious environments to test on.  I forgot to mention that.\n"}]}