file_util.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /* file_util.c - convenience routines for common stat operations
  2. Copyright (C) 2009 Ubiq Technologies <graham.gower@gmail.com>
  3. Carl D. Worth
  4. Copyright (C) 2001 University of Southern California
  5. This program is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License as
  7. published by the Free Software Foundation; either version 2, or (at
  8. your option) any later version.
  9. This program is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. General Public License for more details.
  13. */
  14. #include "config.h"
  15. #include <stdio.h>
  16. #include <sys/types.h>
  17. #include <sys/stat.h>
  18. #include <dirent.h>
  19. #include <unistd.h>
  20. #include <ctype.h>
  21. #include "sprintf_alloc.h"
  22. #include "file_util.h"
  23. #ifdef HAVE_MD5
  24. #include "md5.h"
  25. #endif
  26. #include "libbb/libbb.h"
  27. #if defined HAVE_SHA256
  28. #include "sha256.h"
  29. #endif
  30. int file_exists(const char *file_name)
  31. {
  32. struct stat st;
  33. if (stat(file_name, &st) == -1)
  34. return 0;
  35. return 1;
  36. }
  37. int file_is_dir(const char *file_name)
  38. {
  39. struct stat st;
  40. if (stat(file_name, &st) == -1)
  41. return 0;
  42. return S_ISDIR(st.st_mode);
  43. }
  44. /* read a single line from a file, stopping at a newline or EOF.
  45. If a newline is read, it will appear in the resulting string.
  46. Return value is a malloc'ed char * which should be freed at
  47. some point by the caller.
  48. Return value is NULL if the file is at EOF when called.
  49. */
  50. char *file_read_line_alloc(FILE * fp)
  51. {
  52. char buf[BUFSIZ];
  53. unsigned int buf_len;
  54. char *line = NULL;
  55. unsigned int line_size = 0;
  56. int got_nl = 0;
  57. buf[0] = '\0';
  58. while (fgets(buf, BUFSIZ, fp)) {
  59. buf_len = strlen(buf);
  60. if (buf[buf_len - 1] == '\n') {
  61. buf_len--;
  62. buf[buf_len] = '\0';
  63. got_nl = 1;
  64. }
  65. if (line) {
  66. line_size += buf_len;
  67. line = xrealloc(line, line_size + 1);
  68. strncat(line, buf, line_size);
  69. } else {
  70. line_size = buf_len + 1;
  71. line = xstrdup(buf);
  72. }
  73. if (got_nl)
  74. break;
  75. }
  76. return line;
  77. }
  78. int file_move(const char *src, const char *dest)
  79. {
  80. int err;
  81. err = rename(src, dest);
  82. if (err == -1) {
  83. if (errno == EXDEV) {
  84. /* src & dest live on different file systems */
  85. err = file_copy(src, dest);
  86. if (err == 0)
  87. unlink(src);
  88. } else {
  89. opkg_perror(ERROR, "Failed to rename %s to %s",
  90. src, dest);
  91. }
  92. }
  93. return err;
  94. }
  95. int file_copy(const char *src, const char *dest)
  96. {
  97. int err;
  98. err = copy_file(src, dest, FILEUTILS_FORCE | FILEUTILS_PRESERVE_STATUS);
  99. if (err)
  100. opkg_msg(ERROR, "Failed to copy file %s to %s.\n", src, dest);
  101. return err;
  102. }
  103. int file_mkdir_hier(const char *path, long mode)
  104. {
  105. return make_directory(path, mode, FILEUTILS_RECUR);
  106. }
  107. #ifdef HAVE_MD5
  108. char *file_md5sum_alloc(const char *file_name)
  109. {
  110. static const int md5sum_bin_len = 16;
  111. static const int md5sum_hex_len = 32;
  112. static const unsigned char bin2hex[16] = {
  113. '0', '1', '2', '3',
  114. '4', '5', '6', '7',
  115. '8', '9', 'a', 'b',
  116. 'c', 'd', 'e', 'f'
  117. };
  118. int i, err;
  119. FILE *file;
  120. char *md5sum_hex;
  121. unsigned char md5sum_bin[md5sum_bin_len];
  122. md5sum_hex = xcalloc(1, md5sum_hex_len + 1);
  123. file = fopen(file_name, "r");
  124. if (file == NULL) {
  125. opkg_perror(ERROR, "Failed to open file %s", file_name);
  126. free(md5sum_hex);
  127. return NULL;
  128. }
  129. err = md5_stream(file, md5sum_bin);
  130. if (err) {
  131. opkg_msg(ERROR, "Could't compute md5sum for %s.\n", file_name);
  132. fclose(file);
  133. free(md5sum_hex);
  134. return NULL;
  135. }
  136. fclose(file);
  137. for (i = 0; i < md5sum_bin_len; i++) {
  138. md5sum_hex[i * 2] = bin2hex[md5sum_bin[i] >> 4];
  139. md5sum_hex[i * 2 + 1] = bin2hex[md5sum_bin[i] & 0xf];
  140. }
  141. md5sum_hex[md5sum_hex_len] = '\0';
  142. return md5sum_hex;
  143. }
  144. #endif
  145. #ifdef HAVE_SHA256
  146. char *file_sha256sum_alloc(const char *file_name)
  147. {
  148. static const int sha256sum_bin_len = 32;
  149. static const int sha256sum_hex_len = 64;
  150. static const unsigned char bin2hex[16] = {
  151. '0', '1', '2', '3',
  152. '4', '5', '6', '7',
  153. '8', '9', 'a', 'b',
  154. 'c', 'd', 'e', 'f'
  155. };
  156. int i, err;
  157. FILE *file;
  158. char *sha256sum_hex;
  159. unsigned char sha256sum_bin[sha256sum_bin_len];
  160. sha256sum_hex = xcalloc(1, sha256sum_hex_len + 1);
  161. file = fopen(file_name, "r");
  162. if (file == NULL) {
  163. opkg_perror(ERROR, "Failed to open file %s", file_name);
  164. free(sha256sum_hex);
  165. return NULL;
  166. }
  167. err = sha256_stream(file, sha256sum_bin);
  168. if (err) {
  169. opkg_msg(ERROR, "Could't compute sha256sum for %s.\n",
  170. file_name);
  171. fclose(file);
  172. free(sha256sum_hex);
  173. return NULL;
  174. }
  175. fclose(file);
  176. for (i = 0; i < sha256sum_bin_len; i++) {
  177. sha256sum_hex[i * 2] = bin2hex[sha256sum_bin[i] >> 4];
  178. sha256sum_hex[i * 2 + 1] = bin2hex[sha256sum_bin[i] & 0xf];
  179. }
  180. sha256sum_hex[sha256sum_hex_len] = '\0';
  181. return sha256sum_hex;
  182. }
  183. #endif
  184. char *checksum_bin2hex(const char *src, size_t len)
  185. {
  186. unsigned char *p;
  187. static unsigned char buf[65];
  188. const unsigned char *s = (unsigned char *)src;
  189. static const unsigned char bin2hex[16] = {
  190. '0', '1', '2', '3',
  191. '4', '5', '6', '7',
  192. '8', '9', 'a', 'b',
  193. 'c', 'd', 'e', 'f'
  194. };
  195. if (!s || len > 32)
  196. return NULL;
  197. for (p = buf; len > 0; s++, len--) {
  198. *p++ = bin2hex[*s / 16];
  199. *p++ = bin2hex[*s % 16];
  200. }
  201. *p = 0;
  202. return (char *)buf;
  203. }
  204. char *checksum_hex2bin(const char *src, size_t *len)
  205. {
  206. size_t slen;
  207. unsigned char *p;
  208. const unsigned char *s = (unsigned char *)src;
  209. static unsigned char buf[32];
  210. if (!src) {
  211. *len = 0;
  212. return NULL;
  213. }
  214. while (isspace(*src))
  215. src++;
  216. slen = strlen(src);
  217. if (slen > 64) {
  218. *len = 0;
  219. return NULL;
  220. }
  221. #define hex(c) \
  222. (c >= 'a' ? (c - 'a') : (c >= 'A' ? (c - 'A') : (c - '0')))
  223. for (p = buf, *len = 0;
  224. slen > 0 && isxdigit(s[0]) && isxdigit(s[1]);
  225. slen--, s += 2, (*len)++)
  226. *p++ = hex(s[0]) * 16 + hex(s[1]);
  227. return (char *)buf;
  228. }
  229. int rm_r(const char *path)
  230. {
  231. int ret = 0;
  232. DIR *dir;
  233. struct dirent *dent;
  234. if (path == NULL) {
  235. opkg_perror(ERROR, "Missing directory parameter");
  236. return -1;
  237. }
  238. dir = opendir(path);
  239. if (dir == NULL) {
  240. opkg_perror(ERROR, "Failed to open dir %s", path);
  241. return -1;
  242. }
  243. if (fchdir(dirfd(dir)) == -1) {
  244. opkg_perror(ERROR, "Failed to change to dir %s", path);
  245. closedir(dir);
  246. return -1;
  247. }
  248. while (1) {
  249. errno = 0;
  250. if ((dent = readdir(dir)) == NULL) {
  251. if (errno) {
  252. opkg_perror(ERROR, "Failed to read dir %s",
  253. path);
  254. ret = -1;
  255. }
  256. break;
  257. }
  258. if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
  259. continue;
  260. #ifdef _BSD_SOURCE
  261. if (dent->d_type == DT_DIR) {
  262. if ((ret = rm_r(dent->d_name)) == -1)
  263. break;
  264. continue;
  265. } else if (dent->d_type == DT_UNKNOWN)
  266. #endif
  267. {
  268. struct stat st;
  269. if ((ret = lstat(dent->d_name, &st)) == -1) {
  270. opkg_perror(ERROR, "Failed to lstat %s",
  271. dent->d_name);
  272. break;
  273. }
  274. if (S_ISDIR(st.st_mode)) {
  275. if ((ret = rm_r(dent->d_name)) == -1)
  276. break;
  277. continue;
  278. }
  279. }
  280. if ((ret = unlink(dent->d_name)) == -1) {
  281. opkg_perror(ERROR, "Failed to unlink %s", dent->d_name);
  282. break;
  283. }
  284. }
  285. if (chdir("..") == -1) {
  286. ret = -1;
  287. opkg_perror(ERROR, "Failed to change to dir %s/..", path);
  288. }
  289. if (rmdir(path) == -1) {
  290. ret = -1;
  291. opkg_perror(ERROR, "Failed to remove dir %s", path);
  292. }
  293. if (closedir(dir) == -1) {
  294. ret = -1;
  295. opkg_perror(ERROR, "Failed to close dir %s", path);
  296. }
  297. return ret;
  298. }