{"thread":{"id":"157","subject":"[PATCH 3/3] init-db.c: create and use safe_create_dir helper","startedAt":"2005-04-20T04:28:39Z","lastAt":"2005-04-20T04:28:39Z","messageCount":1,"participants":["Zach Welch"],"isPatch":true,"patchVersion":1,"patchTotal":3},"messages":[{"id":"932","messageId":"mailbox-1242-1113971319-974200@spoon","threadId":"157","inReplyTo":null,"subject":"[PATCH 3/3] init-db.c: create and use safe_create_dir helper","fromName":"Zach Welch","fromEmail":"zw@superlucidity.net","sentAt":"2005-04-20T04:28:39Z","receivedAt":"2005-04-20T04:28:39Z","isPatch":true,"sender":{"key":"zw@superlucidity.net","avatar":null},"body":"This patch applies on top of:\n        [PATCH 1/3] init-db.c: cleanup comments\n        [PATCH 2/3] init-db.c: normalize env var handling.\n\n init-db.c |   30 ++++++++++++++----------------\n 1 files changed, 14 insertions(+), 16 deletions(-)\n\nSigned-Off-By: Zach Welch <zw@superlucidity.net>\n\nFactor mkdir calls into common safe_create_dir subroutine.\n\n--- a/init-db.c\t2005-04-19 18:50:14.000000000 -0700\n+++ b/init-db.c\t2005-04-19 18:45:48.000000000 -0700\n@@ -5,6 +5,16 @@\n  */\n #include \"cache.h\"\n \n+void safe_create_dir(char *dir)\n+{\n+\tif (mkdir(dir, 0755) < 0) {\n+\t\tif (errno != EEXIST) {\n+\t\t\tperror(dir);\n+\t\t\texit(1);\n+\t\t}\n+\t}\n+}\n+\n /*\n  * If you want to, you can share the DB area with any number of branches.\n  * That has advantages: you can save space by sharing all the SHA1 objects.\n@@ -16,10 +26,7 @@\n \tchar *sha1_dir, *path;\n \tint len, i;\n \n-\tif (mkdir(\".git\", 0755) < 0) {\n-\t\tperror(\"unable to create .git directory\");\n-\t\texit(1);\n-\t}\n+\tsafe_create_dir(\".git\");\n \n \tsha1_dir = getenv(DB_ENVIRONMENT);\n \tif (!sha1_dir) {\n@@ -27,22 +34,13 @@\n \t\tfprintf(stderr, \"defaulting to local storage area\\n\");\n \t}\n \tlen = strlen(sha1_dir);\n-\tif (mkdir(sha1_dir, 0755) < 0) {\n-\t\tif (errno != EEXIST) {\n-\t\t\tperror(sha1_dir);\n-\t\t\texit(1);\n-\t\t}\n-\t}\n \tpath = malloc(len + 40);\n \tmemcpy(path, sha1_dir, len);\n+\n+\tsafe_create_dir(sha1_dir);\n \tfor (i = 0; i < 256; i++) {\n \t\tsprintf(path+len, \"/%02x\", i);\n-\t\tif (mkdir(path, 0755) < 0) {\n-\t\t\tif (errno != EEXIST) {\n-\t\t\t\tperror(path);\n-\t\t\t\texit(1);\n-\t\t\t}\n-\t\t}\n+\t\tsafe_create_dir(path);\n \t}\n \treturn 0;\n }\n"}]}