blob: 8969fbc78822c1b506cd365062f2d6a256effbcc [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
138 if (ret = store_st(context->blob_dir, cst, full_path))
139 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 Dutta4f1c5e32011-07-11 13:25:04 -0700153 fprintf(context->output_manifest, "%s\t\n", link);
Koushik Dutta67700e72011-07-11 12:26:45 -0700154 return 0;
155}
156
Koushik Dutta4f1c5e32011-07-11 13:25:04 -0700157static int store_st(struct DEDUPE_STORE_CONTEXT *context, struct stat st, const char* s) {
Koushik Dutta67700e72011-07-11 12:26:45 -0700158 if (S_ISREG(st.st_mode)) {
159 print_stat(context, 'f', st, s);
160 return store_file(context, st, s);
161 }
162 else if (S_ISDIR(st.st_mode)) {
163 print_stat(context, 'd', st, s);
164 fprintf(context->output_manifest, "\n");
165 return store_dir(context, st, s);
166 }
167 else if (S_ISLNK(st.st_mode)) {
168 print_stat(context, 'l', st, s);
Koushik Dutta4f1c5e32011-07-11 13:25:04 -0700169 return store_link(context, st, s);
Koushik Dutta67700e72011-07-11 12:26:45 -0700170 }
171 else {
Koushik Dutta4f1c5e32011-07-11 13:25:04 -0700172 fprintf(stderr, "Skipping special: %s\n", s);
173 return 0;
Koushik Dutta67700e72011-07-11 12:26:45 -0700174 }
175}
176
Koushik Dutta4f1c5e32011-07-11 13:25:04 -0700177void get_full_path(char *out_path, char *rel_path) {
Koushik Dutta67700e72011-07-11 12:26:45 -0700178 char tmp[PATH_MAX];
179 getcwd(tmp, PATH_MAX);
180 chdir(rel_path);
181 getcwd(out_path, PATH_MAX);
182 chdir(tmp);
183}
184
Koushik Dutta4f1c5e32011-07-11 13:25:04 -0700185static char* tokenize(char *out, const char* line, const char sep) {
186 while (*line != sep) {
187 if (*line == NULL) {
188 return NULL;
189 }
190
191 *out = *line;
192 out++;
193 line++;
194 }
195
196 *out = NULL;
197 // resume at the next char
198 return line + 1;
199}
200
201static int dec_to_oct(int dec) {
202 int ret = 0;
203 int mult = 1;
204 while (dec != 0) {
205 int rem = dec % 10;
206 ret += (rem * mult);
207 dec /= 10;
208 mult *= 8;
209 }
210
211 return ret;
212}
213
Koushik Dutta67700e72011-07-11 12:26:45 -0700214int main(int argc, char** argv) {
215 if (argc != 5) {
216 usage(argv);
217 return 1;
218 }
219
220 if (strcmp(argv[1], "c") == 0) {
221 struct stat st;
222 int ret;
223 if (0 != (ret = lstat(argv[2], &st))) {
224 fprintf(stderr, "Error opening input_file/input_directory.\n");
225 return ret;
226 }
227
228 if (!S_ISDIR(st.st_mode)) {
229 fprintf(stderr, "%s must be a directory.\n", argv[2]);
230 return;
231 }
232
233 char blob_dir[PATH_MAX];
Koushik Dutta4f1c5e32011-07-11 13:25:04 -0700234 struct DEDUPE_STORE_CONTEXT context;
Koushik Dutta67700e72011-07-11 12:26:45 -0700235 context.output_manifest = fopen(argv[4], "wb");
236 if (context.output_manifest == NULL) {
237 fprintf(stderr, "Unable to open output file %s\n", argv[4]);
238 return 1;
239 }
Koushik Dutta4f1c5e32011-07-11 13:25:04 -0700240 get_full_path(context.blob_dir, argv[3]);
Koushik Dutta67700e72011-07-11 12:26:45 -0700241 chdir(argv[2]);
242
243 return store_dir(&context, st, ".");
244 }
245 else if (strcmp(argv[1], "x") == 0) {
Koushik Dutta4f1c5e32011-07-11 13:25:04 -0700246 FILE *input_manifest = fopen(argv[2], "rb");
247 if (input_manifest == NULL) {
248 fprintf(stderr, "Unable to open input manifest %s\n", argv[2]);
249 return 1;
250 }
251
252 char blob_dir[PATH_MAX];
253 char *output_dir = argv[4];
254 get_full_path(blob_dir, argv[3]);
255
256 printf("%s\n" , output_dir);
257 chdir(output_dir);
258
259 char line[PATH_MAX];
260 while (fgets(line, PATH_MAX, input_manifest)) {
261 //printf("%s", line);
262
263 char type[4];
264 char mode[8];
265 char uid[32];
266 char gid[32];
267 char filename[PATH_MAX];
268
269 char *token = line;
270 token = tokenize(type, token, '\t');
271 token = tokenize(mode, token, '\t');
272 token = tokenize(uid, token, '\t');
273 token = tokenize(gid, token, '\t');
274 token = tokenize(filename, token, '\t');
275
276 int mode_oct = dec_to_oct(atoi(mode));
277 int ret;
278 printf("%s\t%s\t%s\t%s\t%s\t", type, mode, uid, gid, filename);
279 if (strcmp(type, "f") == 0) {
280 char md5[41];
281 token = tokenize(md5, token, '\t');
282 printf("%s\n", md5);
283
284 char blob_file[PATH_MAX];
285 sprintf(blob_file, "%s/%s", blob_dir, md5);
286 if (ret = copy_file(filename, blob_file)) {
287 fprintf(stderr, "Unable to copy file %s\n", filename);
288 return ret;
289 }
290
291 chmod(filename, mode_oct);
292 chown(filename, uid, gid);
293 }
294 else if (strcmp(type, "l") == 0) {
295 char link[41];
296 token = tokenize(link, token, '\t');
297 printf("%s\n", link);
298
299 symlink(link, filename);
300
301 chmod(filename, mode_oct);
302 lchown(filename, uid, gid);
303 }
304 else if (strcmp(type, "d") == 0) {
305 printf("\n");
306
307 mkdir(filename, mode_oct);
308
309 chmod(filename, mode_oct);
310 chown(filename, uid, gid);
311 }
312 }
313
314 fclose(input_manifest);
315 return 0;
Koushik Dutta67700e72011-07-11 12:26:45 -0700316 }
317 else {
318 usage(argv);
319 return 1;
320 }
Koushik Duttafd6a28b2011-07-11 13:26:14 -0700321}