blob: b7cf583440ff7bd1e65622324ca2fec500dbd9a7 [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
38static bool sk_ino(SkFILE* a, SkFILEID* id) {
39 int fd = fileno((FILE*)a);
40 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
52bool sk_fidentical(SkFILE* a, SkFILE* b) {
53 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)) {
bungeman@google.com6cab1a42013-05-29 13:43:31 +000066 return NULL;
67 }
bungeman@google.com11c9a552013-06-03 17:10:35 +000068 if (!S_ISREG(status.st_mode)) {
bungeman@google.com6cab1a42013-05-29 13:43:31 +000069 return NULL;
70 }
bungeman@google.com11c9a552013-06-03 17:10:35 +000071 if (!SkTFitsIn<size_t>(status.st_size)) {
72 return NULL;
73 }
74 size_t fileSize = static_cast<size_t>(status.st_size);
bungeman@google.com6cab1a42013-05-29 13:43:31 +000075
76 void* addr = mmap(NULL, fileSize, PROT_READ, MAP_PRIVATE, fd, 0);
77 if (MAP_FAILED == addr) {
78 return NULL;
79 }
80
81 *size = fileSize;
82 return addr;
83}
bungeman@google.com11c9a552013-06-03 17:10:35 +000084
85int sk_fileno(SkFILE* f) {
86 return fileno((FILE*)f);
87}
88
89void* sk_fmmap(SkFILE* f, size_t* size) {
90 int fd = sk_fileno(f);
91 if (fd < 0) {
92 return NULL;
93 }
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
98////////////////////////////////////////////////////////////////////////////
99
100struct SkOSFileIterData {
101 SkOSFileIterData() : fDIR(0) { }
102 DIR* fDIR;
103 SkString fPath, fSuffix;
104};
bungeman99fe8222015-08-20 07:57:51 -0700105static_assert(sizeof(SkOSFileIterData) <= SkOSFile::Iter::kStorageSize, "not_enough_space");
bungemane998b7f2015-02-12 07:18:27 -0800106
107SkOSFile::Iter::Iter() {
108 SkNEW_PLACEMENT(fSelf.get(), SkOSFileIterData);
109}
110
111SkOSFile::Iter::Iter(const char path[], const char suffix[]) {
112 SkNEW_PLACEMENT(fSelf.get(), SkOSFileIterData);
113 this->reset(path, suffix);
114}
115
116SkOSFile::Iter::~Iter() {
117 SkOSFileIterData& self = *static_cast<SkOSFileIterData*>(fSelf.get());
118 if (self.fDIR) {
119 ::closedir(self.fDIR);
120 }
121 self.~SkOSFileIterData();
122}
123
124void SkOSFile::Iter::reset(const char path[], const char suffix[]) {
125 SkOSFileIterData& self = *static_cast<SkOSFileIterData*>(fSelf.get());
126 if (self.fDIR) {
127 ::closedir(self.fDIR);
128 self.fDIR = 0;
129 }
130
131 self.fPath.set(path);
132 if (path) {
133 self.fDIR = ::opendir(path);
134 self.fSuffix.set(suffix);
135 } else {
136 self.fSuffix.reset();
137 }
138}
139
140// returns true if suffix is empty, or if str ends with suffix
141static bool issuffixfor(const SkString& suffix, const char str[]) {
142 size_t suffixLen = suffix.size();
143 size_t strLen = strlen(str);
144
145 return strLen >= suffixLen &&
146 memcmp(suffix.c_str(), str + strLen - suffixLen, suffixLen) == 0;
147}
148
149bool SkOSFile::Iter::next(SkString* name, bool getDir) {
150 SkOSFileIterData& self = *static_cast<SkOSFileIterData*>(fSelf.get());
151 if (self.fDIR) {
152 dirent* entry;
153
154 while ((entry = ::readdir(self.fDIR)) != NULL) {
155 struct stat s;
156 SkString str(self.fPath);
157
158 if (!str.endsWith("/") && !str.endsWith("\\")) {
159 str.append("/");
160 }
161 str.append(entry->d_name);
162
163 if (0 == stat(str.c_str(), &s)) {
164 if (getDir) {
165 if (s.st_mode & S_IFDIR) {
166 break;
167 }
168 } else {
169 if (!(s.st_mode & S_IFDIR) && issuffixfor(self.fSuffix, entry->d_name)) {
170 break;
171 }
172 }
173 }
174 }
175 if (entry) { // we broke out with a file
176 if (name) {
177 name->set(entry->d_name);
178 }
179 return true;
180 }
181 }
182 return false;
183}