[newlib-cygwin/main] Cygwin: fhandler: rename ftruncate method to fallocate

Corinna Vinschen corinna@sourceware.org
Tue Nov 28 10:02:55 GMT 2023


https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=f3554bf8905bfca5fbe54e57a452196bd9499cea

commit f3554bf8905bfca5fbe54e57a452196bd9499cea
Author:     Corinna Vinschen <corinna@vinschen.de>
AuthorDate: Sun Nov 26 12:50:38 2023 +0100
Commit:     Corinna Vinschen <corinna@vinschen.de>
CommitDate: Tue Nov 28 10:52:05 2023 +0100

    Cygwin: fhandler: rename ftruncate method to fallocate
    
    also, take mode flags parameter instead of just a bool.
    Introduce __FALLOC_FL_TRUNCATE mode flag as internal flag to
    indictae being called from ftruncate(2).
    
    This is in preparation of an upcoming change introducing the
    Linx-specific fallocate(2) call.
    
    Signed-off-by: Corinna Vinschen <corinna@vinschen.de>

Diff:
---
 winsup/cygwin/fhandler/base.cc          |  2 +-
 winsup/cygwin/fhandler/disk_file.cc     | 16 +++++++++-------
 winsup/cygwin/fhandler/pipe.cc          |  4 ++--
 winsup/cygwin/include/fcntl.h           |  3 +++
 winsup/cygwin/local_includes/fhandler.h |  8 ++++----
 winsup/cygwin/syscalls.cc               |  4 ++--
 6 files changed, 21 insertions(+), 16 deletions(-)

diff --git a/winsup/cygwin/fhandler/base.cc b/winsup/cygwin/fhandler/base.cc
index 3f0dc5f69847..d859870cb40d 100644
--- a/winsup/cygwin/fhandler/base.cc
+++ b/winsup/cygwin/fhandler/base.cc
@@ -1795,7 +1795,7 @@ fhandler_base::fadvise (off_t offset, off_t length, int advice)
 }
 
 int
-fhandler_base::ftruncate (off_t length, bool allow_truncate)
+fhandler_base::fallocate (int mode, off_t offset, off_t length)
 {
   return ENODEV;
 }
diff --git a/winsup/cygwin/fhandler/disk_file.cc b/winsup/cygwin/fhandler/disk_file.cc
index 245e9bb75dc8..51602f30f10a 100644
--- a/winsup/cygwin/fhandler/disk_file.cc
+++ b/winsup/cygwin/fhandler/disk_file.cc
@@ -25,6 +25,7 @@ details. */
 #include "devices.h"
 #include "ldap.h"
 #include <aio.h>
+#include <fcntl.h>
 #include <cygwin/fs.h>
 
 #define _LIBC
@@ -1130,7 +1131,7 @@ fhandler_disk_file::fadvise (off_t offset, off_t length, int advice)
 }
 
 int
-fhandler_disk_file::ftruncate (off_t length, bool allow_truncate)
+fhandler_disk_file::fallocate (int mode, off_t offset, off_t length)
 {
   int res = 0;
 
@@ -1152,17 +1153,18 @@ fhandler_disk_file::ftruncate (off_t length, bool allow_truncate)
       if (!NT_SUCCESS (status))
 	return geterrno_from_nt_status (status);
 
-      /* If called through posix_fallocate, silently succeed if length
-	 is less than the file's actual length. */
-      if (!allow_truncate && length < fsi.EndOfFile.QuadPart)
+      /* If called through posix_fallocate, silently succeed if
+	 offset + length is less than the file's actual length. */
+      if (mode == 0 && offset + length < fsi.EndOfFile.QuadPart)
 	return 0;
 
-      feofi.EndOfFile.QuadPart = length;
+      feofi.EndOfFile.QuadPart = offset + length;
       /* Create sparse files only when called through ftruncate, not when
 	 called through posix_fallocate. */
-      if (allow_truncate && pc.support_sparse ()
+      if ((mode & __FALLOC_FL_TRUNCATE)
 	  && !has_attribute (FILE_ATTRIBUTE_SPARSE_FILE)
-	  && length >= fsi.EndOfFile.QuadPart + (128 * 1024))
+	  && pc.support_sparse ()
+	  && offset + length >= fsi.EndOfFile.QuadPart + (128 * 1024))
 	{
 	  status = NtFsControlFile (get_handle (), NULL, NULL, NULL, &io,
 				    FSCTL_SET_SPARSE, NULL, 0, NULL, 0);
diff --git a/winsup/cygwin/fhandler/pipe.cc b/winsup/cygwin/fhandler/pipe.cc
index e72ee65ca80a..1a97108b5d62 100644
--- a/winsup/cygwin/fhandler/pipe.cc
+++ b/winsup/cygwin/fhandler/pipe.cc
@@ -247,9 +247,9 @@ fhandler_pipe::fadvise (off_t offset, off_t length, int advice)
 }
 
 int
-fhandler_pipe::ftruncate (off_t length, bool allow_truncate)
+fhandler_pipe::fallocate (int mode, off_t offset, off_t length)
 {
-  return allow_truncate ? EINVAL : ESPIPE;
+  return (mode & __FALLOC_FL_TRUNCATE) ? EINVAL : ESPIPE;
 }
 
 char *
diff --git a/winsup/cygwin/include/fcntl.h b/winsup/cygwin/include/fcntl.h
index ed396eab64bd..de64d4f7c010 100644
--- a/winsup/cygwin/include/fcntl.h
+++ b/winsup/cygwin/include/fcntl.h
@@ -40,9 +40,12 @@ details. */
 #define POSIX_FADV_DONTNEED	4
 #define POSIX_FADV_NOREUSE	5
 
+#define __FALLOC_FL_TRUNCATE		0x0001	/* internal */
+
 #ifdef __cplusplus
 extern "C" {
 #endif
+
 extern int posix_fadvise (int, off_t, off_t, int);
 extern int posix_fallocate (int, off_t, off_t);
 #ifdef __cplusplus
diff --git a/winsup/cygwin/local_includes/fhandler.h b/winsup/cygwin/local_includes/fhandler.h
index ca685a627642..54e0c6e804c8 100644
--- a/winsup/cygwin/local_includes/fhandler.h
+++ b/winsup/cygwin/local_includes/fhandler.h
@@ -389,7 +389,7 @@ public:
   virtual ssize_t fgetxattr (const char *, void *, size_t);
   virtual int fsetxattr (const char *, const void *, size_t, int);
   virtual int fadvise (off_t, off_t, int);
-  virtual int ftruncate (off_t, bool);
+  virtual int fallocate (int, off_t, off_t);
   virtual int link (const char *);
   virtual int utimens (const struct timespec *);
   virtual int fsync ();
@@ -1222,7 +1222,7 @@ public:
   int fstat (struct stat *buf);
   int fstatvfs (struct statvfs *buf);
   int fadvise (off_t, off_t, int);
-  int ftruncate (off_t, bool);
+  int fallocate (int, off_t, off_t);
   int init (HANDLE, DWORD, mode_t, int64_t);
   static int create (fhandler_pipe *[2], unsigned, int);
   static DWORD create (LPSECURITY_ATTRIBUTES, HANDLE *, HANDLE *, DWORD,
@@ -1727,7 +1727,7 @@ class fhandler_disk_file: public fhandler_base
   ssize_t fgetxattr (const char *, void *, size_t);
   int fsetxattr (const char *, const void *, size_t, int);
   int fadvise (off_t, off_t, int);
-  int ftruncate (off_t, bool);
+  int fallocate (int, off_t, off_t);
   int link (const char *);
   int utimens (const struct timespec *);
   int fstatvfs (struct statvfs *buf);
@@ -3413,7 +3413,7 @@ public:
   ssize_t fgetxattr (const char *, void *, size_t) NO_IMPL;
   int fsetxattr (const char *, const void *, size_t, int) NO_IMPL;
   int fadvise (off_t, off_t, int) NO_IMPL;
-  int ftruncate (off_t, bool) NO_IMPL;
+  int fallocate (int, off_t, off_t) NO_IMPL;
   int link (const char *) NO_IMPL;
   int mkdir (mode_t) NO_IMPL;
   ssize_t pread (void *, size_t, off_t, void *aio = NULL) NO_IMPL;
diff --git a/winsup/cygwin/syscalls.cc b/winsup/cygwin/syscalls.cc
index c3c17d604af3..b2e6ba16ca04 100644
--- a/winsup/cygwin/syscalls.cc
+++ b/winsup/cygwin/syscalls.cc
@@ -2997,7 +2997,7 @@ posix_fallocate (int fd, off_t offset, off_t len)
     {
       cygheap_fdget cfd (fd);
       if (cfd >= 0)
-	res = cfd->ftruncate (offset + len, false);
+	res = cfd->fallocate (0, offset, len);
       else
 	res = EBADF;
       if (res == EISDIR)
@@ -3014,7 +3014,7 @@ ftruncate (int fd, off_t length)
   cygheap_fdget cfd (fd);
   if (cfd >= 0)
     {
-      res = cfd->ftruncate (length, true);
+      res = cfd->fallocate (__FALLOC_FL_TRUNCATE, 0, length);
       if (res)
 	{
 	  if (res == ENODEV)


More information about the Cygwin-cvs mailing list