blob: 48b5b95ad37fd5038ae037f0173912b38355943d [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
22bool sk_exists(const char *path, SkFILE_Flags flags) {
23 int mode = F_OK;
24 if (flags & kRead_SkFILE_Flag) {
25 mode |= R_OK;
26 }
27 if (flags & kWrite_SkFILE_Flag) {
28 mode |= W_OK;
29 }
30 return (0 == access(path, mode));
31}
bungeman@google.com6cab1a42013-05-29 13:43:31 +000032
33typedef struct {
34 dev_t dev;
35 ino_t ino;
36} SkFILEID;
37
halcanaryd76be9c2015-11-20 13:47:49 -080038static bool sk_ino(FILE* a, SkFILEID* id) {
39 int fd = fileno(a);
bungeman@google.com6cab1a42013-05-29 13:43:31 +000040 if (fd < 0) {
41 return 0;
42 }
43 struct stat status;
44 if (0 != fstat(fd, &status)) {
45 return 0;
46 }
47 id->dev = status.st_dev;
48 id->ino = status.st_ino;
49 return true;
50}
51
halcanaryd76be9c2015-11-20 13:47:49 -080052bool sk_fidentical(FILE* a, FILE* b) {
bungeman@google.com6cab1a42013-05-29 13:43:31 +000053 SkFILEID aID, bID;
54 return sk_ino(a, &aID) && sk_ino(b, &bID)
55 && aID.ino == bID.ino
56 && aID.dev == bID.dev;
57}
58
59void sk_fmunmap(const void* addr, size_t length) {
60 munmap(const_cast<void*>(addr), length);
61}
62
bungeman@google.com11c9a552013-06-03 17:10:35 +000063void* sk_fdmmap(int fd, size_t* size) {
64 struct stat status;
65 if (0 != fstat(fd, &status)) {
halcanary96fcdcc2015-08-27 07:41:13 -070066 return nullptr;
bungeman@google.com6cab1a42013-05-29 13:43:31 +000067 }
bungeman@google.com11c9a552013-06-03 17:10:35 +000068 if (!S_ISREG(status.st_mode)) {
halcanary96fcdcc2015-08-27 07:41:13 -070069 return nullptr;
bungeman@google.com6cab1a42013-05-29 13:43:31 +000070 }
bungeman@google.com11c9a552013-06-03 17:10:35 +000071 if (!SkTFitsIn<size_t>(status.st_size)) {
halcanary96fcdcc2015-08-27 07:41:13 -070072 return nullptr;
bungeman@google.com11c9a552013-06-03 17:10:35 +000073 }
74 size_t fileSize = static_cast<size_t>(status.st_size);
bungeman@google.com6cab1a42013-05-29 13:43:31 +000075
halcanary96fcdcc2015-08-27 07:41:13 -070076 void* addr = mmap(nullptr, fileSize, PROT_READ, MAP_PRIVATE, fd, 0);
bungeman@google.com6cab1a42013-05-29 13:43:31 +000077 if (MAP_FAILED == addr) {
halcanary96fcdcc2015-08-27 07:41:13 -070078 return nullptr;
bungeman@google.com6cab1a42013-05-29 13:43:31 +000079 }
80
81 *size = fileSize;
82 return addr;
83}
bungeman@google.com11c9a552013-06-03 17:10:35 +000084
halcanaryd76be9c2015-11-20 13:47:49 -080085int sk_fileno(FILE* f) {
86 return fileno(f);
bungeman@google.com11c9a552013-06-03 17:10:35 +000087}
88
halcanaryd76be9c2015-11-20 13:47:49 -080089void* sk_fmmap(FILE* f, size_t* size) {
bungeman@google.com11c9a552013-06-03 17:10:35 +000090 int fd = sk_fileno(f);
91 if (fd < 0) {
halcanary96fcdcc2015-08-27 07:41:13 -070092 return nullptr;
bungeman@google.com11c9a552013-06-03 17:10:35 +000093 }
skia.committer@gmail.com11f2b442013-06-04 07:00:53 +000094
bungeman@google.com11c9a552013-06-03 17:10:35 +000095 return sk_fdmmap(fd, size);
96}
bungemane998b7f2015-02-12 07:18:27 -080097
Ben Wagner4d1955c2017-03-10 13:08:15 -050098size_t sk_qread(FILE* file, void* buffer, size_t count, size_t offset) {
99 int fd = sk_fileno(file);
100 if (fd < 0) {
101 return SIZE_MAX;
102 }
103 ssize_t bytesRead = pread(fd, buffer, count, offset);
104 if (bytesRead < 0) {
105 return SIZE_MAX;
106 }
107 return bytesRead;
108}
109
bungemane998b7f2015-02-12 07:18:27 -0800110////////////////////////////////////////////////////////////////////////////
111
112struct SkOSFileIterData {
113 SkOSFileIterData() : fDIR(0) { }
114 DIR* fDIR;
115 SkString fPath, fSuffix;
116};
bungeman99fe8222015-08-20 07:57:51 -0700117static_assert(sizeof(SkOSFileIterData) <= SkOSFile::Iter::kStorageSize, "not_enough_space");
bungemane998b7f2015-02-12 07:18:27 -0800118
halcanary385fe4d2015-08-26 13:07:48 -0700119SkOSFile::Iter::Iter() { new (fSelf.get()) SkOSFileIterData; }
bungemane998b7f2015-02-12 07:18:27 -0800120
121SkOSFile::Iter::Iter(const char path[], const char suffix[]) {
halcanary385fe4d2015-08-26 13:07:48 -0700122 new (fSelf.get()) SkOSFileIterData;
bungemane998b7f2015-02-12 07:18:27 -0800123 this->reset(path, suffix);
124}
125
126SkOSFile::Iter::~Iter() {
127 SkOSFileIterData& self = *static_cast<SkOSFileIterData*>(fSelf.get());
128 if (self.fDIR) {
129 ::closedir(self.fDIR);
130 }
131 self.~SkOSFileIterData();
132}
133
134void SkOSFile::Iter::reset(const char path[], const char suffix[]) {
135 SkOSFileIterData& self = *static_cast<SkOSFileIterData*>(fSelf.get());
136 if (self.fDIR) {
137 ::closedir(self.fDIR);
138 self.fDIR = 0;
139 }
140
141 self.fPath.set(path);
142 if (path) {
143 self.fDIR = ::opendir(path);
144 self.fSuffix.set(suffix);
145 } else {
146 self.fSuffix.reset();
147 }
148}
149
150// returns true if suffix is empty, or if str ends with suffix
151static bool issuffixfor(const SkString& suffix, const char str[]) {
152 size_t suffixLen = suffix.size();
153 size_t strLen = strlen(str);
154
155 return strLen >= suffixLen &&
156 memcmp(suffix.c_str(), str + strLen - suffixLen, suffixLen) == 0;
157}
158
159bool SkOSFile::Iter::next(SkString* name, bool getDir) {
160 SkOSFileIterData& self = *static_cast<SkOSFileIterData*>(fSelf.get());
161 if (self.fDIR) {
162 dirent* entry;
163
halcanary96fcdcc2015-08-27 07:41:13 -0700164 while ((entry = ::readdir(self.fDIR)) != nullptr) {
bungemane998b7f2015-02-12 07:18:27 -0800165 struct stat s;
166 SkString str(self.fPath);
167
168 if (!str.endsWith("/") && !str.endsWith("\\")) {
169 str.append("/");
170 }
171 str.append(entry->d_name);
172
173 if (0 == stat(str.c_str(), &s)) {
174 if (getDir) {
175 if (s.st_mode & S_IFDIR) {
176 break;
177 }
178 } else {
179 if (!(s.st_mode & S_IFDIR) && issuffixfor(self.fSuffix, entry->d_name)) {
180 break;
181 }
182 }
183 }
184 }
185 if (entry) { // we broke out with a file
186 if (name) {
187 name->set(entry->d_name);
188 }
189 return true;
190 }
191 }
192 return false;
193}