I have a set of files with multiple links to them.
The files are owned by TFS source control but other links to them are made to them. How do I delete the additional links without clearing the readonly bit.
It's safe to assume:
It's not safe to assume:
Do not migrate to superuser -- if migrated there the answer is impossible because no standard tool can do this.
On a hypothetical *nix system in which one needs write permission on a file to delete it, there is a solution involving fchmod(). However the system that exhibiting this behavior is a Windows system.
Have you tried enabling SeBackupPrivilege and SeRestorePrivilege, which allow admins to relax many of the security checks?
You might find this newsgroup thread helpful.
EDIT: To do it without privileges, and without creating a race condition, you'll need transactional NTFS support present in Vista and above. BTW, you can set attributes using a handle, pass FILE_BASIC_INFO to SetFileInformationByHandle, which can be transacted, see the notes. Or you can use FindFirstFileName to find another hard link to the same file which isn't being deleted, with which to set read-only.
Thanks to Ben Voigt:
#include <windows.h>
int main(int argc, char **argv)
{
while (*++argv) {
HANDLE h;
DWORD attrs;
attrs = GetFileAttributes(*argv);
SetFileAttributes(*argv, attrs & ~FILE_ATTRIBUTE_READONLY);
h = CreateFile(*argv, GENERIC_READ|GENERIC_WRITE, 7, NULL, OPEN_EXISTING,
FILE_FLAG_DELETE_ON_CLOSE, NULL);
SetFileAttributes(*argv, attrs);
if (h != INVALID_HANDLE_VALUE) {
CloseHandle(h);
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With