blob: 96a29e1378777fa97b30e6f337f4014582c9f3c3 [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
17#include "FileBlobCache.h"
18
Dan Albertcbf81da2018-01-05 14:24:09 -080019#include <errno.h>
Tom Cherry3387cab2020-04-13 11:04:01 -070020#include <fcntl.h>
Stan Iliev9e7cd072017-10-09 15:56:10 -040021#include <inttypes.h>
Stan Iliev9e7cd072017-10-09 15:56:10 -040022#include <sys/mman.h>
23#include <sys/stat.h>
24
Tom Cherry3387cab2020-04-13 11:04:01 -070025#include <log/log.h>
Stan Iliev9e7cd072017-10-09 15:56:10 -040026
27// Cache file header
28static const char* cacheFileMagic = "EGL$";
29static const size_t cacheFileHeaderSize = 8;
30
31namespace android {
32
33static uint32_t crc32c(const uint8_t* buf, size_t len) {
34 const uint32_t polyBits = 0x82F63B78;
35 uint32_t r = 0;
36 for (size_t i = 0; i < len; i++) {
37 r ^= buf[i];
38 for (int j = 0; j < 8; j++) {
39 if (r & 1) {
40 r = (r >> 1) ^ polyBits;
41 } else {
42 r >>= 1;
43 }
44 }
45 }
46 return r;
47}
48
49FileBlobCache::FileBlobCache(size_t maxKeySize, size_t maxValueSize, size_t maxTotalSize,
50 const std::string& filename)
51 : BlobCache(maxKeySize, maxValueSize, maxTotalSize)
52 , mFilename(filename) {
53 if (mFilename.length() > 0) {
54 size_t headerSize = cacheFileHeaderSize;
55
56 int fd = open(mFilename.c_str(), O_RDONLY, 0);
57 if (fd == -1) {
58 if (errno != ENOENT) {
59 ALOGE("error opening cache file %s: %s (%d)", mFilename.c_str(),
60 strerror(errno), errno);
61 }
62 return;
63 }
64
65 struct stat statBuf;
66 if (fstat(fd, &statBuf) == -1) {
67 ALOGE("error stat'ing cache file: %s (%d)", strerror(errno), errno);
68 close(fd);
69 return;
70 }
71
72 // Sanity check the size before trying to mmap it.
73 size_t fileSize = statBuf.st_size;
74 if (fileSize > mMaxTotalSize * 2) {
75 ALOGE("cache file is too large: %#" PRIx64,
76 static_cast<off64_t>(statBuf.st_size));
77 close(fd);
78 return;
79 }
80
Yi Kong48a6cd22018-07-18 10:07:09 -070081 uint8_t* buf = reinterpret_cast<uint8_t*>(mmap(nullptr, fileSize,
Stan Iliev9e7cd072017-10-09 15:56:10 -040082 PROT_READ, MAP_PRIVATE, fd, 0));
83 if (buf == MAP_FAILED) {
84 ALOGE("error mmaping cache file: %s (%d)", strerror(errno),
85 errno);
86 close(fd);
87 return;
88 }
89
90 // Check the file magic and CRC
91 size_t cacheSize = fileSize - headerSize;
92 if (memcmp(buf, cacheFileMagic, 4) != 0) {
93 ALOGE("cache file has bad mojo");
94 close(fd);
95 return;
96 }
97 uint32_t* crc = reinterpret_cast<uint32_t*>(buf + 4);
98 if (crc32c(buf + headerSize, cacheSize) != *crc) {
99 ALOGE("cache file failed CRC check");
100 close(fd);
101 return;
102 }
103
104 int err = unflatten(buf + headerSize, cacheSize);
105 if (err < 0) {
106 ALOGE("error reading cache contents: %s (%d)", strerror(-err),
107 -err);
108 munmap(buf, fileSize);
109 close(fd);
110 return;
111 }
112
113 munmap(buf, fileSize);
114 close(fd);
115 }
116}
117
118void FileBlobCache::writeToFile() {
119 if (mFilename.length() > 0) {
120 size_t cacheSize = getFlattenedSize();
121 size_t headerSize = cacheFileHeaderSize;
122 const char* fname = mFilename.c_str();
123
124 // Try to create the file with no permissions so we can write it
125 // without anyone trying to read it.
126 int fd = open(fname, O_CREAT | O_EXCL | O_RDWR, 0);
127 if (fd == -1) {
128 if (errno == EEXIST) {
129 // The file exists, delete it and try again.
130 if (unlink(fname) == -1) {
131 // No point in retrying if the unlink failed.
132 ALOGE("error unlinking cache file %s: %s (%d)", fname,
133 strerror(errno), errno);
134 return;
135 }
136 // Retry now that we've unlinked the file.
137 fd = open(fname, O_CREAT | O_EXCL | O_RDWR, 0);
138 }
139 if (fd == -1) {
140 ALOGE("error creating cache file %s: %s (%d)", fname,
141 strerror(errno), errno);
142 return;
143 }
144 }
145
146 size_t fileSize = headerSize + cacheSize;
147
148 uint8_t* buf = new uint8_t [fileSize];
149 if (!buf) {
150 ALOGE("error allocating buffer for cache contents: %s (%d)",
151 strerror(errno), errno);
152 close(fd);
153 unlink(fname);
154 return;
155 }
156
157 int err = flatten(buf + headerSize, cacheSize);
158 if (err < 0) {
159 ALOGE("error writing cache contents: %s (%d)", strerror(-err),
160 -err);
161 delete [] buf;
162 close(fd);
163 unlink(fname);
164 return;
165 }
166
167 // Write the file magic and CRC
168 memcpy(buf, cacheFileMagic, 4);
169 uint32_t* crc = reinterpret_cast<uint32_t*>(buf + 4);
170 *crc = crc32c(buf + headerSize, cacheSize);
171
172 if (write(fd, buf, fileSize) == -1) {
173 ALOGE("error writing cache file: %s (%d)", strerror(errno),
174 errno);
175 delete [] buf;
176 close(fd);
177 unlink(fname);
178 return;
179 }
180
181 delete [] buf;
182 fchmod(fd, S_IRUSR);
183 close(fd);
184 }
185}
186
Dan Albertcbf81da2018-01-05 14:24:09 -0800187}