1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <sys/stat.h>
5 #include <fcntl.h>
6 #include <errno.h>
7
8 #define DB_ENVIRONMENT "SHA1_FILE_DIRECTOY"
9 #define DEFAULT_DB_ENVIRONMENT ".dircache/objects"
10
11 int main(int argc, char **argv)
12 {
13 char *sha1_dir = getenv(DB_ENVIRONMENT), *path;
14 int len, i, fd;
15
16 if (mkdir(".dircache", 0700) < 0) {
17 perror("unable to create .dircache");
18 exit(1);
19 }
20
21 sha1_dir = getenv(DB_ENVIRONMENT);
22 if (sha1_dir) {
23 struct stat st;
24 if (!stat(sha1_dir, &st) < 0 && S_ISDIR(st.st_mode))
25 return 0;
26 fprintf(stderr, "DB_ENVIRONMENT set to bad directory %s: ", sha1_dir);
27 }
28
29 sha1_dir = DEFAULT_DB_ENVIRONMENT;
30 fprintf(stderr, "defaulting to private storage area\n");
31 len = strlen(sha1_dir);
32 if (mkdir(sha1_dir, 0700) < 0) {
33 if (errno != EEXIST) {
34 perror(sha1_dir);
35 exit(1);
36 }
37 }
38
39 path = malloc(len + 40);
40 memcpy(path, sha1_dir, len);
41 for (i = 0; i < 256; i++) {
42 sprintf(path+len, "/%02x", i);
43 if (mkdir(path, 0700) < 0) {
44 if (errno != EEXIST) {
45 perror(path);
46 exit(1);
47 }
48 }
49 }
50 free(path);
51
52 return 0;
53 }