/*
- * $Id: ad_lock.c,v 1.7 2003-01-16 20:06:33 didg Exp $
+ * $Id: ad_lock.c,v 1.11 2003-02-16 12:35:05 didg Exp $
*
* Copyright (c) 1998,1999 Adrian Sun (asun@zoology.washington.edu)
* All Rights Reserved. See COPYRIGHT for more information.
}
/* ----------------------- */
-
static int OVERLAP(off_t a, off_t alen, off_t b, off_t blen)
{
return (!alen && a <= b) ||
( (a + alen > b) && (b + blen > a) );
}
-
/* allocation for lock regions. we allocate aggressively and shrink
* only in large chunks. */
#define ARRAY_BLOCK_SIZE 10
if (--(*lock->refcount) < 1) {
free(lock->refcount);
- lock->lock.l_type = F_UNLCK;
- fcntl(ad->adf_fd, F_SETLK, &lock->lock); /* unlock */
+ if (!ad->adf_excl) {
+ lock->lock.l_type = F_UNLCK;
+ fcntl(ad->adf_fd, F_SETLK, &lock->lock); /* unlock */
+ }
}
ad->adf_lockcount--;
adf_lock_t *lock = ad->adf_lock;
int i;
- for (i = 0; i < ad->adf_lockcount; 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);
}
translate a data fork lock to an offset
*/
-static int df2off(int off)
+static off_t df2off(int off)
{
int start = off;
if (off == AD_FILELOCK_OPEN_WR)
translate a resource fork lock to an offset
*/
-static int hf2off(int off)
+static off_t hf2off(int off)
{
int start = off;
if (off == AD_FILELOCK_OPEN_WR)
{
struct flock lock;
struct ad_fd *adf;
- adf_lock_t *adflock, *oldlock;
+ adf_lock_t *adflock;
+ int oldlock;
int i;
int type;
}
/* attempt to lock the file. */
- if (fcntl(adf->adf_fd, F_SETLK, &lock) < 0)
+ if (!adf->adf_excl && fcntl(adf->adf_fd, F_SETLK, &lock) < 0)
return -1;
/* we upgraded this lock. */
}
/* it wasn't an upgrade */
- oldlock = NULL;
- if ((lock.l_type == F_RDLCK) &&
- ((i = adf_findxlock(adf, user, ADLOCK_RD, lock.l_start, lock.l_len)) > -1)) {
- oldlock = adf->adf_lock + i;
+ oldlock = -1;
+ if (lock.l_type == F_RDLCK) {
+ oldlock = adf_findxlock(adf, user, ADLOCK_RD, lock.l_start, lock.l_len);
}
/* no more space. this will also happen if lockmax == lockcount == 0 */
/* fill in fields */
memcpy(&adflock->lock, &lock, sizeof(lock));
adflock->user = user;
- if (oldlock)
- adflock->refcount = oldlock->refcount;
- else if ((adflock->refcount = calloc(1, sizeof(int))) == NULL) {
+ if (oldlock > -1) {
+ adflock->refcount = (adf->adf_lock + oldlock)->refcount;
+ } else if ((adflock->refcount = calloc(1, sizeof(int))) == NULL) {
goto fcntl_lock_err;
}
fcntl_lock_err:
lock.l_type = F_UNLCK;
- fcntl(adf->adf_fd, F_SETLK, &lock);
+ if (!adf->adf_excl) fcntl(adf->adf_fd, F_SETLK, &lock);
return -1;
}
/* -------------------------
*/
-int ad_fcntl_tmplock(struct adouble *ad, const u_int32_t eid, const int type,
+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 user)
{
struct flock lock;
struct ad_fd *adf;
int err;
+ int type;
lock.l_start = off;
+ type = locktype;
if (eid == ADEID_DFORK) {
adf = &ad->ad_df;
} else {
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;
* 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. */
- err = fcntl(adf->adf_fd, F_SETLK, &lock);
+ 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;
}
+/* -------------------------
+ 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)
+{
+ struct ad_fd *adf;
+ struct flock lock;
+ int err;
+
+ if (eid == ADEID_DFORK) {
+ adf = &ad->ad_df;
+ } else {
+ adf = &ad->ad_hf;
+ }
+ lock.l_start = 0;
+ lock.l_type = F_WRLCK;
+ lock.l_whence = SEEK_SET;
+ lock.l_len = 0;
+
+ err = fcntl(adf->adf_fd, F_SETLK, &lock);
+ if (!err)
+ adf->adf_excl = 1;
+ return err;
+}
+/* --------------------- */
void ad_fcntl_unlock(struct adouble *ad, const int user)
{
if (ad->ad_df.adf_fd != -1) {