int64_t omrfile_seek(struct OMRPortLibrary *portLibrary, intptr_t fd, int64_t offset, int32_t whence) { DWORD moveMethod; LARGE_INTEGER liOffset; int64_t result; int32_t error; Trc_PRT_file_seek_Entry(fd, offset, whence); liOffset.QuadPart = offset; if ((whence < EsSeekSet) || (whence > EsSeekEnd)) { Trc_PRT_file_seek_Exit(-1); return -1; } if (whence == EsSeekSet) { moveMethod = FILE_BEGIN; } if (whence == EsSeekEnd) { moveMethod = FILE_END; } if (whence == EsSeekCur) { moveMethod = FILE_CURRENT; } liOffset.LowPart = SetFilePointer((HANDLE)fd, liOffset.LowPart, &liOffset.HighPart, moveMethod); if (INVALID_SET_FILE_POINTER == liOffset.LowPart) { error = GetLastError(); if (error != NO_ERROR) { portLibrary->error_set_last_error(portLibrary, error, findError(error)); Trc_PRT_file_seek_Exit(-1); return -1; } } result = (int64_t)liOffset.QuadPart; Trc_PRT_file_seek_Exit(result); return result; }
/** * Repositions the offset of the file descriptor to a given offset as per directive whence. * * @param[in] portLibrary The port library * @param[in] fd The file descriptor. * @param[in] offset The offset in the file to position to. * @param[in] whence Portable constant describing how to apply the offset. * * @return The resulting offset on success, -1 on failure. * @note whence is one of EsSeekSet (seek from beginning of file), * EsSeekCur (seek from current file pointer) or EsSeekEnd (seek backwards from * end of file). * @internal @note seek operations return -1 on failure. Negative offsets * can be returned when seeking beyond end of file. */ int64_t omrfile_seek(struct OMRPortLibrary *portLibrary, intptr_t inFD, int64_t offset, int32_t whence) { int fd = (int)inFD; off_t localOffset = (off_t)offset; int64_t result; Trc_PRT_file_seek_Entry(fd, offset, whence); if ((whence < EsSeekSet) || (whence > EsSeekEnd)) { int32_t errorCode = portLibrary->error_set_last_error(portLibrary, -1, OMRPORT_ERROR_FILE_INVAL); Trc_PRT_file_seek_Exit(errorCode); return -1; } /* If file offsets are 32 bit, truncate the seek to that range */ if (sizeof(off_t) < sizeof(int64_t)) { if (offset > 0x7FFFFFFF) { localOffset = 0x7FFFFFFF; } else if (offset < -0x7FFFFFFF) { localOffset = -0x7FFFFFFF; } } #if (FD_BIAS != 0) if (fd < FD_BIAS) { /* Cannot seek on STD streams, and no other FD's should exist <FD_BIAS */ int32_t errorCode = portLibrary->error_set_last_error(portLibrary, -1, OMRPORT_ERROR_FILE_BADF); Trc_PRT_file_seek_Exit(errorCode); return -1; } #endif result = (int64_t)lseek(fd - FD_BIAS, localOffset, whence); if (-1 == result) { portLibrary->error_set_last_error(portLibrary, errno, findError(errno)); } Trc_PRT_file_seek_Exit(result); return result; }