如何检查文件是否已被 C++ 中的另一个应用程序打开?

发布于 2024-07-25 23:28:50 字数 117 浏览 6 评论 0原文

我知道,C++ 中有 is_open() 函数,但我希望一个程序检查文件是否尚未被另一个应用程序打开。 有没有办法使用标准库来做到这一点?

编辑 - 在答案中澄清这是针对 Linux 应用程序的。

I know, that there's the is_open() function in C++, but I want one program to check if a file hasn't been opened by another application. Is there any way to do it using standard library?

EDIT - Clarified in the answers that this is for a Linux application.

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

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

发布评论

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

评论(10

若能看破又如何 2024-08-01 23:28:50

不仅标准库没有这个功能,一般来说也是不可能的。 您可以(在 Linux 上)检查 /proc/*/fd — 但您的程序可能无权在其他用户的进程上执行此操作(例如,这是 Ubuntu 中的默认设置) )。

Not only the standard library does not have this funcionality, it's not even possible in general. You could (on linux) check /proc/*/fd — but it is possible that your program does not have permission to do it on processes from other users (this is the default in Ubuntu, for instance).

情深缘浅 2024-08-01 23:28:50

不,标准库没有这样的功能。

No, the standard library has no such functionality.

罗罗贝儿 2024-08-01 23:28:50

如果您控制其他进程(有源代码),最好的计划是在两个进程中使用咨询锁。 这种锁定是在 POSIX 中定义的,并且可以跨操作系统移植。

在 Linux 中,您可以使用实用程序 lsof 来查看其他进程打开了哪些文件。

这仅限于您拥有的权限 - 您必须以特权用户身份进行检查,否则您只能获得与执行检查的用户相同的用户打开的文件的结果。

我只知道命令行实用程序,不知道任何可以直接从 C 代码使用的系统调用。

在 Linux 中,还可以为给定的文件系统打开强制锁定(mount - o 命令),并在文件上设置特殊标志(chmod gx,g+s)。 然后,当您的进程尝试获取写锁时,如果另一个进程打开了该文件,它将失败。 这几乎从未被使用过,但如果您完全控制有问题的系统,它可能是一个选择。

If you control the other process (have source code), the best plan is to use advisory locks in both processes. This locking is defined in POSIX, and will be portable across operating systems.

In Linux, you can use the utility lsof to see what files are opened by other processes.

This is limited to what you have permissions for - you have to do the check as a privileged user, or you'll only get results for files opened by the same user as the one doing the check.

I only know of the command line utility, not of any system call you can use directly from C code.

In Linux, it's also possible to turn on mandatory locking for a given filesystem (mount -o mand), and set special flags on the file (chmod g-x,g+s). Then when your process attempts to acquire a write lock, it will fail if another process has the file open. This is hardly ever used, but if you completely control the system in question, it may be an option.

污味仙女 2024-08-01 23:28:50

以下代码可能有效。

int main(int argc, char ** argv)
{
    int fd = open(argv[1], O_RDONLY);
    if (fd < 0) {
        perror("open");
        return 1;
    }

    if (fcntl(fd, F_SETLEASE, F_WRLCK) && EAGAIN == errno) {
        puts("file has been opened");
    }
    else {
        fcntl(fd, F_SETLEASE, F_UNLCK);
        puts("file has not been opened");
    }

    close(fd);
    return 0;
}

The following code may work.

int main(int argc, char ** argv)
{
    int fd = open(argv[1], O_RDONLY);
    if (fd < 0) {
        perror("open");
        return 1;
    }

    if (fcntl(fd, F_SETLEASE, F_WRLCK) && EAGAIN == errno) {
        puts("file has been opened");
    }
    else {
        fcntl(fd, F_SETLEASE, F_UNLCK);
        puts("file has not been opened");
    }

    close(fd);
    return 0;
}
假面具 2024-08-01 23:28:50

也许您可以尝试获得完全写锁? 如果其他人打开它进行读取或写入,它将失败。

fopen("myfile.txt", "r+")

如果它不是跨平台并且是 Win32,那么您可以请求更细粒度的锁集。

请参阅此处
并查看 dwShareMode,值为 0,以及其他参数。

Perhaps you could just try and get a full write lock? It'll fail if anyone else has it open for reading or writing.

fopen("myfile.txt", "r+")

If it's not cross platform and is Win32, then you can request even more fine-grained set of locks.

See here
and look at dwShareMode, value of 0, as well as the other parameters.

很酷又爱笑 2024-08-01 23:28:50

没有。 除非其他应用程序使用咨询锁。

请参阅 http://docs.sun.com/app /docs/doc/816-0213/6m6ne37v5?a=视图

Nope. Unless other application uses advisory locks.

See http://docs.sun.com/app/docs/doc/816-0213/6m6ne37v5?a=view

冧九 2024-08-01 23:28:50

非本机,您可以调用 Sysinternals 的handle.exe作为最后的手段...

Non-natively, you could call out to Sysinternals' handle.exe as a last resort...

靑春怀旧 2024-08-01 23:28:50

正如 @Neil Butterworth 所说,标准库没有。

在unix中,您可以使用 fcntl 来使用文件锁。

您可以为 open 函数编写一个包装器,该包装器检查文件是否被其他人打开(如果不存在则锁定)。 您还应该为关闭编写一个包装器,以释放文件关闭时的锁定。

As @Neil Butterworth says, the standard library doesnt.

In unix you can use fcntl to use file locks.

You could write a wrapper for your open function, that checks for a lock (and locks if none exists) a file if its open by no one else. You shold write a wrapper for close as well, that releases that lock on file close.

楠木可依 2024-08-01 23:28:50

看到你已经标记了 linux -> Linux 内核中还添加了一个命令行和 API 来执行此操作: inotify

这是 手册 页面。

Seeing you have tagged linux -> there's a command-line too and API that have been added to the Linux kernel and do just that: inotify.

Here's the man page.

感受沵的脚步 2024-08-01 23:28:50

在 Windows 中,这个小技巧将起作用(如果文件存在并且您具有正确的权限),

  if (  0 != rename("c:/foo.txt", "c:/foo.txt")  ) {
     printf("already opened\n");
  }

它也可能在 Linux 中起作用。

In Windows this little and dirty trick will work (if the file exists and you have the right permissions)

  if (  0 != rename("c:/foo.txt", "c:/foo.txt")  ) {
     printf("already opened\n");
  }

It's likely to work also in Linux.

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