Line | Count | Source |
1 | | /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
2 | | * Copyright by The HDF Group. * |
3 | | * All rights reserved. * |
4 | | * * |
5 | | * This file is part of HDF5. The full HDF5 copyright notice, including * |
6 | | * terms governing use, modification, and redistribution, is contained in * |
7 | | * the LICENSE file, which can be found at the root of the source code * |
8 | | * distribution tree, or in https://www.hdfgroup.org/licenses. * |
9 | | * If you do not have access to either file, you may request a copy from * |
10 | | * help@hdfgroup.org. * |
11 | | * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ |
12 | | |
13 | | /* |
14 | | * Purpose: The object modification time message |
15 | | */ |
16 | | |
17 | | #include "H5Omodule.h" /* This source code file is part of the H5O module */ |
18 | | |
19 | | #include "H5private.h" /* Generic Functions */ |
20 | | #include "H5Eprivate.h" /* Error handling */ |
21 | | #include "H5FLprivate.h" /* Free lists */ |
22 | | #include "H5Opkg.h" /* Object headers */ |
23 | | |
24 | | static void *H5O__mtime_new_decode(H5F_t *f, H5O_t *open_oh, unsigned mesg_flags, unsigned *ioflags, |
25 | | size_t p_size, const uint8_t *p); |
26 | | static herr_t H5O__mtime_new_encode(H5F_t *f, bool disable_shared, size_t H5_ATTR_UNUSED p_size, uint8_t *p, |
27 | | const void *_mesg); |
28 | | static size_t H5O__mtime_new_size(const H5F_t *f, bool disable_shared, const void *_mesg); |
29 | | |
30 | | static void *H5O__mtime_decode(H5F_t *f, H5O_t *open_oh, unsigned mesg_flags, unsigned *ioflags, |
31 | | size_t p_size, const uint8_t *p); |
32 | | static herr_t H5O__mtime_encode(H5F_t *f, bool disable_shared, size_t p_size, uint8_t *p, const void *_mesg); |
33 | | static void *H5O__mtime_copy(const void *_mesg, void *_dest); |
34 | | static size_t H5O__mtime_size(const H5F_t *f, bool disable_shared, const void *_mesg); |
35 | | static herr_t H5O__mtime_free(void *_mesg); |
36 | | static herr_t H5O__mtime_debug(H5F_t *f, const void *_mesg, FILE *stream, int indent, int fwidth); |
37 | | |
38 | | /* This message derives from H5O message class */ |
39 | | const H5O_msg_class_t H5O_MSG_MTIME[1] = {{ |
40 | | H5O_MTIME_ID, /*message id number */ |
41 | | "mtime", /*message name for debugging */ |
42 | | sizeof(time_t), /*native message size */ |
43 | | 0, /* messages are shareable? */ |
44 | | H5O__mtime_decode, /*decode message */ |
45 | | H5O__mtime_encode, /*encode message */ |
46 | | H5O__mtime_copy, /*copy the native value */ |
47 | | H5O__mtime_size, /*raw message size */ |
48 | | NULL, /* reset method */ |
49 | | H5O__mtime_free, /* free method */ |
50 | | NULL, /* file delete method */ |
51 | | NULL, /* link method */ |
52 | | NULL, /*set share method */ |
53 | | NULL, /*can share method */ |
54 | | NULL, /* pre copy native value to file */ |
55 | | NULL, /* copy native value to file */ |
56 | | NULL, /* post copy native value to file */ |
57 | | NULL, /* get creation index */ |
58 | | NULL, /* set creation index */ |
59 | | H5O__mtime_debug /*debug the message */ |
60 | | }}; |
61 | | |
62 | | /* This message derives from H5O message class */ |
63 | | /* (Only encode, decode & size routines are different from old mtime routines) */ |
64 | | const H5O_msg_class_t H5O_MSG_MTIME_NEW[1] = {{ |
65 | | H5O_MTIME_NEW_ID, /*message id number */ |
66 | | "mtime_new", /*message name for debugging */ |
67 | | sizeof(time_t), /*native message size */ |
68 | | 0, /* messages are shareable? */ |
69 | | H5O__mtime_new_decode, /*decode message */ |
70 | | H5O__mtime_new_encode, /*encode message */ |
71 | | H5O__mtime_copy, /*copy the native value */ |
72 | | H5O__mtime_new_size, /*raw message size */ |
73 | | NULL, /* reset method */ |
74 | | H5O__mtime_free, /* free method */ |
75 | | NULL, /* file delete method */ |
76 | | NULL, /* link method */ |
77 | | NULL, /*set share method */ |
78 | | NULL, /*can share method */ |
79 | | NULL, /* pre copy native value to file */ |
80 | | NULL, /* copy native value to file */ |
81 | | NULL, /* post copy native value to file */ |
82 | | NULL, /* get creation index */ |
83 | | NULL, /* set creation index */ |
84 | | H5O__mtime_debug /*debug the message */ |
85 | | }}; |
86 | | |
87 | | /* Current version of new mtime information */ |
88 | 47.7k | #define H5O_MTIME_VERSION 1 |
89 | | |
90 | | /* Declare a free list to manage the time_t struct */ |
91 | | H5FL_DEFINE(time_t); |
92 | | |
93 | | /*------------------------------------------------------------------------- |
94 | | * Function: H5O__mtime_new_decode |
95 | | * |
96 | | * Purpose: Decode a new modification time message and return a pointer to |
97 | | * a new time_t value. |
98 | | * |
99 | | * This version of the modification time was used in HDF5 |
100 | | * 1.6.1 and later. |
101 | | * |
102 | | * The new modification time message format was added due to the |
103 | | * performance overhead of the old format. |
104 | | * |
105 | | * Return: Success: Pointer to new message in native struct |
106 | | * Failure: NULL |
107 | | *------------------------------------------------------------------------- |
108 | | */ |
109 | | static void * |
110 | | H5O__mtime_new_decode(H5F_t H5_ATTR_NDEBUG_UNUSED *f, H5O_t H5_ATTR_UNUSED *open_oh, |
111 | | unsigned H5_ATTR_UNUSED mesg_flags, unsigned H5_ATTR_UNUSED *ioflags, size_t p_size, |
112 | | const uint8_t *p) |
113 | 47.7k | { |
114 | 47.7k | const uint8_t *p_end = p + p_size - 1; /* End of input buffer */ |
115 | 47.7k | time_t *mesg = NULL; |
116 | 47.7k | uint32_t tmp_time; /* Temporary copy of the time */ |
117 | 47.7k | void *ret_value = NULL; /* Return value */ |
118 | | |
119 | 47.7k | FUNC_ENTER_PACKAGE |
120 | | |
121 | 47.7k | assert(f); |
122 | 47.7k | assert(p); |
123 | | |
124 | 47.7k | if (H5_IS_BUFFER_OVERFLOW(p, 1, p_end)) |
125 | 13 | HGOTO_ERROR(H5E_OHDR, H5E_OVERFLOW, NULL, "ran off end of input buffer while decoding"); |
126 | 47.7k | if (*p++ != H5O_MTIME_VERSION) |
127 | 46 | HGOTO_ERROR(H5E_OHDR, H5E_CANTLOAD, NULL, "bad version number for mtime message"); |
128 | | |
129 | | /* Skip reserved bytes */ |
130 | 47.7k | if (H5_IS_BUFFER_OVERFLOW(p, 3, p_end)) |
131 | 0 | HGOTO_ERROR(H5E_OHDR, H5E_OVERFLOW, NULL, "ran off end of input buffer while decoding"); |
132 | 47.7k | p += 3; |
133 | | |
134 | | /* Get the time_t from the file */ |
135 | 47.7k | if (H5_IS_BUFFER_OVERFLOW(p, 4, p_end)) |
136 | 0 | HGOTO_ERROR(H5E_OHDR, H5E_OVERFLOW, NULL, "ran off end of input buffer while decoding"); |
137 | 47.7k | UINT32DECODE(p, tmp_time); |
138 | | |
139 | | /* The return value */ |
140 | 47.7k | if (NULL == (mesg = H5FL_MALLOC(time_t))) |
141 | 0 | HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, NULL, "memory allocation failed"); |
142 | 47.7k | *mesg = (time_t)tmp_time; |
143 | | |
144 | | /* Set return value */ |
145 | 47.7k | ret_value = mesg; |
146 | | |
147 | 47.7k | done: |
148 | 47.7k | FUNC_LEAVE_NOAPI(ret_value) |
149 | 47.7k | } /* end H5O__mtime_new_decode() */ |
150 | | |
151 | | /*------------------------------------------------------------------------- |
152 | | * Function: H5O__mtime_decode |
153 | | * |
154 | | * Purpose: Decode a modification time message and return a pointer to a |
155 | | * new time_t value. |
156 | | * |
157 | | * This version of the modification time was used in HDF5 |
158 | | * 1.6.0 and earlier. |
159 | | * |
160 | | * The new modification time message format was added due to the |
161 | | * performance overhead of the old format. |
162 | | * |
163 | | * Return: Success: Pointer to new message in native struct |
164 | | * Failure: NULL |
165 | | *------------------------------------------------------------------------- |
166 | | */ |
167 | | static void * |
168 | | H5O__mtime_decode(H5F_t H5_ATTR_NDEBUG_UNUSED *f, H5O_t H5_ATTR_UNUSED *open_oh, |
169 | | unsigned H5_ATTR_UNUSED mesg_flags, unsigned H5_ATTR_UNUSED *ioflags, size_t p_size, |
170 | | const uint8_t *p) |
171 | 16 | { |
172 | 16 | const uint8_t *p_end = p + p_size - 1; /* End of input buffer */ |
173 | 16 | time_t *mesg = NULL; |
174 | 16 | time_t the_time; |
175 | 16 | struct tm tm; |
176 | 16 | void *ret_value = NULL; |
177 | | |
178 | 16 | FUNC_ENTER_PACKAGE |
179 | | |
180 | 16 | assert(f); |
181 | 16 | assert(p); |
182 | | |
183 | | /* Buffer should have 14 message bytes and 2 reserved bytes */ |
184 | 16 | if (H5_IS_BUFFER_OVERFLOW(p, 16, p_end)) |
185 | 1 | HGOTO_ERROR(H5E_OHDR, H5E_OVERFLOW, NULL, "ran off end of input buffer while decoding"); |
186 | 15 | for (int i = 0; i < 14; i++) |
187 | 15 | if (!isdigit(p[i])) |
188 | 15 | HGOTO_ERROR(H5E_OHDR, H5E_CANTINIT, NULL, "badly formatted modification time message"); |
189 | | |
190 | | /* Convert YYYYMMDDhhmmss UTC to a time_t. */ |
191 | 0 | memset(&tm, 0, sizeof tm); |
192 | 0 | tm.tm_year = (p[0] - '0') * 1000 + (p[1] - '0') * 100 + (p[2] - '0') * 10 + (p[3] - '0') - 1900; |
193 | 0 | tm.tm_mon = (p[4] - '0') * 10 + (p[5] - '0') - 1; |
194 | 0 | tm.tm_mday = (p[6] - '0') * 10 + (p[7] - '0'); |
195 | 0 | tm.tm_hour = (p[8] - '0') * 10 + (p[9] - '0'); |
196 | 0 | tm.tm_min = (p[10] - '0') * 10 + (p[11] - '0'); |
197 | 0 | tm.tm_sec = (p[12] - '0') * 10 + (p[13] - '0'); |
198 | 0 | tm.tm_isdst = -1; /* (figure it out) */ |
199 | 0 | if ((time_t)-1 == (the_time = H5_make_time(&tm))) |
200 | 0 | HGOTO_ERROR(H5E_OHDR, H5E_CANTINIT, NULL, "can't construct time info"); |
201 | | |
202 | | /* The return value */ |
203 | 0 | if (NULL == (mesg = H5FL_MALLOC(time_t))) |
204 | 0 | HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, NULL, "memory allocation failed"); |
205 | 0 | *mesg = the_time; |
206 | | |
207 | | /* Set return value */ |
208 | 0 | ret_value = mesg; |
209 | |
|
210 | 16 | done: |
211 | 16 | FUNC_LEAVE_NOAPI(ret_value) |
212 | 16 | } /* end H5O__mtime_decode() */ |
213 | | |
214 | | /*------------------------------------------------------------------------- |
215 | | * Function: H5O__mtime_new_encode |
216 | | * |
217 | | * Purpose: Encodes a new modification time message. |
218 | | * |
219 | | * Return: Non-negative on success/Negative on failure |
220 | | * |
221 | | *------------------------------------------------------------------------- |
222 | | */ |
223 | | static herr_t |
224 | | H5O__mtime_new_encode(H5F_t H5_ATTR_UNUSED *f, bool H5_ATTR_UNUSED disable_shared, |
225 | | size_t H5_ATTR_UNUSED p_size, uint8_t *p, const void *_mesg) |
226 | 0 | { |
227 | 0 | const time_t *mesg = (const time_t *)_mesg; |
228 | |
|
229 | 0 | FUNC_ENTER_PACKAGE_NOERR |
230 | | |
231 | | /* check args */ |
232 | 0 | assert(f); |
233 | 0 | assert(p); |
234 | 0 | assert(mesg); |
235 | | |
236 | | /* Version */ |
237 | 0 | *p++ = H5O_MTIME_VERSION; |
238 | | |
239 | | /* Reserved bytes */ |
240 | 0 | *p++ = 0; |
241 | 0 | *p++ = 0; |
242 | 0 | *p++ = 0; |
243 | | |
244 | | /* Encode time */ |
245 | 0 | UINT32ENCODE(p, *mesg); |
246 | |
|
247 | 0 | FUNC_LEAVE_NOAPI(SUCCEED) |
248 | 0 | } /* end H5O__mtime_new_encode() */ |
249 | | |
250 | | /*------------------------------------------------------------------------- |
251 | | * Function: H5O__mtime_encode |
252 | | * |
253 | | * Purpose: Encodes a modification time message. |
254 | | * |
255 | | * Return: Non-negative on success/Negative on failure |
256 | | * |
257 | | *------------------------------------------------------------------------- |
258 | | */ |
259 | | static herr_t |
260 | | H5O__mtime_encode(H5F_t H5_ATTR_UNUSED *f, bool H5_ATTR_UNUSED disable_shared, size_t p_size, uint8_t *p, |
261 | | const void *_mesg) |
262 | 0 | { |
263 | 0 | const time_t *mesg = (const time_t *)_mesg; |
264 | 0 | struct tm tm_buf; |
265 | 0 | herr_t ret_value = SUCCEED; |
266 | |
|
267 | 0 | FUNC_ENTER_PACKAGE |
268 | | |
269 | | /* check args */ |
270 | 0 | assert(f); |
271 | 0 | assert(p); |
272 | 0 | assert(mesg); |
273 | | |
274 | | /* encode */ |
275 | 0 | if (HDgmtime_r(mesg, &tm_buf) == NULL) |
276 | 0 | HGOTO_ERROR(H5E_OHDR, H5E_BADVALUE, FAIL, "gmtime_r failed on time value"); |
277 | | |
278 | 0 | snprintf((char *)p, p_size, "%04d%02d%02d%02d%02d%02d", 1900 + tm_buf.tm_year, 1 + tm_buf.tm_mon, |
279 | 0 | tm_buf.tm_mday, tm_buf.tm_hour, tm_buf.tm_min, tm_buf.tm_sec); |
280 | |
|
281 | 0 | done: |
282 | 0 | FUNC_LEAVE_NOAPI(ret_value) |
283 | 0 | } /* end H5O__mtime_encode() */ |
284 | | |
285 | | /*------------------------------------------------------------------------- |
286 | | * Function: H5O__mtime_copy |
287 | | * |
288 | | * Purpose: Copies a message from _MESG to _DEST, allocating _DEST if |
289 | | * necessary. |
290 | | * |
291 | | * Return: Success: Ptr to _DEST |
292 | | * |
293 | | * Failure: NULL |
294 | | * |
295 | | *------------------------------------------------------------------------- |
296 | | */ |
297 | | static void * |
298 | | H5O__mtime_copy(const void *_mesg, void *_dest) |
299 | 0 | { |
300 | 0 | const time_t *mesg = (const time_t *)_mesg; |
301 | 0 | time_t *dest = (time_t *)_dest; |
302 | 0 | void *ret_value = NULL; /* Return value */ |
303 | |
|
304 | 0 | FUNC_ENTER_PACKAGE |
305 | | |
306 | | /* check args */ |
307 | 0 | assert(mesg); |
308 | 0 | if (!dest && NULL == (dest = H5FL_MALLOC(time_t))) |
309 | 0 | HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, NULL, "memory allocation failed"); |
310 | | |
311 | | /* copy */ |
312 | 0 | *dest = *mesg; |
313 | | |
314 | | /* Set return value */ |
315 | 0 | ret_value = dest; |
316 | |
|
317 | 0 | done: |
318 | 0 | FUNC_LEAVE_NOAPI(ret_value) |
319 | 0 | } /* end H5O__mtime_copy() */ |
320 | | |
321 | | /*------------------------------------------------------------------------- |
322 | | * Function: H5O__mtime_new_size |
323 | | * |
324 | | * Purpose: Returns the size of the raw message in bytes not |
325 | | * counting the message type or size fields, but only the data |
326 | | * fields. This function doesn't take into account |
327 | | * alignment. |
328 | | * |
329 | | * Return: Success: Message data size in bytes w/o alignment. |
330 | | * |
331 | | * Failure: 0 |
332 | | * |
333 | | *------------------------------------------------------------------------- |
334 | | */ |
335 | | static size_t |
336 | | H5O__mtime_new_size(const H5F_t H5_ATTR_UNUSED *f, bool H5_ATTR_UNUSED disable_shared, |
337 | | const void H5_ATTR_UNUSED *mesg) |
338 | 0 | { |
339 | 0 | FUNC_ENTER_PACKAGE_NOERR |
340 | | |
341 | | /* check args */ |
342 | 0 | assert(f); |
343 | 0 | assert(mesg); |
344 | |
|
345 | 0 | FUNC_LEAVE_NOAPI(8) |
346 | 0 | } /* end H5O__mtime_new_size() */ |
347 | | |
348 | | /*------------------------------------------------------------------------- |
349 | | * Function: H5O__mtime_size |
350 | | * |
351 | | * Purpose: Returns the size of the raw message in bytes not |
352 | | * counting the message type or size fields, but only the data |
353 | | * fields. This function doesn't take into account |
354 | | * alignment. |
355 | | * |
356 | | * Return: Success: Message data size in bytes w/o alignment. |
357 | | * |
358 | | * Failure: 0 |
359 | | * |
360 | | *------------------------------------------------------------------------- |
361 | | */ |
362 | | static size_t |
363 | | H5O__mtime_size(const H5F_t H5_ATTR_UNUSED *f, bool H5_ATTR_UNUSED disable_shared, |
364 | | const void H5_ATTR_UNUSED *mesg) |
365 | 0 | { |
366 | 0 | FUNC_ENTER_PACKAGE_NOERR |
367 | | |
368 | | /* check args */ |
369 | 0 | assert(f); |
370 | 0 | assert(mesg); |
371 | |
|
372 | 0 | FUNC_LEAVE_NOAPI(16) |
373 | 0 | } /* end H5O__mtime_size() */ |
374 | | |
375 | | /*------------------------------------------------------------------------- |
376 | | * Function: H5O__mtime_free |
377 | | * |
378 | | * Purpose: Frees the message |
379 | | * |
380 | | * Return: Non-negative on success/Negative on failure |
381 | | * |
382 | | *------------------------------------------------------------------------- |
383 | | */ |
384 | | static herr_t |
385 | | H5O__mtime_free(void *mesg) |
386 | 47.7k | { |
387 | 47.7k | FUNC_ENTER_PACKAGE_NOERR |
388 | | |
389 | 47.7k | assert(mesg); |
390 | | |
391 | 47.7k | mesg = H5FL_FREE(time_t, mesg); |
392 | | |
393 | 47.7k | FUNC_LEAVE_NOAPI(SUCCEED) |
394 | 47.7k | } /* end H5O__mtime_free() */ |
395 | | |
396 | | /*------------------------------------------------------------------------- |
397 | | * Function: H5O__mtime_debug |
398 | | * |
399 | | * Purpose: Prints debugging info for the message. |
400 | | * |
401 | | * Return: Non-negative on success/Negative on failure |
402 | | * |
403 | | *------------------------------------------------------------------------- |
404 | | */ |
405 | | static herr_t |
406 | | H5O__mtime_debug(H5F_t H5_ATTR_UNUSED *f, const void *_mesg, FILE *stream, int indent, int fwidth) |
407 | 0 | { |
408 | 0 | const time_t *mesg = (const time_t *)_mesg; |
409 | 0 | struct tm tm_buf; |
410 | 0 | char buf[128]; |
411 | |
|
412 | 0 | FUNC_ENTER_PACKAGE_NOERR |
413 | | |
414 | | /* check args */ |
415 | 0 | assert(f); |
416 | 0 | assert(mesg); |
417 | 0 | assert(stream); |
418 | 0 | assert(indent >= 0); |
419 | 0 | assert(fwidth >= 0); |
420 | | |
421 | | /* debug */ |
422 | 0 | if (HDlocaltime_r(mesg, &tm_buf) != NULL) |
423 | 0 | strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S %Z", &tm_buf); |
424 | 0 | else |
425 | 0 | snprintf(buf, sizeof(buf), "(invalid time)"); |
426 | 0 | fprintf(stream, "%*s%-*s %s\n", indent, "", fwidth, "Time:", buf); |
427 | |
|
428 | 0 | FUNC_LEAVE_NOAPI(SUCCEED) |
429 | 0 | } /* end H5O__mtime_debug() */ |