7e955edd1635f7f0849d3ac1c024990ce20e6042
[project/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 <stdio.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <dirent.h>
23 #include <unistd.h>
24 #include <ctype.h>
25
26 #include "sprintf_alloc.h"
27 #include "file_util.h"
28 #include <libubox/md5.h>
29 #include "libbb/libbb.h"
30
31 #include "sha256.h"
32
33 int file_exists(const char *file_name)
34 {
35 struct stat st;
36
37 if (stat(file_name, &st) == -1)
38 return 0;
39
40 return 1;
41 }
42
43 int file_is_dir(const char *file_name)
44 {
45 struct stat st;
46
47 if (stat(file_name, &st) == -1)
48 return 0;
49
50 return S_ISDIR(st.st_mode);
51 }
52
53 /* read a single line from a file, stopping at a newline or EOF.
54 If a newline is read, it will appear in the resulting string.
55 Return value is a malloc'ed char * which should be freed at
56 some point by the caller.
57
58 Return value is NULL if the file is at EOF when called.
59 */
60 char *file_read_line_alloc(FILE * fp)
61 {
62 char buf[BUFSIZ];
63 unsigned int buf_len;
64 char *line = NULL;
65 unsigned int line_size = 0;
66 int got_nl = 0;
67
68 buf[0] = '\0';
69
70 while (fgets(buf, BUFSIZ, fp)) {
71 buf_len = strlen(buf);
72 if (buf[buf_len - 1] == '\n') {
73 buf_len--;
74 buf[buf_len] = '\0';
75 got_nl = 1;
76 }
77 if (line) {
78 line_size += buf_len;
79 line = xrealloc(line, line_size + 1);
80 strncat(line, buf, line_size);
81 } else {
82 line_size = buf_len + 1;
83 line = xstrdup(buf);
84 }
85 if (got_nl)
86 break;
87 }
88
89 return line;
90 }
91
92 int file_move(const char *src, const char *dest)
93 {
94 int err;
95
96 err = rename(src, dest);
97 if (err == -1) {
98 if (errno == EXDEV) {
99 /* src & dest live on different file systems */
100 err = file_copy(src, dest);
101 if (err == 0)
102 unlink(src);
103 } else {
104 opkg_perror(ERROR, "Failed to rename %s to %s",
105 src, dest);
106 }
107 }
108
109 return err;
110 }
111
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 opkg_msg(ERROR, "Failed to copy file %s to %s.\n", src, dest);
119
120 return err;
121 }
122
123 int file_mkdir_hier(const char *path, long mode)
124 {
125 return make_directory(path, mode, FILEUTILS_RECUR);
126 }
127
128
129 static int hex2bin(unsigned char x)
130 {
131 if (x >= 'a' && x <= 'f')
132 return x - 'a' + 10;
133 else if (x >= 'A' && x <= 'F')
134 return x - 'A' + 10;
135 else if (x >= '0' && x <= '9')
136 return x - '0';
137 else
138 return 0;
139 }
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 char *file_md5sum_alloc(const char *file_name)
149 {
150 static const int md5sum_bin_len = 16;
151 static const int md5sum_hex_len = 32;
152
153 int i, len;
154 char *md5sum_hex;
155 unsigned char md5sum_bin[md5sum_bin_len];
156
157 len = md5sum(file_name, md5sum_bin);
158
159 if (len) {
160 opkg_msg(ERROR, "Could't compute md5sum for %s.\n", file_name);
161 return NULL;
162 }
163
164 md5sum_hex = xcalloc(1, md5sum_hex_len + 1);
165
166 for (i = 0; i < md5sum_bin_len; i++) {
167 md5sum_hex[i * 2] = bin2hex[md5sum_bin[i] >> 4];
168 md5sum_hex[i * 2 + 1] = bin2hex[md5sum_bin[i] & 0xf];
169 }
170
171 md5sum_hex[md5sum_hex_len] = '\0';
172
173 return md5sum_hex;
174 }
175
176 char *file_sha256sum_alloc(const char *file_name)
177 {
178 static const int sha256sum_bin_len = 32;
179 static const int sha256sum_hex_len = 64;
180
181 int i, err;
182 FILE *file;
183 char *sha256sum_hex;
184 unsigned char sha256sum_bin[sha256sum_bin_len];
185
186 sha256sum_hex = xcalloc(1, sha256sum_hex_len + 1);
187
188 file = fopen(file_name, "r");
189 if (file == NULL) {
190 opkg_perror(ERROR, "Failed to open file %s", file_name);
191 free(sha256sum_hex);
192 return NULL;
193 }
194
195 err = sha256_stream(file, sha256sum_bin);
196 if (err) {
197 opkg_msg(ERROR, "Could't compute sha256sum for %s.\n",
198 file_name);
199 fclose(file);
200 free(sha256sum_hex);
201 return NULL;
202 }
203
204 fclose(file);
205
206 for (i = 0; i < sha256sum_bin_len; i++) {
207 sha256sum_hex[i * 2] = bin2hex[sha256sum_bin[i] >> 4];
208 sha256sum_hex[i * 2 + 1] = bin2hex[sha256sum_bin[i] & 0xf];
209 }
210
211 sha256sum_hex[sha256sum_hex_len] = '\0';
212
213 return sha256sum_hex;
214 }
215
216 char *checksum_bin2hex(const char *src, size_t len)
217 {
218 unsigned char *p;
219 static unsigned char buf[65];
220 const unsigned char *s = (unsigned char *)src;
221 if (!s || len > 32)
222 return NULL;
223
224 for (p = buf; len > 0; s++, len--) {
225 *p++ = bin2hex[*s / 16];
226 *p++ = bin2hex[*s % 16];
227 }
228
229 *p = 0;
230
231 return (char *)buf;
232 }
233
234 char *checksum_hex2bin(const char *src, size_t *len)
235 {
236 size_t slen;
237 unsigned char *p;
238 const unsigned char *s;
239 static unsigned char buf[32];
240
241 if (!src) {
242 *len = 0;
243 return NULL;
244 }
245
246 while (isspace(*src))
247 src++;
248
249 slen = strlen(src);
250
251 if (slen > 64) {
252 *len = 0;
253 return NULL;
254 }
255
256 for (s = (unsigned char *)src, p = buf, *len = 0;
257 slen > 0 && isxdigit(s[0]) && isxdigit(s[1]);
258 slen--, s += 2, (*len)++)
259 *p++ = hex2bin(s[0]) * 16 + hex2bin(s[1]);
260
261 return (char *)buf;
262 }
263
264 int rm_r(const char *path)
265 {
266 int ret = 0;
267 DIR *dir;
268 struct dirent *dent;
269
270 if (path == NULL) {
271 opkg_perror(ERROR, "Missing directory parameter");
272 return -1;
273 }
274
275 dir = opendir(path);
276 if (dir == NULL) {
277 opkg_perror(ERROR, "Failed to open dir %s", path);
278 return -1;
279 }
280
281 if (fchdir(dirfd(dir)) == -1) {
282 opkg_perror(ERROR, "Failed to change to dir %s", path);
283 closedir(dir);
284 return -1;
285 }
286
287 while (1) {
288 errno = 0;
289 if ((dent = readdir(dir)) == NULL) {
290 if (errno) {
291 opkg_perror(ERROR, "Failed to read dir %s",
292 path);
293 ret = -1;
294 }
295 break;
296 }
297
298 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
299 continue;
300
301 #ifdef _BSD_SOURCE
302 if (dent->d_type == DT_DIR) {
303 if ((ret = rm_r(dent->d_name)) == -1)
304 break;
305 continue;
306 } else if (dent->d_type == DT_UNKNOWN)
307 #endif
308 {
309 struct stat st;
310 if ((ret = lstat(dent->d_name, &st)) == -1) {
311 opkg_perror(ERROR, "Failed to lstat %s",
312 dent->d_name);
313 break;
314 }
315 if (S_ISDIR(st.st_mode)) {
316 if ((ret = rm_r(dent->d_name)) == -1)
317 break;
318 continue;
319 }
320 }
321
322 if ((ret = unlink(dent->d_name)) == -1) {
323 opkg_perror(ERROR, "Failed to unlink %s", dent->d_name);
324 break;
325 }
326 }
327
328 if (chdir("..") == -1) {
329 ret = -1;
330 opkg_perror(ERROR, "Failed to change to dir %s/..", path);
331 }
332
333 if (rmdir(path) == -1) {
334 ret = -1;
335 opkg_perror(ERROR, "Failed to remove dir %s", path);
336 }
337
338 if (closedir(dir) == -1) {
339 ret = -1;
340 opkg_perror(ERROR, "Failed to close dir %s", path);
341 }
342
343 return ret;
344 }
345
346 static int urlencode_is_specialchar(char c)
347 {
348 switch (c) {
349 case ':':
350 case '?':
351 case '#':
352 case '[':
353 case ']':
354 case '@':
355 case '!':
356 case '$':
357 case '&':
358 case '\'':
359 case '(':
360 case ')':
361 case '*':
362 case '+':
363 case ',':
364 case ';':
365 case '=':
366 case '%':
367 return 1;
368
369 default:
370 return 0;
371 }
372 }
373
374 char *urlencode_path(const char *filename)
375 {
376 size_t len = 0;
377 const unsigned char *in;
378 unsigned char *copy, *out;
379
380 for (in = (unsigned char *)filename; *in != 0; in++)
381 len += urlencode_is_specialchar(*in) ? 3 : 1;
382
383 copy = xcalloc(1, len + 1);
384
385 for (in = (unsigned char *)filename, out = copy; *in != 0; in++) {
386 if (urlencode_is_specialchar(*in)) {
387 *out++ = '%';
388 *out++ = bin2hex[*in / 16];
389 *out++ = bin2hex[*in % 16];
390 }
391 else {
392 *out++ = *in;
393 }
394 }
395
396 return (char *)copy;
397 }
398
399 char *urldecode_path(const char *filename)
400 {
401 unsigned char *copy = (unsigned char *)xstrdup(filename);
402 unsigned char *in, *out;
403
404 for (in = copy, out = copy; *in != 0; in++) {
405 if (*in == '%' && isxdigit(in[1]) && isxdigit(in[2])) {
406 *out++ = hex2bin(in[1]) * 16 + hex2bin(in[2]);
407 in += 2;
408 }
409 else {
410 *out++ = *in;
411 }
412 }
413
414 *out = 0;
415
416 return (char *)copy;
417 }