Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Function to check if a a file is locked by another process, and if yes, which process #23

Open
gaborcsardi opened this issue Sep 13, 2019 · 2 comments
Labels
feature a feature request or enhancement

Comments

@gaborcsardi
Copy link
Member

Useful for r-lib/pkgdepends#141, then we can print out the pid of the other process.

@gaborcsardi
Copy link
Member Author

gaborcsardi commented Sep 13, 2019

@gaborcsardi gaborcsardi added the feature a feature request or enhancement label Apr 4, 2022
@mikapfl
Copy link

mikapfl commented Jun 10, 2022

To check if a file is locked on Unix, the fcntl API can be used. In R, there does not seem to be a way to call fcntl directly, but in python it works like this:

def locked(fname):
    with open(fname, "r") as fd:
        ask_lock = struct.pack("hhllhl", fcntl.F_WRLCK, 0, 0, 0, 0, 0)
        answer_raw = struct.unpack("hhllhl", fcntl.fcntl(fd, fcntl.F_GETLK, ask_lock))
        return answer_raw[0] != fcntl.F_UNLCK

I'm not aware of a general mechanism to find out which process is holding the lock on Unix. lslocks and lsof can only show local processes, not processes on other machines for network filesystems like NFS or GPFS, and I'm not aware of APIs to obtain the other machine/process which holds locks in these cases (but fcntl does show that the file is locked when the filesystem has remote locking support like NFS4 or GPFS).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature a feature request or enhancement
Projects
None yet
Development

No branches or pull requests

2 participants