Remove dead code, sprintf_alloc() cannot fail. Opkg will exit instead.
[oweals/opkg-lede.git] / libopkg / file_util.c
1 /* file_util.c - convenience routines for common stat operations
2
3    Copyright (C) 2009 Ubiq Technologies <graham.gower@gmail.com>
4
5    Carl D. Worth
6    Copyright (C) 2001 University of Southern California
7
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.
12
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.
17 */
18
19 #include "config.h"
20
21 #include <stdio.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <dirent.h>
25 #include <unistd.h>
26
27 #include "sprintf_alloc.h"
28 #include "file_util.h"
29 #include "md5.h"
30 #include "libbb/libbb.h"
31
32 #if defined HAVE_SHA256
33 #include "sha256.h"
34 #endif
35
36 int
37 file_exists(const char *file_name)
38 {
39         struct stat st;
40
41         if (stat(file_name, &st) == -1)
42                 return 0;
43
44         return 1;
45 }
46
47 int
48 file_is_dir(const char *file_name)
49 {
50         struct stat st;
51
52         if (stat(file_name, &st) == -1)
53                 return 0;
54
55         return S_ISDIR(st.st_mode);
56 }
57
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.
62
63    Return value is NULL if the file is at EOF when called.
64 */
65 char *
66 file_read_line_alloc(FILE *fp)
67 {
68         char buf[BUFSIZ];
69         unsigned int buf_len;
70         char *line = NULL;
71         unsigned int line_size = 0;
72         int got_nl = 0;
73
74         buf[0] = '\0';
75
76         while (fgets(buf, BUFSIZ, fp)) {
77                 buf_len = strlen(buf);
78                 if (buf[buf_len - 1] == '\n') {
79                         buf_len--;
80                         buf[buf_len] = '\0';
81                         got_nl = 1;
82                 }
83                 if (line) {
84                         line_size += buf_len;
85                         line = xrealloc(line, line_size+1);
86                         strncat(line, buf, line_size);
87                 } else {
88                         line_size = buf_len + 1;
89                         line = xstrdup(buf);
90                 }
91                 if (got_nl)
92                         break;
93         }
94
95         return line;
96 }
97
98 int
99 file_move(const char *src, const char *dest)
100 {
101         int err;
102
103         err = rename(src, dest);
104         if (err == -1) {
105                 if (errno == EXDEV) {
106                         /* src & dest live on different file systems */
107                         err = file_copy(src, dest);
108                         if (err == 0)
109                                 unlink(src);
110                 } else {
111                         opkg_perror(ERROR, "Failed to rename %s to %s",
112                                 src, dest);
113                 }
114         }
115
116         return err;
117 }
118
119 int
120 file_copy(const char *src, const char *dest)
121 {
122         int err;
123
124         err = copy_file(src, dest, FILEUTILS_FORCE | FILEUTILS_PRESERVE_STATUS);
125         if (err)
126                 opkg_msg(ERROR, "Failed to copy file %s to %s.\n",
127                                 src, dest);
128
129         return err;
130 }
131
132 int
133 file_mkdir_hier(const char *path, long mode)
134 {
135         return make_directory(path, mode, FILEUTILS_RECUR);
136 }
137
138 char *file_md5sum_alloc(const char *file_name)
139 {
140     static const int md5sum_bin_len = 16;
141     static const int md5sum_hex_len = 32;
142
143     static const unsigned char bin2hex[16] = {
144         '0', '1', '2', '3',
145         '4', '5', '6', '7',
146         '8', '9', 'a', 'b',
147         'c', 'd', 'e', 'f'
148     };
149
150     int i, err;
151     FILE *file;
152     char *md5sum_hex;
153     unsigned char md5sum_bin[md5sum_bin_len];
154
155     md5sum_hex = xcalloc(1, md5sum_hex_len + 1);
156
157     file = fopen(file_name, "r");
158     if (file == NULL) {
159         opkg_perror(ERROR, "Failed to open file %s", file_name);
160         free(md5sum_hex);
161         return NULL;
162     }
163
164     err = md5_stream(file, md5sum_bin);
165     if (err) {
166         opkg_msg(ERROR, "Could't compute md5sum for %s.\n", file_name);
167         fclose(file);
168         free(md5sum_hex);
169         return NULL;
170     }
171
172     fclose(file);
173
174     for (i=0; i < md5sum_bin_len; i++) {
175         md5sum_hex[i*2] = bin2hex[md5sum_bin[i] >> 4];
176         md5sum_hex[i*2+1] = bin2hex[md5sum_bin[i] & 0xf];
177     }
178
179     md5sum_hex[md5sum_hex_len] = '\0';
180
181     return md5sum_hex;
182 }
183
184 #ifdef HAVE_SHA256
185 char *file_sha256sum_alloc(const char *file_name)
186 {
187     static const int sha256sum_bin_len = 32;
188     static const int sha256sum_hex_len = 64;
189
190     static const unsigned char bin2hex[16] = {
191         '0', '1', '2', '3',
192         '4', '5', '6', '7',
193         '8', '9', 'a', 'b',
194         'c', 'd', 'e', 'f'
195     };
196
197     int i, err;
198     FILE *file;
199     char *sha256sum_hex;
200     unsigned char sha256sum_bin[sha256sum_bin_len];
201
202     sha256sum_hex = xcalloc(1, sha256sum_hex_len + 1);
203
204     file = fopen(file_name, "r");
205     if (file == NULL) {
206         opkg_perror(ERROR, "Failed to open file %s", file_name);
207         free(sha256sum_hex);
208         return NULL;
209     }
210
211     err = sha256_stream(file, sha256sum_bin);
212     if (err) {
213         opkg_msg(ERROR, "Could't compute sha256sum for %s.\n", file_name);
214         fclose(file);
215         free(sha256sum_hex);
216         return NULL;
217     }
218
219     fclose(file);
220
221     for (i=0; i < sha256sum_bin_len; i++) {
222         sha256sum_hex[i*2] = bin2hex[sha256sum_bin[i] >> 4];
223         sha256sum_hex[i*2+1] = bin2hex[sha256sum_bin[i] & 0xf];
224     }
225
226     sha256sum_hex[sha256sum_hex_len] = '\0';
227
228     return sha256sum_hex;
229 }
230
231 #endif
232
233
234 int
235 rm_r(const char *path)
236 {
237         int ret = 0;
238         DIR *dir;
239         struct dirent *dent;
240
241         if (path == NULL) {
242                 opkg_perror(ERROR, "Missing directory parameter");
243                 return -1;
244         }
245
246         dir = opendir(path);
247         if (dir == NULL) {
248                 opkg_perror(ERROR, "Failed to open dir %s", path);
249                 return -1;
250         }
251
252         if (fchdir(dirfd(dir)) == -1) {
253                 opkg_perror(ERROR, "Failed to change to dir %s", path);
254                 closedir(dir);
255                 return -1;
256         }
257
258         while (1) {
259                 errno = 0;
260                 if ((dent = readdir(dir)) == NULL) {
261                         if (errno) {
262                                 opkg_perror(ERROR, "Failed to read dir %s",
263                                                 path);
264                                 ret = -1;
265                         }
266                         break;
267                 }
268
269                 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
270                         continue;
271
272 #ifdef _BSD_SOURCE
273                 if (dent->d_type == DT_DIR) {
274                         if ((ret = rm_r(dent->d_name)) == -1)
275                                 break;
276                         continue;
277                 } else if (dent->d_type == DT_UNKNOWN)
278 #endif
279                 {
280                         struct stat st;
281                         if ((ret = lstat(dent->d_name, &st)) == -1) {
282                                 opkg_perror(ERROR, "Failed to lstat %s",
283                                                 dent->d_name);
284                                 break;
285                         }
286                         if (S_ISDIR(st.st_mode)) {
287                                 if ((ret = rm_r(dent->d_name)) == -1)
288                                         break;
289                                 continue;
290                         }
291                 }
292
293                 if ((ret = unlink(dent->d_name)) == -1) {
294                         opkg_perror(ERROR, "Failed to unlink %s", dent->d_name);
295                         break;
296                 }
297         }
298
299         if (chdir("..") == -1) {
300                 ret = -1;
301                 opkg_perror(ERROR, "Failed to change to dir %s/..", path);
302         }
303
304         if (rmdir(path) == -1 ) {
305                 ret = -1;
306                 opkg_perror(ERROR, "Failed to remove dir %s", path);
307         }
308
309         if (closedir(dir) == -1) {
310                 ret = -1;
311                 opkg_perror(ERROR, "Failed to close dir %s", path);
312         }
313
314         return ret;
315 }