id3fs-index: removed unused entries when reindexing
[id3fs.git] / bin / id3fs-index
index 89f6f12..9493f7c 100755 (executable)
@@ -16,6 +16,7 @@ my $basedir=undef;
 my $dbpath=undef;
 my $list=0;
 my @extensions=qw(mp3 flac ogg);
+my $files_pruned;
 
 Configure(qw(bundling no_ignore_case));
 my $optret=GetOptions(
@@ -44,10 +45,16 @@ else
 {
     $db->last_update(time());
 
+    my $directories_pruned=$db->prune_directories();
     while(my $path=shift)
     {
        File::Find::find( {wanted => \&wanted, follow => 1, no_chdir => 1}, $path);
     }
+    if($files_pruned || $directories_pruned)
+    {
+       print "Removing data from pruned files\n" if $verbose;
+       $db->remove_unused();
+    }
 }
 
 sub wanted
@@ -57,6 +64,7 @@ sub wanted
     if(-d)
     {
        print("$_\n") if $verbose;
+       prune($_);
     }
     elsif(-f && scalar(grep({ $ext eq lc($_);} @extensions)))
     {
@@ -65,6 +73,33 @@ sub wanted
     }
 }
 
+
+sub prune
+{
+    my $dir=shift;
+    return unless(opendir(DIR, $dir));
+    print "Pruning $dir\n";
+    my @oldfiles=$db->files_in($dir);
+    my @newfiles=grep { !/^\.\.?$/; } readdir(DIR);
+    closedir(DIR);
+    @oldfiles=sort @oldfiles;
+    @newfiles=sort @newfiles;
+    my %hash;
+    @hash{@newfiles}=();
+    for my $file (@oldfiles)
+    {
+       unless(exists($hash{$file}))
+       {
+           # FIXME: add path, rebasify
+           $files_pruned=1;
+           $db->unindex($file);
+       }
+    }
+}
+
+
+
+
 sub list_tags
 {
     my($db)=@_;