1 /* file_util.c - convenience routines for common stat operations
3 Copyright (C) 2009 Ubiq Technologies <graham.gower@gmail.com>
6 Copyright (C) 2001 University of Southern California
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2, or (at
11 your option) any later version.
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
20 #include <sys/types.h>
26 #include "sprintf_alloc.h"
27 #include "file_util.h"
28 #include <libubox/md5.h>
29 #include "libbb/libbb.h"
33 int file_exists(const char *file_name)
37 if (stat(file_name, &st) == -1)
43 int file_is_dir(const char *file_name)
47 if (stat(file_name, &st) == -1)
50 return S_ISDIR(st.st_mode);
53 /* read a single line from a file, stopping at a newline or EOF.
54 If a newline is read, it will appear in the resulting string.
55 Return value is a malloc'ed char * which should be freed at
56 some point by the caller.
58 Return value is NULL if the file is at EOF when called.
60 char *file_read_line_alloc(FILE * fp)
65 unsigned int line_size = 0;
70 while (fgets(buf, BUFSIZ, fp)) {
71 buf_len = strlen(buf);
72 if (buf[buf_len - 1] == '\n') {
79 line = xrealloc(line, line_size + 1);
80 strncat(line, buf, line_size);
82 line_size = buf_len + 1;
92 int file_move(const char *src, const char *dest)
96 err = rename(src, dest);
99 /* src & dest live on different file systems */
100 err = file_copy(src, dest);
104 opkg_perror(ERROR, "Failed to rename %s to %s",
112 int file_copy(const char *src, const char *dest)
116 err = copy_file(src, dest, FILEUTILS_FORCE | FILEUTILS_PRESERVE_STATUS);
118 opkg_msg(ERROR, "Failed to copy file %s to %s.\n", src, dest);
123 int file_mkdir_hier(const char *path, long mode)
125 return make_directory(path, mode, FILEUTILS_RECUR);
128 char *file_md5sum_alloc(const char *file_name)
130 static const int md5sum_bin_len = 16;
131 static const int md5sum_hex_len = 32;
133 static const unsigned char bin2hex[16] = {
142 unsigned char md5sum_bin[md5sum_bin_len];
144 len = md5sum(file_name, md5sum_bin);
147 opkg_msg(ERROR, "Could't compute md5sum for %s.\n", file_name);
151 md5sum_hex = xcalloc(1, md5sum_hex_len + 1);
153 for (i = 0; i < md5sum_bin_len; i++) {
154 md5sum_hex[i * 2] = bin2hex[md5sum_bin[i] >> 4];
155 md5sum_hex[i * 2 + 1] = bin2hex[md5sum_bin[i] & 0xf];
158 md5sum_hex[md5sum_hex_len] = '\0';
163 char *file_sha256sum_alloc(const char *file_name)
165 static const int sha256sum_bin_len = 32;
166 static const int sha256sum_hex_len = 64;
168 static const unsigned char bin2hex[16] = {
178 unsigned char sha256sum_bin[sha256sum_bin_len];
180 sha256sum_hex = xcalloc(1, sha256sum_hex_len + 1);
182 file = fopen(file_name, "r");
184 opkg_perror(ERROR, "Failed to open file %s", file_name);
189 err = sha256_stream(file, sha256sum_bin);
191 opkg_msg(ERROR, "Could't compute sha256sum for %s.\n",
200 for (i = 0; i < sha256sum_bin_len; i++) {
201 sha256sum_hex[i * 2] = bin2hex[sha256sum_bin[i] >> 4];
202 sha256sum_hex[i * 2 + 1] = bin2hex[sha256sum_bin[i] & 0xf];
205 sha256sum_hex[sha256sum_hex_len] = '\0';
207 return sha256sum_hex;
210 char *checksum_bin2hex(const char *src, size_t len)
213 static unsigned char buf[65];
214 const unsigned char *s = (unsigned char *)src;
215 static const unsigned char bin2hex[16] = {
225 for (p = buf; len > 0; s++, len--) {
226 *p++ = bin2hex[*s / 16];
227 *p++ = bin2hex[*s % 16];
235 char *checksum_hex2bin(const char *src, size_t *len)
239 const unsigned char *s = (unsigned char *)src;
240 static unsigned char buf[32];
247 while (isspace(*src))
258 (c >= 'a' ? (c - 'a') : (c >= 'A' ? (c - 'A') : (c - '0')))
260 for (p = buf, *len = 0;
261 slen > 0 && isxdigit(s[0]) && isxdigit(s[1]);
262 slen--, s += 2, (*len)++)
263 *p++ = hex(s[0]) * 16 + hex(s[1]);
268 int rm_r(const char *path)
275 opkg_perror(ERROR, "Missing directory parameter");
281 opkg_perror(ERROR, "Failed to open dir %s", path);
285 if (fchdir(dirfd(dir)) == -1) {
286 opkg_perror(ERROR, "Failed to change to dir %s", path);
293 if ((dent = readdir(dir)) == NULL) {
295 opkg_perror(ERROR, "Failed to read dir %s",
302 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
306 if (dent->d_type == DT_DIR) {
307 if ((ret = rm_r(dent->d_name)) == -1)
310 } else if (dent->d_type == DT_UNKNOWN)
314 if ((ret = lstat(dent->d_name, &st)) == -1) {
315 opkg_perror(ERROR, "Failed to lstat %s",
319 if (S_ISDIR(st.st_mode)) {
320 if ((ret = rm_r(dent->d_name)) == -1)
326 if ((ret = unlink(dent->d_name)) == -1) {
327 opkg_perror(ERROR, "Failed to unlink %s", dent->d_name);
332 if (chdir("..") == -1) {
334 opkg_perror(ERROR, "Failed to change to dir %s/..", path);
337 if (rmdir(path) == -1) {
339 opkg_perror(ERROR, "Failed to remove dir %s", path);
342 if (closedir(dir) == -1) {
344 opkg_perror(ERROR, "Failed to close dir %s", path);