Automatic forward-decl fixup
[hiphop-php.git] / hphp / util / cache / cache-data.h
blob6bb2c98768d3029a4d0c79895f8089b3884c7db3
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-2016 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 // Individual in-memory component of the cache.
18 // Usually normal files (web static resources).
20 #ifndef incl_HPHP_CACHE_DATA_H_
21 #define incl_HPHP_CACHE_DATA_H_
23 #include <cstdint>
24 #include <string>
26 namespace HPHP {
28 struct CacheSaver;
29 struct MmapFile;
31 struct CacheData {
32 CacheData();
33 ~CacheData();
35 CacheData(const CacheData&) = delete;
36 CacheData& operator=(const CacheData&) = delete;
38 // --- Creation functions: call at most one of these per instance.
40 // Create a named entry with contents of regular file at <path>.
41 bool loadFromFile(const std::string& name, uint64_t id,
42 const std::string& path);
44 // Create a named entry without any contents.
45 void createEmpty(const std::string& name, uint64_t id);
47 // Create a named directory entry.
48 void createDirectory(const std::string& name, uint64_t id);
50 // Point into an existing cache file on disk. Populates name on success.
51 bool loadFromMmap(MmapFile* mmap_file, std::string* name);
53 // --- End creation functions.
55 // Push the internal data for this instance to CacheSaver for serialization.
56 bool save(CacheSaver* cs) const;
58 bool isRegularFile() const;
59 bool isDirectory() const;
60 bool isCompressed() const;
61 bool isEmpty() const;
63 uint64_t fileSize() const;
65 // Access contents if possible. Populates all three arguments on success.
66 bool getDataPointer(const char** data, uint64_t* datalen,
67 bool* compressed) const;
69 // Make a decompressed copy of the data. Only works if actually compressed.
70 // Returns false if data is not compressed, or on any other error.
71 bool getDecompressedData(std::string* data) const;
73 void dump() const;
75 private:
76 static const int kFlag_Compressed = 0x00000001;
78 static const int kFlag_RegularFile = 0x00010000;
79 static const int kFlag_EmptyEntry = 0x00020000;
80 static const int kFlag_Directory = 0x00040000;
82 uint64_t createChecksum() const;
83 bool sufficientlyCompressed(uint64_t orig_size, uint64_t new_size) const;
85 uint64_t id_;
86 uint64_t flags_;
87 uint64_t mtime_;
88 uint64_t checksum_;
90 // It might be something we malloced, or something we got via mmap...
91 const char* file_data_;
92 uint64_t file_data_length_;
94 // ... hence this bool.
95 bool should_free_;
97 std::string name_;
100 } // namespace HPHP
102 #endif // incl_HPHP_CACHE_DATA_H_