blob: 1985607ac7e8cdf147e8f7b6ae05d49fa629626b [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
8#include "SkOSFile.h"
bungemanf20488b2015-07-29 11:49:40 -07009#include "SkString.h"
bungeman@google.comf5cc5b12013-07-12 18:22:49 +000010#include "SkTFitsIn.h"
bungemanf20488b2015-07-29 11:49:40 -070011#include "SkTemplates.h"
bungemane998b7f2015-02-12 07:18:27 -080012#include "SkTypes.h"
bungeman@google.com11c9a552013-06-03 17:10:35 +000013
bungemane998b7f2015-02-12 07:18:27 -080014#include <dirent.h>
bungeman@google.com6cab1a42013-05-29 13:43:31 +000015#include <stdio.h>
bungemanf20488b2015-07-29 11:49:40 -070016#include <string.h>
bungeman@google.com6cab1a42013-05-29 13:43:31 +000017#include <sys/mman.h>
18#include <sys/stat.h>
19#include <sys/types.h>
bungemanbf0b9ce2014-07-10 15:18:23 -070020#include <unistd.h>
21
Jim Van Verth329c5a62017-11-29 11:42:33 -050022#ifdef SK_BUILD_FOR_IOS
23#include "SkOSFile_ios.h"
24#endif
25
bungemanbf0b9ce2014-07-10 15:18:23 -070026bool sk_exists(const char *path, SkFILE_Flags flags) {
27 int mode = F_OK;
28 if (flags & kRead_SkFILE_Flag) {
29 mode |= R_OK;
30 }
31 if (flags & kWrite_SkFILE_Flag) {
32 mode |= W_OK;
33 }
Jim Van Verth329c5a62017-11-29 11:42:33 -050034#ifdef SK_BUILD_FOR_IOS
35 // if the default path fails, check the bundle (but only if read-only)
36 if (0 == access(path, mode)) {
37 return true;
38 } else {
39 return (kRead_SkFILE_Flag == flags && ios_get_path_in_bundle(path, nullptr));
40 }
41#else
bungemanbf0b9ce2014-07-10 15:18:23 -070042 return (0 == access(path, mode));
Jim Van Verth329c5a62017-11-29 11:42:33 -050043#endif
bungemanbf0b9ce2014-07-10 15:18:23 -070044}
bungeman@google.com6cab1a42013-05-29 13:43:31 +000045
46typedef struct {
47 dev_t dev;
48 ino_t ino;
49} SkFILEID;
50
halcanaryd76be9c2015-11-20 13:47:49 -080051static bool sk_ino(FILE* a, SkFILEID* id) {
52 int fd = fileno(a);
bungeman@google.com6cab1a42013-05-29 13:43:31 +000053 if (fd < 0) {
54 return 0;
55 }
56 struct stat status;
57 if (0 != fstat(fd, &status)) {
58 return 0;
59 }
60 id->dev = status.st_dev;
61 id->ino = status.st_ino;
62 return true;
63}
64
halcanaryd76be9c2015-11-20 13:47:49 -080065bool sk_fidentical(FILE* a, FILE* b) {
bungeman@google.com6cab1a42013-05-29 13:43:31 +000066 SkFILEID aID, bID;
67 return sk_ino(a, &aID) && sk_ino(b, &bID)
68 && aID.ino == bID.ino
69 && aID.dev == bID.dev;
70}
71
72void sk_fmunmap(const void* addr, size_t length) {
73 munmap(const_cast<void*>(addr), length);
74}
75
bungeman@google.com11c9a552013-06-03 17:10:35 +000076void* sk_fdmmap(int fd, size_t* size) {
77 struct stat status;
78 if (0 != fstat(fd, &status)) {
halcanary96fcdcc2015-08-27 07:41:13 -070079 return nullptr;
bungeman@google.com6cab1a42013-05-29 13:43:31 +000080 }
bungeman@google.com11c9a552013-06-03 17:10:35 +000081 if (!S_ISREG(status.st_mode)) {
halcanary96fcdcc2015-08-27 07:41:13 -070082 return nullptr;
bungeman@google.com6cab1a42013-05-29 13:43:31 +000083 }
bungeman@google.com11c9a552013-06-03 17:10:35 +000084 if (!SkTFitsIn<size_t>(status.st_size)) {
halcanary96fcdcc2015-08-27 07:41:13 -070085 return nullptr;
bungeman@google.com11c9a552013-06-03 17:10:35 +000086 }
87 size_t fileSize = static_cast<size_t>(status.st_size);
bungeman@google.com6cab1a42013-05-29 13:43:31 +000088
halcanary96fcdcc2015-08-27 07:41:13 -070089 void* addr = mmap(nullptr, fileSize, PROT_READ, MAP_PRIVATE, fd, 0);
bungeman@google.com6cab1a42013-05-29 13:43:31 +000090 if (MAP_FAILED == addr) {
halcanary96fcdcc2015-08-27 07:41:13 -070091 return nullptr;
bungeman@google.com6cab1a42013-05-29 13:43:31 +000092 }
93
94 *size = fileSize;
95 return addr;
96}
bungeman@google.com11c9a552013-06-03 17:10:35 +000097
halcanaryd76be9c2015-11-20 13:47:49 -080098int sk_fileno(FILE* f) {
99 return fileno(f);
bungeman@google.com11c9a552013-06-03 17:10:35 +0000100}
101
halcanaryd76be9c2015-11-20 13:47:49 -0800102void* sk_fmmap(FILE* f, size_t* size) {
bungeman@google.com11c9a552013-06-03 17:10:35 +0000103 int fd = sk_fileno(f);
104 if (fd < 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700105 return nullptr;
bungeman@google.com11c9a552013-06-03 17:10:35 +0000106 }
skia.committer@gmail.com11f2b442013-06-04 07:00:53 +0000107
bungeman@google.com11c9a552013-06-03 17:10:35 +0000108 return sk_fdmmap(fd, size);
109}
bungemane998b7f2015-02-12 07:18:27 -0800110
Ben Wagner4d1955c2017-03-10 13:08:15 -0500111size_t sk_qread(FILE* file, void* buffer, size_t count, size_t offset) {
112 int fd = sk_fileno(file);
113 if (fd < 0) {
114 return SIZE_MAX;
115 }
116 ssize_t bytesRead = pread(fd, buffer, count, offset);
117 if (bytesRead < 0) {
118 return SIZE_MAX;
119 }
120 return bytesRead;
121}
122
bungemane998b7f2015-02-12 07:18:27 -0800123////////////////////////////////////////////////////////////////////////////
124
125struct SkOSFileIterData {
Ben Wagnera93a14a2017-08-28 10:34:05 -0400126 SkOSFileIterData() : fDIR(nullptr) { }
bungemane998b7f2015-02-12 07:18:27 -0800127 DIR* fDIR;
128 SkString fPath, fSuffix;
129};
bungeman99fe8222015-08-20 07:57:51 -0700130static_assert(sizeof(SkOSFileIterData) <= SkOSFile::Iter::kStorageSize, "not_enough_space");
bungemane998b7f2015-02-12 07:18:27 -0800131
halcanary385fe4d2015-08-26 13:07:48 -0700132SkOSFile::Iter::Iter() { new (fSelf.get()) SkOSFileIterData; }
bungemane998b7f2015-02-12 07:18:27 -0800133
134SkOSFile::Iter::Iter(const char path[], const char suffix[]) {
halcanary385fe4d2015-08-26 13:07:48 -0700135 new (fSelf.get()) SkOSFileIterData;
bungemane998b7f2015-02-12 07:18:27 -0800136 this->reset(path, suffix);
137}
138
139SkOSFile::Iter::~Iter() {
140 SkOSFileIterData& self = *static_cast<SkOSFileIterData*>(fSelf.get());
141 if (self.fDIR) {
142 ::closedir(self.fDIR);
143 }
144 self.~SkOSFileIterData();
145}
146
147void SkOSFile::Iter::reset(const char path[], const char suffix[]) {
148 SkOSFileIterData& self = *static_cast<SkOSFileIterData*>(fSelf.get());
149 if (self.fDIR) {
150 ::closedir(self.fDIR);
Ben Wagnera93a14a2017-08-28 10:34:05 -0400151 self.fDIR = nullptr;
bungemane998b7f2015-02-12 07:18:27 -0800152 }
bungemane998b7f2015-02-12 07:18:27 -0800153 self.fPath.set(path);
Jim Van Verth329c5a62017-11-29 11:42:33 -0500154
bungemane998b7f2015-02-12 07:18:27 -0800155 if (path) {
156 self.fDIR = ::opendir(path);
Jim Van Verth329c5a62017-11-29 11:42:33 -0500157#ifdef SK_BUILD_FOR_IOS
158 // check bundle for directory
159 if (!self.fDIR && ios_get_path_in_bundle(path, &self.fPath)) {
160 self.fDIR = ::opendir(path);
161 }
162#endif
bungemane998b7f2015-02-12 07:18:27 -0800163 self.fSuffix.set(suffix);
164 } else {
165 self.fSuffix.reset();
166 }
167}
168
169// returns true if suffix is empty, or if str ends with suffix
170static bool issuffixfor(const SkString& suffix, const char str[]) {
171 size_t suffixLen = suffix.size();
172 size_t strLen = strlen(str);
173
174 return strLen >= suffixLen &&
175 memcmp(suffix.c_str(), str + strLen - suffixLen, suffixLen) == 0;
176}
177
178bool SkOSFile::Iter::next(SkString* name, bool getDir) {
179 SkOSFileIterData& self = *static_cast<SkOSFileIterData*>(fSelf.get());
180 if (self.fDIR) {
181 dirent* entry;
182
halcanary96fcdcc2015-08-27 07:41:13 -0700183 while ((entry = ::readdir(self.fDIR)) != nullptr) {
bungemane998b7f2015-02-12 07:18:27 -0800184 struct stat s;
185 SkString str(self.fPath);
186
187 if (!str.endsWith("/") && !str.endsWith("\\")) {
188 str.append("/");
189 }
190 str.append(entry->d_name);
191
192 if (0 == stat(str.c_str(), &s)) {
193 if (getDir) {
194 if (s.st_mode & S_IFDIR) {
195 break;
196 }
197 } else {
198 if (!(s.st_mode & S_IFDIR) && issuffixfor(self.fSuffix, entry->d_name)) {
199 break;
200 }
201 }
202 }
203 }
204 if (entry) { // we broke out with a file
205 if (name) {
206 name->set(entry->d_name);
207 }
208 return true;
209 }
210 }
211 return false;
212}