| ... | @@ -38,22 +38,24 @@ void os_spawn_process(const char *exe, ZigList<const char *> &args, bool detache | ... | @@ -38,22 +38,24 @@ void os_spawn_process(const char *exe, ZigList<const char *> &args, bool detache |
| 38 | zig_panic("execvp failed: %s", strerror(errno)); | 38 | zig_panic("execvp failed: %s", strerror(errno)); |
| 39 | } | 39 | } |
| 40 | | 40 | |
| 41 | static void read_all_fd_stream(int fd, Buf *out_buf) { | 41 | static int read_all_fd_stream(int fd, Buf *out_buf) { |
| 42 | static const ssize_t buf_size = 0x2000; | 42 | static const ssize_t buf_size = 0x2000; |
| 43 | buf_resize(out_buf, buf_size); | 43 | buf_resize(out_buf, buf_size); |
| 44 | ssize_t actual_buf_len = 0; | 44 | ssize_t actual_buf_len = 0; |
| 45 | for (;;) { | 45 | for (;;) { |
| 46 | ssize_t amt_read = read(fd, buf_ptr(out_buf), buf_len(out_buf)); | 46 | ssize_t amt_read = read(fd, buf_ptr(out_buf), buf_len(out_buf)); |
| 47 | if (amt_read < 0) | 47 | if (amt_read < 0) { |
| 48 | zig_panic("fd read error"); | 48 | return ErrorFileSystem; |
| | 49 | } |
| 49 | actual_buf_len += amt_read; | 50 | actual_buf_len += amt_read; |
| 50 | if (amt_read == 0) { | 51 | if (amt_read == 0) { |
| 51 | buf_resize(out_buf, actual_buf_len); | 52 | buf_resize(out_buf, actual_buf_len); |
| 52 | return; | 53 | return 0; |
| 53 | } | 54 | } |
| 54 | | 55 | |
| 55 | buf_resize(out_buf, actual_buf_len + buf_size); | 56 | buf_resize(out_buf, actual_buf_len + buf_size); |
| 56 | } | 57 | } |
| | 58 | zig_unreachable(); |
| 57 | } | 59 | } |
| 58 | | 60 | |
| 59 | void os_path_split(Buf *full_path, Buf *out_dirname, Buf *out_basename) { | 61 | void os_path_split(Buf *full_path, Buf *out_dirname, Buf *out_basename) { |
| ... | @@ -142,46 +144,7 @@ void os_write_file(Buf *full_path, Buf *contents) { | ... | @@ -142,46 +144,7 @@ void os_write_file(Buf *full_path, Buf *contents) { |
| 142 | } | 144 | } |
| 143 | | 145 | |
| 144 | int os_fetch_file(FILE *f, Buf *out_contents) { | 146 | int os_fetch_file(FILE *f, Buf *out_contents) { |
| 145 | int fd = fileno(f); | 147 | return read_all_fd_stream(fileno(f), out_contents); |
| 146 | struct stat st; | | |
| 147 | if (fstat(fd, &st)) { | | |
| 148 | switch (errno) { | | |
| 149 | case EACCES: | | |
| 150 | return ErrorAccess; | | |
| 151 | case ENOENT: | | |
| 152 | return ErrorFileNotFound; | | |
| 153 | case ENOMEM: | | |
| 154 | return ErrorSystemResources; | | |
| 155 | case EINTR: | | |
| 156 | return ErrorInterrupted; | | |
| 157 | case EINVAL: | | |
| 158 | zig_unreachable(); | | |
| 159 | default: | | |
| 160 | return ErrorFileSystem; | | |
| 161 | } | | |
| 162 | } | | |
| 163 | off_t big_size = st.st_size; | | |
| 164 | if (big_size > INT_MAX) { | | |
| 165 | return ErrorFileTooBig; | | |
| 166 | } | | |
| 167 | int size = (int)big_size; | | |
| 168 | | | |
| 169 | buf_resize(out_contents, size); | | |
| 170 | ssize_t ret = read(fd, buf_ptr(out_contents), size); | | |
| 171 | | | |
| 172 | if (ret != size) { | | |
| 173 | switch (errno) { | | |
| 174 | case EINTR: | | |
| 175 | return ErrorInterrupted; | | |
| 176 | case EINVAL: | | |
| 177 | case EISDIR: | | |
| 178 | zig_unreachable(); | | |
| 179 | default: | | |
| 180 | return ErrorFileSystem; | | |
| 181 | } | | |
| 182 | } | | |
| 183 | | | |
| 184 | return 0; | | |
| 185 | } | 148 | } |
| 186 | | 149 | |
| 187 | int os_fetch_file_path(Buf *full_path, Buf *out_contents) { | 150 | int os_fetch_file_path(Buf *full_path, Buf *out_contents) { |