summaryrefslogtreecommitdiff
path: root/src/libdisfluid/disfluid-cache-entry.h
blob: 76de29a52971463e03bd54e8a294c6eb56ffbfe0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#ifndef DISFLUID_DISFLUID_CACHE_ENTRY_INCLUDED
# define DISFLUID_DISFLUID_CACHE_ENTRY_INCLUDED

# include <config.h>
# include "string-desc.h"
# include <stdbool.h>

MAYBE_UNUSED static int
db_mark_cache_entry (const char *db_root, const string_desc_t * id);

MAYBE_UNUSED static int
db_read_cache_entry (const char *db_root, const string_desc_t * id,
		     struct timespec *request_date,
		     struct timespec *response_date, bool *invalidated,
		     string_desc_t * key_id,
		     string_desc_t * response_header_id,
		     string_desc_t * response_body_id);

MAYBE_UNUSED static int
db_write_cache_entry (const char *db_root, string_desc_t * id,
		      const struct timespec *request_date,
		      const struct timespec *response_date, bool invalidated,
		      const string_desc_t * key_id,
		      const string_desc_t * response_header_id,
		      const string_desc_t * response_body_id);

# include "disfluid-db.h"
# include "safe-alloc.h"

MAYBE_UNUSED static int
db_mark_cache_entry (const char *db_root, const string_desc_t * id)
{
  struct timespec request_date = { 0 }, response_date = { 0 };
  bool invalidated = false;
  string_desc_t key = { 0 }, header = { 0 }, body = { 0 };
  int error = db_read_cache_entry (db_root, id, &request_date, &response_date,
				   &invalidated, &key, &header, &body);
  if (error < 0)
    {
      goto cleanup;
    }
  int key_error = db_mark_leaf (db_root, &key);
  int header_error = db_mark_leaf (db_root, &header);
  int body_error = db_mark_leaf (db_root, &body);
  if (key_error < 0 || header_error < 0 || body_error < 0)
    {
      error = -1;
    }
cleanup:
  FREE (key._data);
  FREE (header._data);
  FREE (body._data);
  return error;
}

/* The cache entry file is organized as follows:

   - on 16 bytes, "disfluid c.entry",
   - on 8 bytes, the request date (timestamp in seconds),
   - on 4 bytes, the request date (number of nanoseconds),
   - on 8 bytes, the response date (timestamp in seconds),
   - on 4 bytes, the response date (number of nanoseconds),
   - on 32 bytes, the key digest,
   - on 32 bytes, the header digest,
   - on 32 bytes, the body digest,
   - 80 if the entry has been artificially invalidated, 00 otherwise.
 */

static int
parse_timespec (const char *data, struct timespec *ts)
{
  ts->tv_sec = 0;
  ts->tv_nsec = 0;
  for (size_t i = 0; i < 8; i++)
    {
      ts->tv_sec *= 256;
      ts->tv_sec += (uint8_t) data[i];
    }
  for (size_t i = 0; i < 4; i++)
    {
      ts->tv_nsec *= 256;
      ts->tv_nsec += (uint8_t) data[8 + i];
    }
  if (ts->tv_nsec < 1000000000)
    {
      return 0;
    }
  return -1;
}

static int
represent_timespec (char *data, const struct timespec *ts)
{
  struct timespec cp = {.tv_sec = ts->tv_sec,.tv_nsec = ts->tv_nsec };
  for (size_t i = 8; i-- > 0;)
    {
      data[i] = (char) (uint8_t) cp.tv_sec % 256;
      cp.tv_sec /= 256;
    }
  for (size_t i = 4; i-- > 0;)
    {
      data[8 + i] = (char) (uint8_t) cp.tv_nsec % 256;
      cp.tv_nsec /= 256;
    }
}

MAYBE_UNUSED static int
db_read_cache_entry (const char *db_root, const string_desc_t * id,
		     struct timespec *request_date,
		     struct timespec *response_date, bool *invalidated,
		     string_desc_t * key_id,
		     string_desc_t * response_header_id,
		     string_desc_t * response_body_id)
{
  string_desc_t file_data = { 0 };
  request_date->tv_sec = 0;
  request_date->tv_nsec = 0;
  response_date->tv_sec = 0;
  response_date->tv_nsec = 0;
  *invalidated = false;
  assert (key_id->_data == NULL);
  assert (response_header_id->_data == NULL);
  assert (response_body_id->_data == NULL);
  key_id->_nbytes = 0;
  response_header_id->_nbytes = 0;
  response_body_id->_nbytes = 0;
  int error = db_read (db_root, id, &file_data);
  if (error < 0)
    {
      goto cleanup;
    }
  if (file_data._nbytes != 16 + 8 + 4 + 8 + 4 + 32 + 32 + 32 + 1)
    {
      error = -1;
      goto cleanup;
    }
  if (memcmp (file_data._data, "disfluid c.entry", 16) != 0)
    {
      error = -1;
      goto cleanup;
    }
  const char *data = file_data._data + 16;
  error = parse_timespec (data, request_date);
  data += 12;
  if (error != 0)
    {
      goto cleanup;
    }
  error = parse_timespec (data, response_date);
  data += 12;
  if (error != 0)
    {
      goto cleanup;
    }
  if (ALLOC_N (key_id->_data, 32) < 0)
    {
      error = -2;
      goto cleanup;
    }
  key_id->_nbytes = 32;
  memcpy (key_id->_data, data, 32);
  data += 32;
  if (ALLOC_N (response_header_id->_data, 32) < 0)
    {
      error = -2;
      goto cleanup;
    }
  response_header_id->_nbytes = 32;
  memcpy (response_header_id->_data, data, 32);
  data += 32;
  if (ALLOC_N (response_body_id->_data, 32) < 0)
    {
      error = -2;
      goto cleanup;
    }
  response_body_id->_nbytes = 32;
  memcpy (response_body_id->_data, data, 32);
  data += 32;
  switch (*data)
    {
    case 0:
      break;
    case 80:
      *invalidated = true;
      break;
    default:
      error = -1;
      goto cleanup;
    }
cleanup:
  if (error != 0)
    {
      FREE (key_id->_data);
      FREE (response_header_id->_data);
      FREE (response_body_id->_data);
    }
  FREE (file_data._data);
  return error;
}

MAYBE_UNUSED static int
db_write_cache_entry (const char *db_root, string_desc_t * id,
		      const struct timespec *request_date,
		      const struct timespec *response_date, bool invalidated,
		      const string_desc_t * key_id,
		      const string_desc_t * response_header_id,
		      const string_desc_t * response_body_id)
{
  char file_data[16 + 8 + 4 + 8 + 4 + 32 + 32 + 32 + 1];
  if (key_id->_nbytes != 32 || response_header_id->_nbytes != 32
      || response_body_id->_nbytes != 32)
    {
      return -1;
    }
  memcpy (file_data, "disfluid c.entry", 16);
  represent_timespec (file_data + 16, request_date);
  represent_timespec (file_data + 16 + 12, response_date);
  memcpy (file_data + 16 + 12 + 12, key_id->_data, 32);
  memcpy (file_data + 16 + 12 + 12 + 32, response_header_id->_data, 32);
  memcpy (file_data + 16 + 12 + 12 + 32 + 32, response_body_id->_data, 32);
  file_data[16 + 12 + 12 + 32 + 32 + 32] = 0;
  if (invalidated)
    {
      file_data[16 + 12 + 12 + 32 + 32 + 32] = 80;
    }
  const string_desc_t sd_data = {
    ._nbytes = sizeof (file_data),
    ._data = file_data
  };
  return db_write (db_root, id, &sd_data);
}

#endif /* DISFLUID_DISFLUID_CACHE_ENTRY_INCLUDED */