file_util: implement urlencode_path() helper
[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 char *file_md5sum_alloc(const char *file_name)
129 {
130 static const int md5sum_bin_len = 16;
131 static const int md5sum_hex_len = 32;
132
133 static const unsigned char bin2hex[16] = {
134 '0', '1', '2', '3',
135 '4', '5', '6', '7',
136 '8', '9', 'a', 'b',
137 'c', 'd', 'e', 'f'
138 };
139
140 int i, len;
141 char *md5sum_hex;
142 unsigned char md5sum_bin[md5sum_bin_len];
143
144 len = md5sum(file_name, md5sum_bin);
145
146 if (len) {
147 opkg_msg(ERROR, "Could't compute md5sum for %s.\n", file_name);
148 return NULL;
149 }
150
151 md5sum_hex = xcalloc(1, md5sum_hex_len + 1);
152
153 for (i = 0; i < md5sum_bin_len; i++) {
154 md5sum_hex[i * 2] = bin2hex[md5sum_bin[i] >> 4];
155 md5sum_hex[i * 2 + 1] = bin2hex[md5sum_bin[i] & 0xf];
156 }
157
158 md5sum_hex[md5sum_hex_len] = '\0';
159
160 return md5sum_hex;
161 }
162
163 char *file_sha256sum_alloc(const char *file_name)
164 {
165 static const int sha256sum_bin_len = 32;
166 static const int sha256sum_hex_len = 64;
167
168 static const unsigned char bin2hex[16] = {
169 '0', '1', '2', '3',
170 '4', '5', '6', '7',
171 '8', '9', 'a', 'b',
172 'c', 'd', 'e', 'f'
173 };
174
175 int i, err;
176 FILE *file;
177 char *sha256sum_hex;
178 unsigned char sha256sum_bin[sha256sum_bin_len];
179
180 sha256sum_hex = xcalloc(1, sha256sum_hex_len + 1);
181
182 file = fopen(file_name, "r");
183 if (file == NULL) {
184 opkg_perror(ERROR, "Failed to open file %s", file_name);
185 free(sha256sum_hex);
186 return NULL;
187 }
188
189 err = sha256_stream(file, sha256sum_bin);
190 if (err) {
191 opkg_msg(ERROR, "Could't compute sha256sum for %s.\n",
192 file_name);
193 fclose(file);
194 free(sha256sum_hex);
195 return NULL;
196 }
197
198 fclose(file);
199
200 for (i = 0; i < sha256sum_bin_len; i++) {
201 sha256sum_hex[i * 2] = bin2hex[sha256sum_bin[i] >> 4];
202 sha256sum_hex[i * 2 + 1] = bin2hex[sha256sum_bin[i] & 0xf];
203 }
204
205 sha256sum_hex[sha256sum_hex_len] = '\0';
206
207 return sha256sum_hex;
208 }
209
210 char *checksum_bin2hex(const char *src, size_t len)
211 {
212 unsigned char *p;
213 static unsigned char buf[65];
214 const unsigned char *s = (unsigned char *)src;
215 static const unsigned char bin2hex[16] = {
216 '0', '1', '2', '3',
217 '4', '5', '6', '7',
218 '8', '9', 'a', 'b',
219 'c', 'd', 'e', 'f'
220 };
221
222 if (!s || len > 32)
223 return NULL;
224
225 for (p = buf; len > 0; s++, len--) {
226 *p++ = bin2hex[*s / 16];
227 *p++ = bin2hex[*s % 16];
228 }
229
230 *p = 0;
231
232 return (char *)buf;
233 }
234
235 char *checksum_hex2bin(const char *src, size_t *len)
236 {
237 size_t slen;
238 unsigned char *p;
239 const unsigned char *s = (unsigned char *)src;
240 static unsigned char buf[32];
241
242 if (!src) {
243 *len = 0;
244 return NULL;
245 }
246
247 while (isspace(*src))
248 src++;
249
250 slen = strlen(src);
251
252 if (slen > 64) {
253 *len = 0;
254 return NULL;
255 }
256
257 #define hex(c) \
258 (c >= 'a' ? (c - 'a') : (c >= 'A' ? (c - 'A') : (c - '0')))
259
260 for (p = buf, *len = 0;
261 slen > 0 && isxdigit(s[0]) && isxdigit(s[1]);
262 slen--, s += 2, (*len)++)
263 *p++ = hex(s[0]) * 16 + hex(s[1]);
264
265 return (char *)buf;
266 }
267
268 int rm_r(const char *path)
269 {
270 int ret = 0;
271 DIR *dir;
272 struct dirent *dent;
273
274 if (path == NULL) {
275 opkg_perror(ERROR, "Missing directory parameter");
276 return -1;
277 }
278
279 dir = opendir(path);
280 if (dir == NULL) {
281 opkg_perror(ERROR, "Failed to open dir %s", path);
282 return -1;
283 }
284
285 if (fchdir(dirfd(dir)) == -1) {
286 opkg_perror(ERROR, "Failed to change to dir %s", path);
287 closedir(dir);
288 return -1;
289 }
290
291 while (1) {
292 errno = 0;
293 if ((dent = readdir(dir)) == NULL) {
294 if (errno) {
295 opkg_perror(ERROR, "Failed to read dir %s",
296 path);
297 ret = -1;
298 }
299 break;
300 }
301
302 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
303 continue;
304
305 #ifdef _BSD_SOURCE
306 if (dent->d_type == DT_DIR) {
307 if ((ret = rm_r(dent->d_name)) == -1)
308 break;
309 continue;
310 } else if (dent->d_type == DT_UNKNOWN)
311 #endif
312 {
313 struct stat st;
314 if ((ret = lstat(dent->d_name, &st)) == -1) {
315 opkg_perror(ERROR, "Failed to lstat %s",
316 dent->d_name);
317 break;
318 }
319 if (S_ISDIR(st.st_mode)) {
320 if ((ret = rm_r(dent->d_name)) == -1)
321 break;
322 continue;
323 }
324 }
325
326 if ((ret = unlink(dent->d_name)) == -1) {
327 opkg_perror(ERROR, "Failed to unlink %s", dent->d_name);
328 break;
329 }
330 }
331
332 if (chdir("..") == -1) {
333 ret = -1;
334 opkg_perror(ERROR, "Failed to change to dir %s/..", path);
335 }
336
337 if (rmdir(path) == -1) {
338 ret = -1;
339 opkg_perror(ERROR, "Failed to remove dir %s", path);
340 }
341
342 if (closedir(dir) == -1) {
343 ret = -1;
344 opkg_perror(ERROR, "Failed to close dir %s", path);
345 }
346
347 return ret;
348 }
349
350 static int urlencode_is_specialchar(char c)
351 {
352 switch (c) {
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 case ',':
368 case ';':
369 case '=':
370 case '%':
371 return 1;
372
373 default:
374 return 0;
375 }
376 }
377
378 char *urlencode_path(const char *filename)
379 {
380 static const char bin2hex[16] = {
381 '0', '1', '2', '3',
382 '4', '5', '6', '7',
383 '8', '9', 'a', 'b',
384 'c', 'd', 'e', 'f'
385 };
386
387 size_t len = 0;
388 const char *in;
389 char *copy, *out;
390
391 for (in = filename; *in != 0; in++)
392 len += urlencode_is_specialchar(*in) ? 3 : 1;
393
394 copy = xcalloc(1, len + 1);
395
396 for (in = filename, out = copy; *in != 0; in++) {
397 if (urlencode_is_specialchar(*in)) {
398 *out++ = '%';
399 *out++ = bin2hex[*in / 16];
400 *out++ = bin2hex[*in % 16];
401 }
402 else {
403 *out++ = *in;
404 }
405 }
406
407 return copy;
408 }