dir_hash.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /*
  2. * Copyright (C) 2007 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <dirent.h>
  17. #include <errno.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <sha1.h>
  22. #include <unistd.h>
  23. #include <limits.h>
  24. #include <sys/stat.h>
  25. #include <netinet/in.h>
  26. #include <resolv.h>
  27. #include <cutils/dir_hash.h>
  28. /**
  29. * Copies, if it fits within max_output_string bytes, into output_string
  30. * a hash of the contents, size, permissions, uid, and gid of the file
  31. * specified by path, using the specified algorithm. Returns the length
  32. * of the output string, or a negative number if the buffer is too short.
  33. */
  34. int get_file_hash(HashAlgorithm algorithm, const char *path,
  35. char *output_string, size_t max_output_string) {
  36. SHA1_CTX context;
  37. struct stat sb;
  38. unsigned char md[SHA1_DIGEST_LENGTH];
  39. int used = 0;
  40. size_t n;
  41. if (algorithm != SHA_1) {
  42. errno = EINVAL;
  43. return -1;
  44. }
  45. if (stat(path, &sb) != 0) {
  46. return -1;
  47. }
  48. if (S_ISLNK(sb.st_mode)) {
  49. char buf[PATH_MAX];
  50. int len;
  51. len = readlink(path, buf, sizeof(buf));
  52. if (len < 0) {
  53. return -1;
  54. }
  55. SHA1Init(&context);
  56. SHA1Update(&context, (unsigned char *) buf, len);
  57. SHA1Final(md, &context);
  58. } else if (S_ISREG(sb.st_mode)) {
  59. char buf[10000];
  60. FILE *f = fopen(path, "rb");
  61. int len;
  62. if (f == NULL) {
  63. return -1;
  64. }
  65. SHA1Init(&context);
  66. while ((len = fread(buf, 1, sizeof(buf), f)) > 0) {
  67. SHA1Update(&context, (unsigned char *) buf, len);
  68. }
  69. if (ferror(f)) {
  70. fclose(f);
  71. return -1;
  72. }
  73. fclose(f);
  74. SHA1Final(md, &context);
  75. }
  76. if (S_ISLNK(sb.st_mode) || S_ISREG(sb.st_mode)) {
  77. used = b64_ntop(md, SHA1_DIGEST_LENGTH,
  78. output_string, max_output_string);
  79. if (used < 0) {
  80. errno = ENOSPC;
  81. return -1;
  82. }
  83. n = snprintf(output_string + used, max_output_string - used,
  84. " %d 0%o %d %d", (int) sb.st_size, sb.st_mode,
  85. (int) sb.st_uid, (int) sb.st_gid);
  86. } else {
  87. n = snprintf(output_string, max_output_string,
  88. "- - 0%o %d %d", sb.st_mode,
  89. (int) sb.st_uid, (int) sb.st_gid);
  90. }
  91. if (n >= max_output_string - used) {
  92. errno = ENOSPC;
  93. return -(used + n);
  94. }
  95. return used + n;
  96. }
  97. struct list {
  98. char *name;
  99. struct list *next;
  100. };
  101. static int cmp(const void *a, const void *b) {
  102. struct list *const *ra = a;
  103. struct list *const *rb = b;
  104. return strcmp((*ra)->name, (*rb)->name);
  105. }
  106. static int recurse(HashAlgorithm algorithm, const char *directory_path,
  107. struct list **out) {
  108. struct list *list = NULL;
  109. struct list *f;
  110. struct dirent *de;
  111. DIR *d = opendir(directory_path);
  112. if (d == NULL) {
  113. return -1;
  114. }
  115. while ((de = readdir(d)) != NULL) {
  116. if (strcmp(de->d_name, ".") == 0) {
  117. continue;
  118. }
  119. if (strcmp(de->d_name, "..") == 0) {
  120. continue;
  121. }
  122. char *name = malloc(strlen(de->d_name) + 1);
  123. struct list *node = malloc(sizeof(struct list));
  124. if (name == NULL || node == NULL) {
  125. struct list *next;
  126. for (f = list; f != NULL; f = next) {
  127. next = f->next;
  128. free(f->name);
  129. free(f);
  130. }
  131. free(name);
  132. free(node);
  133. closedir(d);
  134. return -1;
  135. }
  136. strcpy(name, de->d_name);
  137. node->name = name;
  138. node->next = list;
  139. list = node;
  140. }
  141. closedir(d);
  142. for (f = list; f != NULL; f = f->next) {
  143. struct stat sb;
  144. char *name;
  145. char outstr[NAME_MAX + 100];
  146. char *keep;
  147. struct list *res;
  148. name = malloc(strlen(f->name) + strlen(directory_path) + 2);
  149. if (name == NULL) {
  150. struct list *next;
  151. for (f = list; f != NULL; f = f->next) {
  152. next = f->next;
  153. free(f->name);
  154. free(f);
  155. }
  156. for (f = *out; f != NULL; f = f->next) {
  157. next = f->next;
  158. free(f->name);
  159. free(f);
  160. }
  161. *out = NULL;
  162. return -1;
  163. }
  164. sprintf(name, "%s/%s", directory_path, f->name);
  165. int len = get_file_hash(algorithm, name,
  166. outstr, sizeof(outstr));
  167. if (len < 0) {
  168. // should not happen
  169. free(name);
  170. return -1;
  171. }
  172. keep = malloc(len + strlen(name) + 3);
  173. res = malloc(sizeof(struct list));
  174. if (keep == NULL || res == NULL) {
  175. struct list *next;
  176. for (f = list; f != NULL; f = f->next) {
  177. next = f->next;
  178. free(f->name);
  179. free(f);
  180. }
  181. for (f = *out; f != NULL; f = f->next) {
  182. next = f->next;
  183. free(f->name);
  184. free(f);
  185. }
  186. *out = NULL;
  187. free(keep);
  188. free(res);
  189. free(name);
  190. return -1;
  191. }
  192. sprintf(keep, "%s %s\n", name, outstr);
  193. res->name = keep;
  194. res->next = *out;
  195. *out = res;
  196. if ((stat(name, &sb) == 0) && S_ISDIR(sb.st_mode)) {
  197. if (recurse(algorithm, name, out) < 0) {
  198. struct list *next;
  199. for (f = list; f != NULL; f = next) {
  200. next = f->next;
  201. free(f->name);
  202. free(f);
  203. }
  204. return -1;
  205. }
  206. }
  207. }
  208. struct list *next;
  209. for (f = list; f != NULL; f = next) {
  210. next = f->next;
  211. free(f->name);
  212. free(f);
  213. }
  214. }
  215. /**
  216. * Allocates a string containing the names and hashes of all files recursively
  217. * reached under the specified directory_path, using the specified algorithm.
  218. * The string is returned as *output_string; the return value is the length
  219. * of the string, or a negative number if there was a failure.
  220. */
  221. int get_recursive_hash_manifest(HashAlgorithm algorithm,
  222. const char *directory_path,
  223. char **output_string) {
  224. struct list *out = NULL;
  225. struct list *r;
  226. struct list **list;
  227. int count = 0;
  228. int len = 0;
  229. int retlen = 0;
  230. int i;
  231. char *buf;
  232. if (recurse(algorithm, directory_path, &out) < 0) {
  233. return -1;
  234. }
  235. for (r = out; r != NULL; r = r->next) {
  236. count++;
  237. len += strlen(r->name);
  238. }
  239. list = malloc(count * sizeof(struct list *));
  240. if (list == NULL) {
  241. struct list *next;
  242. for (r = out; r != NULL; r = next) {
  243. next = r->next;
  244. free(r->name);
  245. free(r);
  246. }
  247. return -1;
  248. }
  249. count = 0;
  250. for (r = out; r != NULL; r = r->next) {
  251. list[count++] = r;
  252. }
  253. qsort(list, count, sizeof(struct list *), cmp);
  254. buf = malloc(len + 1);
  255. if (buf == NULL) {
  256. struct list *next;
  257. for (r = out; r != NULL; r = next) {
  258. next = r->next;
  259. free(r->name);
  260. free(r);
  261. }
  262. free(list);
  263. return -1;
  264. }
  265. for (i = 0; i < count; i++) {
  266. int n = strlen(list[i]->name);
  267. strcpy(buf + retlen, list[i]->name);
  268. retlen += n;
  269. }
  270. free(list);
  271. struct list *next;
  272. for (r = out; r != NULL; r = next) {
  273. next = r->next;
  274. free(r->name);
  275. free(r);
  276. }
  277. *output_string = buf;
  278. return retlen;
  279. }