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