added file handling functions

This commit is contained in:
pyr0ball 2023-05-08 22:17:36 -07:00
parent 868ff8831f
commit 87d3e369cd

View file

@ -2,7 +2,7 @@
# pyr0ball script functions library # pyr0ball script functions library
# Initial Vars # Initial Vars
functionsrev=1.1.13 functionsrev=1.1.14
#scriptname="${BASH_SOURCE[0]##*/}" #scriptname="${BASH_SOURCE[0]##*/}"
#rundir="${BASH_SOURCE[0]%/*}" #rundir="${BASH_SOURCE[0]%/*}"
#rundir_absolute=$(cd $rundir && pwd) #rundir_absolute=$(cd $rundir && pwd)
@ -446,6 +446,94 @@ logger(){
) )
} }
# File handling functions
take-backup(){
name="$1"
if [[ $update_run != true ]] ; then
# Check if a backup file or symbolic link already exists
if [[ -e "$name.bak" || -L "$name.bak" ]]; then
run boxline " $name.bak backup already exists"
else
# Check if the file is a hidden file (starts with a dot)
if [[ "$name" == .* ]]; then
# Add a dot to the beginning of the backup file name
backup_name=".${name}.bak"
else
# Create the backup file name by appending ".bak" to the original file name
backup_name="${name}.bak"
fi
# Copy the file to the backup file with preservation of file attributes
run cp -p "$name" "$backup_name"
# Add the original file to the list of backup files
backup_files+=("$name")
# Log the original file name to the backup file list file
run echo "$name" >> "$rundir/backup_files.list"
fi
fi
}
restore-backup(){
run echo "${#backup_files[@]}"
for file in "${backup_files[@]}" ; do
run cp "$file".bak $file
run echo "$file is restored"
done
backup_files=()
if [ -f $rundir/backup_files.list ] ; then
run rm $rundir/backup_files.list
fi
}
install-file(){
local _source="$1"
local _destination="$2"
local _source_root="$3"
local _filename=${_source##*/}
local _destination_file=${_destination}/${_filename#${_source_root}}
installed_files+=("${_destination_file}")
if [[ $update_run == true ]] ; then
run boxline "$scriptname: added file ${_destination_file} to list"
else
run cp -p $_source $_destination_file && boxline "Installed ${_filename}" || warn "Unable to install ${_filename}"
run echo "${_destination_file}" >> $rundir/installed_files.list
fi
}
install-dir() {
local _source="$1"
local _destination="$2"
installed_dirs+=("$_source -> $_destination")
# Iterate through all files in the source directory recursively
while IFS= read -r -d '' source_file; do
# Construct the destination file path by removing the source directory path
# and appending it to the destination directory path
local _filename="${source_file#${_source}}"
local destination_file="${_destination}/${source_file#${_source}}"
# Create the parent directory of the destination file if it doesn't exist
# Log the destination file path to the logfile
#echo "$destination_file" >> "$logfile"
installed_files+=($destination_file)
if [[ $update_run == true ]] ; then
run boxline "$scriptname: added file ${destination_file} to list"
else
run mkdir -p "$(dirname "$destination_file")"
run cp -p ${_source}${_filename} $destination_file && boxline "Installed ${_filename}" || warn "Unable to install ${_filename}"
run echo "${destination_file}" >> $rundir/installed_files.list
fi
done < <(find "$_source" -type f -print0)
}
clone-repo(){
# git clone <source url> <destination>
local _url=$1
local _destination=$2
if [ ! -z $_destination ] ; then
run mkdir -p $_destination
fi
run git clone --recurse-submodules $_url $_destination
}
ctrl_c(){ ctrl_c(){
echo -e "\n" echo -e "\n"
fail "User interrupted with Ctrl-C" fail "User interrupted with Ctrl-C"