如何检查文件是否已被 C++ 中的另一个应用程序打开?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1048592/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check if a file has been opened by another application in C++?
提问by
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?
我知道,is_open()
C++中有这个函数,但我想要一个程序来检查文件是否没有被另一个应用程序打开。有没有办法使用标准库来做到这一点?
EDIT - Clarified in the answers that this is for a Linux application.
编辑 - 在答案中澄清这是针对 Linux 应用程序的。
回答by
No, the standard library has no such functionality.
不,标准库没有这样的功能。
回答by CesarB
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).
不仅标准库没有这个功能,它在一般情况下甚至是不可能的。您可以(在 linux 上)检查/proc/*/fd
— 但您的程序可能没有权限在其他用户的进程上执行此操作(例如,这是 Ubuntu 中的默认设置)。
回答by tinytaro
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;
}
回答by jmanning2k
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.
如果您控制另一个进程(有源代码),最好的计划是在两个进程中使用咨询锁。这种锁定是在 POSIX 中定义的,并且可以跨操作系统移植。
In Linux, you can use the utility lsofto see what files are opened by other processes.
在 Linux 中,您可以使用实用程序lsof来查看其他进程打开了哪些文件。
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.
我只知道命令行实用程序,不知道可以直接从 C 代码使用的任何系统调用。
In Linux, it's also possible to turn on mandatory lockingfor 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.
在 Linux 中,还可以为给定的文件系统打开强制锁定(mount -o mand),并在文件上设置特殊标志 (chmod gx,g+s)。然后,当您的进程尝试获取写锁时,如果另一个进程打开了该文件,它将失败。这几乎从未使用过,但如果您完全控制相关系统,它可能是一种选择。
回答by Pod
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+")
fopen("myfile.txt", "r+")
If it's not cross platform and is Win32, then you can request even more fine-grained set of locks.
如果它不是跨平台的并且是 Win32,那么您可以请求更细粒度的锁集。
See hereand look at dwShareMode
, value of 0, as well as the other parameters.
请参阅此处并查看dwShareMode
0 的值以及其他参数。
回答by Tom
As @Neil Butterworth says, the standard library doesnt.
正如@Neil Butterworth 所说,标准库没有。
In unix you can use fcntlto use file locks.
在 unix 中,您可以使用fcntl来使用文件锁。
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.
您可以为 open 函数编写一个包装器,如果文件没有其他人打开,它会检查锁定(如果不存在则锁定)文件。您也应该为 close 编写一个包装器,以在文件关闭时释放该锁定。
回答by Nick Dandoulakis
In Windows this little and dirtytrick will work (if the file exists and you have the right permissions)
在 Windows 中,这个小而肮脏的技巧将起作用(如果文件存在并且您具有正确的权限)
if ( 0 != rename("c:/foo.txt", "c:/foo.txt") ) {
printf("already opened\n");
}
It's likely to work also in Linux.
它可能也适用于 Linux。
回答by Eugene Bujak
Nope. Unless other application uses advisory locks.
不。除非其他应用程序使用咨询锁。
See http://docs.sun.com/app/docs/doc/816-0213/6m6ne37v5?a=view
请参阅http://docs.sun.com/app/docs/doc/816-0213/6m6ne37v5?a=view
回答by ZombieSheep
Non-natively, you could call out to Sysinternals' handle.exeas a last resort...
非本地,你可以调用Sysinternals 的 handle.exe作为最后的手段......