libopkg: fix pkg_set_ptr() to properly set NULL pointers
[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 #include <ctype.h>
27
28 #include "sprintf_alloc.h"
29 #include "file_util.h"
30 #ifdef HAVE_MD5
31 #include "md5.h"
32 #endif
33 #include "libbb/libbb.h"
34
35 #if defined HAVE_SHA256
36 #include "sha256.h"
37 #endif
38
39 int file_exists(const char *file_name)
40 {
41         struct stat st;
42
43         if (stat(file_name, &st) == -1)
44                 return 0;
45
46         return 1;
47 }
48
49 int file_is_dir(const char *file_name)
50 {
51         struct stat st;
52
53         if (stat(file_name, &st) == -1)
54                 return 0;
55
56         return S_ISDIR(st.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 char *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 file_move(const char *src, const char *dest)
99 {
100         int err;
101
102         err = rename(src, dest);
103         if (err == -1) {
104                 if (errno == EXDEV) {
105                         /* src & dest live on different file systems */
106                         err = file_copy(src, dest);
107                         if (err == 0)
108                                 unlink(src);
109                 } else {
110                         opkg_perror(ERROR, "Failed to rename %s to %s",
111                                     src, dest);
112                 }
113         }
114
115         return err;
116 }
117
118 int 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", src, dest);
125
126         return err;
127 }
128
129 int file_mkdir_hier(const char *path, long mode)
130 {
131         return make_directory(path, mode, FILEUTILS_RECUR);
132 }
133
134 #ifdef HAVE_MD5
135 char *file_md5sum_alloc(const char *file_name)
136 {
137         static const int md5sum_bin_len = 16;
138         static const int md5sum_hex_len = 32;
139
140         static const unsigned char bin2hex[16] = {
141                 '0', '1', '2', '3',
142                 '4', '5', '6', '7',
143                 '8', '9', 'a', 'b',
144                 'c', 'd', 'e', 'f'
145         };
146
147         int i, err;
148         FILE *file;
149         char *md5sum_hex;
150         unsigned char md5sum_bin[md5sum_bin_len];
151
152         md5sum_hex = xcalloc(1, md5sum_hex_len + 1);
153
154         file = fopen(file_name, "r");
155         if (file == NULL) {
156                 opkg_perror(ERROR, "Failed to open file %s", file_name);
157                 free(md5sum_hex);
158                 return NULL;
159         }
160
161         err = md5_stream(file, md5sum_bin);
162         if (err) {
163                 opkg_msg(ERROR, "Could't compute md5sum for %s.\n", file_name);
164                 fclose(file);
165                 free(md5sum_hex);
166                 return NULL;
167         }
168
169         fclose(file);
170
171         for (i = 0; i < md5sum_bin_len; i++) {
172                 md5sum_hex[i * 2] = bin2hex[md5sum_bin[i] >> 4];
173                 md5sum_hex[i * 2 + 1] = bin2hex[md5sum_bin[i] & 0xf];
174         }
175
176         md5sum_hex[md5sum_hex_len] = '\0';
177
178         return md5sum_hex;
179 }
180 #endif
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",
212                          file_name);
213                 fclose(file);
214                 free(sha256sum_hex);
215                 return NULL;
216         }
217
218         fclose(file);
219
220         for (i = 0; i < sha256sum_bin_len; i++) {
221                 sha256sum_hex[i * 2] = bin2hex[sha256sum_bin[i] >> 4];
222                 sha256sum_hex[i * 2 + 1] = bin2hex[sha256sum_bin[i] & 0xf];
223         }
224
225         sha256sum_hex[sha256sum_hex_len] = '\0';
226
227         return sha256sum_hex;
228 }
229
230 #endif
231
232 char *checksum_bin2hex(const char *src, size_t len)
233 {
234         unsigned char *p;
235         static unsigned char buf[65];
236         const unsigned char *s = (unsigned char *)src;
237         static const unsigned char bin2hex[16] = {
238                 '0', '1', '2', '3',
239                 '4', '5', '6', '7',
240                 '8', '9', 'a', 'b',
241                 'c', 'd', 'e', 'f'
242         };
243
244         if (!s || len > 32)
245                 return NULL;
246
247         for (p = buf; len > 0; s++, len--) {
248                 *p++ = bin2hex[*s / 16];
249                 *p++ = bin2hex[*s % 16];
250         }
251
252         *p = 0;
253
254         return (char *)buf;
255 }
256
257 char *checksum_hex2bin(const char *src, size_t *len)
258 {
259         size_t slen;
260         unsigned char *p;
261         const unsigned char *s = (unsigned char *)src;
262         static unsigned char buf[32];
263
264         if (!src) {
265                 *len = 0;
266                 return NULL;
267         }
268
269         while (isspace(*src))
270                 src++;
271
272         slen = strlen(src);
273
274         if (slen > 64) {
275                 *len = 0;
276                 return NULL;
277         }
278
279 #define hex(c) \
280         (c >= 'a' ? (c - 'a') : (c >= 'A' ? (c - 'A') : (c - '0')))
281
282         for (p = buf, *len = 0;
283              slen > 0 && isxdigit(s[0]) && isxdigit(s[1]);
284              slen--, s += 2, (*len)++)
285                 *p++ = hex(s[0]) * 16 + hex(s[1]);
286
287         return (char *)buf;
288 }
289
290 int rm_r(const char *path)
291 {
292         int ret = 0;
293         DIR *dir;
294         struct dirent *dent;
295
296         if (path == NULL) {
297                 opkg_perror(ERROR, "Missing directory parameter");
298                 return -1;
299         }
300
301         dir = opendir(path);
302         if (dir == NULL) {
303                 opkg_perror(ERROR, "Failed to open dir %s", path);
304                 return -1;
305         }
306
307         if (fchdir(dirfd(dir)) == -1) {
308                 opkg_perror(ERROR, "Failed to change to dir %s", path);
309                 closedir(dir);
310                 return -1;
311         }
312
313         while (1) {
314                 errno = 0;
315                 if ((dent = readdir(dir)) == NULL) {
316                         if (errno) {
317                                 opkg_perror(ERROR, "Failed to read dir %s",
318                                             path);
319                                 ret = -1;
320                         }
321                         break;
322                 }
323
324                 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
325                         continue;
326
327 #ifdef _BSD_SOURCE
328                 if (dent->d_type == DT_DIR) {
329                         if ((ret = rm_r(dent->d_name)) == -1)
330                                 break;
331                         continue;
332                 } else if (dent->d_type == DT_UNKNOWN)
333 #endif
334                 {
335                         struct stat st;
336                         if ((ret = lstat(dent->d_name, &st)) == -1) {
337                                 opkg_perror(ERROR, "Failed to lstat %s",
338                                             dent->d_name);
339                                 break;
340                         }
341                         if (S_ISDIR(st.st_mode)) {
342                                 if ((ret = rm_r(dent->d_name)) == -1)
343                                         break;
344                                 continue;
345                         }
346                 }
347
348                 if ((ret = unlink(dent->d_name)) == -1) {
349                         opkg_perror(ERROR, "Failed to unlink %s", dent->d_name);
350                         break;
351                 }
352         }
353
354         if (chdir("..") == -1) {
355                 ret = -1;
356                 opkg_perror(ERROR, "Failed to change to dir %s/..", path);
357         }
358
359         if (rmdir(path) == -1) {
360                 ret = -1;
361                 opkg_perror(ERROR, "Failed to remove dir %s", path);
362         }
363
364         if (closedir(dir) == -1) {
365                 ret = -1;
366                 opkg_perror(ERROR, "Failed to close dir %s", path);
367         }
368
369         return ret;
370 }