| 40 | namespace fleece { |
| 41 | |
| 42 | alloc_slice readFile(const char *path) { |
| 43 | int fd = ::_open(path, O_RDONLY | O_BINARY); |
| 44 | if (fd < 0) |
| 45 | FleeceException::_throwErrno("Can't open file %s", path); |
| 46 | struct stat stat; |
| 47 | fstat(fd, &stat); |
| 48 | if (stat.st_size > SIZE_MAX) |
| 49 | throw std::logic_error("File too big for address space"); |
| 50 | alloc_slice data(narrow_cast<size_t>(stat.st_size)); |
| 51 | ssize_t bytesRead = narrow_cast<ssize_t>(::_read(fd, (void*)data.buf, narrow_cast<unsigned int>(data.size))); |
| 52 | if (bytesRead < narrow_cast<ssize_t>(data.size)) |
| 53 | FleeceException::_throwErrno("Can't read file %s", path); |
| 54 | ::_close(fd); |
| 55 | return data; |
| 56 | } |
| 57 | |
| 58 | void writeToFile(slice s, const char *path, int mode) { |
| 59 | int fd = ::_open(path, mode | O_WRONLY | O_BINARY, 0600); |
no outgoing calls