blob: 5488712d39f7ea5fd845048813fca0adb2ed642c [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"
bungeman@google.com6cab1a42013-05-29 13:43:31 +000013#include "SkOSFile.h"
Hal Canaryee08b4a2018-03-01 15:56:37 -050014#include "SkStringUtils.h"
bungeman@google.comf5cc5b12013-07-12 18:22:49 +000015#include "SkTFitsIn.h"
bungeman@google.com11c9a552013-06-03 17:10:35 +000016
bungeman@google.com6cab1a42013-05-29 13:43:31 +000017#include <io.h>
18#include <stdio.h>
19#include <sys/stat.h>
20
bungemanbf0b9ce2014-07-10 15:18:23 -070021bool sk_exists(const char *path, SkFILE_Flags flags) {
22 int mode = 0; // existence
23 if (flags & kRead_SkFILE_Flag) {
24 mode |= 4; // read
25 }
26 if (flags & kWrite_SkFILE_Flag) {
27 mode |= 2; // write
28 }
29 return (0 == _access(path, mode));
30}
31
bungeman@google.com6cab1a42013-05-29 13:43:31 +000032typedef struct {
33 ULONGLONG fVolume;
34 ULONGLONG fLsbSize;
35 ULONGLONG fMsbSize;
36} SkFILEID;
37
halcanaryd76be9c2015-11-20 13:47:49 -080038static bool sk_ino(FILE* f, SkFILEID* id) {
bungeman@google.com6cab1a42013-05-29 13:43:31 +000039 int fileno = _fileno((FILE*)f);
40 if (fileno < 0) {
41 return false;
42 }
43
44 HANDLE file = (HANDLE)_get_osfhandle(fileno);
45 if (INVALID_HANDLE_VALUE == file) {
46 return false;
47 }
48
49 //TODO: call GetFileInformationByHandleEx on Vista and later with FileIdInfo.
50 BY_HANDLE_FILE_INFORMATION info;
51 if (0 == GetFileInformationByHandle(file, &info)) {
52 return false;
53 }
54 id->fVolume = info.dwVolumeSerialNumber;
55 id->fLsbSize = info.nFileIndexLow + (((ULONGLONG)info.nFileIndexHigh) << 32);
56 id->fMsbSize = 0;
57
58 return true;
59}
60
halcanaryd76be9c2015-11-20 13:47:49 -080061bool sk_fidentical(FILE* a, FILE* b) {
bungeman@google.com6cab1a42013-05-29 13:43:31 +000062 SkFILEID aID, bID;
63 return sk_ino(a, &aID) && sk_ino(b, &bID)
64 && aID.fLsbSize == bID.fLsbSize
65 && aID.fMsbSize == bID.fMsbSize
66 && aID.fVolume == bID.fVolume;
67}
68
commit-bot@chromium.org8e13a152014-03-19 19:28:00 +000069class SkAutoNullKernelHandle : SkNoncopyable {
bungeman@google.com6cab1a42013-05-29 13:43:31 +000070public:
commit-bot@chromium.org8e13a152014-03-19 19:28:00 +000071 SkAutoNullKernelHandle(const HANDLE handle) : fHandle(handle) { }
72 ~SkAutoNullKernelHandle() { CloseHandle(fHandle); }
73 operator HANDLE() const { return fHandle; }
bsalomon49f085d2014-09-05 13:34:00 -070074 bool isValid() const { return SkToBool(fHandle); }
bungeman@google.com6cab1a42013-05-29 13:43:31 +000075private:
commit-bot@chromium.org8e13a152014-03-19 19:28:00 +000076 HANDLE fHandle;
bungeman@google.com6cab1a42013-05-29 13:43:31 +000077};
commit-bot@chromium.org8e13a152014-03-19 19:28:00 +000078typedef SkAutoNullKernelHandle SkAutoWinMMap;
bungeman@google.com6cab1a42013-05-29 13:43:31 +000079
80void sk_fmunmap(const void* addr, size_t) {
81 UnmapViewOfFile(addr);
82}
83
bungeman@google.com11c9a552013-06-03 17:10:35 +000084void* sk_fdmmap(int fileno, size_t* length) {
bungeman@google.com6cab1a42013-05-29 13:43:31 +000085 HANDLE file = (HANDLE)_get_osfhandle(fileno);
86 if (INVALID_HANDLE_VALUE == file) {
halcanary96fcdcc2015-08-27 07:41:13 -070087 return nullptr;
bungeman@google.com6cab1a42013-05-29 13:43:31 +000088 }
89
bungeman@google.com11c9a552013-06-03 17:10:35 +000090 LARGE_INTEGER fileSize;
91 if (0 == GetFileSizeEx(file, &fileSize)) {
92 //TODO: use SK_TRACEHR(GetLastError(), "Could not get file size.") to report.
halcanary96fcdcc2015-08-27 07:41:13 -070093 return nullptr;
bungeman@google.com11c9a552013-06-03 17:10:35 +000094 }
95 if (!SkTFitsIn<size_t>(fileSize.QuadPart)) {
halcanary96fcdcc2015-08-27 07:41:13 -070096 return nullptr;
bungeman@google.com11c9a552013-06-03 17:10:35 +000097 }
98
halcanary96fcdcc2015-08-27 07:41:13 -070099 SkAutoWinMMap mmap(CreateFileMapping(file, nullptr, PAGE_READONLY, 0, 0, nullptr));
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000100 if (!mmap.isValid()) {
101 //TODO: use SK_TRACEHR(GetLastError(), "Could not create file mapping.") to report.
halcanary96fcdcc2015-08-27 07:41:13 -0700102 return nullptr;
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000103 }
104
105 // Eventually call UnmapViewOfFile
106 void* addr = MapViewOfFile(mmap, FILE_MAP_READ, 0, 0, 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700107 if (nullptr == addr) {
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000108 //TODO: use SK_TRACEHR(GetLastError(), "Could not map view of file.") to report.
halcanary96fcdcc2015-08-27 07:41:13 -0700109 return nullptr;
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000110 }
111
bungeman@google.com11c9a552013-06-03 17:10:35 +0000112 *length = static_cast<size_t>(fileSize.QuadPart);
bungeman@google.com6cab1a42013-05-29 13:43:31 +0000113 return addr;
114}
bungeman@google.com11c9a552013-06-03 17:10:35 +0000115
halcanaryd76be9c2015-11-20 13:47:49 -0800116int sk_fileno(FILE* f) {
bungeman@google.com11c9a552013-06-03 17:10:35 +0000117 return _fileno((FILE*)f);
118}
119
halcanaryd76be9c2015-11-20 13:47:49 -0800120void* sk_fmmap(FILE* f, size_t* length) {
bungeman@google.com11c9a552013-06-03 17:10:35 +0000121 int fileno = sk_fileno(f);
122 if (fileno < 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700123 return nullptr;
bungeman@google.com11c9a552013-06-03 17:10:35 +0000124 }
125
126 return sk_fdmmap(fileno, length);
127}
bungemane998b7f2015-02-12 07:18:27 -0800128
Ben Wagner4d1955c2017-03-10 13:08:15 -0500129size_t sk_qread(FILE* file, void* buffer, size_t count, size_t offset) {
130 int fileno = sk_fileno(file);
131 HANDLE fileHandle = (HANDLE)_get_osfhandle(fileno);
132 if (INVALID_HANDLE_VALUE == file) {
133 return SIZE_MAX;
134 }
135
Chris Dalton1ef80942017-12-04 12:01:30 -0700136 OVERLAPPED overlapped;
137 memset(&overlapped, 0, sizeof(overlapped));
Ben Wagner4d1955c2017-03-10 13:08:15 -0500138 ULARGE_INTEGER winOffset;
139 winOffset.QuadPart = offset;
140 overlapped.Offset = winOffset.LowPart;
141 overlapped.OffsetHigh = winOffset.HighPart;
142
143 if (!SkTFitsIn<DWORD>(count)) {
144 count = std::numeric_limits<DWORD>::max();
145 }
146
147 DWORD bytesRead;
148 if (ReadFile(fileHandle, buffer, static_cast<DWORD>(count), &bytesRead, &overlapped)) {
149 return bytesRead;
150 }
151 if (GetLastError() == ERROR_HANDLE_EOF) {
152 return 0;
153 }
154 return SIZE_MAX;
155}
156
bungemane998b7f2015-02-12 07:18:27 -0800157////////////////////////////////////////////////////////////////////////////
158
159struct SkOSFileIterData {
halcanary96fcdcc2015-08-27 07:41:13 -0700160 SkOSFileIterData() : fHandle(0), fPath16(nullptr) { }
bungemane998b7f2015-02-12 07:18:27 -0800161 HANDLE fHandle;
162 uint16_t* fPath16;
163};
bungeman99fe8222015-08-20 07:57:51 -0700164static_assert(sizeof(SkOSFileIterData) <= SkOSFile::Iter::kStorageSize, "not_enough_space");
bungemane998b7f2015-02-12 07:18:27 -0800165
166static uint16_t* concat_to_16(const char src[], const char suffix[]) {
167 size_t i, len = strlen(src);
168 size_t len2 = 3 + (suffix ? strlen(suffix) : 0);
169 uint16_t* dst = (uint16_t*)sk_malloc_throw((len + len2) * sizeof(uint16_t));
170
171 for (i = 0; i < len; i++) {
172 dst[i] = src[i];
173 }
174
175 if (i > 0 && dst[i-1] != '/') {
176 dst[i++] = '/';
177 }
178 dst[i++] = '*';
179
180 if (suffix) {
181 while (*suffix) {
182 dst[i++] = *suffix++;
183 }
184 }
185 dst[i] = 0;
186 SkASSERT(i + 1 <= len + len2);
187
188 return dst;
189}
190
halcanary385fe4d2015-08-26 13:07:48 -0700191SkOSFile::Iter::Iter() { new (fSelf.get()) SkOSFileIterData; }
bungemane998b7f2015-02-12 07:18:27 -0800192
193SkOSFile::Iter::Iter(const char path[], const char suffix[]) {
halcanary385fe4d2015-08-26 13:07:48 -0700194 new (fSelf.get()) SkOSFileIterData;
bungemane998b7f2015-02-12 07:18:27 -0800195 this->reset(path, suffix);
196}
197
198SkOSFile::Iter::~Iter() {
199 SkOSFileIterData& self = *static_cast<SkOSFileIterData*>(fSelf.get());
200 sk_free(self.fPath16);
201 if (self.fHandle) {
202 ::FindClose(self.fHandle);
203 }
204 self.~SkOSFileIterData();
205}
206
207void SkOSFile::Iter::reset(const char path[], const char suffix[]) {
208 SkOSFileIterData& self = *static_cast<SkOSFileIterData*>(fSelf.get());
209 if (self.fHandle) {
210 ::FindClose(self.fHandle);
211 self.fHandle = 0;
212 }
halcanary96fcdcc2015-08-27 07:41:13 -0700213 if (nullptr == path) {
bungemane998b7f2015-02-12 07:18:27 -0800214 path = "";
215 }
216
217 sk_free(self.fPath16);
218 self.fPath16 = concat_to_16(path, suffix);
219}
220
221static bool is_magic_dir(const uint16_t dir[]) {
222 // return true for "." and ".."
223 return dir[0] == '.' && (dir[1] == 0 || (dir[1] == '.' && dir[2] == 0));
224}
225
226static bool get_the_file(HANDLE handle, SkString* name, WIN32_FIND_DATAW* dataPtr, bool getDir) {
227 WIN32_FIND_DATAW data;
228
halcanary96fcdcc2015-08-27 07:41:13 -0700229 if (nullptr == dataPtr) {
bungemane998b7f2015-02-12 07:18:27 -0800230 if (::FindNextFileW(handle, &data))
231 dataPtr = &data;
232 else
233 return false;
234 }
235
236 for (;;) {
237 if (getDir) {
238 if ((dataPtr->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) &&
239 !is_magic_dir((uint16_t*)dataPtr->cFileName))
240 {
241 break;
242 }
243 } else {
244 if (!(dataPtr->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
245 break;
246 }
247 }
248 if (!::FindNextFileW(handle, dataPtr)) {
249 return false;
250 }
251 }
252 // if we get here, we've found a file/dir
253 if (name) {
Hal Canaryee08b4a2018-03-01 15:56:37 -0500254 const uint16_t* utf16name = (const uint16_t*)dataPtr->cFileName;
255 const uint16_t* ptr = utf16name;
256 while (*ptr != 0) { ++ptr; }
257 *name = SkStringFromUTF16(utf16name, ptr - utf16name);
bungemane998b7f2015-02-12 07:18:27 -0800258 }
259 return true;
260}
261
262bool SkOSFile::Iter::next(SkString* name, bool getDir) {
263 SkOSFileIterData& self = *static_cast<SkOSFileIterData*>(fSelf.get());
264 WIN32_FIND_DATAW data;
halcanary96fcdcc2015-08-27 07:41:13 -0700265 WIN32_FIND_DATAW* dataPtr = nullptr;
bungemane998b7f2015-02-12 07:18:27 -0800266
267 if (self.fHandle == 0) { // our first time
halcanary96fcdcc2015-08-27 07:41:13 -0700268 if (self.fPath16 == nullptr || *self.fPath16 == 0) { // check for no path
bungemane998b7f2015-02-12 07:18:27 -0800269 return false;
270 }
271
272 self.fHandle = ::FindFirstFileW((LPCWSTR)self.fPath16, &data);
273 if (self.fHandle != 0 && self.fHandle != (HANDLE)~0) {
274 dataPtr = &data;
275 }
276 }
277 return self.fHandle != (HANDLE)~0 && get_the_file(self.fHandle, name, dataPtr, getDir);
278}
mtklein1ee76512015-11-02 10:20:27 -0800279
Mike Klein8f11d4d2018-01-24 12:42:55 -0500280#endif//defined(SK_BUILD_FOR_WIN)