changeset 3:3cdee66547d0

Enhancement: removes file name mangling. Enhancement: no longer changes the permissions on a file that already has the correct permissions.
author John Schneiderman <JohnMS@CodeGNU.com>
date Wed, 10 Mar 2010 08:34:59 +0000
parents 9ecf26c73fb8
children 1009f603958e
files ufpm.sh
diffstat 1 files changed, 104 insertions(+), 150 deletions(-) [+]
line wrap: on
line diff
--- a/ufpm.sh	Wed Mar 10 07:08:21 2010 +0000
+++ b/ufpm.sh	Wed Mar 10 08:34:59 2010 +0000
@@ -1,12 +1,12 @@
 #!/bin/bash
-#********************************************* ufpm ************************************************
-#**** Last Modified On: 17 Mar 2007 by John Schneiderman                        Version: 1.4.1 *****
+#********************************************* UFPM ************************************************
+#**** Last Modified On: 09 Mar 2010 by John Schneiderman                        Version: 1.5.0 *****
 #**** Desc: This script changes the files and folders to be appropriate for a users home folder*****
 #****       Changes the permissions of directories to 750, regular files to 640, and backup    *****
 #****       files (those with ~ at the end of the file name) to 440. Hidden files and folders  *****
 #****       have their group settings set to zero.                                             *****
 #***************************************************************************************************
-#****           Copyright (C) 2006 - 2007  John Schneiderman <JohnMS@member.fsf.org>           *****
+#****           Copyright (C) 2006, 2009, 2010  John Schneiderman <JohnMS@member.fsf.org>      *****
 #****                                                                                          *****
 #*****          This program is free software: you can redistribute it and/or modify           *****
 #*****          it under the terms of the GNU General Public License as published by           *****
@@ -58,7 +58,7 @@
 #****      ufpm:                                                                               *****
 #****           Added user and group changing options.                                         *****
 #****           Moved the copyright notice to the line below the programme name.               *****
-#****           Removed potentially confusing status messages witha "." at the end.            *****
+#****           Removed potentially confusing status messages with a "." at the end.           *****
 #****      changePermissions:                                                                  *****
 #****           Sets the owner to user if ENABLE_USER is true.                                 *****
 #****           Sets the group to group if ENABLE_GROUP is true.                               *****
@@ -67,98 +67,49 @@
 #****           Will exit if we can't list the home folder.                                    *****
 #**** 1.4.1 on 14 Sep 2009 by John Schneiderman                                                *****
 #****     ufpm:                                                                                *****
-#****          Fixed function delcarations.                                                    *****
-#****          Fixed function variable declartions.                                            *****
+#****          Fixed function declarations.                                                    *****
+#****          Fixed function variable declarations.                                           *****
 #****          Updated to GPL v3                                                               *****
+#**** 1.5.0 on 09 Mar 2010 by John Schneiderman                                                *****
+#****     ufpm:                                                                                *****
+#****          File name and directories are no longer mangled before parsing.                 *****
+#****     changePermissions:                                                                   *****
+#****          Files and directories that already have the correct permissions are left alone. *****
 #***************************************************************************************************
-shopt -s -o nounset                   #Don't allow the use of variables that haven't been declared
-shopt -s extglob                      #Allow the use of the extra pattern
+shopt -s -o nounset  #Don't allow the use of variables that haven't been declared
+shopt -s extglob  #Allow the use of the extra pattern
 
 #Programme Variables
 #___________________________________________________________________________________________________
-declare -rx script=${0##*/}                    #Name of the script
-declare -r version="1.4.1"                     #Current Version
-declare -r copyright_years="2006 - 2009"       #The current (C) years
-declare -r OPT_STRING="vhVseu:g:"              #Command line options
-declare OPT_RESULT="EMPTY"                     #Used to parse the options
-declare IS_VERBOSE="false"                     #Enables verbose mode output
-declare IS_SILENT="false"                      #Enables silent mode
-declare ENABLE_ELF="false"                     #Enables executable ELFs
-declare ENABLE_USER="false"                    #Enables the user setting
-declare ENABLE_GROUP="false"                   #Enables the group setting
-declare user=`id -nu`                          #Current user
-declare group=`id -nu`                         #Current group
-declare -ri filePermissions=640                #The permissions to set files to
-declare -ri backupFilePermissions=440          #The permissions to set backup files to
-declare -ri hiddenFilePermissions=600          #The permissions to set hidden files to
-declare -ri scriptFilePermissions=740          #The permissions to set script files to
-declare -ri elfFilePermissions=700             #The permissions to set elf files to
-declare -ri directoryPermissions=750           #The permissions to set directories to
-declare -ri hiddenDirectoryPermissions=700     #The permissions to set hidden directories to
-declare -r MANGLE="~_~"                        #The mangling sequence
-declare files="Empty"                          #All of the users files
-declare File="Empty"                           #A users file
-declare Path="Empty"                           #The current parent path we are examining.
-declare FUNCTION_RESULT="Empty"                #The result from a function
+declare -rx script=${0##*/}  #Name of the script
+declare -r version="1.5.0"  #Current Version
+declare -r copyright_years="2006 - 2010"  #The current (C) years
+declare -r OPT_STRING="vhVseu:g:"  #Command line options
+declare OPT_RESULT="EMPTY"  #Used to parse the options
+declare IS_VERBOSE="false"  #Enables verbose mode output
+declare IS_SILENT="false"  #Enables silent mode
+declare ENABLE_ELF="false"  #Enables executable ELFs
+declare ENABLE_USER="false"  #Enables the user setting
+declare ENABLE_GROUP="false"  #Enables the group setting
+declare user=`id -nu`  #Current user
+declare group=`id -nu`  #Current group
+declare -ri filePermissions=640  #The permissions to set files to
+declare -ri backupFilePermissions=440  #The permissions to set backup files to
+declare -ri hiddenFilePermissions=600  #The permissions to set hidden files to
+declare -ri scriptFilePermissions=740  #The permissions to set script files to
+declare -ri elfFilePermissions=700  #The permissions to set elf files to
+declare -ri directoryPermissions=750  #The permissions to set directories to
+declare -ri hiddenDirectoryPermissions=700  #The permissions to set hidden directories to
+declare files="Empty"  #All of the users files
+declare File="Empty"  #A users file
+declare Path="Empty"  #The current parent path we are examining.
+declare FUNCTION_RESULT="Empty"  #The result from a function
 #Programme Commands
 #___________________________________________________________________________________________________
-# We have none.
+# We currently have none.
 
 #Functions
 #___________________________________________________________________________________________________
-function mangleFilenames()
-#Mangles any file name that contains spaces in the users home folder.
-#All the mangled file names are placed into the function result.
-{
-    local fileListing #Hold all the file names of the user.
-
-    #If TMPDIR doesn't exist use /tmp as the default value.
-    fileListing=`mktemp ${TMPDIR:="/tmp"}/${user}s_files.XXXXXX`
-    # Make sure we list the correct home folder
-    if [ $ENABLE_USER = "true" ]
-    then
-        ls --almost-all --recursive --escape /home/${user} > $fileListing
-    else
-        ls --almost-all --recursive --escape $HOME > $fileListing
-    fi
-    if [ $? -ne 0 ]
-    then
-        printf "Failed to list the users home folder!\n"
-        exit 2
-    fi
-    FUNCTION_RESULT=`cat $fileListing | sed s/\ /${MANGLE}/g`
-    rm -f $fileListing
-}
-readonly -f mangleFilenames
-declare -t mangleFilenames
-
-function demangle()
-#Undoes the mangling of the file name.
-#The first parameter is the file name.
-#The demangled name is placed into the function result.
-#Returns a 0 on success and a 1 on failure.
-{
-    local mangled=$1
-
-    if [[ $mangled = *$MANGLE* ]]
-    then
-        mangled="${mangled//$MANGLE/ }"         #Replace the mangling with a space
-        mangled="${mangled//\\}"                #Remove the slash for the file\ name situation
-        if [ $? -eq 0 ]
-        then
-            FUNCTION_RESULT=$mangled
-            return 0
-        else
-            return 1
-        fi
-    else
-        FUNCTION_RESULT=$mangled
-        return 0
-    fi
-}
-readonly -f demangle
-declare -t demangle
-
 function changePermissions()
 #Changes the permissions and/or owner(s) of the file.
 #Parameters are: path, file name, and permissions respectively.
@@ -167,22 +118,40 @@
     local filePath=$1
     local filename=$2
     local permissions=$3
+    local fileUser=`stat --format=%U $filePath/$filename`
+    local fileGroup=`stat --format=%G $filePath/$filename`
+    local currentFilePermissions=`stat --format=%a $filePath/$filename`
 
-    #Change the owner of the file
-    if [ $ENABLE_USER = "true" ]
-    then
+    if [ \( "$ENABLE_USER" = "true" \) -a \( "$fileUser" != "$user" \) ]
+    then  #Change the owner of the file
         chown $user "$filePath/$filename"
+        if [ $? -ne 0 ]
+        then
+            return 1
+        fi
     fi
-    #Change the group owner of the file
-    if [ $ENABLE_GROUP = "true" ]
-    then
+
+    if [ \( "$ENABLE_GROUP" = "true" \) -a \( "$fileGroup" != "$group" \) ]
+    then  #Change the group owner of the file
         chgrp $group "$filePath/$filename"
+        if [ $? -ne 0 ]
+        then
+            return 1
+        fi
     fi
-    if [ $IS_VERBOSE = "true" ]
+
+    if [ "$currentFilePermissions" != "$permissions" ]
+    then  # File permissions need to be changed
+        if [ \( "$IS_VERBOSE" = "true" \) ]
+        then
+            printf "Changing %s to %d\n" "$filename" "$permissions"
+        fi
+        chmod $permissions "$filePath/$filename"
+    elif [ \( "$IS_VERBOSE" = "true" \) ]
     then
-        printf "Changing file %s to %d\n" "$filename" "$permissions"
+        printf "Determined %s already is %d\n" "$filename" "$permissions"
     fi
-    chmod $permissions "$filePath/$filename"
+
     if [ $? -eq 0 ]
     then
         return 0
@@ -202,18 +171,18 @@
     local file=$2
 
     if [ ! -e "$path/$file" ]
-    then                    #Must have been a temp file
+    then  #Assumed to have been a temp file
         return 0
     elif [ -h "$path/$file" ]
-    then                    #File is a symbolic link
+    then  #File is a symbolic link
         return 0
     elif [ -S "$path/$file" ]
-    then                    #File is a socket
+    then  #File is a socket
         return 0
     elif [ -f "$path/$file" ]
-    then                    #Change permissions of a file
+    then  #Change permissions of a file
         if [[ "$path" = *tmp* ]]
-        then                #Don't change temporary files
+        then  #Don't change temporary files
             return 0
         elif [[ "$path" = *temp* ]]
         then
@@ -221,29 +190,29 @@
         fi
         isSpecialFile "$path" "$file"
         if [ "$FUNCTION_RESULT" = "script" ]
-        then    #Script file
+        then  #Script file
             changePermissions "$path" "$file" "$scriptFilePermissions"
         elif [ "$FUNCTION_RESULT" = "elf" ]
-        then #ELF file
+        then  #ELF file
             changePermissions "$path" "$file" "$elfFilePermissions"
         elif [[ "$file" = *~ ]]
-        then                 #Backup file
+        then  #Backup file
             changePermissions "$path" "$file" "$backupFilePermissions"
         elif [[ "$file" = .* ]]
-        then                 #Hidden file
+        then  #Hidden file
             changePermissions "$path" "$file" "$hiddenFilePermissions"
         elif [[ "$path" = *.ssh* ]]
-        then             #ssh file
+        then  #ssh file
             changePermissions "$path" "$file" "$hiddenFilePermissions"
-        else                                           #Normal file
+        else  #Normal file
             changePermissions "$path" "$file" "$filePermissions"
         fi
     elif [ -d "$path/$file" ]
-    then                    #Change permissions of a directory
+    then  #Change permissions of a directory
         if [[ "$file" = .* ]]
-        then                   #Hidden directory
+        then  #Hidden directory
             changePermissions "$path" "$file" "$hiddenDirectoryPermissions"
-        else                                           #Normal directory
+        else  #Normal directory
             changePermissions "$path" "$file" "$directoryPermissions"
         fi
     else
@@ -287,16 +256,17 @@
 
 #Sanity Checks
 #___________________________________________________________________________________________________
-while getopts "$OPT_STRING" OPT_RESULT; do  #Parse command line options
+while getopts "$OPT_STRING" OPT_RESULT
+do  #Parse command line options
     case $OPT_RESULT in
-    e) #Enable ELFs
+    e)  #Enable ELFs
         ENABLE_ELF="true"
         ;;
-    g)
+    g)  #Group name to use for files and directories
         group="$OPTARG"
         ENABLE_GROUP="true"
         ;;
-    h) #Show help
+    h)  #Show help
         printf "usage: $script [options]\n"
         printf "Options:\n"
         printf "  -e                       Enables ELFs to be executable.\n"
@@ -305,31 +275,31 @@
         printf "For bug reporting instructions, please see the man page.\n"
         exit 0
         ;;
-    s) #Enable Silent mode
+    s)  #Enable Silent mode
         IS_SILENT="true"
         ;;
-    u)
+    u)  #User name to use for files and directories
         user="$OPTARG"
         ENABLE_USER="true"
         ;;
-    v) #Version information
+    v)  #Version information
         printf "$script version $version\nCopyright (C) ${copyright_years} John Schneiderman\n"
         exit 0
         ;;
-    V) #Enable verbose mode
+    V)  #Enable verbose mode
         IS_VERBOSE="true"
         ;;
-    \?) #Invalid argument given
+    \?)  #Invalid argument given
         exit 2
         ;;
     *)
-        printf "$script:$LINENO: Unhandled argument."
+        printf "Passed argument is not known."
         exit 2
         ;;
     esac
 done
 if test -z "$BASH"
-then          #Shell check
+then  #Shell check
     printf "$script:$LINENO: Please run this script with the BASH shell.\n" >&2
     exit 2
 fi
@@ -343,44 +313,28 @@
 printf "under certain conditions; see the COPYING file for details,\n"
 printf "or the Free Software Foundation's GPL.\n\n"
 
-# Make sure we tell them the correct home folder
+# Display the home directory being worked on
 if [ $ENABLE_USER = "true" ]
 then
     printf "$script is examining files in /home/${user}...\n"
 else
     printf "$script is examining files in $HOME...\n"
 fi
-mangleFilenames                         #Mangle the names so we can use files with spaces.
-files=$FUNCTION_RESULT
+
 printf "$script is setting the file permissions...\n"
-for File in $files;                     #Convert the files in the users home folder
+### TODO need to parse directory structure into a temporary file and parse by reading that file
+for File in `ls --almost-all --recursive --escape /home/${user}/`
 do
-    if [[ $File = *: ]]
-    then         #Handle directory mangling
-        Path="${File//:}"             #Remove the colon
-        demangle $Path
-        if [ $? -eq 0 ]
-        then        #Did we succeed in demangling the directory
-            Path=$FUNCTION_RESULT
-            if [ $IS_SILENT = "false" ]
-            then
-                printf "Examining directory $Path\n"
-            fi
-            continue
-        else
-            printf "Failed to demangle $Path\n"
-            exit 2
+    if [[ "$File" = *: ]]
+    then
+        Path="${File//:}"
+        if [ "$IS_SILENT" = "false" ]
+        then
+            printf "Examining directory %s\n" "$Path"
         fi
-    else                               #Handle file mangling
-        demangle $File
-        if [ $? -eq 0 ]
-        then        #Did we succeed in demangling the filename
-            File=$FUNCTION_RESULT
-            modifyFileTypePermissions "$Path" "$File"
-        else
-            printf "Failed to demangle $Path/$File\n"
-            exit 2
-        fi
+        continue
+    else
+        modifyFileTypePermissions "$Path" "$File"
     fi
-done ;
+done
 printf "$script is done setting permissions.\n"