WordPtr Burger::File::GetMark(void) { WordPtr uMark = 0; HANDLE fp = m_pFile; if (fp) { LARGE_INTEGER uNewPointer; LARGE_INTEGER uOldPointer; uNewPointer.QuadPart = 0; // Get the current file mark BOOL bPositionResult = SetFilePointerEx(fp,uNewPointer,&uOldPointer,FILE_CURRENT); // If no error, restore the old file mark if (bPositionResult) { if (uOldPointer.HighPart) { uMark = 0xFFFFFFFFUL; } else { uMark = uOldPointer.LowPart; } } } return uMark; }
void File::truncate(fileofs size) { if (len() <= size) { return; } LARGE_INTEGER li; li.QuadPart = size; if (SetFilePointerEx(_handle, li, NULL, FILE_BEGIN) == 0) { _bad = true; DWORD dosError = GetLastError(); log() << "In File::truncate(), SetFilePointerEx for '" << _name << "' tried to set the file pointer to " << size << " but failed with " << errnoWithDescription(dosError) << std::endl; return; } if (SetEndOfFile(_handle) == 0) { _bad = true; DWORD dosError = GetLastError(); log() << "In File::truncate(), SetEndOfFile for '" << _name << "' failed with " << errnoWithDescription(dosError) << std::endl; } }
void File::write(fileofs o, const char* data, unsigned len) { LARGE_INTEGER li; li.QuadPart = o; if (SetFilePointerEx(_handle, li, NULL, FILE_BEGIN) == 0) { _bad = true; DWORD dosError = GetLastError(); log() << "In File::write(), SetFilePointerEx for '" << _name << "' tried to set the file pointer to " << o << " but failed with " << errnoWithDescription(dosError) << std::endl; return; } DWORD bytesWritten; if (WriteFile(_handle, data, len, &bytesWritten, NULL) == 0) { _bad = true; DWORD dosError = GetLastError(); log() << "In File::write(), WriteFile for '" << _name << "' tried to write " << len << " bytes but only wrote " << bytesWritten << " bytes, failing with " << errnoWithDescription(dosError) << std::endl; } }
void *__PHYSFS_platformOpenAppend(const char *filename) { void *retval = doOpen(filename, GENERIC_WRITE, OPEN_ALWAYS, 0); if (retval != NULL) { HANDLE h = ((WinApiFile *)retval)->handle; //DWORD rc = SetFilePointer(h, 0, NULL, FILE_END); const LARGE_INTEGER zero = { 0 }; DWORD rc = SetFilePointerEx(h, zero, NULL, FILE_END); if (rc == PHYSFS_INVALID_SET_FILE_POINTER) { const char *err = winApiStrError(); CloseHandle(h); allocator.Free(retval); BAIL_MACRO(err, NULL); } /* if */ } /* if */ return retval; } /* __PHYSFS_platformOpenAppend */
long CRawFile::Tell( void ) const { #ifdef _WIN32 LARGE_INTEGER posToMoveTo; posToMoveTo.LowPart = 0; posToMoveTo.HighPart = 0; LARGE_INTEGER currentPos; BOOL success = SetFilePointerEx( this->m_file, posToMoveTo, ¤tPos, FILE_CURRENT ); if ( success == FALSE ) return -1; return (long)( currentPos.LowPart ); #elif defined(__linux__) return ftell( m_file ); #else return -1; #endif //OS DEPENDANT CODE }
U1 MIOSystem::LoadFile( FileInfo& info,STD_VECTOR<U8>& data ) { data.resize(info.compressize); LARGE_INTEGER fpos; fpos.QuadPart = info.offset; LARGE_INTEGER oldpos; m_CSRead.Enter(); HANDLE h = GetFileReadHandle(info.idx&0xffff); if(h==NULL){ m_CSRead.Leave(); return false; } SetFilePointerEx(h,fpos,&oldpos,FILE_BEGIN); DWORD dwRead=0; ReadFile(h,&data[0],info.compressize,&dwRead,NULL); m_CSRead.Leave(); return true; }
bool C_File::Seek(long long val, SeekMode mode, long long* newdest) { if(m_handle!=INVALID_HANDLE_VALUE) { LARGE_INTEGER dest, ndest; ndest.QuadPart = 0; dest.QuadPart = val; unsigned int mmode = 0; if(mode==Seek_Set) mmode = FILE_BEGIN; else if(mode==Seek_Current) mmode = FILE_CURRENT; else if(mode==Seek_End) mmode = FILE_END; if(SetFilePointerEx(m_handle, dest, &ndest, mmode)) { if(newdest!=0) *newdest = ndest.QuadPart; return true; } } return false; }
HANDLE createfile(const TCHAR *filename, size_w length) { TCHAR tmpfile[MAX_PATH]; const TCHAR *newname = filename; HANDLE hFile; if(filename == 0) { GetTempPath(MAX_PATH, tmpfile); GetTempFileName(tmpfile, TEXT("~HX"), 0, tmpfile); newname = tmpfile; } hFile = CreateFile(newname, GENERIC_READ|GENERIC_WRITE, 0, 0, OPEN_ALWAYS, 0, 0); if(hFile != INVALID_HANDLE_VALUE) { LARGE_INTEGER set; LARGE_INTEGER result; set.QuadPart = length; // allocate space for desired file size if(SetFilePointerEx(hFile, set, &result, FILE_BEGIN)) { if(SetEndOfFile(hFile)) { return hFile; } } CloseHandle(hFile); } if(filename == 0) DeleteFile(tmpfile); return 0; }
static void create_sparse_file(const char *path, const struct sparse *s) { char buff[1024]; HANDLE handle; DWORD dmy; memset(buff, ' ', sizeof(buff)); handle = CreateFileA(path, GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL); assert(handle != INVALID_HANDLE_VALUE); assert(DeviceIoControl(handle, FSCTL_SET_SPARSE, NULL, 0, NULL, 0, &dmy, NULL) != 0); while (s->type != END) { if (s->type == HOLE) { LARGE_INTEGER distance; distance.QuadPart = s->size; assert(SetFilePointerEx(handle, distance, NULL, FILE_CURRENT) != 0); } else { DWORD w, wr; size_t size; size = s->size; while (size) { if (size > sizeof(buff)) w = sizeof(buff); else w = (DWORD)size; assert(WriteFile(handle, buff, w, &wr, NULL) != 0); size -= wr; } } s++; } assertEqualInt(CloseHandle(handle), 1); }
__int64 NSISCALL _dodecomp(__int64 offset, HANDLE hFileOut, unsigned char *outbuf, int outbuflen) { DWORD r; __int64 input_len; __int64 retval; if (offset>=0) { dbd_pos=g_blocks[NB_DATA].offset+offset; SetFilePointerEx(dbd_hFile,(LARGE_INTEGER*)&dbd_pos,NULL,FILE_BEGIN); } retval=__ensuredata(sizeof(__int64)); if (retval<0) return retval; if (!ReadFile(dbd_hFile,(LPVOID)&input_len,sizeof(__int64),&r,NULL) || r!=sizeof(__int64)) return -3; dbd_pos+=sizeof(__int64); retval=__ensuredata(input_len); if (retval < 0) return retval; if (!outbuf) { while (input_len > 0) { DWORD t; DWORD l=min(input_len,IBUFSIZE); if (!ReadFile(dbd_hFile,(LPVOID)_inbuffer,l,&r,NULL) || l != r) return -3; if (!WriteFile(hFileOut,_inbuffer,r,&t,NULL) || t != l) return -2; retval+=r; input_len-=r; dbd_pos+=r; } } else { if (!ReadFile(dbd_hFile,(LPVOID)outbuf,min(input_len,outbuflen),&r,NULL)) return -3; retval=r; dbd_pos+=r; } return retval; }
bool create_very_big_file(_In_ const wchar_t* file_path, _In_ uint32_t size_in_mb) { _ASSERTE(NULL != file_path); if (NULL == file_path) return false; if (is_file_existsW(file_path)) { ::DeleteFileW(file_path); } // create very big file HANDLE file_handle = CreateFile( file_path, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL ); if (INVALID_HANDLE_VALUE == file_handle) { print("err ] CreateFile( %ws ) failed. gle = %u", file_path, GetLastError()); return false; } LARGE_INTEGER file_size = { 0 }; file_size.QuadPart = (uint32_t)(1024 * 1024) * size_in_mb; if (!SetFilePointerEx(file_handle, file_size, NULL, FILE_BEGIN)) { print("err ] SetFilePointerEx() failed. gle = %u", GetLastError()); CloseHandle(file_handle); return false; } SetEndOfFile(file_handle); CloseHandle(file_handle); return true; }
void mDisk::readSector(char* data, long long startingSector, int nBytes, int& numberOfBytesRead) { numberOfBytesRead = 0; int nBytesLeft = nBytes; //pointer to the starting position of data field char* pointer = data; int numberOfBytesToRead; while (nBytesLeft > 0) { if (startingSector > this->ending || startingSector < this->starting) { LARGE_INTEGER offset; offset.QuadPart = startingSector; offset.QuadPart *= 512; SetFilePointerEx(this->vol, offset, 0, FILE_BEGIN); DWORD nOfRead; ReadFile(this->vol, this->disk, (DWORD)MAXSECTOR*512, &nOfRead, 0); this->starting = startingSector; this->ending = this->starting + (nOfRead / 512 + int(nOfRead % 512 > 0)) - 1; } if (this->ending < startingSector) return; //point to the position of startingSector between this->starting and this->ending char* startingPointer = this->disk + (startingSector - this->starting) * 512; int numberOfBytesToCopy = min((this->ending - startingSector + 1) * 512, nBytesLeft); memcpy(pointer, startingPointer, numberOfBytesToCopy); numberOfBytesRead += numberOfBytesToCopy; nBytesLeft -= numberOfBytesToCopy; //shift the pointer to the next position of data field to copy pointer += numberOfBytesToCopy; //shift the starting sector to the next position in disk startingSector += numberOfBytesToCopy / 512 + int(numberOfBytesToCopy % 512 > 0); } }
static NTSTATUS DOKAN_CALLBACK MirrorSetAllocationSize( LPCWSTR FileName, LONGLONG AllocSize, PDOKAN_FILE_INFO DokanFileInfo) { WCHAR filePath[MAX_PATH]; HANDLE handle; LARGE_INTEGER fileSize; GetFilePath(filePath, MAX_PATH, FileName); DbgPrint(L"SetAllocationSize %s, %I64d\n", filePath, AllocSize); handle = (HANDLE)DokanFileInfo->Context; if (!handle || handle == INVALID_HANDLE_VALUE) { DbgPrint(L"\tinvalid handle\n\n"); return STATUS_INVALID_HANDLE; } if (GetFileSizeEx(handle, &fileSize)) { if (AllocSize < fileSize.QuadPart) { fileSize.QuadPart = AllocSize; if (!SetFilePointerEx(handle, fileSize, NULL, FILE_BEGIN)) { DWORD error = GetLastError(); DbgPrint(L"\tSetAllocationSize: SetFilePointer eror: %d, " L"offset = %I64d\n\n", error, AllocSize); return ToNtStatus(error); } if (!SetEndOfFile(handle)) { DWORD error = GetLastError(); DbgPrint(L"\tSetEndOfFile error code = %d\n\n", error); return ToNtStatus(error); } } } else { DWORD error = GetLastError(); DbgPrint(L"\terror code = %d\n\n", error); return ToNtStatus(error); } return STATUS_SUCCESS; }
bool fs::truncate_file(const std::string& path, u64 length) { if (auto device = get_virtual_device(path)) { return device->trunc(path, length); } #ifdef _WIN32 // Open the file const auto handle = CreateFileW(to_wchar(path).get(), GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (handle == INVALID_HANDLE_VALUE) { g_tls_error = to_error(GetLastError()); return false; } LARGE_INTEGER distance; distance.QuadPart = length; // Seek and truncate if (!SetFilePointerEx(handle, distance, NULL, FILE_BEGIN) || !SetEndOfFile(handle)) { g_tls_error = to_error(GetLastError()); CloseHandle(handle); return false; } CloseHandle(handle); return true; #else if (::truncate(path.c_str(), length) != 0) { g_tls_error = to_error(errno); return false; } return true; #endif }
/* * __wt_ftruncate -- * Truncate a file. */ int __wt_ftruncate(WT_SESSION_IMPL *session, WT_FH *fh, wt_off_t len) { WT_DECL_RET; LARGE_INTEGER largeint; largeint.QuadPart = len; if ((ret = SetFilePointerEx( fh->filehandle_secondary, largeint, NULL, FILE_BEGIN)) == FALSE) WT_RET_MSG(session, __wt_errno(), "%s SetFilePointerEx error", fh->name); ret = SetEndOfFile(fh->filehandle_secondary); if (ret != FALSE) return (0); if (GetLastError() == ERROR_USER_MAPPED_FILE) return (EBUSY); WT_RET_MSG(session, __wt_errno(), "%s SetEndOfFile error", fh->name); }
int FSSys::Seek( int fd, SEEK_FILE_MODE mode, seek_t pos, seek_t* pRet, int* err, FSCInfo* info ) { HANDLE* p = this->handles.Handle( fd ); if ( !p ) { SetError( err, ERROR_INVALID_PARAMETER ); return -1; } LARGE_INTEGER li; li.QuadPart = pos; if ( !SetFilePointerEx( *p, li, &li, mode ) ) { SetError( err, GetLastError() ); return -1; } if ( pRet ) { *pRet = li.QuadPart; } return 0; }
VOID WriteDebugLog(LPSTR lpFile, UINT iLine, LPSTR lpFunc, LPWSTR lpMsg, ...) { LARGE_INTEGER FileSize, MoveTo, NewPos; WCHAR szMsg[MAX_STR_LEN * 3]; WCHAR szText[MAX_STR_LEN * 4], szTime[MAX_STR_LEN]; DWORD dwBytesWritten; va_list args; if (!hDebugLog || hDebugLog == INVALID_HANDLE_VALUE) return; MoveTo.QuadPart = 0; if (!SetFilePointerEx(hDebugLog, MoveTo, &NewPos, FILE_END)) return; if (!GetFileSizeEx(hDebugLog, &FileSize)) return; LockFile(hDebugLog, (DWORD_PTR)NewPos.QuadPart, 0, (DWORD_PTR)FileSize.QuadPart, 0); GetTimeFormat(LOCALE_USER_DEFAULT, 0, NULL, NULL, szTime, MAX_STR_LEN); va_start(args, lpMsg); StringCbVPrintf(szMsg, sizeof(szMsg), lpMsg, args); va_end(args); StringCbPrintf(szText, sizeof(szText), L"[%s] %S:%ld %S(): \"%s\"\r\n", szTime, lpFile, iLine, lpFunc, szMsg); WriteFile(hDebugLog, szText, wcslen(szText) * sizeof(WCHAR), &dwBytesWritten, NULL); UnlockFile(hDebugLog, (DWORD_PTR)NewPos.QuadPart, 0, (DWORD_PTR)FileSize.QuadPart, 0); }
static Sint64 SDLCALL windows_file_seek(SDL_RWops * context, Sint64 offset, int whence) { DWORD windowswhence; LARGE_INTEGER windowsoffset; if (!context || context->hidden.windowsio.h == INVALID_HANDLE_VALUE) { SDL_SetError("windows_file_seek: invalid context/file not opened"); return -1; } /* FIXME: We may be able to satisfy the seek within buffered data */ if (whence == RW_SEEK_CUR && context->hidden.windowsio.buffer.left) { offset -= (long)context->hidden.windowsio.buffer.left; } context->hidden.windowsio.buffer.left = 0; switch (whence) { case RW_SEEK_SET: windowswhence = FILE_BEGIN; break; case RW_SEEK_CUR: windowswhence = FILE_CURRENT; break; case RW_SEEK_END: windowswhence = FILE_END; break; default: SDL_SetError("windows_file_seek: Unknown value for 'whence'"); return -1; } windowsoffset.QuadPart = offset; if (!SetFilePointerEx(context->hidden.windowsio.h, windowsoffset, &windowsoffset, windowswhence)) { WIN_SetError("windows_file_seek"); return -1; } return windowsoffset.QuadPart; }
void WfsFileBase::set_size(offset_type newsize) { std::unique_lock<std::mutex> fd_lock(fd_mutex_); offset_type cur_size = _size(); if (!(mode_ & RDONLY)) { LARGE_INTEGER desired_pos; desired_pos.QuadPart = newsize; bool direct_with_bad_size = (mode_& FileBase::DIRECT) && (newsize % bytes_per_sector); if (direct_with_bad_size) { if (!CloseHandle(file_des_)) THRILL_THROW_WIN_LASTERROR(IoError, "closing file (call of ::CloseHandle() from set_size) "); file_des_ = INVALID_HANDLE_VALUE; file_des_ = open_file_impl(filename, WRONLY); } if (!SetFilePointerEx(file_des_, desired_pos, nullptr, FILE_BEGIN)) THRILL_THROW_WIN_LASTERROR(IoError, "SetFilePointerEx() in wfs_file_base::set_size(..) oldsize=" << cur_size << " newsize=" << newsize << " "); if (!SetEndOfFile(file_des_)) THRILL_THROW_WIN_LASTERROR(IoError, "SetEndOfFile() oldsize=" << cur_size << " newsize=" << newsize << " "); if (direct_with_bad_size) { if (!CloseHandle(file_des_)) THRILL_THROW_WIN_LASTERROR(IoError, "closing file (call of ::CloseHandle() from set_size) "); file_des_ = INVALID_HANDLE_VALUE; file_des_ = open_file_impl(filename, mode_ & ~TRUNC); } } }
void initIndex::writeAuthorListToIndexFile() { msgpack::sbuffer buffer; msgpack::packer<msgpack::sbuffer> pk(&buffer); pk.pack_array(AuthorList.size()); for (const auto &i : AuthorList) { pk.pack_array(2); pk.pack(i.first); pk.pack_array(i.second.size()); for (const auto &a : i.second) { pk.pack_array(2); pk.pack(a.first); pk.pack(a.second); } } unsigned int _size = (buffer.size() / 4096 + 1) * 4096; authorListManifest.push_back(_size); HANDLE nIndFile = CreateFile(TEXT("data\\name.ind"), GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); DWORD dwNB; LARGE_INTEGER li = { 0 }; SetFilePointerEx(nIndFile, li, NULL, FILE_END); WriteFile(nIndFile, buffer.data(), _size, &dwNB, NULL); CloseHandle(nIndFile); AuthorList.clear(); }
// IDiaReadExeAtOffsetCallback HRESULT STDMETHODCALLTYPE ReadExecutableAt( DWORDLONG fileOffset, DWORD cbData, DWORD *pcbData, BYTE *pbData) { #ifdef PDBTOTIL if ( exe_reader != NULL ) { uint32 read; bool ok = exe_reader(fileOffset, cbData, pbData, &read); if ( !ok ) return E_FAIL; *pcbData = read; return S_OK; } #endif LARGE_INTEGER pos; pos.QuadPart = (LONGLONG)fileOffset; return hFile == INVALID_HANDLE_VALUE || !SetFilePointerEx(hFile, pos, NULL, FILE_BEGIN) || !ReadFile(hFile, pbData, cbData, pcbData, NULL) ? E_FAIL : S_OK; }
::FLAC__StreamDecoderTellStatus OurDecoder::tell_callback(FLAC__uint64 *absolute_byte_offset) { #ifdef PRWIN_ENV LARGE_INTEGER lpos, zero; zero.QuadPart = 0; BOOL result = SetFilePointerEx(_fp, zero, &lpos, FILE_CURRENT); *absolute_byte_offset = lpos.QuadPart; return (result ? FLAC__STREAM_DECODER_TELL_STATUS_OK : FLAC__STREAM_DECODER_TELL_STATUS_ERROR); #else SInt64 lpos; OSErr result = FSGetForkPosition(CAST_REFNUM(_fp), &lpos); *absolute_byte_offset = lpos; return (result == noErr ? FLAC__STREAM_DECODER_TELL_STATUS_OK : FLAC__STREAM_DECODER_TELL_STATUS_ERROR); #endif }
static int winfs_llseek(struct file *f, loff_t offset, loff_t *newoffset, int whence) { struct winfs_file *winfile = (struct winfs_file *) f; DWORD dwMoveMethod; if (whence == SEEK_SET) dwMoveMethod = FILE_BEGIN; else if (whence == SEEK_CUR) dwMoveMethod = FILE_CURRENT; else if (whence == SEEK_END) dwMoveMethod = FILE_END; else return -EINVAL; LARGE_INTEGER liDistanceToMove, liNewFilePointer; liDistanceToMove.QuadPart = offset; SetFilePointerEx(winfile->handle, liDistanceToMove, &liNewFilePointer, dwMoveMethod); *newoffset = liNewFilePointer.QuadPart; if (whence == SEEK_SET && offset == 0) { /* TODO: Currently we don't know if it is a directory, pretend it is */ winfile->restart_scan = 1; } return 0; }
PHYSFS_sint64 __PHYSFS_platformTell(void *opaque) { HANDLE Handle = ((WinApiFile *)opaque)->handle; PHYSFS_sint64 retval; BOOL rc; LARGE_INTEGER zero; zero.QuadPart = 0; LARGE_INTEGER out; rc = SetFilePointerEx(Handle, zero, &out, FILE_CURRENT); if (!rc) { BAIL_MACRO(errcodeFromWinApi(), -1); } /* if */ else { retval = out.QuadPart; assert(retval >= 0); } /* else */ return retval; } /* __PHYSFS_platformTell */
AP4_Result My_ByteStream::Seek(AP4_Position position) { #ifdef PRWIN_ENV LARGE_INTEGER lpos; lpos.QuadPart = position; #if _MSC_VER < 1300 DWORD pos = SetFilePointer(_fp, lpos.u.LowPart, &lpos.u.HighPart, FILE_BEGIN); BOOL result = (pos != 0xFFFFFFFF || NO_ERROR == GetLastError()); #else BOOL result = SetFilePointerEx(_fp, lpos, NULL, FILE_BEGIN); #endif return (result ? AP4_SUCCESS : AP4_FAILURE); #else OSErr result = FSSetForkPosition(CAST_REFNUM(_fp), fsFromStart, position); return (result == noErr ? AP4_SUCCESS : AP4_FAILURE); #endif }
int ReadPage(ULONG64 StartAddress, unsigned char buffer[]) { HANDLE hDevice; ULONG bytesReturned; LARGE_INTEGER largeStart; hDevice = CreateFileA("\\\\.\\pmem", GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if (hDevice == INVALID_HANDLE_VALUE) return 1; largeStart.QuadPart = StartAddress; if (0xFFFFFFFF == SetFilePointerEx(hDevice, largeStart, NULL, FILE_BEGIN)) { CloseHandle(hDevice); return 3; } if (!ReadFile(hDevice, buffer, 4096, &bytesReturned, NULL)) { CloseHandle(hDevice); return 4; } CloseHandle(hDevice); return 0; }
DWORD CGameUpdate::OpenFile(tagIdxFile* pFile, utility::CAutoFile& clFile, BOOL& bUseNoBuf) { wchar_t FileName[MAX_PATH] = {0}; bUseNoBuf = pFile->size >= 0x10000; DWORD flg = (bUseNoBuf ? FILE_FLAG_NO_BUFFERING : 0) | FILE_FLAG_OVERLAPPED | FILE_FLAG_SEQUENTIAL_SCAN; swprintf_s(FileName, L"%s%s", m_strDevPath.c_str(), pFile->name.c_str()); SetFileAttributesW(FileName, FILE_ATTRIBUTE_NORMAL); clFile = CreateFileW(FileName, GENERIC_WRITE, FILE_SHARE_WRITE|FILE_SHARE_READ, NULL, OPEN_ALWAYS, flg, NULL); if (clFile.Get() == INVALID_HANDLE_VALUE) return GetLastError(); LARGE_INTEGER liSize; liSize.QuadPart = bUseNoBuf ? ALIGN_SIZE(pFile->size, 512) : pFile->size; if (!SetFilePointerEx(clFile, liSize, NULL, FILE_BEGIN) || !SetEndOfFile(clFile) || !SetFileAttributesW(FileName, FILE_ATTRIBUTE_NORMAL)) { return GetLastError(); } return ERROR_SUCCESS; }
static void StartDebugLog() { wstring iniPath; GetModuleIniPath(iniPath); if( GetPrivateProfileInt(L"SET", L"SaveDebugLog", 0, iniPath.c_str()) != 0 ){ wstring logPath; GetModuleFolderPath(logPath); logPath += L"\\EpgTimerSrvDebugLog.txt"; g_hDebugLog = CreateFile(logPath.c_str(), FILE_APPEND_DATA, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if( g_hDebugLog != INVALID_HANDLE_VALUE ){ if( GetLastError() == ERROR_SUCCESS ){ DWORD dwWritten; WriteFile(g_hDebugLog, "\xFF\xFE", sizeof(char) * 2, &dwWritten, NULL); }else{ LARGE_INTEGER liPos = {}; SetFilePointerEx(g_hDebugLog, liPos, NULL, FILE_END); } InitializeCriticalSection(&g_debugLogLock); g_saveDebugLog = true; OutputDebugString(L"****** LOG START ******\r\n"); } } }
/* * __win_file_set_end -- * Truncate or extend a file. */ static int __win_file_set_end( WT_FILE_HANDLE *file_handle, WT_SESSION *wt_session, wt_off_t len) { DWORD windows_error; WT_FILE_HANDLE_WIN *win_fh; WT_SESSION_IMPL *session; LARGE_INTEGER largeint; win_fh = (WT_FILE_HANDLE_WIN *)file_handle; session = (WT_SESSION_IMPL *)wt_session; largeint.QuadPart = len; if (win_fh->filehandle_secondary == INVALID_HANDLE_VALUE) WT_RET_MSG(session, EINVAL, "%s: handle-set-end: no secondary handle", file_handle->name); if (SetFilePointerEx(win_fh->filehandle_secondary, largeint, NULL, FILE_BEGIN) == FALSE) { windows_error = __wt_getlasterror(); __wt_errx(session, "%s: handle-set-end: SetFilePointerEx: %s", file_handle->name, __wt_formatmessage(session, windows_error)); return (__wt_map_windows_error(windows_error)); } if (SetEndOfFile(win_fh->filehandle_secondary) == FALSE) { if (GetLastError() == ERROR_USER_MAPPED_FILE) return (EBUSY); windows_error = __wt_getlasterror(); __wt_errx(session, "%s: handle-set-end: SetEndOfFile: %s", file_handle->name, __wt_formatmessage(session, windows_error)); return (__wt_map_windows_error(windows_error)); } return (0); }
/** * this function tries to make a particular range of a file allocated (corresponding to disk space) * it is advisory, and the range specified in the arguments will never contain live data */ void AllocateFileRange(FILE *file, unsigned int offset, unsigned int length) { #if defined(WIN32) // Windows-specific version HANDLE hFile = (HANDLE)_get_osfhandle(_fileno(file)); LARGE_INTEGER nFileSize; int64_t nEndPos = (int64_t)offset + length; nFileSize.u.LowPart = nEndPos & 0xFFFFFFFF; nFileSize.u.HighPart = nEndPos >> 32; SetFilePointerEx(hFile, nFileSize, 0, FILE_BEGIN); SetEndOfFile(hFile); #elif defined(MAC_OSX) // OSX specific version fstore_t fst; fst.fst_flags = F_ALLOCATECONTIG; fst.fst_posmode = F_PEOFPOSMODE; fst.fst_offset = 0; fst.fst_length = (off_t)offset + length; fst.fst_bytesalloc = 0; if (fcntl(fileno(file), F_PREALLOCATE, &fst) == -1) { fst.fst_flags = F_ALLOCATEALL; fcntl(fileno(file), F_PREALLOCATE, &fst); } ftruncate(fileno(file), fst.fst_length); #elif defined(__linux__) // Version using posix_fallocate off_t nEndPos = (off_t)offset + length; posix_fallocate(fileno(file), 0, nEndPos); #else // Fallback version // TODO: just write one byte per block static const char buf[65536] = {}; fseek(file, offset, SEEK_SET); while (length > 0) { unsigned int now = 65536; if (length < now) now = length; fwrite(buf, 1, now, file); // allowed to fail; this function is advisory anyway length -= now; } #endif }