Bartek
Bartek

Reputation:

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.

Upvotes: 12

Views: 25717

Answers (10)

SpectreVert
SpectreVert

Reputation: 197

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.

Upvotes: 0

Pod
Pod

Reputation: 4130

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.

Upvotes: 2

tinytaro
tinytaro

Reputation: 390

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

Upvotes: 3

jmanning2k
jmanning2k

Reputation: 9478

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.

Upvotes: 3

CesarB
CesarB

Reputation: 45555

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

Upvotes: 5

Nick Dandoulakis
Nick Dandoulakis

Reputation: 43110

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.

Upvotes: -1

Tom
Tom

Reputation: 45104

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.

Upvotes: 0

ZombieSheep
ZombieSheep

Reputation: 29953

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

Upvotes: 0

HMage
HMage

Reputation: 1591

Nope. Unless other application uses advisory locks.

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

Upvotes: 0

anon
anon

Reputation:

No, the standard library has no such functionality.

Upvotes: 4

Related Questions