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" |
| 9 | |
bungeman@google.com | f5cc5b1 | 2013-07-12 18:22:49 +0000 | [diff] [blame] | 10 | #include "SkTFitsIn.h" |
bungeman | e998b7f | 2015-02-12 07:18:27 -0800 | [diff] [blame^] | 11 | #include "SkTypes.h" |
bungeman@google.com | 11c9a55 | 2013-06-03 17:10:35 +0000 | [diff] [blame] | 12 | |
bungeman | e998b7f | 2015-02-12 07:18:27 -0800 | [diff] [blame^] | 13 | #include <dirent.h> |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 14 | #include <stdio.h> |
| 15 | #include <sys/mman.h> |
| 16 | #include <sys/stat.h> |
| 17 | #include <sys/types.h> |
bungeman | bf0b9ce | 2014-07-10 15:18:23 -0700 | [diff] [blame] | 18 | #include <unistd.h> |
| 19 | |
| 20 | bool sk_exists(const char *path, SkFILE_Flags flags) { |
| 21 | int mode = F_OK; |
| 22 | if (flags & kRead_SkFILE_Flag) { |
| 23 | mode |= R_OK; |
| 24 | } |
| 25 | if (flags & kWrite_SkFILE_Flag) { |
| 26 | mode |= W_OK; |
| 27 | } |
| 28 | return (0 == access(path, mode)); |
| 29 | } |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 30 | |
| 31 | typedef struct { |
| 32 | dev_t dev; |
| 33 | ino_t ino; |
| 34 | } SkFILEID; |
| 35 | |
| 36 | static bool sk_ino(SkFILE* a, SkFILEID* id) { |
| 37 | int fd = fileno((FILE*)a); |
| 38 | if (fd < 0) { |
| 39 | return 0; |
| 40 | } |
| 41 | struct stat status; |
| 42 | if (0 != fstat(fd, &status)) { |
| 43 | return 0; |
| 44 | } |
| 45 | id->dev = status.st_dev; |
| 46 | id->ino = status.st_ino; |
| 47 | return true; |
| 48 | } |
| 49 | |
| 50 | bool sk_fidentical(SkFILE* a, SkFILE* b) { |
| 51 | SkFILEID aID, bID; |
| 52 | return sk_ino(a, &aID) && sk_ino(b, &bID) |
| 53 | && aID.ino == bID.ino |
| 54 | && aID.dev == bID.dev; |
| 55 | } |
| 56 | |
| 57 | void sk_fmunmap(const void* addr, size_t length) { |
| 58 | munmap(const_cast<void*>(addr), length); |
| 59 | } |
| 60 | |
bungeman@google.com | 11c9a55 | 2013-06-03 17:10:35 +0000 | [diff] [blame] | 61 | void* sk_fdmmap(int fd, size_t* size) { |
| 62 | struct stat status; |
| 63 | if (0 != fstat(fd, &status)) { |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 64 | return NULL; |
| 65 | } |
bungeman@google.com | 11c9a55 | 2013-06-03 17:10:35 +0000 | [diff] [blame] | 66 | if (!S_ISREG(status.st_mode)) { |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 67 | return NULL; |
| 68 | } |
bungeman@google.com | 11c9a55 | 2013-06-03 17:10:35 +0000 | [diff] [blame] | 69 | if (!SkTFitsIn<size_t>(status.st_size)) { |
| 70 | return NULL; |
| 71 | } |
| 72 | size_t fileSize = static_cast<size_t>(status.st_size); |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 73 | |
| 74 | void* addr = mmap(NULL, fileSize, PROT_READ, MAP_PRIVATE, fd, 0); |
| 75 | if (MAP_FAILED == addr) { |
| 76 | return NULL; |
| 77 | } |
| 78 | |
| 79 | *size = fileSize; |
| 80 | return addr; |
| 81 | } |
bungeman@google.com | 11c9a55 | 2013-06-03 17:10:35 +0000 | [diff] [blame] | 82 | |
| 83 | int sk_fileno(SkFILE* f) { |
| 84 | return fileno((FILE*)f); |
| 85 | } |
| 86 | |
| 87 | void* sk_fmmap(SkFILE* f, size_t* size) { |
| 88 | int fd = sk_fileno(f); |
| 89 | if (fd < 0) { |
| 90 | return NULL; |
| 91 | } |
skia.committer@gmail.com | 11f2b44 | 2013-06-04 07:00:53 +0000 | [diff] [blame] | 92 | |
bungeman@google.com | 11c9a55 | 2013-06-03 17:10:35 +0000 | [diff] [blame] | 93 | return sk_fdmmap(fd, size); |
| 94 | } |
bungeman | e998b7f | 2015-02-12 07:18:27 -0800 | [diff] [blame^] | 95 | |
| 96 | //////////////////////////////////////////////////////////////////////////// |
| 97 | |
| 98 | struct SkOSFileIterData { |
| 99 | SkOSFileIterData() : fDIR(0) { } |
| 100 | DIR* fDIR; |
| 101 | SkString fPath, fSuffix; |
| 102 | }; |
| 103 | SK_COMPILE_ASSERT(sizeof(SkOSFileIterData) <= SkOSFile::Iter::kStorageSize, not_enough_space); |
| 104 | |
| 105 | SkOSFile::Iter::Iter() { |
| 106 | SkNEW_PLACEMENT(fSelf.get(), SkOSFileIterData); |
| 107 | } |
| 108 | |
| 109 | SkOSFile::Iter::Iter(const char path[], const char suffix[]) { |
| 110 | SkNEW_PLACEMENT(fSelf.get(), SkOSFileIterData); |
| 111 | this->reset(path, suffix); |
| 112 | } |
| 113 | |
| 114 | SkOSFile::Iter::~Iter() { |
| 115 | SkOSFileIterData& self = *static_cast<SkOSFileIterData*>(fSelf.get()); |
| 116 | if (self.fDIR) { |
| 117 | ::closedir(self.fDIR); |
| 118 | } |
| 119 | self.~SkOSFileIterData(); |
| 120 | } |
| 121 | |
| 122 | void SkOSFile::Iter::reset(const char path[], const char suffix[]) { |
| 123 | SkOSFileIterData& self = *static_cast<SkOSFileIterData*>(fSelf.get()); |
| 124 | if (self.fDIR) { |
| 125 | ::closedir(self.fDIR); |
| 126 | self.fDIR = 0; |
| 127 | } |
| 128 | |
| 129 | self.fPath.set(path); |
| 130 | if (path) { |
| 131 | self.fDIR = ::opendir(path); |
| 132 | self.fSuffix.set(suffix); |
| 133 | } else { |
| 134 | self.fSuffix.reset(); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | // returns true if suffix is empty, or if str ends with suffix |
| 139 | static bool issuffixfor(const SkString& suffix, const char str[]) { |
| 140 | size_t suffixLen = suffix.size(); |
| 141 | size_t strLen = strlen(str); |
| 142 | |
| 143 | return strLen >= suffixLen && |
| 144 | memcmp(suffix.c_str(), str + strLen - suffixLen, suffixLen) == 0; |
| 145 | } |
| 146 | |
| 147 | bool SkOSFile::Iter::next(SkString* name, bool getDir) { |
| 148 | SkOSFileIterData& self = *static_cast<SkOSFileIterData*>(fSelf.get()); |
| 149 | if (self.fDIR) { |
| 150 | dirent* entry; |
| 151 | |
| 152 | while ((entry = ::readdir(self.fDIR)) != NULL) { |
| 153 | struct stat s; |
| 154 | SkString str(self.fPath); |
| 155 | |
| 156 | if (!str.endsWith("/") && !str.endsWith("\\")) { |
| 157 | str.append("/"); |
| 158 | } |
| 159 | str.append(entry->d_name); |
| 160 | |
| 161 | if (0 == stat(str.c_str(), &s)) { |
| 162 | if (getDir) { |
| 163 | if (s.st_mode & S_IFDIR) { |
| 164 | break; |
| 165 | } |
| 166 | } else { |
| 167 | if (!(s.st_mode & S_IFDIR) && issuffixfor(self.fSuffix, entry->d_name)) { |
| 168 | break; |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | if (entry) { // we broke out with a file |
| 174 | if (name) { |
| 175 | name->set(entry->d_name); |
| 176 | } |
| 177 | return true; |
| 178 | } |
| 179 | } |
| 180 | return false; |
| 181 | } |