File Write Permission Rules

Richard Tobin richard at aiai.ed.ac.uk
Tue Feb 14 06:28:29 AEST 1989


In article <306 at wubios.wustl.edu> david at wubios.wustl.edu (David J. Camp) writes:
>We have a strange situation where a program can write to a file even
>though it does not have write permission.  What it does is remove the
>file and write a new one in its place.  It can do this because it has
>write permission to the directory in which the file is contained.
>
>My question is:  What is the (historical or otherwise) justification for
>this rule?  It seems wrong.  I would have required write permission to
>the file itself in order that it be removed.

In Unix, a file has an existence separate from a directory entry that 
refers to it.  Changing the file, and changing a reference to it in a
directory, are logically distinct.  So it's not surprising that there
are separate permissions for the two operations.

Of course, the kernel could prevent you from removing a reference to
a file you can't write.  Why doesn't it?  Because it would remove a
useful feature.  This feature is most apparent when there are multiple
references (or "links" as they're usually known) to a single file.
Then writing the file has a quite different effect from removing it
and creating a new file.  

Try it:

% echo hello > fred
% ln fred jane
% ls -li fred janeb
  571 -rw-r--r--  2 richard         6 Feb 13 20:20 fred
  571 -rw-r--r--  2 richard         6 Feb 13 20:20 jane
   ^              ^
   |              this says there are two links to the file
   this is the "inode number" of the file, 
   which identifies it uniquely (more or less)

% echo xyzzy >fred
% cat fred
xyzzy
% cat jane
xyzzy
skye% ls -li fred jane 
  571 -rw-r--r--  2 richard         6 Feb 13 20:23 fred
  571 -rw-r--r--  2 richard         6 Feb 13 20:23 jane
   ^
   still the same file

% rm jane
% echo plugh >jane
% cat fred
xyzzy
% cat jane
plugh
% ls -li fred jane
  571 -rw-r--r--  1 richard         6 Feb 13 20:23 fred
  586 -rw-r--r--  1 richard         6 Feb 13 20:25 jane
   ^
   now they're different files

-- Richard
-- 
Richard Tobin,                         JANET: R.Tobin at uk.ac.ed             
AI Applications Institute,             ARPA:  R.Tobin%uk.ac.ed at nss.cs.ucl.ac.uk
Edinburgh University.                  UUCP:  ...!ukc!ed.ac.uk!R.Tobin



More information about the Comp.unix.questions mailing list