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)
62 size_t buf_len, line_size;
67 while (fgets(buf, BUFSIZ, fp)) {
68 buf_len = strlen(buf);
69 if (buf_len > 0 && buf[buf_len - 1] == '\n') {
76 line = xrealloc(line, line_size + 1);
77 strncat(line, buf, line_size);
79 line_size = buf_len + 1;
89 int file_move(const char *src, const char *dest)
93 err = rename(src, dest);
96 /* src & dest live on different file systems */
97 err = file_copy(src, dest);
101 opkg_perror(ERROR, "Failed to rename %s to %s",
109 int file_copy(const char *src, const char *dest)
113 err = copy_file(src, dest, FILEUTILS_FORCE | FILEUTILS_PRESERVE_STATUS);
115 opkg_msg(ERROR, "Failed to copy file %s to %s.\n", src, dest);
120 int file_mkdir_hier(const char *path, long mode)
122 return make_directory(path, mode, FILEUTILS_RECUR);
126 static int hex2bin(unsigned char x)
128 if (x >= 'a' && x <= 'f')
130 else if (x >= 'A' && x <= 'F')
132 else if (x >= '0' && x <= '9')
138 static const unsigned char bin2hex[16] = {
145 char *file_md5sum_alloc(const char *file_name)
147 static const int md5sum_bin_len = 16;
148 static const int md5sum_hex_len = 32;
152 unsigned char md5sum_bin[md5sum_bin_len];
154 len = md5sum(file_name, md5sum_bin);
157 opkg_msg(ERROR, "Could't compute md5sum for %s.\n", file_name);
161 md5sum_hex = xcalloc(1, md5sum_hex_len + 1);
163 for (i = 0; i < md5sum_bin_len; i++) {
164 md5sum_hex[i * 2] = bin2hex[md5sum_bin[i] >> 4];
165 md5sum_hex[i * 2 + 1] = bin2hex[md5sum_bin[i] & 0xf];
168 md5sum_hex[md5sum_hex_len] = '\0';
173 char *file_sha256sum_alloc(const char *file_name)
175 static const int sha256sum_bin_len = 32;
176 static const int sha256sum_hex_len = 64;
181 unsigned char sha256sum_bin[sha256sum_bin_len];
183 sha256sum_hex = xcalloc(1, sha256sum_hex_len + 1);
185 file = fopen(file_name, "r");
187 opkg_perror(ERROR, "Failed to open file %s", file_name);
192 err = sha256_stream(file, sha256sum_bin);
194 opkg_msg(ERROR, "Could't compute sha256sum for %s.\n",
203 for (i = 0; i < sha256sum_bin_len; i++) {
204 sha256sum_hex[i * 2] = bin2hex[sha256sum_bin[i] >> 4];
205 sha256sum_hex[i * 2 + 1] = bin2hex[sha256sum_bin[i] & 0xf];
208 sha256sum_hex[sha256sum_hex_len] = '\0';
210 return sha256sum_hex;
213 char *checksum_bin2hex(const char *src, size_t len)
216 static unsigned char buf[65];
217 const unsigned char *s = (unsigned char *)src;
221 for (p = buf; len > 0; s++, len--) {
222 *p++ = bin2hex[*s / 16];
223 *p++ = bin2hex[*s % 16];
231 char *checksum_hex2bin(const char *src, size_t *len)
233 static unsigned char buf[32];
241 while (isspace(*src))
244 if (strlen(src) > sizeof(buf) * 2)
248 if (n >= sizeof(buf) || !isxdigit(src[0]) || !isxdigit(src[1]))
251 buf[n++] = hex2bin(src[0]) * 16 + hex2bin(src[1]);
256 return n ? (char *)buf : NULL;
259 int rm_r(const char *path)
266 opkg_perror(ERROR, "Missing directory parameter");
272 opkg_perror(ERROR, "Failed to open dir %s", path);
276 if (fchdir(dirfd(dir)) == -1) {
277 opkg_perror(ERROR, "Failed to change to dir %s", path);
284 if ((dent = readdir(dir)) == NULL) {
286 opkg_perror(ERROR, "Failed to read dir %s",
293 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
297 if (dent->d_type == DT_DIR) {
298 if ((ret = rm_r(dent->d_name)) == -1)
301 } else if (dent->d_type == DT_UNKNOWN)
305 if ((ret = lstat(dent->d_name, &st)) == -1) {
306 opkg_perror(ERROR, "Failed to lstat %s",
310 if (S_ISDIR(st.st_mode)) {
311 if ((ret = rm_r(dent->d_name)) == -1)
317 if ((ret = unlink(dent->d_name)) == -1) {
318 opkg_perror(ERROR, "Failed to unlink %s", dent->d_name);
323 if (chdir("..") == -1) {
325 opkg_perror(ERROR, "Failed to change to dir %s/..", path);
328 if (rmdir(path) == -1) {
330 opkg_perror(ERROR, "Failed to remove dir %s", path);
333 if (closedir(dir) == -1) {
335 opkg_perror(ERROR, "Failed to close dir %s", path);
341 static int urlencode_is_specialchar(char c)
369 char *urlencode_path(const char *filename)
372 const unsigned char *in;
373 unsigned char *copy, *out;
375 for (in = (unsigned char *)filename; *in != 0; in++)
376 len += urlencode_is_specialchar(*in) ? 3 : 1;
378 copy = xcalloc(1, len + 1);
380 for (in = (unsigned char *)filename, out = copy; *in != 0; in++) {
381 if (urlencode_is_specialchar(*in)) {
383 *out++ = bin2hex[*in / 16];
384 *out++ = bin2hex[*in % 16];
394 char *urldecode_path(const char *filename)
396 unsigned char *copy = (unsigned char *)xstrdup(filename);
397 unsigned char *in, *out;
399 for (in = copy, out = copy; *in != 0; in++) {
400 if (*in == '%' && isxdigit(in[1]) && isxdigit(in[2])) {
401 *out++ = hex2bin(in[1]) * 16 + hex2bin(in[2]);