删除文件链接而不清除只读位

发布于 2024-09-06 02:10:57 字数 476 浏览 2 评论 0 原文

我有一组文件,其中有多个链接。

这些文件归 TFS 源代码管理所有,但也对它们建立了其他链接。如何在不清除只读位的情况下删除附加链接。

可以安全地假设:

  • 这些文件有多个链接到它们
  • 您没有删除 TFS 拥有的名称
  • 不存在潜在的竞争条件
  • 您对这些文件拥有 ACL 完全控制
  • 权 机器不会断电,您的程序也不会被终止除非需要太长时间。

假设是不安全的:

  • 只读位已设置(如果未设置,则不要设置)
  • 如果遇到错误并且最初设置了只读位,则可以将只读位清除

不要迁移到超级用户 - 如果迁移到那里答案这是不可能的,因为没有标准工具可以做到这一点。

在假设的 *nix 系统上,需要对文件进行写权限才能将其删除,有一种涉及 fchmod() 的解决方案。然而,表现出此行为的系统是 Windows 系统。

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:

  • The files have more than one link to them
  • You are not deleting the name owned by TFS
  • There are no potential race conditions
  • You have ACL full control for the files
  • The machine will not lose power, nor will your program be killed unless it takes way too long.

It's not safe to assume:

  • The readonly bit is set (don't set it if its not)
  • You can leave the readonly bit clear if you encounter an error and it was initially set

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浮生面具三千个 2024-09-13 02:10:57

您是否尝试过启用 SeBackupPrivilege 和 SeRestorePrivilege,这允许管理员放松许多安全检查?

您可能会发现此新闻组帖子很有帮助。

编辑:
要在没有特权且不创建竞争条件的情况下执行此操作,您需要 Vista 及更高版本中提供事务性 NTFS 支持。顺便说一句,您可以使用句柄设置属性,将 FILE_BASIC_INFO 传递给 SetFileInformationByHandle ,可进行交易,详见备注。或者您可以使用 FindFirstFileName 查找未删除的同一文件的另一个硬链接,并将其设置为只读。

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.

小猫一只 2024-09-13 02:10:57

感谢本·沃伊特:

#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);
        }
    }
}

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);
        }
    }
}
千纸鹤带着心事 2024-09-13 02:10:57

这是不可能的。硬链接只是文件的另一个名称;您可以有许多硬链接,但只有一个底层文件对象(数据、安全描述符、属性、文件时间等)。如果文件对象设置了只读属性,则根据定义,任何硬链接也将设置该属性。

This isn't possible. A hard link is just another name for a file; you can have many hard links, but there is only one underlying file object (data, security descriptor, attributes, file times, etc). If the file object has the read only attribute set, then any hard links by definition will also have the attribute set.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文