1 /* file_util.c - convenience routines for common stat operations
5 Copyright (C) 2001 University of Southern California
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2, or (at
10 your option) any later version.
12 This program is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
19 #include <sys/types.h>
23 #include "sprintf_alloc.h"
24 #include "file_util.h"
26 #include "libbb/libbb.h"
29 #if defined HAVE_SHA256
34 file_exists(const char *file_name)
38 if (stat(file_name, &st) == -1)
45 file_is_dir(const char *file_name)
49 if (stat(file_name, &st) == -1)
52 return S_ISDIR(st.st_mode);
55 /* read a single line from a file, stopping at a newline or EOF.
56 If a newline is read, it will appear in the resulting string.
57 Return value is a malloc'ed char * which should be freed at
58 some point by the caller.
60 Return value is NULL if the file is at EOF when called.
63 file_read_line_alloc(FILE *fp)
73 while (fgets(buf, BUFSIZ, fp)) {
74 buf_len = strlen(buf);
75 if (buf[buf_len - 1] == '\n') {
82 line = xrealloc(line, line_size+1);
83 strncat(line, buf, line_size);
85 line_size = buf_len + 1;
96 file_move(const char *src, const char *dest)
100 err = rename(src, dest);
102 if (errno == EXDEV) {
103 /* src & dest live on different file systems */
104 err = file_copy(src, dest);
108 fprintf(stderr, "%s: rename(%s, %s): %s\n",
109 __FUNCTION__, src, dest, strerror(errno));
117 file_copy(const char *src, const char *dest)
121 err = copy_file(src, dest, FILEUTILS_FORCE | FILEUTILS_PRESERVE_STATUS);
123 fprintf(stderr, "%s: copy_file(%s, %s)\n",
124 __FUNCTION__, src, dest);
130 file_mkdir_hier(const char *path, long mode)
132 return make_directory(path, mode, FILEUTILS_RECUR);
135 char *file_md5sum_alloc(const char *file_name)
137 static const int md5sum_bin_len = 16;
138 static const int md5sum_hex_len = 32;
140 static const unsigned char bin2hex[16] = {
150 unsigned char md5sum_bin[md5sum_bin_len];
152 md5sum_hex = xcalloc(1, md5sum_hex_len + 1);
154 file = fopen(file_name, "r");
156 fprintf(stderr, "%s: Failed to open file %s: %s\n",
157 __FUNCTION__, file_name, strerror(errno));
162 err = md5_stream(file, md5sum_bin);
164 fprintf(stderr, "%s: ERROR computing md5sum for %s: %s\n",
165 __FUNCTION__, file_name, strerror(err));
173 for (i=0; i < md5sum_bin_len; i++) {
174 md5sum_hex[i*2] = bin2hex[md5sum_bin[i] >> 4];
175 md5sum_hex[i*2+1] = bin2hex[md5sum_bin[i] & 0xf];
178 md5sum_hex[md5sum_hex_len] = '\0';
184 char *file_sha256sum_alloc(const char *file_name)
186 static const int sha256sum_bin_len = 32;
187 static const int sha256sum_hex_len = 64;
189 static const unsigned char bin2hex[16] = {
199 unsigned char sha256sum_bin[sha256sum_bin_len];
201 sha256sum_hex = xcalloc(1, sha256sum_hex_len + 1);
203 file = fopen(file_name, "r");
205 fprintf(stderr, "%s: Failed to open file %s: %s\n",
206 __FUNCTION__, file_name, strerror(errno));
211 err = sha256_stream(file, sha256sum_bin);
213 fprintf(stderr, "%s: ERROR computing sha256sum for %s: %s\n",
214 __FUNCTION__, file_name, strerror(err));
222 for (i=0; i < sha256sum_bin_len; i++) {
223 sha256sum_hex[i*2] = bin2hex[sha256sum_bin[i] >> 4];
224 sha256sum_hex[i*2+1] = bin2hex[sha256sum_bin[i] & 0xf];
227 sha256sum_hex[sha256sum_hex_len] = '\0';
229 return sha256sum_hex;
236 rm_r(const char *path)
244 perror_msg("%s: opendir(%s)", __FUNCTION__, path);
248 if (fchdir(dirfd(dir)) == -1) {
249 perror_msg("%s: fchdir(%s)", __FUNCTION__, path);
256 if ((dent = readdir(dir)) == NULL) {
258 perror_msg("%s: readdir(%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 perror_msg("%s: lstat(%s)",
279 __FUNCTION__, dent->d_name);
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 perror_msg("%s: unlink(%s)",
291 __FUNCTION__, dent->d_name);
296 if (chdir("..") == -1) {
298 perror_msg("%s: chdir(%s/..)", __FUNCTION__, path);
301 if (rmdir(path) == -1 ) {
303 perror_msg("%s: rmdir(%s)", __FUNCTION__, path);
306 if (closedir(dir) == -1) {
308 perror_msg("%s: closedir(%s)", __FUNCTION__, path);