[Vala] Checking file locks in Vala
- From: David Ma <dm10005 yahoo com>
- To: vala-list gnome org
- Subject: [Vala] Checking file locks in Vala
- Date: Mon, 11 Oct 2010 17:53:41 +0000 (UTC)
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]