Ijar: extract MakeDirs to platform_utils zip_main.cc no longer needs <unistd.h>. This change takes us closer to compiling ijar, thus Bazel, with MSVC. See https://github.com/bazelbuild/bazel/issues/2157 See https://github.com/bazelbuild/bazel/issues/2107 -- MOS_MIGRATED_REVID=140724421
diff --git a/third_party/ijar/platform_utils.cc b/third_party/ijar/platform_utils.cc index a7e3daf..8ffcb06 100644 --- a/third_party/ijar/platform_utils.cc +++ b/third_party/ijar/platform_utils.cc
@@ -132,4 +132,35 @@ #endif // COMPILER_MSVC } +bool make_dirs(const char* path, mode_t mode) { +#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 + // Directories created must have executable bit set and be owner writeable. + // Otherwise, we cannot write or create any file inside. + mode |= S_IWUSR | S_IXUSR; + char path_[PATH_MAX]; + Stat file_stat; + strncpy(path_, path, PATH_MAX); + path_[PATH_MAX - 1] = 0; + char* pointer = path_; + while ((pointer = strchr(pointer, '/')) != NULL) { + if (path_ != pointer) { // skip leading slash + *pointer = 0; + if (!stat_file(path_, &file_stat) && mkdir(path_, mode) < 0) { + fprintf(stderr, "Cannot create folder %s: %s\n", + path_, strerror(errno)); + return false; + } + *pointer = '/'; + } + pointer++; + } + return true; +#endif // COMPILER_MSVC +} + } // namespace devtools_ijar
diff --git a/third_party/ijar/platform_utils.h b/third_party/ijar/platform_utils.h index b66a2b1..8870ae2 100644 --- a/third_party/ijar/platform_utils.h +++ b/third_party/ijar/platform_utils.h
@@ -53,6 +53,14 @@ // Returns the empty string upon failure and reports the error to stderr. std::string get_cwd(); +// Do a recursive mkdir of all folders of path except the last path +// segment (if path ends with a / then the last path segment is empty). +// All folders are created using "perm" for creation mode, and are writable and +// openable by the current user. +// Returns true if all directories were created and permissions set. +// Returns false upon failure and reports the error to stderr. +bool make_dirs(const char* path, mode_t perm); + } // 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 578d64c..74853dc 100644 --- a/third_party/ijar/zip_main.cc +++ b/third_party/ijar/zip_main.cc
@@ -94,31 +94,6 @@ } } -// Do a recursive mkdir of all folders of path except the last path -// segment (if path ends with a / then the last path segment is empty). -// All folders are created using "mode" for creation mode. -void mkdirs(const char *path, mode_t mode) { - char path_[PATH_MAX]; - Stat file_stat; - strncpy(path_, path, PATH_MAX); - path_[PATH_MAX-1] = 0; - char *pointer = path_; - while ((pointer = strchr(pointer, '/')) != NULL) { - if (path_ != pointer) { // skip leading slash - *pointer = 0; - if (!stat_file(path_, &file_stat)) { - if (mkdir(path_, mode) < 0) { - fprintf(stderr, "Cannot create folder %s: %s\n", - path_, strerror(errno)); - abort(); - } - } - *pointer = '/'; - } - pointer++; - } -} - void UnzipProcessor::Process(const char* filename, const u4 attr, const u1* data, const size_t size) { mode_t mode = zipattr_to_mode(attr); @@ -135,10 +110,8 @@ if (extract_) { char path[PATH_MAX]; 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 && !write_file(path, perm, data, size)) { + if (!make_dirs(path, perm) || + (!isdir && !write_file(path, perm, data, size))) { abort(); } }