#!/usr/bin/ksh93 ################################################################ function usagemsg_getfilestruct_k93 { print -- "" print -- "${1:+Program: ${1}} ${2:+Version: ${2}}" print -- "" print -- "This script builds arrays of values representing directory and file" print -- "names, with owner, group, and permissions. It will also generate the" print -- "commands necessary to recreate the directory structure, and to restore" print -- "the owner, group, and permissions to all directories and files under a" print -- "top level directory." print -- "" print -- "Usage: ${1} [-v|-V] [-a] [-c] [-s] [-d|-f] [-R /Alt_Root] TopDir" print -- "" print -- " Where '-v' = Verbose mode" print -- " '-V' = Very Verbose mode" print -- " '-a' = Generate Arrays of values as Output" print -- " (Default: array definitions)" print -- " '-c' = Generate commands to create directory structures" print -- " (Default: array definitions)" print -- " '-s' = Generate commands to create Symbolic Links" print -- " (Default: OFF)" print -- " '-d' = Only gather directory structure information" print -- " (Default: Files and Directories)" print -- " '-f' = Only gather file information" print -- " (Default: Files and Directories)" print -- " '-R /ALT_ROOT' = Add an alternate root directory to every path" print -- " This directory does not need to actually exist" print -- " Useful for chroot'ed dirs or ALT_DISK filesystems" print -- " (Default: NUL)" print -- "" print -- " TopDir = The full path directory name from which to" print -- " extract the directory and file structure" print -- "" print -- "Author: Dana French (dfrench@mtxia.com)" print -- "Copyright 2007-2015, All Rights Reserved" print -- "" print -- "\"AutoContent\" enabled" print -- "" } ################################################################ #### #### Description: #### #### This script builds arrays of values representing directory and file #### names, with owner, group, and permissions. It will also generate the #### commands necessary to recreate the directory structure, and to restore #### the owner, group, and permissions to all directories and files under a #### top level directory. #### #### This script can operate in two modes: Array or Command generating. #### In "Array" mode, Korn Shell 93 code is generated is used #### to define an array of values containing the directory information #### and attributes. In "Command" mode, unix style commands are generated #### to recreate the directory structure and attributes. The default is #### "Array" mode. #### #### Assumptions: #### #### If you are using this script to build arrays of values, it is assumed #### the number of files in the specified top level directory will be limited #### on only a few thousand. This is because the values are stored in korn #### shell arrays which are limited in size, depending upon the platform. #### You will need to test this on your own system to determine the actual #### limitations. #### #### Dependencies: #### #### This script is dependent upon the following Unix utilities: #### ksh93 #### ls #### find #### uname #### #### Products: #### #### This script generates Korn Shell 93 Compliant commands that can #### be used to define an array of values, or this script can generate #### Unix commands to create the directory structure and its attributes. #### #### Configured Usage: #### #### This script can be run from the command line, used as a function, or #### called from a function library. #### #### Details: #### ################################################################ function getfilestruct_k93 { typeset VERSION="3.1-ksh93" typeset TRUE="1" typeset FALSE="0" typeset VERBOSE="${FALSE}" typeset VERYVERB="${FALSE}" typeset GENCMDS="${FALSE}" typeset GENSLNK="${FALSE}" typeset GENARYS="${FALSE}" typeset SHWDIRS="${TRUE}" typeset SHWFILS="${TRUE}" typeset DCNT="0" typeset FCNT="0" typeset SCNT="0" typeset ALT_ROOT="" typeset CMD="" typeset DGROUP typeset DIRID1 typeset DIRIDX typeset DIRNAME typeset DMODE typeset DOWNER typeset GPERMS typeset LNAME typeset MACHNAME typeset OPERMS typeset PERMS typeset RAWFNAME typeset RAWGROUP typeset RAWOWNER typeset RAWPERMS typeset SNAME typeset UPERMS while getopts ":vVcasdfR:" OPTION do case "${OPTION}" in 'a') GENARYS="${TRUE}";; 'c') GENCMDS="${TRUE}";; 's') GENSLNK="${TRUE}";; 'd') SHWFILS="${FALSE}";; # Yes, -d turns off showing files 'f') SHWDIRS="${FALSE}";; # Yes, -f turns off showing dirs 'R') ALT_ROOT="${OPTARG}";; 'v') VERBOSE="${TRUE}";; 'V') VERBOSE="${TRUE}" VERYVERB="${TRUE}";; '?') usagemsg_getfilestruct_k93 "${0}" "${VERSION}" && return 1 ;; esac done shift $(( ${OPTIND} - 1 )) ################################################################ trap "usagemsg_getfilestruct_k93 ${0} ${VERSION} " EXIT MACHNAME=$( uname -n ) DIRID1="${1:?ERROR: Top level directory not specified}" if [[ ! -d "${DIRID1}" ]] then print -- "# ERROR: \"${DIRID1}\" is not a directory or does not exist" return 4 fi if (( GENARYS == FALSE )) && (( GENCMDS == FALSE )) then GENARYS="${TRUE}" fi if (( SHWFILS == FALSE )) && (( SHWDIRS == FALSE )) then print -u 2 -- "# ERROR: Do not specify both -d and -f, you must specify one or the other, or neither." return 2 fi trap "-" EXIT ################################################################ DCNT="0" FCNT="0" SCNT="0" DIRIDX="${DIRID1##*/}" (( ${#DIRIDX} > 8 )) && typeset -L8 DIRIDX="${DIRIDX//[[:space:]]/}" (( VERBOSE == TRUE )) && print -- "#!/usr/bin/ksh93" (( VERBOSE == TRUE )) && print -- "################################################################" (( VERBOSE == TRUE )) && print -- "# Program Name..........: ${0}" (( VERBOSE == TRUE )) && print -- "# Version...............: ${VERSION}" (( VERBOSE == TRUE )) && print -- "# Copyright Info........: Copyright 2007-2015 by Dana French, All Rights Reserved" (( VERBOSE == TRUE )) && print -- "# Local hostname........: ${MACHNAME}" (( VERBOSE == TRUE )) && print -- "# Top Level Directory...: ${DIRID1}" (( VERBOSE == TRUE )) && print -- "# Variable index name...: ${DIRIDX}" (( VERBOSE == TRUE )) && (( SHWFILS == TRUE )) && print -- "# Show Files............: TRUE" (( VERBOSE == TRUE )) && (( SHWFILS == FALSE )) && print -- "# Show Files............: FALSE" (( VERBOSE == TRUE )) && (( SHWDIRS == TRUE )) && print -- "# Show Directories......: TRUE" (( VERBOSE == TRUE )) && (( SHWDIRS == FALSE )) && print -- "# Show Directories......: FALSE" (( VERBOSE == TRUE )) && (( GENCMDS == TRUE )) && print -- "# Generate Commands.....: TRUE" (( VERBOSE == TRUE )) && (( GENCMDS == FALSE )) && print -- "# Generate Commands.....: FALSE" (( VERBOSE == TRUE )) && (( GENARYS == TRUE )) && print -- "# Generate Arrays.......: TRUE" (( VERBOSE == TRUE )) && (( GENARYS == FALSE )) && print -- "# Generate Arrays.......: FALSE" (( VERBOSE == TRUE )) && (( GENSLNK == TRUE )) && print -- "# Generate Sym Link CMDS: TRUE" (( VERBOSE == TRUE )) && (( GENSLNK == FALSE )) && print -- "# Generate Sym Link CMDS: FALSE" print -- "" print -- "ALT_ROOT=\"${ALT_ROOT}\"" ################################################################ #### #### The first thing the "getfilestruct" script does is to execute a "find" #### command to retrieve all the directories under the top level directory #### specified on the command line when the script was run. The "find" #### command produces "ls -ld" style output for the purpose of extracting and #### processing the attributes associated with each directory. #### ################################################################ (( SHWFILS == FALSE )) && (( SHWDIRS == TRUE )) && CMD='find "${DIRID1%/}" -type d -exec ls -ld {} \;' (( SHWFILS == TRUE )) && (( SHWDIRS == FALSE )) && CMD='find "${DIRID1%/}" ! -type d -exec ls -ld {} \;' (( SHWFILS == TRUE )) && (( SHWDIRS == TRUE )) && CMD='find "${DIRID1%/}" -exec ls -ld {} \;' (( SHWFILS == FALSE )) && (( SHWDIRS == FALSE )) && return 3 eval ${CMD} | while read -r -- RAWPERMS RAWLINKS RAWOWNER RAWGROUP RAWSIZE RAWDATE1 RAWDATE2 RAWDATE3 RAWFNAME do ################################################################ #### #### If the file is a symbolic link, generate the command to recreate the #### symbolic link and continue with the next file. #### ################################################################ if [[ "_${RAWPERMS}" == _l* ]] then LNAME="${RAWFNAME##* -> }" SNAME="${RAWFNAME% -> *}" [[ "_${LNAME}" != _/* ]] && LNAME="${SNAME%/*}/${LNAME}" if (( GENARYS == TRUE )) then print -- "\n#### symbolic link \"${RAWFNAME}\"" print -- "SYM_${DIRIDX}_SNAME[${SCNT}]=\"\${ALT_ROOT}${SNAME}\"" print -- "SYM_${DIRIDX}_FLINK[${SCNT}]=\"\${ALT_ROOT}${LNAME}\"" SCNT=$(( ${SCNT} + 1 )) fi if (( GENSLNK == TRUE )) then print -- "\n#### Generate symbolic link \"${RAWFNAME}\"" print -- "ln -s \"${LNAME}\" \"${SNAME}\"" else print -- "\n#### Skipping symbolic link \"${RAWFNAME}\"" print -- "# ln -s \"${LNAME}\" \"${SNAME}\"" fi continue fi ################################################################ #### #### The file owner and group are easily #### extracted from the "ls -ld" output, however the permissions require #### some processing. #### ################################################################ # extract the owner for the directory from the ls -l output DOWNER="${RAWOWNER}" # extract the group for the directory from the ls -l output DGROUP="${RAWGROUP}" # extract the permission settings for the directory from the ls -l output PERMS="${RAWPERMS:1:9}" # extract the name of the directory from the ls -l output DIRNAME="${RAWFNAME}" [[ "_${DIRNAME}" == *lost+found ]] && continue ################################################################ #### #### The permission settings are contained within the first 10 characters #### of the "ls -ld" output and are processed in 3 steps. The first character #### is the file type and is ignored, the next 3 characters are associated #### with the "user" or file owner permissions. The permissions are #### checked to see if the tacky bit or SUID bit is set. If so #### the appropriate permission setting is added to the permission string #### which will be used when directory permissions are set. #### ################################################################ # extract the user permission settings for the directory from the ls -ld output UPERMS="${PERMS:0:3}" # remove the dashes "-" from the user permissions UPERMS="${UPERMS//\-/}" # Convert lowercase "s" to "xs" in the user permissions UPERMS="${UPERMS//s/xs}" # Convert uppercase "S" to "s" in the user permissions UPERMS="${UPERMS//S/s}" # Convert lowercase "t" to "xt" in the user permissions UPERMS="${UPERMS//t/xt}" # Convert uppercase "T" to "t" in the user permissions UPERMS="${UPERMS//T/t}" ################################################################ #### #### The next set of 3 characters are associated with the "group" #### category of permissions. The permissions are checked to see #### if the tacky bit or SUID bit is set. If so the appropriate #### permission setting is added to the permission string which will #### be used when the directory permissions are set. #### ################################################################ # extract the group permission settings for the directory from the ls -ld output GPERMS="${PERMS:3:3}" # remove the dashes "-" from the group permissions GPERMS="${GPERMS//\-/}" # Convert lowercase "s" to "xs" in the group permissions GPERMS="${GPERMS//s/xs}" # Convert uppercase "S" to "s" in the group permissions GPERMS="${GPERMS//S/s}" # Convert lowercase "t" to "xt" in the group permissions GPERMS="${GPERMS//t/xt}" # Convert uppercase "T" to "t" in the group permissions GPERMS="${GPERMS//T/t}" ################################################################ #### #### The last set of 3 characters are associated with the "other" #### category of permissions. Again, the permissions are checked to see #### if the tacky bit or SUID bit is set. If so the appropriate permission #### setting is added to the permission string which will be used when the #### directory permissions are set. #### ################################################################ # extract the other permission settings for the directory from the ls -ld output OPERMS="${PERMS:6:3}" # remove the dashes "-" from the other permissions OPERMS="${OPERMS//\-/}" # Convert lowercase "s" to "xs" in the other permissions OPERMS="${OPERMS//s/xs}" # Convert uppercase "S" to "s" in the other permissions OPERMS="${OPERMS//S/s}" # Convert lowercase "t" to "xt" in the other permissions OPERMS="${OPERMS//t/xt}" # Convert uppercase "T" to "t" in the other permissions OPERMS="${OPERMS//T/t}" ################################################################ #### #### With the permission setting strings for each user category now known #### and extracted from the "ls -ld" output, a mnemonic "exact setting" #### string is constructed for use with the "chmod" command. #### ################################################################ # Build the mnemonic mode exact permission setting command DMODE="u=${UPERMS},g=${GPERMS},o=${OPERMS}" (( VERBOSE == TRUE )) && print -- "\n#### \n#### ${MACHNAME}:${DMODE}:${DOWNER}:${DGROUP}:${DIRNAME}" ################################################################ #### Depending upon the options specified when this script was run, #### either Korn Shell 93 compliant commands or Unix commands will #### be generated. #### #### The generated Korn Shell 93 Compliant commands can be used to #### define a variable array of values which contain the directory name #### and various attributes associated with each directory. #### #### The Unix commands generated consist of mkdir, chown, chgrp, and #### chmod, which can be used to create the directory and change its #### attributes to match those of the original directory. #### ################################################################ if [[ -d "${DIRNAME}" ]] then (( VERBOSE == TRUE )) && print -- "\nprint -- \"# Working on ${DIRNAME}\"" if (( GENARYS == TRUE )) then print -- "DIR_${DIRIDX}_DNAME[${DCNT}]=\"\${ALT_ROOT}${DIRNAME}\"" print -- "DIR_${DIRIDX}_OWNER[${DCNT}]=\"${DOWNER}\"" print -- "DIR_${DIRIDX}_GROUP[${DCNT}]=\"${DGROUP}\"" print -- "DIR_${DIRIDX}_CHMOD[${DCNT}]=\"${DMODE}\"" fi if (( GENCMDS == TRUE )) then print -- "mkdir -p \"\${ALT_ROOT}${DIRNAME}\"" print -- "chown ${DOWNER} \"\${ALT_ROOT}${DIRNAME}\"" print -- "chgrp ${DGROUP} \"\${ALT_ROOT}${DIRNAME}\"" print -- "chmod \"${DMODE}\" \"\${ALT_ROOT}${DIRNAME}\"" fi DCNT=$(( ${DCNT} + 1 )) else if (( GENARYS == TRUE )) then print -- "FIL_${DIRIDX}_FNAME[${FCNT}]=\"\${ALT_ROOT}${DIRNAME}\"" print -- "FIL_${DIRIDX}_OWNER[${FCNT}]=\"${DOWNER}\"" print -- "FIL_${DIRIDX}_GROUP[${FCNT}]=\"${DGROUP}\"" print -- "FIL_${DIRIDX}_CHMOD[${FCNT}]=\"${DMODE}\"" fi if (( GENCMDS == TRUE )) then print -- "chown ${DOWNER} \"\${ALT_ROOT}${DIRNAME}\"" print -- "chgrp ${DGROUP} \"\${ALT_ROOT}${DIRNAME}\"" print -- "chmod \"${DMODE}\" \"\${ALT_ROOT}${DIRNAME}\"" fi FCNT=$(( ${FCNT} + 1 )) fi done return 0 } ################################################################ getfilestruct_k93 "${@}"