blob: 97d0023c4d955ef6f10918f5af7ae4874d3b257d [file] [log] [blame]
Laszlo Csomor645dbc42016-12-01 12:56:43 +00001// Copyright 2016 The Bazel Authors. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#include "third_party/ijar/platform_utils.h"
16
Laszlo Csomor645dbc42016-12-01 12:56:43 +000017#include <limits.h>
18#include <stdio.h>
19
Loo Rong Jie4022bac2018-06-11 02:04:52 -070020#if defined(_WIN32) || defined(__CYGWIN__)
Laszlo Csomor9f15d152017-03-01 15:55:34 +000021#include <windows.h>
Loo Rong Jie4022bac2018-06-11 02:04:52 -070022#else // !(defined(_WIN32) || defined(__CYGWIN__))
Googler14b53892017-03-02 14:48:01 +000023#include <sys/stat.h>
Laszlo Csomorb2f1e192017-03-03 09:41:53 +000024#include <sys/types.h>
Googler14b53892017-03-02 14:48:01 +000025#include <unistd.h>
Loo Rong Jie4022bac2018-06-11 02:04:52 -070026#endif // defined(_WIN32) || defined(__CYGWIN__)
Googler14b53892017-03-02 14:48:01 +000027
Laszlo Csomor645dbc42016-12-01 12:56:43 +000028#include <string>
29
Laszlo Csomor9f15d152017-03-01 15:55:34 +000030#include "src/main/cpp/util/errors.h"
Laszlo Csomorb2f1e192017-03-03 09:41:53 +000031#include "src/main/cpp/util/file.h"
Laszlo Csomor9f15d152017-03-01 15:55:34 +000032#include "src/main/cpp/util/file_platform.h"
ccalvarin8448f572018-04-06 12:42:09 -070033#include "src/main/cpp/util/logging.h"
ccalvarinac69da02018-06-05 15:27:26 -070034#include "src/main/cpp/util/path.h"
35#include "src/main/cpp/util/path_platform.h"
Laszlo Csomor9f15d152017-03-01 15:55:34 +000036
Laszlo Csomor645dbc42016-12-01 12:56:43 +000037namespace devtools_ijar {
38
Laszlo Csomor8457f3f2016-12-01 14:25:18 +000039using std::string;
40
Laszlo Csomor645dbc42016-12-01 12:56:43 +000041bool stat_file(const char* path, Stat* result) {
Loo Rong Jie4022bac2018-06-11 02:04:52 -070042#if defined(_WIN32) || defined(__CYGWIN__)
Laszlo Csomor9f15d152017-03-01 15:55:34 +000043 std::wstring wpath;
Laszlo Csomor8c400c82018-04-24 01:44:17 -070044 std::string error;
45 if (!blaze_util::AsAbsoluteWindowsPath(path, &wpath, &error)) {
ccalvarin8448f572018-04-06 12:42:09 -070046 BAZEL_DIE(255) << "stat_file: AsAbsoluteWindowsPath(" << path
Laszlo Csomor8c400c82018-04-24 01:44:17 -070047 << ") failed: " << error;
Laszlo Csomor9f15d152017-03-01 15:55:34 +000048 }
Laszlo Csomor6fddc832018-11-08 03:12:53 -080049
Laszlo Csomor9f15d152017-03-01 15:55:34 +000050 bool success = false;
51 BY_HANDLE_FILE_INFORMATION info;
52 HANDLE handle = ::CreateFileW(
53 /* lpFileName */ wpath.c_str(),
54 /* dwDesiredAccess */ GENERIC_READ,
55 /* dwShareMode */ FILE_SHARE_READ,
56 /* lpSecurityAttributes */ NULL,
57 /* dwCreationDisposition */ OPEN_EXISTING,
58 /* dwFlagsAndAttributes */ FILE_ATTRIBUTE_NORMAL,
59 /* hTemplateFile */ NULL);
Laszlo Csomor6fddc832018-11-08 03:12:53 -080060
61 if (handle == INVALID_HANDLE_VALUE) {
62 // Opening it as a file failed, try opening it as a directory.
63 handle = ::CreateFileW(
64 /* lpFileName */ wpath.c_str(),
65 /* dwDesiredAccess */ GENERIC_READ,
66 /* dwShareMode */ FILE_SHARE_READ,
67 /* lpSecurityAttributes */ NULL,
68 /* dwCreationDisposition */ OPEN_EXISTING,
69 /* dwFlagsAndAttributes */ FILE_FLAG_BACKUP_SEMANTICS,
70 /* hTemplateFile */ NULL);
71 }
72
Laszlo Csomor9f15d152017-03-01 15:55:34 +000073 if (handle != INVALID_HANDLE_VALUE &&
74 ::GetFileInformationByHandle(handle, &info)) {
75 success = true;
Laszlo Csomor6fddc832018-11-08 03:12:53 -080076 bool is_dir = (info.dwFileAttributes != INVALID_FILE_ATTRIBUTES) &&
77 (info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
Laszlo Csomor9f15d152017-03-01 15:55:34 +000078 // TODO(laszlocsomor): use info.nFileSizeHigh after we updated total_size to
79 // be u8 type.
Laszlo Csomor6fddc832018-11-08 03:12:53 -080080 result->total_size = is_dir ? 0 : info.nFileSizeLow;
Laszlo Csomor9f15d152017-03-01 15:55:34 +000081 // TODO(laszlocsomor): query the actual permissions and write in file_mode.
82 result->file_mode = 0777;
Laszlo Csomor6fddc832018-11-08 03:12:53 -080083 result->is_directory = is_dir;
Laszlo Csomor9f15d152017-03-01 15:55:34 +000084 }
85 ::CloseHandle(handle);
86 return success;
Loo Rong Jie4022bac2018-06-11 02:04:52 -070087#else // !(defined(_WIN32) || defined(__CYGWIN__))
Laszlo Csomor645dbc42016-12-01 12:56:43 +000088 struct stat statst;
89 if (stat(path, &statst) < 0) {
90 return false;
91 }
92 result->total_size = statst.st_size;
93 result->file_mode = statst.st_mode;
94 result->is_directory = (statst.st_mode & S_IFDIR) != 0;
95 return true;
Loo Rong Jie4022bac2018-06-11 02:04:52 -070096#endif // defined(_WIN32) || defined(__CYGWIN__)
Laszlo Csomor645dbc42016-12-01 12:56:43 +000097}
98
Laszlo Csomorb2f1e192017-03-03 09:41:53 +000099bool write_file(const char* path, unsigned int perm, const void* data,
100 size_t size) {
101 return blaze_util::WriteFile(data, size, path, perm);
Laszlo Csomor8d6da002016-12-01 13:16:13 +0000102}
103
Laszlo Csomord2ed0692016-12-01 14:04:35 +0000104bool read_file(const char* path, void* buffer, size_t size) {
Laszlo Csomorb2f1e192017-03-03 09:41:53 +0000105 return blaze_util::ReadFile(path, buffer, size);
Laszlo Csomord2ed0692016-12-01 14:04:35 +0000106}
107
Laszlo Csomorb2f1e192017-03-03 09:41:53 +0000108string get_cwd() { return blaze_util::GetCwd(); }
Laszlo Csomor8457f3f2016-12-01 14:25:18 +0000109
Laszlo Csomorb2f1e192017-03-03 09:41:53 +0000110bool make_dirs(const char* path, unsigned int mode) {
Loo Rong Jie4022bac2018-06-11 02:04:52 -0700111#ifndef _WIN32
Laszlo Csomorb2f1e192017-03-03 09:41:53 +0000112 // TODO(laszlocsomor): respect `mode` on Windows/MSVC.
Laszlo Csomorb8caca02016-12-01 14:37:46 +0000113 mode |= S_IWUSR | S_IXUSR;
Loo Rong Jie4022bac2018-06-11 02:04:52 -0700114#endif // not _WIN32
Laszlo Csomorb2f1e192017-03-03 09:41:53 +0000115 string spath(path);
116 if (spath.empty()) {
117 return true;
Laszlo Csomorb8caca02016-12-01 14:37:46 +0000118 }
Laszlo Csomorb2f1e192017-03-03 09:41:53 +0000119 if (spath.back() != '/' && spath.back() != '\\') {
120 spath = blaze_util::Dirname(spath);
121 }
122 return blaze_util::MakeDirectories(spath, mode);
Laszlo Csomorb8caca02016-12-01 14:37:46 +0000123}
124
Laszlo Csomor645dbc42016-12-01 12:56:43 +0000125} // namespace devtools_ijar