设为首页 加入收藏

TOP

git 源码学习(init-db) 提交版本号 083c516331(二)
2019-03-23 16:08:09 】 浏览:274
Tags:git 源码 学习 init-db 提交 版本 083c516331
a1_dir = getenv(DB_ENVIRONMENT); 24 if (sha1_dir) { 25 struct stat st; 26 if (!stat(sha1_dir, &st) < 0 && S_ISDIR(st.st_mode)) 27 return 0; 28 fprintf(stderr, "DB_ENVIRONMENT set to bad directory %s: ", sha1_dir); 29 } 30 31 sha1_dir = DEFAULT_DB_ENVIRONMENT; 32 fprintf(stderr, "defaulting to private storage area\n"); 33 len = strlen(sha1_dir); 34 if (mkdir(sha1_dir, 0700) < 0) { 35 if (errno != EEXIST) { 36 perror(sha1_dir); 37 exit(1); 38 } 39 } 40 41 return 0; 42 }

 

最后一个逻辑块是在objects目录下生成256个文件,文件名称是从00-ff。这些文件用来保存sha1值,即commit号,比如commit号为083c516331,会保存在08目录中内容为3c516331

 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 }

 

至此init-db.c分析完毕,谢谢阅读,如有不足之处,欢迎指出邮箱BruceContact@163.com

 

首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇C语言中的面向对象思想! 下一篇C语言数据结构基础学习笔记——C..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目