/*****************************************************************************\ * Copyright (c) 2004 Mark Aylett * * * * Permission is hereby granted, free of charge, to any person obtaining a * * copy of this software and associated documentation files (the * * "Software"), to deal in the Software without restriction, including * * without limitation the rights to use, copy, modify, merge, publish, * * distribute, sublicense, and/or sell copies of the Software, and to permit * * persons to whom the Software is furnished to do so, subject to the * * following conditions: * * * * The above copyright notice and this permission notice shall be included * * in all copies or substantial portions of the Software. * * * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN * * NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE * * USE OR OTHER DEALINGS IN THE SOFTWARE. * \*****************************************************************************/ static const char rcsid[] = "$Id: mar_file_c.c,v 1.3 2004/12/14 13:09:23 marayl Exp $"; #include "mar_file_c.h" #include "mar_debug_c.h" #include "mar_flags_c.h" #include "mar_format_c.h" #include #include #include #if !defined(WIN32) #include "unix/mar_file_c.c" #else /* WIN32 */ #include "win/mar_file_c.c" #endif /* WIN32 */ MAR_EXTERN int mar_closefile_(int fd) { return close(fd); } MAR_EXTERN int mar_openfile_(const char* path, int flags, mode_t mode) { int local; if (-1 == mar_toflags_(&local, flags)) return -1; return open(path, local, mode); } MAR_EXTERN int mar_extendfile_(int fd, size_t size) { /** * The lseek function can be used to move the file pointer beyond the end * of the file, in which case any subsequent write operation should fill * the gap between the current end of file and the file pointer with zeros. * * Bug: On Windows 95/98 the gap is filled with garbage instead of zeros. */ static const mar_byte_t ZERO = 0; off_t cur; int err; if (!size) return 0; /** * Store current file pointer for later restoration. */ if (-1 == (cur = lseek(fd, 0, SEEK_CUR))) { MAR_DEBUG_MSG("Failed to extend file:" " Bad call to lseek\n"); return -1; } if (-1 == lseek(fd, (off_t)(size - 1), SEEK_END)) { MAR_DEBUG_MSG("Failed to extend file:" " Bad call to lseek\n"); return -1; } if (1 != write(fd, &ZERO, 1)) { MAR_DEBUG_MSG("Failed to extend file:" " Bad call to write\n"); goto fail; } /** * Ensure that gap is filled with zeros. */ if (-1 == fsync(fd)) { MAR_DEBUG_MSG("Failed to extend file:" " Bad call to fsync\n"); goto fail; } /** * Restore original file pointer. */ if (-1 == lseek(fd, cur, SEEK_SET)) { MAR_DEBUG_MSG("Failed to extend file:" " Bad call to lseek\n"); return -1; } return 0; fail: err = errno; lseek(fd, cur, SEEK_SET); errno = err; return -1; } MAR_EXTERN int mar_syncfile_(int fd) { int ret = fsync(fd); MAR_DEBUG_MSG("Failed to sync file:" " Bad call to fsync\n"); return ret; } MAR_EXTERN int mar_truncatefile_(int fd, off_t size) { int ret = ftruncate(fd, size); MAR_DEBUG_MSG("Failed to truncate file:" " Bad call to ftruncate\n"); return ret; } MAR_EXTERN int mar_filesize_(int fd, size_t* size) { assert(size); return mar_dofilesize_(fd, size); }