Stan Iliev | 9e7cd07 | 2017-10-09 15:56:10 -0400 | [diff] [blame] | 1 | /* |
| 2 | ** Copyright 2017, The Android Open Source Project |
| 3 | ** |
| 4 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | ** you may not use this file except in compliance with the License. |
| 6 | ** You may obtain a copy of the License at |
| 7 | ** |
| 8 | ** http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | ** |
| 10 | ** Unless required by applicable law or agreed to in writing, software |
| 11 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | ** See the License for the specific language governing permissions and |
| 14 | ** limitations under the License. |
| 15 | */ |
| 16 | |
Yiwei Zhang | bcbfe60 | 2023-04-22 04:43:33 +0000 | [diff] [blame] | 17 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 18 | |
Stan Iliev | 9e7cd07 | 2017-10-09 15:56:10 -0400 | [diff] [blame] | 19 | #include "FileBlobCache.h" |
| 20 | |
Dan Albert | cbf81da | 2018-01-05 14:24:09 -0800 | [diff] [blame] | 21 | #include <errno.h> |
Tom Cherry | 3387cab | 2020-04-13 11:04:01 -0700 | [diff] [blame] | 22 | #include <fcntl.h> |
Stan Iliev | 9e7cd07 | 2017-10-09 15:56:10 -0400 | [diff] [blame] | 23 | #include <inttypes.h> |
Stan Iliev | 9e7cd07 | 2017-10-09 15:56:10 -0400 | [diff] [blame] | 24 | #include <sys/mman.h> |
| 25 | #include <sys/stat.h> |
Tom Cherry | 28ac3b9 | 2020-04-13 15:19:23 -0700 | [diff] [blame] | 26 | #include <unistd.h> |
Stan Iliev | 9e7cd07 | 2017-10-09 15:56:10 -0400 | [diff] [blame] | 27 | |
Tom Cherry | 3387cab | 2020-04-13 11:04:01 -0700 | [diff] [blame] | 28 | #include <log/log.h> |
Yiwei Zhang | bcbfe60 | 2023-04-22 04:43:33 +0000 | [diff] [blame] | 29 | #include <utils/Trace.h> |
Jisun Lee | 88a1b76 | 2024-10-17 20:12:29 +0900 | [diff] [blame^] | 30 | #include <zlib.h> |
Stan Iliev | 9e7cd07 | 2017-10-09 15:56:10 -0400 | [diff] [blame] | 31 | |
| 32 | // Cache file header |
| 33 | static const char* cacheFileMagic = "EGL$"; |
| 34 | static const size_t cacheFileHeaderSize = 8; |
| 35 | |
| 36 | namespace android { |
| 37 | |
Jisun Lee | 88a1b76 | 2024-10-17 20:12:29 +0900 | [diff] [blame^] | 38 | uint32_t GenerateCRC32(const uint8_t *data, size_t size) |
| 39 | { |
| 40 | const unsigned long initialValue = crc32_z(0u, nullptr, 0u); |
| 41 | return static_cast<uint32_t>(crc32_z(initialValue, data, size)); |
Stan Iliev | 9e7cd07 | 2017-10-09 15:56:10 -0400 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | FileBlobCache::FileBlobCache(size_t maxKeySize, size_t maxValueSize, size_t maxTotalSize, |
| 45 | const std::string& filename) |
| 46 | : BlobCache(maxKeySize, maxValueSize, maxTotalSize) |
| 47 | , mFilename(filename) { |
Yiwei Zhang | bcbfe60 | 2023-04-22 04:43:33 +0000 | [diff] [blame] | 48 | ATRACE_CALL(); |
| 49 | |
Stan Iliev | 9e7cd07 | 2017-10-09 15:56:10 -0400 | [diff] [blame] | 50 | if (mFilename.length() > 0) { |
| 51 | size_t headerSize = cacheFileHeaderSize; |
| 52 | |
| 53 | int fd = open(mFilename.c_str(), O_RDONLY, 0); |
| 54 | if (fd == -1) { |
| 55 | if (errno != ENOENT) { |
| 56 | ALOGE("error opening cache file %s: %s (%d)", mFilename.c_str(), |
| 57 | strerror(errno), errno); |
| 58 | } |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | struct stat statBuf; |
| 63 | if (fstat(fd, &statBuf) == -1) { |
| 64 | ALOGE("error stat'ing cache file: %s (%d)", strerror(errno), errno); |
| 65 | close(fd); |
| 66 | return; |
| 67 | } |
| 68 | |
Yiwei Zhang | 26169cd | 2020-07-28 15:46:12 -0700 | [diff] [blame] | 69 | // Check the size before trying to mmap it. |
Stan Iliev | 9e7cd07 | 2017-10-09 15:56:10 -0400 | [diff] [blame] | 70 | size_t fileSize = statBuf.st_size; |
| 71 | if (fileSize > mMaxTotalSize * 2) { |
| 72 | ALOGE("cache file is too large: %#" PRIx64, |
| 73 | static_cast<off64_t>(statBuf.st_size)); |
| 74 | close(fd); |
| 75 | return; |
| 76 | } |
| 77 | |
Yi Kong | 48a6cd2 | 2018-07-18 10:07:09 -0700 | [diff] [blame] | 78 | uint8_t* buf = reinterpret_cast<uint8_t*>(mmap(nullptr, fileSize, |
Stan Iliev | 9e7cd07 | 2017-10-09 15:56:10 -0400 | [diff] [blame] | 79 | PROT_READ, MAP_PRIVATE, fd, 0)); |
| 80 | if (buf == MAP_FAILED) { |
| 81 | ALOGE("error mmaping cache file: %s (%d)", strerror(errno), |
| 82 | errno); |
| 83 | close(fd); |
| 84 | return; |
| 85 | } |
| 86 | |
| 87 | // Check the file magic and CRC |
| 88 | size_t cacheSize = fileSize - headerSize; |
| 89 | if (memcmp(buf, cacheFileMagic, 4) != 0) { |
| 90 | ALOGE("cache file has bad mojo"); |
| 91 | close(fd); |
| 92 | return; |
| 93 | } |
| 94 | uint32_t* crc = reinterpret_cast<uint32_t*>(buf + 4); |
Jisun Lee | 88a1b76 | 2024-10-17 20:12:29 +0900 | [diff] [blame^] | 95 | if (GenerateCRC32(buf + headerSize, cacheSize) != *crc) { |
Stan Iliev | 9e7cd07 | 2017-10-09 15:56:10 -0400 | [diff] [blame] | 96 | ALOGE("cache file failed CRC check"); |
| 97 | close(fd); |
| 98 | return; |
| 99 | } |
| 100 | |
| 101 | int err = unflatten(buf + headerSize, cacheSize); |
| 102 | if (err < 0) { |
| 103 | ALOGE("error reading cache contents: %s (%d)", strerror(-err), |
| 104 | -err); |
| 105 | munmap(buf, fileSize); |
| 106 | close(fd); |
| 107 | return; |
| 108 | } |
| 109 | |
| 110 | munmap(buf, fileSize); |
| 111 | close(fd); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | void FileBlobCache::writeToFile() { |
Yiwei Zhang | bcbfe60 | 2023-04-22 04:43:33 +0000 | [diff] [blame] | 116 | ATRACE_CALL(); |
| 117 | |
Stan Iliev | 9e7cd07 | 2017-10-09 15:56:10 -0400 | [diff] [blame] | 118 | if (mFilename.length() > 0) { |
| 119 | size_t cacheSize = getFlattenedSize(); |
| 120 | size_t headerSize = cacheFileHeaderSize; |
| 121 | const char* fname = mFilename.c_str(); |
| 122 | |
| 123 | // Try to create the file with no permissions so we can write it |
| 124 | // without anyone trying to read it. |
| 125 | int fd = open(fname, O_CREAT | O_EXCL | O_RDWR, 0); |
| 126 | if (fd == -1) { |
| 127 | if (errno == EEXIST) { |
| 128 | // The file exists, delete it and try again. |
| 129 | if (unlink(fname) == -1) { |
| 130 | // No point in retrying if the unlink failed. |
| 131 | ALOGE("error unlinking cache file %s: %s (%d)", fname, |
| 132 | strerror(errno), errno); |
| 133 | return; |
| 134 | } |
| 135 | // Retry now that we've unlinked the file. |
| 136 | fd = open(fname, O_CREAT | O_EXCL | O_RDWR, 0); |
| 137 | } |
| 138 | if (fd == -1) { |
| 139 | ALOGE("error creating cache file %s: %s (%d)", fname, |
| 140 | strerror(errno), errno); |
| 141 | return; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | size_t fileSize = headerSize + cacheSize; |
| 146 | |
| 147 | uint8_t* buf = new uint8_t [fileSize]; |
| 148 | if (!buf) { |
| 149 | ALOGE("error allocating buffer for cache contents: %s (%d)", |
| 150 | strerror(errno), errno); |
| 151 | close(fd); |
| 152 | unlink(fname); |
| 153 | return; |
| 154 | } |
| 155 | |
| 156 | int err = flatten(buf + headerSize, cacheSize); |
| 157 | if (err < 0) { |
| 158 | ALOGE("error writing cache contents: %s (%d)", strerror(-err), |
| 159 | -err); |
| 160 | delete [] buf; |
| 161 | close(fd); |
| 162 | unlink(fname); |
| 163 | return; |
| 164 | } |
| 165 | |
| 166 | // Write the file magic and CRC |
| 167 | memcpy(buf, cacheFileMagic, 4); |
| 168 | uint32_t* crc = reinterpret_cast<uint32_t*>(buf + 4); |
Jisun Lee | 88a1b76 | 2024-10-17 20:12:29 +0900 | [diff] [blame^] | 169 | *crc = GenerateCRC32(buf + headerSize, cacheSize); |
Stan Iliev | 9e7cd07 | 2017-10-09 15:56:10 -0400 | [diff] [blame] | 170 | |
| 171 | if (write(fd, buf, fileSize) == -1) { |
| 172 | ALOGE("error writing cache file: %s (%d)", strerror(errno), |
| 173 | errno); |
| 174 | delete [] buf; |
| 175 | close(fd); |
| 176 | unlink(fname); |
| 177 | return; |
| 178 | } |
| 179 | |
| 180 | delete [] buf; |
| 181 | fchmod(fd, S_IRUSR); |
| 182 | close(fd); |
| 183 | } |
| 184 | } |
| 185 | |
Cody Northrop | 2c9085b | 2022-12-12 11:35:54 -0700 | [diff] [blame] | 186 | size_t FileBlobCache::getSize() { |
| 187 | if (mFilename.length() > 0) { |
| 188 | return getFlattenedSize() + cacheFileHeaderSize; |
| 189 | } |
| 190 | return 0; |
| 191 | } |
Dan Albert | cbf81da | 2018-01-05 14:24:09 -0800 | [diff] [blame] | 192 | } |