bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
mtklein | 1ee7651 | 2015-11-02 10:20:27 -0800 | [diff] [blame] | 8 | #include "SkTypes.h" |
Mike Klein | 8f11d4d | 2018-01-24 12:42:55 -0500 | [diff] [blame] | 9 | #if defined(SK_BUILD_FOR_WIN) |
mtklein | 1ee7651 | 2015-11-02 10:20:27 -0800 | [diff] [blame] | 10 | |
halcanary | 4dbbd04 | 2016-06-07 17:21:10 -0700 | [diff] [blame] | 11 | #include "SkLeanWindows.h" |
Herb Derby | b549cc3 | 2017-03-27 13:35:15 -0400 | [diff] [blame] | 12 | #include "SkMalloc.h" |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 13 | #include "SkOSFile.h" |
bungeman@google.com | f5cc5b1 | 2013-07-12 18:22:49 +0000 | [diff] [blame] | 14 | #include "SkTFitsIn.h" |
bungeman@google.com | 11c9a55 | 2013-06-03 17:10:35 +0000 | [diff] [blame] | 15 | |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 16 | #include <io.h> |
| 17 | #include <stdio.h> |
| 18 | #include <sys/stat.h> |
| 19 | |
bungeman | bf0b9ce | 2014-07-10 15:18:23 -0700 | [diff] [blame] | 20 | bool sk_exists(const char *path, SkFILE_Flags flags) { |
| 21 | int mode = 0; // existence |
| 22 | if (flags & kRead_SkFILE_Flag) { |
| 23 | mode |= 4; // read |
| 24 | } |
| 25 | if (flags & kWrite_SkFILE_Flag) { |
| 26 | mode |= 2; // write |
| 27 | } |
| 28 | return (0 == _access(path, mode)); |
| 29 | } |
| 30 | |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 31 | typedef struct { |
| 32 | ULONGLONG fVolume; |
| 33 | ULONGLONG fLsbSize; |
| 34 | ULONGLONG fMsbSize; |
| 35 | } SkFILEID; |
| 36 | |
halcanary | d76be9c | 2015-11-20 13:47:49 -0800 | [diff] [blame] | 37 | static bool sk_ino(FILE* f, SkFILEID* id) { |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 38 | int fileno = _fileno((FILE*)f); |
| 39 | if (fileno < 0) { |
| 40 | return false; |
| 41 | } |
| 42 | |
| 43 | HANDLE file = (HANDLE)_get_osfhandle(fileno); |
| 44 | if (INVALID_HANDLE_VALUE == file) { |
| 45 | return false; |
| 46 | } |
| 47 | |
| 48 | //TODO: call GetFileInformationByHandleEx on Vista and later with FileIdInfo. |
| 49 | BY_HANDLE_FILE_INFORMATION info; |
| 50 | if (0 == GetFileInformationByHandle(file, &info)) { |
| 51 | return false; |
| 52 | } |
| 53 | id->fVolume = info.dwVolumeSerialNumber; |
| 54 | id->fLsbSize = info.nFileIndexLow + (((ULONGLONG)info.nFileIndexHigh) << 32); |
| 55 | id->fMsbSize = 0; |
| 56 | |
| 57 | return true; |
| 58 | } |
| 59 | |
halcanary | d76be9c | 2015-11-20 13:47:49 -0800 | [diff] [blame] | 60 | bool sk_fidentical(FILE* a, FILE* b) { |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 61 | SkFILEID aID, bID; |
| 62 | return sk_ino(a, &aID) && sk_ino(b, &bID) |
| 63 | && aID.fLsbSize == bID.fLsbSize |
| 64 | && aID.fMsbSize == bID.fMsbSize |
| 65 | && aID.fVolume == bID.fVolume; |
| 66 | } |
| 67 | |
commit-bot@chromium.org | 8e13a15 | 2014-03-19 19:28:00 +0000 | [diff] [blame] | 68 | class SkAutoNullKernelHandle : SkNoncopyable { |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 69 | public: |
commit-bot@chromium.org | 8e13a15 | 2014-03-19 19:28:00 +0000 | [diff] [blame] | 70 | SkAutoNullKernelHandle(const HANDLE handle) : fHandle(handle) { } |
| 71 | ~SkAutoNullKernelHandle() { CloseHandle(fHandle); } |
| 72 | operator HANDLE() const { return fHandle; } |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 73 | bool isValid() const { return SkToBool(fHandle); } |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 74 | private: |
commit-bot@chromium.org | 8e13a15 | 2014-03-19 19:28:00 +0000 | [diff] [blame] | 75 | HANDLE fHandle; |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 76 | }; |
commit-bot@chromium.org | 8e13a15 | 2014-03-19 19:28:00 +0000 | [diff] [blame] | 77 | typedef SkAutoNullKernelHandle SkAutoWinMMap; |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 78 | |
| 79 | void sk_fmunmap(const void* addr, size_t) { |
| 80 | UnmapViewOfFile(addr); |
| 81 | } |
| 82 | |
bungeman@google.com | 11c9a55 | 2013-06-03 17:10:35 +0000 | [diff] [blame] | 83 | void* sk_fdmmap(int fileno, size_t* length) { |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 84 | HANDLE file = (HANDLE)_get_osfhandle(fileno); |
| 85 | if (INVALID_HANDLE_VALUE == file) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 86 | return nullptr; |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 87 | } |
| 88 | |
bungeman@google.com | 11c9a55 | 2013-06-03 17:10:35 +0000 | [diff] [blame] | 89 | LARGE_INTEGER fileSize; |
| 90 | if (0 == GetFileSizeEx(file, &fileSize)) { |
| 91 | //TODO: use SK_TRACEHR(GetLastError(), "Could not get file size.") to report. |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 92 | return nullptr; |
bungeman@google.com | 11c9a55 | 2013-06-03 17:10:35 +0000 | [diff] [blame] | 93 | } |
| 94 | if (!SkTFitsIn<size_t>(fileSize.QuadPart)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 95 | return nullptr; |
bungeman@google.com | 11c9a55 | 2013-06-03 17:10:35 +0000 | [diff] [blame] | 96 | } |
| 97 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 98 | SkAutoWinMMap mmap(CreateFileMapping(file, nullptr, PAGE_READONLY, 0, 0, nullptr)); |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 99 | if (!mmap.isValid()) { |
| 100 | //TODO: use SK_TRACEHR(GetLastError(), "Could not create file mapping.") to report. |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 101 | return nullptr; |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | // Eventually call UnmapViewOfFile |
| 105 | void* addr = MapViewOfFile(mmap, FILE_MAP_READ, 0, 0, 0); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 106 | if (nullptr == addr) { |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 107 | //TODO: use SK_TRACEHR(GetLastError(), "Could not map view of file.") to report. |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 108 | return nullptr; |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 109 | } |
| 110 | |
bungeman@google.com | 11c9a55 | 2013-06-03 17:10:35 +0000 | [diff] [blame] | 111 | *length = static_cast<size_t>(fileSize.QuadPart); |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 112 | return addr; |
| 113 | } |
bungeman@google.com | 11c9a55 | 2013-06-03 17:10:35 +0000 | [diff] [blame] | 114 | |
halcanary | d76be9c | 2015-11-20 13:47:49 -0800 | [diff] [blame] | 115 | int sk_fileno(FILE* f) { |
bungeman@google.com | 11c9a55 | 2013-06-03 17:10:35 +0000 | [diff] [blame] | 116 | return _fileno((FILE*)f); |
| 117 | } |
| 118 | |
halcanary | d76be9c | 2015-11-20 13:47:49 -0800 | [diff] [blame] | 119 | void* sk_fmmap(FILE* f, size_t* length) { |
bungeman@google.com | 11c9a55 | 2013-06-03 17:10:35 +0000 | [diff] [blame] | 120 | int fileno = sk_fileno(f); |
| 121 | if (fileno < 0) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 122 | return nullptr; |
bungeman@google.com | 11c9a55 | 2013-06-03 17:10:35 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | return sk_fdmmap(fileno, length); |
| 126 | } |
bungeman | e998b7f | 2015-02-12 07:18:27 -0800 | [diff] [blame] | 127 | |
Ben Wagner | 4d1955c | 2017-03-10 13:08:15 -0500 | [diff] [blame] | 128 | size_t sk_qread(FILE* file, void* buffer, size_t count, size_t offset) { |
| 129 | int fileno = sk_fileno(file); |
| 130 | HANDLE fileHandle = (HANDLE)_get_osfhandle(fileno); |
| 131 | if (INVALID_HANDLE_VALUE == file) { |
| 132 | return SIZE_MAX; |
| 133 | } |
| 134 | |
Chris Dalton | 1ef8094 | 2017-12-04 12:01:30 -0700 | [diff] [blame] | 135 | OVERLAPPED overlapped; |
| 136 | memset(&overlapped, 0, sizeof(overlapped)); |
Ben Wagner | 4d1955c | 2017-03-10 13:08:15 -0500 | [diff] [blame] | 137 | ULARGE_INTEGER winOffset; |
| 138 | winOffset.QuadPart = offset; |
| 139 | overlapped.Offset = winOffset.LowPart; |
| 140 | overlapped.OffsetHigh = winOffset.HighPart; |
| 141 | |
| 142 | if (!SkTFitsIn<DWORD>(count)) { |
| 143 | count = std::numeric_limits<DWORD>::max(); |
| 144 | } |
| 145 | |
| 146 | DWORD bytesRead; |
| 147 | if (ReadFile(fileHandle, buffer, static_cast<DWORD>(count), &bytesRead, &overlapped)) { |
| 148 | return bytesRead; |
| 149 | } |
| 150 | if (GetLastError() == ERROR_HANDLE_EOF) { |
| 151 | return 0; |
| 152 | } |
| 153 | return SIZE_MAX; |
| 154 | } |
| 155 | |
bungeman | e998b7f | 2015-02-12 07:18:27 -0800 | [diff] [blame] | 156 | //////////////////////////////////////////////////////////////////////////// |
| 157 | |
| 158 | struct SkOSFileIterData { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 159 | SkOSFileIterData() : fHandle(0), fPath16(nullptr) { } |
bungeman | e998b7f | 2015-02-12 07:18:27 -0800 | [diff] [blame] | 160 | HANDLE fHandle; |
| 161 | uint16_t* fPath16; |
| 162 | }; |
bungeman | 99fe822 | 2015-08-20 07:57:51 -0700 | [diff] [blame] | 163 | static_assert(sizeof(SkOSFileIterData) <= SkOSFile::Iter::kStorageSize, "not_enough_space"); |
bungeman | e998b7f | 2015-02-12 07:18:27 -0800 | [diff] [blame] | 164 | |
| 165 | static uint16_t* concat_to_16(const char src[], const char suffix[]) { |
| 166 | size_t i, len = strlen(src); |
| 167 | size_t len2 = 3 + (suffix ? strlen(suffix) : 0); |
| 168 | uint16_t* dst = (uint16_t*)sk_malloc_throw((len + len2) * sizeof(uint16_t)); |
| 169 | |
| 170 | for (i = 0; i < len; i++) { |
| 171 | dst[i] = src[i]; |
| 172 | } |
| 173 | |
| 174 | if (i > 0 && dst[i-1] != '/') { |
| 175 | dst[i++] = '/'; |
| 176 | } |
| 177 | dst[i++] = '*'; |
| 178 | |
| 179 | if (suffix) { |
| 180 | while (*suffix) { |
| 181 | dst[i++] = *suffix++; |
| 182 | } |
| 183 | } |
| 184 | dst[i] = 0; |
| 185 | SkASSERT(i + 1 <= len + len2); |
| 186 | |
| 187 | return dst; |
| 188 | } |
| 189 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 190 | SkOSFile::Iter::Iter() { new (fSelf.get()) SkOSFileIterData; } |
bungeman | e998b7f | 2015-02-12 07:18:27 -0800 | [diff] [blame] | 191 | |
| 192 | SkOSFile::Iter::Iter(const char path[], const char suffix[]) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 193 | new (fSelf.get()) SkOSFileIterData; |
bungeman | e998b7f | 2015-02-12 07:18:27 -0800 | [diff] [blame] | 194 | this->reset(path, suffix); |
| 195 | } |
| 196 | |
| 197 | SkOSFile::Iter::~Iter() { |
| 198 | SkOSFileIterData& self = *static_cast<SkOSFileIterData*>(fSelf.get()); |
| 199 | sk_free(self.fPath16); |
| 200 | if (self.fHandle) { |
| 201 | ::FindClose(self.fHandle); |
| 202 | } |
| 203 | self.~SkOSFileIterData(); |
| 204 | } |
| 205 | |
| 206 | void SkOSFile::Iter::reset(const char path[], const char suffix[]) { |
| 207 | SkOSFileIterData& self = *static_cast<SkOSFileIterData*>(fSelf.get()); |
| 208 | if (self.fHandle) { |
| 209 | ::FindClose(self.fHandle); |
| 210 | self.fHandle = 0; |
| 211 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 212 | if (nullptr == path) { |
bungeman | e998b7f | 2015-02-12 07:18:27 -0800 | [diff] [blame] | 213 | path = ""; |
| 214 | } |
| 215 | |
| 216 | sk_free(self.fPath16); |
| 217 | self.fPath16 = concat_to_16(path, suffix); |
| 218 | } |
| 219 | |
| 220 | static bool is_magic_dir(const uint16_t dir[]) { |
| 221 | // return true for "." and ".." |
| 222 | return dir[0] == '.' && (dir[1] == 0 || (dir[1] == '.' && dir[2] == 0)); |
| 223 | } |
| 224 | |
| 225 | static bool get_the_file(HANDLE handle, SkString* name, WIN32_FIND_DATAW* dataPtr, bool getDir) { |
| 226 | WIN32_FIND_DATAW data; |
| 227 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 228 | if (nullptr == dataPtr) { |
bungeman | e998b7f | 2015-02-12 07:18:27 -0800 | [diff] [blame] | 229 | if (::FindNextFileW(handle, &data)) |
| 230 | dataPtr = &data; |
| 231 | else |
| 232 | return false; |
| 233 | } |
| 234 | |
| 235 | for (;;) { |
| 236 | if (getDir) { |
| 237 | if ((dataPtr->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && |
| 238 | !is_magic_dir((uint16_t*)dataPtr->cFileName)) |
| 239 | { |
| 240 | break; |
| 241 | } |
| 242 | } else { |
| 243 | if (!(dataPtr->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) { |
| 244 | break; |
| 245 | } |
| 246 | } |
| 247 | if (!::FindNextFileW(handle, dataPtr)) { |
| 248 | return false; |
| 249 | } |
| 250 | } |
| 251 | // if we get here, we've found a file/dir |
| 252 | if (name) { |
| 253 | name->setUTF16((uint16_t*)dataPtr->cFileName); |
| 254 | } |
| 255 | return true; |
| 256 | } |
| 257 | |
| 258 | bool SkOSFile::Iter::next(SkString* name, bool getDir) { |
| 259 | SkOSFileIterData& self = *static_cast<SkOSFileIterData*>(fSelf.get()); |
| 260 | WIN32_FIND_DATAW data; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 261 | WIN32_FIND_DATAW* dataPtr = nullptr; |
bungeman | e998b7f | 2015-02-12 07:18:27 -0800 | [diff] [blame] | 262 | |
| 263 | if (self.fHandle == 0) { // our first time |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 264 | if (self.fPath16 == nullptr || *self.fPath16 == 0) { // check for no path |
bungeman | e998b7f | 2015-02-12 07:18:27 -0800 | [diff] [blame] | 265 | return false; |
| 266 | } |
| 267 | |
| 268 | self.fHandle = ::FindFirstFileW((LPCWSTR)self.fPath16, &data); |
| 269 | if (self.fHandle != 0 && self.fHandle != (HANDLE)~0) { |
| 270 | dataPtr = &data; |
| 271 | } |
| 272 | } |
| 273 | return self.fHandle != (HANDLE)~0 && get_the_file(self.fHandle, name, dataPtr, getDir); |
| 274 | } |
mtklein | 1ee7651 | 2015-11-02 10:20:27 -0800 | [diff] [blame] | 275 | |
Mike Klein | 8f11d4d | 2018-01-24 12:42:55 -0500 | [diff] [blame] | 276 | #endif//defined(SK_BUILD_FOR_WIN) |