diff options
| author | Josh Wolfe <thejoshwolfe@gmail.com> | 2015-12-01 10:44:30 -0700 |
|---|---|---|
| committer | Josh Wolfe <thejoshwolfe@gmail.com> | 2015-12-01 10:44:30 -0700 |
| commit | ab327344b671bff7f874f24691d7e3f19176167c (patch) | |
| tree | c437760bbc1f7059417b21e37c3d15a42534d4e8 /src/os.cpp | |
| parent | 9278dbedd5242bf4253da29c0fab36eea9dda61b (diff) | |
| parent | dfb6682089ad758b7ba72733778a9aa8c544c164 (diff) | |
| download | zig-ab327344b671bff7f874f24691d7e3f19176167c.tar.gz zig-ab327344b671bff7f874f24691d7e3f19176167c.zip | |
merge conflicts
Diffstat (limited to 'src/os.cpp')
| -rw-r--r-- | src/os.cpp | 60 |
1 files changed, 52 insertions, 8 deletions
diff --git a/src/os.cpp b/src/os.cpp index 61dd1c01ee..be0b01f8a7 100644 --- a/src/os.cpp +++ b/src/os.cpp @@ -7,6 +7,7 @@ #include "os.hpp" #include "util.hpp" +#include "error.hpp" #include <unistd.h> #include <errno.h> @@ -143,26 +144,65 @@ void os_write_file(Buf *full_path, Buf *contents) { int os_fetch_file(FILE *f, Buf *out_contents) { int fd = fileno(f); struct stat st; - if (fstat(fd, &st)) - zig_panic("unable to stat file: %s", strerror(errno)); + if (fstat(fd, &st)) { + switch (errno) { + case EACCES: + return ErrorAccess; + case ENOENT: + return ErrorFileNotFound; + case ENOMEM: + return ErrorSystemResources; + case EINTR: + return ErrorInterrupted; + case EINVAL: + zig_unreachable(); + default: + return ErrorFileSystem; + } + } off_t big_size = st.st_size; - if (big_size > INT_MAX) - zig_panic("file too big"); + if (big_size > INT_MAX) { + return ErrorFileTooBig; + } int size = (int)big_size; buf_resize(out_contents, size); ssize_t ret = read(fd, buf_ptr(out_contents), size); - if (ret != size) - zig_panic("unable to read file: %s", strerror(errno)); + if (ret != size) { + switch (errno) { + case EINTR: + return ErrorInterrupted; + case EINVAL: + case EISDIR: + zig_unreachable(); + default: + return ErrorFileSystem; + } + } return 0; } int os_fetch_file_path(Buf *full_path, Buf *out_contents) { FILE *f = fopen(buf_ptr(full_path), "rb"); - if (!f) - zig_panic("unable to open %s: %s\n", buf_ptr(full_path), strerror(errno)); + if (!f) { + switch (errno) { + case EACCES: + return ErrorAccess; + case EINTR: + return ErrorInterrupted; + case EINVAL: + zig_unreachable(); + case ENFILE: + case ENOMEM: + return ErrorSystemResources; + case ENOENT: + return ErrorFileNotFound; + default: + return ErrorFileSystem; + } + } int result = os_fetch_file(f, out_contents); fclose(f); return result; @@ -180,3 +220,7 @@ int os_get_cwd(Buf *out_cwd) { return 0; } + +bool os_stderr_tty(void) { + return isatty(STDERR_FILENO); +} |
