/libcutils/dir_hash.c

https://gitlab.com/infraredbg/android_system_core-mt6589 · C · 335 lines · 249 code · 58 blank · 28 comment · 72 complexity · 5202201032c4e347db56f9e35c76191e MD5 · raw file

  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;
  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. return -1;
  170. }
  171. keep = malloc(len + strlen(name) + 3);
  172. res = malloc(sizeof(struct list));
  173. if (keep == NULL || res == NULL) {
  174. struct list *next;
  175. for (f = list; f != NULL; f = f->next) {
  176. next = f->next;
  177. free(f->name);
  178. free(f);
  179. }
  180. for (f = *out; f != NULL; f = f->next) {
  181. next = f->next;
  182. free(f->name);
  183. free(f);
  184. }
  185. *out = NULL;
  186. free(keep);
  187. free(res);
  188. return -1;
  189. }
  190. sprintf(keep, "%s %s\n", name, outstr);
  191. res->name = keep;
  192. res->next = *out;
  193. *out = res;
  194. if ((stat(name, &sb) == 0) && S_ISDIR(sb.st_mode)) {
  195. if (recurse(algorithm, name, out) < 0) {
  196. struct list *next;
  197. for (f = list; f != NULL; f = next) {
  198. next = f->next;
  199. free(f->name);
  200. free(f);
  201. }
  202. return -1;
  203. }
  204. }
  205. }
  206. struct list *next;
  207. for (f = list; f != NULL; f = next) {
  208. next = f->next;
  209. free(f->name);
  210. free(f);
  211. }
  212. }
  213. /**
  214. * Allocates a string containing the names and hashes of all files recursively
  215. * reached under the specified directory_path, using the specified algorithm.
  216. * The string is returned as *output_string; the return value is the length
  217. * of the string, or a negative number if there was a failure.
  218. */
  219. int get_recursive_hash_manifest(HashAlgorithm algorithm,
  220. const char *directory_path,
  221. char **output_string) {
  222. struct list *out = NULL;
  223. struct list *r;
  224. struct list **list;
  225. int count = 0;
  226. int len = 0;
  227. int retlen = 0;
  228. int i;
  229. char *buf;
  230. if (recurse(algorithm, directory_path, &out) < 0) {
  231. return -1;
  232. }
  233. for (r = out; r != NULL; r = r->next) {
  234. count++;
  235. len += strlen(r->name);
  236. }
  237. list = malloc(count * sizeof(struct list *));
  238. if (list == NULL) {
  239. struct list *next;
  240. for (r = out; r != NULL; r = next) {
  241. next = r->next;
  242. free(r->name);
  243. free(r);
  244. }
  245. return -1;
  246. }
  247. count = 0;
  248. for (r = out; r != NULL; r = r->next) {
  249. list[count++] = r;
  250. }
  251. qsort(list, count, sizeof(struct list *), cmp);
  252. buf = malloc(len + 1);
  253. if (buf == NULL) {
  254. struct list *next;
  255. for (r = out; r != NULL; r = next) {
  256. next = r->next;
  257. free(r->name);
  258. free(r);
  259. }
  260. free(list);
  261. return -1;
  262. }
  263. for (i = 0; i < count; i++) {
  264. int n = strlen(list[i]->name);
  265. strcpy(buf + retlen, list[i]->name);
  266. retlen += n;
  267. }
  268. free(list);
  269. struct list *next;
  270. for (r = out; r != NULL; r = next) {
  271. next = r->next;
  272. free(r->name);
  273. free(r);
  274. }
  275. *output_string = buf;
  276. return retlen;
  277. }