From 36c4a441f6eb951779c4fc74d0e00ceec5ee3005 Mon Sep 17 00:00:00 2001 From: Christopher Faylor Date: Sun, 9 Mar 2003 18:10:42 +0000 Subject: [PATCH] * syscalls.cc (unlink): Attempt to be more clever about setting attributes of file. Only open file in query mode to avoid having to mess with security stuff for reading. --- winsup/cygwin/ChangeLog | 6 ++++++ winsup/cygwin/syscalls.cc | 36 ++++++++++++++++++++++++------------ 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/winsup/cygwin/ChangeLog b/winsup/cygwin/ChangeLog index 34730cf71..33b3df380 100644 --- a/winsup/cygwin/ChangeLog +++ b/winsup/cygwin/ChangeLog @@ -1,3 +1,9 @@ +2003-03-09 Christopher Faylor + + * syscalls.cc (unlink): Attempt to be more clever about setting + attributes of file. Only open file in query mode to avoid having to + mess with security stuff for reading. + 2003-03-09 Corinna Vinschen * rmsym: Fix regular expression. diff --git a/winsup/cygwin/syscalls.cc b/winsup/cygwin/syscalls.cc index 27e40d050..3551723c2 100644 --- a/winsup/cygwin/syscalls.cc +++ b/winsup/cygwin/syscalls.cc @@ -130,36 +130,48 @@ unlink (const char *ourname) if (!writable_directory (win32_name)) { syscall_printf ("non-writable directory"); + set_errno (EPERM); goto done; } + /* Allow us to delete even if read-only */ SetFileAttributes (win32_name, (DWORD) win32_name & ~(FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM)); /* Attempt to use "delete on close" semantics to handle removing a file which may be open. */ HANDLE h; - h = CreateFile (win32_name, GENERIC_READ, FILE_SHARE_DELETE, &sec_none_nih, + h = CreateFile (win32_name, 0, FILE_SHARE_READ, &sec_none_nih, OPEN_EXISTING, FILE_FLAG_DELETE_ON_CLOSE, 0); - - (void) SetFileAttributes (win32_name, (DWORD) win32_name); - (void) DeleteFile (win32_name); - DWORD lasterr; - lasterr = GetLastError (); if (h != INVALID_HANDLE_VALUE) - CloseHandle (h); - - if (GetFileAttributes (win32_name) == INVALID_FILE_ATTRIBUTES - || (!win32_name.isremote () && wincap.has_delete_on_close ())) { - syscall_printf ("DeleteFile succeeded"); - goto ok; + (void) SetFileAttributes (win32_name, (DWORD) win32_name); + BOOL res = CloseHandle (h); + syscall_printf ("%d = CloseHandle (%p)", res, h); + if (GetFileAttributes (win32_name) == INVALID_FILE_ATTRIBUTES + || (!win32_name.isremote () && wincap.has_delete_on_close ())) + { + syscall_printf ("CreateFile (FILE_FLAG_DELETE_ON_CLOSE) succeeded"); + goto ok; + } + else + { + syscall_printf ("CreateFile (FILE_FLAG_DELETE_ON_CLOSE) failed"); + SetFileAttributes (win32_name, (DWORD) win32_name & ~(FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM)); + } } + + /* Try a delete with attributes reset */ if (DeleteFile (win32_name)) { syscall_printf ("DeleteFile after CreateFile/ClosHandle succeeded"); goto ok; } + DWORD lasterr; + lasterr = GetLastError (); + + (void) SetFileAttributes (win32_name, (DWORD) win32_name); + /* Windows 9x seems to report ERROR_ACCESS_DENIED rather than sharing violation. So, set lasterr to ERROR_SHARING_VIOLATION in this case to simplify tests. */