Laszlo Csomor | 645dbc4 | 2016-12-01 12:56:43 +0000 | [diff] [blame] | 1 | // 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 Csomor | 645dbc4 | 2016-12-01 12:56:43 +0000 | [diff] [blame] | 17 | #include <limits.h> |
| 18 | #include <stdio.h> |
| 19 | |
Loo Rong Jie | 4022bac | 2018-06-11 02:04:52 -0700 | [diff] [blame] | 20 | #if defined(_WIN32) || defined(__CYGWIN__) |
Laszlo Csomor | 9f15d15 | 2017-03-01 15:55:34 +0000 | [diff] [blame] | 21 | #include <windows.h> |
Loo Rong Jie | 4022bac | 2018-06-11 02:04:52 -0700 | [diff] [blame] | 22 | #else // !(defined(_WIN32) || defined(__CYGWIN__)) |
Googler | 14b5389 | 2017-03-02 14:48:01 +0000 | [diff] [blame] | 23 | #include <sys/stat.h> |
Laszlo Csomor | b2f1e19 | 2017-03-03 09:41:53 +0000 | [diff] [blame] | 24 | #include <sys/types.h> |
Googler | 14b5389 | 2017-03-02 14:48:01 +0000 | [diff] [blame] | 25 | #include <unistd.h> |
Loo Rong Jie | 4022bac | 2018-06-11 02:04:52 -0700 | [diff] [blame] | 26 | #endif // defined(_WIN32) || defined(__CYGWIN__) |
Googler | 14b5389 | 2017-03-02 14:48:01 +0000 | [diff] [blame] | 27 | |
Laszlo Csomor | 645dbc4 | 2016-12-01 12:56:43 +0000 | [diff] [blame] | 28 | #include <string> |
| 29 | |
Laszlo Csomor | 9f15d15 | 2017-03-01 15:55:34 +0000 | [diff] [blame] | 30 | #include "src/main/cpp/util/errors.h" |
Laszlo Csomor | b2f1e19 | 2017-03-03 09:41:53 +0000 | [diff] [blame] | 31 | #include "src/main/cpp/util/file.h" |
Laszlo Csomor | 9f15d15 | 2017-03-01 15:55:34 +0000 | [diff] [blame] | 32 | #include "src/main/cpp/util/file_platform.h" |
ccalvarin | 8448f57 | 2018-04-06 12:42:09 -0700 | [diff] [blame] | 33 | #include "src/main/cpp/util/logging.h" |
ccalvarin | ac69da0 | 2018-06-05 15:27:26 -0700 | [diff] [blame] | 34 | #include "src/main/cpp/util/path.h" |
| 35 | #include "src/main/cpp/util/path_platform.h" |
Laszlo Csomor | 9f15d15 | 2017-03-01 15:55:34 +0000 | [diff] [blame] | 36 | |
Laszlo Csomor | 645dbc4 | 2016-12-01 12:56:43 +0000 | [diff] [blame] | 37 | namespace devtools_ijar { |
| 38 | |
Laszlo Csomor | 8457f3f | 2016-12-01 14:25:18 +0000 | [diff] [blame] | 39 | using std::string; |
| 40 | |
Laszlo Csomor | 645dbc4 | 2016-12-01 12:56:43 +0000 | [diff] [blame] | 41 | bool stat_file(const char* path, Stat* result) { |
Loo Rong Jie | 4022bac | 2018-06-11 02:04:52 -0700 | [diff] [blame] | 42 | #if defined(_WIN32) || defined(__CYGWIN__) |
Laszlo Csomor | 9f15d15 | 2017-03-01 15:55:34 +0000 | [diff] [blame] | 43 | std::wstring wpath; |
Laszlo Csomor | 8c400c8 | 2018-04-24 01:44:17 -0700 | [diff] [blame] | 44 | std::string error; |
| 45 | if (!blaze_util::AsAbsoluteWindowsPath(path, &wpath, &error)) { |
ccalvarin | 8448f57 | 2018-04-06 12:42:09 -0700 | [diff] [blame] | 46 | BAZEL_DIE(255) << "stat_file: AsAbsoluteWindowsPath(" << path |
Laszlo Csomor | 8c400c8 | 2018-04-24 01:44:17 -0700 | [diff] [blame] | 47 | << ") failed: " << error; |
Laszlo Csomor | 9f15d15 | 2017-03-01 15:55:34 +0000 | [diff] [blame] | 48 | } |
Laszlo Csomor | 6fddc83 | 2018-11-08 03:12:53 -0800 | [diff] [blame] | 49 | |
Laszlo Csomor | 9f15d15 | 2017-03-01 15:55:34 +0000 | [diff] [blame] | 50 | 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 Csomor | 6fddc83 | 2018-11-08 03:12:53 -0800 | [diff] [blame] | 60 | |
| 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 Csomor | 9f15d15 | 2017-03-01 15:55:34 +0000 | [diff] [blame] | 73 | if (handle != INVALID_HANDLE_VALUE && |
| 74 | ::GetFileInformationByHandle(handle, &info)) { |
| 75 | success = true; |
Laszlo Csomor | 6fddc83 | 2018-11-08 03:12:53 -0800 | [diff] [blame] | 76 | bool is_dir = (info.dwFileAttributes != INVALID_FILE_ATTRIBUTES) && |
| 77 | (info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY); |
Laszlo Csomor | 9f15d15 | 2017-03-01 15:55:34 +0000 | [diff] [blame] | 78 | // TODO(laszlocsomor): use info.nFileSizeHigh after we updated total_size to |
| 79 | // be u8 type. |
Laszlo Csomor | 6fddc83 | 2018-11-08 03:12:53 -0800 | [diff] [blame] | 80 | result->total_size = is_dir ? 0 : info.nFileSizeLow; |
Laszlo Csomor | 9f15d15 | 2017-03-01 15:55:34 +0000 | [diff] [blame] | 81 | // TODO(laszlocsomor): query the actual permissions and write in file_mode. |
| 82 | result->file_mode = 0777; |
Laszlo Csomor | 6fddc83 | 2018-11-08 03:12:53 -0800 | [diff] [blame] | 83 | result->is_directory = is_dir; |
Laszlo Csomor | 9f15d15 | 2017-03-01 15:55:34 +0000 | [diff] [blame] | 84 | } |
| 85 | ::CloseHandle(handle); |
| 86 | return success; |
Loo Rong Jie | 4022bac | 2018-06-11 02:04:52 -0700 | [diff] [blame] | 87 | #else // !(defined(_WIN32) || defined(__CYGWIN__)) |
Laszlo Csomor | 645dbc4 | 2016-12-01 12:56:43 +0000 | [diff] [blame] | 88 | 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 Jie | 4022bac | 2018-06-11 02:04:52 -0700 | [diff] [blame] | 96 | #endif // defined(_WIN32) || defined(__CYGWIN__) |
Laszlo Csomor | 645dbc4 | 2016-12-01 12:56:43 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Laszlo Csomor | b2f1e19 | 2017-03-03 09:41:53 +0000 | [diff] [blame] | 99 | bool 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 Csomor | 8d6da00 | 2016-12-01 13:16:13 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Laszlo Csomor | d2ed069 | 2016-12-01 14:04:35 +0000 | [diff] [blame] | 104 | bool read_file(const char* path, void* buffer, size_t size) { |
Laszlo Csomor | b2f1e19 | 2017-03-03 09:41:53 +0000 | [diff] [blame] | 105 | return blaze_util::ReadFile(path, buffer, size); |
Laszlo Csomor | d2ed069 | 2016-12-01 14:04:35 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Laszlo Csomor | b2f1e19 | 2017-03-03 09:41:53 +0000 | [diff] [blame] | 108 | string get_cwd() { return blaze_util::GetCwd(); } |
Laszlo Csomor | 8457f3f | 2016-12-01 14:25:18 +0000 | [diff] [blame] | 109 | |
Laszlo Csomor | b2f1e19 | 2017-03-03 09:41:53 +0000 | [diff] [blame] | 110 | bool make_dirs(const char* path, unsigned int mode) { |
Loo Rong Jie | 4022bac | 2018-06-11 02:04:52 -0700 | [diff] [blame] | 111 | #ifndef _WIN32 |
Laszlo Csomor | b2f1e19 | 2017-03-03 09:41:53 +0000 | [diff] [blame] | 112 | // TODO(laszlocsomor): respect `mode` on Windows/MSVC. |
Laszlo Csomor | b8caca0 | 2016-12-01 14:37:46 +0000 | [diff] [blame] | 113 | mode |= S_IWUSR | S_IXUSR; |
Loo Rong Jie | 4022bac | 2018-06-11 02:04:52 -0700 | [diff] [blame] | 114 | #endif // not _WIN32 |
Laszlo Csomor | b2f1e19 | 2017-03-03 09:41:53 +0000 | [diff] [blame] | 115 | string spath(path); |
| 116 | if (spath.empty()) { |
| 117 | return true; |
Laszlo Csomor | b8caca0 | 2016-12-01 14:37:46 +0000 | [diff] [blame] | 118 | } |
Laszlo Csomor | b2f1e19 | 2017-03-03 09:41:53 +0000 | [diff] [blame] | 119 | if (spath.back() != '/' && spath.back() != '\\') { |
| 120 | spath = blaze_util::Dirname(spath); |
| 121 | } |
| 122 | return blaze_util::MakeDirectories(spath, mode); |
Laszlo Csomor | b8caca0 | 2016-12-01 14:37:46 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Laszlo Csomor | 645dbc4 | 2016-12-01 12:56:43 +0000 | [diff] [blame] | 125 | } // namespace devtools_ijar |