From 0d6f2b0117aa7fe5470117b6a43f16dac139f5b9 Mon Sep 17 00:00:00 2001 From: Corinna Vinschen Date: Sat, 2 Apr 2011 11:43:43 +0000 Subject: [PATCH] * autoload.cc (FindFirstVolumeA): Remove. (FindNextVolumeA): Remove. (FindVolumeClose): Remove. (GetVolumeNameForVolumeMountPointA): Remove. * ntdll.h (NtFlushBuffersFile): Declare. * syscalls.cc (sync_worker): Rewrite using native NT functions. (sync): Ditto. --- winsup/cygwin/ChangeLog | 10 ++++ winsup/cygwin/autoload.cc | 4 -- winsup/cygwin/ntdll.h | 1 + winsup/cygwin/syscalls.cc | 106 ++++++++++++++++---------------------- 4 files changed, 55 insertions(+), 66 deletions(-) diff --git a/winsup/cygwin/ChangeLog b/winsup/cygwin/ChangeLog index e6d859e49..12f25c3ff 100644 --- a/winsup/cygwin/ChangeLog +++ b/winsup/cygwin/ChangeLog @@ -1,3 +1,13 @@ +2011-04-02 Corinna Vinschen + + * autoload.cc (FindFirstVolumeA): Remove. + (FindNextVolumeA): Remove. + (FindVolumeClose): Remove. + (GetVolumeNameForVolumeMountPointA): Remove. + * ntdll.h (NtFlushBuffersFile): Declare. + * syscalls.cc (sync_worker): Rewrite using native NT functions. + (sync): Ditto. + 2011-04-02 Corinna Vinschen * fhandler_proc.cc (format_proc_partitions): Express length in WCHAR diff --git a/winsup/cygwin/autoload.cc b/winsup/cygwin/autoload.cc index b57079d7f..54c4cab32 100644 --- a/winsup/cygwin/autoload.cc +++ b/winsup/cygwin/autoload.cc @@ -395,13 +395,9 @@ LoadDLLfunc (GetNetworkParams, 8, iphlpapi) LoadDLLfunc (GetUdpTable, 12, iphlpapi) LoadDLLfuncEx (AttachConsole, 4, kernel32, 1) -LoadDLLfunc (FindFirstVolumeA, 8, kernel32) -LoadDLLfunc (FindNextVolumeA, 12, kernel32) -LoadDLLfunc (FindVolumeClose, 4, kernel32) LoadDLLfunc (GetConsoleWindow, 0, kernel32) LoadDLLfuncEx (GetNamedPipeClientProcessId, 8, kernel32, 1) LoadDLLfuncEx (GetSystemWindowsDirectoryW, 8, kernel32, 1) -LoadDLLfunc (GetVolumeNameForVolumeMountPointA, 12, kernel32) LoadDLLfunc (LocaleNameToLCID, 8, kernel32) LoadDLLfunc (WNetCloseEnum, 4, mpr) diff --git a/winsup/cygwin/ntdll.h b/winsup/cygwin/ntdll.h index d0f15b90d..2937da224 100644 --- a/winsup/cygwin/ntdll.h +++ b/winsup/cygwin/ntdll.h @@ -1003,6 +1003,7 @@ extern "C" NTSTATUS NTAPI NtFsControlFile (HANDLE, HANDLE, PIO_APC_ROUTINE, PVOID, PIO_STATUS_BLOCK, ULONG, PVOID, ULONG, PVOID, ULONG); + NTSTATUS NTAPI NtFlushBuffersFile (HANDLE, PIO_STATUS_BLOCK); NTSTATUS NTAPI NtLoadKey (POBJECT_ATTRIBUTES, POBJECT_ATTRIBUTES); NTSTATUS NTAPI NtLockVirtualMemory (HANDLE, PVOID *, ULONG *, ULONG); NTSTATUS NTAPI NtMapViewOfSection (HANDLE, HANDLE, PVOID *, ULONG, ULONG, diff --git a/winsup/cygwin/syscalls.cc b/winsup/cygwin/syscalls.cc index 350edc3e5..f0b155ef8 100644 --- a/winsup/cygwin/syscalls.cc +++ b/winsup/cygwin/syscalls.cc @@ -36,9 +36,9 @@ details. */ #include #include #include +#include #include #include -#include #include "ntdll.h" #undef fstat @@ -1492,80 +1492,62 @@ fsync (int fd) EXPORT_ALIAS (fsync, fdatasync) static void -sync_worker (const char *vol) +sync_worker (HANDLE dir, USHORT len, LPCWSTR vol) { - HANDLE fh = CreateFileA (vol, GENERIC_WRITE, FILE_SHARE_VALID_FLAGS, - &sec_none_nih, OPEN_EXISTING, 0, NULL); - if (fh != INVALID_HANDLE_VALUE) - { - FlushFileBuffers (fh); - CloseHandle (fh); - } + NTSTATUS status; + HANDLE fh; + IO_STATUS_BLOCK io; + OBJECT_ATTRIBUTES attr; + UNICODE_STRING uvol = { len, len, (WCHAR *) vol }; + + InitializeObjectAttributes (&attr, &uvol, OBJ_CASE_INSENSITIVE, dir, NULL); + status = NtOpenFile (&fh, GENERIC_WRITE, &attr, &io, + FILE_SHARE_VALID_FLAGS, 0); + if (!NT_SUCCESS (status)) + debug_printf ("NtOpenFile (%S), status %p", &uvol, status); else - debug_printf ("Open failed with %E"); + { + status = NtFlushBuffersFile (fh, &io); + if (!NT_SUCCESS (status)) + debug_printf ("NtFlushBuffersFile (%S), status %p", &uvol, status); + NtClose (fh); + } } /* sync: SUSv3 */ extern "C" void sync () { - /* Per MSDN, 50 bytes should be enough here. */ - char vol[MAX_PATH]; + OBJECT_ATTRIBUTES attr; + NTSTATUS status; + HANDLE devhdl; + UNICODE_STRING device; - if (wincap.has_guid_volumes ()) /* Win2k and newer */ + /* Open \Device object directory. */ + RtlInitUnicodeString (&device, L"\\Device"); + InitializeObjectAttributes (&attr, &device, OBJ_CASE_INSENSITIVE, NULL, NULL); + status = NtOpenDirectoryObject (&devhdl, DIRECTORY_QUERY, &attr); + if (!NT_SUCCESS (status)) { - char a_drive[MAX_PATH] = {0}; - char b_drive[MAX_PATH] = {0}; - - if (is_floppy ("A:")) - GetVolumeNameForVolumeMountPointA ("A:\\", a_drive, MAX_PATH); - if (is_floppy ("B:")) - GetVolumeNameForVolumeMountPointA ("B:\\", b_drive, MAX_PATH); - - HANDLE sh = FindFirstVolumeA (vol, MAX_PATH); - if (sh != INVALID_HANDLE_VALUE) - { - do - { - debug_printf ("Try volume %s", vol); - - /* Check vol for being a floppy on A: or B:. Skip them. */ - if (strcasematch (vol, a_drive) || strcasematch (vol, b_drive)) - { - debug_printf ("Is floppy, don't sync"); - continue; - } - - /* Eliminate trailing backslash. */ - vol[strlen (vol) - 1] = '\0'; - sync_worker (vol); - } - while (FindNextVolumeA (sh, vol, MAX_PATH)); - FindVolumeClose (sh); - } + debug_printf ("NtOpenDirectoryObject, status %p", status); + return; } - else + /* Traverse \Device directory ... */ + PDIRECTORY_BASIC_INFORMATION dbi = (PDIRECTORY_BASIC_INFORMATION) + alloca (640); + BOOLEAN restart = TRUE; + ULONG context = 0; + while (NT_SUCCESS (NtQueryDirectoryObject (devhdl, dbi, 640, TRUE, restart, + &context, NULL))) { - DWORD drives = GetLogicalDrives (); - DWORD mask = 1; - /* Skip floppies on A: and B: as in setmntent. */ - if ((drives & 1) && is_floppy ("A:")) - drives &= ~1; - if ((drives & 2) && is_floppy ("B:")) - drives &= ~2; - strcpy (vol, "\\\\.\\A:"); - do - { - /* Geeh. Try to sync only non-floppy drives. */ - if (drives & mask) - { - debug_printf ("Try volume %s", vol); - sync_worker (vol); - } - vol[4]++; - } - while ((mask <<= 1) <= 1 << 25); + restart = FALSE; + /* ... and call sync_worker for each HarddiskVolumeX entry. */ + if (dbi->ObjectName.Length >= 15 * sizeof (WCHAR) + && !wcsncasecmp (dbi->ObjectName.Buffer, L"HarddiskVolume", 14) + && iswdigit (dbi->ObjectName.Buffer[14])) + sync_worker (devhdl, dbi->ObjectName.Length, dbi->ObjectName.Buffer); } + NtClose (devhdl); } /* Cygwin internal */