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.
22 #include <sys/types.h>
27 #include "sprintf_alloc.h"
28 #include "file_util.h"
32 #include "libbb/libbb.h"
34 #if defined HAVE_SHA256
38 int file_exists(const char *file_name)
42 if (stat(file_name, &st) == -1)
48 int file_is_dir(const char *file_name)
52 if (stat(file_name, &st) == -1)
55 return S_ISDIR(st.st_mode);
58 /* read a single line from a file, stopping at a newline or EOF.
59 If a newline is read, it will appear in the resulting string.
60 Return value is a malloc'ed char * which should be freed at
61 some point by the caller.
63 Return value is NULL if the file is at EOF when called.
65 char *file_read_line_alloc(FILE * fp)
70 unsigned int line_size = 0;
75 while (fgets(buf, BUFSIZ, fp)) {
76 buf_len = strlen(buf);
77 if (buf[buf_len - 1] == '\n') {
84 line = xrealloc(line, line_size + 1);
85 strncat(line, buf, line_size);
87 line_size = buf_len + 1;
97 int file_move(const char *src, const char *dest)
101 err = rename(src, dest);
103 if (errno == EXDEV) {
104 /* src & dest live on different file systems */
105 err = file_copy(src, dest);
109 opkg_perror(ERROR, "Failed to rename %s to %s",
117 int file_copy(const char *src, const char *dest)
121 err = copy_file(src, dest, FILEUTILS_FORCE | FILEUTILS_PRESERVE_STATUS);
123 opkg_msg(ERROR, "Failed to copy file %s to %s.\n", src, dest);
128 int file_mkdir_hier(const char *path, long mode)
130 return make_directory(path, mode, FILEUTILS_RECUR);
134 char *file_md5sum_alloc(const char *file_name)
136 static const int md5sum_bin_len = 16;
137 static const int md5sum_hex_len = 32;
139 static const unsigned char bin2hex[16] = {
149 unsigned char md5sum_bin[md5sum_bin_len];
151 md5sum_hex = xcalloc(1, md5sum_hex_len + 1);
153 file = fopen(file_name, "r");
155 opkg_perror(ERROR, "Failed to open file %s", file_name);
160 err = md5_stream(file, md5sum_bin);
162 opkg_msg(ERROR, "Could't compute md5sum for %s.\n", file_name);
170 for (i = 0; i < md5sum_bin_len; i++) {
171 md5sum_hex[i * 2] = bin2hex[md5sum_bin[i] >> 4];
172 md5sum_hex[i * 2 + 1] = bin2hex[md5sum_bin[i] & 0xf];
175 md5sum_hex[md5sum_hex_len] = '\0';
182 char *file_sha256sum_alloc(const char *file_name)
184 static const int sha256sum_bin_len = 32;
185 static const int sha256sum_hex_len = 64;
187 static const unsigned char bin2hex[16] = {
197 unsigned char sha256sum_bin[sha256sum_bin_len];
199 sha256sum_hex = xcalloc(1, sha256sum_hex_len + 1);
201 file = fopen(file_name, "r");
203 opkg_perror(ERROR, "Failed to open file %s", file_name);
208 err = sha256_stream(file, sha256sum_bin);
210 opkg_msg(ERROR, "Could't compute sha256sum for %s.\n",
219 for (i = 0; i < sha256sum_bin_len; i++) {
220 sha256sum_hex[i * 2] = bin2hex[sha256sum_bin[i] >> 4];
221 sha256sum_hex[i * 2 + 1] = bin2hex[sha256sum_bin[i] & 0xf];
224 sha256sum_hex[sha256sum_hex_len] = '\0';
226 return sha256sum_hex;
231 int rm_r(const char *path)
238 opkg_perror(ERROR, "Missing directory parameter");
244 opkg_perror(ERROR, "Failed to open dir %s", path);
248 if (fchdir(dirfd(dir)) == -1) {
249 opkg_perror(ERROR, "Failed to change to dir %s", path);
256 if ((dent = readdir(dir)) == NULL) {
258 opkg_perror(ERROR, "Failed to read dir %s",
265 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
269 if (dent->d_type == DT_DIR) {
270 if ((ret = rm_r(dent->d_name)) == -1)
273 } else if (dent->d_type == DT_UNKNOWN)
277 if ((ret = lstat(dent->d_name, &st)) == -1) {
278 opkg_perror(ERROR, "Failed to lstat %s",
282 if (S_ISDIR(st.st_mode)) {
283 if ((ret = rm_r(dent->d_name)) == -1)
289 if ((ret = unlink(dent->d_name)) == -1) {
290 opkg_perror(ERROR, "Failed to unlink %s", dent->d_name);
295 if (chdir("..") == -1) {
297 opkg_perror(ERROR, "Failed to change to dir %s/..", path);
300 if (rmdir(path) == -1) {
302 opkg_perror(ERROR, "Failed to remove dir %s", path);
305 if (closedir(dir) == -1) {
307 opkg_perror(ERROR, "Failed to close dir %s", path);