summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEdward Thomson <ethomson@edwardthomson.com>2019-02-22 11:22:28 +0000
committerEdward Thomson <ethomson@edwardthomson.com>2019-02-22 14:41:29 +0000
commit32f50452073c2cb76b36428017d7f547ef39791b (patch)
tree107ba6436eb065e83ca59e0c595ed27863b8bf39
parent4e3949b73e2dfcc0dfe29c1f39c813769772d44f (diff)
downloadlibgit2-32f50452073c2cb76b36428017d7f547ef39791b.tar.gz
p_fallocate: add Windows emulation
Emulate `p_fallocate` on Windows by seeking beyond the end of the file and setting the size to the current seek position.
-rw-r--r--src/win32/posix_w32.c50
1 files changed, 48 insertions, 2 deletions
diff --git a/src/win32/posix_w32.c b/src/win32/posix_w32.c
index 835804de4..fcaf77e89 100644
--- a/src/win32/posix_w32.c
+++ b/src/win32/posix_w32.c
@@ -518,8 +518,54 @@ int p_creat(const char *path, mode_t mode)
int p_fallocate(int fd, off_t offset, off_t len)
{
- error = ENOSYS;
- return -1;
+ HANDLE fh = (HANDLE)_get_osfhandle(fd);
+ LARGE_INTEGER zero, position, oldsize, newsize;
+ size_t size;
+
+ if (fh == INVALID_HANDLE_VALUE) {
+ errno = EBADF;
+ return -1;
+ }
+
+ if (offset < 0 || len <= 0) {
+ errno = EINVAL;
+ return -1;
+ }
+
+ if (git__add_sizet_overflow(&size, offset, len)) {
+ errno = EINVAL;
+ return -1;
+ }
+
+ zero.u.LowPart = 0;
+ zero.u.HighPart = 0;
+
+ newsize.u.LowPart = (size & 0xffffffff);
+
+#if (SIZE_MAX > UINT32_MAX)
+ newsize.u.HighPart = size >> 32;
+#else
+ newsize.u.HighPart = 0;
+#endif
+
+ if (!GetFileSizeEx(fh, &oldsize)) {
+ set_errno();
+ return -1;
+ }
+
+ /* POSIX emulation: attempting to shrink the file is ignored */
+ if (oldsize.QuadPart >= newsize.QuadPart)
+ return 0;
+
+ if (!SetFilePointerEx(fh, zero, &position, FILE_CURRENT) ||
+ !SetFilePointerEx(fh, newsize, NULL, FILE_BEGIN) ||
+ !SetEndOfFile(fh) ||
+ !SetFilePointerEx(fh, position, 0, FILE_BEGIN)) {
+ set_errno();
+ return -1;
+ }
+
+ return 0;
}
int p_utimes(const char *path, const struct p_timeval times[2])