Commit beac80cd437fb383eeaccc4224a4d7faebd77a23
1 parent
3587d7e6
Windows sparse file support (Frediano Ziglio)
git-svn-id: svn://svn.savannah.nongnu.org/qemu/trunk@2027 c046a42c-6fe2-441c-8c8c-71466251a162
Showing
3 changed files
with
50 additions
and
2 deletions
Changelog
block.c
... | ... | @@ -761,6 +761,51 @@ static void raw_close(BlockDriverState *bs) |
761 | 761 | close(s->fd); |
762 | 762 | } |
763 | 763 | |
764 | +#ifdef _WIN32 | |
765 | +#include <windows.h> | |
766 | +#include <winioctl.h> | |
767 | + | |
768 | +int qemu_ftruncate64(int fd, int64_t length) | |
769 | +{ | |
770 | + LARGE_INTEGER li; | |
771 | + LONG high; | |
772 | + HANDLE h; | |
773 | + BOOL res; | |
774 | + | |
775 | + if ((GetVersion() & 0x80000000UL) && (length >> 32) != 0) | |
776 | + return -1; | |
777 | + | |
778 | + h = (HANDLE)_get_osfhandle(fd); | |
779 | + | |
780 | + /* get current position, ftruncate do not change position */ | |
781 | + li.HighPart = 0; | |
782 | + li.LowPart = SetFilePointer (h, 0, &li.HighPart, FILE_CURRENT); | |
783 | + if (li.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR) | |
784 | + return -1; | |
785 | + | |
786 | + high = length >> 32; | |
787 | + if (!SetFilePointer(h, (DWORD) length, &high, FILE_BEGIN)) | |
788 | + return -1; | |
789 | + res = SetEndOfFile(h); | |
790 | + | |
791 | + /* back to old position */ | |
792 | + SetFilePointer(h, li.LowPart, &li.HighPart, FILE_BEGIN); | |
793 | + return res ? 0 : -1; | |
794 | +} | |
795 | + | |
796 | +static int set_sparse(int fd) | |
797 | +{ | |
798 | + DWORD returned; | |
799 | + return (int) DeviceIoControl((HANDLE)_get_osfhandle(fd), FSCTL_SET_SPARSE, | |
800 | + NULL, 0, NULL, 0, &returned, NULL); | |
801 | +} | |
802 | +#else | |
803 | +static inline int set_sparse(int fd) | |
804 | +{ | |
805 | + return 1; | |
806 | +} | |
807 | +#endif | |
808 | + | |
764 | 809 | static int raw_create(const char *filename, int64_t total_size, |
765 | 810 | const char *backing_file, int flags) |
766 | 811 | { |
... | ... | @@ -773,6 +818,7 @@ static int raw_create(const char *filename, int64_t total_size, |
773 | 818 | 0644); |
774 | 819 | if (fd < 0) |
775 | 820 | return -EIO; |
821 | + set_sparse(fd); | |
776 | 822 | ftruncate(fd, total_size * 512); |
777 | 823 | close(fd); |
778 | 824 | return 0; | ... | ... |
vl.h
... | ... | @@ -51,8 +51,9 @@ |
51 | 51 | #define fsync _commit |
52 | 52 | #define lseek _lseeki64 |
53 | 53 | #define ENOTSUP 4096 |
54 | -/* XXX: find 64 bit version */ | |
55 | -#define ftruncate chsize | |
54 | +extern int qemu_ftruncate64(int, int64_t); | |
55 | +#define ftruncate qemu_ftruncate64 | |
56 | + | |
56 | 57 | |
57 | 58 | static inline char *realpath(const char *path, char *resolved_path) |
58 | 59 | { | ... | ... |