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 | |
| 13 | typedef struct DEDUPE_STORE_CTX { |
| 14 | char blob_dir[PATH_MAX]; |
| 15 | FILE *output_manifest; |
| 16 | }; |
| 17 | |
| 18 | static void usage(char** argv) { |
| 19 | fprintf(stderr, "usage: %s c input_file|input_directory blob_dir output_manifest\n", argv[0]); |
| 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 | |
| 84 | static int store_st(struct DEDUPE_STORE_CTX *context, struct stat st, const char* s); |
| 85 | |
| 86 | void print_stat(struct DEDUPE_STORE_CTX *context, char type, struct stat st, const char *f) { |
| 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 | |
| 90 | static int store_file(struct DEDUPE_STORE_CTX *context, struct stat st, const char* f) { |
| 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'; |
| 103 | |
| 104 | |
| 105 | //char cmd[PATH_MAX]; |
| 106 | //sprintf(cmd, "cat %s > '/%s/%s'", f, context->blob_dir, psum); |
| 107 | //system(cmd); |
| 108 | |
| 109 | char out_blob[PATH_MAX]; |
| 110 | sprintf(out_blob, "%s/%s", context->blob_dir, psum); |
| 111 | if (ret = copy_file(out_blob, f)) { |
| 112 | fprintf(stderr, "Error copying blob %s\n", f); |
| 113 | return ret; |
| 114 | } |
| 115 | |
| 116 | //sprintf("cat %s > $OUTPUT_DIR") |
| 117 | |
| 118 | fprintf(context->output_manifest, "%s\n", psum); |
| 119 | return 0; |
| 120 | } |
| 121 | |
| 122 | static int store_dir(struct DEDUPE_STORE_CTX *context, struct stat st, const char* d) { |
| 123 | printf("%s\n", d); |
| 124 | DIR *dp = opendir(d); |
| 125 | if (d == NULL) { |
| 126 | fprintf(stderr, "Error opening directory: %s\n", d); |
| 127 | return 1; |
| 128 | } |
| 129 | struct dirent *ep; |
| 130 | char full_path[PATH_MAX]; |
| 131 | while (ep = readdir(dp)) { |
| 132 | if (strcmp(ep->d_name, ".") == 0) |
| 133 | continue; |
| 134 | if (strcmp(ep->d_name, "..") == 0) |
| 135 | continue; |
| 136 | struct stat cst; |
| 137 | int ret; |
| 138 | sprintf(full_path, "%s/%s", d, ep->d_name); |
| 139 | if (0 != (ret = lstat(full_path, &cst))) { |
| 140 | fprintf(stderr, "Error opening: %s\n", ep->d_name); |
| 141 | closedir(dp); |
| 142 | return ret; |
| 143 | } |
| 144 | |
| 145 | if (ret = store_st(context->blob_dir, cst, full_path)) |
| 146 | return ret; |
| 147 | } |
| 148 | closedir(dp); |
| 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | static int store_link(struct DEDUPE_STORE_CTX *context, struct stat st, const char* l) { |
| 153 | printf("%s\n", l); |
| 154 | char link[PATH_MAX]; |
| 155 | int ret = readlink(l, link, PATH_MAX); |
| 156 | if (ret < 0) { |
| 157 | fprintf(stderr, "Error reading symlink\n"); |
| 158 | return errno; |
| 159 | } |
| 160 | fprintf(context->output_manifest, "%s\n", link); |
| 161 | return 0; |
| 162 | } |
| 163 | |
| 164 | static int store_st(struct DEDUPE_STORE_CTX *context, struct stat st, const char* s) { |
| 165 | if (S_ISREG(st.st_mode)) { |
| 166 | print_stat(context, 'f', st, s); |
| 167 | return store_file(context, st, s); |
| 168 | } |
| 169 | else if (S_ISDIR(st.st_mode)) { |
| 170 | print_stat(context, 'd', st, s); |
| 171 | fprintf(context->output_manifest, "\n"); |
| 172 | return store_dir(context, st, s); |
| 173 | } |
| 174 | else if (S_ISLNK(st.st_mode)) { |
| 175 | print_stat(context, 'l', st, s); |
| 176 | store_link(context, st, s); |
| 177 | } |
| 178 | else { |
| 179 | return fprintf(stderr, "Skipping special: %s\n", s); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | void get_full_path(char *rel_path, char *out_path) { |
| 184 | char tmp[PATH_MAX]; |
| 185 | getcwd(tmp, PATH_MAX); |
| 186 | chdir(rel_path); |
| 187 | getcwd(out_path, PATH_MAX); |
| 188 | chdir(tmp); |
| 189 | } |
| 190 | |
| 191 | int main(int argc, char** argv) { |
| 192 | if (argc != 5) { |
| 193 | usage(argv); |
| 194 | return 1; |
| 195 | } |
| 196 | |
| 197 | if (strcmp(argv[1], "c") == 0) { |
| 198 | struct stat st; |
| 199 | int ret; |
| 200 | if (0 != (ret = lstat(argv[2], &st))) { |
| 201 | fprintf(stderr, "Error opening input_file/input_directory.\n"); |
| 202 | return ret; |
| 203 | } |
| 204 | |
| 205 | if (!S_ISDIR(st.st_mode)) { |
| 206 | fprintf(stderr, "%s must be a directory.\n", argv[2]); |
| 207 | return; |
| 208 | } |
| 209 | |
| 210 | char blob_dir[PATH_MAX]; |
| 211 | struct DEDUPE_STORE_CTX context; |
| 212 | context.output_manifest = fopen(argv[4], "wb"); |
| 213 | if (context.output_manifest == NULL) { |
| 214 | fprintf(stderr, "Unable to open output file %s\n", argv[4]); |
| 215 | return 1; |
| 216 | } |
| 217 | get_full_path(argv[3], context.blob_dir); |
| 218 | chdir(argv[2]); |
| 219 | |
| 220 | return store_dir(&context, st, "."); |
| 221 | } |
| 222 | else if (strcmp(argv[1], "x") == 0) { |
| 223 | } |
| 224 | else { |
| 225 | usage(argv); |
| 226 | return 1; |
| 227 | } |
| 228 | |
| 229 | return 0; |
| 230 | } |