blob: 573ca5411adfdd35951f3e5ceccb384da740037c [file] [log] [blame]
Stan Iliev9e7cd072017-10-09 15:56:10 -04001/*
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 Zhangbcbfe602023-04-22 04:43:33 +000017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
Stan Iliev9e7cd072017-10-09 15:56:10 -040019#include "FileBlobCache.h"
20
Dan Albertcbf81da2018-01-05 14:24:09 -080021#include <errno.h>
Tom Cherry3387cab2020-04-13 11:04:01 -070022#include <fcntl.h>
Stan Iliev9e7cd072017-10-09 15:56:10 -040023#include <inttypes.h>
Stan Iliev9e7cd072017-10-09 15:56:10 -040024#include <sys/mman.h>
25#include <sys/stat.h>
Tom Cherry28ac3b92020-04-13 15:19:23 -070026#include <unistd.h>
Stan Iliev9e7cd072017-10-09 15:56:10 -040027
Tom Cherry3387cab2020-04-13 11:04:01 -070028#include <log/log.h>
Yiwei Zhangbcbfe602023-04-22 04:43:33 +000029#include <utils/Trace.h>
Jisun Lee88a1b762024-10-17 20:12:29 +090030#include <zlib.h>
Stan Iliev9e7cd072017-10-09 15:56:10 -040031
32// Cache file header
33static const char* cacheFileMagic = "EGL$";
34static const size_t cacheFileHeaderSize = 8;
35
36namespace android {
37
Jisun Lee88a1b762024-10-17 20:12:29 +090038uint32_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 Iliev9e7cd072017-10-09 15:56:10 -040042}
43
44FileBlobCache::FileBlobCache(size_t maxKeySize, size_t maxValueSize, size_t maxTotalSize,
45 const std::string& filename)
46 : BlobCache(maxKeySize, maxValueSize, maxTotalSize)
47 , mFilename(filename) {
Yiwei Zhangbcbfe602023-04-22 04:43:33 +000048 ATRACE_CALL();
49
Stan Iliev9e7cd072017-10-09 15:56:10 -040050 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 Zhang26169cd2020-07-28 15:46:12 -070069 // Check the size before trying to mmap it.
Stan Iliev9e7cd072017-10-09 15:56:10 -040070 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 Kong48a6cd22018-07-18 10:07:09 -070078 uint8_t* buf = reinterpret_cast<uint8_t*>(mmap(nullptr, fileSize,
Stan Iliev9e7cd072017-10-09 15:56:10 -040079 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 Lee88a1b762024-10-17 20:12:29 +090095 if (GenerateCRC32(buf + headerSize, cacheSize) != *crc) {
Stan Iliev9e7cd072017-10-09 15:56:10 -040096 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
115void FileBlobCache::writeToFile() {
Yiwei Zhangbcbfe602023-04-22 04:43:33 +0000116 ATRACE_CALL();
117
Stan Iliev9e7cd072017-10-09 15:56:10 -0400118 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 Lee88a1b762024-10-17 20:12:29 +0900169 *crc = GenerateCRC32(buf + headerSize, cacheSize);
Stan Iliev9e7cd072017-10-09 15:56:10 -0400170
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 Northrop2c9085b2022-12-12 11:35:54 -0700186size_t FileBlobCache::getSize() {
187 if (mFilename.length() > 0) {
188 return getFlattenedSize() + cacheFileHeaderSize;
189 }
190 return 0;
191}
Dan Albertcbf81da2018-01-05 14:24:09 -0800192}