1
0
Fork 0

remove old scripts

This commit is contained in:
Luca Bilke 2024-05-04 16:39:25 +02:00
parent a13172dc76
commit f9fea26aa3
3 changed files with 0 additions and 201 deletions

View File

@ -1,75 +0,0 @@
#!/bin/perl
use strict;
use warnings;
use feature("signatures");
use File::Path("make_path");
use List::Util("max");
sub fs_root ($file_name) {
chomp( my $ret = `stat -c %m "$file_name"` );
return $ret;
}
sub file_info ($file) {
my $file_name = `basename "$file"`;
my $file_path = `realpath -s "$file"`;
chomp( $file_name, $file_path );
my $file_fs_root = fs_root($file_name);
return ( $file_name, $file_path, $file_fs_root );
}
sub trash_dirs ($file_fs_root) {
my $data_path = "$ENV{'XDG_DATA_HOME'}" // "$ENV{'HOME'}/.local/share";
my $trash_dir;
if ( $file_fs_root eq fs_root($data_path) ) {
$trash_dir = "$data_path/Trash/";
}
else {
$trash_dir = "$file_fs_root/.Trash/";
}
return "${trash_dir}info", "${trash_dir}files";
}
sub target_file_name ( $file_name, $trash_file_path ) {
opendir( my $dir, "$trash_file_path" ) or die "$!";
my $existing_suffix_nums =
map( ( $_ =~ /^${file_name}\.~(\d+)~/ ), readdir $dir );
my $suffix_num = max($existing_suffix_nums) + 1;
my $suffix = "~" . $suffix_num . "~";
closedir($dir);
return "${file_name}.${suffix}";
}
sub write_file ( $file_path, $contents ) {
open( FH, '>', $file_path ) or die "$!";
print FH $contents;
}
# NOTE: Execution starts here
if ( $#ARGV + 1 != 1 ) {
die "Bad Arguments";
}
stat $ARGV[0] or die "$!";
my ( $file_name, $file_src, $file_fs_root ) = file_info( $ARGV[0] );
my ( $trash_info_path, $trash_file_path ) = trash_dirs($file_fs_root);
make_path( $trash_info_path, $trash_file_path );
my $target_name = target_file_name( $file_name, $trash_file_path );
chomp( my $deletion_time = `date -u +%Y%m%dUTC%T` );
my $info_contents = <<EOF;
[Trash Info]
Path=$file_src
DeletionDate=$deletion_time
EOF
rename( $file_src, "$trash_file_path/$target_name" ) or die "$!";
write_file( "$trash_info_path/$target_name", $info_contents ) or die "$!";

View File

@ -1,77 +0,0 @@
#!/bin/perl
use strict;
use warnings;
use feature("signatures");
use File::Basename;
use Cwd;
sub fs_root ($file_name) {
chomp( my $ret = `stat -c %m $file_name` );
return $ret;
}
sub trash_dir ($file_fs_root) {
my $data_path = "$ENV{'XDG_DATA_HOME'}" // "$ENV{'HOME'}/.local/share";
return "$data_path/Trash/" if ( $file_fs_root eq fs_root($data_path) );
return "$file_fs_root/.Trash/";
}
sub trashed_files ( $search_path, $trash_path ) {
my @ret;
opendir( my $dir, "$trash_path/info" ) or die "$!\n";
for ( readdir $dir ) {
$_ =~ /\.~\d+~$/ or next;
open( FH, "<", "$trash_path/info/$_" ) or next;
<FH> eq "[Trash Info]\n" or next;
my ( $name, $path, $suffix ) =
fileparse( ( <FH> =~ /(?<=Path=)(.+)$/ )[0] );
$path =~ /^$search_path\/?$/ or next;
my $deletion_date = ( <FH> =~ /(?<=DeletionDate=)(.+)$/ )[0];
chomp( $deletion_date = `date -d ${deletion_date} +'%x %X'` );
push( @ret, [ $path, "${name}${suffix}", $deletion_date, $_ ] );
}
return @ret;
}
sub fzf_files (@files) {
my $n = 0;
my $str = "";
for my $e (@files) {
$str = "$n | $$e[2] | $$e[1]\n" . $str;
$n++;
}
chomp($str);
my $sel = `echo '$str' | fzf`;
!$sel and exit;
my $index = substr( $sel, 0, index( $sel, ' ' ) );
return $index;
}
# NOTE: Execution starts here
#
my $target_directory;
if ( $#ARGV + 1 > 1 ) {
die "Bad Arguments\n";
}
elsif ( $#ARGV + 1 == 1 ) {
$target_directory = shift;
chomp( $target_directory = `realpath -s $target_directory` );
stat $target_directory or die "$!\n";
}
else {
$target_directory = getcwd();
}
my $trash_dir = trash_dir( fs_root($target_directory) );
my @files = trashed_files( $target_directory, $trash_dir );
!@files and exit;
my $index = fzf_files(@files);
my $file_dest = $files[$index][0] . $files[$index][1];
my $file_src = $trash_dir . "files/" . $files[$index][3];
my $file_info = $trash_dir . "info/" . $files[$index][3];
rename( $file_src, $file_dest ) or die "$!\n";
unlink($file_info);

View File

@ -1,49 +0,0 @@
#!/bin/sh
IFS='
'
days=${1:-7}
types=${2:-ext2,ext3,ext4,xfs,btrfs,vfat,nfs}
getfsroot() {
printf "%s" "$(df "$1" --output=target | tail -1)"
}
list_trash_dirs() {
for mnt in $(findmnt -Py -t "$types"); do
eval "$mnt"
# shellcheck disable=2153
if [ "$TARGET" = "$(getfsroot "${XDG_DATA_HOME:-$HOME/.local/share}")" ]; then
target=${XDG_DATA_HOME:-$HOME/.local/share}/Trash
elif [ "$TARGET" = "/" ]; then
target=/.Trash
else
target=${TARGET}/.Trash
fi
[ -w "$target" ] && { echo "$target"; return; }
echo
done
}
prune() {
[ -w "$1" ] || return 1
for file in "$1"/files/*; do
file=$(basename "$file")
[ "$file" = '*' ] && return
if [ ! -r "$1/info/$file" ] || [ ! "$(head -1 "$1/info/$file")" = "[Trash Info]" ]; then
[ -w "$1/info/$file" ] && rm -rf "$1/info/$file"
rm -rf "$1/files/$file"
continue
fi
if [ "$(date -ud "$days days ago" +%s)" -gt "$(date -ud "$(grep DeletionDate "$1/info/$file" | cut -d '=' -f2)" +%s)" ]; then
echo "$file too old!"
rm -rf "$1/info/$file"
rm -rf "$1/files/$file"
fi
done
}
for dir in $(list_trash_dirs); do
prune "$dir" || echo "Failed to prune $dir"
done