2 +----------------------------------------------------------------------+
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-present Facebook, Inc. (http://www.facebook.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
17 #include "hphp/util/file-cache.h"
22 #include "hphp/util/cache/cache-manager.h"
23 #include "hphp/util/exception.h"
24 #include "hphp/util/logger.h"
31 string
FileCache::SourceRoot
;
32 bool FileCache::UseNewCache
= true;
34 FileCache::FileCache()
35 : cache_manager_(new CacheManager
) {
39 FileCache::~FileCache() {}
41 string
FileCache::GetRelativePath(const char *path
) {
44 string relative
= path
;
45 unsigned int len
= SourceRoot
.size();
46 if (len
> 0 && relative
.size() > len
&&
47 strncmp(relative
.data(), SourceRoot
.c_str(), len
) == 0) {
48 relative
= relative
.substr(len
);
50 if (!relative
.empty() && relative
[relative
.length() - 1] == '/') {
51 relative
= relative
.substr(0, relative
.length() - 1);
56 void FileCache::write(const char *name
) {
57 assert(name
&& *name
);
58 assert(!exists(name
));
60 if (!cache_manager_
->addEmptyEntry(name
)) {
61 throw Exception("Unable to add entry for %s", name
);
65 void FileCache::write(const char *name
, const char *fullpath
) {
66 assert(name
&& *name
);
67 assert(fullpath
&& *fullpath
);
68 assert(!exists(name
));
70 if (!cache_manager_
->addFileContents(name
, fullpath
)) {
71 throw Exception("Unable to add entry for %s (%s)", name
, fullpath
);
75 void FileCache::save(const char *filename
) {
76 assert(filename
&& *filename
);
78 if (!cache_manager_
->saveCache(filename
)) {
79 throw Exception("Unable to save cache to %s", filename
);
83 void FileCache::loadMmap(const char *filename
) {
84 assert(filename
&& *filename
);
86 if (!cache_manager_
->loadCache(filename
)) {
87 throw Exception("Unable to load cache from %s", filename
);
91 bool FileCache::fileExists(const char *name
,
92 bool isRelative
/* = true */) const {
94 // Original cache behavior: an empty entry is also a "file".
95 return cache_manager_
->fileExists(name
) ||
96 cache_manager_
->emptyEntryExists(name
);
99 return fileExists(GetRelativePath(name
).c_str());
102 bool FileCache::dirExists(const char *name
,
103 bool isRelative
/* = true */) const {
105 return cache_manager_
->dirExists(name
);
108 return dirExists(GetRelativePath(name
).c_str());
111 bool FileCache::exists(const char *name
,
112 bool isRelative
/* = true */) const {
114 return cache_manager_
->entryExists(name
);
117 return exists(GetRelativePath(name
).c_str());
120 char *FileCache::read(const char *name
, int &len
, bool &compressed
) const {
121 if (!name
|| !*name
) {
127 bool data_compressed
;
129 if (!cache_manager_
->getFileContents(name
, &data
, &data_len
,
134 compressed
= data_compressed
;
137 // Yep, throwing away const here (for now) for API compatibility.
141 int64_t FileCache::fileSize(const char *name
, bool isRelative
) const {
142 if (!name
|| !*name
) {
148 if (!cache_manager_
->getUncompressedFileSize(name
, &size
)) {
155 return fileSize(GetRelativePath(name
).c_str(), true);
158 void FileCache::dump() const {
159 cache_manager_
->dump();