Koushik Dutta | 67700e7 | 2011-07-11 12:26:45 -0700 | [diff] [blame] | 1 | #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 Dutta | 4f1c5e3 | 2011-07-11 13:25:04 -0700 | [diff] [blame] | 13 | typedef struct DEDUPE_STORE_CONTEXT { |
Koushik Dutta | 67700e7 | 2011-07-11 12:26:45 -0700 | [diff] [blame] | 14 | char blob_dir[PATH_MAX]; |
| 15 | FILE *output_manifest; |
| 16 | }; |
| 17 | |
| 18 | static void usage(char** argv) { |
Koushik Dutta | 4f1c5e3 | 2011-07-11 13:25:04 -0700 | [diff] [blame] | 19 | fprintf(stderr, "usage: %s c input_directory blob_dir output_manifest\n", argv[0]); |
Koushik Dutta | 67700e7 | 2011-07-11 12:26:45 -0700 | [diff] [blame] | 20 | fprintf(stderr, "usage: %s x input_manifest blob_dir output_directory\n", argv[0]); |
| 21 | } |
| 22 | |
| 23 | static 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 | |
| 57 | static 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 | |
| 73 | static 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 Dutta | 4f1c5e3 | 2011-07-11 13:25:04 -0700 | [diff] [blame] | 84 | static int store_st(struct DEDUPE_STORE_CONTEXT *context, struct stat st, const char* s); |
Koushik Dutta | 67700e7 | 2011-07-11 12:26:45 -0700 | [diff] [blame] | 85 | |
Koushik Dutta | 4f1c5e3 | 2011-07-11 13:25:04 -0700 | [diff] [blame] | 86 | void print_stat(struct DEDUPE_STORE_CONTEXT *context, char type, struct stat st, const char *f) { |
Koushik Dutta | 67700e7 | 2011-07-11 12:26:45 -0700 | [diff] [blame] | 87 | 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 Dutta | 4f1c5e3 | 2011-07-11 13:25:04 -0700 | [diff] [blame] | 90 | static int store_file(struct DEDUPE_STORE_CONTEXT *context, struct stat st, const char* f) { |
Koushik Dutta | 67700e7 | 2011-07-11 12:26:45 -0700 | [diff] [blame] | 91 | 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 Dutta | 4f1c5e3 | 2011-07-11 13:25:04 -0700 | [diff] [blame] | 103 | |
Koushik Dutta | 67700e7 | 2011-07-11 12:26:45 -0700 | [diff] [blame] | 104 | 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 Dutta | 4f1c5e3 | 2011-07-11 13:25:04 -0700 | [diff] [blame] | 111 | fprintf(context->output_manifest, "%s\t\n", psum); |
Koushik Dutta | 67700e7 | 2011-07-11 12:26:45 -0700 | [diff] [blame] | 112 | return 0; |
| 113 | } |
| 114 | |
Koushik Dutta | 4f1c5e3 | 2011-07-11 13:25:04 -0700 | [diff] [blame] | 115 | static int store_dir(struct DEDUPE_STORE_CONTEXT *context, struct stat st, const char* d) { |
Koushik Dutta | 67700e7 | 2011-07-11 12:26:45 -0700 | [diff] [blame] | 116 | 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 Dutta | e8bdefd | 2011-07-11 14:13:43 -0700 | [diff] [blame^] | 138 | if (ret = store_st(context, cst, full_path)) |
Koushik Dutta | 67700e7 | 2011-07-11 12:26:45 -0700 | [diff] [blame] | 139 | return ret; |
| 140 | } |
| 141 | closedir(dp); |
| 142 | return 0; |
| 143 | } |
| 144 | |
Koushik Dutta | 4f1c5e3 | 2011-07-11 13:25:04 -0700 | [diff] [blame] | 145 | static int store_link(struct DEDUPE_STORE_CONTEXT *context, struct stat st, const char* l) { |
Koushik Dutta | 67700e7 | 2011-07-11 12:26:45 -0700 | [diff] [blame] | 146 | 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 Dutta | e8bdefd | 2011-07-11 14:13:43 -0700 | [diff] [blame^] | 153 | link[ret] = '\0'; |
Koushik Dutta | 4f1c5e3 | 2011-07-11 13:25:04 -0700 | [diff] [blame] | 154 | fprintf(context->output_manifest, "%s\t\n", link); |
Koushik Dutta | 67700e7 | 2011-07-11 12:26:45 -0700 | [diff] [blame] | 155 | return 0; |
| 156 | } |
| 157 | |
Koushik Dutta | 4f1c5e3 | 2011-07-11 13:25:04 -0700 | [diff] [blame] | 158 | static int store_st(struct DEDUPE_STORE_CONTEXT *context, struct stat st, const char* s) { |
Koushik Dutta | 67700e7 | 2011-07-11 12:26:45 -0700 | [diff] [blame] | 159 | 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 Dutta | 4f1c5e3 | 2011-07-11 13:25:04 -0700 | [diff] [blame] | 170 | return store_link(context, st, s); |
Koushik Dutta | 67700e7 | 2011-07-11 12:26:45 -0700 | [diff] [blame] | 171 | } |
| 172 | else { |
Koushik Dutta | 4f1c5e3 | 2011-07-11 13:25:04 -0700 | [diff] [blame] | 173 | fprintf(stderr, "Skipping special: %s\n", s); |
| 174 | return 0; |
Koushik Dutta | 67700e7 | 2011-07-11 12:26:45 -0700 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | |
Koushik Dutta | 4f1c5e3 | 2011-07-11 13:25:04 -0700 | [diff] [blame] | 178 | void get_full_path(char *out_path, char *rel_path) { |
Koushik Dutta | 67700e7 | 2011-07-11 12:26:45 -0700 | [diff] [blame] | 179 | 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 Dutta | 4f1c5e3 | 2011-07-11 13:25:04 -0700 | [diff] [blame] | 186 | static char* tokenize(char *out, const char* line, const char sep) { |
| 187 | while (*line != sep) { |
Koushik Dutta | e8bdefd | 2011-07-11 14:13:43 -0700 | [diff] [blame^] | 188 | if (*line == '\0') { |
Koushik Dutta | 4f1c5e3 | 2011-07-11 13:25:04 -0700 | [diff] [blame] | 189 | return NULL; |
| 190 | } |
| 191 | |
| 192 | *out = *line; |
| 193 | out++; |
| 194 | line++; |
| 195 | } |
| 196 | |
Koushik Dutta | e8bdefd | 2011-07-11 14:13:43 -0700 | [diff] [blame^] | 197 | *out = '\0'; |
Koushik Dutta | 4f1c5e3 | 2011-07-11 13:25:04 -0700 | [diff] [blame] | 198 | // resume at the next char |
Koushik Dutta | e8bdefd | 2011-07-11 14:13:43 -0700 | [diff] [blame^] | 199 | return ++line; |
Koushik Dutta | 4f1c5e3 | 2011-07-11 13:25:04 -0700 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | static 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 Dutta | 67700e7 | 2011-07-11 12:26:45 -0700 | [diff] [blame] | 215 | int 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 Dutta | e8bdefd | 2011-07-11 14:13:43 -0700 | [diff] [blame^] | 231 | return 1; |
Koushik Dutta | 67700e7 | 2011-07-11 12:26:45 -0700 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | char blob_dir[PATH_MAX]; |
Koushik Dutta | 4f1c5e3 | 2011-07-11 13:25:04 -0700 | [diff] [blame] | 235 | struct DEDUPE_STORE_CONTEXT context; |
Koushik Dutta | 67700e7 | 2011-07-11 12:26:45 -0700 | [diff] [blame] | 236 | 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 Dutta | 4f1c5e3 | 2011-07-11 13:25:04 -0700 | [diff] [blame] | 241 | get_full_path(context.blob_dir, argv[3]); |
Koushik Dutta | 67700e7 | 2011-07-11 12:26:45 -0700 | [diff] [blame] | 242 | chdir(argv[2]); |
| 243 | |
| 244 | return store_dir(&context, st, "."); |
| 245 | } |
| 246 | else if (strcmp(argv[1], "x") == 0) { |
Koushik Dutta | 4f1c5e3 | 2011-07-11 13:25:04 -0700 | [diff] [blame] | 247 | 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 Dutta | e8bdefd | 2011-07-11 14:13:43 -0700 | [diff] [blame^] | 278 | int uid_int = atoi(uid); |
| 279 | int gid_int = atoi(gid); |
Koushik Dutta | 4f1c5e3 | 2011-07-11 13:25:04 -0700 | [diff] [blame] | 280 | 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 Dutta | e8bdefd | 2011-07-11 14:13:43 -0700 | [diff] [blame^] | 295 | chown(filename, uid_int, gid_int); |
Koushik Dutta | 4f1c5e3 | 2011-07-11 13:25:04 -0700 | [diff] [blame] | 296 | } |
| 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 Dutta | e8bdefd | 2011-07-11 14:13:43 -0700 | [diff] [blame^] | 305 | lchown(filename, uid_int, gid_int); |
Koushik Dutta | 4f1c5e3 | 2011-07-11 13:25:04 -0700 | [diff] [blame] | 306 | } |
| 307 | else if (strcmp(type, "d") == 0) { |
| 308 | printf("\n"); |
| 309 | |
| 310 | mkdir(filename, mode_oct); |
| 311 | |
| 312 | chmod(filename, mode_oct); |
Koushik Dutta | e8bdefd | 2011-07-11 14:13:43 -0700 | [diff] [blame^] | 313 | chown(filename, uid_int, gid_int); |
Koushik Dutta | 4f1c5e3 | 2011-07-11 13:25:04 -0700 | [diff] [blame] | 314 | } |
| 315 | } |
| 316 | |
| 317 | fclose(input_manifest); |
| 318 | return 0; |
Koushik Dutta | 67700e7 | 2011-07-11 12:26:45 -0700 | [diff] [blame] | 319 | } |
| 320 | else { |
| 321 | usage(argv); |
| 322 | return 1; |
| 323 | } |
Koushik Dutta | fd6a28b | 2011-07-11 13:26:14 -0700 | [diff] [blame] | 324 | } |