Whamcloud - gitweb
libext2fs: code adaptation to use the Windows IO manager
authorPaulo Antonio Alvarez <pauloaalvarez@gmail.com>
Tue, 22 Dec 2020 18:15:51 +0000 (15:15 -0300)
committerTheodore Ts'o <tytso@mit.edu>
Wed, 31 Mar 2021 20:09:14 +0000 (16:09 -0400)
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
include/mingw/unistd.h
lib/ext2fs/ext2_io.h
lib/ext2fs/getsectsize.c
lib/ext2fs/getsize.c
lib/support/plausible.c
util/subst.c

index 9c0dc81..b201858 100644 (file)
@@ -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
index 95c25a7..8fe5b32 100644 (file)
@@ -142,9 +142,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;
index d6bc376..3a461eb 100644 (file)
  */
 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
 }
index be06775..7265682 100644 (file)
@@ -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;
 }
 
index 024f205..2a3ae14 100644 (file)
@@ -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;
index 66d7d9a..c0eda5c 100644 (file)
@@ -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);