-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecycle
More file actions
executable file
·214 lines (185 loc) · 7.25 KB
/
recycle
File metadata and controls
executable file
·214 lines (185 loc) · 7.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/bin/bash
################################
# UNIXycle - A UNIX recycle bin
################################
# Author: Elliott Steer
# Docs: https://github.com/essteer/unixycle/blob/main/README.md
#
# `recycle` creates a recyclebin directory in a user's $HOME and
# stores target files there with their inode numbers appended. It
# can recursively recycle directory contents via the -r option.
#
# File usage:
# "$ bash recycle -iv file1.txt file2.doc moreFiles*"
#
# Directory usage:
# "$ bash recycle -irv dir1"
#
# Data for recycled files is stored in $HOME/.restore.info.
# See the accompanying `restore` script for details.
################################
log_filepath="logs/recycle.log"
recycle_script_abs_path="$(realpath $0)"
recyclebin_abs_path="$HOME/recyclebin"
restore_info_abs_path="$HOME/.restore.info"
function timestamp() {
date +"%Y-%m-%d %H:%M:%S"
}
if [[ ! -f "${log_filepath}" ]]; then
mkdir -p logs
touch "${log_filepath}"
echo "$(timestamp) - INFO - recycle - Log file created at ${log_filepath}" >> "${log_filepath}"
fi
mkdir -p "$recyclebin_abs_path" # create recyclebin if it does not exist
if ! [ -e "$restore_info_abs_path" ] ; then
touch "$restore_info_abs_path" # create .restore.info if it does not exist
echo "$(timestamp) - INFO - recycle - .restore.info created at ${restore_info_abs_path}" >> "${log_filepath}"
fi
if [ $# -eq 0 ] ; then
echo "$(timestamp) - WARNING - recycle - Missing operand: no files or directories provided" | tee -a "${log_filepath}"
echo "Usage: bash $(basename "$0") [OPTIONS] [FILE|DIR]"
exit 1 # exit if no args provided
fi
function validate_file() {
# validates: file exists, file is file, file is not recycle
if ! [ -e "$1" ] ; then
echo "$(timestamp) - WARNING - recycle - Cannot recycle '$1': no such file" | tee -a "${log_filepath}"
return 3 # exit function if file does not exist
elif [ -d "$1" ] && [ "$recursive" != true ] ; then
echo "$(timestamp) - WARNING - recycle - Cannot recycle '$1': use -r to recycle directories" | tee -a "${log_filepath}"
return 4 # exit function if dir provided
fi
target_abs_path="$(realpath $1)"
if [ "$target_abs_path" = "$recycle_script_abs_path" ] ; then
echo "$(timestamp) - WARNING - recycle - Cannot recycle recycle script: operation aborted" | tee -a "${log_filepath}"
exit 5 # exit script if recycle script provided
elif [ "$target_abs_path" = "$(dirname $(realpath $0))/restore" ] ; then
echo "$(timestamp) - WARNING - recycle - Cannot recycle restore script: operation aborted" | tee -a "${log_filepath}"
exit 6 # exit script if restore script provided
fi
echo "$(timestamp) - INFO - recycle - '$1' passed validation" >> "${log_filepath}"
return 0 # is valid file
}
function recycle_file() {
# recycles a previously validated file
target_abs_path="$(realpath $1)"
target_filename="$(basename $1)"
target_inode="$(ls -i $target_abs_path | cut -d' ' -f1)"
recycle_state_name="${target_filename}_${target_inode}"
recycle_state_abs_path="${recyclebin_abs_path}/${recycle_state_name}"
name_in_restore_info="${recycle_state_name}:${target_abs_path}"
# move target file to recyclebin under recycle_state_name
mv "$target_abs_path" "$recycle_state_abs_path"
if [ $? -eq 0 ] ; then
# store name in .restore.info
echo "$name_in_restore_info" >> "$restore_info_abs_path"
echo "$(timestamp) - INFO - recycle - Moved ${target_filename} to recyclebin as ${recycle_state_name}" >> "${log_filepath}"
else
echo "$(timestamp) - ERROR - recycle - Failed to recycle ${target_filename} with exit status $?" | tee -a "${log_filepath}"
exit 7
fi
}
function recycle_dir() {
# recycles all files in a directory via recursion
for j in "$1"/* ; do
# validate to prevent deletion of recycle script in sub dir
if validate_file "$j" ; then
if [ -f "$j" ] ; then
recycle_file "$j"
if [ "$verbose" = true ] ; then
echo "recycled '$j'"
fi
elif [ -d "$j" ] ; then
recycle_dir "$j" # recursion
fi
fi
done
rm -rf "$1" # delete dir once all contents recycled
if [ $? -eq 0 ] ; then
echo "$(timestamp) - INFO - recycle - Removed parent directory $1" >> "${log_filepath}"
return 0
else
echo "$(timestamp) - ERROR - recycle - Failed to recycle parent directory $1 with exit status $?" | tee -a "${log_filepath}"
exit 8
fi
}
function show_ascii() {
# displays ASCII art
cat << "EOF"
.,aadd88888888bbaa,.
,ad8888888888888888888888ba,
,a888888888888888888888888888888a,
a888888888888888888888888888888888888a
a888888888888P` `8, `Y8888888888a
d888888888888` )8, `88888888888b
d888888888888` ,888, ,8888888888b
d888888888888` ,88888a ,888888888888b
I88888888888888ba, ,88888P` ,88888888888888I
,888888888```````Y88ba88888888888888P` `Y8888888888,
I888888888P Y88P 9P`` `Y888888888I
888888888P `8 UNIXycle b `Y888888888
88888888P ,d888 88b )888888888
I88888888b ,d88888888888888888b ,d88888888I
`888888888b ,d888888888888P 88888baaad8888888888`
I888888888b ,8` 888P d888888888I
Y888888888b,8` 88P d888888888P
Y88888888888 88b d888888888P
Y8888888888b, 888b d888888888P
`88888888888888888888888b 888888888888888`
`888888888888888888888888888888888888`
``888888888888888888888888888888``
``Y8888888888888888888888P``
``""YY88888888PP""``
EOF
}
function show_help() {
# displays options
echo "Usage: bash $(basename "$0") [OPTIONS] [FILE|DIR]"
echo
echo "Options:"
echo " -a Display ASCII art"
echo " -h Display help"
echo " -i Run in interactive mode to require confirmation before each operation"
echo " -r Run in recursive mode to recycle directory contents"
echo " -v Run in verbose mode to view process messages"
}
OPTIND=1
while getopts ":ahirv" opt
do
case $opt in
a|A)
show_ascii
;;
h|H)
show_help
exit 0
;;
i|I) interactive=true ;;
r|R) recursive=true ;;
v|V) verbose=true ;;
\?) echo "invalid option: $OPTARG"
show_help
exit 2 ;;
esac
done
shift $((OPTIND-1))
for i in "$@"
do
if validate_file "$i" ; then # validate each file before attempting to recycle
if [ "$interactive" = true ] ; then
read -p "recycle '$i'? y/n " recycleDecision
if [ "$recycleDecision" != "y" ] && [ "$recycleDecision" != "Y" ] ; then
continue ; # continue iteration if user doesn't affirm deletion
fi
fi
if [ -d "$i" ] && [ "$recursive" = true ] ; then
recycle_dir "$i" # recycle dir contents (-r)
else
recycle_file "$i" # recycle file
fi
if [ "$verbose" = true ] ; then
echo "recycled '$i'"
fi
fi
done
exit 0