blob: 03647142e9e6d8c52c28fa9726f73e1a0c2d912d [file] [log] [blame]
bungeman@google.com6cab1a42013-05-29 13:43:31 +00001/*
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
mtklein1ee76512015-11-02 10:20:27 -08008#include "SkTypes.h"
Mike Klein8f11d4d2018-01-24 12:42:55 -05009#if defined(SK_BUILD_FOR_WIN)
mtklein1ee76512015-11-02 10:20:27 -080010
halcanary4dbbd042016-06-07 17:21:10 -070011#include "SkLeanWindows.h"
Herb Derbyb549cc32017-03-27 13:35:15 -040012#include "SkMalloc.h"
Ben Wagnerd5148e32018-07-16 17:44:06 -040013#include "SkNoncopyable.h"
bungeman@google.com6cab1a42013-05-29 13:43:31 +000014#include "SkOSFile.h"
Hal Canaryee08b4a2018-03-01 15:56:37 -050015#include "SkStringUtils.h"
bungeman@google.comf5cc5b12013-07-12 18:22:49 +000016#include "SkTFitsIn.h"
bungeman@google.com11c9a552013-06-03 17:10:35 +000017
bungeman@google.com6cab1a42013-05-29 13:43:31 +000018#include <io.h>
Mike Klein79aea6a2018-06-11 10:45:26 -040019#include <new>
bungeman@google.com6cab1a42013-05-29 13:43:31 +000020#include <stdio.h>
21#include <sys/stat.h>
22
bungemanbf0b9ce2014-07-10 15:18:23 -070023bool sk_exists(const char *path, SkFILE_Flags flags) {
24 int mode = 0; // existence
25 if (flags & kRead_SkFILE_Flag) {
26 mode |= 4; // read
27 }
28 if (flags & kWrite_SkFILE_Flag) {
29 mode |= 2; // write
30 }
31 return (0 == _access(path, mode));
32}
33
bungeman@google.com6cab1a42013-05-29 13:43:31 +000034typedef struct {
35 ULONGLONG fVolume;
36 ULONGLONG fLsbSize;
37 ULONGLONG fMsbSize;
38} SkFILEID;
39
halcanaryd76be9c2015-11-20 13:47:49 -080040static bool sk_ino(FILE* f, SkFILEID* id) {
bungeman@google.com6cab1a42013-05-29 13:43:31 +000041 int fileno = _fileno((FILE*)f);
42 if (fileno < 0) {
43 return false;
44 }
45
46 HANDLE file = (HANDLE)_get_osfhandle(fileno);
47 if (INVALID_HANDLE_VALUE == file) {
48 return false;
49 }
50
51 //TODO: call GetFileInformationByHandleEx on Vista and later with FileIdInfo.
52 BY_HANDLE_FILE_INFORMATION info;
53 if (0 == GetFileInformationByHandle(file, &info)) {
54 return false;
55 }
56 id->fVolume = info.dwVolumeSerialNumber;
57 id->fLsbSize = info.nFileIndexLow + (((ULONGLONG)info.nFileIndexHigh) << 32);
58 id->fMsbSize = 0;
59
60 return true;
61}
62
halcanaryd76be9c2015-11-20 13:47:49 -080063bool sk_fidentical(FILE* a, FILE* b) {
bungeman@google.com6cab1a42013-05-29 13:43:31 +000064 SkFILEID aID, bID;
65 return sk_ino(a, &aID) && sk_ino(b, &bID)
66 && aID.fLsbSize == bID.fLsbSize
67 && aID.fMsbSize == bID.fMsbSize
68 && aID.fVolume == bID.fVolume;
69}
70
commit-bot@chromium.org8e13a152014-03-19 19:28:00 +000071class SkAutoNullKernelHandle : SkNoncopyable {
bungeman@google.com6cab1a42013-05-29 13:43:31 +000072public:
commit-bot@chromium.org8e13a152014-03-19 19:28:00 +000073 SkAutoNullKernelHandle(const HANDLE handle) : fHandle(handle) { }
74 ~SkAutoNullKernelHandle() { CloseHandle(fHandle); }
75 operator HANDLE() const { return fHandle; }
bsalomon49f085d2014-09-05 13:34:00 -070076 bool isValid() const { return SkToBool(fHandle); }
bungeman@google.com6cab1a42013-05-29 13:43:31 +000077private:
commit-bot@chromium.org8e13a152014-03-19 19:28:00 +000078 HANDLE fHandle;
bungeman@google.com6cab1a42013-05-29 13:43:31 +000079};
commit-bot@chromium.org8e13a152014-03-19 19:28:00 +000080typedef SkAutoNullKernelHandle SkAutoWinMMap;
bungeman@google.com6cab1a42013-05-29 13:43:31 +000081
82void sk_fmunmap(const void* addr, size_t) {
83 UnmapViewOfFile(addr);
84}
85
bungeman@google.com11c9a552013-06-03 17:10:35 +000086void* sk_fdmmap(int fileno, size_t* length) {
bungeman@google.com6cab1a42013-05-29 13:43:31 +000087 HANDLE file = (HANDLE)_get_osfhandle(fileno);
88 if (INVALID_HANDLE_VALUE == file) {
halcanary96fcdcc2015-08-27 07:41:13 -070089 return nullptr;
bungeman@google.com6cab1a42013-05-29 13:43:31 +000090 }
91
bungeman@google.com11c9a552013-06-03 17:10:35 +000092 LARGE_INTEGER fileSize;
93 if (0 == GetFileSizeEx(file, &fileSize)) {
94 //TODO: use SK_TRACEHR(GetLastError(), "Could not get file size.") to report.
halcanary96fcdcc2015-08-27 07:41:13 -070095 return nullptr;
bungeman@google.com11c9a552013-06-03 17:10:35 +000096 }
97 if (!SkTFitsIn<size_t>(fileSize.QuadPart)) {
halcanary96fcdcc2015-08-27 07:41:13 -070098 return nullptr;
bungeman@google.com11c9a552013-06-03 17:10:35 +000099 }
100
halcanary96fcdcc2015-08-27 07:41:13 -0700101 SkAutoWinMMap mmap(CreateFileMapping(file, nullptr, PAGE_READONLY, 0, 0, nullptr));
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000102 if (!mmap.isValid()) {
103 //TODO: use SK_TRACEHR(GetLastError(), "Could not create file mapping.") to report.
halcanary96fcdcc2015-08-27 07:41:13 -0700104 return nullptr;
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000105 }
106
107 // Eventually call UnmapViewOfFile
108 void* addr = MapViewOfFile(mmap, FILE_MAP_READ, 0, 0, 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700109 if (nullptr == addr) {
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000110 //TODO: use SK_TRACEHR(GetLastError(), "Could not map view of file.") to report.
halcanary96fcdcc2015-08-27 07:41:13 -0700111 return nullptr;
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000112 }
113
bungeman@google.com11c9a552013-06-03 17:10:35 +0000114 *length = static_cast<size_t>(fileSize.QuadPart);
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000115 return addr;
116}
bungeman@google.com11c9a552013-06-03 17:10:35 +0000117
halcanaryd76be9c2015-11-20 13:47:49 -0800118int sk_fileno(FILE* f) {
bungeman@google.com11c9a552013-06-03 17:10:35 +0000119 return _fileno((FILE*)f);
120}
121
halcanaryd76be9c2015-11-20 13:47:49 -0800122void* sk_fmmap(FILE* f, size_t* length) {
bungeman@google.com11c9a552013-06-03 17:10:35 +0000123 int fileno = sk_fileno(f);
124 if (fileno < 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700125 return nullptr;
bungeman@google.com11c9a552013-06-03 17:10:35 +0000126 }
127
128 return sk_fdmmap(fileno, length);
129}
bungemane998b7f2015-02-12 07:18:27 -0800130
Ben Wagner4d1955c2017-03-10 13:08:15 -0500131size_t sk_qread(FILE* file, void* buffer, size_t count, size_t offset) {
132 int fileno = sk_fileno(file);
133 HANDLE fileHandle = (HANDLE)_get_osfhandle(fileno);
134 if (INVALID_HANDLE_VALUE == file) {
135 return SIZE_MAX;
136 }
137
Chris Dalton1ef80942017-12-04 12:01:30 -0700138 OVERLAPPED overlapped;
139 memset(&overlapped, 0, sizeof(overlapped));
Ben Wagner4d1955c2017-03-10 13:08:15 -0500140 ULARGE_INTEGER winOffset;
141 winOffset.QuadPart = offset;
142 overlapped.Offset = winOffset.LowPart;
143 overlapped.OffsetHigh = winOffset.HighPart;
144
145 if (!SkTFitsIn<DWORD>(count)) {
146 count = std::numeric_limits<DWORD>::max();
147 }
148
149 DWORD bytesRead;
150 if (ReadFile(fileHandle, buffer, static_cast<DWORD>(count), &bytesRead, &overlapped)) {
151 return bytesRead;
152 }
153 if (GetLastError() == ERROR_HANDLE_EOF) {
154 return 0;
155 }
156 return SIZE_MAX;
157}
158
bungemane998b7f2015-02-12 07:18:27 -0800159////////////////////////////////////////////////////////////////////////////
160
161struct SkOSFileIterData {
halcanary96fcdcc2015-08-27 07:41:13 -0700162 SkOSFileIterData() : fHandle(0), fPath16(nullptr) { }
bungemane998b7f2015-02-12 07:18:27 -0800163 HANDLE fHandle;
164 uint16_t* fPath16;
165};
bungeman99fe8222015-08-20 07:57:51 -0700166static_assert(sizeof(SkOSFileIterData) <= SkOSFile::Iter::kStorageSize, "not_enough_space");
bungemane998b7f2015-02-12 07:18:27 -0800167
168static uint16_t* concat_to_16(const char src[], const char suffix[]) {
169 size_t i, len = strlen(src);
170 size_t len2 = 3 + (suffix ? strlen(suffix) : 0);
171 uint16_t* dst = (uint16_t*)sk_malloc_throw((len + len2) * sizeof(uint16_t));
172
173 for (i = 0; i < len; i++) {
174 dst[i] = src[i];
175 }
176
177 if (i > 0 && dst[i-1] != '/') {
178 dst[i++] = '/';
179 }
180 dst[i++] = '*';
181
182 if (suffix) {
183 while (*suffix) {
184 dst[i++] = *suffix++;
185 }
186 }
187 dst[i] = 0;
188 SkASSERT(i + 1 <= len + len2);
189
190 return dst;
191}
192
halcanary385fe4d2015-08-26 13:07:48 -0700193SkOSFile::Iter::Iter() { new (fSelf.get()) SkOSFileIterData; }
bungemane998b7f2015-02-12 07:18:27 -0800194
195SkOSFile::Iter::Iter(const char path[], const char suffix[]) {
halcanary385fe4d2015-08-26 13:07:48 -0700196 new (fSelf.get()) SkOSFileIterData;
bungemane998b7f2015-02-12 07:18:27 -0800197 this->reset(path, suffix);
198}
199
200SkOSFile::Iter::~Iter() {
201 SkOSFileIterData& self = *static_cast<SkOSFileIterData*>(fSelf.get());
202 sk_free(self.fPath16);
203 if (self.fHandle) {
204 ::FindClose(self.fHandle);
205 }
206 self.~SkOSFileIterData();
207}
208
209void SkOSFile::Iter::reset(const char path[], const char suffix[]) {
210 SkOSFileIterData& self = *static_cast<SkOSFileIterData*>(fSelf.get());
211 if (self.fHandle) {
212 ::FindClose(self.fHandle);
213 self.fHandle = 0;
214 }
halcanary96fcdcc2015-08-27 07:41:13 -0700215 if (nullptr == path) {
bungemane998b7f2015-02-12 07:18:27 -0800216 path = "";
217 }
218
219 sk_free(self.fPath16);
220 self.fPath16 = concat_to_16(path, suffix);
221}
222
223static bool is_magic_dir(const uint16_t dir[]) {
224 // return true for "." and ".."
225 return dir[0] == '.' && (dir[1] == 0 || (dir[1] == '.' && dir[2] == 0));
226}
227
228static bool get_the_file(HANDLE handle, SkString* name, WIN32_FIND_DATAW* dataPtr, bool getDir) {
229 WIN32_FIND_DATAW data;
230
halcanary96fcdcc2015-08-27 07:41:13 -0700231 if (nullptr == dataPtr) {
bungemane998b7f2015-02-12 07:18:27 -0800232 if (::FindNextFileW(handle, &data))
233 dataPtr = &data;
234 else
235 return false;
236 }
237
238 for (;;) {
239 if (getDir) {
240 if ((dataPtr->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) &&
241 !is_magic_dir((uint16_t*)dataPtr->cFileName))
242 {
243 break;
244 }
245 } else {
246 if (!(dataPtr->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
247 break;
248 }
249 }
250 if (!::FindNextFileW(handle, dataPtr)) {
251 return false;
252 }
253 }
254 // if we get here, we've found a file/dir
255 if (name) {
Hal Canaryee08b4a2018-03-01 15:56:37 -0500256 const uint16_t* utf16name = (const uint16_t*)dataPtr->cFileName;
257 const uint16_t* ptr = utf16name;
258 while (*ptr != 0) { ++ptr; }
259 *name = SkStringFromUTF16(utf16name, ptr - utf16name);
bungemane998b7f2015-02-12 07:18:27 -0800260 }
261 return true;
262}
263
264bool SkOSFile::Iter::next(SkString* name, bool getDir) {
265 SkOSFileIterData& self = *static_cast<SkOSFileIterData*>(fSelf.get());
266 WIN32_FIND_DATAW data;
halcanary96fcdcc2015-08-27 07:41:13 -0700267 WIN32_FIND_DATAW* dataPtr = nullptr;
bungemane998b7f2015-02-12 07:18:27 -0800268
269 if (self.fHandle == 0) { // our first time
halcanary96fcdcc2015-08-27 07:41:13 -0700270 if (self.fPath16 == nullptr || *self.fPath16 == 0) { // check for no path
bungemane998b7f2015-02-12 07:18:27 -0800271 return false;
272 }
273
274 self.fHandle = ::FindFirstFileW((LPCWSTR)self.fPath16, &data);
275 if (self.fHandle != 0 && self.fHandle != (HANDLE)~0) {
276 dataPtr = &data;
277 }
278 }
279 return self.fHandle != (HANDLE)~0 && get_the_file(self.fHandle, name, dataPtr, getDir);
280}
mtklein1ee76512015-11-02 10:20:27 -0800281
Mike Klein8f11d4d2018-01-24 12:42:55 -0500282#endif//defined(SK_BUILD_FOR_WIN)