git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 3/8] init-db.c: refactor directory creation

From
ZWZach Welch <zw@superlucidity.net>
Date
Apr 19, 2005, 09:09 UTC
Message-ID
<mailbox-23311-1113901779-726360@spoon>

This patch factors the init-db directory creation into a new function, which is then reused in the next patch.

This patch applies on top of:
        [PATCH 0/8] init-db.c cleanup, add INDEX_FILE_DIRECTORY support
        [PATCH 1/8] init-db.c: [RESEND] remove redundant getenv call
        [PATCH 2/8] init-db.c: [RESEND] make init-db work with common objects
 init-db.c |   61 ++++++++++++++++++++++++++++-----------------------
 1 files changed, 34 insertions(+), 27 deletions(-)
Signed-Off-By: Zach Welch <zw@superlucidity.net>
--- a/init-db.c
+++ b/init-db.c
@@ -4,43 +4,50 @@
  * Copyright (C) Linus Torvalds, 2005
  */
 #include "cache.h"
+/*
+ * If you want to, you can share the DB area with any number of branches.
+ * That has advantages: you can save space by sharing all the SHA1 objects.
+ * On the other hand, it might just make lookup slower and messier. You
+ * be the judge.  The default case is to have a DB per managed directory.
+ */
+
+static char* init_dir(char *env, char *std, char *label, int *len)
+{
+	char *dir;
+	dir = getenv(env);
+	if (dir) {
+		struct stat st;
+		if (stat(dir, &st) < 0 || !S_ISDIR(st.st_mode)) {
+			fprintf(stderr, "%s set to bad directory %s: ", env, dir);
+			exit(1);
+		}
+	} 
+	else {
+		dir = std;
+		fprintf(stderr, "defaulting to private %s area\n", label);
+	}
+	if (mkdir(dir, 0755) < 0) {
+		if (errno != EEXIST) {
+			perror(dir);
+			exit(1);
+		}
+	}
+	if (len)
+		*len = strlen(dir);
+	return dir;
+}
 
 int main(int argc, char **argv)
 {
	char *sha1_dir, *path;
 	int len, i;
 
 	if (mkdir(".git", 0755) < 0) {
 		perror("unable to create .git directory");
 		exit(1);
 	}
-
-	/*
-	 * If you want to, you can share the DB area with any number of branches.
-	 * That has advantages: you can save space by sharing all the SHA1 objects.
-	 * On the other hand, it might just make lookup slower and messier. You
-	 * be the judge.
-	 */
-	sha1_dir = getenv(DB_ENVIRONMENT);
-	if (sha1_dir) {
-		struct stat st;
-		if (!stat(sha1_dir, &st) && S_ISDIR(st.st_mode))
-			return 0;
-		fprintf(stderr, "DB_ENVIRONMENT set to bad directory %s: ", sha1_dir);
-	}
-
-	/*
-	 * The default case is to have a DB per managed directory.
-	 */
-	sha1_dir = DEFAULT_DB_ENVIRONMENT;
-	fprintf(stderr, "defaulting to private storage area\n");
-	len = strlen(sha1_dir);
-	if (mkdir(sha1_dir, 0755) < 0) {
-		if (errno != EEXIST) {
-			perror(sha1_dir);
-			exit(1);
-		}
-	}
+	sha1_dir = init_dir(DB_ENVIRONMENT, DEFAULT_DB_ENVIRONMENT, "storage", &len);
+	
 	path = malloc(len + 40);
 	memcpy(path, sha1_dir, len);
 	for (i = 0; i < 256; i++) {
Message 1 of 1 in “init-db.c: refactor directory creation”
  1. 3/8 init-db.c: refactor directory creationZach Welch, Apr 19, 2005

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.