blob: fa5987c04c93d0ca9ab27b38022fa4b117bcdfe1 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkString.h"
9#include "include/core/SkTypes.h"
10#include "include/private/SkTFitsIn.h"
11#include "include/private/SkTemplates.h"
12#include "src/core/SkOSFile.h"
bungeman@google.com11c9a552013-06-03 17:10:35 +000013
bungemane998b7f2015-02-12 07:18:27 -080014#include <dirent.h>
Mike Klein79aea6a2018-06-11 10:45:26 -040015#include <new>
bungeman@google.com6cab1a42013-05-29 13:43:31 +000016#include <stdio.h>
bungemanf20488b2015-07-29 11:49:40 -070017#include <string.h>
bungeman@google.com6cab1a42013-05-29 13:43:31 +000018#include <sys/mman.h>
19#include <sys/stat.h>
20#include <sys/types.h>
bungemanbf0b9ce2014-07-10 15:18:23 -070021#include <unistd.h>
22
Jim Van Verth329c5a62017-11-29 11:42:33 -050023#ifdef SK_BUILD_FOR_IOS
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "src/ports/SkOSFile_ios.h"
Jim Van Verth329c5a62017-11-29 11:42:33 -050025#endif
26
bungemanbf0b9ce2014-07-10 15:18:23 -070027bool sk_exists(const char *path, SkFILE_Flags flags) {
28 int mode = F_OK;
29 if (flags & kRead_SkFILE_Flag) {
30 mode |= R_OK;
31 }
32 if (flags & kWrite_SkFILE_Flag) {
33 mode |= W_OK;
34 }
Jim Van Verth329c5a62017-11-29 11:42:33 -050035#ifdef SK_BUILD_FOR_IOS
36 // if the default path fails, check the bundle (but only if read-only)
37 if (0 == access(path, mode)) {
38 return true;
39 } else {
40 return (kRead_SkFILE_Flag == flags && ios_get_path_in_bundle(path, nullptr));
41 }
42#else
bungemanbf0b9ce2014-07-10 15:18:23 -070043 return (0 == access(path, mode));
Jim Van Verth329c5a62017-11-29 11:42:33 -050044#endif
bungemanbf0b9ce2014-07-10 15:18:23 -070045}
bungeman@google.com6cab1a42013-05-29 13:43:31 +000046
47typedef struct {
48 dev_t dev;
49 ino_t ino;
50} SkFILEID;
51
halcanaryd76be9c2015-11-20 13:47:49 -080052static bool sk_ino(FILE* a, SkFILEID* id) {
53 int fd = fileno(a);
bungeman@google.com6cab1a42013-05-29 13:43:31 +000054 if (fd < 0) {
55 return 0;
56 }
57 struct stat status;
58 if (0 != fstat(fd, &status)) {
59 return 0;
60 }
61 id->dev = status.st_dev;
62 id->ino = status.st_ino;
63 return true;
64}
65
halcanaryd76be9c2015-11-20 13:47:49 -080066bool sk_fidentical(FILE* a, FILE* b) {
bungeman@google.com6cab1a42013-05-29 13:43:31 +000067 SkFILEID aID, bID;
68 return sk_ino(a, &aID) && sk_ino(b, &bID)
69 && aID.ino == bID.ino
70 && aID.dev == bID.dev;
71}
72
73void sk_fmunmap(const void* addr, size_t length) {
74 munmap(const_cast<void*>(addr), length);
75}
76
bungeman@google.com11c9a552013-06-03 17:10:35 +000077void* sk_fdmmap(int fd, size_t* size) {
78 struct stat status;
79 if (0 != fstat(fd, &status)) {
halcanary96fcdcc2015-08-27 07:41:13 -070080 return nullptr;
bungeman@google.com6cab1a42013-05-29 13:43:31 +000081 }
bungeman@google.com11c9a552013-06-03 17:10:35 +000082 if (!S_ISREG(status.st_mode)) {
halcanary96fcdcc2015-08-27 07:41:13 -070083 return nullptr;
bungeman@google.com6cab1a42013-05-29 13:43:31 +000084 }
bungeman@google.com11c9a552013-06-03 17:10:35 +000085 if (!SkTFitsIn<size_t>(status.st_size)) {
halcanary96fcdcc2015-08-27 07:41:13 -070086 return nullptr;
bungeman@google.com11c9a552013-06-03 17:10:35 +000087 }
88 size_t fileSize = static_cast<size_t>(status.st_size);
bungeman@google.com6cab1a42013-05-29 13:43:31 +000089
halcanary96fcdcc2015-08-27 07:41:13 -070090 void* addr = mmap(nullptr, fileSize, PROT_READ, MAP_PRIVATE, fd, 0);
bungeman@google.com6cab1a42013-05-29 13:43:31 +000091 if (MAP_FAILED == addr) {
halcanary96fcdcc2015-08-27 07:41:13 -070092 return nullptr;
bungeman@google.com6cab1a42013-05-29 13:43:31 +000093 }
94
95 *size = fileSize;
96 return addr;
97}
bungeman@google.com11c9a552013-06-03 17:10:35 +000098
halcanaryd76be9c2015-11-20 13:47:49 -080099int sk_fileno(FILE* f) {
100 return fileno(f);
bungeman@google.com11c9a552013-06-03 17:10:35 +0000101}
102
halcanaryd76be9c2015-11-20 13:47:49 -0800103void* sk_fmmap(FILE* f, size_t* size) {
bungeman@google.com11c9a552013-06-03 17:10:35 +0000104 int fd = sk_fileno(f);
105 if (fd < 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700106 return nullptr;
bungeman@google.com11c9a552013-06-03 17:10:35 +0000107 }
skia.committer@gmail.com11f2b442013-06-04 07:00:53 +0000108
bungeman@google.com11c9a552013-06-03 17:10:35 +0000109 return sk_fdmmap(fd, size);
110}
bungemane998b7f2015-02-12 07:18:27 -0800111
Ben Wagner4d1955c2017-03-10 13:08:15 -0500112size_t sk_qread(FILE* file, void* buffer, size_t count, size_t offset) {
113 int fd = sk_fileno(file);
114 if (fd < 0) {
115 return SIZE_MAX;
116 }
117 ssize_t bytesRead = pread(fd, buffer, count, offset);
118 if (bytesRead < 0) {
119 return SIZE_MAX;
120 }
121 return bytesRead;
122}
123
bungemane998b7f2015-02-12 07:18:27 -0800124////////////////////////////////////////////////////////////////////////////
125
126struct SkOSFileIterData {
Ben Wagnera93a14a2017-08-28 10:34:05 -0400127 SkOSFileIterData() : fDIR(nullptr) { }
bungemane998b7f2015-02-12 07:18:27 -0800128 DIR* fDIR;
129 SkString fPath, fSuffix;
130};
bungeman99fe8222015-08-20 07:57:51 -0700131static_assert(sizeof(SkOSFileIterData) <= SkOSFile::Iter::kStorageSize, "not_enough_space");
bungemane998b7f2015-02-12 07:18:27 -0800132
Mike Klein339f0ee2020-11-09 12:21:53 -0600133SkOSFile::Iter::Iter() { new (fSelf) SkOSFileIterData; }
bungemane998b7f2015-02-12 07:18:27 -0800134
135SkOSFile::Iter::Iter(const char path[], const char suffix[]) {
Mike Klein339f0ee2020-11-09 12:21:53 -0600136 new (fSelf) SkOSFileIterData;
bungemane998b7f2015-02-12 07:18:27 -0800137 this->reset(path, suffix);
138}
139
140SkOSFile::Iter::~Iter() {
Mike Klein339f0ee2020-11-09 12:21:53 -0600141 SkOSFileIterData& self = *reinterpret_cast<SkOSFileIterData*>(fSelf);
bungemane998b7f2015-02-12 07:18:27 -0800142 if (self.fDIR) {
143 ::closedir(self.fDIR);
144 }
145 self.~SkOSFileIterData();
146}
147
148void SkOSFile::Iter::reset(const char path[], const char suffix[]) {
Mike Klein339f0ee2020-11-09 12:21:53 -0600149 SkOSFileIterData& self = *reinterpret_cast<SkOSFileIterData*>(fSelf);
bungemane998b7f2015-02-12 07:18:27 -0800150 if (self.fDIR) {
151 ::closedir(self.fDIR);
Ben Wagnera93a14a2017-08-28 10:34:05 -0400152 self.fDIR = nullptr;
bungemane998b7f2015-02-12 07:18:27 -0800153 }
bungemane998b7f2015-02-12 07:18:27 -0800154 self.fPath.set(path);
Jim Van Verth329c5a62017-11-29 11:42:33 -0500155
bungemane998b7f2015-02-12 07:18:27 -0800156 if (path) {
157 self.fDIR = ::opendir(path);
Jim Van Verth329c5a62017-11-29 11:42:33 -0500158#ifdef SK_BUILD_FOR_IOS
159 // check bundle for directory
160 if (!self.fDIR && ios_get_path_in_bundle(path, &self.fPath)) {
Brian Osmand5e4d852018-01-16 11:08:53 -0500161 self.fDIR = ::opendir(self.fPath.c_str());
Jim Van Verth329c5a62017-11-29 11:42:33 -0500162 }
163#endif
bungemane998b7f2015-02-12 07:18:27 -0800164 self.fSuffix.set(suffix);
165 } else {
166 self.fSuffix.reset();
167 }
168}
169
170// returns true if suffix is empty, or if str ends with suffix
171static bool issuffixfor(const SkString& suffix, const char str[]) {
172 size_t suffixLen = suffix.size();
173 size_t strLen = strlen(str);
174
175 return strLen >= suffixLen &&
176 memcmp(suffix.c_str(), str + strLen - suffixLen, suffixLen) == 0;
177}
178
179bool SkOSFile::Iter::next(SkString* name, bool getDir) {
Mike Klein339f0ee2020-11-09 12:21:53 -0600180 SkOSFileIterData& self = *reinterpret_cast<SkOSFileIterData*>(fSelf);
bungemane998b7f2015-02-12 07:18:27 -0800181 if (self.fDIR) {
182 dirent* entry;
183
halcanary96fcdcc2015-08-27 07:41:13 -0700184 while ((entry = ::readdir(self.fDIR)) != nullptr) {
bungemane998b7f2015-02-12 07:18:27 -0800185 struct stat s;
186 SkString str(self.fPath);
187
188 if (!str.endsWith("/") && !str.endsWith("\\")) {
189 str.append("/");
190 }
191 str.append(entry->d_name);
192
193 if (0 == stat(str.c_str(), &s)) {
194 if (getDir) {
195 if (s.st_mode & S_IFDIR) {
196 break;
197 }
198 } else {
199 if (!(s.st_mode & S_IFDIR) && issuffixfor(self.fSuffix, entry->d_name)) {
200 break;
201 }
202 }
203 }
204 }
205 if (entry) { // we broke out with a file
206 if (name) {
207 name->set(entry->d_name);
208 }
209 return true;
210 }
211 }
212 return false;
213}