/src/barebox/lib/libfile.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2014 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix |
3 | | * |
4 | | * This program is free software; you can redistribute it and/or modify |
5 | | * it under the terms of the GNU General Public License version 2 |
6 | | * as published by the Free Software Foundation. |
7 | | * |
8 | | * This program is distributed in the hope that it will be useful, |
9 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11 | | * GNU General Public License for more details. |
12 | | * |
13 | | */ |
14 | | #include <common.h> |
15 | | #include <memory.h> |
16 | | #include <zero_page.h> |
17 | | #include <fs.h> |
18 | | #include <fcntl.h> |
19 | | #include <malloc.h> |
20 | | #include <libfile.h> |
21 | | #include <progress.h> |
22 | | #include <stdlib.h> |
23 | | #include <string.h> |
24 | | #include <linux/stat.h> |
25 | | |
26 | | /* |
27 | | * pwrite_full - write to filedescriptor at offset |
28 | | * |
29 | | * Like pwrite, but guarantees to write the full buffer out, else it |
30 | | * returns with an error. |
31 | | */ |
32 | | int pwrite_full(int fd, const void *buf, size_t size, loff_t offset) |
33 | 0 | { |
34 | 0 | size_t insize = size; |
35 | 0 | int now; |
36 | |
|
37 | 0 | while (size) { |
38 | 0 | now = pwrite(fd, buf, size, offset); |
39 | 0 | if (now == 0) |
40 | 0 | return errno_set(-ENOSPC); |
41 | 0 | if (now < 0) |
42 | 0 | return now; |
43 | 0 | size -= now; |
44 | 0 | buf += now; |
45 | 0 | offset += now; |
46 | 0 | } |
47 | | |
48 | 0 | return insize; |
49 | 0 | } |
50 | | EXPORT_SYMBOL(pwrite_full); |
51 | | |
52 | | /* |
53 | | * write_full - write to filedescriptor |
54 | | * |
55 | | * Like write, but guarantees to write the full buffer out, else |
56 | | * it returns with an error. |
57 | | */ |
58 | | int write_full(int fd, const void *buf, size_t size) |
59 | 501 | { |
60 | 501 | size_t insize = size; |
61 | 501 | int now; |
62 | | |
63 | 1.00k | while (size) { |
64 | 501 | now = write(fd, buf, size); |
65 | 501 | if (now == 0) |
66 | 0 | return errno_set(-ENOSPC); |
67 | 501 | if (now < 0) |
68 | 0 | return now; |
69 | 501 | size -= now; |
70 | 501 | buf += now; |
71 | 501 | } |
72 | | |
73 | 501 | return insize; |
74 | 501 | } |
75 | | EXPORT_SYMBOL(write_full); |
76 | | |
77 | | /* |
78 | | * pread_full - read to filedescriptor at offset |
79 | | * |
80 | | * Like pread, but this function only returns less bytes than |
81 | | * requested when the end of file is reached. |
82 | | */ |
83 | | int pread_full(int fd, void *buf, size_t size, loff_t offset) |
84 | 526 | { |
85 | 526 | size_t insize = size; |
86 | 526 | int now; |
87 | | |
88 | 990 | while (size) { |
89 | 464 | now = pread(fd, buf, size, offset); |
90 | 464 | if (now == 0) |
91 | 0 | break; |
92 | 464 | if (now < 0) |
93 | 0 | return now; |
94 | 464 | size -= now; |
95 | 464 | buf += now; |
96 | 464 | offset += now; |
97 | 464 | } |
98 | | |
99 | 526 | return insize - size; |
100 | 526 | } |
101 | | EXPORT_SYMBOL(pread_full); |
102 | | |
103 | | /* |
104 | | * read_full - read from filedescriptor |
105 | | * |
106 | | * Like read, but this function only returns less bytes than |
107 | | * requested when the end of file is reached. |
108 | | */ |
109 | | int read_full(int fd, void *buf, size_t size) |
110 | 1.14k | { |
111 | 1.14k | size_t insize = size; |
112 | 1.14k | int now; |
113 | | |
114 | 2.26k | while (size) { |
115 | 1.11k | now = read(fd, buf, size); |
116 | 1.11k | if (now == 0) |
117 | 0 | break; |
118 | 1.11k | if (now < 0) |
119 | 0 | return now; |
120 | 1.11k | size -= now; |
121 | 1.11k | buf += now; |
122 | 1.11k | } |
123 | | |
124 | 1.14k | return insize - size; |
125 | 1.14k | } |
126 | | EXPORT_SYMBOL(read_full); |
127 | | |
128 | | /* |
129 | | * copy_fd - copy one file descriptor to another |
130 | | * @in: input file descriptor |
131 | | * @out: output file descriptor |
132 | | * @size: size to copy |
133 | | * |
134 | | * This copies at @size bytes from @in file descriptor to @out |
135 | | * file descriptor. If @size is zero then all data is copied until |
136 | | * EOF is reached. |
137 | | * |
138 | | * @return: 0 for success and negatibe error code otherwise. If @size |
139 | | * is non zero and not all data could be read from @in then -ENODATA |
140 | | * is returned. |
141 | | */ |
142 | | int copy_fd(int in, int out, size_t size) |
143 | 0 | { |
144 | 0 | size_t copied = 0, bs = 4096; |
145 | 0 | void *buf = malloc(bs); |
146 | 0 | int ret; |
147 | |
|
148 | 0 | if (!buf) |
149 | 0 | return -ENOMEM; |
150 | | |
151 | 0 | while (1) { |
152 | 0 | size_t now; |
153 | |
|
154 | 0 | if (size) |
155 | 0 | now = min(bs, size - copied); |
156 | 0 | else |
157 | 0 | now = bs; |
158 | |
|
159 | 0 | if (!now) { |
160 | 0 | ret = 0; |
161 | 0 | break; |
162 | 0 | } |
163 | | |
164 | 0 | ret = read(in, buf, now); |
165 | 0 | if (ret <= 0) |
166 | 0 | break; |
167 | | |
168 | 0 | now = ret; |
169 | |
|
170 | 0 | ret = write_full(out, buf, ret); |
171 | 0 | if (ret < 0) |
172 | 0 | break; |
173 | | |
174 | 0 | copied += now; |
175 | 0 | } |
176 | |
|
177 | 0 | free(buf); |
178 | |
|
179 | 0 | if (size && copied < size) |
180 | 0 | ret = -ENODATA; |
181 | |
|
182 | 0 | return ret; |
183 | 0 | } |
184 | | |
185 | | /* |
186 | | * read_file_line - read a line from a file |
187 | | * |
188 | | * Used to compose a filename from a printf format and to read a line from this |
189 | | * file. All leading and trailing whitespaces (including line endings) are |
190 | | * removed. The returned buffer must be freed with free(). This function is |
191 | | * supposed for reading variable like content into a buffer, so files > 1024 |
192 | | * bytes are ignored. |
193 | | */ |
194 | | char *read_file_line(const char *fmt, ...) |
195 | 120 | { |
196 | 120 | va_list args; |
197 | 120 | char *filename; |
198 | 120 | char *buf, *line = NULL; |
199 | 120 | int ret; |
200 | 120 | struct stat s; |
201 | | |
202 | 120 | va_start(args, fmt); |
203 | 120 | filename = bvasprintf(fmt, args); |
204 | 120 | va_end(args); |
205 | | |
206 | 120 | ret = stat(filename, &s); |
207 | 120 | if (ret) |
208 | 0 | goto out; |
209 | | |
210 | 120 | if (s.st_size > 1024) |
211 | 0 | goto out; |
212 | | |
213 | 120 | buf = read_file(filename, NULL); |
214 | 120 | if (!buf) |
215 | 0 | goto out; |
216 | | |
217 | 120 | line = strim(buf); |
218 | | |
219 | 120 | line = xstrdup(line); |
220 | 120 | free(buf); |
221 | 120 | out: |
222 | 120 | free(filename); |
223 | 120 | return line; |
224 | 120 | } |
225 | | EXPORT_SYMBOL_GPL(read_file_line); |
226 | | |
227 | | /** |
228 | | * read_file_into_buf - read a file to an external buffer |
229 | | * @filename: The filename to read |
230 | | * @buf: The buffer to read into |
231 | | * @size: The buffer size |
232 | | * |
233 | | * This function reads a file to an external buffer. At maximum @size |
234 | | * bytes are read. |
235 | | * |
236 | | * Return: number of bytes read, or negative error code. |
237 | | */ |
238 | | ssize_t read_file_into_buf(const char *filename, void *buf, size_t size) |
239 | 120 | { |
240 | 120 | int fd; |
241 | 120 | ssize_t ret; |
242 | | |
243 | 120 | fd = open(filename, O_RDONLY); |
244 | 120 | if (fd < 0) |
245 | 0 | return fd; |
246 | | |
247 | 120 | ret = read_full(fd, buf, size); |
248 | | |
249 | 120 | close(fd); |
250 | | |
251 | 120 | return ret; |
252 | 120 | } |
253 | | |
254 | | /** |
255 | | * read_file_2 - read a file to an allocated buffer |
256 | | * @filename: The filename to read |
257 | | * @size: After successful return contains the size of the file |
258 | | * @outbuf: contains a pointer to the file data after successful return |
259 | | * @max_size: The maximum size to read. Use FILESIZE_MAX for reading files |
260 | | * of any size. |
261 | | * |
262 | | * This function reads a file to an allocated buffer. At maximum @max_size |
263 | | * bytes are read. The actual read size is returned in @size. -EFBIG is |
264 | | * returned if the file is bigger than @max_size, but the buffer is read |
265 | | * anyway up to @max_size in this case. Free the buffer with free() after |
266 | | * usage. The allocated buffer is actually one byte bigger than the file |
267 | | * and the extra byte is initialized to '\0' so that the returned buffer |
268 | | * can safely be interpreted as a string. |
269 | | * |
270 | | * Return: 0 for success, or negative error code. -EFBIG is returned |
271 | | * when the file has been bigger than max_size. |
272 | | */ |
273 | | int read_file_2(const char *filename, size_t *size, void **outbuf, |
274 | | loff_t max_size) |
275 | 120 | { |
276 | 120 | struct stat s; |
277 | 120 | void *buf = NULL; |
278 | 120 | const char *tmpfile = "/.read_file_tmp"; |
279 | 120 | ssize_t ret; |
280 | 120 | loff_t read_size; |
281 | | |
282 | 120 | again: |
283 | 120 | ret = stat(filename, &s); |
284 | 120 | if (ret) |
285 | 0 | return ret; |
286 | | |
287 | 120 | if (max_size == FILESIZE_MAX) |
288 | 120 | read_size = s.st_size; |
289 | 0 | else |
290 | 0 | read_size = max_size; |
291 | | |
292 | 120 | if (read_size == FILESIZE_MAX) { |
293 | 0 | ret = copy_file(filename, tmpfile, 0); |
294 | 0 | if (ret) |
295 | 0 | return ret; |
296 | 0 | filename = tmpfile; |
297 | 0 | goto again; |
298 | 0 | } |
299 | | |
300 | | /* ensure wchar_t nul termination */ |
301 | 120 | buf = calloc(ALIGN(read_size, 2) + 2, 1); |
302 | 120 | if (!buf) { |
303 | 0 | ret = errno_set(-ENOMEM); |
304 | 0 | goto err_out; |
305 | 0 | } |
306 | | |
307 | 120 | ret = read_file_into_buf(filename, buf, read_size); |
308 | 120 | if (ret < 0) |
309 | 0 | goto err_out; |
310 | | |
311 | 120 | if (size) |
312 | 0 | *size = ret; |
313 | | |
314 | 120 | if (filename == tmpfile) |
315 | 0 | unlink(tmpfile); |
316 | | |
317 | 120 | *outbuf = buf; |
318 | | |
319 | 120 | if (read_size < s.st_size) |
320 | 0 | return -EFBIG; |
321 | | |
322 | 120 | return 0; |
323 | | |
324 | 0 | err_out: |
325 | 0 | free(buf); |
326 | |
|
327 | 0 | if (filename == tmpfile) |
328 | 0 | unlink(tmpfile); |
329 | |
|
330 | 0 | return ret; |
331 | 120 | } |
332 | | EXPORT_SYMBOL(read_file_2); |
333 | | |
334 | | /** |
335 | | * read_file - read a file to an allocated buffer |
336 | | * @filename: The filename to read |
337 | | * @size: After successful return contains the size of the file |
338 | | * |
339 | | * This function reads a file to an allocated buffer. |
340 | | * Some TFTP servers do not transfer the size of a file. In this case |
341 | | * the file is first read to a temporary file. |
342 | | * |
343 | | * Return: The buffer containing the file or NULL on failure |
344 | | */ |
345 | | void *read_file(const char *filename, size_t *size) |
346 | 120 | { |
347 | 120 | int ret; |
348 | 120 | void *buf; |
349 | | |
350 | 120 | ret = read_file_2(filename, size, &buf, FILESIZE_MAX); |
351 | 120 | if (!ret) |
352 | 120 | return buf; |
353 | | |
354 | 0 | return NULL; |
355 | 120 | } |
356 | | EXPORT_SYMBOL(read_file); |
357 | | |
358 | | /** |
359 | | * read_fd - read from a file descriptor to an allocated buffer |
360 | | * @filename: The file descriptor to read |
361 | | * @size: After successful return contains the size of the file |
362 | | * |
363 | | * This function reads a file descriptor from offset 0 until EOF to an |
364 | | * allocated buffer. |
365 | | * |
366 | | * Return: On success, returns a nul-terminated buffer with the file's |
367 | | * contents that should be deallocated with free(). |
368 | | * On error, NULL is returned and errno is set to an error code. |
369 | | */ |
370 | | void *read_fd(int fd, size_t *out_size) |
371 | 526 | { |
372 | 526 | struct stat st; |
373 | 526 | ssize_t ret; |
374 | 526 | void *buf; |
375 | | |
376 | 526 | ret = fstat(fd, &st); |
377 | 526 | if (ret < 0) |
378 | 0 | return NULL; |
379 | | |
380 | 526 | if (st.st_size == FILE_SIZE_STREAM) { |
381 | 0 | errno = EINVAL; |
382 | 0 | return NULL; |
383 | 0 | } |
384 | | |
385 | | /* For user convenience, we always nul-terminate the buffer in |
386 | | * case it contains a string. As we don't want to assume the string |
387 | | * to be either an array of char or wchar_t, we just unconditionally |
388 | | * add 2 bytes as terminator. As the two byte terminator needs to be |
389 | | * aligned, we just make it three bytes |
390 | | */ |
391 | 526 | buf = malloc(st.st_size + 3); |
392 | 526 | if (!buf) |
393 | 0 | return NULL; |
394 | | |
395 | 526 | ret = pread_full(fd, buf, st.st_size, 0); |
396 | 526 | if (ret < 0) { |
397 | 0 | free(buf); |
398 | 0 | return NULL; |
399 | 0 | } |
400 | | |
401 | 526 | memset(buf + st.st_size, '\0', 3); |
402 | 526 | *out_size = st.st_size; |
403 | | |
404 | 526 | return buf; |
405 | 526 | } |
406 | | EXPORT_SYMBOL(read_fd); |
407 | | |
408 | | /** |
409 | | * write_file - write a buffer to a file |
410 | | * @filename: The filename to write |
411 | | * @size: The size of the buffer |
412 | | * |
413 | | * Return: 0 for success or negative error value |
414 | | */ |
415 | | int write_file(const char *filename, const void *buf, size_t size) |
416 | 441 | { |
417 | 441 | int fd, ret; |
418 | | |
419 | 441 | fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, 0644); |
420 | 441 | if (fd < 0) |
421 | 0 | return fd; |
422 | | |
423 | 441 | ret = write_full(fd, buf, size); |
424 | | |
425 | 441 | close(fd); |
426 | | |
427 | 441 | if (ret < 0) |
428 | 0 | return ret; |
429 | | |
430 | 441 | return 0; |
431 | 441 | } |
432 | | EXPORT_SYMBOL(write_file); |
433 | | |
434 | | /** |
435 | | * write_file_flash - write a buffer to a file backed by flash |
436 | | * @filename: The filename to write |
437 | | * @size: The size of the buffer |
438 | | * |
439 | | * Functional this is identical to write_file but calls erase() before writing. |
440 | | * |
441 | | * Return: 0 for success or negative error value |
442 | | */ |
443 | | int write_file_flash(const char *filename, const void *buf, size_t size) |
444 | 0 | { |
445 | 0 | int fd, ret; |
446 | |
|
447 | 0 | fd = open(filename, O_WRONLY); |
448 | 0 | if (fd < 0) |
449 | 0 | return fd; |
450 | | |
451 | 0 | ret = erase(fd, size, 0, ERASE_TO_WRITE); |
452 | 0 | if (ret < 0) |
453 | 0 | goto out_close; |
454 | | |
455 | 0 | ret = write_full(fd, buf, size); |
456 | |
|
457 | 0 | out_close: |
458 | 0 | close(fd); |
459 | |
|
460 | 0 | if (ret < 0) |
461 | 0 | return ret; |
462 | | |
463 | 0 | return 0; |
464 | 0 | } |
465 | | EXPORT_SYMBOL(write_file_flash); |
466 | | |
467 | | /** |
468 | | * copy_file - Copy a file |
469 | | * @src: The source filename |
470 | | * @dst: The destination filename |
471 | | * @flags: A bitmask of COPY_FILE_* flags. Possible values: |
472 | | * |
473 | | * COPY_FILE_VERBOSE: show a progression bar |
474 | | * COPY_FILE_NO_OVERWRITE: don't clobber existing files |
475 | | * |
476 | | * Return: 0 for success or negative error code |
477 | | */ |
478 | | int copy_file(const char *src, const char *dst, unsigned flags) |
479 | 60 | { |
480 | 60 | char *rw_buf = NULL; |
481 | 60 | int srcfd = 0, dstfd = 0; |
482 | 60 | int r, s; |
483 | 60 | int ret = 1, err1 = 0; |
484 | 60 | int mode; |
485 | 60 | loff_t total = 0; |
486 | 60 | struct stat srcstat, dststat; |
487 | | |
488 | 60 | rw_buf = xmalloc(RW_BUF_SIZE); |
489 | | |
490 | 60 | srcfd = open(src, O_RDONLY); |
491 | 60 | if (srcfd < 0) { |
492 | 0 | printf("could not open %s: %m\n", src); |
493 | 0 | ret = srcfd; |
494 | 0 | goto out; |
495 | 0 | } |
496 | | |
497 | 60 | mode = O_WRONLY | O_CREAT; |
498 | | |
499 | 60 | s = stat(dst, &dststat); |
500 | 60 | if (s && s != -ENOENT) { |
501 | 0 | printf("could not stat %s: %m\n", dst); |
502 | 0 | ret = s; |
503 | 0 | goto out; |
504 | 0 | } |
505 | | |
506 | | /* Set O_TRUNC only if file exists and is a regular file */ |
507 | 60 | if (!s && S_ISREG(dststat.st_mode)) { |
508 | 60 | if (flags & COPY_FILE_NO_OVERWRITE) { |
509 | 0 | ret = 0; |
510 | 0 | goto out; |
511 | 0 | } |
512 | 60 | mode |= O_TRUNC; |
513 | 60 | } |
514 | | |
515 | 60 | dstfd = open(dst, mode, 0666); |
516 | 60 | if (dstfd < 0) { |
517 | 0 | printf("could not open %s: %m\n", dst); |
518 | 0 | ret = dstfd; |
519 | 0 | goto out; |
520 | 0 | } |
521 | | |
522 | 60 | ret = stat(src, &srcstat); |
523 | 60 | if (ret) |
524 | 0 | goto out; |
525 | | |
526 | 60 | if (srcstat.st_size != FILESIZE_MAX) { |
527 | 60 | discard_range(dstfd, srcstat.st_size, 0); |
528 | 60 | if (s || S_ISREG(dststat.st_mode)) { |
529 | 60 | ret = ftruncate(dstfd, srcstat.st_size); |
530 | 60 | if (ret) |
531 | 0 | goto out; |
532 | 60 | } |
533 | 60 | } |
534 | | |
535 | 60 | if (flags & COPY_FILE_VERBOSE) |
536 | 0 | init_progression_bar(srcstat.st_size); |
537 | | |
538 | 120 | while (1) { |
539 | 120 | r = read(srcfd, rw_buf, RW_BUF_SIZE); |
540 | 120 | if (r < 0) { |
541 | 0 | perror("read"); |
542 | 0 | ret = r; |
543 | 0 | goto out_newline; |
544 | 0 | } |
545 | 120 | if (!r) |
546 | 60 | break; |
547 | | |
548 | 60 | ret = write_full(dstfd, rw_buf, r); |
549 | 60 | if (ret < 0) { |
550 | 0 | perror("write"); |
551 | 0 | goto out_newline; |
552 | 0 | } |
553 | | |
554 | 60 | total += r; |
555 | | |
556 | 60 | if (flags & COPY_FILE_VERBOSE) { |
557 | 0 | if (srcstat.st_size && srcstat.st_size != FILESIZE_MAX) |
558 | 0 | show_progress(total); |
559 | 0 | else |
560 | 0 | show_progress(total / 16384); |
561 | 0 | } |
562 | 60 | } |
563 | | |
564 | 60 | ret = 0; |
565 | 60 | out_newline: |
566 | 60 | if (flags & COPY_FILE_VERBOSE) |
567 | 0 | putchar('\n'); |
568 | 60 | out: |
569 | 60 | free(rw_buf); |
570 | 60 | if (srcfd > 0) |
571 | 60 | close(srcfd); |
572 | 60 | if (dstfd > 0) |
573 | 60 | err1 = close(dstfd); |
574 | | |
575 | 60 | return ret ?: err1; |
576 | 60 | } |
577 | | EXPORT_SYMBOL(copy_file); |
578 | | |
579 | | /** |
580 | | * copy_recursive - Copy files recursively |
581 | | * @src: The source filename or directory |
582 | | * @dst: The destination filename or directory |
583 | | * @flags: A bitmask of COPY_FILE_* flags. Possible values: |
584 | | * |
585 | | * COPY_FILE_VERBOSE: show a progression bar |
586 | | * COPY_FILE_NO_OVERWRITE: don't clobber existing files |
587 | | * |
588 | | * Return: 0 for success or negative error code |
589 | | */ |
590 | | int copy_recursive(const char *src, const char *dst, unsigned flags) |
591 | 150 | { |
592 | 150 | struct stat s; |
593 | 150 | DIR *dir; |
594 | 150 | struct dirent *d; |
595 | 150 | int ret; |
596 | | |
597 | 150 | ret = stat(src, &s); |
598 | 150 | if (ret) |
599 | 0 | return ret; |
600 | | |
601 | 150 | if (!S_ISDIR(s.st_mode)) |
602 | 60 | return copy_file(src, dst, 0); |
603 | | |
604 | 90 | ret = make_directory(dst); |
605 | 90 | if (ret) |
606 | 0 | return ret; |
607 | | |
608 | 90 | dir = opendir(src); |
609 | 90 | if (!dir) |
610 | 0 | return -EIO; |
611 | | |
612 | 390 | while ((d = readdir(dir))) { |
613 | 300 | char *from, *to; |
614 | 300 | if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, "..")) |
615 | 180 | continue; |
616 | | |
617 | 120 | from = basprintf("%s/%s", src, d->d_name); |
618 | 120 | to = basprintf("%s/%s", dst, d->d_name); |
619 | 120 | ret = copy_recursive(from, to, flags); |
620 | 120 | if (ret) |
621 | 0 | break; |
622 | 120 | free(from); |
623 | 120 | free(to); |
624 | 120 | } |
625 | 90 | closedir(dir); |
626 | | |
627 | 90 | return ret; |
628 | 90 | } |
629 | | |
630 | | /** |
631 | | * compare_file - Compare two files |
632 | | * @f1: The first file |
633 | | * @f2: The second file |
634 | | * |
635 | | * Return: 0 if both files are identical, 1 if they differ, |
636 | | * a negative error code if some error occured |
637 | | */ |
638 | | int compare_file(const char *f1, const char *f2) |
639 | 0 | { |
640 | 0 | int fd1, fd2, ret; |
641 | 0 | struct stat s1, s2; |
642 | 0 | void *buf1, *buf2; |
643 | 0 | loff_t left; |
644 | |
|
645 | 0 | fd1 = open(f1, O_RDONLY); |
646 | 0 | if (fd1 < 0) |
647 | 0 | return -errno; |
648 | | |
649 | 0 | fd2 = open(f2, O_RDONLY); |
650 | 0 | if (fd2 < 0) { |
651 | 0 | ret = -errno; |
652 | 0 | goto err_out1; |
653 | 0 | } |
654 | | |
655 | 0 | ret = fstat(fd1, &s1); |
656 | 0 | if (ret) |
657 | 0 | goto err_out2; |
658 | | |
659 | 0 | ret = fstat(fd2, &s2); |
660 | 0 | if (ret) |
661 | 0 | goto err_out2; |
662 | | |
663 | 0 | if (s1.st_size != s2.st_size) { |
664 | 0 | ret = 1; |
665 | 0 | goto err_out2; |
666 | 0 | } |
667 | | |
668 | 0 | buf1 = xmalloc(RW_BUF_SIZE); |
669 | 0 | buf2 = xmalloc(RW_BUF_SIZE); |
670 | |
|
671 | 0 | left = s1.st_size; |
672 | 0 | while (left) { |
673 | 0 | loff_t now = min(left, (loff_t)RW_BUF_SIZE); |
674 | |
|
675 | 0 | ret = read_full(fd1, buf1, now); |
676 | 0 | if (ret < 0) |
677 | 0 | goto err_out3; |
678 | | |
679 | 0 | ret = read_full(fd2, buf2, now); |
680 | 0 | if (ret < 0) |
681 | 0 | goto err_out3; |
682 | | |
683 | 0 | if (memcmp(buf1, buf2, now)) { |
684 | 0 | ret = 1; |
685 | 0 | goto err_out3; |
686 | 0 | } |
687 | | |
688 | 0 | left -= now; |
689 | 0 | } |
690 | | |
691 | 0 | ret = 0; |
692 | |
|
693 | 0 | err_out3: |
694 | 0 | free(buf1); |
695 | 0 | free(buf2); |
696 | 0 | err_out2: |
697 | 0 | close(fd2); |
698 | 0 | err_out1: |
699 | 0 | close(fd1); |
700 | 0 | return ret; |
701 | 0 | } |
702 | | |
703 | | /** |
704 | | * open_and_lseek - open file and lseek to position |
705 | | * @filename: The file to open |
706 | | * @mode: The file open mode |
707 | | * @pos: The position to lseek to |
708 | | * |
709 | | * Return: If successful this function returns a positive |
710 | | * filedescriptor number, otherwise a negative error code is returned |
711 | | */ |
712 | | int open_and_lseek(const char *filename, int mode, loff_t pos) |
713 | 0 | { |
714 | 0 | int fd, ret; |
715 | |
|
716 | 0 | fd = open(filename, mode); |
717 | 0 | if (fd < 0) |
718 | 0 | return fd; |
719 | | |
720 | 0 | if (!pos) |
721 | 0 | return fd; |
722 | | |
723 | 0 | if (mode & (O_WRONLY | O_RDWR)) { |
724 | 0 | struct stat s; |
725 | |
|
726 | 0 | ret = fstat(fd, &s); |
727 | 0 | if (ret < 0) |
728 | 0 | goto out; |
729 | | |
730 | 0 | if (s.st_size < pos) { |
731 | 0 | ret = ftruncate(fd, pos); |
732 | 0 | if (ret) |
733 | 0 | goto out; |
734 | 0 | } |
735 | 0 | } |
736 | | |
737 | 0 | if (lseek(fd, pos, SEEK_SET) != pos) { |
738 | 0 | ret = -errno; |
739 | 0 | goto out; |
740 | 0 | } |
741 | | |
742 | 0 | return fd; |
743 | 0 | out: |
744 | 0 | close(fd); |
745 | 0 | return ret; |
746 | 0 | } |
747 | | |
748 | | /** |
749 | | * make_temp - create a name for a temporary file |
750 | | * @template: The filename prefix |
751 | | * |
752 | | * This function creates a name for a temporary file. @template is used as a |
753 | | * template for the name which gets appended a 8-digit hexadecimal number to |
754 | | * create a unique filename. |
755 | | * |
756 | | * Return: This function returns a filename which can be used as a temporary |
757 | | * file later on. The returned filename must be freed by the caller. |
758 | | */ |
759 | | char *make_temp(const char *template) |
760 | 0 | { |
761 | 0 | char *name = NULL; |
762 | 0 | struct stat s; |
763 | 0 | int ret; |
764 | |
|
765 | 0 | do { |
766 | 0 | free(name); |
767 | 0 | name = basprintf("/tmp/%s-%08x", template, random32()); |
768 | 0 | ret = stat(name, &s); |
769 | 0 | } while (!ret); |
770 | |
|
771 | 0 | return name; |
772 | 0 | } |
773 | | |
774 | | /** |
775 | | * cache_file - Cache a file in /tmp |
776 | | * @path: The file to cache |
777 | | * @newpath: The return path where the file is copied to |
778 | | * |
779 | | * This function copies a given file to /tmp and returns its name in @newpath. |
780 | | * @newpath is dynamically allocated and must be freed by the caller. |
781 | | * |
782 | | * Return: 0 for success, negative error code otherwise. |
783 | | */ |
784 | | int cache_file(const char *path, char **newpath) |
785 | 0 | { |
786 | 0 | char *npath; |
787 | 0 | int ret; |
788 | |
|
789 | 0 | npath = make_temp("filecache"); |
790 | |
|
791 | 0 | ret = copy_file(path, npath, 0); |
792 | 0 | if (ret) { |
793 | 0 | free(npath); |
794 | 0 | return ret; |
795 | 0 | } |
796 | | |
797 | 0 | *newpath = npath; |
798 | |
|
799 | 0 | return 0; |
800 | 0 | } |
801 | | |
802 | | /* |
803 | | * __read_full_anywhere - read from filedescriptor, even into zero_page |
804 | | * |
805 | | * Like read_full, but this function will temporarily remap the zero |
806 | | * page if data is to be placed there. You should not need to use this |
807 | | * outside of boot code! |
808 | | */ |
809 | | int __read_full_anywhere(int fd, void *buf, size_t size) |
810 | 0 | { |
811 | 0 | ssize_t nbytes = 0, now; |
812 | |
|
813 | 0 | if (unlikely(zero_page_contains((ulong)buf))) { |
814 | 0 | void *tmp = malloc(PAGE_SIZE); |
815 | 0 | if (!tmp) |
816 | 0 | return -ENOMEM; |
817 | | |
818 | 0 | now = read_full(fd, tmp, min_t(size_t, size, PAGE_SIZE)); |
819 | 0 | if (now > 0) |
820 | 0 | zero_page_memcpy(buf, tmp, now); |
821 | |
|
822 | 0 | free(tmp); |
823 | |
|
824 | 0 | if (now <= 0) |
825 | 0 | return now; |
826 | | |
827 | 0 | nbytes += now; |
828 | 0 | } |
829 | | |
830 | 0 | now = read_full(fd, buf + nbytes, size - nbytes); |
831 | 0 | return now < 0 ? now : now + nbytes; |
832 | 0 | } |
833 | | |
834 | | int fixup_path_case(int fd, const char **path) |
835 | 0 | { |
836 | 0 | DIR *dir; |
837 | 0 | struct dirent *entry; |
838 | 0 | char *resolved_path, *curr, *next; |
839 | |
|
840 | 0 | next = resolved_path = xstrdup(*path); |
841 | |
|
842 | 0 | while ((curr = strsep(&next, "/"))) { |
843 | 0 | size_t nextlen = strlen(curr); |
844 | 0 | char *imatch = NULL; |
845 | |
|
846 | 0 | dir = fdopendir(fd); |
847 | 0 | if (!dir) |
848 | 0 | goto err; |
849 | | |
850 | 0 | while ((entry = readdir(dir)) != NULL) { |
851 | 0 | size_t d_namelen = strlen(entry->d_name); |
852 | |
|
853 | 0 | if (nextlen != d_namelen) |
854 | 0 | continue; |
855 | | |
856 | 0 | if (!strcmp(entry->d_name, curr)) |
857 | 0 | goto next_component; |
858 | | |
859 | 0 | if (!imatch && !strcasecmp(entry->d_name, curr)) |
860 | 0 | imatch = xstrdup(entry->d_name); |
861 | 0 | } |
862 | | |
863 | 0 | if (!imatch) { |
864 | 0 | errno = ENOENT; |
865 | 0 | goto err; |
866 | 0 | } |
867 | | |
868 | 0 | strncpy(curr, imatch, nextlen); |
869 | 0 | free(imatch); |
870 | |
|
871 | 0 | next_component: |
872 | | /* |
873 | | * The final component may be a directory as well, which |
874 | | * can't be opened without O_DIRECTORY, but O_PATH suffices |
875 | | * for the fstat() callers usually follow up with. |
876 | | */ |
877 | 0 | fd = openat(fd, curr, next ? O_DIRECTORY : O_PATH); |
878 | 0 | closedir(dir); |
879 | |
|
880 | 0 | if (fd < 0) |
881 | 0 | goto err; |
882 | | |
883 | 0 | if (next) |
884 | 0 | curr[nextlen] = '/'; |
885 | 0 | } |
886 | | |
887 | 0 | *path = resolved_path; |
888 | 0 | return fd; |
889 | 0 | err: |
890 | 0 | free(resolved_path); |
891 | 0 | return -errno; |
892 | 0 | } |
893 | | |
894 | | /** |
895 | | * open_fdt - open a flattened device tree file and determine its size |
896 | | * @filename: path to the FDT file |
897 | | * @size: returns the total size from the FDT header |
898 | | * |
899 | | * Opens the file, reads the FDT header to determine totalsize, validates |
900 | | * it against the file size, then reopens the file for sequential reading. |
901 | | * |
902 | | * Return: file descriptor on success, negative error code on failure |
903 | | */ |
904 | | int open_fdt(const char *filename, size_t *size) |
905 | 441 | { |
906 | 441 | __be32 fdt_hdr[2]; |
907 | 441 | u32 fdt_size; |
908 | 441 | struct stat st; |
909 | 441 | int fd, ret; |
910 | | |
911 | 441 | fd = open(filename, O_RDONLY); |
912 | 441 | if (fd < 0) |
913 | 0 | return fd; |
914 | | |
915 | 441 | ret = fstat(fd, &st); |
916 | 441 | if (ret) |
917 | 0 | goto err; |
918 | | |
919 | 441 | ret = read_full(fd, &fdt_hdr, sizeof(fdt_hdr)); |
920 | 441 | if (ret >= 0 && ret < sizeof(fdt_hdr)) |
921 | 0 | ret = -EILSEQ; |
922 | 441 | if (ret < 0) |
923 | 0 | goto err; |
924 | | |
925 | 441 | fdt_size = be32_to_cpu(fdt_hdr[1]); |
926 | 441 | if (st.st_size < fdt_size) { |
927 | 41 | ret = -ENODATA; |
928 | 41 | goto err; |
929 | 41 | } |
930 | | |
931 | 400 | if (lseek(fd, 0, SEEK_SET) != 0) { |
932 | 0 | ret = -errno; |
933 | 0 | goto err; |
934 | 0 | } |
935 | | |
936 | 400 | *size = fdt_size; |
937 | 400 | return fd; |
938 | 41 | err: |
939 | 41 | close(fd); |
940 | 41 | return ret; |
941 | 400 | } |