blob: 40028246ff706d8c33915a4781db2638a81a4051 [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
Ben Wagnereefa2892017-03-17 13:49:31 -040022size_t sk_fgetsize(FILE* f) {
23 int fd = fileno(f);
24 if (fd < 0) {
25 return 0;
26 }
27
28 struct stat status;
29 if (0 != fstat(fd, &status)) {
30 return 0;
31 }
32 if (!S_ISREG(status.st_mode)) {
33 return 0;
34 }
35 if (!SkTFitsIn<size_t>(status.st_size)) {
36 return 0;
37 }
38 return static_cast<size_t>(status.st_size);
39}
40
bungemanbf0b9ce2014-07-10 15:18:23 -070041bool sk_exists(const char *path, SkFILE_Flags flags) {
42 int mode = F_OK;
43 if (flags & kRead_SkFILE_Flag) {
44 mode |= R_OK;
45 }
46 if (flags & kWrite_SkFILE_Flag) {
47 mode |= W_OK;
48 }
49 return (0 == access(path, mode));
50}
bungeman@google.com6cab1a42013-05-29 13:43:31 +000051
52typedef struct {
53 dev_t dev;
54 ino_t ino;
55} SkFILEID;
56
halcanaryd76be9c2015-11-20 13:47:49 -080057static bool sk_ino(FILE* a, SkFILEID* id) {
58 int fd = fileno(a);
bungeman@google.com6cab1a42013-05-29 13:43:31 +000059 if (fd < 0) {
60 return 0;
61 }
62 struct stat status;
63 if (0 != fstat(fd, &status)) {
64 return 0;
65 }
66 id->dev = status.st_dev;
67 id->ino = status.st_ino;
68 return true;
69}
70
halcanaryd76be9c2015-11-20 13:47:49 -080071bool sk_fidentical(FILE* a, FILE* b) {
bungeman@google.com6cab1a42013-05-29 13:43:31 +000072 SkFILEID aID, bID;
73 return sk_ino(a, &aID) && sk_ino(b, &bID)
74 && aID.ino == bID.ino
75 && aID.dev == bID.dev;
76}
77
78void sk_fmunmap(const void* addr, size_t length) {
79 munmap(const_cast<void*>(addr), length);
80}
81
bungeman@google.com11c9a552013-06-03 17:10:35 +000082void* sk_fdmmap(int fd, size_t* size) {
83 struct stat status;
84 if (0 != fstat(fd, &status)) {
halcanary96fcdcc2015-08-27 07:41:13 -070085 return nullptr;
bungeman@google.com6cab1a42013-05-29 13:43:31 +000086 }
bungeman@google.com11c9a552013-06-03 17:10:35 +000087 if (!S_ISREG(status.st_mode)) {
halcanary96fcdcc2015-08-27 07:41:13 -070088 return nullptr;
bungeman@google.com6cab1a42013-05-29 13:43:31 +000089 }
bungeman@google.com11c9a552013-06-03 17:10:35 +000090 if (!SkTFitsIn<size_t>(status.st_size)) {
halcanary96fcdcc2015-08-27 07:41:13 -070091 return nullptr;
bungeman@google.com11c9a552013-06-03 17:10:35 +000092 }
93 size_t fileSize = static_cast<size_t>(status.st_size);
bungeman@google.com6cab1a42013-05-29 13:43:31 +000094
halcanary96fcdcc2015-08-27 07:41:13 -070095 void* addr = mmap(nullptr, fileSize, PROT_READ, MAP_PRIVATE, fd, 0);
bungeman@google.com6cab1a42013-05-29 13:43:31 +000096 if (MAP_FAILED == addr) {
halcanary96fcdcc2015-08-27 07:41:13 -070097 return nullptr;
bungeman@google.com6cab1a42013-05-29 13:43:31 +000098 }
99
100 *size = fileSize;
101 return addr;
102}
bungeman@google.com11c9a552013-06-03 17:10:35 +0000103
halcanaryd76be9c2015-11-20 13:47:49 -0800104int sk_fileno(FILE* f) {
105 return fileno(f);
bungeman@google.com11c9a552013-06-03 17:10:35 +0000106}
107
halcanaryd76be9c2015-11-20 13:47:49 -0800108void* sk_fmmap(FILE* f, size_t* size) {
bungeman@google.com11c9a552013-06-03 17:10:35 +0000109 int fd = sk_fileno(f);
110 if (fd < 0) {
halcanary96fcdcc2015-08-27 07:41:13 -0700111 return nullptr;
bungeman@google.com11c9a552013-06-03 17:10:35 +0000112 }
skia.committer@gmail.com11f2b442013-06-04 07:00:53 +0000113
bungeman@google.com11c9a552013-06-03 17:10:35 +0000114 return sk_fdmmap(fd, size);
115}
bungemane998b7f2015-02-12 07:18:27 -0800116
Ben Wagner4d1955c2017-03-10 13:08:15 -0500117size_t sk_qread(FILE* file, void* buffer, size_t count, size_t offset) {
118 int fd = sk_fileno(file);
119 if (fd < 0) {
120 return SIZE_MAX;
121 }
122 ssize_t bytesRead = pread(fd, buffer, count, offset);
123 if (bytesRead < 0) {
124 return SIZE_MAX;
125 }
126 return bytesRead;
127}
128
bungemane998b7f2015-02-12 07:18:27 -0800129////////////////////////////////////////////////////////////////////////////
130
131struct SkOSFileIterData {
132 SkOSFileIterData() : fDIR(0) { }
133 DIR* fDIR;
134 SkString fPath, fSuffix;
135};
bungeman99fe8222015-08-20 07:57:51 -0700136static_assert(sizeof(SkOSFileIterData) <= SkOSFile::Iter::kStorageSize, "not_enough_space");
bungemane998b7f2015-02-12 07:18:27 -0800137
halcanary385fe4d2015-08-26 13:07:48 -0700138SkOSFile::Iter::Iter() { new (fSelf.get()) SkOSFileIterData; }
bungemane998b7f2015-02-12 07:18:27 -0800139
140SkOSFile::Iter::Iter(const char path[], const char suffix[]) {
halcanary385fe4d2015-08-26 13:07:48 -0700141 new (fSelf.get()) SkOSFileIterData;
bungemane998b7f2015-02-12 07:18:27 -0800142 this->reset(path, suffix);
143}
144
145SkOSFile::Iter::~Iter() {
146 SkOSFileIterData& self = *static_cast<SkOSFileIterData*>(fSelf.get());
147 if (self.fDIR) {
148 ::closedir(self.fDIR);
149 }
150 self.~SkOSFileIterData();
151}
152
153void SkOSFile::Iter::reset(const char path[], const char suffix[]) {
154 SkOSFileIterData& self = *static_cast<SkOSFileIterData*>(fSelf.get());
155 if (self.fDIR) {
156 ::closedir(self.fDIR);
157 self.fDIR = 0;
158 }
159
160 self.fPath.set(path);
161 if (path) {
162 self.fDIR = ::opendir(path);
163 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}