Add logging for comparison behaviors
[hiphop-php.git] / hphp / util / file-cache.cpp
blobfd9e112b2b60885bf086e27e10ff3d9f4db59896
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
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"
19 #include <set>
20 #include <string>
22 #include "hphp/util/cache/cache-manager.h"
23 #include "hphp/util/exception.h"
24 #include "hphp/util/logger.h"
26 namespace HPHP {
28 using std::set;
29 using std::string;
31 string FileCache::SourceRoot;
32 bool FileCache::UseNewCache = true;
34 FileCache::FileCache()
35 : cache_manager_(new CacheManager) {
36 UseNewCache = true;
39 FileCache::~FileCache() {}
41 string FileCache::GetRelativePath(const char *path) {
42 assert(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);
53 return relative;
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 {
93 if (isRelative) {
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 {
104 if (isRelative) {
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 {
113 if (isRelative) {
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) {
122 return nullptr;
125 const char* data;
126 uint64_t data_len;
127 bool data_compressed;
129 if (!cache_manager_->getFileContents(name, &data, &data_len,
130 &data_compressed)) {
131 return nullptr;
134 compressed = data_compressed;
135 len = data_len;
137 // Yep, throwing away const here (for now) for API compatibility.
138 return (char*) data;
141 int64_t FileCache::fileSize(const char *name, bool isRelative) const {
142 if (!name || !*name) {
143 return -1;
146 if (isRelative) {
147 uint64_t size;
148 if (!cache_manager_->getUncompressedFileSize(name, &size)) {
149 return -1;
152 return size;
155 return fileSize(GetRelativePath(name).c_str(), true);
158 void FileCache::dump() const {
159 cache_manager_->dump();
162 } // namespace HPHP