diff options
author | Laszlo Csomor <laszlocsomor@google.com> | 2016-12-01 13:16:13 +0000 |
---|---|---|
committer | Irina Iancu <elenairina@google.com> | 2016-12-01 13:31:53 +0000 |
commit | 8d6da00ff0c84d623ccdde7232e0514690d95269 (patch) | |
tree | f63aba37d6950d8847964dc0702b4d0dea812fd7 /third_party/ijar | |
parent | 645dbc4e24f3c5b08cf4701906f4a1e54db3583b (diff) |
Ijar: extract file writing logic to platform_utils
This change takes us closer to compiling ijar,
thus Bazel, with MSVC.
Also update the StatFile method added by
unknown commit to report any errors.
See https://github.com/bazelbuild/bazel/issues/2157
See https://github.com/bazelbuild/bazel/issues/2107
--
MOS_MIGRATED_REVID=140719249
Diffstat (limited to 'third_party/ijar')
-rw-r--r-- | third_party/ijar/platform_utils.cc | 28 | ||||
-rw-r--r-- | third_party/ijar/platform_utils.h | 7 | ||||
-rw-r--r-- | third_party/ijar/zip_main.cc | 21 |
3 files changed, 37 insertions, 19 deletions
diff --git a/third_party/ijar/platform_utils.cc b/third_party/ijar/platform_utils.cc index 19e10c08f1..a3c4cce82b 100644 --- a/third_party/ijar/platform_utils.cc +++ b/third_party/ijar/platform_utils.cc @@ -38,6 +38,7 @@ bool stat_file(const char* path, Stat* result) { #else // not COMPILER_MSVC struct stat statst; if (stat(path, &statst) < 0) { + fprintf(stderr, "Cannot stat file %s: %s\n", path, strerror(errno)); return false; } result->total_size = statst.st_size; @@ -47,4 +48,31 @@ bool stat_file(const char* path, Stat* result) { #endif // COMPILER_MSVC } +bool write_file(const char* path, mode_t perm, const void* data, 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 + int fd = open(path, O_CREAT | O_WRONLY, perm); + if (fd < 0) { + fprintf(stderr, "Cannot open file %s for writing: %s\n", + path, strerror(errno)); + return false; + } + bool result = true; + if (write(fd, data, size) != size) { + fprintf(stderr, "Cannot write %zu bytes to file %s: %s\n", + size, path, strerror(errno)); + result = false; + } + 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 cb488196e4..9ba9247b2f 100644 --- a/third_party/ijar/platform_utils.h +++ b/third_party/ijar/platform_utils.h @@ -35,8 +35,15 @@ struct Stat { // Writes stat data into `result` about the file under `path`. // Returns true upon success: file is found and can be stat'ed. +// Returns false upon failure and reports the error to stderr. bool stat_file(const char* path, Stat* result); +// Writes `size` bytes from `data` into file under `path`. +// The file is created or overwritten and is set to have `perm` permissions. +// Returns true upon success: file is created and all data is written. +// 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); + } // 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 351faba616..4347f018ed 100644 --- a/third_party/ijar/zip_main.cc +++ b/third_party/ijar/zip_main.cc @@ -37,13 +37,6 @@ namespace devtools_ijar { -#define SYSCALL(expr) do { \ - if ((expr) < 0) { \ - perror(#expr); \ - abort(); \ - } \ - } while (0) - // // A ZipExtractorProcessor that extract files in the ZIP file. // @@ -142,20 +135,12 @@ void UnzipProcessor::Process(const char* filename, const u4 attr, } if (extract_) { char path[PATH_MAX]; - int fd; concat_path(path, PATH_MAX, output_root_, filename); // Directories created must have executable bit set and be owner writeable. // Otherwise, we cannot write or create any file inside. mkdirs(path, perm | S_IWUSR | S_IXUSR); - if (!isdir) { - fd = open(path, O_CREAT | O_WRONLY, perm); - if (fd < 0) { - fprintf(stderr, "Cannot open file %s for writing: %s\n", - path, strerror(errno)); - abort(); - } - SYSCALL(write(fd, data, size)); - SYSCALL(close(fd)); + if (!isdir && !write_file(path, perm, data, size)) { + abort(); } } } @@ -230,7 +215,6 @@ int add_file(std::unique_ptr<ZipBuilder> const &builder, char *file, file_stat.file_mode = 0666; if (file != NULL) { if (!stat_file(file, &file_stat)) { - fprintf(stderr, "Cannot stat file %s: %s.\n", file, strerror(errno)); return -1; } } @@ -293,7 +277,6 @@ int add_file(std::unique_ptr<ZipBuilder> const &builder, char *file, char **read_filelist(char *filename) { Stat file_stat; if (!stat_file(filename, &file_stat)) { - fprintf(stderr, "Cannot stat file %s: %s.\n", filename, strerror(errno)); return NULL; } |