fix pruning tags + only prune files/dirs under @ARGV master
authorIan Beckwith <ianb@erislabs.net>
Tue, 9 Nov 2010 02:48:46 +0000 (02:48 +0000)
committerIan Beckwith <ianb@erislabs.net>
Tue, 9 Nov 2010 02:48:46 +0000 (02:48 +0000)
bin/id3fs-index
lib/ID3FS/DB.pm

index f27c7f6..4ee56e5 100755 (executable)
@@ -75,7 +75,7 @@ else
     $db->last_update(time());
     my $base=$db->base_dir();
     my $abs_base=Cwd::abs_path($base);
-    while(my $path=shift)
+    for my $path (@ARGV)
     {
        if(Cwd::abs_path($path) !~ /^$abs_base/)
        {
@@ -83,7 +83,7 @@ else
        }
        File::Find::find( {wanted => \&wanted, follow => 1, no_chdir => 1}, $path);
     }
-    my $directories_pruned=$db->prune_directories();
+    my $directories_pruned=$db->prune_directories(@ARGV);
     if($files_pruned || $directories_pruned)
     {
        print "$me: pruning removed files from db\n" if $verbose;
index 5821b53..9972864 100644 (file)
@@ -71,7 +71,8 @@ sub find_db
     my $base=undef;
     for my $dir (@dirs)
     {
-       my $path=Cwd::abs_path($dir);
+#      my $path=Cwd::abs_path($dir);
+       my $path=$dir;
        do
        {
            $base=$path;
@@ -254,6 +255,8 @@ sub add
                                    { "artists_id" => $artist_id,
                                      "albums_id"  => $albums_id,
                                      "paths_id"   => $path_id });
+
+    $self->remove_tag_refs($file_id);
     if(@tags)
     {
        for my $tag (@tags)
@@ -376,6 +379,14 @@ sub add_relation
     $self->cmd($sql, map { $fields->{$_}; } sort keys(%$fields));
 }
 
+# remove existing files_x_tags relations before reindexing
+sub remove_tag_refs
+{
+    my ($self, $id)=@_;
+    my $sql="DELETE FROM files_x_tags WHERE files_id=?\n";
+    return($self->cmd($sql, $id));
+}
+
 sub files_in
 {
     my ($self, $dir)=@_;
@@ -398,13 +409,15 @@ sub unindex
 
 sub prune_directories
 {
-    my($self)=@_;
+    my($self,@includepaths)=@_;
     my $sql=("SELECT name, id FROM paths\n");
     my $pathsref=$self->cmd_rows($sql);
     my @ids=();
     for my $pathpair (@$pathsref)
     {
        my($path, $id)=@$pathpair;
+       my $relpath = $self->{base} . "/" . $path;
+       next unless(grep { $relpath =~ /^$_/; } @includepaths);
        my $fullpath="$self->{absbase}/$path";
        unless(-d $fullpath)
        {