* 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.
This commit is contained in:
Christopher Faylor 2003-03-09 18:10:42 +00:00
parent c379f2ed07
commit 36c4a441f6
2 changed files with 30 additions and 12 deletions

View File

@ -1,3 +1,9 @@
2003-03-09 Christopher Faylor <cgf@redhat.com>
* 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 <corinna@vinschen.de> 2003-03-09 Corinna Vinschen <corinna@vinschen.de>
* rmsym: Fix regular expression. * rmsym: Fix regular expression.

View File

@ -130,36 +130,48 @@ unlink (const char *ourname)
if (!writable_directory (win32_name)) if (!writable_directory (win32_name))
{ {
syscall_printf ("non-writable directory"); syscall_printf ("non-writable directory");
set_errno (EPERM);
goto done; goto done;
} }
/* Allow us to delete even if read-only */
SetFileAttributes (win32_name, (DWORD) win32_name & ~(FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM)); SetFileAttributes (win32_name, (DWORD) win32_name & ~(FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM));
/* Attempt to use "delete on close" semantics to handle removing /* Attempt to use "delete on close" semantics to handle removing
a file which may be open. */ a file which may be open. */
HANDLE h; 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); 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) if (h != INVALID_HANDLE_VALUE)
CloseHandle (h); {
(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 if (GetFileAttributes (win32_name) == INVALID_FILE_ATTRIBUTES
|| (!win32_name.isremote () && wincap.has_delete_on_close ())) || (!win32_name.isremote () && wincap.has_delete_on_close ()))
{ {
syscall_printf ("DeleteFile succeeded"); syscall_printf ("CreateFile (FILE_FLAG_DELETE_ON_CLOSE) succeeded");
goto ok; 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)) if (DeleteFile (win32_name))
{ {
syscall_printf ("DeleteFile after CreateFile/ClosHandle succeeded"); syscall_printf ("DeleteFile after CreateFile/ClosHandle succeeded");
goto ok; goto ok;
} }
DWORD lasterr;
lasterr = GetLastError ();
(void) SetFileAttributes (win32_name, (DWORD) win32_name);
/* Windows 9x seems to report ERROR_ACCESS_DENIED rather than sharing /* Windows 9x seems to report ERROR_ACCESS_DENIED rather than sharing
violation. So, set lasterr to ERROR_SHARING_VIOLATION in this case violation. So, set lasterr to ERROR_SHARING_VIOLATION in this case
to simplify tests. */ to simplify tests. */