/src/h2o/lib/common/file.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) 2015 DeNA Co., Ltd., Kazuho Oku |
3 | | * |
4 | | * Permission is hereby granted, free of charge, to any person obtaining a copy |
5 | | * of this software and associated documentation files (the "Software"), to |
6 | | * deal in the Software without restriction, including without limitation the |
7 | | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
8 | | * sell copies of the Software, and to permit persons to whom the Software is |
9 | | * furnished to do so, subject to the following conditions: |
10 | | * |
11 | | * The above copyright notice and this permission notice shall be included in |
12 | | * all copies or substantial portions of the Software. |
13 | | * |
14 | | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
15 | | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
16 | | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
17 | | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
18 | | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
19 | | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
20 | | * IN THE SOFTWARE. |
21 | | */ |
22 | | #include <errno.h> |
23 | | #include <fcntl.h> |
24 | | #include <stdint.h> |
25 | | #include <stdlib.h> |
26 | | #include <sys/stat.h> |
27 | | #include <sys/types.h> |
28 | | #include <sys/uio.h> |
29 | | #include <unistd.h> |
30 | | #include "h2o/file.h" |
31 | | |
32 | | h2o_iovec_t h2o_file_read(const char *fn) |
33 | 0 | { |
34 | 0 | int fd; |
35 | 0 | struct stat st; |
36 | 0 | h2o_iovec_t ret = {NULL}; |
37 | | |
38 | | /* open */ |
39 | 0 | if ((fd = open(fn, O_RDONLY | O_CLOEXEC)) == -1) |
40 | 0 | goto Error; |
41 | 0 | if (fstat(fd, &st)) |
42 | 0 | goto Error; |
43 | | /* allocate memory */ |
44 | 0 | if (st.st_size > SIZE_MAX) { |
45 | 0 | errno = ENOMEM; |
46 | 0 | goto Error; |
47 | 0 | } |
48 | 0 | if ((ret.base = malloc((size_t)st.st_size)) == NULL) |
49 | 0 | goto Error; |
50 | | /* read */ |
51 | 0 | while (ret.len != (size_t)st.st_size) { |
52 | 0 | ssize_t r; |
53 | 0 | while ((r = read(fd, ret.base + ret.len, (size_t)st.st_size - ret.len)) == -1 && errno == EINTR) |
54 | 0 | ; |
55 | 0 | if (r <= 0) |
56 | 0 | goto Error; |
57 | 0 | ret.len += r; |
58 | 0 | } |
59 | | /* close */ |
60 | 0 | close(fd); |
61 | 0 | return ret; |
62 | | |
63 | 0 | Error: |
64 | 0 | if (fd != -1) |
65 | 0 | close(fd); |
66 | 0 | free(ret.base); |
67 | 0 | return (h2o_iovec_t){NULL}; |
68 | 0 | } |
69 | | |
70 | | int h2o_file_mktemp(const char *fn_template) |
71 | 0 | { |
72 | 0 | int fd; |
73 | 0 | char *tmpfn = alloca(strlen(fn_template) + 1); |
74 | 0 | strcpy(tmpfn, fn_template); |
75 | 0 | if ((fd = mkstemp(tmpfn)) == -1) { |
76 | 0 | return -1; |
77 | 0 | } |
78 | 0 | unlink(tmpfn); |
79 | 0 | return fd; |
80 | 0 | } |