| 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 "include/core/SkString.h" |
| 9 |
#include "include/core/SkTypes.h" |
| 10 |
#include "include/private/base/SkTFitsIn.h" |
| 11 |
#include "include/private/base/SkTemplates.h" |
| 12 |
#include "src/core/SkOSFile.h" |
| 13 |
|
| 14 |
#include <dirent.h> |
| 15 |
#include <new> |
| 16 |
#include <stdio.h> |
| 17 |
#include <string.h> |
| 18 |
#include <sys/mman.h> |
| 19 |
#include <sys/stat.h> |
| 20 |
#include <sys/types.h> |
| 21 |
#include <unistd.h> |
| 22 |
|
| 23 |
#ifdef SK_BUILD_FOR_IOS |
| 24 |
#include "src/ports/SkOSFile_ios.h" |
| 25 |
#endif |
| 26 |
|
| 27 |
void sk_fsync(FILE* f) { |
| 28 |
#if !defined(SK_BUILD_FOR_ANDROID) && !defined(__UCLIBC__) && !defined(_NEWLIB_VERSION) |
| 29 |
int fd = fileno(f); |
| 30 |
fsync(fd); |
| 31 |
#endif |
| 32 |
} |
| 33 |
|
| 34 |
bool sk_exists(const char *path, SkFILE_Flags flags) { |
| 35 |
int mode = F_OK; |
| 36 |
if (flags & kRead_SkFILE_Flag) { |
| 37 |
mode |= R_OK; |
| 38 |
} |
| 39 |
if (flags & kWrite_SkFILE_Flag) { |
| 40 |
mode |= W_OK; |
| 41 |
} |
| 42 |
#ifdef SK_BUILD_FOR_IOS |
| 43 |
// if the default path fails, check the bundle (but only if read-only) |
| 44 |
if (0 == access(path, mode)) { |
| 45 |
return true; |
| 46 |
} else { |
| 47 |
return (kRead_SkFILE_Flag == flags && ios_get_path_in_bundle(path, nullptr)); |
| 48 |
} |
| 49 |
#else |
| 50 |
return (0 == access(path, mode)); |
| 51 |
#endif |
| 52 |
} |
| 53 |
|
| 54 |
typedef struct { |
| 55 |
dev_t dev; |
| 56 |
ino_t ino; |
| 57 |
} SkFILEID; |
| 58 |
|
| 59 |
static bool sk_ino(FILE* a, SkFILEID* id) { |
| 60 |
int fd = fileno(a); |
| 61 |
if (fd < 0) { |
| 62 |
return 0; |
| 63 |
} |
| 64 |
struct stat status = {}; |
| 65 |
if (0 != fstat(fd, &status)) { |
| 66 |
return 0; |
| 67 |
} |
| 68 |
id->dev = status.st_dev; |
| 69 |
id->ino = status.st_ino; |
| 70 |
return true; |
| 71 |
} |
| 72 |
|
| 73 |
bool sk_fidentical(FILE* a, FILE* b) { |
| 74 |
SkFILEID aID, bID; |
| 75 |
return sk_ino(a, &aID) && sk_ino(b, &bID) |
| 76 |
&& aID.ino == bID.ino |
| 77 |
&& aID.dev == bID.dev; |
| 78 |
} |
| 79 |
|
| 80 |
void sk_fmunmap(const void* addr, size_t length) { |
| 81 |
munmap(const_cast<void*>(addr), length); |
| 82 |
} |
| 83 |
|
| 84 |
void* sk_fdmmap(int fd, size_t* size) { |
| 85 |
struct stat status = {}; |
| 86 |
if (0 != fstat(fd, &status)) { |
| 87 |
return nullptr; |
| 88 |
} |
| 89 |
if (!S_ISREG(status.st_mode)) { |
| 90 |
return nullptr; |
| 91 |
} |
| 92 |
if (!SkTFitsIn<size_t>(status.st_size)) { |
| 93 |
return nullptr; |
| 94 |
} |
| 95 |
size_t fileSize = static_cast<size_t>(status.st_size); |
| 96 |
|
| 97 |
void* addr = mmap(nullptr, fileSize, PROT_READ, MAP_PRIVATE, fd, 0); |
| 98 |
if (MAP_FAILED == addr) { |
| 99 |
return nullptr; |
| 100 |
} |
| 101 |
|
| 102 |
*size = fileSize; |
| 103 |
return addr; |
| 104 |
} |
| 105 |
|
| 106 |
int sk_fileno(FILE* f) { |
| 107 |
return fileno(f); |
| 108 |
} |
| 109 |
|
| 110 |
void* sk_fmmap(FILE* f, size_t* size) { |
| 111 |
int fd = sk_fileno(f); |
| 112 |
if (fd < 0) { |
| 113 |
return nullptr; |
| 114 |
} |
| 115 |
|
| 116 |
return sk_fdmmap(fd, size); |
| 117 |
} |
| 118 |
|
| 119 |
size_t sk_qread(FILE* file, void* buffer, size_t count, size_t offset) { |
| 120 |
int fd = sk_fileno(file); |
| 121 |
if (fd < 0) { |
| 122 |
return SIZE_MAX; |
| 123 |
} |
| 124 |
ssize_t bytesRead = pread(fd, buffer, count, offset); |
| 125 |
if (bytesRead < 0) { |
| 126 |
return SIZE_MAX; |
| 127 |
} |
| 128 |
return bytesRead; |
| 129 |
} |
| 130 |
|
| 131 |
//////////////////////////////////////////////////////////////////////////// |
| 132 |
|
| 133 |
struct SkOSFileIterData { |
| 134 |
SkOSFileIterData() : fDIR(nullptr) { } |
| 135 |
DIR* fDIR; |
| 136 |
SkString fPath, fSuffix; |
| 137 |
}; |
| 138 |
static_assert(sizeof(SkOSFileIterData) <= SkOSFile::Iter::kStorageSize, "not_enough_space"); |
| 139 |
|
| 140 |
SkOSFile::Iter::Iter() { new (fSelf) SkOSFileIterData; } |
| 141 |
|
| 142 |
SkOSFile::Iter::Iter(const char path[], const char suffix[]) { |
| 143 |
new (fSelf) SkOSFileIterData; |
| 144 |
this->reset(path, suffix); |
| 145 |
} |
| 146 |
|
| 147 |
SkOSFile::Iter::~Iter() { |
| 148 |
SkOSFileIterData& self = *reinterpret_cast<SkOSFileIterData*>(fSelf); |
| 149 |
if (self.fDIR) { |
| 150 |
::closedir(self.fDIR); |
| 151 |
} |
| 152 |
self.~SkOSFileIterData(); |
| 153 |
} |
| 154 |
|
| 155 |
void SkOSFile::Iter::reset(const char path[], const char suffix[]) { |
| 156 |
SkOSFileIterData& self = *reinterpret_cast<SkOSFileIterData*>(fSelf); |
| 157 |
if (self.fDIR) { |
| 158 |
::closedir(self.fDIR); |
| 159 |
self.fDIR = nullptr; |
| 160 |
} |
| 161 |
self.fPath.set(path); |
| 162 |
|
| 163 |
if (path) { |
| 164 |
self.fDIR = ::opendir(path); |
| 165 |
#ifdef SK_BUILD_FOR_IOS |
| 166 |
// check bundle for directory |
| 167 |
if (!self.fDIR && ios_get_path_in_bundle(path, &self.fPath)) { |
| 168 |
self.fDIR = ::opendir(self.fPath.c_str()); |
| 169 |
} |
| 170 |
#endif |
| 171 |
self.fSuffix.set(suffix); |
| 172 |
} else { |
| 173 |
self.fSuffix.reset(); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
// returns true if suffix is empty, or if str ends with suffix |
| 178 |
static bool issuffixfor(const SkString& suffix, const char str[]) { |
| 179 |
size_t suffixLen = suffix.size(); |
| 180 |
size_t strLen = strlen(str); |
| 181 |
|
| 182 |
return strLen >= suffixLen && |
| 183 |
memcmp(suffix.c_str(), str + strLen - suffixLen, suffixLen) == 0; |
| 184 |
} |
| 185 |
|
| 186 |
bool SkOSFile::Iter::next(SkString* name, bool getDir) { |
| 187 |
SkOSFileIterData& self = *reinterpret_cast<SkOSFileIterData*>(fSelf); |
| 188 |
if (self.fDIR) { |
| 189 |
dirent* entry; |
| 190 |
|
| 191 |
while ((entry = ::readdir(self.fDIR)) != nullptr) { |
| 192 |
struct stat s = {}; |
| 193 |
SkString str(self.fPath); |
| 194 |
|
| 195 |
if (!str.endsWith("/") && !str.endsWith("\\")) { |
| 196 |
str.append("/"); |
| 197 |
} |
| 198 |
str.append(entry->d_name); |
| 199 |
|
| 200 |
if (0 == stat(str.c_str(), &s)) { |
| 201 |
if (getDir) { |
| 202 |
if (s.st_mode & S_IFDIR) { |
| 203 |
break; |
| 204 |
} |
| 205 |
} else { |
| 206 |
if (!(s.st_mode & S_IFDIR) && issuffixfor(self.fSuffix, entry->d_name)) { |
| 207 |
break; |
| 208 |
} |
| 209 |
} |
| 210 |
} |
| 211 |
} |
| 212 |
if (entry) { // we broke out with a file |
| 213 |
if (name) { |
| 214 |
name->set(entry->d_name); |
| 215 |
} |
| 216 |
return true; |
| 217 |
} |
| 218 |
} |
| 219 |
return false; |
| 220 |
} |