UFSSwapLogParser.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2022 The Squid Software Foundation and contributors
3 *
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.
7 */
8
9#include "squid.h"
10#include "debug/Stream.h"
11#include "md5.h"
12#include "StoreSwapLogData.h"
13#include "swap_log_op.h"
14#include "UFSSwapLogParser.h"
15
16#if HAVE_SYS_STAT_H
17#include <sys/stat.h>
18#endif
19
24{
25public:
29 char op;
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;
39 };
42 }
44 bool ReadRecord(StoreSwapLogData &swapData) {
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
69{
70public:
73 }
74 bool ReadRecord(StoreSwapLogData &swapData) {
75 assert(log);
76 return fread(&swapData, sizeof(StoreSwapLogData), 1, log) == 1;
77 }
78};
79
82{
83 StoreSwapLogHeader header;
84
85 assert(fp);
86
87 if (fread(&header, sizeof(StoreSwapLogHeader), 1, fp) != 1)
88 return nullptr;
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)
100 return nullptr;
101
102 debugs(47, DBG_IMPORTANT, "ERROR: Rejecting swap file v1 to avoid cache " <<
103 "index corruption. Forcing a full cache index rebuild. " <<
104 "See Squid bug #3441.");
105 return nullptr;
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.");
112 return nullptr;
113 }
114
115 if (fseek(fp, header.record_size, SEEK_SET) != 0)
116 return nullptr;
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
124 debugs(47, DBG_IMPORTANT, "ERROR: Unknown swap file version: " << header.version);
125 return nullptr;
126}
127
128int
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}
143
void log(char *format,...)
#define assert(EX)
Definition: assert.h:19
static UFSSwapLogParser * GetUFSSwapLogParser(FILE *fp)
unsigned char key[SQUID_MD5_DIGEST_LENGTH]
SwappedTime timestamp
bool sane() const
consistency self-check: whether the data appears to make sense
bool ReadRecord(StoreSwapLogData &swapData)
Convert the on-disk 32-bit format to our current format while reading.
swap.state v2 log parser
bool ReadRecord(StoreSwapLogData &swapData)
#define DBG_IMPORTANT
Definition: Stream.h:41
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Stream.h:196
#define SQUID_MD5_DIGEST_LENGTH
Definition: md5.h:66
Definition: Module.h:13
static struct stat sb
Definition: squidclient.cc:71
signed_int32_t sfileno
Definition: forward.h:22
unsigned char key[SQUID_MD5_DIGEST_LENGTH]
@ SWAP_LOG_VERSION
Definition: swap_log_op.h:16

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors