[Vala] Checking file locks in Vala



How do you check whether a file is locked in Vala? 

I want to check whether the file /var/lib/dpkg/lock is locked (to check whether 
Synaptic is running).

As far as I can see from Posix documentation, the function Posix.fcntl should be 
used per the examples in 
http://www.opengroup.org/onlinepubs/009695399/functions/fcntl.html

However, this function needs to use the struct 'flock' as the third argument 
passed to fcntl, but this struct doesn't exist in the posix.vapi file (at least, 
I can't see it in there, or in Valadoc).

In case it's any help, the following code works in Java (but I'm trying to port 
it to Vala):

File lock_file = new File ("/var/lib/dpkg/lock");
FileChannel channel = 
     new RandomAccessFile(lockFile, "rw").getChannel();
FileLock lock = null;
try {
   lock = channel.tryLock();
} 
catch (Exception e) {
  return true;
}
if (lock == null)
  return true;
else {
        lock.release();
        return false;
}

Thanks.

David




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]