d5f9fa5bcd52d2d988c9ebfda82c2ccb45574e5c
[libjh.git] / io.c
1 // Copyright (2013) Jann Horn <jann@thejh.net>
2 // This code is licensed under the AGPLv3.
3
4 HEADER #include <sys/types.h>
5 #include <unistd.h>
6 #include <errno.h>
7 #include <stdlib.h>
8 #include <stdbool.h>
9 #include <sys/stat.h>
10 #include <fcntl.h>
11 #include <string.h>
12
13 // Wrapper for `read` that retries on partial reads.
14 // If last_res is non-NULL, it will be filled with the
15 // result of the last read() call (but all positive numbers
16 // become 1). In other words:
17 //  - -1: error
18 //  - 0: stream ended
19 //  - 1: no problems occured
20 PUBLIC_FN ssize_t read_nointr(int fd, void *buf, size_t count, int *last_res) {
21   errno = 0;
22   size_t done = 0;
23   while (done < count) {
24     ssize_t part_res = read(fd, buf+done, count-done);
25     if (part_res <= 0) {
26       if (last_res) *last_res = part_res;
27       if (done) return done;
28       return part_res;
29     }
30     done += part_res;
31   }
32   if (last_res) *last_res = 1;
33   return done;
34 }
35
36 // Wrapper for `write` that retries on partial writes.
37 // If last_res is non-NULL, it will be filled with the
38 // result of the last write() call (but all positive numbers
39 // become 1). In other words:
40 //  - -1: error
41 //  - 0: stream ended
42 //  - 1: no problems occured
43 PUBLIC_FN ssize_t write_nointr(int fd, void *buf, size_t count, int *last_res) {
44   errno = 0;
45   size_t done = 0;
46   while (done < count) {
47     ssize_t part_res = write(fd, buf+done, count-done);
48     if (part_res <= 0) {
49       if (last_res) *last_res = part_res;
50       if (done) return done;
51       return part_res;
52     }
53     done += part_res;
54   }
55   if (last_res) *last_res = 1;
56   return done;
57 }
58
59 // Read all data from the given file descriptor. Tries fstat()+read()
60 // first, but if fstat() doesn't work, falls back to multiple read()s.
61 // Specify JH_NO_STAT to prevent the fstat() call.
62 // The return value is a malloc'd buffer.
63 // The buffer will be null-terminated, so you can read text files with
64 // this and can specify len_out as NULL.
65 PUBLIC_CONST JH_SLURP_NO_STAT 1
66 PUBLIC_CONST JH_SLURP_REALLOC 2 /* realloc result block if it saves RAM */
67 PUBLIC_FN char *slurp_fd(int fd, size_t *len_out, int flags) {
68   int errno_;
69   
70   // Let's just guess that the file is 1023 bytes. Will become 1024 with
71   // the nullbyte.
72   size_t size_guess = 1023;
73   bool trusted_guess = false; /* can we rely on the guess? */
74   
75   // If we can determine the exact size, we don't have to guess. So try
76   // to determine the exact size.
77   if (!(flags&JH_SLURP_NO_STAT)) {
78     struct stat st;
79     if (fstat(fd, &st) == 0) {
80       if (st.st_size > 0) {
81         size_guess = st.st_size;
82         trusted_guess = true;
83       }
84     }
85   }
86   
87   char *buf = NULL;
88   int done = 0;
89   
90   while (1) {
91     buf = realloc(buf, size_guess+1);
92     if (buf == NULL) return NULL;
93     int last_res;
94     ssize_t read_res = read_nointr(fd, buf+done, size_guess-done, &last_res);
95     if (last_res == -1) { errno_=errno; free(buf); errno=errno_; return NULL; }
96     done += read_res;
97     if (last_res == 0 || trusted_guess) {
98       // out
99       buf[done] = '\0';
100       if (done != size_guess && (flags&JH_SLURP_REALLOC)) {
101         // Well, it'd be weird if shrinking could fail... but meh, I can't find
102         // an explicit statement about this being disallowed, so try staying on
103         // the safe side.
104         char *buf_ = realloc(buf, done+1);
105         if (buf_) buf = buf_;
106       }
107       if (len_out) *len_out = done;
108       return buf;
109     }
110     size_guess<<=1; // try two times the buffer size
111   }
112 }
113
114 PUBLIC_FN char *slurp_file(char *path, size_t *len_out, int flags) {
115   int fd = open(path, O_RDONLY);
116   if (fd == -1) return NULL;
117   char *res = slurp_fd(fd, len_out, flags);
118   int errno_ = errno;
119   close(fd);
120   errno = errno_;
121   return res;
122 }
123
124 // Write data into a file. len can be -1; that means that
125 // the real length is strlen(buf).
126 PUBLIC_FN int write_file(char *path, char *buf, ssize_t len, int open_flags) {
127   if (len == -1) len = strlen(buf);
128   
129   int fd = open(path, open_flags, 0777);
130   if (fd == -1) return 1;
131   ssize_t write_res = write_nointr(fd, buf, len, NULL);
132   int write_errno = errno;
133   int close_res = close(fd);
134   if (write_res != len) {
135     errno = write_errno;
136     return 1;
137   }
138   if (close_res) return 1;
139   return 0;
140 }