{"thread":{"id":"131","subject":"[PATCH 3/8] init-db.c: refactor directory creation","startedAt":"2005-04-19T09:09:39Z","lastAt":"2005-04-19T09:09:39Z","messageCount":1,"participants":["Zach Welch"],"isPatch":true,"patchVersion":1,"patchTotal":8},"messages":[{"id":"799","messageId":"mailbox-23311-1113901779-726360@spoon","threadId":"131","inReplyTo":null,"subject":"[PATCH 3/8] init-db.c: refactor directory creation","fromName":"Zach Welch","fromEmail":"zw@superlucidity.net","sentAt":"2005-04-19T09:09:39Z","receivedAt":"2005-04-19T09:09:39Z","isPatch":true,"sender":{"key":"zw@superlucidity.net","avatar":null},"body":"This patch factors the init-db directory creation into a new function,\nwhich is then reused in the next patch.  \n\nThis patch applies on top of:\n        [PATCH 0/8] init-db.c cleanup, add INDEX_FILE_DIRECTORY support\n        [PATCH 1/8] init-db.c: [RESEND] remove redundant getenv call\n        [PATCH 2/8] init-db.c: [RESEND] make init-db work with common objects\n init-db.c |   61 ++++++++++++++++++++++++++++-----------------------\n 1 files changed, 34 insertions(+), 27 deletions(-)\nSigned-Off-By: Zach Welch <zw@superlucidity.net>\n\n\n--- a/init-db.c\n+++ b/init-db.c\n@@ -4,43 +4,50 @@\n  * Copyright (C) Linus Torvalds, 2005\n  */\n #include \"cache.h\"\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+ * On the other hand, it might just make lookup slower and messier. You\n+ * be the judge.  The default case is to have a DB per managed directory.\n+ */\n+\n+static char* init_dir(char *env, char *std, char *label, int *len)\n+{\n+\tchar *dir;\n+\tdir = getenv(env);\n+\tif (dir) {\n+\t\tstruct stat st;\n+\t\tif (stat(dir, &st) < 0 || !S_ISDIR(st.st_mode)) {\n+\t\t\tfprintf(stderr, \"%s set to bad directory %s: \", env, dir);\n+\t\t\texit(1);\n+\t\t}\n+\t} \n+\telse {\n+\t\tdir = std;\n+\t\tfprintf(stderr, \"defaulting to private %s area\\n\", label);\n+\t}\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+\tif (len)\n+\t\t*len = strlen(dir);\n+\treturn dir;\n+}\n \n int main(int argc, char **argv)\n {\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-\n-\t/*\n-\t * If you want to, you can share the DB area with any number of branches.\n-\t * That has advantages: you can save space by sharing all the SHA1 objects.\n-\t * On the other hand, it might just make lookup slower and messier. You\n-\t * be the judge.\n-\t */\n-\tsha1_dir = getenv(DB_ENVIRONMENT);\n-\tif (sha1_dir) {\n-\t\tstruct stat st;\n-\t\tif (!stat(sha1_dir, &st) && S_ISDIR(st.st_mode))\n-\t\t\treturn 0;\n-\t\tfprintf(stderr, \"DB_ENVIRONMENT set to bad directory %s: \", sha1_dir);\n-\t}\n-\n-\t/*\n-\t * The default case is to have a DB per managed directory.\n-\t */\n-\tsha1_dir = DEFAULT_DB_ENVIRONMENT;\n-\tfprintf(stderr, \"defaulting to private storage area\\n\");\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+\tsha1_dir = init_dir(DB_ENVIRONMENT, DEFAULT_DB_ENVIRONMENT, \"storage\", &len);\n+\t\n \tpath = malloc(len + 40);\n \tmemcpy(path, sha1_dir, len);\n \tfor (i = 0; i < 256; i++) {\n"}]}