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>
22 #include "sprintf_alloc.h"
23 #include "file_util.h"
25 #include "libbb/libbb.h"
28 #if defined HAVE_SHA256
32 int file_exists(const char *file_name)
37 err = stat(file_name, &stat_buf);
45 int file_is_dir(const char *file_name)
50 err = stat(file_name, &stat_buf);
55 return S_ISDIR(stat_buf.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 #define FILE_READ_LINE_BUF_SIZE 1024
66 char *file_read_line_alloc(FILE *file)
68 char buf[FILE_READ_LINE_BUF_SIZE];
73 memset(buf, 0, FILE_READ_LINE_BUF_SIZE);
74 while (fgets(buf, FILE_READ_LINE_BUF_SIZE, file)) {
75 buf_len = strlen(buf);
78 line = xrealloc(line, line_size);
81 line_size = buf_len + 1;
84 if (buf[buf_len - 1] == '\n') {
92 int file_move(const char *src, const char *dest)
96 err = rename(src, dest);
98 if (err && errno == EXDEV) {
99 err = file_copy(src, dest);
102 fprintf(stderr, "%s: ERROR: failed to rename %s to %s: %s\n",
103 __FUNCTION__, src, dest, strerror(errno));
109 /* I put these here to keep libbb dependencies from creeping all over
111 int file_copy(const char *src, const char *dest)
115 err = copy_file(src, dest, FILEUTILS_FORCE | FILEUTILS_PRESERVE_STATUS);
117 fprintf(stderr, "%s: ERROR: failed to copy %s to %s\n",
118 __FUNCTION__, src, dest);
124 int file_mkdir_hier(const char *path, long mode)
126 return make_directory(path, mode, FILEUTILS_RECUR);
129 char *file_md5sum_alloc(const char *file_name)
131 static const int md5sum_bin_len = 16;
132 static const int md5sum_hex_len = 32;
134 static const unsigned char bin2hex[16] = {
144 unsigned char md5sum_bin[md5sum_bin_len];
146 md5sum_hex = xcalloc(1, md5sum_hex_len + 1);
148 file = fopen(file_name, "r");
150 fprintf(stderr, "%s: Failed to open file %s: %s\n",
151 __FUNCTION__, file_name, strerror(errno));
156 err = md5_stream(file, md5sum_bin);
158 fprintf(stderr, "%s: ERROR computing md5sum for %s: %s\n",
159 __FUNCTION__, file_name, strerror(err));
167 for (i=0; i < md5sum_bin_len; i++) {
168 md5sum_hex[i*2] = bin2hex[md5sum_bin[i] >> 4];
169 md5sum_hex[i*2+1] = bin2hex[md5sum_bin[i] & 0xf];
172 md5sum_hex[md5sum_hex_len] = '\0';
178 char *file_sha256sum_alloc(const char *file_name)
180 static const int sha256sum_bin_len = 32;
181 static const int sha256sum_hex_len = 64;
183 static const unsigned char bin2hex[16] = {
193 unsigned char sha256sum_bin[sha256sum_bin_len];
195 sha256sum_hex = xcalloc(1, sha256sum_hex_len + 1);
197 file = fopen(file_name, "r");
199 fprintf(stderr, "%s: Failed to open file %s: %s\n",
200 __FUNCTION__, file_name, strerror(errno));
205 err = sha256_stream(file, sha256sum_bin);
207 fprintf(stderr, "%s: ERROR computing sha256sum for %s: %s\n",
208 __FUNCTION__, file_name, strerror(err));
216 for (i=0; i < sha256sum_bin_len; i++) {
217 sha256sum_hex[i*2] = bin2hex[sha256sum_bin[i] >> 4];
218 sha256sum_hex[i*2+1] = bin2hex[sha256sum_bin[i] & 0xf];
221 sha256sum_hex[sha256sum_hex_len] = '\0';
223 return sha256sum_hex;