Delete file link without clearing readonly bit

I have a set of files with several links to them.

Files belong to the TFS source, but other links are added to them. How to remove sitelinks without clearing the readonly bit.

We can safely assume:

  • Files have more than one link to them
  • You do not delete the name belonging to TFS
  • There are no potential race conditions.
  • You have full ACL control for files
  • The machine will not lose power, and your program will not be killed if it does not take too long.

It is unsafe to assume:

  • the readonly bit is set (do not set it if it is not)
  • You can leave the readonly bit clear if you encounter an error and was originally set

Do not transfer to the superuser - during its migration, the answer is impossible, because no standard tool can do this.

On a hypothetical * nix system that requires permission to write to a file to delete it, there is a solution that includes fchmod (). However, a system exhibiting this behavior is a Windows system.

+4
source share
3 answers

Have you tried to enable SeBackupPrivilege and SeRestorePrivilege, which allow administrators to eliminate many security checks?

You can find this news feed .

EDIT: To do this without privileges and without creating a race condition, you will need the NTFS transaction support present in Vista and above. BTW, you can set attributes using a descriptor, pass FILE_BASIC_INFO to SetFileInformationByHandle , which can be executed, see Notes. Or you can use FindFirstFileName to find another hard link to the same file that is not deleted, with which you can set read-only.

+4
source

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); } } } 
+4
source

It's impossible. A hard link is just another file name; you can have many hard links, but there is only one main file object (data, security descriptor, attributes, file time, etc.). If a read-only attribute is set in the file object, then any hard links by definition will also have a set of attributes.

+2
source

Source: https://habr.com/ru/post/1313001/


All Articles