From: Nguyen Dinh Phi <[email protected]> qemu_ftruncate64() is a general-purpose utility function that may be used outside of the block layer. Move it to util/oslib-win32.c where other Windows-specific utility functions reside.
Signed-off-by: Nguyen Dinh Phi <[email protected]> --- block/file-win32.c | 32 -------------------------------- util/oslib-win32.c | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 32 deletions(-) diff --git a/block/file-win32.c b/block/file-win32.c index af9aea631c..8a44853389 100644 --- a/block/file-win32.c +++ b/block/file-win32.c @@ -170,38 +170,6 @@ static BlockAIOCB *paio_submit(BlockDriverState *bs, HANDLE hfile, return thread_pool_submit_aio(aio_worker, acb, cb, opaque); } -int qemu_ftruncate64(int fd, int64_t length) -{ - LARGE_INTEGER li; - DWORD dw; - LONG high; - HANDLE h; - BOOL res; - - if ((GetVersion() & 0x80000000UL) && (length >> 32) != 0) - return -1; - - h = (HANDLE)_get_osfhandle(fd); - - /* get current position, ftruncate do not change position */ - li.HighPart = 0; - li.LowPart = SetFilePointer (h, 0, &li.HighPart, FILE_CURRENT); - if (li.LowPart == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR) { - return -1; - } - - high = length >> 32; - dw = SetFilePointer(h, (DWORD) length, &high, FILE_BEGIN); - if (dw == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR) { - return -1; - } - res = SetEndOfFile(h); - - /* back to old position */ - SetFilePointer(h, li.LowPart, &li.HighPart, FILE_BEGIN); - return res ? 0 : -1; -} - static int set_sparse(int fd) { DWORD returned; diff --git a/util/oslib-win32.c b/util/oslib-win32.c index 839b8a4170..766c602172 100644 --- a/util/oslib-win32.c +++ b/util/oslib-win32.c @@ -899,3 +899,37 @@ int qemu_shm_alloc(size_t size, Error **errp) error_setg(errp, "Shared memory is not supported."); return -1; } + +int qemu_ftruncate64(int fd, int64_t length) +{ + LARGE_INTEGER li; + DWORD dw; + LONG high; + HANDLE h; + BOOL res; + + if ((GetVersion()&0x80000000UL) && (length >> 32) != 0) { + return -1; + } + + h = (HANDLE)_get_osfhandle(fd); + + /* get current position, ftruncate do not change position */ + li.HighPart = 0; + li.LowPart = SetFilePointer (h, 0, &li.HighPart, FILE_CURRENT); + if (li.LowPart == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR) { + return -1; + } + + high = length >> 32; + dw = SetFilePointer(h, (DWORD) length, &high, FILE_BEGIN); + if (dw == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR) { + return -1; + } + res = SetEndOfFile(h); + + /* back to old position */ + SetFilePointer(h, li.LowPart, &li.HighPart, FILE_BEGIN); + return res ? 0 : -1; +} + -- 2.43.0
