bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 1 | /* |
| 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" |
bungeman | f20488b | 2015-07-29 11:49:40 -0700 | [diff] [blame] | 9 | #include "SkString.h" |
bungeman@google.com | f5cc5b1 | 2013-07-12 18:22:49 +0000 | [diff] [blame] | 10 | #include "SkTFitsIn.h" |
bungeman | f20488b | 2015-07-29 11:49:40 -0700 | [diff] [blame] | 11 | #include "SkTemplates.h" |
bungeman | e998b7f | 2015-02-12 07:18:27 -0800 | [diff] [blame] | 12 | #include "SkTypes.h" |
bungeman@google.com | 11c9a55 | 2013-06-03 17:10:35 +0000 | [diff] [blame] | 13 | |
bungeman | e998b7f | 2015-02-12 07:18:27 -0800 | [diff] [blame] | 14 | #include <dirent.h> |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 15 | #include <stdio.h> |
bungeman | f20488b | 2015-07-29 11:49:40 -0700 | [diff] [blame] | 16 | #include <string.h> |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 17 | #include <sys/mman.h> |
| 18 | #include <sys/stat.h> |
| 19 | #include <sys/types.h> |
bungeman | bf0b9ce | 2014-07-10 15:18:23 -0700 | [diff] [blame] | 20 | #include <unistd.h> |
| 21 | |
Ben Wagner | eefa289 | 2017-03-17 13:49:31 -0400 | [diff] [blame] | 22 | size_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 | |
bungeman | bf0b9ce | 2014-07-10 15:18:23 -0700 | [diff] [blame] | 41 | bool 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.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 51 | |
| 52 | typedef struct { |
| 53 | dev_t dev; |
| 54 | ino_t ino; |
| 55 | } SkFILEID; |
| 56 | |
halcanary | d76be9c | 2015-11-20 13:47:49 -0800 | [diff] [blame] | 57 | static bool sk_ino(FILE* a, SkFILEID* id) { |
| 58 | int fd = fileno(a); |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 59 | 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 | |
halcanary | d76be9c | 2015-11-20 13:47:49 -0800 | [diff] [blame] | 71 | bool sk_fidentical(FILE* a, FILE* b) { |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 72 | 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 | |
| 78 | void sk_fmunmap(const void* addr, size_t length) { |
| 79 | munmap(const_cast<void*>(addr), length); |
| 80 | } |
| 81 | |
bungeman@google.com | 11c9a55 | 2013-06-03 17:10:35 +0000 | [diff] [blame] | 82 | void* sk_fdmmap(int fd, size_t* size) { |
| 83 | struct stat status; |
| 84 | if (0 != fstat(fd, &status)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 85 | return nullptr; |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 86 | } |
bungeman@google.com | 11c9a55 | 2013-06-03 17:10:35 +0000 | [diff] [blame] | 87 | if (!S_ISREG(status.st_mode)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 88 | return nullptr; |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 89 | } |
bungeman@google.com | 11c9a55 | 2013-06-03 17:10:35 +0000 | [diff] [blame] | 90 | if (!SkTFitsIn<size_t>(status.st_size)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 91 | return nullptr; |
bungeman@google.com | 11c9a55 | 2013-06-03 17:10:35 +0000 | [diff] [blame] | 92 | } |
| 93 | size_t fileSize = static_cast<size_t>(status.st_size); |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 94 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 95 | void* addr = mmap(nullptr, fileSize, PROT_READ, MAP_PRIVATE, fd, 0); |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 96 | if (MAP_FAILED == addr) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 97 | return nullptr; |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | *size = fileSize; |
| 101 | return addr; |
| 102 | } |
bungeman@google.com | 11c9a55 | 2013-06-03 17:10:35 +0000 | [diff] [blame] | 103 | |
halcanary | d76be9c | 2015-11-20 13:47:49 -0800 | [diff] [blame] | 104 | int sk_fileno(FILE* f) { |
| 105 | return fileno(f); |
bungeman@google.com | 11c9a55 | 2013-06-03 17:10:35 +0000 | [diff] [blame] | 106 | } |
| 107 | |
halcanary | d76be9c | 2015-11-20 13:47:49 -0800 | [diff] [blame] | 108 | void* sk_fmmap(FILE* f, size_t* size) { |
bungeman@google.com | 11c9a55 | 2013-06-03 17:10:35 +0000 | [diff] [blame] | 109 | int fd = sk_fileno(f); |
| 110 | if (fd < 0) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 111 | return nullptr; |
bungeman@google.com | 11c9a55 | 2013-06-03 17:10:35 +0000 | [diff] [blame] | 112 | } |
skia.committer@gmail.com | 11f2b44 | 2013-06-04 07:00:53 +0000 | [diff] [blame] | 113 | |
bungeman@google.com | 11c9a55 | 2013-06-03 17:10:35 +0000 | [diff] [blame] | 114 | return sk_fdmmap(fd, size); |
| 115 | } |
bungeman | e998b7f | 2015-02-12 07:18:27 -0800 | [diff] [blame] | 116 | |
Ben Wagner | 4d1955c | 2017-03-10 13:08:15 -0500 | [diff] [blame] | 117 | size_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 | |
bungeman | e998b7f | 2015-02-12 07:18:27 -0800 | [diff] [blame] | 129 | //////////////////////////////////////////////////////////////////////////// |
| 130 | |
| 131 | struct SkOSFileIterData { |
| 132 | SkOSFileIterData() : fDIR(0) { } |
| 133 | DIR* fDIR; |
| 134 | SkString fPath, fSuffix; |
| 135 | }; |
bungeman | 99fe822 | 2015-08-20 07:57:51 -0700 | [diff] [blame] | 136 | static_assert(sizeof(SkOSFileIterData) <= SkOSFile::Iter::kStorageSize, "not_enough_space"); |
bungeman | e998b7f | 2015-02-12 07:18:27 -0800 | [diff] [blame] | 137 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 138 | SkOSFile::Iter::Iter() { new (fSelf.get()) SkOSFileIterData; } |
bungeman | e998b7f | 2015-02-12 07:18:27 -0800 | [diff] [blame] | 139 | |
| 140 | SkOSFile::Iter::Iter(const char path[], const char suffix[]) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 141 | new (fSelf.get()) SkOSFileIterData; |
bungeman | e998b7f | 2015-02-12 07:18:27 -0800 | [diff] [blame] | 142 | this->reset(path, suffix); |
| 143 | } |
| 144 | |
| 145 | SkOSFile::Iter::~Iter() { |
| 146 | SkOSFileIterData& self = *static_cast<SkOSFileIterData*>(fSelf.get()); |
| 147 | if (self.fDIR) { |
| 148 | ::closedir(self.fDIR); |
| 149 | } |
| 150 | self.~SkOSFileIterData(); |
| 151 | } |
| 152 | |
| 153 | void 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 |
| 170 | static 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 | |
| 178 | bool SkOSFile::Iter::next(SkString* name, bool getDir) { |
| 179 | SkOSFileIterData& self = *static_cast<SkOSFileIterData*>(fSelf.get()); |
| 180 | if (self.fDIR) { |
| 181 | dirent* entry; |
| 182 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 183 | while ((entry = ::readdir(self.fDIR)) != nullptr) { |
bungeman | e998b7f | 2015-02-12 07:18:27 -0800 | [diff] [blame] | 184 | 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 | } |