From 888e6963a1e64adf77ba3c1a897fd1cea7a2d8b7 Mon Sep 17 00:00:00 2001 From: Guilhem Moulin Date: Sun, 27 Nov 2011 22:41:09 +0100 Subject: init (that works!) --- videodb-check.pl | 131 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100755 videodb-check.pl diff --git a/videodb-check.pl b/videodb-check.pl new file mode 100755 index 0000000..9ed1540 --- /dev/null +++ b/videodb-check.pl @@ -0,0 +1,131 @@ +#!/usr/bin/perl -w + +# This program is free software. It comes without any warranty, to the +# extent permitted by applicable law. You can redistribute it and/or +# modify it under the terms of the Do What The Fuck You Want To Public +# License, Version 2, as published by Sam Hocevar. +# See http://sam.zoy.org/wtfpl/COPYING for more details. + +$VERSION = "0.1, 27 November 2011"; + +use DBI; +use File::Spec::Functions; +use Cwd; +use Env qw /HOME/; +use strict; + +################################################################################ + +# Configuration +my $symlinks = File::Spec->catdir($HOME,'video','MOVIES'); # Symlinks folder + +my $driver = "mysql"; +my $database = "videodb"; +my $hostname = "127.0.0.1"; +my $user = "videodb"; +my $port = 3306; +my $password = "videodb"; +my $videodata = "videodb_videodata"; + +################################################################################ + +# Connect to database +my $dsn = "DBI:$driver:database=$database;host=$hostname;port=$port"; +my $dbh = DBI->connect($dsn, $user, $password) + or die "Can't connect do database\n"; +$dbh->do( "set names utf8" ) or die; + +################################################################################ + +# Check that all entries in the DB have a symlink, that in turn have a +# valid target + +my $res = $dbh->selectall_arrayref ( "SELECT filename FROM $videodata" ) + or die "Can't select: " .$dbh->errstr. "\n"; + +my @links; +my @files; +foreach (@$res) { + my $l = File::Spec->catfile ($symlinks, $_->[0]); + unless (-l $l or -f $l) { + push @links, $_->[0]; + } + + if (-l $l and not -f File::Spec->catfile ($symlinks, readlink $l)) { + push @files, $_->[0]; + } + +} + +my $r = 0; +if (@links) { + print STDERR "* The following entries are in the DB, but I can't file the files:\n"; + foreach (@links) { + print STDERR " ", $_, "\n"; + } + $r = 1; +} + +if (@files) { + print STDERR "* The following entries don't have a valid target:\n"; + foreach (@files) { + print STDERR " ", $_, "\n"; + } + $r = 1; +} + + +################################################################################ + +# For all symlink, check that it has exactly one entry in the DB + +my @filelist; +opendir (DIR, $symlinks) or die "Can't open dir `" .$symlinks. ".:" .$!. "\n"; +while (my $l = readdir(DIR)) { + next if $l eq File::Spec->curdir(); + next if $l eq File::Spec->updir(); + + my $f = File::Spec->catfile($symlinks, $l); + if ( -d Cwd::realpath($f) ) { + opendir (SUBDIR, Cwd::realpath($f)) + or die "Can't open dir `" .Cwd::realpath($f). ".:" .$!. "\n"; + while (readdir(SUBDIR)) { + next if $_ eq File::Spec->curdir(); + next if $_ eq File::Spec->updir(); + push @filelist, File::Spec->catfile($l,$_); + } + closedir(SUBDIR) or die "Can't close: $!\n"; + } + elsif ( -l $f or -f $f ) { + push @filelist, $l; + } +} +closedir(DIR) or die "Can't close: $!\n"; + + +undef @files; +my $sth = $dbh->prepare ( "SELECT id FROM $videodata WHERE filename = ?" ) + or die "Error: " .$dbh->errstr; +foreach (@filelist) { + $sth->execute ($_) or die "Can't select: " .$dbh->errstr. "\n"; + my @res = $sth->fetchrow_array; + die $sth->errstr if $sth->err; + + push @files, $_ unless $#res == 0; +} +$sth->finish; +$dbh->disconnect; + + +if (@files) { + print STDERR "* The following files have <> 0 corresponding entries in the DB:\n"; + foreach (@files) { + print STDERR " ", $_, "\n"; + } + $r = 1; +} + + +################################################################################ + +exit $r; -- cgit v1.2.3