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>
24 #include "sprintf_alloc.h"
25 #include "file_util.h"
27 #include "libbb/libbb.h"
30 #if defined HAVE_SHA256
35 file_exists(const char *file_name)
39 if (stat(file_name, &st) == -1)
46 file_is_dir(const char *file_name)
50 if (stat(file_name, &st) == -1)
53 return S_ISDIR(st.st_mode);
56 /* read a single line from a file, stopping at a newline or EOF.
57 If a newline is read, it will appear in the resulting string.
58 Return value is a malloc'ed char * which should be freed at
59 some point by the caller.
61 Return value is NULL if the file is at EOF when called.
64 file_read_line_alloc(FILE *fp)
69 unsigned int line_size = 0;
74 while (fgets(buf, BUFSIZ, fp)) {
75 buf_len = strlen(buf);
76 if (buf[buf_len - 1] == '\n') {
83 line = xrealloc(line, line_size+1);
84 strncat(line, buf, line_size);
86 line_size = buf_len + 1;
97 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",
118 file_copy(const char *src, const char *dest)
122 err = copy_file(src, dest, FILEUTILS_FORCE | FILEUTILS_PRESERVE_STATUS);
124 opkg_msg(ERROR, "Failed to copy file %s to %s.\n",
131 file_mkdir_hier(const char *path, long mode)
133 return make_directory(path, mode, FILEUTILS_RECUR);
136 char *file_md5sum_alloc(const char *file_name)
138 static const int md5sum_bin_len = 16;
139 static const int md5sum_hex_len = 32;
141 static const unsigned char bin2hex[16] = {
151 unsigned char md5sum_bin[md5sum_bin_len];
153 md5sum_hex = xcalloc(1, md5sum_hex_len + 1);
155 file = fopen(file_name, "r");
157 opkg_perror(ERROR, "Failed to open file %s", file_name);
162 err = md5_stream(file, md5sum_bin);
164 opkg_msg(ERROR, "Could't compute md5sum for %s.\n", file_name);
172 for (i=0; i < md5sum_bin_len; i++) {
173 md5sum_hex[i*2] = bin2hex[md5sum_bin[i] >> 4];
174 md5sum_hex[i*2+1] = bin2hex[md5sum_bin[i] & 0xf];
177 md5sum_hex[md5sum_hex_len] = '\0';
183 char *file_sha256sum_alloc(const char *file_name)
185 static const int sha256sum_bin_len = 32;
186 static const int sha256sum_hex_len = 64;
188 static const unsigned char bin2hex[16] = {
198 unsigned char sha256sum_bin[sha256sum_bin_len];
200 sha256sum_hex = xcalloc(1, sha256sum_hex_len + 1);
202 file = fopen(file_name, "r");
204 opkg_perror(ERROR, "Failed to open file %s", file_name);
209 err = sha256_stream(file, sha256sum_bin);
211 opkg_msg(ERROR, "Could't compute sha256sum for %s.\n", file_name);
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;
233 rm_r(const char *path)
241 opkg_perror(ERROR, "Failed to open dir %s", path);
245 if (fchdir(dirfd(dir)) == -1) {
246 opkg_perror(ERROR, "Failed to change to dir %s", path);
253 if ((dent = readdir(dir)) == NULL) {
255 opkg_perror(ERROR, "Failed to read dir %s",
262 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
266 if (dent->d_type == DT_DIR) {
267 if ((ret = rm_r(dent->d_name)) == -1)
270 } else if (dent->d_type == DT_UNKNOWN)
274 if ((ret = lstat(dent->d_name, &st)) == -1) {
275 opkg_perror(ERROR, "Failed to lstat %s",
279 if (S_ISDIR(st.st_mode)) {
280 if ((ret = rm_r(dent->d_name)) == -1)
286 if ((ret = unlink(dent->d_name)) == -1) {
287 opkg_perror(ERROR, "Failed to unlink %s", dent->d_name);
292 if (chdir("..") == -1) {
294 opkg_perror(ERROR, "Failed to change to dir %s/..", path);
297 if (rmdir(path) == -1 ) {
299 opkg_perror(ERROR, "Failed to remove dir %s", path);
302 if (closedir(dir) == -1) {
304 opkg_perror(ERROR, "Failed to close dir %s", path);