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