Re: [Vala] Checking file locks in Vala



David Ma <dm10005    > writes:


How do you check whether a file is locked in Vala? 
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).

I put the following into the posix.vapi file, and now it's working. Therefore, 
there seems to currently be an omission in the posix.vapi.


[CCode (cheader_filename = "fcntl.h", cname = "struct flock")]
public struct Flock {
        public int  l_type;
        public int  l_whence;
        public off_t  l_start;
        public off_t  l_len;
        public pid_t  l_pid;
}



For the record, here is the lock-checking code (in case it might be useful to 
someone else):


public bool is_dpkg_locked() {
        
        string lock_file_name = "/var/lib/dpkg/lock";
        int fd = Posix.open (lock_file_name, Posix.O_RDWR); 
        if (fd == -1) {
          print ("There was an error opening the file '"  
            + lock_file_name + " (Error number "  
            + Posix.errno.to_string() + ")\n");
          return true;
        }
                
        // Try to get a lock
        Posix.Flock fl = Posix.Flock();
        fl.l_type = Posix.F_WRLCK;
        fl.l_whence = Posix.SEEK_SET;
        fl.l_start = 100;
        fl.l_len = 0;
                
        int fcntl_return = Posix.fcntl (fd, Posix.F_SETLK, fl);
        if (fcntl_return == -1) 
                return true;
                
        // Release the lock again
        fl.l_type = Posix.F_UNLCK;
        fl.l_whence = Posix.SEEK_SET;
        fl.l_start = 100;
        fl.l_len = 0;
        fcntl_return = Posix.fcntl (fd, Posix.F_SETLK, fl);

        return false;
}





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