Ijar: extract [] 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=140723658
diff --git a/third_party/ijar/platform_utils.cc b/third_party/ijar/platform_utils.cc
index 1ddfd08..a7e3daf 100644
--- a/third_party/ijar/platform_utils.cc
+++ b/third_party/ijar/platform_utils.cc
@@ -29,6 +29,8 @@
namespace devtools_ijar {
+using std::string;
+
bool stat_file(const char* path, Stat* result) {
#ifdef COMPILER_MSVC
// TODO(laszlocsomor) 2016-12-01: implement this and other methods, in order
@@ -113,4 +115,21 @@
#endif // COMPILER_MSVC
}
+string get_cwd() {
+#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 "";
+#else // not COMPILER_MSVC
+ char cwd[PATH_MAX];
+ if (getcwd(cwd, PATH_MAX) == NULL) {
+ fprintf(stderr, "getcwd() failed: %s\n", strerror(errno));
+ return "";
+ } else {
+ return string(cwd);
+ }
+#endif // COMPILER_MSVC
+}
+
} // namespace devtools_ijar
diff --git a/third_party/ijar/platform_utils.h b/third_party/ijar/platform_utils.h
index 65ba902..b66a2b1 100644
--- a/third_party/ijar/platform_utils.h
+++ b/third_party/ijar/platform_utils.h
@@ -49,6 +49,10 @@
// Returns false upon failure and reports the error to stderr.
bool read_file(const char* path, void* buffer, size_t size);
+// Returns the current working directory.
+// Returns the empty string upon failure and reports the error to stderr.
+std::string get_cwd();
+
} // 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 2b99c5f..578d64c 100644
--- a/third_party/ijar/zip_main.cc
+++ b/third_party/ijar/zip_main.cc
@@ -26,7 +26,6 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include <unistd.h>
#include <memory>
#include <set>
@@ -161,17 +160,16 @@
// Execute the extraction (or just listing if just v is provided)
int extract(char *zipfile, char* exdir, char **files, bool verbose,
bool extract) {
- char cwd[PATH_MAX];
- if (getcwd(cwd, PATH_MAX) == NULL) {
- fprintf(stderr, "getcwd() failed: %s.\n", strerror(errno));
+ std::string cwd = get_cwd();
+ if (cwd.empty()) {
return -1;
}
char output_root[PATH_MAX];
if (exdir != NULL) {
- concat_path(output_root, PATH_MAX, cwd, exdir);
+ concat_path(output_root, PATH_MAX, cwd.c_str(), exdir);
} else {
- strncpy(output_root, cwd, PATH_MAX);
+ strncpy(output_root, cwd.c_str(), PATH_MAX);
}
UnzipProcessor processor(output_root, files, verbose, extract);