diff options
author | Laszlo Csomor <laszlocsomor@google.com> | 2016-10-04 10:28:54 +0000 |
---|---|---|
committer | Damien Martin-Guillerez <dmarting@google.com> | 2016-10-04 12:49:17 +0000 |
commit | 87425681e46e04e48ace077e01907587d271f971 (patch) | |
tree | 855476b839cdb3fcaeb567c897dc1eafc27fab4c /third_party/ijar | |
parent | 25b952b8fec4a3e514b4f91fbbd5e5133fcab4b7 (diff) |
Rollback of commit fb65f9382f34c2641b5069c30a57a08445b920a3.
*** Reason for rollback ***
Other projects may depend on Ijar without wanting to depend on Bazel.
*** Original change description ***
Ijar: use more utilities from Bazel's source
Remove duplicate implementations from zip_main.cc
--
MOS_MIGRATED_REVID=135086722
Diffstat (limited to 'third_party/ijar')
-rw-r--r-- | third_party/ijar/BUILD | 1 | ||||
-rw-r--r-- | third_party/ijar/zip_main.cc | 56 |
2 files changed, 34 insertions, 23 deletions
diff --git a/third_party/ijar/BUILD b/third_party/ijar/BUILD index c148d29061..f6d7e49e87 100644 --- a/third_party/ijar/BUILD +++ b/third_party/ijar/BUILD @@ -46,7 +46,6 @@ cc_binary( visibility = ["//visibility:public"], deps = [ ":zip", - "//src/main/cpp:blaze_util", "//src/main/cpp/util:file", ], ) diff --git a/third_party/ijar/zip_main.cc b/third_party/ijar/zip_main.cc index 72371aed93..9759348d39 100644 --- a/third_party/ijar/zip_main.cc +++ b/third_party/ijar/zip_main.cc @@ -32,7 +32,6 @@ #include <set> #include <string> -#include "src/main/cpp/blaze_util.h" #include "src/main/cpp/util/file.h" #include "third_party/ijar/zip.h" @@ -53,9 +52,10 @@ class UnzipProcessor : public ZipExtractorProcessor { // Create a processor who will extract the given files (or all files if NULL) // into output_root if "extract" is set to true and will print the list of // files and their unix modes if "verbose" is set to true. - UnzipProcessor(const std::string &output_root, char **files, bool verbose, - bool extract) - : output_root_(output_root), verbose_(verbose), extract_(extract) { + UnzipProcessor(const std::string& output_root, char **files, bool verbose, + bool extract) : output_root_(output_root), + verbose_(verbose), + extract_(extract) { if (files != NULL) { for (int i = 0; files[i] != NULL; i++) { file_names.insert(std::string(files[i])); @@ -78,7 +78,7 @@ class UnzipProcessor : public ZipExtractorProcessor { } private: - const std::string &output_root_; + const std::string& output_root_; const bool verbose_; const bool extract_; std::set<std::string> file_names; @@ -87,7 +87,7 @@ class UnzipProcessor : public ZipExtractorProcessor { // 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. -int mkdirs(const char *path, mode_t mode) { +void mkdirs(const char *path, mode_t mode) { char path_[PATH_MAX]; struct stat statst; strncpy(path_, path, PATH_MAX); @@ -98,14 +98,15 @@ int mkdirs(const char *path, mode_t mode) { *pointer = 0; if (stat(path_, &statst) != 0) { if (mkdir(path_, mode) < 0) { - return -1; + fprintf(stderr, "Cannot create folder %s: %s\n", + path_, strerror(errno)); + abort(); } } *pointer = '/'; } pointer++; } - return 0; } void UnzipProcessor::Process(const char* filename, const u4 attr, @@ -126,16 +127,12 @@ void UnzipProcessor::Process(const char* filename, const u4 attr, std::string path = blaze_util::JoinPath(output_root_, filename); // Directories created must have executable bit set and be owner writeable. // Otherwise, we cannot write or create any file inside. - if (mkdirs(path.c_str(), perm | S_IWUSR | S_IXUSR) < 0) { - fprintf(stderr, "Cannot create folder %s: %s\n", - path.c_str(), strerror(errno)); - abort(); - } + mkdirs(path.c_str(), perm | S_IWUSR | S_IXUSR); if (!isdir) { fd = open(path.c_str(), O_CREAT | O_WRONLY, perm); if (fd < 0) { - fprintf(stderr, "Cannot open file %s for writing: %s\n", path.c_str(), - strerror(errno)); + fprintf(stderr, "Cannot open file %s for writing: %s\n", + path.c_str(), strerror(errno)); abort(); } SYSCALL(write(fd, data, size)); @@ -144,6 +141,19 @@ void UnzipProcessor::Process(const char* filename, const u4 attr, } } +// Get the basename of path and store it in output. output_size +// is the size of the output buffer. +void basename(const char *path, char *output, size_t output_size) { + const char *pointer = strrchr(path, '/'); + if (pointer == NULL) { + pointer = path; + } else { + pointer++; // Skip the leading slash. + } + strncpy(output, pointer, 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; @@ -214,28 +224,30 @@ int add_file(std::unique_ptr<ZipBuilder> const &builder, char *file, } // Compute the path, flattening it if requested - std::string path; + char path[PATH_MAX]; size_t len = strlen(final_path); if (len > PATH_MAX) { fprintf(stderr, "Path too long: %s.\n", final_path); return -1; } if (flatten) { - path = blaze_util::Basename(final_path); + basename(final_path, path, PATH_MAX); } else { - path = final_path; - if (isdir) { + strncpy(path, final_path, PATH_MAX); + path[PATH_MAX - 1] = 0; + if (isdir && len < PATH_MAX - 1) { // Add the trailing slash for folders - path += '/'; + path[len] = '/'; + path[len + 1] = 0; } } if (verbose) { mode_t perm = statst.st_mode & 0777; - printf("%c %o %s\n", isdir ? 'd' : 'f', perm, path.c_str()); + printf("%c %o %s\n", isdir ? 'd' : 'f', perm, path); } - u1 *buffer = builder->NewFile(path.c_str(), mode_to_zipattr(statst.st_mode)); + u1 *buffer = builder->NewFile(path, mode_to_zipattr(statst.st_mode)); if (isdir || statst.st_size == 0) { builder->FinishFile(0); } else { |