+/*!
+ * Test a lock
+ *
+ * (1) Test against our own locks array
+ * (2) Test fcntl lock, locks from other processes
+ *
+ * @param adf (r) handle
+ * @param off (r) offset
+ * @param len (r) lenght
+ *
+ * @returns 1 if there's an existing lock, 0 if there's no lock,
+ * -1 in case any error occured
+ */
+static int testlock(const struct ad_fd *adf, off_t off, off_t len)
+{
+ struct flock lock;
+ adf_lock_t *plock;
+ int i;
+
+ lock.l_start = off;
+
+ plock = adf->adf_lock;
+ lock.l_whence = SEEK_SET;
+ lock.l_len = len;
+
+ /* (1) Do we have a lock ? */
+ for (i = 0; i < adf->adf_lockcount; i++) {
+ if (OVERLAP(lock.l_start, 1, plock[i].lock.l_start, plock[i].lock.l_len))
+ return 1;
+ }
+
+ /* (2) Does another process have a lock? */
+ lock.l_type = (adf->adf_flags & O_RDWR) ? F_WRLCK : F_RDLCK;
+
+ if (set_lock(adf->adf_fd, F_GETLK, &lock) < 0) {
+ /* is that kind of error possible ?*/
+ return (errno == EACCES || errno == EAGAIN) ? 1 : -1;
+ }
+
+ if (lock.l_type == F_UNLCK) {
+ return 0;
+ }
+ return 1;
+}