简体   繁体   中英

How to check if a file has been opened by another application in C++?

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.

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).

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

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 gx,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.

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

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.

Nope. Unless other application uses advisory locks.

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

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

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM