Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
SkOSFile_posix.cpp
Go to the documentation of this file.
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
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
25#endif
26
27void 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
34bool sk_exists(const char *path, SkFILE_Flags flags) {
35 int mode = F_OK;
37 mode |= R_OK;
38 }
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
54typedef struct {
55 dev_t dev;
56 ino_t ino;
57} SkFILEID;
58
59static 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
73bool 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
80void sk_fmunmap(const void* addr, size_t length) {
81 munmap(const_cast<void*>(addr), length);
82}
83
84void* 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
106int sk_fileno(FILE* f) {
107 return fileno(f);
108}
109
110void* 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
119size_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
138static_assert(sizeof(SkOSFileIterData) <= SkOSFile::Iter::kStorageSize, "not_enough_space");
139
141
142SkOSFile::Iter::Iter(const char path[], const char suffix[]) {
143 new (fSelf) SkOSFileIterData;
144 this->reset(path, suffix);
145}
146
148 SkOSFileIterData& self = *reinterpret_cast<SkOSFileIterData*>(fSelf);
149 if (self.fDIR) {
150 ::closedir(self.fDIR);
151 }
152 self.~SkOSFileIterData();
153}
154
155void 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
178static 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
186bool 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}
m reset()
int count
SkFILE_Flags
Definition SkOSFile.h:19
@ kRead_SkFILE_Flag
Definition SkOSFile.h:20
@ kWrite_SkFILE_Flag
Definition SkOSFile.h:21
static bool issuffixfor(const SkString &suffix, const char str[])
bool sk_exists(const char *path, SkFILE_Flags flags)
int sk_fileno(FILE *f)
size_t sk_qread(FILE *file, void *buffer, size_t count, size_t offset)
void sk_fmunmap(const void *addr, size_t length)
void * sk_fmmap(FILE *f, size_t *size)
void sk_fsync(FILE *f)
bool sk_fidentical(FILE *a, FILE *b)
static bool sk_ino(FILE *a, SkFILEID *id)
void * sk_fdmmap(int fd, size_t *size)
SK_SPI void reset(const char path[], const char suffix[]=nullptr)
SK_SPI bool next(SkString *name, bool getDir=false)
static const size_t kStorageSize
Definition SkOSFile.h:95
void append(const char text[])
Definition SkString.h:203
bool endsWith(const char suffixStr[]) const
Definition SkString.h:146
const char * c_str() const
Definition SkString.h:133
static bool b
struct MyStruct s
struct MyStruct a[10]
FlutterSemanticsFlag flags
static const uint8_t buffer[]
const char * name
Definition fuchsia.cc:50
size_t length
Point offset