Subject: misc/2087: allow /bin/test to check for locally mounted files
To: None <gnats-bugs@NetBSD.ORG>
From: Brook Milligan <brook@trillium.NMSU.Edu>
List: netbsd-bugs
Date: 02/16/1996 09:40:16
>Number:         2087
>Category:       misc
>Synopsis:       allow /bin/test to check for locally mounted files
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    misc-bug-people (Misc Bug People)
>State:          open
>Class:          change-request
>Submitter-Id:   net
>Arrival-Date:   Fri Feb 16 12:05:04 1996
>Last-Modified:
>Originator:     Brook Milligan
>Organization:
Brook G. Milligan                      Internet:  bmilliga@nmsu.edu
Department of Biology
New Mexico State University            Telephone:  (505) 646-7980
Las Cruces, New Mexico  88003  U.S.A.  FAX:        (505) 646-5665
>Release:        1.1
>Environment:
	
System: NetBSD aster.nmsu.edu 1.1 NetBSD 1.1 (ASTER) #0: Wed Jan 17 10:33:17 MST 1996 root@trillium.nmsu.edu:/usr/src/sys/arch/i386/compile/ASTER i386


>Description:
	

In system scripts it is sometimes useful to determine whether a file
is locally mounted or not.  For example, if a script is to be used
across several hosts that cross-mount filesystems, it may wish to only
act on a local version.  This prevents every host sharing the file
from clobbering it.  Addition of a simple test based on statfs(2)
allows checking this aspect of files in the same context as normally
used (e.g., /bin/test).

>How-To-Repeat:
	
>Fix:
	

The following patch adds the test to /bin/test (and updates the man page and test script).

*** ./src/bin/test/TEST.csh.orig	Fri Oct 13 17:44:55 1995
--- ./src/bin/test/TEST.csh	Fri Feb 16 09:15:49 1996
***************
*** 36,41 ****
--- 36,44 ----
  echo 't -g /bin/ps'
  t -g /bin/ps
  
+ echo 't -l test.c'
+ t -l test.c
+ 
  echo 't -n ""'
  t -n ""
  echo 't -n "hello"'
*** ./src/bin/test/test.1.orig	Fri Oct 13 17:44:55 1995
--- ./src/bin/test/test.1	Fri Feb 16 09:10:31 1996
***************
*** 91,96 ****
--- 91,100 ----
  True if
  .Ar file
  exists and its sticky bit is set.
+ .It Fl l Ar file
+ True if
+ .Ar file
+ exists and is mounted locally.
  .It Fl n Ar string
  True if the length of
  .Ar string
*** ./src/bin/test/test.c.orig	Fri Oct 13 17:44:56 1995
--- ./src/bin/test/test.c	Fri Feb 16 09:13:51 1996
***************
*** 16,21 ****
--- 16,23 ----
  
  #include <sys/types.h>
  #include <sys/stat.h>
+ #include <sys/param.h>
+ #include <sys/mount.h>
  #include <unistd.h>
  #include <ctype.h>
  #include <errno.h>
***************
*** 34,40 ****
  		| "(" oexpr ")"
  		;
  	unary-operator ::= "-r"|"-w"|"-x"|"-f"|"-d"|"-c"|"-b"|"-p"|
! 		"-u"|"-g"|"-k"|"-s"|"-t"|"-z"|"-n"|"-o"|"-O"|"-G"|"-L"|"-S";
  
  	binary-operator ::= "="|"!="|"-eq"|"-ne"|"-ge"|"-gt"|"-le"|"-lt"|
  			"-nt"|"-ot"|"-ef";
--- 36,42 ----
  		| "(" oexpr ")"
  		;
  	unary-operator ::= "-r"|"-w"|"-x"|"-f"|"-d"|"-c"|"-b"|"-p"|
! 		"-u"|"-g"|"-k"|"-s"|"-t"|"-z"|"-n"|"-o"|"-O"|"-G"|"-L"|"-S"|"-l";
  
  	binary-operator ::= "="|"!="|"-eq"|"-ne"|"-ge"|"-gt"|"-le"|"-lt"|
  			"-nt"|"-ot"|"-ef";
***************
*** 64,69 ****
--- 66,72 ----
  	FILEQ,
  	FILUID,
  	FILGID,
+ 	FILLOC,
  	STREZ,
  	STRNZ,
  	STREQ,
***************
*** 117,122 ****
--- 120,126 ----
  	{"-G",	FILGID,	UNOP},
  	{"-L",	FILSYM,	UNOP},
  	{"-S",	FILSOCK,UNOP},
+ 	{"-l",  FILLOC, UNOP},
  	{"=",	STREQ,	BINOP},
  	{"!=",	STRNE,	BINOP},
  	{"<",	STRLT,	BINOP},
***************
*** 339,344 ****
--- 343,349 ----
  	char *nm;
  	enum token mode;
  {
+ 	struct statfs fs;
  	struct stat s;
  	int i;
  
***************
*** 354,359 ****
--- 359,366 ----
  
  	if (stat(nm, &s) != 0) 
  		return 0;
+ 	if (statfs(nm, &fs) != 0)
+ 		return 0;
  
  	switch (mode) {
  	case FILRD:
***************
*** 405,410 ****
--- 412,419 ----
  		return s.st_uid == geteuid();
  	case FILGID:
  		return s.st_gid == getegid();
+ 	case FILLOC:
+ 		return (fs.f_flags & MNT_LOCAL) != 0;
  	default:
  		return 1;
  	}
>Audit-Trail:
>Unformatted: