#!/usr/bin/perl use strict; use Cwd 'abs_path'; use DBI(); my $basedir= "..."; my $socket=".../mysqld.sock"; my $database="infodb"; my $table="files"; my $sqluser= getpwuid($<); my $pass=""; $#ARGV == 1 or die "usage: $0 \n" . "Renames or moves a file or directory in the infodb database.\n". "The file has to be moved in the filesystem before calling this script.\n"; my ($db, $dbstring, $query, $rowref); my ($source, $srcname, $srcpath, $dest, $dstname, $dstpath); my ($duplicate, $olspath, $newpath); my @updatepaths; $source= abs_path($ARGV[0]); $source =~ /^$basedir/ or die "$0: Path of `$source' not in info directory\n"; $source =~ /([^\/]*$)/; $srcname= $1; $source =~ /^$basedir(.*)$srcname/; $srcpath= $1; $srcpath =~ s/\/$//; # print "$srcpath $srcname\n"; $dest= abs_path($ARGV[1]); $dest =~ /^$basedir/ or die "$0: Path of `$dest' not in info directory\n"; # check request against "ground truth" in file system: -e $dest or die "$0: `$dest' does not exist. Move file before using this script.\n"; if( -d $dest ) { if( $srcpath ne "" ) { $srcpath= "$srcpath/$srcname"; } else { $srcpath= $srcname; } $srcname= "/"; $dstname= "/"; $dest =~ /^$basedir(.*)/; $dstpath= $1; } else { $dest =~ /([^\/]*$)/; $dstname= $1; $dest =~ /^$basedir(.*)$dstname/; $dstpath= $1; $dstpath =~ s/\/$//; } # print "$srcpath $srcname $dstpath $dstname\n"; $dbstring= "DBI:mysql:database=$database;mysql_socket=$socket"; $db= DBI->connect( $dbstring, $sqluser, $pass ); if( $dstname ne "/" ) { $query= $db->prepare("update $table set path='$dstpath', name='$dstname' where path='$srcpath' and name='$srcname';"); $query->execute or die "$0: Error updating entry\n"; $query->finish; } else { # print "looking for paths\n"; $query= $db->prepare("select path from $table where path rlike '^$srcpath';"); $query->execute or die "$0: Error searching database\n"; if( $query->rows > 0 ) { while( $rowref= $query->fetchrow_arrayref ) { $duplicate= 0; for( @updatepaths ) { if( $_ eq $$rowref[0] ) { $duplicate= 1; } } if( ! $duplicate ) { push @updatepaths, $$rowref[0]; } } } $query->finish; $#updatepaths >= 0 or die "$0: Path `$srcpath' not found in database\n"; for( @updatepaths ) { $newpath= $_; $newpath =~ s/$srcpath/$dstpath/; # print "$_ -> $newpath\n"; $query= $db->prepare("update $table set path='$newpath' where path='$_';"); $query->execute or die "$0: Error updating entries\n"; $query->finish; } } $db->disconnect;