diff mbox series

[2/3] Code adaptation to use the Windows IO manager

Message ID 20201222181552.11267-3-paulo.alvarez@tse.jus.br
State Accepted
Headers show
Series [1/3] Add a Windows implementation of the IO manager | expand

Commit Message

paulo.alvarez@tse.jus.br Dec. 22, 2020, 6:15 p.m. UTC
From: Paulo Antonio Alvarez <pauloaalvarez@gmail.com>

---
 include/mingw/unistd.h   | 51 ++++++++++++++++++++++++++++++++++++----
 lib/ext2fs/ext2_io.h     |  5 ++++
 lib/ext2fs/getsectsize.c | 15 ++++++++++++
 lib/ext2fs/getsize.c     | 16 +++++++++----
 lib/support/plausible.c  |  7 +++++-
 util/subst.c             |  4 ++++
 6 files changed, 88 insertions(+), 10 deletions(-)
diff mbox series

Patch

diff --git a/include/mingw/unistd.h b/include/mingw/unistd.h
index 9c0dc81a..b2018584 100644
--- a/include/mingw/unistd.h
+++ b/include/mingw/unistd.h
@@ -1,13 +1,54 @@ 
-
 #pragma once
 
+// Copyright transferred from Raider Solutions, Inc to
+//   Kern Sibbald and John Walker by express permission.
+//
+// Copyright (C) 2004-2006 Kern Sibbald
+// Copyright (C) 2014 Adam Kropelin
+//
+//   This program is free software; you can redistribute it and/or
+//   modify it under the terms of the GNU General Public License as
+//   published by the Free Software Foundation; either version 2 of
+//   the License, or (at your option) any later version.
+//
+//   This program is distributed in the hope that it will be useful,
+//   but WITHOUT ANY WARRANTY; without even the implied warranty of
+//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+//   General Public License for more details.
+//
+//   You should have received a copy of the GNU General Public
+//   License along with this program; if not, write to the Free
+//   Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
+//   MA 02111-1307, USA.
+
+#ifndef __COMPAT_UNISTD_H_
+#define __COMPAT_UNISTD_H_
+
 #include_next <unistd.h>
 
-__inline __uid_t getuid(void){return 0;}
-__inline int geteuid(void){return 1;}
+#define _PC_PATH_MAX 1
+#define _PC_NAME_MAX 2
+
+#ifdef __cplusplus
+extern "C" {
+#endif
 
-__inline __gid_t getgid(void){return 0;}
-__inline __gid_t getegid(void){return 0;}
+long pathconf(const char *, int);
+#define getpid _getpid
+#define getppid() 0
+
+unsigned int sleep(unsigned int seconds);
+
+#define getuid() 0
+#define getgid() 0
+#define geteuid() 1
+#define getegid() 0
 
 // no-oped sync
 __inline void sync(void){};
+
+#ifdef __cplusplus
+};
+#endif
+
+#endif /* __COMPAT_UNISTD_H_ */
\ No newline at end of file
diff --git a/lib/ext2fs/ext2_io.h b/lib/ext2fs/ext2_io.h
index 5540900a..f605f901 100644
--- a/lib/ext2fs/ext2_io.h
+++ b/lib/ext2fs/ext2_io.h
@@ -139,9 +139,14 @@  extern errcode_t io_channel_cache_readahead(io_channel io,
 					    unsigned long long block,
 					    unsigned long long count);
 
+#ifdef _WIN32
+/* windows_io.c */
+extern io_manager windows_io_manager;
+#else
 /* unix_io.c */
 extern io_manager unix_io_manager;
 extern io_manager unixfd_io_manager;
+#endif
 
 /* sparse_io.c */
 extern io_manager sparse_io_manager;
diff --git a/lib/ext2fs/getsectsize.c b/lib/ext2fs/getsectsize.c
index d6bc3767..3a461eb9 100644
--- a/lib/ext2fs/getsectsize.c
+++ b/lib/ext2fs/getsectsize.c
@@ -51,6 +51,11 @@ 
  */
 errcode_t ext2fs_get_device_sectsize(const char *file, int *sectsize)
 {
+#ifdef _WIN64
+	*sectsize = 512; // just guessing
+	return 0;
+#else // not _WIN64
+
 	int	fd;
 
 	fd = ext2fs_open_file(file, O_RDONLY, 0);
@@ -72,6 +77,8 @@  errcode_t ext2fs_get_device_sectsize(const char *file, int *sectsize)
 	*sectsize = 0;
 	close(fd);
 	return 0;
+
+#endif // ifdef _WIN64
 }
 
 /*
@@ -110,6 +117,12 @@  int ext2fs_get_dio_alignment(int fd)
  */
 errcode_t ext2fs_get_device_phys_sectsize(const char *file, int *sectsize)
 {
+#ifdef _WIN64
+
+	return ext2fs_get_device_sectsize(file, sectsize);
+
+#else // not _WIN64
+
 	int	fd;
 
 	fd = ext2fs_open_file(file, O_RDONLY, 0);
@@ -133,4 +146,6 @@  errcode_t ext2fs_get_device_phys_sectsize(const char *file, int *sectsize)
 	*sectsize = 0;
 	close(fd);
 	return 0;
+
+#endif // ifdef _WIN64
 }
diff --git a/lib/ext2fs/getsize.c b/lib/ext2fs/getsize.c
index be067755..72656821 100644
--- a/lib/ext2fs/getsize.c
+++ b/lib/ext2fs/getsize.c
@@ -71,6 +71,8 @@ 
 #define HAVE_GET_FILE_SIZE_EX 1
 #endif
 
+HANDLE windows_get_handle(io_channel channel);
+
 errcode_t ext2fs_get_device_size2(const char *file, int blocksize,
 				  blk64_t *retblocks)
 {
@@ -84,12 +86,17 @@  errcode_t ext2fs_get_device_size2(const char *file, int blocksize,
 	DWORD filesize;
 #endif /* HAVE_GET_FILE_SIZE_EX */
 
-	dev = CreateFile(file, GENERIC_READ,
-			 FILE_SHARE_READ | FILE_SHARE_WRITE ,
-                	 NULL,  OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,  NULL);
+	io_channel data_io = 0;
+	int retval;
+
+	retval = windows_io_manager->open(file, 0, &data_io);
+	if (retval)
+		return retval;
 
+	dev = windows_get_handle(data_io);
 	if (dev == INVALID_HANDLE_VALUE)
 		return EBADF;
+
 	if (DeviceIoControl(dev, IOCTL_DISK_GET_PARTITION_INFO,
 			    &pi, sizeof(PARTITION_INFORMATION),
 			    &pi, sizeof(PARTITION_INFORMATION),
@@ -120,7 +127,8 @@  errcode_t ext2fs_get_device_size2(const char *file, int blocksize,
 	}
 #endif /* HAVE_GET_FILE_SIZE_EX */
 
-	CloseHandle(dev);
+	windows_io_manager->close(data_io);
+
 	return 0;
 }
 
diff --git a/lib/support/plausible.c b/lib/support/plausible.c
index 024f205e..2a3ae140 100644
--- a/lib/support/plausible.c
+++ b/lib/support/plausible.c
@@ -103,7 +103,12 @@  static void print_ext2_info(const char *device)
 	time_t			tm;
 
 	retval = ext2fs_open2(device, 0, EXT2_FLAG_64BITS, 0, 0,
-			      unix_io_manager, &fs);
+#ifdef _WIN64
+			      windows_io_manager,
+#else
+			      unix_io_manager,
+#endif
+                  &fs);
 	if (retval)
 		return;
 	sb = fs->super;
diff --git a/util/subst.c b/util/subst.c
index 66d7d9a9..c0eda5cf 100644
--- a/util/subst.c
+++ b/util/subst.c
@@ -434,16 +434,20 @@  int main(int argc, char **argv)
 					printf("Using original atime\n");
 				set_utimes(outfn, fileno(old), tv);
 			}
+#ifndef _WIN64
 			if (ofd >= 0)
 				(void) fchmod(ofd, 0444);
+#endif
 			fclose(out);
 			if (unlink(newfn) < 0)
 				perror("unlink");
 		} else {
 			if (verbose)
 				printf("Creating or replacing %s.\n", outfn);
+#ifndef _WIN64
 			if (ofd >= 0)
 				(void) fchmod(ofd, 0444);
+#endif
 			fclose(out);
 			if (old)
 				fclose(old);