blob: d41b8acaa6d0954d848f2c626b382316e0d73909 [file] [log] [blame]
Koushik Dutta67700e72011-07-11 12:26:45 -07001#include <stdio.h>
2#include <sys/stat.h>
3#include <openssl/md5.h>
4#include <openssl/sha.h>
5#include <openssl/ripemd.h>
6#include <errno.h>
7#include <dirent.h>
8#include <limits.h>
9#include <stdlib.h>
10#include <fcntl.h>
11#include <limits.h>
12
Koushik Dutta4f1c5e32011-07-11 13:25:04 -070013typedef struct DEDUPE_STORE_CONTEXT {
Koushik Dutta67700e72011-07-11 12:26:45 -070014 char blob_dir[PATH_MAX];
15 FILE *output_manifest;
16};
17
18static void usage(char** argv) {
Koushik Dutta4f1c5e32011-07-11 13:25:04 -070019 fprintf(stderr, "usage: %s c input_directory blob_dir output_manifest\n", argv[0]);
Koushik Dutta67700e72011-07-11 12:26:45 -070020 fprintf(stderr, "usage: %s x input_manifest blob_dir output_directory\n", argv[0]);
21}
22
23static int copy_file(const char *dst, const char *src) {
24 char buf[4096];
25 int dstfd, srcfd, bytes_read, bytes_written, total_read = 0;
26 if (src == NULL)
27 return 1;
28 if (dst == NULL)
29 return 2;
30
31 srcfd = open(src, O_RDONLY);
32 if (srcfd < 0)
33 return 3;
34
35 dstfd = open(dst, O_RDWR | O_CREAT | O_TRUNC, 0600);
36 if (dstfd < 0) {
37 close(srcfd);
38 return 4;
39 }
40
41 do {
42 total_read += bytes_read = read(srcfd, buf, 4096);
43 if (!bytes_read)
44 break;
45 if (bytes_read < 4096)
46 memset(&buf[bytes_read], 0, 4096 - bytes_read);
47 if (write(dstfd, buf, 4096) < 4096)
48 return 5;
49 } while(bytes_read == 4096);
50
51 close(dstfd);
52 close(srcfd);
53
54 return 0;
55}
56
57static void do_md5sum(FILE *mfile, unsigned char *rptr) {
58 char rdata[BUFSIZ];
59 int rsize;
60 MD5_CTX c;
61
62 MD5_Init(&c);
63 while(!feof(mfile)) {
64 rsize = fread(rdata, sizeof(char), BUFSIZ, mfile);
65 if(rsize > 0) {
66 MD5_Update(&c, rdata, rsize);
67 }
68 }
69
70 MD5_Final(rptr, &c);
71}
72
73static int do_md5sum_file(const char* filename, unsigned char *rptr) {
74 FILE *f = fopen(filename, "rb");
75 if (f == NULL) {
76 fprintf(stderr, "Unable to open file: %s\n", filename);
77 return 1;
78 }
79 do_md5sum(f, rptr);
80 fclose(f);
81 return 0;
82}
83
Koushik Dutta4f1c5e32011-07-11 13:25:04 -070084static int store_st(struct DEDUPE_STORE_CONTEXT *context, struct stat st, const char* s);
Koushik Dutta67700e72011-07-11 12:26:45 -070085
Koushik Dutta4f1c5e32011-07-11 13:25:04 -070086void print_stat(struct DEDUPE_STORE_CONTEXT *context, char type, struct stat st, const char *f) {
Koushik Dutta67700e72011-07-11 12:26:45 -070087 fprintf(context->output_manifest, "%c\t%o\t%d\t%d\t%s\t", type, st.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO | S_ISUID | S_ISGID), st.st_uid, st.st_gid, f);
88}
89
Koushik Dutta4f1c5e32011-07-11 13:25:04 -070090static int store_file(struct DEDUPE_STORE_CONTEXT *context, struct stat st, const char* f) {
Koushik Dutta67700e72011-07-11 12:26:45 -070091 printf("%s\n", f);
92 unsigned char sumdata[SHA_DIGEST_LENGTH];
93 int ret;
94 if (ret = do_md5sum_file(f, sumdata)) {
95 fprintf(stderr, "Error calculating md5sum of %s\n", f);
96 return ret;
97 }
98 char psum[41];
99 int j;
100 for (j = 0; j < MD5_DIGEST_LENGTH; j++)
101 sprintf(&psum[(j*2)], "%02x", (int)sumdata[j]);
102 psum[(MD5_DIGEST_LENGTH * 2)] = '\0';
Koushik Dutta4f1c5e32011-07-11 13:25:04 -0700103
Koushik Dutta67700e72011-07-11 12:26:45 -0700104 char out_blob[PATH_MAX];
105 sprintf(out_blob, "%s/%s", context->blob_dir, psum);
106 if (ret = copy_file(out_blob, f)) {
107 fprintf(stderr, "Error copying blob %s\n", f);
108 return ret;
109 }
110
Koushik Dutta4f1c5e32011-07-11 13:25:04 -0700111 fprintf(context->output_manifest, "%s\t\n", psum);
Koushik Dutta67700e72011-07-11 12:26:45 -0700112 return 0;
113}
114
Koushik Dutta4f1c5e32011-07-11 13:25:04 -0700115static int store_dir(struct DEDUPE_STORE_CONTEXT *context, struct stat st, const char* d) {
Koushik Dutta67700e72011-07-11 12:26:45 -0700116 printf("%s\n", d);
117 DIR *dp = opendir(d);
118 if (d == NULL) {
119 fprintf(stderr, "Error opening directory: %s\n", d);
120 return 1;
121 }
122 struct dirent *ep;
123 char full_path[PATH_MAX];
124 while (ep = readdir(dp)) {
125 if (strcmp(ep->d_name, ".") == 0)
126 continue;
127 if (strcmp(ep->d_name, "..") == 0)
128 continue;
129 struct stat cst;
130 int ret;
131 sprintf(full_path, "%s/%s", d, ep->d_name);
132 if (0 != (ret = lstat(full_path, &cst))) {
133 fprintf(stderr, "Error opening: %s\n", ep->d_name);
134 closedir(dp);
135 return ret;
136 }
137
Koushik Duttae8bdefd2011-07-11 14:13:43 -0700138 if (ret = store_st(context, cst, full_path))
Koushik Dutta67700e72011-07-11 12:26:45 -0700139 return ret;
140 }
141 closedir(dp);
142 return 0;
143}
144
Koushik Dutta4f1c5e32011-07-11 13:25:04 -0700145static int store_link(struct DEDUPE_STORE_CONTEXT *context, struct stat st, const char* l) {
Koushik Dutta67700e72011-07-11 12:26:45 -0700146 printf("%s\n", l);
147 char link[PATH_MAX];
148 int ret = readlink(l, link, PATH_MAX);
149 if (ret < 0) {
150 fprintf(stderr, "Error reading symlink\n");
151 return errno;
152 }
Koushik Duttae8bdefd2011-07-11 14:13:43 -0700153 link[ret] = '\0';
Koushik Dutta4f1c5e32011-07-11 13:25:04 -0700154 fprintf(context->output_manifest, "%s\t\n", link);
Koushik Dutta67700e72011-07-11 12:26:45 -0700155 return 0;
156}
157
Koushik Dutta4f1c5e32011-07-11 13:25:04 -0700158static int store_st(struct DEDUPE_STORE_CONTEXT *context, struct stat st, const char* s) {
Koushik Dutta67700e72011-07-11 12:26:45 -0700159 if (S_ISREG(st.st_mode)) {
160 print_stat(context, 'f', st, s);
161 return store_file(context, st, s);
162 }
163 else if (S_ISDIR(st.st_mode)) {
164 print_stat(context, 'd', st, s);
165 fprintf(context->output_manifest, "\n");
166 return store_dir(context, st, s);
167 }
168 else if (S_ISLNK(st.st_mode)) {
169 print_stat(context, 'l', st, s);
Koushik Dutta4f1c5e32011-07-11 13:25:04 -0700170 return store_link(context, st, s);
Koushik Dutta67700e72011-07-11 12:26:45 -0700171 }
172 else {
Koushik Dutta4f1c5e32011-07-11 13:25:04 -0700173 fprintf(stderr, "Skipping special: %s\n", s);
174 return 0;
Koushik Dutta67700e72011-07-11 12:26:45 -0700175 }
176}
177
Koushik Dutta4f1c5e32011-07-11 13:25:04 -0700178void get_full_path(char *out_path, char *rel_path) {
Koushik Dutta67700e72011-07-11 12:26:45 -0700179 char tmp[PATH_MAX];
180 getcwd(tmp, PATH_MAX);
181 chdir(rel_path);
182 getcwd(out_path, PATH_MAX);
183 chdir(tmp);
184}
185
Koushik Dutta4f1c5e32011-07-11 13:25:04 -0700186static char* tokenize(char *out, const char* line, const char sep) {
187 while (*line != sep) {
Koushik Duttae8bdefd2011-07-11 14:13:43 -0700188 if (*line == '\0') {
Koushik Dutta4f1c5e32011-07-11 13:25:04 -0700189 return NULL;
190 }
191
192 *out = *line;
193 out++;
194 line++;
195 }
196
Koushik Duttae8bdefd2011-07-11 14:13:43 -0700197 *out = '\0';
Koushik Dutta4f1c5e32011-07-11 13:25:04 -0700198 // resume at the next char
Koushik Duttae8bdefd2011-07-11 14:13:43 -0700199 return ++line;
Koushik Dutta4f1c5e32011-07-11 13:25:04 -0700200}
201
202static int dec_to_oct(int dec) {
203 int ret = 0;
204 int mult = 1;
205 while (dec != 0) {
206 int rem = dec % 10;
207 ret += (rem * mult);
208 dec /= 10;
209 mult *= 8;
210 }
211
212 return ret;
213}
214
Koushik Dutta67700e72011-07-11 12:26:45 -0700215int main(int argc, char** argv) {
216 if (argc != 5) {
217 usage(argv);
218 return 1;
219 }
220
221 if (strcmp(argv[1], "c") == 0) {
222 struct stat st;
223 int ret;
224 if (0 != (ret = lstat(argv[2], &st))) {
225 fprintf(stderr, "Error opening input_file/input_directory.\n");
226 return ret;
227 }
228
229 if (!S_ISDIR(st.st_mode)) {
230 fprintf(stderr, "%s must be a directory.\n", argv[2]);
Koushik Duttae8bdefd2011-07-11 14:13:43 -0700231 return 1;
Koushik Dutta67700e72011-07-11 12:26:45 -0700232 }
233
234 char blob_dir[PATH_MAX];
Koushik Dutta4f1c5e32011-07-11 13:25:04 -0700235 struct DEDUPE_STORE_CONTEXT context;
Koushik Dutta67700e72011-07-11 12:26:45 -0700236 context.output_manifest = fopen(argv[4], "wb");
237 if (context.output_manifest == NULL) {
238 fprintf(stderr, "Unable to open output file %s\n", argv[4]);
239 return 1;
240 }
Koushik Dutta4f1c5e32011-07-11 13:25:04 -0700241 get_full_path(context.blob_dir, argv[3]);
Koushik Dutta67700e72011-07-11 12:26:45 -0700242 chdir(argv[2]);
243
244 return store_dir(&context, st, ".");
245 }
246 else if (strcmp(argv[1], "x") == 0) {
Koushik Dutta4f1c5e32011-07-11 13:25:04 -0700247 FILE *input_manifest = fopen(argv[2], "rb");
248 if (input_manifest == NULL) {
249 fprintf(stderr, "Unable to open input manifest %s\n", argv[2]);
250 return 1;
251 }
252
253 char blob_dir[PATH_MAX];
254 char *output_dir = argv[4];
255 get_full_path(blob_dir, argv[3]);
256
257 printf("%s\n" , output_dir);
258 chdir(output_dir);
259
260 char line[PATH_MAX];
261 while (fgets(line, PATH_MAX, input_manifest)) {
262 //printf("%s", line);
263
264 char type[4];
265 char mode[8];
266 char uid[32];
267 char gid[32];
268 char filename[PATH_MAX];
269
270 char *token = line;
271 token = tokenize(type, token, '\t');
272 token = tokenize(mode, token, '\t');
273 token = tokenize(uid, token, '\t');
274 token = tokenize(gid, token, '\t');
275 token = tokenize(filename, token, '\t');
276
277 int mode_oct = dec_to_oct(atoi(mode));
Koushik Duttae8bdefd2011-07-11 14:13:43 -0700278 int uid_int = atoi(uid);
279 int gid_int = atoi(gid);
Koushik Dutta4f1c5e32011-07-11 13:25:04 -0700280 int ret;
281 printf("%s\t%s\t%s\t%s\t%s\t", type, mode, uid, gid, filename);
282 if (strcmp(type, "f") == 0) {
283 char md5[41];
284 token = tokenize(md5, token, '\t');
285 printf("%s\n", md5);
286
287 char blob_file[PATH_MAX];
288 sprintf(blob_file, "%s/%s", blob_dir, md5);
289 if (ret = copy_file(filename, blob_file)) {
290 fprintf(stderr, "Unable to copy file %s\n", filename);
291 return ret;
292 }
293
294 chmod(filename, mode_oct);
Koushik Duttae8bdefd2011-07-11 14:13:43 -0700295 chown(filename, uid_int, gid_int);
Koushik Dutta4f1c5e32011-07-11 13:25:04 -0700296 }
297 else if (strcmp(type, "l") == 0) {
298 char link[41];
299 token = tokenize(link, token, '\t');
300 printf("%s\n", link);
301
302 symlink(link, filename);
303
304 chmod(filename, mode_oct);
Koushik Duttae8bdefd2011-07-11 14:13:43 -0700305 lchown(filename, uid_int, gid_int);
Koushik Dutta4f1c5e32011-07-11 13:25:04 -0700306 }
307 else if (strcmp(type, "d") == 0) {
308 printf("\n");
309
310 mkdir(filename, mode_oct);
311
312 chmod(filename, mode_oct);
Koushik Duttae8bdefd2011-07-11 14:13:43 -0700313 chown(filename, uid_int, gid_int);
Koushik Dutta4f1c5e32011-07-11 13:25:04 -0700314 }
315 }
316
317 fclose(input_manifest);
318 return 0;
Koushik Dutta67700e72011-07-11 12:26:45 -0700319 }
320 else {
321 usage(argv);
322 return 1;
323 }
Koushik Duttafd6a28b2011-07-11 13:26:14 -0700324}