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