Subject: Why lock a file when reading?

From: [email-masked] (Jan-Erik M}ngs)
Date: Wed, 30 Mar 1994 19:33:02 +0100

[email-masked] who has taken care of netatalk here sent me the
following patch ("why lock a file while reading?"). Any comments, is
this wrong?

-Kire

bg$ rcsdiff -c -r1.3 fork.c
===================================================================
RCS file: RCS/fork.c,v
retrieving revision 1.3
diff -c -r1.3 fork.c
*** 1.3 1994/02/23 16:28:13
--- fork.c      1994/03/29 10:26:12
***************
*** 210,216 ****
      } else {
        lockfd = ad_hfileno( &oforks[ ofrefnum ]->of_ad );
      }
!     if ( lockop && flock( lockfd, lockop|LOCK_NB ) < 0 ) {
        ad_close( &oforks[ ofrefnum ]->of_ad, adflags );
        free( oforks[ ofrefnum ]->of_name );
        free( oforks[ ofrefnum ] );
--- 210,224 ----
      } else {
        lockfd = ad_hfileno( &oforks[ ofrefnum ]->of_ad );
      }
!     if ( !lockop )
!       /* no need to lock */;
!     else if ( flock( lockfd, lockop|LOCK_NB ) == 0)
!       /* obtained lock successfully */;
!     else if ( lockop == LOCK_SH && errno == EACCES ) {
!         /* allow read access but don't lock */
!       errno = 0;
!     }
!     else {
        ad_close( &oforks[ ofrefnum ]->of_ad, adflags );
        free( oforks[ ofrefnum ]->of_name );
        free( oforks[ ofrefnum ] );
bg$

---

From: [email-masked]
Date: Wed, 30 Mar 1994 14:23:06 -0500

> From:    [email-masked] (Jan-Erik M}ngs)
> To:      [email-masked]

> following patch ("why lock a file while reading?"). Any comments, is

Yes, it is wrong.  You lock a file when reading to prevent others from
writing the file.

Since you're checking for errno == EACCESS, I assume that you're using
AFS -- AFS will return EACCESS when someone attempts to lock a file but
doesn't have the 'k' access bit.

:wes