]>
Commit | Line | Data |
---|---|---|
58373ff8 | 1 | /* |
bf95c10a | 2 | * Copyright (C) 1996-2022 The Squid Software Foundation and contributors |
187642d0 | 3 | * |
bbc27441 AJ |
4 | * Squid software is distributed under GPLv2+ license and includes |
5 | * contributions from numerous individuals and organizations. | |
6 | * Please see the COPYING and CONTRIBUTORS files for details. | |
58373ff8 FC |
7 | */ |
8 | ||
9 | #include "squid.h" | |
675b8408 | 10 | #include "debug/Stream.h" |
58373ff8 FC |
11 | #include "md5.h" |
12 | #include "StoreSwapLogData.h" | |
13 | #include "swap_log_op.h" | |
14 | #include "UFSSwapLogParser.h" | |
15 | ||
582c2af2 FC |
16 | #if HAVE_SYS_STAT_H |
17 | #include <sys/stat.h> | |
18 | #endif | |
19 | ||
58373ff8 FC |
20 | /// Parse a swap header entry created on a system with 32-bit size_t and sfileno |
21 | /// this is typical of 32-bit systems without large file support | |
22 | /// NP: SQUID_MD5_DIGEST_LENGTH is very risky still. | |
23 | class UFSSwapLogParser_v1_32bs:public Fs::Ufs::UFSSwapLogParser | |
24 | { | |
25 | public: | |
26 | /// version 1 cache swap.state entry with 32-bit size_t (swap_file_sz) | |
27 | /// time_t an sfileno have no variation from the v1 baseline format | |
28 | struct StoreSwapLogDataOld { | |
29 | char op; | |
30 | sfileno swap_filen; | |
31 | time_t timestamp; | |
32 | time_t lastref; | |
33 | time_t expires; | |
34 | time_t lastmod; | |
35 | uint32_t swap_file_sz; | |
36 | uint16_t refcount; | |
37 | uint16_t flags; | |
38 | unsigned char key[SQUID_MD5_DIGEST_LENGTH]; | |
39 | }; | |
40 | UFSSwapLogParser_v1_32bs(FILE *fp):Fs::Ufs::UFSSwapLogParser(fp) { | |
41 | record_size = sizeof(UFSSwapLogParser_v1_32bs::StoreSwapLogDataOld); | |
42 | } | |
43 | /// Convert the on-disk 32-bit format to our current format while reading | |
44 | bool ReadRecord(StoreSwapLogData &swapData) { | |
45 | UFSSwapLogParser_v1_32bs::StoreSwapLogDataOld readData; | |
46 | int bytes = sizeof(UFSSwapLogParser_v1_32bs::StoreSwapLogDataOld); | |
47 | ||
48 | assert(log); | |
49 | ||
50 | if (fread(&readData, bytes, 1, log) != 1) { | |
51 | return false; | |
52 | } | |
53 | swapData.op = readData.op; | |
54 | swapData.swap_filen = readData.swap_filen; | |
55 | swapData.timestamp = readData.timestamp; | |
56 | swapData.lastref = readData.lastref; | |
57 | swapData.expires = readData.expires; | |
58 | swapData.lastmod = readData.lastmod; | |
59 | swapData.swap_file_sz = readData.swap_file_sz; | |
60 | swapData.refcount = readData.refcount; | |
61 | swapData.flags = readData.flags; | |
62 | memcpy(swapData.key, readData.key, SQUID_MD5_DIGEST_LENGTH); | |
63 | return true; | |
64 | } | |
65 | }; | |
66 | ||
67 | /// swap.state v2 log parser | |
68 | class UFSSwapLogParser_v2: public Fs::Ufs::UFSSwapLogParser | |
69 | { | |
70 | public: | |
71 | UFSSwapLogParser_v2(FILE *fp): Fs::Ufs::UFSSwapLogParser(fp) { | |
72 | record_size = sizeof(StoreSwapLogData); | |
73 | } | |
74 | bool ReadRecord(StoreSwapLogData &swapData) { | |
75 | assert(log); | |
76 | return fread(&swapData, sizeof(StoreSwapLogData), 1, log) == 1; | |
77 | } | |
78 | }; | |
79 | ||
80 | Fs::Ufs::UFSSwapLogParser * | |
81 | Fs::Ufs::UFSSwapLogParser::GetUFSSwapLogParser(FILE *fp) | |
82 | { | |
83 | StoreSwapLogHeader header; | |
84 | ||
85 | assert(fp); | |
86 | ||
87 | if (fread(&header, sizeof(StoreSwapLogHeader), 1, fp) != 1) | |
aee3523a | 88 | return nullptr; |
58373ff8 FC |
89 | |
90 | if (header.op != SWAP_LOG_VERSION) { | |
91 | debugs(47, DBG_IMPORTANT, "Old swap file detected..."); | |
92 | fseek(fp, 0, SEEK_SET); | |
93 | return new UFSSwapLogParser_v1_32bs(fp); // Um. 32-bits except time_t, and can't determine that. | |
94 | } | |
95 | ||
96 | debugs(47, 2, "Swap file version: " << header.version); | |
97 | ||
98 | if (header.version == 1) { | |
99 | if (fseek(fp, header.record_size, SEEK_SET) != 0) | |
aee3523a | 100 | return nullptr; |
58373ff8 | 101 | |
d816f28d | 102 | debugs(47, DBG_IMPORTANT, "ERROR: Rejecting swap file v1 to avoid cache " << |
58373ff8 FC |
103 | "index corruption. Forcing a full cache index rebuild. " << |
104 | "See Squid bug #3441."); | |
aee3523a | 105 | return nullptr; |
58373ff8 FC |
106 | } |
107 | ||
108 | if (header.version >= 2) { | |
109 | if (!header.sane()) { | |
110 | debugs(47, DBG_IMPORTANT, "ERROR: Corrupted v" << header.version << | |
111 | " swap file header."); | |
aee3523a | 112 | return nullptr; |
58373ff8 FC |
113 | } |
114 | ||
115 | if (fseek(fp, header.record_size, SEEK_SET) != 0) | |
aee3523a | 116 | return nullptr; |
58373ff8 FC |
117 | |
118 | if (header.version == 2) | |
119 | return new UFSSwapLogParser_v2(fp); | |
120 | } | |
121 | ||
122 | // TODO: v3: write to disk in network-order bytes for the larger fields? | |
123 | ||
d816f28d | 124 | debugs(47, DBG_IMPORTANT, "ERROR: Unknown swap file version: " << header.version); |
aee3523a | 125 | return nullptr; |
58373ff8 FC |
126 | } |
127 | ||
128 | int | |
129 | Fs::Ufs::UFSSwapLogParser::SwapLogEntries() | |
130 | { | |
131 | struct stat sb; | |
132 | ||
133 | if (log_entries >= 0) | |
134 | return log_entries; | |
135 | ||
136 | if (log && record_size && 0 == fstat(fileno(log), &sb)) { | |
137 | log_entries = sb.st_size/record_size; | |
138 | return log_entries; | |
139 | } | |
140 | ||
141 | return 0; | |
142 | } | |
f53969cc | 143 |