]> arthur.barton.de Git - netatalk.git/blobdiff - libatalk/adouble/ad_lock.c
Big configure.in cleanup
[netatalk.git] / libatalk / adouble / ad_lock.c
index 441a272e7db52ec8bb09c053361fb1b6ddb9b249..6fab902858965091b11528fa704d38de48f86e1d 100644 (file)
@@ -1,6 +1,4 @@
 /* 
- * $Id: ad_lock.c,v 1.18 2009-11-12 06:28:40 didg Exp $
- *
  * Copyright (c) 1998,1999 Adrian Sun (asun@zoology.washington.edu)
  * All Rights Reserved. See COPYRIGHT for more information.
  *
 
 #include <string.h>
 
-#include "ad_private.h"
+#include "ad_lock.h"
 
 /* translate between ADLOCK styles and specific locking mechanisms */
 #define XLATE_FLOCK(type) ((type) == ADLOCK_RD ? LOCK_SH : \
 ((type) == ADLOCK_WR ? LOCK_EX : \
  ((type) == ADLOCK_CLR ? LOCK_UN : -1)))
 
-#ifdef DISABLE_LOCKING
-#define fcntl(a, b, c ) (0)
-#endif
+/* ----------------------- */
+static int set_lock(int fd, int cmd,  struct flock *lock)
+{
+  if (fd == -2) {
+      /* We assign fd = -2 for symlinks -> do nothing */
+      if (cmd == F_GETLK)
+           lock->l_type = F_UNLCK;
+      return 0;
+  }
+  return fcntl(fd, cmd, lock);
+}
 
 /* ----------------------- */
 static int XLATE_FCNTL_LOCK(int type) 
@@ -56,9 +62,9 @@ static int XLATE_FCNTL_LOCK(int type)
 /* ----------------------- */
 static int OVERLAP(off_t a, off_t alen, off_t b, off_t blen) 
 {
- return (!alen && a <= b) || 
-       (!blen && b <= a) || 
-       ( (a + alen > b) && (b + blen > a) );
   return (!alen && a <= b) || 
+        (!blen && b <= a) || 
+        ( (a + alen > b) && (b + blen > a) );
 }
 
 /* allocation for lock regions. we allocate aggressively and shrink
@@ -69,13 +75,14 @@ static int OVERLAP(off_t a, off_t alen, off_t b, off_t blen)
 /* remove a lock and compact space if necessary */
 static void adf_freelock(struct ad_fd *ad, const int i)
 {
+#if 0
     adf_lock_t *lock = ad->adf_lock + i;
 
     if (--(*lock->refcount) < 1) {
        free(lock->refcount); 
        if (!ad->adf_excl) {
            lock->lock.l_type = F_UNLCK;
-           fcntl(ad->adf_fd, F_SETLK, &lock->lock); /* unlock */
+           set_lock(ad->adf_fd, F_SETLK, &lock->lock); /* unlock */
        }
     }
 
@@ -101,6 +108,7 @@ static void adf_freelock(struct ad_fd *ad, const int i)
                ad->adf_lockmax = ad->adf_lockcount + ARRAY_FREE_DELTA;
            }
     }
+#endif
 }
 
 
@@ -113,6 +121,7 @@ static void adf_freelock(struct ad_fd *ad, const int i)
  */
 static void adf_unlock(struct ad_fd *ad, const int fork)
 {
+#if 0
     adf_lock_t *lock = ad->adf_lock;
     int i;
 
@@ -127,29 +136,32 @@ static void adf_unlock(struct ad_fd *ad, const int fork)
         lock = ad->adf_lock;       
       }
     }
+#endif
 }
 
 /* relock any byte lock that overlaps off/len. unlock everything
  * else. */
-static void adf_relockrange(struct ad_fd *ad, int fd,
-                                      const off_t off, const off_t len)
+static void adf_relockrange(struct ad_fd *ad, int fd, off_t off, off_t len)
 {
+#if 0
     adf_lock_t *lock = ad->adf_lock;
     int i;
     
     if (!ad->adf_excl) for (i = 0; i < ad->adf_lockcount; i++) {
       if (OVERLAP(off, len, lock[i].lock.l_start, lock[i].lock.l_len)) 
-       fcntl(fd, F_SETLK, &lock[i].lock);
+       set_lock(fd, F_SETLK, &lock[i].lock);
     }
+#endif
 }
 
 
 /* find a byte lock that overlaps off/len for a particular open fork */
 static int adf_findlock(struct ad_fd *ad,
-                                  const int fork, const int type,
-                                  const off_t off,
-                                  const off_t len)
+                        const int fork, const int type,
+                        const off_t off,
+                        const off_t len)
 {
+#if 0
   adf_lock_t *lock = ad->adf_lock;
   int i;
   
@@ -161,17 +173,18 @@ static int adf_findlock(struct ad_fd *ad,
       return i;
     }
   }
-
+#endif
   return -1;
 }
 
 
 /* search other fork lock lists */
 static int adf_findxlock(struct ad_fd *ad, 
-                                    const int fork, const int type,
-                                    const off_t off,
-                                    const off_t len)
+                         const int fork, const int type,
+                         const off_t off,
+                         const off_t len)
 {
+#if 0
   adf_lock_t *lock = ad->adf_lock;
   int i;
   
@@ -182,6 +195,7 @@ static int adf_findxlock(struct ad_fd *ad,
        OVERLAP(off, len, lock[i].lock.l_start, lock[i].lock.l_len)) 
            return i;
   } 
+#endif
   return -1;
 }
 
@@ -251,9 +265,10 @@ int start = off;
 }
 
 /* ------------------ */
-int ad_fcntl_lock(struct adouble *ad, const u_int32_t eid, const int locktype,
-                 const off_t off, const off_t len, const int fork)
+static int ad_fcntl_lock(struct adouble *ad, const u_int32_t eid, const int locktype,
+                         const off_t off, const off_t len, const int fork)
 {
+#if 0
   struct flock lock;
   struct ad_fd *adf;
   adf_lock_t *adflock;
@@ -326,12 +341,16 @@ int ad_fcntl_lock(struct adouble *ad, const u_int32_t eid, const int locktype,
      1) we're trying to re-lock a lock, but we didn't specify an update.
      2) we're trying to free only part of a lock. 
      3) we're trying to free a non-existent lock. */
-  if ((!adflock && (lock.l_type == F_UNLCK)) ||
-      (adflock && !(type & ADLOCK_UPGRADE) && 
-       ((lock.l_type != F_UNLCK) || (adflock->lock.l_start != lock.l_start) ||
-       (adflock->lock.l_len != lock.l_len)))) {
-    errno = EINVAL;
-    return -1;
+  if ( (!adflock && (lock.l_type == F_UNLCK))
+       ||
+       (adflock
+        && !(type & ADLOCK_UPGRADE)
+        && ((lock.l_type != F_UNLCK)
+            || (adflock->lock.l_start != lock.l_start)
+            || (adflock->lock.l_len != lock.l_len) ))
+      ) {
+      errno = EINVAL;
+      return -1;
   }
 
 
@@ -343,7 +362,7 @@ int ad_fcntl_lock(struct adouble *ad, const u_int32_t eid, const int locktype,
   }
 
   /* attempt to lock the file. */
-  if (!adf->adf_excl && fcntl(adf->adf_fd, F_SETLK, &lock) < 0) 
+  if (!adf->adf_excl && set_lock(adf->adf_fd, F_SETLK, &lock) < 0) 
     return -1;
 
   /* we upgraded this lock. */
@@ -385,8 +404,10 @@ int ad_fcntl_lock(struct adouble *ad, const u_int32_t eid, const int locktype,
 
 fcntl_lock_err:
   lock.l_type = F_UNLCK;
-  if (!adf->adf_excl) fcntl(adf->adf_fd, F_SETLK, &lock);
+  if (!adf->adf_excl) set_lock(adf->adf_fd, F_SETLK, &lock);
   return -1;
+#endif
+  return 0;
 }
 
 /* -------------------------
@@ -399,6 +420,7 @@ fcntl_lock_err:
 */
 static int testlock(struct ad_fd *adf, off_t off, off_t len)
 {
+#if 0
   struct flock lock;
   adf_lock_t *plock;
   int i;
@@ -418,7 +440,7 @@ static int testlock(struct ad_fd *adf, off_t off, off_t len)
   */
   lock.l_type = (adf->adf_flags & O_RDWR) ?F_WRLCK : F_RDLCK;
 
-  if (fcntl(adf->adf_fd, F_GETLK, &lock) < 0) {
+  if (set_lock(adf->adf_fd, F_GETLK, &lock) < 0) {
       /* is that kind of error possible ?*/
       return (errno == EACCES || errno == EAGAIN)?1:-1;
   }
@@ -427,11 +449,103 @@ static int testlock(struct ad_fd *adf, off_t off, off_t len)
       return 0;
   }
   return 1;
+#endif
+  return 0;
+}
+
+
+/* -------------------------
+*/
+static int ad_fcntl_tmplock(struct adouble *ad, const u_int32_t eid, const int locktype,
+                            const off_t off, const off_t len, const int fork)
+{
+  struct flock lock;
+  struct ad_fd *adf;
+  int err;
+  int type;  
+
+  lock.l_start = off;
+  type = locktype;
+  if (eid == ADEID_DFORK) {
+    adf = &ad->ad_data_fork;
+  } else {
+    /* FIXME META */
+    adf = &ad->ad_resource_fork;
+    if (adf->adf_fd == -1) {
+        /* there's no resource fork. return success */
+        return 0;
+    }
+    /* if ADLOCK_FILELOCK we want a lock from offset 0
+     * it's used when deleting a file:
+     * in open we put read locks on meta datas
+     * in delete a write locks on the whole file
+     * so if the file is open by somebody else it fails
+    */
+    if (!(type & ADLOCK_FILELOCK))
+        lock.l_start += ad_getentryoff(ad, eid);
+  }
+
+  if (!(adf->adf_flags & O_RDWR) && (type & ADLOCK_WR)) {
+      type = (type & ~ADLOCK_WR) | ADLOCK_RD;
+  }
+  
+  lock.l_type = XLATE_FCNTL_LOCK(type & ADLOCK_MASK);
+  lock.l_whence = SEEK_SET;
+  lock.l_len = len;
+
+  /* see if it's locked by another fork. */
+  if (fork && adf_findxlock(adf, fork, ADLOCK_WR | 
+                   ((type & ADLOCK_WR) ? ADLOCK_RD : 0), 
+                   lock.l_start, lock.l_len) > -1) {
+    errno = EACCES;
+    return -1;
+  }
+
+  /* okay, we might have ranges byte-locked. we need to make sure that
+   * we restore the appropriate ranges once we're done. so, we check
+   * for overlap on an unlock and relock. 
+   * XXX: in the future, all the byte locks will be sorted and contiguous.
+   *      we just want to upgrade all the locks and then downgrade them
+   *      here. */
+  if (!adf->adf_excl) {
+       err = set_lock(adf->adf_fd, F_SETLK, &lock);
+  }
+  else {
+      err = 0;
+  }
+  if (!err && (lock.l_type == F_UNLCK))
+    adf_relockrange(adf, adf->adf_fd, lock.l_start, len);
+
+  return err;
 }
 
+/* --------------------- */
+static void ad_fcntl_unlock(struct adouble *ad, const int fork)
+{
+  if (ad_data_fileno(ad) != -1) {
+    adf_unlock(&ad->ad_data_fork, fork);
+  }
+  if (ad_reso_fileno(ad) != -1) {
+    adf_unlock(&ad->ad_resource_fork, fork);
+  }
+
+  if (ad->ad_flags != AD_VERSION_EA) {
+    return;
+  }
+  if (ad_meta_fileno(ad) != -1) {
+    adf_unlock(&ad->ad_metadata_fork, fork);
+  }
+}
+
+/******************************************************************************
+ * Public functions
+ ******************************************************************************/
+
 /* --------------- */
 int ad_testlock(struct adouble *ad, int eid, const off_t off)
 {
+    return 0;
+#if 0
   struct ad_fd *adf;
   off_t      lock_offset;
 
@@ -452,6 +566,7 @@ int ad_testlock(struct adouble *ad, int eid, const off_t off)
     lock_offset = hf2off(off);
   }
   return testlock(adf, lock_offset, 1);
+#endif
 }
 
 /* -------------------------
@@ -460,8 +575,10 @@ int ad_testlock(struct adouble *ad, int eid, const off_t off)
    - there's no locks held by another process (clients)
    - or we already know the answer and don't need to test.
 */
-u_int16_t ad_openforks(struct adouble *ad, u_int16_t attrbits)
+uint16_t ad_openforks(struct adouble *ad, u_int16_t attrbits)
 {
+    return 0;
+#if 0
   u_int16_t ret = 0;
   struct ad_fd *adf;
   off_t off;
@@ -513,71 +630,7 @@ u_int16_t ad_openforks(struct adouble *ad, u_int16_t attrbits)
   }
 
   return ret;
-}
-
-/* -------------------------
-*/
-int ad_fcntl_tmplock(struct adouble *ad, const u_int32_t eid, const int locktype,
-                    const off_t off, const off_t len, const int fork)
-{
-  struct flock lock;
-  struct ad_fd *adf;
-  int err;
-  int type;  
-
-  lock.l_start = off;
-  type = locktype;
-  if (eid == ADEID_DFORK) {
-    adf = &ad->ad_data_fork;
-  } else {
-    /* FIXME META */
-    adf = &ad->ad_resource_fork;
-    if (adf->adf_fd == -1) {
-        /* there's no resource fork. return success */
-        return 0;
-    }
-    /* if ADLOCK_FILELOCK we want a lock from offset 0
-     * it's used when deleting a file:
-     * in open we put read locks on meta datas
-     * in delete a write locks on the whole file
-     * so if the file is open by somebody else it fails
-    */
-    if (!(type & ADLOCK_FILELOCK))
-        lock.l_start += ad_getentryoff(ad, eid);
-  }
-
-  if (!(adf->adf_flags & O_RDWR) && (type & ADLOCK_WR)) {
-      type = (type & ~ADLOCK_WR) | ADLOCK_RD;
-  }
-  
-  lock.l_type = XLATE_FCNTL_LOCK(type & ADLOCK_MASK);
-  lock.l_whence = SEEK_SET;
-  lock.l_len = len;
-
-  /* see if it's locked by another fork. */
-  if (fork && adf_findxlock(adf, fork, ADLOCK_WR | 
-                   ((type & ADLOCK_WR) ? ADLOCK_RD : 0), 
-                   lock.l_start, lock.l_len) > -1) {
-    errno = EACCES;
-    return -1;
-  }
-
-  /* okay, we might have ranges byte-locked. we need to make sure that
-   * we restore the appropriate ranges once we're done. so, we check
-   * for overlap on an unlock and relock. 
-   * XXX: in the future, all the byte locks will be sorted and contiguous.
-   *      we just want to upgrade all the locks and then downgrade them
-   *      here. */
-  if (!adf->adf_excl) {
-       err = fcntl(adf->adf_fd, F_SETLK, &lock);
-  }
-  else {
-      err = 0;
-  }
-  if (!err && (lock.l_type == F_UNLCK))
-    adf_relockrange(adf, adf->adf_fd, lock.l_start, len);
-
-  return err;
+#endif
 }
 
 /* -------------------------
@@ -586,6 +639,8 @@ int ad_fcntl_tmplock(struct adouble *ad, const u_int32_t eid, const int locktype
 */
 int ad_excl_lock(struct adouble *ad, const u_int32_t eid)
 {
+    return 0;
+#if 0
   struct ad_fd *adf;
   struct flock lock;
   int    err;
@@ -602,27 +657,24 @@ int ad_excl_lock(struct adouble *ad, const u_int32_t eid)
     lock.l_start = ad_getentryoff(ad, eid);
   }
   
-  err = fcntl(adf->adf_fd, F_SETLK, &lock);
+  err = set_lock(adf->adf_fd, F_SETLK, &lock);
   if (!err)
       adf->adf_excl = 1;
   return err;
+#endif
 }
 
-/* --------------------- */
-void ad_fcntl_unlock(struct adouble *ad, const int fork)
+int ad_lock(struct adouble *ad, uint32_t eid, int type, off_t off, off_t len, int user)
 {
-  if (ad_data_fileno(ad) != -1) {
-    adf_unlock(&ad->ad_data_fork, fork);
-  }
-  if (ad_reso_fileno(ad) != -1) {
-    adf_unlock(&ad->ad_resource_fork, fork);
-  }
+    return 0;
+}
 
-  if (ad->ad_flags != AD_VERSION1_SFM) {
+void ad_unlock(struct adouble *ad, int user)
+{
     return;
-  }
-  if (ad_meta_fileno(ad) != -1) {
-    adf_unlock(&ad->ad_metadata_fork, fork);
-  }
+}
 
+int ad_tmplock(struct adouble *ad, uint32_t eid, int type, off_t off, off_t len, int user)
+{
+    return 0;
 }