2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
8 static void create_directories(const char *path)
10 int len = strlen(path);
11 char *buf = malloc(len + 1);
12 const char *slash = path;
14 while ((slash = strchr(slash+1, '/')) != NULL) {
16 memcpy(buf, path, len);
22 static int create_file(const char *path)
24 int fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, 0600);
26 if (errno == ENOENT) {
27 create_directories(path);
28 fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, 0600);
34 static int unpack(unsigned char *sha1)
40 buffer = read_sha1_file(sha1, type, &size);
42 usage("unable to read sha1 file");
43 if (strcmp(type, "tree"))
44 usage("expected a 'tree' node");
46 int len = strlen(buffer)+1;
47 unsigned char *sha1 = buffer + len;
48 char *path = strchr(buffer, ' ')+1;
50 unsigned long filesize;
54 if (size < len + 20 || sscanf(buffer, "%o", &mode) != 1)
55 usage("corrupt 'tree' file");
58 data = read_sha1_file(sha1, type, &filesize);
59 if (!data || strcmp(type, "blob"))
60 usage("tree file refers to bad file data");
61 fd = create_file(path);
63 usage("unable to create file");
64 if (write(fd, data, filesize) != filesize)
65 usage("unable to write file");
73 int main(int argc, char **argv)
75 unsigned char sha1[20];
78 usage("read-tree <key>");
79 if (get_sha1_hex(argv[1], sha1) < 0)
80 usage("read-tree <key>");
81 sha1_file_directory = getenv(DB_ENVIRONMENT);
82 if (!sha1_file_directory)
83 sha1_file_directory = DEFAULT_DB_ENVIRONMENT;
85 usage("unpack failed");