#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)
{
/* ----------------------- */
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
/* 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) {
ad->adf_lockmax = ad->adf_lockcount + ARRAY_FREE_DELTA;
}
}
+#endif
}
*/
static void adf_unlock(struct ad_fd *ad, const int fork)
{
+#if 0
adf_lock_t *lock = ad->adf_lock;
int i;
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 (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;
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;
OVERLAP(off, len, lock[i].lock.l_start, lock[i].lock.l_len))
return i;
}
+#endif
return -1;
}
}
/* ------------------ */
-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;
lock.l_type = F_UNLCK;
if (!adf->adf_excl) set_lock(adf->adf_fd, F_SETLK, &lock);
return -1;
+#endif
+ return 0;
}
/* -------------------------
*/
static int testlock(struct ad_fd *adf, off_t off, off_t len)
{
+#if 0
struct flock lock;
adf_lock_t *plock;
int i;
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;
lock_offset = hf2off(off);
}
return testlock(adf, lock_offset, 1);
+#endif
}
/* -------------------------
- 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;
}
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;
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;
}