]> arthur.barton.de Git - netatalk.git/blobdiff - libatalk/adouble/ad_lock.c
Merge remote branch 'netafp/master' into branch-allea
[netatalk.git] / libatalk / adouble / ad_lock.c
index e2139e5850b7ed4eacd1dd09dd5f88ce37d31cc5..38d2226eec845cb8b0f48d3dbfb6e787c489da9f 100644 (file)
 
 #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)
 {
@@ -68,9 +64,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
@@ -81,6 +77,7 @@ 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) {
@@ -113,6 +110,7 @@ static void adf_freelock(struct ad_fd *ad, const int i)
                ad->adf_lockmax = ad->adf_lockcount + ARRAY_FREE_DELTA;
            }
     }
+#endif
 }
 
 
@@ -125,6 +123,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;
 
@@ -139,13 +138,14 @@ 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;
     
@@ -153,15 +153,17 @@ static void adf_relockrange(struct ad_fd *ad, int fd,
       if (OVERLAP(off, len, lock[i].lock.l_start, lock[i].lock.l_len)) 
        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;
   
@@ -173,17 +175,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;
   
@@ -194,6 +197,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;
 }
 
@@ -263,9 +267,10 @@ static off_t hf2off(off_t 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 uint32_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;
@@ -403,6 +408,8 @@ fcntl_lock_err:
   lock.l_type = F_UNLCK;
   if (!adf->adf_excl) set_lock(adf->adf_fd, F_SETLK, &lock);
   return -1;
+#endif
+  return 0;
 }
 
 /* -------------------------
@@ -415,6 +422,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;
@@ -443,11 +451,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 uint32_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;
 
@@ -468,6 +568,7 @@ int ad_testlock(struct adouble *ad, int eid, const off_t off)
     lock_offset = hf2off(off);
   }
   return testlock(adf, lock_offset, 1);
+#endif
 }
 
 /* -------------------------
@@ -476,8 +577,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, uint16_t attrbits)
 {
+    return 0;
+#if 0
   u_int16_t ret = 0;
   struct ad_fd *adf;
   off_t off;
@@ -529,79 +632,17 @@ 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 = 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;
+#endif
 }
 
 /* -------------------------
    the fork is opened in Read Write, Deny Read, Deny Write mode
    lock the whole file once   
 */
-int ad_excl_lock(struct adouble *ad, const u_int32_t eid)
+int ad_excl_lock(struct adouble *ad, const uint32_t eid)
 {
+    return 0;
+#if 0
   struct ad_fd *adf;
   struct flock lock;
   int    err;
@@ -622,23 +663,20 @@ int ad_excl_lock(struct adouble *ad, const u_int32_t eid)
   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;
 }