From e6680912a298dc2dee83f41837cb52160cf562d9 Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Tue, 24 Mar 2020 16:37:13 +0000 Subject: dd: create a sparse file when seek=N is used When the seek=N argument is used mark the file as sparse and set the range that is sparse. See https://stackoverflow.com/questions/4011508/how-to-create-a-sparse-file-on-ntfs --- coreutils/dd.c | 3 +++ include/mingw.h | 1 + win32/mingw.c | 17 +++++++++++++++++ 3 files changed, 21 insertions(+) diff --git a/coreutils/dd.c b/coreutils/dd.c index 3054ec6ea..fc8b1dbb2 100644 --- a/coreutils/dd.c +++ b/coreutils/dd.c @@ -502,6 +502,9 @@ int dd_main(int argc UNUSED_PARAM, char **argv) } if (seek) { size_t blocksz = (G.flags & FLAG_SEEK_BYTES) ? 1 : obs; +#if ENABLE_PLATFORM_MINGW32 + seek_sparse(ofd, seek * blocksz); +#endif if (lseek(ofd, seek * blocksz, SEEK_CUR) < 0) goto die_outfile; } diff --git a/include/mingw.h b/include/mingw.h index 3ab49a920..a5a84cff2 100644 --- a/include/mingw.h +++ b/include/mingw.h @@ -533,3 +533,4 @@ int chdir_system_drive(void); char *xabsolute_path(char *path); char *get_drive_cwd(const char *path, char *buffer, int size); void fix_path_case(char *path); +void seek_sparse(int fd, size_t size); diff --git a/win32/mingw.c b/win32/mingw.c index 894388b64..7f8fecdc3 100644 --- a/win32/mingw.c +++ b/win32/mingw.c @@ -1750,3 +1750,20 @@ void fix_path_case(char *path) } } } + +void seek_sparse(int fd, size_t size) +{ + DWORD dwTemp; + HANDLE fh; + FILE_ZERO_DATA_INFORMATION fzdi; + + if ((fh=(HANDLE)_get_osfhandle(fd)) == INVALID_HANDLE_VALUE) + return; + + DeviceIoControl(fh, FSCTL_SET_SPARSE, NULL, 0, NULL, 0, &dwTemp, NULL); + + fzdi.FileOffset.QuadPart = 0; + fzdi.BeyondFinalZero.QuadPart = size; + DeviceIoControl(fh, FSCTL_SET_ZERO_DATA, &fzdi, sizeof(fzdi), + NULL, 0, &dwTemp, NULL); +} -- cgit v1.2.3-55-g6feb