treewrite: use Lindent to reformat to kernel coding style
[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 #ifdef HAVE_MD5
30 #include "md5.h"
31 #endif
32 #include "libbb/libbb.h"
33
34 #if defined HAVE_SHA256
35 #include "sha256.h"
36 #endif
37
38 int file_exists(const char *file_name)
39 {
40         struct stat st;
41
42         if (stat(file_name, &st) == -1)
43                 return 0;
44
45         return 1;
46 }
47
48 int 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 *file_read_line_alloc(FILE * fp)
66 {
67         char buf[BUFSIZ];
68         unsigned int buf_len;
69         char *line = NULL;
70         unsigned int line_size = 0;
71         int got_nl = 0;
72
73         buf[0] = '\0';
74
75         while (fgets(buf, BUFSIZ, fp)) {
76                 buf_len = strlen(buf);
77                 if (buf[buf_len - 1] == '\n') {
78                         buf_len--;
79                         buf[buf_len] = '\0';
80                         got_nl = 1;
81                 }
82                 if (line) {
83                         line_size += buf_len;
84                         line = xrealloc(line, line_size + 1);
85                         strncat(line, buf, line_size);
86                 } else {
87                         line_size = buf_len + 1;
88                         line = xstrdup(buf);
89                 }
90                 if (got_nl)
91                         break;
92         }
93
94         return line;
95 }
96
97 int 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 file_copy(const char *src, const char *dest)
118 {
119         int err;
120
121         err = copy_file(src, dest, FILEUTILS_FORCE | FILEUTILS_PRESERVE_STATUS);
122         if (err)
123                 opkg_msg(ERROR, "Failed to copy file %s to %s.\n", src, dest);
124
125         return err;
126 }
127
128 int file_mkdir_hier(const char *path, long mode)
129 {
130         return make_directory(path, mode, FILEUTILS_RECUR);
131 }
132
133 #ifdef HAVE_MD5
134 char *file_md5sum_alloc(const char *file_name)
135 {
136         static const int md5sum_bin_len = 16;
137         static const int md5sum_hex_len = 32;
138
139         static const unsigned char bin2hex[16] = {
140                 '0', '1', '2', '3',
141                 '4', '5', '6', '7',
142                 '8', '9', 'a', 'b',
143                 'c', 'd', 'e', 'f'
144         };
145
146         int i, err;
147         FILE *file;
148         char *md5sum_hex;
149         unsigned char md5sum_bin[md5sum_bin_len];
150
151         md5sum_hex = xcalloc(1, md5sum_hex_len + 1);
152
153         file = fopen(file_name, "r");
154         if (file == NULL) {
155                 opkg_perror(ERROR, "Failed to open file %s", file_name);
156                 free(md5sum_hex);
157                 return NULL;
158         }
159
160         err = md5_stream(file, md5sum_bin);
161         if (err) {
162                 opkg_msg(ERROR, "Could't compute md5sum for %s.\n", file_name);
163                 fclose(file);
164                 free(md5sum_hex);
165                 return NULL;
166         }
167
168         fclose(file);
169
170         for (i = 0; i < md5sum_bin_len; i++) {
171                 md5sum_hex[i * 2] = bin2hex[md5sum_bin[i] >> 4];
172                 md5sum_hex[i * 2 + 1] = bin2hex[md5sum_bin[i] & 0xf];
173         }
174
175         md5sum_hex[md5sum_hex_len] = '\0';
176
177         return md5sum_hex;
178 }
179 #endif
180
181 #ifdef HAVE_SHA256
182 char *file_sha256sum_alloc(const char *file_name)
183 {
184         static const int sha256sum_bin_len = 32;
185         static const int sha256sum_hex_len = 64;
186
187         static const unsigned char bin2hex[16] = {
188                 '0', '1', '2', '3',
189                 '4', '5', '6', '7',
190                 '8', '9', 'a', 'b',
191                 'c', 'd', 'e', 'f'
192         };
193
194         int i, err;
195         FILE *file;
196         char *sha256sum_hex;
197         unsigned char sha256sum_bin[sha256sum_bin_len];
198
199         sha256sum_hex = xcalloc(1, sha256sum_hex_len + 1);
200
201         file = fopen(file_name, "r");
202         if (file == NULL) {
203                 opkg_perror(ERROR, "Failed to open file %s", file_name);
204                 free(sha256sum_hex);
205                 return NULL;
206         }
207
208         err = sha256_stream(file, sha256sum_bin);
209         if (err) {
210                 opkg_msg(ERROR, "Could't compute sha256sum for %s.\n",
211                          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 int rm_r(const char *path)
232 {
233         int ret = 0;
234         DIR *dir;
235         struct dirent *dent;
236
237         if (path == NULL) {
238                 opkg_perror(ERROR, "Missing directory parameter");
239                 return -1;
240         }
241
242         dir = opendir(path);
243         if (dir == NULL) {
244                 opkg_perror(ERROR, "Failed to open dir %s", path);
245                 return -1;
246         }
247
248         if (fchdir(dirfd(dir)) == -1) {
249                 opkg_perror(ERROR, "Failed to change to dir %s", path);
250                 closedir(dir);
251                 return -1;
252         }
253
254         while (1) {
255                 errno = 0;
256                 if ((dent = readdir(dir)) == NULL) {
257                         if (errno) {
258                                 opkg_perror(ERROR, "Failed to read dir %s",
259                                             path);
260                                 ret = -1;
261                         }
262                         break;
263                 }
264
265                 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
266                         continue;
267
268 #ifdef _BSD_SOURCE
269                 if (dent->d_type == DT_DIR) {
270                         if ((ret = rm_r(dent->d_name)) == -1)
271                                 break;
272                         continue;
273                 } else if (dent->d_type == DT_UNKNOWN)
274 #endif
275                 {
276                         struct stat st;
277                         if ((ret = lstat(dent->d_name, &st)) == -1) {
278                                 opkg_perror(ERROR, "Failed to lstat %s",
279                                             dent->d_name);
280                                 break;
281                         }
282                         if (S_ISDIR(st.st_mode)) {
283                                 if ((ret = rm_r(dent->d_name)) == -1)
284                                         break;
285                                 continue;
286                         }
287                 }
288
289                 if ((ret = unlink(dent->d_name)) == -1) {
290                         opkg_perror(ERROR, "Failed to unlink %s", dent->d_name);
291                         break;
292                 }
293         }
294
295         if (chdir("..") == -1) {
296                 ret = -1;
297                 opkg_perror(ERROR, "Failed to change to dir %s/..", path);
298         }
299
300         if (rmdir(path) == -1) {
301                 ret = -1;
302                 opkg_perror(ERROR, "Failed to remove dir %s", path);
303         }
304
305         if (closedir(dir) == -1) {
306                 ret = -1;
307                 opkg_perror(ERROR, "Failed to close dir %s", path);
308         }
309
310         return ret;
311 }