[prev in list] [next in list] [prev in thread] [next in thread]
List: kde-user
Subject: Re: Challenge to KDE programmers -- Write a script for Trash, please!
From: faust nijhuis <faust_rachel () wxs ! nl>
Date: 2000-07-31 21:38:26
[Download RAW message or body]
Benjamin Sher wrote:
>
> Well, I think I have hinted long enough. Would one of you KDE gurus
> consider writing such a script and sharing it with us ordinary users who
> love KDE and Linux and would really appreciate the security such a
> simple script would provide for everyone.
You may tryout something I made.
There are two scripts:
- rm is bash script and it moves the files (dir's) to $HOME/.trash
- open_trash is a perl script with which you can restore and delete
the files in the trash can ($HOME/.trash).
(you need to install the perl TK toolkit, Tk800.022.tar.gz)
I don't have documentation yet.
Don't use it as root !!! If somthing goes wrong it is possible that you
delete all files. First try it out as a normal user.
Faust
["rm" (text/plain)]
#!/bin/bash
rmm()
{
#########################################
# Actually define the details ... #
#########################################
TRASH=$HOME/.trash
typeset -i rec=0
typeset -i real=0
typeset -i interactive=0
#################################################
# If trashcan does not exist, create it. #
#################################################
[ ! -d "$TRASH" ] && mkdir -p $TRASH
path=$PWD
cd $TRASH 2> /dev/null
if [ $? = "1" ]
then
/bin/rm $*
exit 0
fi
cd $path
#################################################
# Check the trashcan before any processing #
#################################################
#check_trash_space
#[ $? -ne 0 ] &&
# exit 1
#################################################
# Parse any arguments passed to "rm" command. #
#################################################
set -- `getopt fiRrvaT $*` #getopt breaks up options in command lines for \
easy parsing
#by shell scripts, and checks for legal options
allargs="$*"
allargs=$(echo $allargs |sed 's/-T//' |sed 's/--//')
args=""
for i in $*
do
case $i in
-f) args="$args -f"; shift;;
-r|-R) rec=1;args="$args -r"; shift;;
-i) interactive=1;shift;;
-T) real=1; shift;;
-v) args="$args -v"; shift;;
--) shift;break;;
esac
done
if [ $real -eq 1 ]
then
/bin/rm $allargs
exit $?
fi
#################################
# Process each file argument #
#################################
for x in $*
do
#########################################################
# If the file/dir does not have an absolute path, #
# then make sure that one is added to the name. #
#########################################################
i=$(echo $x|awk -v path=$path '{if(substr($0,1,1)!="/") {
printf("%s/%s\n",path,$0) } else {
print $0} }')
if [ -d "$i" ]
then
if [ $rec -eq 1 ]
then
#########################################################
# When removing a directory use "find" to ensure #
# that all files in a directory are given before the#
# directory name - so directories are created correctly.#
#########################################################
found=`find $i -depth -print `
for df in $found
do
pa=$(dirname $df)
fi=$(basename $df)
if [ -d $df ]
then
found_in_dir=`find $df -type f -depth -print \
2> /dev/null`
if [ "$found_in_dir" = "" ]
then
/bin/rm $args $df
fi
else
#########################################
# Create the file in the trashcan #
# using tar to preserve all file info. #
#########################################
remove=1
if [ $interactive = 1 ]
then
remove=0
echo -n "rm: remove $df (y/n)?"
read resp
case $resp in
y|Y|yes|YES|ye|YE) remove=1;;
esac
fi
if [ $remove = 1 ]
then
#sz=$(ls -ld $df | awk '{print $5}')
sz=$(du -sk $i | awk '{print $1}')
if [ -t 0 ] && [ $sz -gt 10000 ]
then
echo -n "rm: \"$df\" is $sz \
bytes long - use trashcan?(n/y)[n]" read resp
resp=${resp:-n}
#{ [ "$resp" = "n" ] && \
/bin/rm -f $df &&
{ [ "$resp" = "n" ] && \
/bin/rm $args $df &&
echo "rm: \"$df\" \
removed completely."; } ||
echo "rm: Failed to \
remove \"$df\"!" continue
fi
#echo -n "Removing $df ..."
(cd / ; tar cpBf - .$df ) |
( cd $TRASH; tar xpBf -)
rc=$?
#/bin/rm -f $df
/bin/rm $args $df
if [ $rc -ne 0 ]
then
echo "Error: Trash full!"
exit 2
fi
\
#########################################
# Now create the status file,used \
#
# for indicating when files are \
purged. #
\
#########################################
touch $TRASH/$pa/._.status.$fi
if [ $? -ne 0 ]
then
echo "Error : Trash full!"
exit 2
fi
#echo " Done"
fi
fi
done
found=`find $i -type f -depth -print 2> /dev/null`
if [ "$found" = "" ]
then
#nargs=$(echo $args|sed 's/-i//')
#/bin/rm -rf $i
/bin/rm $args $i 2> /dev/null
fi
else
echo "rm: $i is a directory"
fi
else
pa=$(dirname $i)
fi=$(basename $i)
if [ -a $i ]
then
#########################################
# Simply remove an individual file. #
#########################################
remove=1
if [ $interactive = 1 ]
then
remove=0
echo -n "rm: remove $df (y/n)?"
read resp
case $resp in
y|Y|yes|YES|ye|YE) remove=1;;
esac
fi
if [ $remove = 1 ]
then
#sz=$(ls -ld $i | awk '{print $5}')
sz=$(du -sk $i | awk '{print $1}')
if [ -t ] && [ $sz -gt 10000 ]
then
echo -n "rm: \"$i\" is $sz bytes long - use \
trashcan?(n/y)[n]" read resp
resp=${resp:-n}
#{ [ "$resp" = "n" ] && /bin/rm -f $i &&
{ [ "$resp" = "n" ] && /bin/rm $args $i &&
echo "rm: \"$i\" removed \
completely."; } ||
echo "rm: Failed to remove \"$i\"!"
exit $?
fi
#echo -n "Removing $i ..."
(cd / ; tar cpBf - .$i) |
( cd $TRASH; tar xpBf -)
rc=$?
/bin/rm $args $i
if [ $rc -ne 0 ]
then
echo "Error: Trash full!"
exit 2
fi
touch $TRASH/$pa/._.status.$fi
if [ $? -ne 0 ]
then
echo "Error : Trash full!"
exit 2
fi
fi
else
echo "$i does not exist!"
fi
fi
done
exit 0
}
rmm $*
["open_trash" (text/plain)]
#!/usr/bin/perl -w
require 5.002;
use English;
use Tk;
use File::Basename ();
use strict;
my $home = $ENV{"HOME"};
my $TRASH = "$home/.trash";
my $top = MainWindow->new;
my $buttons = $top->Frame()->pack (side => 'top', -expand => 1,-fill => 'x');
my $button_exit = $buttons->Button(text => 'Exit', command => [\&exit]);
my $button_update = $buttons->Button(text => 'Update', command => [\&update_list]);
my $button_restore = $buttons->Button(text => 'Restore', command => [\&restore]);
my $button_delete = $buttons->Button(text => 'Delete', command => [\&delete]);
#my $button_options = $buttons->Button(text => 'Options', command => [\&options]);
$button_exit->pack (-side => 'left', -expand => 0);
$button_update->pack (-side => 'left', -expand => 0);
$button_restore->pack (-side => 'left', -expand => 0);
$button_delete->pack (-side => 'left', -expand => 0);
#$button_options->pack (-side => 'left');
my $day1 = 0;
my $day2 = 1;
my $file = "";
my $entry = $top->Frame()->pack (side => 'top', -expand => 1 , -fill => 'x');
my $ent_day1 = $entry->Entry(-textvariable => \$day1 ,
-width => 5, )->pack(-side => 'left' , -expand => 0 );
my $ent_day2 = $entry->Entry(-textvariable => \$day2 ,
-width => 5, )->pack(-side => 'left', -expand => 0);
my $ent_find = $entry->Entry(-textvariable => \$file ,
-width => 20, )->pack(-side => 'left', -expand => 0);
$ent_day1->bind('<Return>' => \&update_list);
$ent_day2->bind('<Return>' => \&update_list);
$ent_find->bind('<Return>' => \&update_list);
my $scroll = $top->Scrollbar();
$scroll->pack(-side => 'right', -fill => 'y');
my $list = $top->Listbox(
-yscrollcommand => ['set', $scroll],
-relief => 'sunken',
-width => 50,
-height => 20,
-setgrid => 'yes',
-selectmode => 'extended',
);
$list->pack(-side => 'bottom', -fill => 'both', -expand => 'yes');
$scroll->configure(-command => ['yview', $list]);
$top->minsize(1, 1);
sub list_trash {
my ($opt,$day1,$day2,$file) = @_;
my @found;
$day1 =~ s/w//;
$day1 =~ s/m//;
$day1 =~ s/d//;
if ($day2 =~ s/w//){ $day2 = $day2 * 7; $day1 = $day1 * 7;}
if ($day2 =~ s/m//){ $day2 = $day2 * 30; $day1 = $day1 * 30;}
if ($day2 =~ s/d//){ $day2 = $day2 ; $day1 = $day1;}
if ( $opt eq "-a") {
chdir($TRASH) || die "Cannot cd to $TRASH";
if ($file) {
@found = `find . -type f -name $file -print | grep -v "\._\.status\." `;
} else {
@found = `find . -type f -print | grep -v "\._\.status\." `;
}
my @ret ;
foreach (@found) {
chomp ;
s/^\.// ;
my $dn = File::Basename::dirname($_);
my $fn = File::Basename::basename($_);
my $f = "$TRASH/$dn/._.status.$fn";
$day1 = $day1 - .1;
if ( (-e $f ) && (-M $f >= $day1) && (-M $f <= $day2)) {push(@ret,$_);}
}
return @ret;
} else {
my $PWD = `pwd`;
chomp $PWD;
my @ret;
chdir("$TRASH/$PWD") || die "Cannot cd to $TRASH";
foreach (<{*}>) {
if (! /^d|\._\.status\./) {
push(@ret,$_);
}
return @ret;
}
}
}
sub restore_trash {
my $PWD = `pwd`;
foreach (@_) {
my $dn = File::Basename::dirname($_);
my $fn = File::Basename::basename($_);
if ( ! -e "$TRASH/$_" ) {
print "$_ is not in the trashcan!";
} else {
#Restoring $x from trashcan ...
chdir $TRASH;
system ("(tar cpBf - .$_) | ( cd /; tar xpBf - )") == 0
or die "Unable to restore $_ - aborting";
unlink ("$TRASH/$_","$TRASH/$dn/._.status.$fn");
}
}
}
sub restore {
my @cursel = $list->curselection;
foreach (@cursel) {
my $f = $list->get($_);
restore_trash $f;
}
show_list();
}
sub update_list {
show_list();
}
sub delete {
my @cursel = $list->curselection;
foreach (@cursel) {
my $f = $list->get($_);
unlink("$TRASH/$f");
}
show_list();
}
sub show_list {
my @files = list_trash("-a", $day1 ,$day2,$file);
chomp (@files);
$list->delete(0,'end');
foreach (@files) {
$list->insert('end', $_);
}
}
show_list();
MainLoop;
--
Send posts to: kde-user@lists.netcentral.net
Send all commands to: kde-user-request@lists.netcentral.net
Put your command in the SUBJECT of the message:
"subscribe", "unsubscribe", "set digest on", or "set digest off"
All kde mailing lists are archived at http://lists.kde.org
**********************************************************************
This list is from your pals at NetCentral <http://www.netcentral.net/>
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic