/src/hdf5/src/H5FDstdio.c
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 C STDIO virtual file driver which only uses calls from stdio.h. |
15 | | * This also serves as an example of coding a simple file driver, |
16 | | * therefore, it should not use any non-public definitions. |
17 | | * |
18 | | * NOTE: This driver is not as well tested as the standard SEC2 driver |
19 | | * and is not intended for production use! |
20 | | */ |
21 | | |
22 | | #include <assert.h> |
23 | | #include <errno.h> |
24 | | #include <stdio.h> |
25 | | #include <stdlib.h> |
26 | | #include <string.h> |
27 | | #include <sys/stat.h> |
28 | | |
29 | | #include "hdf5.h" |
30 | | |
31 | | #ifdef H5_HAVE_FLOCK |
32 | | /* Needed for lock type definitions (e.g., LOCK_EX) */ |
33 | | #include <sys/file.h> |
34 | | #endif /* H5_HAVE_FLOCK */ |
35 | | |
36 | | #ifdef H5_HAVE_UNISTD_H |
37 | | #include <unistd.h> |
38 | | #endif |
39 | | |
40 | | #ifdef H5_HAVE_WIN32_API |
41 | | /* The following two defines must be before any windows headers are included */ |
42 | | #define WIN32_LEAN_AND_MEAN /* Exclude rarely-used stuff from Windows headers */ |
43 | | #define NOGDI /* Exclude Graphic Display Interface macros */ |
44 | | |
45 | | #include <windows.h> |
46 | | #include <io.h> |
47 | | |
48 | | #endif /* H5_HAVE_WIN32_API */ |
49 | | |
50 | | /* Flag to indicate whether global driver resources & settings have been |
51 | | * initialized. |
52 | | */ |
53 | | static bool H5FD_stdio_init_s = false; |
54 | | |
55 | | /* Whether to ignore file locks when disabled (env var value) */ |
56 | | static htri_t ignore_disabled_file_locks_s = -1; |
57 | | |
58 | | /* The maximum number of bytes which can be written in a single I/O operation */ |
59 | | static size_t H5_STDIO_MAX_IO_BYTES_g = (size_t)-1; |
60 | | |
61 | | /* File operations */ |
62 | | typedef enum { |
63 | | H5FD_STDIO_OP_UNKNOWN = 0, |
64 | | H5FD_STDIO_OP_READ = 1, |
65 | | H5FD_STDIO_OP_WRITE = 2, |
66 | | H5FD_STDIO_OP_SEEK = 3 |
67 | | } H5FD_stdio_file_op; |
68 | | |
69 | | /* The description of a file belonging to this driver. The 'eoa' and 'eof' |
70 | | * determine the amount of hdf5 address space in use and the high-water mark |
71 | | * of the file (the current size of the underlying Unix file). The 'pos' |
72 | | * value is used to eliminate file position updates when they would be a |
73 | | * no-op. Unfortunately we've found systems that use separate file position |
74 | | * indicators for reading and writing so the lseek can only be eliminated if |
75 | | * the current operation is the same as the previous operation. When opening |
76 | | * a file the 'eof' will be set to the current file size, 'eoa' will be set |
77 | | * to zero, 'pos' will be set to H5F_ADDR_UNDEF (as it is when an error |
78 | | * occurs), and 'op' will be set to H5F_OP_UNKNOWN. |
79 | | */ |
80 | | typedef struct H5FD_stdio_t { |
81 | | H5FD_t pub; /* public stuff, must be first */ |
82 | | FILE *fp; /* the file handle */ |
83 | | int fd; /* file descriptor (for truncate) */ |
84 | | haddr_t eoa; /* end of allocated region */ |
85 | | haddr_t eof; /* end of file; current file size */ |
86 | | haddr_t pos; /* current file I/O position */ |
87 | | unsigned write_access; /* Flag to indicate the file was opened with write access */ |
88 | | bool ignore_disabled_file_locks; |
89 | | H5FD_stdio_file_op op; /* last operation */ |
90 | | #ifndef H5_HAVE_WIN32_API |
91 | | /* On most systems the combination of device and i-node number uniquely |
92 | | * identify a file. Note that Cygwin, MinGW and other Windows POSIX |
93 | | * environments have the stat function (which fakes inodes) |
94 | | * and will use the 'device + inodes' scheme as opposed to the |
95 | | * Windows code further below. |
96 | | */ |
97 | | dev_t device; /* file device number */ |
98 | | ino_t inode; /* file i-node number */ |
99 | | #else |
100 | | /* Files in windows are uniquely identified by the volume serial |
101 | | * number and the file index (both low and high parts). |
102 | | * |
103 | | * There are caveats where these numbers can change, especially |
104 | | * on FAT file systems. On NTFS, however, a file should keep |
105 | | * those numbers the same until renamed or deleted (though you |
106 | | * can use ReplaceFile() on NTFS to keep the numbers the same |
107 | | * while renaming). |
108 | | * |
109 | | * See the MSDN "BY_HANDLE_FILE_INFORMATION Structure" entry for |
110 | | * more information. |
111 | | * |
112 | | * http://msdn.microsoft.com/en-us/library/aa363788(v=VS.85).aspx |
113 | | */ |
114 | | DWORD nFileIndexLow; |
115 | | DWORD nFileIndexHigh; |
116 | | DWORD dwVolumeSerialNumber; |
117 | | |
118 | | HANDLE hFile; /* Native windows file handle */ |
119 | | #endif /* H5_HAVE_WIN32_API */ |
120 | | } H5FD_stdio_t; |
121 | | |
122 | | /* Platform-independent names for some file-oriented functions */ |
123 | | |
124 | | #ifdef H5_HAVE_WIN32_API |
125 | | /* Windows and MinGW */ |
126 | | #define file_ftell _ftelli64 |
127 | | #else |
128 | | /* Everyone else */ |
129 | 0 | #define file_ftell ftello |
130 | | #endif |
131 | | |
132 | | #if defined(H5_HAVE_WIN32_API) && !defined(H5_HAVE_MINGW) |
133 | | /* Windows (but NOT MinGW) */ |
134 | | #define file_fseek _fseeki64 |
135 | | #define file_ftruncate _chsize_s |
136 | | #else |
137 | | /* Everyone else */ |
138 | 0 | #define file_fseek fseeko |
139 | 0 | #define file_ftruncate ftruncate |
140 | | #endif |
141 | | |
142 | | /* These macros check for overflow of various quantities. These macros |
143 | | * assume that HDoff_t is signed and haddr_t and size_t are unsigned. |
144 | | * |
145 | | * MY_ADDR_OVERFLOW: Checks whether a file address of type `haddr_t' |
146 | | * is too large to be represented by the second argument |
147 | | * of the file seek function. |
148 | | * |
149 | | * MY_SIZE_OVERFLOW: Checks whether a buffer size of type `hsize_t' is too |
150 | | * large to be represented by the `size_t' type. |
151 | | * |
152 | | * MY_REGION_OVERFLOW: Checks whether an address and size pair describe data |
153 | | * which can be addressed entirely by the second |
154 | | * argument of the file seek function. |
155 | | */ |
156 | 0 | #define MY_MAXADDR (((haddr_t)1 << (8 * sizeof(HDoff_t) - 1)) - 1) |
157 | 0 | #define MY_ADDR_OVERFLOW(A) (HADDR_UNDEF == (A) || ((A) & ~(haddr_t)MY_MAXADDR)) |
158 | 0 | #define MY_SIZE_OVERFLOW(Z) ((Z) & ~(hsize_t)MY_MAXADDR) |
159 | | #define MY_REGION_OVERFLOW(A, Z) \ |
160 | 0 | (MY_ADDR_OVERFLOW(A) || MY_SIZE_OVERFLOW(Z) || HADDR_UNDEF == (A) + (Z) || \ |
161 | 0 | (HDoff_t)((A) + (Z)) < (HDoff_t)(A)) |
162 | | |
163 | | /* Prototypes */ |
164 | | static H5FD_t *H5FD_stdio_open(const char *name, unsigned flags, hid_t fapl_id, haddr_t maxaddr); |
165 | | static herr_t H5FD_stdio_close(H5FD_t *lf); |
166 | | static int H5FD_stdio_cmp(const H5FD_t *_f1, const H5FD_t *_f2); |
167 | | static herr_t H5FD_stdio_query(const H5FD_t *_f1, unsigned long *flags); |
168 | | static haddr_t H5FD_stdio_alloc(H5FD_t *_file, H5FD_mem_t type, hid_t dxpl_id, hsize_t size); |
169 | | static haddr_t H5FD_stdio_get_eoa(const H5FD_t *_file, H5FD_mem_t type); |
170 | | static herr_t H5FD_stdio_set_eoa(H5FD_t *_file, H5FD_mem_t type, haddr_t addr); |
171 | | static haddr_t H5FD_stdio_get_eof(const H5FD_t *_file, H5FD_mem_t type); |
172 | | static herr_t H5FD_stdio_get_handle(H5FD_t *_file, hid_t fapl, void **file_handle); |
173 | | static herr_t H5FD_stdio_read(H5FD_t *lf, H5FD_mem_t type, hid_t fapl_id, haddr_t addr, size_t size, |
174 | | void *buf); |
175 | | static herr_t H5FD_stdio_write(H5FD_t *lf, H5FD_mem_t type, hid_t fapl_id, haddr_t addr, size_t size, |
176 | | const void *buf); |
177 | | static herr_t H5FD_stdio_flush(H5FD_t *_file, hid_t dxpl_id, bool closing); |
178 | | static herr_t H5FD_stdio_truncate(H5FD_t *_file, hid_t dxpl_id, bool closing); |
179 | | static herr_t H5FD_stdio_lock(H5FD_t *_file, bool rw); |
180 | | static herr_t H5FD_stdio_unlock(H5FD_t *_file); |
181 | | static herr_t H5FD_stdio_delete(const char *filename, hid_t fapl_id); |
182 | | |
183 | | const H5FD_class_t H5FD_stdio_g = { |
184 | | H5FD_CLASS_VERSION, /* struct version */ |
185 | | H5_VFD_STDIO, /* value */ |
186 | | "stdio", /* name */ |
187 | | MY_MAXADDR, /* maxaddr */ |
188 | | H5F_CLOSE_WEAK, /* fc_degree */ |
189 | | NULL, /* terminate */ |
190 | | NULL, /* sb_size */ |
191 | | NULL, /* sb_encode */ |
192 | | NULL, /* sb_decode */ |
193 | | 0, /* fapl_size */ |
194 | | NULL, /* fapl_get */ |
195 | | NULL, /* fapl_copy */ |
196 | | NULL, /* fapl_free */ |
197 | | 0, /* dxpl_size */ |
198 | | NULL, /* dxpl_copy */ |
199 | | NULL, /* dxpl_free */ |
200 | | H5FD_stdio_open, /* open */ |
201 | | H5FD_stdio_close, /* close */ |
202 | | H5FD_stdio_cmp, /* cmp */ |
203 | | H5FD_stdio_query, /* query */ |
204 | | NULL, /* get_type_map */ |
205 | | H5FD_stdio_alloc, /* alloc */ |
206 | | NULL, /* free */ |
207 | | H5FD_stdio_get_eoa, /* get_eoa */ |
208 | | H5FD_stdio_set_eoa, /* set_eoa */ |
209 | | H5FD_stdio_get_eof, /* get_eof */ |
210 | | H5FD_stdio_get_handle, /* get_handle */ |
211 | | H5FD_stdio_read, /* read */ |
212 | | H5FD_stdio_write, /* write */ |
213 | | NULL, /* read_vector */ |
214 | | NULL, /* write_vector */ |
215 | | NULL, /* read_selection */ |
216 | | NULL, /* write_selection */ |
217 | | H5FD_stdio_flush, /* flush */ |
218 | | H5FD_stdio_truncate, /* truncate */ |
219 | | H5FD_stdio_lock, /* lock */ |
220 | | H5FD_stdio_unlock, /* unlock */ |
221 | | H5FD_stdio_delete, /* del */ |
222 | | NULL, /* ctl */ |
223 | | H5FD_FLMAP_DICHOTOMY /* fl_map */ |
224 | | }; |
225 | | |
226 | | /*------------------------------------------------------------------------- |
227 | | * Function: H5FD__stdio_init |
228 | | * |
229 | | * Purpose: Singleton to initialize global driver settings & resources. |
230 | | * |
231 | | * Return: Non-negative on success/Negative on failure |
232 | | * |
233 | | *------------------------------------------------------------------------- |
234 | | */ |
235 | | static herr_t |
236 | | H5FD__stdio_init(void) |
237 | 0 | { |
238 | 0 | char *lock_env_var = NULL; /* Environment variable pointer */ |
239 | | |
240 | | /* Check the use disabled file locks environment variable */ |
241 | 0 | lock_env_var = getenv(HDF5_USE_FILE_LOCKING); |
242 | 0 | if (lock_env_var && !strcmp(lock_env_var, "BEST_EFFORT")) |
243 | 0 | ignore_disabled_file_locks_s = 1; /* Override: Ignore disabled locks */ |
244 | 0 | else if (lock_env_var && (!strcmp(lock_env_var, "TRUE") || !strcmp(lock_env_var, "1"))) |
245 | 0 | ignore_disabled_file_locks_s = 0; /* Override: Don't ignore disabled locks */ |
246 | 0 | else |
247 | 0 | ignore_disabled_file_locks_s = -1; /* Environment variable not set, or not set correctly */ |
248 | | |
249 | | /* Indicate that driver is set up */ |
250 | 0 | H5FD_stdio_init_s = true; |
251 | |
|
252 | 0 | return 0; |
253 | 0 | } /* end H5FD__stdio_init() */ |
254 | | |
255 | | /*------------------------------------------------------------------------- |
256 | | * Function: H5Pset_fapl_stdio |
257 | | * |
258 | | * Purpose: Modify the file access property list to use the H5FD_STDIO |
259 | | * driver defined in this source file. There are no driver |
260 | | * specific properties. |
261 | | * |
262 | | * Return: Non-negative on success/Negative on failure |
263 | | * |
264 | | *------------------------------------------------------------------------- |
265 | | */ |
266 | | herr_t |
267 | | H5Pset_fapl_stdio(hid_t fapl_id) |
268 | 0 | { |
269 | | |
270 | | /* Clear the error stack */ |
271 | 0 | H5Eclear2(H5E_DEFAULT); |
272 | |
|
273 | 0 | if (0 == H5Pisa_class(fapl_id, H5P_FILE_ACCESS)) |
274 | 0 | H5Epush_ret(__func__, H5E_ERR_CLS, H5E_PLIST, H5E_BADTYPE, "not a file access property list", -1); |
275 | | |
276 | 0 | return H5Pset_driver(fapl_id, H5FD_STDIO, NULL); |
277 | 0 | } /* end H5Pset_fapl_stdio() */ |
278 | | |
279 | | /*------------------------------------------------------------------------- |
280 | | * Function: H5FD_stdio_open |
281 | | * |
282 | | * Purpose: Create and/or opens a Standard C file as an HDF5 file. |
283 | | * |
284 | | * Errors: |
285 | | * IO CANTOPENFILE File doesn't exist and CREAT wasn't |
286 | | * specified. |
287 | | * IO CANTOPENFILE fopen() failed. |
288 | | * IO FILEEXISTS File exists but CREAT and EXCL were |
289 | | * specified. |
290 | | * |
291 | | * Return: |
292 | | * Success: A pointer to a new file data structure. The |
293 | | * public fields will be initialized by the |
294 | | * caller, which is always H5FD_open(). |
295 | | * |
296 | | * Failure: NULL |
297 | | * |
298 | | *------------------------------------------------------------------------- |
299 | | */ |
300 | | static H5FD_t * |
301 | | H5FD_stdio_open(const char *name, unsigned flags, hid_t fapl_id, haddr_t maxaddr) |
302 | 0 | { |
303 | 0 | FILE *f = NULL; |
304 | 0 | unsigned write_access = 0; /* File opened with write access? */ |
305 | 0 | H5FD_stdio_t *file = NULL; |
306 | | #ifdef H5_HAVE_WIN32_API |
307 | | struct _BY_HANDLE_FILE_INFORMATION fileinfo; |
308 | | #else /* H5_HAVE_WIN32_API */ |
309 | 0 | struct stat sb; |
310 | 0 | #endif /* H5_HAVE_WIN32_API */ |
311 | | |
312 | | /* Sanity check on file offsets */ |
313 | 0 | assert(sizeof(HDoff_t) >= sizeof(size_t)); |
314 | | |
315 | | /* Quiet compiler */ |
316 | 0 | (void)fapl_id; |
317 | | |
318 | | /* Clear the error stack */ |
319 | 0 | H5Eclear2(H5E_DEFAULT); |
320 | | |
321 | | /* Initialize driver, if it's not yet */ |
322 | 0 | if (!H5FD_stdio_init_s) |
323 | 0 | if (H5FD__stdio_init() < 0) |
324 | 0 | H5Epush_ret(__func__, H5E_ERR_CLS, H5E_VFL, H5E_CANTINIT, "can't initialize driver", NULL); |
325 | | |
326 | | /* Check arguments */ |
327 | 0 | if (!name || !*name) |
328 | 0 | H5Epush_ret(__func__, H5E_ERR_CLS, H5E_ARGS, H5E_BADVALUE, "invalid file name", NULL); |
329 | 0 | if (0 == maxaddr || HADDR_UNDEF == maxaddr) |
330 | 0 | H5Epush_ret(__func__, H5E_ERR_CLS, H5E_ARGS, H5E_BADRANGE, "bogus maxaddr", NULL); |
331 | 0 | if (MY_ADDR_OVERFLOW(maxaddr)) |
332 | 0 | H5Epush_ret(__func__, H5E_ERR_CLS, H5E_ARGS, H5E_OVERFLOW, "maxaddr too large", NULL); |
333 | | |
334 | | /* Tentatively open file in read-only mode, to check for existence */ |
335 | 0 | if (flags & H5F_ACC_RDWR) |
336 | 0 | f = fopen(name, "rb+"); |
337 | 0 | else |
338 | 0 | f = fopen(name, "rb"); |
339 | |
|
340 | 0 | if (!f) { |
341 | | /* File doesn't exist */ |
342 | 0 | if (flags & H5F_ACC_CREAT) { |
343 | 0 | assert(flags & H5F_ACC_RDWR); |
344 | 0 | f = fopen(name, "wb+"); |
345 | 0 | write_access = 1; /* Note the write access */ |
346 | 0 | } |
347 | 0 | else |
348 | 0 | H5Epush_ret(__func__, H5E_ERR_CLS, H5E_IO, H5E_CANTOPENFILE, |
349 | 0 | "file doesn't exist and CREAT wasn't specified", NULL); |
350 | 0 | } |
351 | 0 | else if (flags & H5F_ACC_EXCL) { |
352 | | /* File exists, but EXCL is passed. Fail. */ |
353 | 0 | assert(flags & H5F_ACC_CREAT); |
354 | 0 | fclose(f); |
355 | 0 | H5Epush_ret(__func__, H5E_ERR_CLS, H5E_IO, H5E_FILEEXISTS, |
356 | 0 | "file exists but CREAT and EXCL were specified", NULL); |
357 | 0 | } |
358 | 0 | else if (flags & H5F_ACC_RDWR) { |
359 | 0 | if (flags & H5F_ACC_TRUNC) |
360 | 0 | f = freopen(name, "wb+", f); |
361 | 0 | write_access = 1; /* Note the write access */ |
362 | 0 | } /* end if */ |
363 | | /* Note there is no need to reopen if neither TRUNC nor EXCL are specified, |
364 | | * as the tentative open will work */ |
365 | | |
366 | 0 | if (!f) |
367 | 0 | H5Epush_ret(__func__, H5E_ERR_CLS, H5E_IO, H5E_CANTOPENFILE, "fopen failed", NULL); |
368 | | |
369 | | /* Build the return value */ |
370 | 0 | if (NULL == (file = (H5FD_stdio_t *)calloc((size_t)1, sizeof(H5FD_stdio_t)))) { |
371 | 0 | fclose(f); |
372 | 0 | H5Epush_ret(__func__, H5E_ERR_CLS, H5E_RESOURCE, H5E_NOSPACE, "memory allocation failed", NULL); |
373 | 0 | } /* end if */ |
374 | 0 | file->fp = f; |
375 | 0 | file->op = H5FD_STDIO_OP_SEEK; |
376 | 0 | file->pos = HADDR_UNDEF; |
377 | 0 | file->write_access = write_access; /* Note the write_access for later */ |
378 | 0 | if (file_fseek(file->fp, 0, SEEK_END) < 0) { |
379 | 0 | file->op = H5FD_STDIO_OP_UNKNOWN; |
380 | 0 | } |
381 | 0 | else { |
382 | 0 | HDoff_t x = file_ftell(file->fp); |
383 | 0 | assert(x >= 0); |
384 | 0 | file->eof = (haddr_t)x; |
385 | 0 | } |
386 | | |
387 | | /* Check the file locking flags in the fapl */ |
388 | 0 | if (ignore_disabled_file_locks_s != -1) |
389 | | /* The environment variable was set, so use that preferentially */ |
390 | 0 | file->ignore_disabled_file_locks = ignore_disabled_file_locks_s; |
391 | 0 | else { |
392 | 0 | bool unused; |
393 | | |
394 | | /* Use the value in the property list */ |
395 | 0 | if (H5Pget_file_locking(fapl_id, &unused, &file->ignore_disabled_file_locks) < 0) { |
396 | 0 | fclose(file->fp); |
397 | 0 | free(file); |
398 | 0 | H5Epush_ret(__func__, H5E_ERR_CLS, H5E_FILE, H5E_CANTGET, |
399 | 0 | "unable to get use disabled file locks property", NULL); |
400 | 0 | } |
401 | 0 | } |
402 | | |
403 | | /* Get the file descriptor (needed for truncate and some Windows information) */ |
404 | | #ifdef H5_HAVE_WIN32_API |
405 | | file->fd = _fileno(file->fp); |
406 | | #else /* H5_HAVE_WIN32_API */ |
407 | 0 | file->fd = fileno(file->fp); |
408 | 0 | #endif /* H5_HAVE_WIN32_API */ |
409 | 0 | if (file->fd < 0) { |
410 | 0 | fclose(file->fp); |
411 | 0 | free(file); |
412 | 0 | H5Epush_ret(__func__, H5E_ERR_CLS, H5E_FILE, H5E_CANTOPENFILE, "unable to get file descriptor", NULL); |
413 | 0 | } /* end if */ |
414 | | |
415 | | #ifdef H5_HAVE_WIN32_API |
416 | | file->hFile = (HANDLE)_get_osfhandle(file->fd); |
417 | | if (INVALID_HANDLE_VALUE == file->hFile) { |
418 | | fclose(file->fp); |
419 | | free(file); |
420 | | H5Epush_ret(__func__, H5E_ERR_CLS, H5E_FILE, H5E_CANTOPENFILE, "unable to get Windows file handle", |
421 | | NULL); |
422 | | } /* end if */ |
423 | | |
424 | | if (!GetFileInformationByHandle((HANDLE)file->hFile, &fileinfo)) { |
425 | | fclose(file->fp); |
426 | | free(file); |
427 | | H5Epush_ret(__func__, H5E_ERR_CLS, H5E_FILE, H5E_CANTOPENFILE, |
428 | | "unable to get Windows file descriptor information", NULL); |
429 | | } /* end if */ |
430 | | |
431 | | file->nFileIndexHigh = fileinfo.nFileIndexHigh; |
432 | | file->nFileIndexLow = fileinfo.nFileIndexLow; |
433 | | file->dwVolumeSerialNumber = fileinfo.dwVolumeSerialNumber; |
434 | | #else /* H5_HAVE_WIN32_API */ |
435 | 0 | if (fstat(file->fd, &sb) < 0) { |
436 | 0 | fclose(file->fp); |
437 | 0 | free(file); |
438 | 0 | H5Epush_ret(__func__, H5E_ERR_CLS, H5E_FILE, H5E_BADFILE, "unable to fstat file", NULL); |
439 | 0 | } /* end if */ |
440 | 0 | file->device = sb.st_dev; |
441 | 0 | file->inode = sb.st_ino; |
442 | 0 | #endif /* H5_HAVE_WIN32_API */ |
443 | |
|
444 | 0 | return ((H5FD_t *)file); |
445 | 0 | } /* end H5FD_stdio_open() */ |
446 | | |
447 | | /*------------------------------------------------------------------------- |
448 | | * Function: H5F_stdio_close |
449 | | * |
450 | | * Purpose: Closes a file. |
451 | | * |
452 | | * Errors: |
453 | | * IO CLOSEERROR Fclose failed. |
454 | | * |
455 | | * Return: Non-negative on success/Negative on failure |
456 | | * |
457 | | *------------------------------------------------------------------------- |
458 | | */ |
459 | | static herr_t |
460 | | H5FD_stdio_close(H5FD_t *_file) |
461 | 0 | { |
462 | 0 | H5FD_stdio_t *file = (H5FD_stdio_t *)_file; |
463 | | |
464 | | /* Clear the error stack */ |
465 | 0 | H5Eclear2(H5E_DEFAULT); |
466 | |
|
467 | 0 | if (fclose(file->fp) < 0) |
468 | 0 | H5Epush_ret(__func__, H5E_ERR_CLS, H5E_IO, H5E_CLOSEERROR, "fclose failed", -1); |
469 | | |
470 | 0 | free(file); |
471 | |
|
472 | 0 | return 0; |
473 | 0 | } /* end H5FD_stdio_close() */ |
474 | | |
475 | | /*------------------------------------------------------------------------- |
476 | | * Function: H5FD_stdio_cmp |
477 | | * |
478 | | * Purpose: Compares two files belonging to this driver using an |
479 | | * arbitrary (but consistent) ordering. |
480 | | * |
481 | | * Return: |
482 | | * Success: A value like strcmp() |
483 | | * |
484 | | * Failure: never fails (arguments were checked by the caller). |
485 | | * |
486 | | *------------------------------------------------------------------------- |
487 | | */ |
488 | | static int |
489 | | H5FD_stdio_cmp(const H5FD_t *_f1, const H5FD_t *_f2) |
490 | 0 | { |
491 | 0 | const H5FD_stdio_t *f1 = (const H5FD_stdio_t *)_f1; |
492 | 0 | const H5FD_stdio_t *f2 = (const H5FD_stdio_t *)_f2; |
493 | | |
494 | | /* Clear the error stack */ |
495 | 0 | H5Eclear2(H5E_DEFAULT); |
496 | |
|
497 | | #ifdef H5_HAVE_WIN32_API |
498 | | if (f1->dwVolumeSerialNumber < f2->dwVolumeSerialNumber) |
499 | | return -1; |
500 | | if (f1->dwVolumeSerialNumber > f2->dwVolumeSerialNumber) |
501 | | return 1; |
502 | | |
503 | | if (f1->nFileIndexHigh < f2->nFileIndexHigh) |
504 | | return -1; |
505 | | if (f1->nFileIndexHigh > f2->nFileIndexHigh) |
506 | | return 1; |
507 | | |
508 | | if (f1->nFileIndexLow < f2->nFileIndexLow) |
509 | | return -1; |
510 | | if (f1->nFileIndexLow > f2->nFileIndexLow) |
511 | | return 1; |
512 | | #else /* H5_HAVE_WIN32_API */ |
513 | 0 | if (f1->device < f2->device) |
514 | 0 | return -1; |
515 | 0 | if (f1->device > f2->device) |
516 | 0 | return 1; |
517 | | |
518 | 0 | if (f1->inode < f2->inode) |
519 | 0 | return -1; |
520 | 0 | if (f1->inode > f2->inode) |
521 | 0 | return 1; |
522 | 0 | #endif /* H5_HAVE_WIN32_API */ |
523 | | |
524 | 0 | return 0; |
525 | 0 | } /* H5FD_stdio_cmp() */ |
526 | | |
527 | | /*------------------------------------------------------------------------- |
528 | | * Function: H5FD_stdio_query |
529 | | * |
530 | | * Purpose: Set the flags that this VFL driver is capable of supporting. |
531 | | * (listed in H5FDpublic.h) |
532 | | * |
533 | | * Return: Success: non-negative |
534 | | * |
535 | | * Failure: negative |
536 | | * |
537 | | *------------------------------------------------------------------------- |
538 | | */ |
539 | | static herr_t |
540 | | H5FD_stdio_query(const H5FD_t *_f, unsigned long /*OUT*/ *flags) |
541 | 0 | { |
542 | | /* Quiet the compiler */ |
543 | 0 | (void)_f; |
544 | | |
545 | | /* Set the VFL feature flags that this driver supports. |
546 | | * |
547 | | * Note that this VFD does not support SWMR due to the unpredictable |
548 | | * nature of the buffering layer. |
549 | | */ |
550 | 0 | if (flags) { |
551 | 0 | *flags = 0; |
552 | 0 | *flags |= H5FD_FEAT_AGGREGATE_METADATA; /* OK to aggregate metadata allocations */ |
553 | 0 | *flags |= H5FD_FEAT_ACCUMULATE_METADATA; /* OK to accumulate metadata for faster writes */ |
554 | 0 | *flags |= H5FD_FEAT_DATA_SIEVE; /* OK to perform data sieving for faster raw data reads & writes */ |
555 | 0 | *flags |= H5FD_FEAT_AGGREGATE_SMALLDATA; /* OK to aggregate "small" raw data allocations */ |
556 | 0 | *flags |= H5FD_FEAT_DEFAULT_VFD_COMPATIBLE; /* VFD creates a file which can be opened with the default |
557 | | VFD */ |
558 | 0 | } |
559 | |
|
560 | 0 | return 0; |
561 | 0 | } /* end H5FD_stdio_query() */ |
562 | | |
563 | | /*------------------------------------------------------------------------- |
564 | | * Function: H5FD_stdio_alloc |
565 | | * |
566 | | * Purpose: Allocates file memory. If fseeko isn't available, makes |
567 | | * sure the file size isn't bigger than 2GB because the |
568 | | * parameter OFFSET of fseek is of the type LONG INT, limiting |
569 | | * the file size to 2GB. |
570 | | * |
571 | | * Return: |
572 | | * Success: Address of new memory |
573 | | * |
574 | | * Failure: HADDR_UNDEF |
575 | | * |
576 | | *------------------------------------------------------------------------- |
577 | | */ |
578 | | static haddr_t |
579 | | H5FD_stdio_alloc(H5FD_t *_file, H5FD_mem_t /*UNUSED*/ type, hid_t /*UNUSED*/ dxpl_id, hsize_t size) |
580 | 0 | { |
581 | 0 | H5FD_stdio_t *file = (H5FD_stdio_t *)_file; |
582 | 0 | haddr_t addr; |
583 | | |
584 | | /* Quiet compiler */ |
585 | 0 | (void)type; |
586 | 0 | (void)dxpl_id; |
587 | | |
588 | | /* Clear the error stack */ |
589 | 0 | H5Eclear2(H5E_DEFAULT); |
590 | | |
591 | | /* Compute the address for the block to allocate */ |
592 | 0 | addr = file->eoa; |
593 | |
|
594 | 0 | file->eoa = addr + size; |
595 | |
|
596 | 0 | return addr; |
597 | 0 | } /* end H5FD_stdio_alloc() */ |
598 | | |
599 | | /*------------------------------------------------------------------------- |
600 | | * Function: H5FD_stdio_get_eoa |
601 | | * |
602 | | * Purpose: Gets the end-of-address marker for the file. The EOA marker |
603 | | * is the first address past the last byte allocated in the |
604 | | * format address space. |
605 | | * |
606 | | * Return: Success: The end-of-address marker. |
607 | | * |
608 | | * Failure: HADDR_UNDEF |
609 | | * |
610 | | *------------------------------------------------------------------------- |
611 | | */ |
612 | | static haddr_t |
613 | | H5FD_stdio_get_eoa(const H5FD_t *_file, H5FD_mem_t /*UNUSED*/ type) |
614 | 0 | { |
615 | 0 | const H5FD_stdio_t *file = (const H5FD_stdio_t *)_file; |
616 | | |
617 | | /* Clear the error stack */ |
618 | 0 | H5Eclear2(H5E_DEFAULT); |
619 | | |
620 | | /* Quiet compiler */ |
621 | 0 | (void)type; |
622 | |
|
623 | 0 | return file->eoa; |
624 | 0 | } /* end H5FD_stdio_get_eoa() */ |
625 | | |
626 | | /*------------------------------------------------------------------------- |
627 | | * Function: H5FD_stdio_set_eoa |
628 | | * |
629 | | * Purpose: Set the end-of-address marker for the file. This function is |
630 | | * called shortly after an existing HDF5 file is opened in order |
631 | | * to tell the driver where the end of the HDF5 data is located. |
632 | | * |
633 | | * Return: Success: 0 |
634 | | * |
635 | | * Failure: Does not fail |
636 | | * |
637 | | *------------------------------------------------------------------------- |
638 | | */ |
639 | | static herr_t |
640 | | H5FD_stdio_set_eoa(H5FD_t *_file, H5FD_mem_t /*UNUSED*/ type, haddr_t addr) |
641 | 0 | { |
642 | 0 | H5FD_stdio_t *file = (H5FD_stdio_t *)_file; |
643 | | |
644 | | /* Clear the error stack */ |
645 | 0 | H5Eclear2(H5E_DEFAULT); |
646 | | |
647 | | /* Quiet the compiler */ |
648 | 0 | (void)type; |
649 | |
|
650 | 0 | file->eoa = addr; |
651 | |
|
652 | 0 | return 0; |
653 | 0 | } |
654 | | |
655 | | /*------------------------------------------------------------------------- |
656 | | * Function: H5FD_stdio_get_eof |
657 | | * |
658 | | * Purpose: Returns the end-of-file marker, which is the greater of |
659 | | * either the Unix end-of-file or the HDF5 end-of-address |
660 | | * markers. |
661 | | * |
662 | | * Return: Success: End of file address, the first address past |
663 | | * the end of the "file", either the Unix file |
664 | | * or the HDF5 file. |
665 | | * |
666 | | * Failure: HADDR_UNDEF |
667 | | * |
668 | | *------------------------------------------------------------------------- |
669 | | */ |
670 | | static haddr_t |
671 | | H5FD_stdio_get_eof(const H5FD_t *_file, H5FD_mem_t /*UNUSED*/ type) |
672 | 0 | { |
673 | 0 | const H5FD_stdio_t *file = (const H5FD_stdio_t *)_file; |
674 | | |
675 | | /* Quiet the compiler */ |
676 | 0 | (void)type; |
677 | | |
678 | | /* Clear the error stack */ |
679 | 0 | H5Eclear2(H5E_DEFAULT); |
680 | | |
681 | | /* Quiet the compiler */ |
682 | 0 | (void)type; |
683 | |
|
684 | 0 | return (file->eof); |
685 | 0 | } /* end H5FD_stdio_get_eof() */ |
686 | | |
687 | | /*------------------------------------------------------------------------- |
688 | | * Function: H5FD_stdio_get_handle |
689 | | * |
690 | | * Purpose: Returns the file handle of stdio file driver. |
691 | | * |
692 | | * Returns: Non-negative if succeed or negative if fails. |
693 | | * |
694 | | *------------------------------------------------------------------------- |
695 | | */ |
696 | | static herr_t |
697 | | H5FD_stdio_get_handle(H5FD_t *_file, hid_t /*UNUSED*/ fapl, void **file_handle) |
698 | 0 | { |
699 | 0 | H5FD_stdio_t *file = (H5FD_stdio_t *)_file; |
700 | | |
701 | | /* Quiet the compiler */ |
702 | 0 | (void)fapl; |
703 | | |
704 | | /* Clear the error stack */ |
705 | 0 | H5Eclear2(H5E_DEFAULT); |
706 | |
|
707 | 0 | *file_handle = &(file->fp); |
708 | 0 | if (*file_handle == NULL) |
709 | 0 | H5Epush_ret(__func__, H5E_ERR_CLS, H5E_IO, H5E_WRITEERROR, "get handle failed", -1); |
710 | | |
711 | 0 | return 0; |
712 | 0 | } /* end H5FD_stdio_get_handle() */ |
713 | | |
714 | | /*------------------------------------------------------------------------- |
715 | | * Function: H5FD_stdio_read |
716 | | * |
717 | | * Purpose: Reads SIZE bytes beginning at address ADDR in file LF and |
718 | | * places them in buffer BUF. Reading past the logical or |
719 | | * physical end of file returns zeros instead of failing. |
720 | | * |
721 | | * Errors: |
722 | | * IO READERROR fread failed. |
723 | | * IO SEEKERROR fseek failed. |
724 | | * |
725 | | * Return: Non-negative on success/Negative on failure |
726 | | * |
727 | | *------------------------------------------------------------------------- |
728 | | */ |
729 | | static herr_t |
730 | | H5FD_stdio_read(H5FD_t *_file, H5FD_mem_t /*UNUSED*/ type, hid_t /*UNUSED*/ dxpl_id, haddr_t addr, |
731 | | size_t size, void /*OUT*/ *buf) |
732 | 0 | { |
733 | 0 | H5FD_stdio_t *file = (H5FD_stdio_t *)_file; |
734 | | |
735 | | /* Quiet the compiler */ |
736 | 0 | (void)type; |
737 | 0 | (void)dxpl_id; |
738 | | |
739 | | /* Clear the error stack */ |
740 | 0 | H5Eclear2(H5E_DEFAULT); |
741 | | |
742 | | /* Check for overflow */ |
743 | 0 | if (HADDR_UNDEF == addr) |
744 | 0 | H5Epush_ret(__func__, H5E_ERR_CLS, H5E_IO, H5E_OVERFLOW, "file address overflowed", -1); |
745 | 0 | if (MY_REGION_OVERFLOW(addr, size)) |
746 | 0 | H5Epush_ret(__func__, H5E_ERR_CLS, H5E_IO, H5E_OVERFLOW, "file address overflowed", -1); |
747 | | |
748 | | /* Check easy cases */ |
749 | 0 | if (0 == size) |
750 | 0 | return 0; |
751 | 0 | if ((haddr_t)addr >= file->eof) { |
752 | 0 | memset(buf, 0, size); |
753 | 0 | return 0; |
754 | 0 | } |
755 | | |
756 | | /* Seek to the correct file position. */ |
757 | 0 | if (!(file->op == H5FD_STDIO_OP_READ || file->op == H5FD_STDIO_OP_SEEK) || file->pos != addr) { |
758 | 0 | if (file_fseek(file->fp, (HDoff_t)addr, SEEK_SET) < 0) { |
759 | 0 | file->op = H5FD_STDIO_OP_UNKNOWN; |
760 | 0 | file->pos = HADDR_UNDEF; |
761 | 0 | H5Epush_ret(__func__, H5E_ERR_CLS, H5E_IO, H5E_SEEKERROR, "fseek failed", -1); |
762 | 0 | } |
763 | 0 | file->pos = addr; |
764 | 0 | } |
765 | | |
766 | | /* Read zeros past the logical end of file (physical is handled below) */ |
767 | 0 | if (addr + size > file->eof) { |
768 | 0 | size_t nbytes = (size_t)(addr + size - file->eof); |
769 | 0 | memset((unsigned char *)buf + size - nbytes, 0, nbytes); |
770 | 0 | size -= nbytes; |
771 | 0 | } |
772 | | |
773 | | /* Read the data. Since we're reading single-byte values, a partial read |
774 | | * will advance the file position by N. If N is zero or an error |
775 | | * occurs then the file position is undefined. |
776 | | */ |
777 | 0 | while (size > 0) { |
778 | |
|
779 | 0 | size_t bytes_in = 0; /* # of bytes to read */ |
780 | 0 | size_t bytes_read = 0; /* # of bytes actually read */ |
781 | 0 | size_t item_size = 1; /* size of items in bytes */ |
782 | |
|
783 | 0 | if (size > H5_STDIO_MAX_IO_BYTES_g) |
784 | 0 | bytes_in = H5_STDIO_MAX_IO_BYTES_g; |
785 | 0 | else |
786 | 0 | bytes_in = size; |
787 | |
|
788 | 0 | bytes_read = fread(buf, item_size, bytes_in, file->fp); |
789 | |
|
790 | 0 | if (0 == bytes_read && ferror(file->fp)) { /* error */ |
791 | 0 | file->op = H5FD_STDIO_OP_UNKNOWN; |
792 | 0 | file->pos = HADDR_UNDEF; |
793 | 0 | H5Epush_ret(__func__, H5E_ERR_CLS, H5E_IO, H5E_READERROR, "fread failed", -1); |
794 | 0 | } /* end if */ |
795 | | |
796 | 0 | if (0 == bytes_read && feof(file->fp)) { |
797 | | /* end of file but not end of format address space */ |
798 | 0 | memset((unsigned char *)buf, 0, size); |
799 | 0 | break; |
800 | 0 | } /* end if */ |
801 | | |
802 | 0 | size -= bytes_read; |
803 | 0 | addr += (haddr_t)bytes_read; |
804 | 0 | buf = (char *)buf + bytes_read; |
805 | 0 | } /* end while */ |
806 | | |
807 | | /* Update the file position data. */ |
808 | 0 | file->op = H5FD_STDIO_OP_READ; |
809 | 0 | file->pos = addr; |
810 | |
|
811 | 0 | return 0; |
812 | 0 | } |
813 | | |
814 | | /*------------------------------------------------------------------------- |
815 | | * Function: H5FD_stdio_write |
816 | | * |
817 | | * Purpose: Writes SIZE bytes from the beginning of BUF into file LF at |
818 | | * file address ADDR. |
819 | | * |
820 | | * Errors: |
821 | | * IO SEEKERROR fseek failed. |
822 | | * IO WRITEERROR fwrite failed. |
823 | | * |
824 | | * Return: Non-negative on success/Negative on failure |
825 | | * |
826 | | *------------------------------------------------------------------------- |
827 | | */ |
828 | | static herr_t |
829 | | H5FD_stdio_write(H5FD_t *_file, H5FD_mem_t /*UNUSED*/ type, hid_t /*UNUSED*/ dxpl_id, haddr_t addr, |
830 | | size_t size, const void *buf) |
831 | 0 | { |
832 | 0 | H5FD_stdio_t *file = (H5FD_stdio_t *)_file; |
833 | | |
834 | | /* Quiet the compiler */ |
835 | 0 | (void)dxpl_id; |
836 | 0 | (void)type; |
837 | | |
838 | | /* Clear the error stack */ |
839 | 0 | H5Eclear2(H5E_DEFAULT); |
840 | | |
841 | | /* Check for overflow conditions */ |
842 | 0 | if (HADDR_UNDEF == addr) |
843 | 0 | H5Epush_ret(__func__, H5E_ERR_CLS, H5E_IO, H5E_OVERFLOW, "file address overflowed", -1); |
844 | 0 | if (MY_REGION_OVERFLOW(addr, size)) |
845 | 0 | H5Epush_ret(__func__, H5E_ERR_CLS, H5E_IO, H5E_OVERFLOW, "file address overflowed", -1); |
846 | | |
847 | | /* Seek to the correct file position. */ |
848 | 0 | if ((file->op != H5FD_STDIO_OP_WRITE && file->op != H5FD_STDIO_OP_SEEK) || file->pos != addr) { |
849 | 0 | if (file_fseek(file->fp, (HDoff_t)addr, SEEK_SET) < 0) { |
850 | 0 | file->op = H5FD_STDIO_OP_UNKNOWN; |
851 | 0 | file->pos = HADDR_UNDEF; |
852 | 0 | H5Epush_ret(__func__, H5E_ERR_CLS, H5E_IO, H5E_SEEKERROR, "fseek failed", -1); |
853 | 0 | } |
854 | 0 | file->pos = addr; |
855 | 0 | } |
856 | | |
857 | | /* Write the buffer. On successful return, the file position will be |
858 | | * advanced by the number of bytes read. On failure, the file position is |
859 | | * undefined. |
860 | | */ |
861 | 0 | while (size > 0) { |
862 | |
|
863 | 0 | size_t bytes_in = 0; /* # of bytes to write */ |
864 | 0 | size_t bytes_wrote = 0; /* # of bytes written */ |
865 | 0 | size_t item_size = 1; /* size of items in bytes */ |
866 | |
|
867 | 0 | if (size > H5_STDIO_MAX_IO_BYTES_g) |
868 | 0 | bytes_in = H5_STDIO_MAX_IO_BYTES_g; |
869 | 0 | else |
870 | 0 | bytes_in = size; |
871 | |
|
872 | 0 | bytes_wrote = fwrite(buf, item_size, bytes_in, file->fp); |
873 | |
|
874 | 0 | if (bytes_wrote != bytes_in || (0 == bytes_wrote && ferror(file->fp))) { /* error */ |
875 | 0 | file->op = H5FD_STDIO_OP_UNKNOWN; |
876 | 0 | file->pos = HADDR_UNDEF; |
877 | 0 | H5Epush_ret(__func__, H5E_ERR_CLS, H5E_IO, H5E_WRITEERROR, "fwrite failed", -1); |
878 | 0 | } /* end if */ |
879 | | |
880 | 0 | assert(bytes_wrote > 0); |
881 | 0 | assert((size_t)bytes_wrote <= size); |
882 | |
|
883 | 0 | size -= bytes_wrote; |
884 | 0 | addr += (haddr_t)bytes_wrote; |
885 | 0 | buf = (const char *)buf + bytes_wrote; |
886 | 0 | } |
887 | | |
888 | | /* Update seek optimizing data. */ |
889 | 0 | file->op = H5FD_STDIO_OP_WRITE; |
890 | 0 | file->pos = addr; |
891 | | |
892 | | /* Update EOF if necessary */ |
893 | 0 | if (file->pos > file->eof) |
894 | 0 | file->eof = file->pos; |
895 | |
|
896 | 0 | return 0; |
897 | 0 | } |
898 | | |
899 | | /*------------------------------------------------------------------------- |
900 | | * Function: H5FD_stdio_flush |
901 | | * |
902 | | * Purpose: Makes sure that all data is on disk. |
903 | | * |
904 | | * Errors: |
905 | | * IO SEEKERROR fseek failed. |
906 | | * IO WRITEERROR fflush or fwrite failed. |
907 | | * |
908 | | * Return: Non-negative on success/Negative on failure |
909 | | * |
910 | | *------------------------------------------------------------------------- |
911 | | */ |
912 | | static herr_t |
913 | | H5FD_stdio_flush(H5FD_t *_file, hid_t /*UNUSED*/ dxpl_id, bool closing) |
914 | 0 | { |
915 | 0 | H5FD_stdio_t *file = (H5FD_stdio_t *)_file; |
916 | | |
917 | | /* Quiet the compiler */ |
918 | 0 | (void)dxpl_id; |
919 | | |
920 | | /* Clear the error stack */ |
921 | 0 | H5Eclear2(H5E_DEFAULT); |
922 | | |
923 | | /* Only try to flush the file if we have write access */ |
924 | 0 | if (file->write_access) { |
925 | 0 | if (!closing) { |
926 | 0 | if (fflush(file->fp) < 0) |
927 | 0 | H5Epush_ret(__func__, H5E_ERR_CLS, H5E_IO, H5E_WRITEERROR, "fflush failed", -1); |
928 | | |
929 | | /* Reset last file I/O information */ |
930 | 0 | file->pos = HADDR_UNDEF; |
931 | 0 | file->op = H5FD_STDIO_OP_UNKNOWN; |
932 | 0 | } /* end if */ |
933 | 0 | } /* end if */ |
934 | | |
935 | 0 | return 0; |
936 | 0 | } /* end H5FD_stdio_flush() */ |
937 | | |
938 | | /*------------------------------------------------------------------------- |
939 | | * Function: H5FD_stdio_truncate |
940 | | * |
941 | | * Purpose: Makes sure that the true file size is the same (or larger) |
942 | | * than the end-of-address. |
943 | | * |
944 | | * Errors: |
945 | | * IO SEEKERROR fseek failed. |
946 | | * IO WRITEERROR fflush or fwrite failed. |
947 | | * |
948 | | * Return: Non-negative on success/Negative on failure |
949 | | * |
950 | | *------------------------------------------------------------------------- |
951 | | */ |
952 | | static herr_t |
953 | | H5FD_stdio_truncate(H5FD_t *_file, hid_t /*UNUSED*/ dxpl_id, bool /*UNUSED*/ closing) |
954 | 0 | { |
955 | 0 | H5FD_stdio_t *file = (H5FD_stdio_t *)_file; |
956 | | |
957 | | /* Quiet the compiler */ |
958 | 0 | (void)dxpl_id; |
959 | 0 | (void)closing; |
960 | | |
961 | | /* Clear the error stack */ |
962 | 0 | H5Eclear2(H5E_DEFAULT); |
963 | | |
964 | | /* Only try to flush the file if we have write access */ |
965 | 0 | if (file->write_access) { |
966 | | /* Makes sure that the true file size is the same as the end-of-address. */ |
967 | 0 | if (file->eoa != file->eof) { |
968 | |
|
969 | | #ifdef H5_HAVE_WIN32_API |
970 | | LARGE_INTEGER li; /* 64-bit (union) integer for SetFilePointer() call */ |
971 | | DWORD dwPtrLow; /* Low-order pointer bits from SetFilePointer() |
972 | | * Only used as an error code here. |
973 | | */ |
974 | | DWORD dwError; /* DWORD error code from GetLastError() */ |
975 | | BOOL bError; /* Boolean error flag */ |
976 | | |
977 | | /* Reset seek offset to beginning of file, so that file isn't re-extended later */ |
978 | | rewind(file->fp); |
979 | | |
980 | | /* Windows uses this odd QuadPart union for 32/64-bit portability */ |
981 | | li.QuadPart = (LONGLONG)file->eoa; |
982 | | |
983 | | /* Extend the file to make sure it's large enough. |
984 | | * |
985 | | * Since INVALID_SET_FILE_POINTER can technically be a valid return value |
986 | | * from SetFilePointer(), we also need to check GetLastError(). |
987 | | */ |
988 | | dwPtrLow = SetFilePointer(file->hFile, li.LowPart, &li.HighPart, FILE_BEGIN); |
989 | | if (INVALID_SET_FILE_POINTER == dwPtrLow) { |
990 | | dwError = GetLastError(); |
991 | | if (dwError != NO_ERROR) |
992 | | H5Epush_ret(__func__, H5E_ERR_CLS, H5E_FILE, H5E_FILEOPEN, "unable to set file pointer", |
993 | | -1); |
994 | | } |
995 | | |
996 | | bError = SetEndOfFile(file->hFile); |
997 | | if (0 == bError) |
998 | | H5Epush_ret(__func__, H5E_ERR_CLS, H5E_IO, H5E_SEEKERROR, |
999 | | "unable to truncate/extend file properly", -1); |
1000 | | #else /* H5_HAVE_WIN32_API */ |
1001 | | /* Reset seek offset to beginning of file, so that file isn't re-extended later */ |
1002 | 0 | rewind(file->fp); |
1003 | | |
1004 | | /* Truncate file to proper length */ |
1005 | 0 | if (-1 == file_ftruncate(file->fd, (HDoff_t)file->eoa)) |
1006 | 0 | H5Epush_ret(__func__, H5E_ERR_CLS, H5E_IO, H5E_SEEKERROR, |
1007 | 0 | "unable to truncate/extend file properly", -1); |
1008 | 0 | #endif /* H5_HAVE_WIN32_API */ |
1009 | | |
1010 | | /* Update the eof value */ |
1011 | 0 | file->eof = file->eoa; |
1012 | | |
1013 | | /* Reset last file I/O information */ |
1014 | 0 | file->pos = HADDR_UNDEF; |
1015 | 0 | file->op = H5FD_STDIO_OP_UNKNOWN; |
1016 | 0 | } /* end if */ |
1017 | 0 | } /* end if */ |
1018 | 0 | else { |
1019 | | /* Double-check for problems */ |
1020 | 0 | if (file->eoa > file->eof) |
1021 | 0 | H5Epush_ret(__func__, H5E_ERR_CLS, H5E_IO, H5E_TRUNCATED, "eoa > eof!", -1); |
1022 | 0 | } /* end else */ |
1023 | | |
1024 | 0 | return 0; |
1025 | 0 | } /* end H5FD_stdio_truncate() */ |
1026 | | |
1027 | | /*------------------------------------------------------------------------- |
1028 | | * Function: H5FD_stdio_lock |
1029 | | * |
1030 | | * Purpose: Lock a file via flock |
1031 | | * NOTE: This function is a no-op if flock() is not present. |
1032 | | * |
1033 | | * Errors: |
1034 | | * IO FCNTL flock failed. |
1035 | | * |
1036 | | * Return: Non-negative on success/Negative on failure |
1037 | | * |
1038 | | *------------------------------------------------------------------------- |
1039 | | */ |
1040 | | static herr_t |
1041 | | H5FD_stdio_lock(H5FD_t *_file, bool rw) |
1042 | 0 | { |
1043 | 0 | #ifdef H5_HAVE_FLOCK |
1044 | 0 | H5FD_stdio_t *file = (H5FD_stdio_t *)_file; /* VFD file struct */ |
1045 | 0 | int lock_flags; /* file locking flags */ |
1046 | | |
1047 | | /* Clear the error stack */ |
1048 | 0 | H5Eclear2(H5E_DEFAULT); |
1049 | |
|
1050 | 0 | assert(file); |
1051 | | |
1052 | | /* Set exclusive or shared lock based on rw status */ |
1053 | 0 | lock_flags = rw ? LOCK_EX : LOCK_SH; |
1054 | | |
1055 | | /* Place a non-blocking lock on the file */ |
1056 | 0 | if (flock(file->fd, lock_flags | LOCK_NB) < 0) { |
1057 | 0 | if (file->ignore_disabled_file_locks && ENOSYS == errno) |
1058 | | /* When errno is set to ENOSYS, the file system does not support |
1059 | | * locking, so ignore it. |
1060 | | */ |
1061 | 0 | errno = 0; |
1062 | 0 | else |
1063 | 0 | H5Epush_ret(__func__, H5E_ERR_CLS, H5E_VFL, H5E_CANTLOCKFILE, "file lock failed", -1); |
1064 | 0 | } /* end if */ |
1065 | | |
1066 | | /* Flush the stream */ |
1067 | 0 | if (fflush(file->fp) < 0) |
1068 | 0 | H5Epush_ret(__func__, H5E_ERR_CLS, H5E_IO, H5E_WRITEERROR, "fflush failed", -1); |
1069 | | |
1070 | 0 | #endif /* H5_HAVE_FLOCK */ |
1071 | | |
1072 | 0 | return 0; |
1073 | 0 | } /* end H5FD_stdio_lock() */ |
1074 | | |
1075 | | /*------------------------------------------------------------------------- |
1076 | | * Function: H5F_stdio_unlock |
1077 | | * |
1078 | | * Purpose: Unlock a file via flock |
1079 | | * NOTE: This function is a no-op if flock() is not present. |
1080 | | * |
1081 | | * Errors: |
1082 | | * IO FCNTL flock failed. |
1083 | | * |
1084 | | * Return: Non-negative on success/Negative on failure |
1085 | | * |
1086 | | *------------------------------------------------------------------------- |
1087 | | */ |
1088 | | static herr_t |
1089 | | H5FD_stdio_unlock(H5FD_t *_file) |
1090 | 0 | { |
1091 | 0 | #ifdef H5_HAVE_FLOCK |
1092 | 0 | H5FD_stdio_t *file = (H5FD_stdio_t *)_file; /* VFD file struct */ |
1093 | | |
1094 | | /* Clear the error stack */ |
1095 | 0 | H5Eclear2(H5E_DEFAULT); |
1096 | |
|
1097 | 0 | assert(file); |
1098 | | |
1099 | | /* Flush the stream */ |
1100 | 0 | if (fflush(file->fp) < 0) |
1101 | 0 | H5Epush_ret(__func__, H5E_ERR_CLS, H5E_IO, H5E_WRITEERROR, "fflush failed", -1); |
1102 | | |
1103 | | /* Place a non-blocking lock on the file */ |
1104 | 0 | if (flock(file->fd, LOCK_UN) < 0) { |
1105 | 0 | if (file->ignore_disabled_file_locks && ENOSYS == errno) |
1106 | | /* When errno is set to ENOSYS, the file system does not support |
1107 | | * locking, so ignore it. |
1108 | | */ |
1109 | 0 | errno = 0; |
1110 | 0 | else |
1111 | 0 | H5Epush_ret(__func__, H5E_ERR_CLS, H5E_VFL, H5E_CANTUNLOCKFILE, "file unlock failed", -1); |
1112 | 0 | } /* end if */ |
1113 | | |
1114 | 0 | #endif /* H5_HAVE_FLOCK */ |
1115 | | |
1116 | 0 | return 0; |
1117 | 0 | } /* end H5FD_stdio_unlock() */ |
1118 | | |
1119 | | /*------------------------------------------------------------------------- |
1120 | | * Function: H5FD_stdio_delete |
1121 | | * |
1122 | | * Purpose: Delete a file |
1123 | | * |
1124 | | * Return: Non-negative on success/Negative on failure |
1125 | | * |
1126 | | *------------------------------------------------------------------------- |
1127 | | */ |
1128 | | static herr_t |
1129 | | H5FD_stdio_delete(const char *filename, hid_t /*UNUSED*/ fapl_id) |
1130 | 0 | { |
1131 | |
|
1132 | 0 | assert(filename); |
1133 | | |
1134 | | /* Quiet compiler */ |
1135 | 0 | (void)fapl_id; |
1136 | | |
1137 | | /* Clear the error stack */ |
1138 | 0 | H5Eclear2(H5E_DEFAULT); |
1139 | | |
1140 | | /* Initialize driver, if it's not yet */ |
1141 | 0 | if (!H5FD_stdio_init_s) |
1142 | 0 | if (H5FD__stdio_init() < 0) |
1143 | 0 | H5Epush_ret(__func__, H5E_ERR_CLS, H5E_VFL, H5E_CANTINIT, "can't initialize driver", -1); |
1144 | | |
1145 | 0 | if (remove(filename) < 0) |
1146 | 0 | H5Epush_ret(__func__, H5E_ERR_CLS, H5E_VFL, H5E_CANTDELETEFILE, "can't delete file)", -1); |
1147 | | |
1148 | 0 | return 0; |
1149 | 0 | } /* end H5FD_stdio_delete() */ |
1150 | | |
1151 | | #ifdef H5private_H |
1152 | | /* |
1153 | | * This is not related to the functionality of the driver code. |
1154 | | * It is added here to trigger warning if HDF5 private definitions are included |
1155 | | * by mistake. The code should use only HDF5 public API and definitions. |
1156 | | */ |
1157 | | #error "Do not use HDF5 private definitions" |
1158 | | #endif |