Skip to content

Commit

Permalink
Ijar: extract file writing logic to platform_utils
Browse files Browse the repository at this point in the history
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 #2157
See #2107

--
MOS_MIGRATED_REVID=140719249
  • Loading branch information
laszlocsomor authored and iirina committed Dec 1, 2016
1 parent 645dbc4 commit 8d6da00
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 19 deletions.
28 changes: 28 additions & 0 deletions third_party/ijar/platform_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
7 changes: 7 additions & 0 deletions third_party/ijar/platform_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_
21 changes: 2 additions & 19 deletions third_party/ijar/zip_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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.
//
Expand Down Expand Up @@ -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();
}
}
}
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit 8d6da00

Please sign in to comment.