diff options
Diffstat (limited to 'third_party/ijar')
-rw-r--r-- | third_party/ijar/platform_utils.cc | 38 | ||||
-rw-r--r-- | third_party/ijar/platform_utils.h | 5 | ||||
-rw-r--r-- | third_party/ijar/zip_main.cc | 43 |
3 files changed, 46 insertions, 40 deletions
diff --git a/third_party/ijar/platform_utils.cc b/third_party/ijar/platform_utils.cc index a3c4cce82b..1ddfd088a0 100644 --- a/third_party/ijar/platform_utils.cc +++ b/third_party/ijar/platform_utils.cc @@ -75,4 +75,42 @@ bool write_file(const char* path, mode_t perm, const void* data, size_t size) { #endif // COMPILER_MSVC } +bool read_file(const char* path, void* buffer, size_t size) { +#ifdef COMPILER_MSVC + // TODO(laszlocsomor) 2016-12-01: implement this and other methods, in order + // to close https://github.com/bazelbuild/bazel/issues/2157. + fprintf(stderr, "Not yet implemented on Windows\n"); + return false; +#else // not COMPILER_MSVC + // read the input file + int fd = open(path, O_RDONLY); + if (fd < 0) { + fprintf(stderr, "Can't open file %s for reading: %s\n", + path, strerror(errno)); + return false; + } + bool result = true; + size_t nb_read = 0; + while (nb_read < size) { + size_t to_read = size - nb_read; + if (to_read > 16384 /* 16K */) { + to_read = 16384; + } + ssize_t r = read(fd, static_cast<uint8_t*>(buffer) + nb_read, to_read); + if (r < 0) { + fprintf(stderr, "Can't read %zu bytes from file %s: %s\n", + to_read, path, strerror(errno)); + result = false; + break; + } + nb_read += r; + } + if (close(fd)) { + fprintf(stderr, "Cannot close file %s: %s\n", path, strerror(errno)); + result = false; + } + return result; +#endif // COMPILER_MSVC +} + } // namespace devtools_ijar diff --git a/third_party/ijar/platform_utils.h b/third_party/ijar/platform_utils.h index 9ba9247b2f..65ba902dbf 100644 --- a/third_party/ijar/platform_utils.h +++ b/third_party/ijar/platform_utils.h @@ -44,6 +44,11 @@ bool stat_file(const char* path, Stat* result); // Returns false upon failure and reports the error to stderr. bool write_file(const char* path, mode_t perm, const void* data, size_t size); +// Reads at most `size` bytes into `buffer` from the file under `path`. +// Returns true upon success: file is opened and all data is read. +// Returns false upon failure and reports the error to stderr. +bool read_file(const char* path, void* buffer, size_t size); + } // namespace devtools_ijar #endif // THIRD_PARTY_IJAR_PLATFORM_UTILS_H_ diff --git a/third_party/ijar/zip_main.cc b/third_party/ijar/zip_main.cc index 4347f018ed..2b99c5f487 100644 --- a/third_party/ijar/zip_main.cc +++ b/third_party/ijar/zip_main.cc @@ -21,13 +21,13 @@ // #include <errno.h> -#include <fcntl.h> #include <limits.h> #include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> + #include <memory> #include <set> #include <string> @@ -158,23 +158,6 @@ void basename(const char *path, char *output, size_t output_size) { output[output_size-1] = 0; } -// copy size bytes from file descriptor fd into buffer. -int copy_file_to_buffer(int fd, size_t size, void *buffer) { - size_t nb_read = 0; - while (nb_read < size) { - size_t to_read = size - nb_read; - if (to_read > 16384 /* 16K */) { - to_read = 16384; - } - ssize_t r = read(fd, static_cast<uint8_t *>(buffer) + nb_read, to_read); - if (r < 0) { - return -1; - } - nb_read += r; - } - return 0; -} - // Execute the extraction (or just listing if just v is provided) int extract(char *zipfile, char* exdir, char **files, bool verbose, bool extract) { @@ -254,19 +237,9 @@ int add_file(std::unique_ptr<ZipBuilder> const &builder, char *file, if (isdir || file_stat.total_size == 0) { builder->FinishFile(0); } else { - // read the input file - int fd = open(file, O_RDONLY); - if (fd < 0) { - fprintf(stderr, "Can't open file %s for reading: %s.\n", file, - strerror(errno)); - return -1; - } - if (copy_file_to_buffer(fd, file_stat.total_size, buffer) < 0) { - fprintf(stderr, "Can't read file %s: %s.\n", file, strerror(errno)); - close(fd); + if (!read_file(file, buffer, file_stat.total_size)) { return -1; } - close(fd); builder->FinishFile(file_stat.total_size, compress, true); } return 0; @@ -280,20 +253,10 @@ char **read_filelist(char *filename) { return NULL; } - int fd = open(filename, O_RDONLY); - if (fd < 0) { - fprintf(stderr, "Can't open file %s for reading: %s.\n", filename, - strerror(errno)); - return NULL; - } - char *data = static_cast<char *>(malloc(file_stat.total_size)); - if (copy_file_to_buffer(fd, file_stat.total_size, data) < 0) { - fprintf(stderr, "Can't read file %s: %s.\n", filename, strerror(errno)); - close(fd); + if (!read_file(filename, data, file_stat.total_size)) { return NULL; } - close(fd); int nb_entries = 1; for (int i = 0; i < file_stat.total_size; i++) { |