tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

shmod



When errno is EACCESS, it's sometimes unclear what the problem is when
the mode itself is OK.  A file with mode 777 may be unreadable if there
is one directory in the pathname that prevents access to it.  

I propose a new utility, shmod, to show the mode of the path to a
file and report problems.  The script below is simple version: correct,
but not robust to weird filenames.  

If others agree shmod deserves consideration for /usr/bin, I would
rewrite it in C to 

1.  make it impervious to weird filenames
2.  support mulitple filenames
3.  quiet default: if no error, print nothing and exec /bin/ls on the
filename(s), else exec ls on first problem directory
4.  -q option to print nothing if no error
5.  -v option to print mode of each pathname component (as below)

--jkl

Example output:

$ shmod a/E
drwxrwxr-x  3 jklowden  wheel  512 Mar 15 15:44 a
lrwxrwxr-x  1 jklowden  wheel  7 Mar 15 15:44 a/E -> b/c/d/e
drwx---r-x  3 nobody  nobody  512 Mar 15 15:42 b
dr-x------  3 jklowden  wheel  512 Mar 15 13:39 b/c
drwxrwxr-x  2 jklowden  wheel  512 Mar 15 13:40 b/c/d
-rw-rw-r--  1 jklowden  wheel  4 Mar 15 13:40 b/c/d/e



[snip]
#! /bin/sh
set -e

test "$1" || { echo syntax: shmod pathname >&2; exit 1; }

shmod() {
    TOP=$(echo $1 | awk -F/ '{print $1}')

    if [ ! "${TOP}" ]
    then
	TOP=/
    else
	unset TOP
    fi

    PATHNAME=""

    for D in $(echo "$1" | awk -v TOP=${TOP} \
			       '{gsub("/", " ");  print TOP, $0}')
    do
	PATHNAME="${PATHNAME}$D"
	ls -ld "${PATHNAME}"

	if [ -h "${PATHNAME}" ]
	then
	    cd "${PATHNAME%/*}"
	    shmod "$(readlink "$D")"
	elif [ -d "${PATHNAME}" ]
	then
	    (cd "${PATHNAME}")
	    PATHNAME="${PATHNAME}"/
	else
	    exec 3< "${PATHNAME}"
	fi
    done
}

shmod "$1"
[pins]



Home | Main Index | Thread Index | Old Index