OLD | NEW |
(Empty) | |
| 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 |
| 8 #include "SkOSFile.h" |
| 9 |
| 10 #include <io.h> |
| 11 #include <stdio.h> |
| 12 #include <sys/stat.h> |
| 13 |
| 14 typedef struct { |
| 15 ULONGLONG fVolume; |
| 16 ULONGLONG fLsbSize; |
| 17 ULONGLONG fMsbSize; |
| 18 } SkFILEID; |
| 19 |
| 20 static bool sk_ino(SkFILE* f, SkFILEID* id) { |
| 21 int fileno = _fileno((FILE*)f); |
| 22 if (fileno < 0) { |
| 23 return false; |
| 24 } |
| 25 |
| 26 HANDLE file = (HANDLE)_get_osfhandle(fileno); |
| 27 if (INVALID_HANDLE_VALUE == file) { |
| 28 return false; |
| 29 } |
| 30 |
| 31 //TODO: call GetFileInformationByHandleEx on Vista and later with FileIdInfo
. |
| 32 BY_HANDLE_FILE_INFORMATION info; |
| 33 if (0 == GetFileInformationByHandle(file, &info)) { |
| 34 return false; |
| 35 } |
| 36 id->fVolume = info.dwVolumeSerialNumber; |
| 37 id->fLsbSize = info.nFileIndexLow + (((ULONGLONG)info.nFileIndexHigh) << 32)
; |
| 38 id->fMsbSize = 0; |
| 39 |
| 40 return true; |
| 41 } |
| 42 |
| 43 bool sk_fidentical(SkFILE* a, SkFILE* b) { |
| 44 SkFILEID aID, bID; |
| 45 return sk_ino(a, &aID) && sk_ino(b, &bID) |
| 46 && aID.fLsbSize == bID.fLsbSize |
| 47 && aID.fMsbSize == bID.fMsbSize |
| 48 && aID.fVolume == bID.fVolume; |
| 49 } |
| 50 |
| 51 template <typename HandleType, HandleType InvalidValue, BOOL (WINAPI * Close)(Ha
ndleType)> |
| 52 class SkAutoTHandle : SkNoncopyable { |
| 53 public: |
| 54 SkAutoTHandle(HandleType handle) : fHandle(handle) { } |
| 55 ~SkAutoTHandle() { Close(fHandle); } |
| 56 operator HandleType() { return fHandle; } |
| 57 bool isValid() { return InvalidValue != fHandle; } |
| 58 private: |
| 59 HandleType fHandle; |
| 60 }; |
| 61 typedef SkAutoTHandle<HANDLE, INVALID_HANDLE_VALUE, CloseHandle> SkAutoWinFile; |
| 62 typedef SkAutoTHandle<HANDLE, NULL, CloseHandle> SkAutoWinMMap; |
| 63 |
| 64 void sk_fmunmap(const void* addr, size_t) { |
| 65 UnmapViewOfFile(addr); |
| 66 } |
| 67 |
| 68 void* sk_fmmap(SkFILE* f, size_t* length) { |
| 69 size_t fileSize = sk_fgetsize(f); |
| 70 if (0 == fileSize) { |
| 71 return NULL; |
| 72 } |
| 73 |
| 74 int fileno = _fileno((FILE*)f); |
| 75 if (fileno < 0) { |
| 76 return NULL; |
| 77 } |
| 78 |
| 79 HANDLE file = (HANDLE)_get_osfhandle(fileno); |
| 80 if (INVALID_HANDLE_VALUE == file) { |
| 81 return NULL; |
| 82 } |
| 83 |
| 84 SkAutoWinMMap mmap(CreateFileMapping(file, NULL, PAGE_READONLY, 0, 0, NULL))
; |
| 85 if (!mmap.isValid()) { |
| 86 //TODO: use SK_TRACEHR(GetLastError(), "Could not create file mapping.")
to report. |
| 87 return NULL; |
| 88 } |
| 89 |
| 90 // Eventually call UnmapViewOfFile |
| 91 void* addr = MapViewOfFile(mmap, FILE_MAP_READ, 0, 0, 0); |
| 92 if (NULL == addr) { |
| 93 //TODO: use SK_TRACEHR(GetLastError(), "Could not map view of file.") to
report. |
| 94 return NULL; |
| 95 } |
| 96 |
| 97 *length = fileSize; |
| 98 return addr; |
| 99 } |
OLD | NEW |