Skip to content

Commit 2b8d649

Browse files
committed
Preparing release of rmtrash 1.8
1 parent f6a86b0 commit 2b8d649

File tree

3 files changed

+241
-180
lines changed

3 files changed

+241
-180
lines changed

README.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
rmtrash 1.7
1+
rmtrash 1.8
22
===========
33
Put files (and directories) in trash using the ```trash-put``` command in a way that is, otherwise as ```trash-put``` itself, compatible to GNUs ```rm``` and ```rmdir```.
44

@@ -58,11 +58,10 @@ rmdirtrash [OPTION]... [DIRECTORY...]
5858
* ```-p```, ```--parents```: remove DIRECTORY and its ancestors; e.g., ```rmdirtrash -p a/b/c``` is similar to ```rmdirtrash a/b/c a/b a```
5959
* ```-v```, ```--verbose```: output a diagnostic for every directory processed
6060

61-
Replacement options
62-
-------------------
63-
These options are not supposed to be used when calling ```rmtrash``` resp. ```rmdirtrash```. They help you to control how and in which cases ```rm``` resp. ```rmdir``` are replaced.
64-
* ```--forbid-root```: forbid user ```root``` to trash files/directories. When standard input is a terminal, the user is asked to pass the command to ```rm``` resp. ```rmdir```, otherwise the entire command is aborted
65-
* ```--forbid-root-force```: when user ```root``` trys to trash files/directories, the entire command is passed automatically to ```rm``` resp. ```rmdir```
61+
Replacement option
62+
------------------
63+
This option is not supposed to be used when calling ```rmtrash``` resp. ```rmdirtrash```. It helps you to control how and in which cases ```rm``` resp. ```rmdir``` are replaced.
64+
* ```--forbid-root[=HOW]```: forbid user ```root``` to trash files. When standard input is a terminal, ```ask-forbid``` and ```ask-pass``` will question the user to pass the command to ```/bin/rm```. When standard input is no terminal, ```ask-forbid``` will abort the command, whereas ```ask-pass``` will pass the command to ```/bin/rm```. Use ```pass``` to pass all commands of user ```root``` to ```/bin/rm```. If user ```root``` should never trash files, use ```always```. Without ```HOW```, ```ask-forbid``` is assumed
6665

6766
Additional Notes
6867
----------------
@@ -75,7 +74,7 @@ Typically you won't notice a time delay when using ```rmtrash``` and ```rmdirtra
7574

7675
License & Copyright
7776
-------------------
78-
Copyright (C) 2011-2013 Daniel Rudolf <http://www.daniel-rudolf.de/>
77+
Copyright (C) 2011-2014 Daniel Rudolf <http://www.daniel-rudolf.de/>
7978

8079
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 the Free Software Foundation, version 3 of the License only.
8180

rmdirtrash

Lines changed: 118 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/bash
22
# rmdirtrash - `rmdir' compatible layer for `trash'
3-
# Version 1.7 (build 20131129)
3+
# Version 1.8 (build 20140601)
44
#
55
#
66
# SHORT DESCRIPTION:
@@ -10,16 +10,18 @@
1010
#
1111
# DEPENDENCIES:
1212
# - `trash' or `trash-put', provided by the package `trash-cli'
13-
# - `dpkg', provided by the package `dpkg'
1413
#
1514
# Note that there are many more dependencies. Nearly every distribution meets
1615
# those dependencies by default, so they are not listed here. Please also
1716
# note that this script uses options of POSIX commands, which are not part of
1817
# the POSIX standard. The extended variants provided by GNU are recommended.
1918
#
2019
# This script was tested with Ubuntu 10.04 LTS (Lucid Lynx) and
21-
# Ubuntu 12.04 LTS (Precise Pangolin) only. It SHOULD work great with all
22-
# Debian-based distributions.
20+
# Ubuntu 12.04 LTS (Precise Pangolin) only. It SHOULD work great with any
21+
# other distribution.
22+
#
23+
# You want to make rmtrash work with your favorite distribution?
24+
# Go on, I appreciate it!
2325
#
2426
#
2527
# EXIT CODES:
@@ -47,6 +49,7 @@
4749
# 512 directory not empty
4850
# 1024 unable to create trashcan: permission denied
4951
# 2048 unable to trash the trashcan
52+
# 4096 user `root' isn't allowed to trash files
5053
#
5154
#
5255
# KNOWN BUGS:
@@ -60,7 +63,7 @@
6063
#
6164
#
6265
# COPYRIGHT AND LICENSING:
63-
# Copyright (C) 2011-2013 Daniel Rudolf <http://www.daniel-rudolf.de/>
66+
# Copyright (C) 2011-2014 Daniel Rudolf <http://www.daniel-rudolf.de/>
6467
#
6568
# This program is free software: you can redistribute it and/or modify
6669
# it under the terms of the GNU General Public License as published by
@@ -99,10 +102,17 @@
99102
# + adding function showUsage()
100103
# * moving --help and --version handling
101104
# * improving documentation
105+
# v1.8 - 2014-06-01 23:05:00+0200
106+
# + add VERSION and BUILD variables
107+
# * change --forbid-root handling
108+
# * remove dpkg
102109

103110
LC_ALL=C
104111
APP_NAME=$(basename "$0")
105112

113+
VERSION="1.8"
114+
BUILD="20140601"
115+
106116
function ShowUsage() {
107117
echo "Usage:"
108118
echo " $APP_NAME [OPTION]... DIRECTORY..."
@@ -141,34 +151,18 @@ if [ ! -x "$RMDIR_CMD" ]; then
141151
exit 4
142152
fi
143153

144-
# check if trash-cli package is installed
145-
TRASH_PACKAGE_INFORMATION="$(dpkg -p trash-cli)"
146-
if [ "$?" -ne 0 ] || [ -z "$TRASH_PACKAGE_INFORMATION" ]; then
147-
echo "$APP_NAME: This program requires a command line interface trashcan utility." >&2
148-
echo "It seems that the required program is not installed yet. You can catch up on" >&2
149-
echo "that by typing: sudo apt-get install trash-cli" >&2
150-
exit 4
151-
fi
152-
153-
# get version of the trash-cli package
154-
TRASH_VERSION="$(echo "$TRASH_PACKAGE_INFORMATION" | sed -n 's/^Version: \(.*\)/\1/p')"
155-
if [ -z "$TRASH_VERSION" ]; then
156-
echo "$APP_NAME: Unable to determine version of the \`trash-cli' package." >&2
157-
exit 4
158-
fi
159-
160154
# get path of trash
161-
# the path depends on the installed version of the trash-cli package
162-
dpkg --compare-versions "$TRASH_VERSION" "ge" "0.11"
163-
if [ "$?" -eq "0" ]; then
164-
TRASH_CMD="$(which "trash-put")"
165-
else
155+
# the path depends on the installed version of the trash-cli package
156+
TRASH_CMD="$(which "trash-put")"
157+
if [ -z "$TRASH_CMD" ]; then
166158
TRASH_CMD="$(which "trash")"
167159
fi
168160

169-
# check if trash command is found
161+
# check if trasgh-cli is installed
170162
if [ -z "$TRASH_CMD" ]; then
171-
echo "$APP_NAME: Unable to find the trashcan command within \$PATH." >&2
163+
echo "$APP_NAME: command \`trash-put' was not found." >&2
164+
echo "This program requires a command line interface trashcan utility." >&2
165+
echo "It seems that the required program is not installed yet." >&2
172166
exit 4
173167
fi
174168

@@ -186,17 +180,17 @@ if [ $? -eq 0 ]; then
186180
fi
187181

188182
# use getopt to parse parameters
189-
if ! OPTIONS=$(getopt -n "$APP_NAME" -o pv -l "ignore-fail-on-non-empty" -l "parents" -l "verbose" -l "forbid-root" -l "forbid-root-force" -l "help" -l "version" -- "$@"); then
183+
if ! OPTIONS=$(getopt -n "$APP_NAME" -o pv -l "ignore-fail-on-non-empty" -l "parents" -l "verbose" -l "forbid-root::" -l "forbid-root-force" -l "help" -l "version" -- "$@"); then
190184
ShowUsage
191185
exit 2
192186
fi
193187
eval set -- "${OPTIONS}"
194188

195189
# default option values
196-
IGNORE_FAIL_ON_NON_EMPTY=false # --ignore-fail-on-non-empty bool
197-
PARENTS=false # -p / --parents bool
198-
VERBOSE=false # -v / --verbose bool
199-
FORBID_ROOT="no" # --forbid-root / --forbid-root-force string ( no / yes / force )
190+
IGNORE_FAIL_ON_NON_EMPTY=false # --ignore-fail-on-non-empty bool
191+
PARENTS=false # -p / --parents bool
192+
VERBOSE=false # -v / --verbose bool
193+
FORBID_ROOT="never" # --forbid-root string ( always / ask-forbid / ask-pass / pass / never )
200194

201195
# parse options
202196
while true; do
@@ -217,12 +211,34 @@ while true; do
217211
;;
218212

219213
"--forbid-root")
220-
FORBID_ROOT="yes"
221-
shift
214+
if [ "$2" == "always" ] || [ "$2" == "yes" ]; then
215+
FORBID_ROOT="always"
216+
elif [ "$2" == "ask-forbid" ] || [ -z "$2" ]; then
217+
FORBID_ROOT="ask-forbid"
218+
elif [ "$2" == "ask-pass" ]; then
219+
FORBID_ROOT="ask-pass"
220+
elif [ "$2" == "pass" ]; then
221+
FORBID_ROOT="pass"
222+
elif [ "$2" == "never" ] || [ "$2" == "no" ]; then
223+
FORBID_ROOT="never"
224+
else
225+
echo "$APP_NAME: invalid argument \`$2' for \`--forbid-root'" >&2
226+
echo "Valid arguments are:" >&2
227+
echo " - \`always', \`yes'" >&2
228+
echo " - \`ask-forbid'" >&2
229+
echo " - \`ask-pass'" >&2
230+
echo " - \`pass'" >&2
231+
echo " - \`never', \`no'" >&2
232+
echo "Try \`$APP_NAME --help' for more information." >&2
233+
exit 2
234+
fi
235+
236+
shift 2
222237
;;
223238

224239
"--forbid-root-force")
225-
FORBID_ROOT="force"
240+
# backward compatibility
241+
FORBID_ROOT="pass"
226242
shift
227243
;;
228244

@@ -246,16 +262,20 @@ while true; do
246262
echo " is similar to \`$APP_NAME a/b/c a/b a'"
247263
echo " -v, --verbose output a diagnostic for every directory processed"
248264
echo
249-
echo "Replacement options:"
250-
echo " These options are not supposed to be used when calling $APP_NAME, they help"
251-
echo " you to control how and in which cases \`rmdir' is replaced."
252-
echo " --forbid-root forbid user \`root' to trash directories. When"
253-
echo " standard input is a terminal, the user is asked"
254-
echo " to pass the command to \`$RMDIR_CMD', otherwise"
255-
echo " the entire command is aborted"
256-
echo " --forbid-root-force when user \`root' trys to trash directories, the"
257-
echo " entire command is automatically passed to"
258-
echo " \`$RMDIR_CMD'."
265+
echo "Replacement option:"
266+
echo " This option is not supposed to be used when calling $APP_NAME, it helps"
267+
echo " you to control how and in which cases \`rm' is replaced."
268+
echo " --forbid-root[=HOW] forbid user \`root' to trash directories. When"
269+
echo " standard input is a terminal, \`ask-forbid'"
270+
echo " and \`ask-pass' will question the user to"
271+
echo " pass the command to \`$RMDIR_CMD'. When"
272+
echo " standard input is no terminal, \`ask-forbid'"
273+
echo " will abort the command, whereas \`ask-pass'"
274+
echo " will pass the command to \`$RMDIR_CMD'."
275+
echo " Use \`pass' to pass all commands of user \`root'"
276+
echo " to \`$RMDIR_CMD'. If user \`root' should never"
277+
echo " trash directories, use \`always'. Without HOW,"
278+
echo " \`ask-forbid' is assumed"
259279
echo
260280
echo "To remove a directory whose name starts with a \`-', for example \`-foo', use"
261281
echo "one of these commands:"
@@ -272,8 +292,8 @@ while true; do
272292
;;
273293

274294
"--version")
275-
echo "rmdirtrash 1.7 (build 20131129)"
276-
echo "Copyright (C) 2011-2013 Daniel Rudolf"
295+
echo "rmdirtrash $VERSION (build $BUILD)"
296+
echo "Copyright (C) 2011-2014 Daniel Rudolf"
277297
echo "License GPLv3: GNU GPL version 3 only <http://gnu.org/licenses/gpl.html>."
278298
echo "This is free software: you are free to change and redistribute it."
279299
echo "There is NO WARRANTY, to the extent permitted by law."
@@ -303,51 +323,62 @@ if [ $# -eq 0 ]; then
303323
fi
304324

305325
# forbid root?
306-
if [ "$FORBID_ROOT" != "no" ] && [ $(id -u) -eq 0 ]; then
326+
if [ "$FORBID_ROOT" != "never" ] && [ "$(id -u)" -eq 0 ]; then
307327
echo "$APP_NAME: user \`root' should never trash directories" >&2
308-
309-
PASS_COMMAND=false
310-
if [ "$FORBID_ROOT" == "yes" ]; then
311-
# prompt
312-
if [ $SHELL_IN_INTERACTIVE_MODE == true ]; then
313-
echo -n "pass entire command to \`$RMDIR_CMD' (delete arguments instead of trashing)? "
314-
read PASS_COMMAND_ANSWER
315-
316-
if [ "$PASS_COMMAND_ANSWER" == "y" ] || [ "$PASS_COMMAND_ANSWER" == "yes" ]; then
317-
PASS_COMMAND=true
328+
329+
if [ "$FORBID_ROOT" == "always" ]; then
330+
exit 4096
331+
else
332+
PASS_COMMAND=false
333+
if [ "$FORBID_ROOT" == "ask-forbid" ] || [ "$FORBID_ROOT" == "ask-pass" ]; then
334+
# prompt
335+
if [ $SHELL_IN_INTERACTIVE_MODE == true ]; then
336+
echo -n "pass entire command to \`$RMDIR_CMD' (delete arguments instead of trashing)? "
337+
read PASS_COMMAND_ANSWER
338+
339+
if [ "$PASS_COMMAND_ANSWER" == "y" ] || [ "$PASS_COMMAND_ANSWER" == "yes" ]; then
340+
PASS_COMMAND=true
341+
fi
342+
else
343+
# shell is not running in interactive mode
344+
if [ "$FORBID_ROOT" == "ask-forbid" ]; then
345+
# unable to question user - forbid execution
346+
exit 4096
347+
else
348+
# unable to question user - pass command
349+
echo "$APP_NAME: entire command will be passed to \`$RMDIR_CMD'..."
350+
PASS_COMMAND=true
351+
fi
318352
fi
319353
else
320-
# exit if shell is not running in interactive mode
321-
exit 32
354+
# pass always
355+
PASS_COMMAND=true
356+
echo "$APP_NAME: entire command will be passed to \`$RMDIR_CMD'..."
322357
fi
323-
else
324-
# force
325-
PASS_COMMAND=true
326-
echo "$APP_NAME: entire command will be passed to \`$RMDIR_CMD'..."
327-
fi
328-
329-
if [ $PASS_COMMAND == true ]; then
330-
# create command
331-
CMD="$RMDIR_CMD"
332-
CMD_OPTIONS="$(getOptionsAsCmdString)"
333-
CMD_ARGUMENTS=""
334-
while [ $# -gt 0 ]; do
335-
CMD_ARGUMENTS+=" \"$1\""
336-
shift
337-
done
338358

339-
# execute command
340-
if [ $VERBOSE == true ]; then
341-
echo "$APP_NAME: executing \`$CMD $CMD_OPTIONS ${CMD_ARGUMENTS:1}'"
342-
fi
343-
eval "$CMD $CMD_OPTIONS ${CMD_ARGUMENTS:1}"
344-
RMDIR_EXIT_STATUS=$?
345-
346-
if [ "$RMDIR_EXIT_STATUS" -ne 0 ]; then
347-
echo "$APP_NAME: execution of \`$RMDIR_CMD' failed (exit status $RMDIR_EXIT_STATUS)" >&2
348-
exit 16
359+
if [ $PASS_COMMAND == true ]; then
360+
# create command
361+
CMD="$RMDIR_CMD"
362+
CMD_OPTIONS="$(getOptionsAsCmdString)"
363+
CMD_ARGUMENTS=""
364+
while [ $# -gt 0 ]; do
365+
CMD_ARGUMENTS+=" \"$1\""
366+
shift
367+
done
368+
369+
# execute command
370+
if [ $VERBOSE == true ]; then
371+
echo "$APP_NAME: executing \`$CMD $CMD_OPTIONS ${CMD_ARGUMENTS:1}'"
372+
fi
373+
eval "$CMD $CMD_OPTIONS ${CMD_ARGUMENTS:1}"
374+
RMDIR_EXIT_STATUS=$?
375+
376+
if [ "$RMDIR_EXIT_STATUS" -ne 0 ]; then
377+
echo "$APP_NAME: execution of \`$RMDIR_CMD' failed (exit status $RMDIR_EXIT_STATUS)" >&2
378+
exit 16
379+
fi
380+
exit 0
349381
fi
350-
exit 0
351382
fi
352383
fi
353384

0 commit comments

Comments
 (0)