dc5ea7e0d58b8f00f3d5edf3fb0558a75f62e9f2
[id3fs.git] / lib / ID3FS / Path.pm
1 # id3fs - a FUSE-based filesystem for browsing audio metadata
2 # Copyright (C) 2010  Ian Beckwith <ianb@erislabs.net>
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 package ID3FS::Path;
18
19 use strict;
20 use warnings;
21 use ID3FS::Path::Node;
22
23 our ($STATE_INVALID, $STATE_ROOT, $STATE_TAG, $STATE_TAGVAL,
24      $STATE_BOOLEAN, $STATE_ALBUMS, $STATE_TRACKLIST,
25      $STATE_FILE, $STATE_ALL)=(0..8);
26
27 our %priorities=( "OR" => 0, "AND" => 1, "NOT" => 2 );
28
29 our $PATH_ALLTRACKS= "TRACKS";
30 our $PATH_NOARTIST = "NOARTIST";
31 our $PATH_NOALBUM  = "NOALBUM";
32
33 our $ENABLE_FILTER = 1;
34
35 sub new
36 {
37     my $proto=shift;
38     my $class=ref($proto) || $proto;
39     my $self={};
40     bless($self,$class);
41
42     $self->{elements}=[];
43     $self->{db}=shift;
44     $self->{path}=shift;
45     $self->{verbose}=shift;
46     $self->{maxtagdepth}=shift;
47     $self->{curtagdepth}=0;
48     $self->{path} =~ s/\/\//\//g; # drop doubled slashes
49
50     $self->parse();
51 #    print "STATE: ", $self->state(), "\n";
52     return $self;
53 }
54
55 sub isdir
56 {
57     my($self)=@_;
58     if(($self->state() == $STATE_FILE) ||
59        ($self->state() == $STATE_INVALID))
60     {
61         return 0;
62     }
63     return 1;
64 }
65
66 sub isfile
67 {
68     my($self)=@_;
69     return($self->state() == $STATE_FILE);
70 }
71
72 sub isvalid
73 {
74     my($self)=@_;
75     return($self->state() != $STATE_INVALID);
76 }
77
78 sub dest
79 {
80     my($self, $mountpoint)=@_;
81     if($self->state() == $STATE_FILE)
82     {
83         return $self->filename($mountpoint);
84     }
85     return "ERROR"; #should never happen?
86 }
87
88 sub dirents
89 {
90     my($self)=@_;
91     my @dents=();
92     my @fents=();
93     my $state=$self->state();
94 #    print "DIRENTS: STATE: $state\n";
95 #    print "DIRENTS: FILE: $self->{path}\n";
96     if($state==$STATE_ALL)
97     {
98         @dents=($self->filter($PATH_ALLTRACKS, $PATH_NOARTIST), $self->artists());
99     }
100     elsif($state==$STATE_TAG || $state==$STATE_TAGVAL)
101     {
102         if($state==$STATE_TAG && $self->at("tag") &&
103            $self->{db}->tag_has_values($self->tail()->id()))
104         {
105             @dents=$self->tags();
106         }
107         else
108         {
109             if($self->{maxtagdepth} && ($self->{curtagdepth} < $self->{maxtagdepth}))
110             {
111                 @dents=qw(AND OR);
112             }
113             push(@dents, $self->filter($PATH_ALLTRACKS, $PATH_NOARTIST), $self->artists());
114         }
115     }
116     elsif($state==$STATE_BOOLEAN)
117     {
118         my $parent=$self->tail();
119         unless($self->is("boolean", $parent) &&
120                $parent->{name} eq "NOT")
121         {
122             @dents=("NOT");
123         }
124         push(@dents,$self->tags());
125     }
126     elsif($state==$STATE_ROOT)
127     {
128         @dents=(qw(ALL NOT), $self->tags());
129     }
130     elsif($state==$STATE_ALBUMS)
131     {
132         @dents=$self->filter($PATH_ALLTRACKS, $PATH_NOALBUM, $self->albums());
133     }
134     elsif($state==$STATE_TRACKLIST)
135     {
136         @fents=$self->tracks();
137     }
138     else
139     {
140         print "DIRENTS: UNHANDLED STATE: $state\n";
141     }
142     return(\@dents, \@fents);
143 }
144
145 sub parse
146 {
147     my($self)=@_;
148     @{$self->{components}}=split(/\//, $self->{path});
149     shift @{$self->{components}}; # drop empty field before leading /
150 #    print "PATH: $self->{path}\n";
151     $self->state($STATE_ROOT);
152     return if($self->{path} eq "/");
153     my @parts=@{$self->{components}};
154     my($tag, $tagval);
155     $self->{elements}=[];
156     $self->{in_all}=0;
157     my $root_not=0;
158     my $tags_seen=0;
159     while(defined(my $name=shift @parts))
160     {
161 #       print "NAME: $name\n";
162         my $state=$self->state();
163         if($state==$STATE_INVALID)
164         {
165 #           print "SM: INVALID: $name\n";
166             return;
167         }
168         elsif($state==$STATE_ROOT)
169         {
170 #           print "SM: ROOT: $name\n";
171             if($name eq "ALL")
172             {
173                 $self->{in_all}=1;
174                 $self->state($STATE_ALL);
175             }
176             elsif($name eq "NOT")
177             {
178                 $root_not=1;
179                 push(@{$self->{elements}}, ID3FS::Path::Node->new($self->{db}, "boolean", $name));
180                 $self->state($STATE_BOOLEAN);
181             }
182             else
183             {
184                 $tag=ID3FS::Path::Node->new($self->{db},"tag", $name);
185                 if($tag)
186                 {
187                     push(@{$self->{elements}}, $tag);
188                     $tags_seen++;
189                     $self->state($STATE_TAG);
190                 }
191                 else
192                 {
193                     $self->state($STATE_INVALID);
194                 }
195             }
196         }
197         elsif($state==$STATE_TAG || $state==$STATE_TAGVAL)
198         {
199             my $tag=$self->tail();
200 #           print "SM: TAG/TAGVAL($state): $name\n";
201             if($state==$STATE_TAG && $self->is("tag", $tag) &&
202                $self->{db}->tag_has_values($tag->id()))
203             {
204 #               print "Parsing: parent: $tag->id()\n";
205                 my $tagval=ID3FS::Path::Node->new($self->{db}, "tag", $name, $tag->id());
206                 if(defined($tagval))
207                 {
208                     $self->state($STATE_TAGVAL);
209                     # stay in tag state
210                     push(@{$self->{elements}}, $tagval);
211                 }
212                 else
213                 {
214                     $self->state($STATE_INVALID);
215                 }
216             }
217             elsif($name eq $PATH_ALLTRACKS)
218             {
219                 $self->state($STATE_TRACKLIST);
220             }
221             elsif($name eq $PATH_NOARTIST)
222             {
223                 $self->state($STATE_TRACKLIST);
224             }
225             elsif($name eq "AND")
226             {
227                 $self->state($STATE_BOOLEAN);
228                 push(@{$self->{elements}}, ID3FS::Path::Node->new($self->{db}, "boolean", $name));
229             }
230             elsif($name eq "OR")
231             {
232                 $self->state($STATE_BOOLEAN);
233                 push(@{$self->{elements}}, ID3FS::Path::Node->new($self->{db}, "boolean", $name));
234             }
235             else
236             {
237                 my $artist=ID3FS::Path::Node->new($self->{db}, "artist", $name);
238                 if($artist)
239                 {
240                     push(@{$self->{elements}}, $artist);
241                     $self->state($STATE_ALBUMS);
242                 }
243                 else
244                 {
245                     $self->state($STATE_INVALID);
246                 }
247             }
248         }
249         elsif($state==$STATE_BOOLEAN)
250         {
251 #           print "SM: BOOLEAN: $name\n";
252             my $parent=$self->tail();
253             my $allownot=1;
254             if($self->is("boolean", $parent) &&
255                $parent->{name} eq "NOT")
256             {
257                 $allownot=0;
258             }
259             if($allownot && $name eq "NOT")
260             {
261                 $self->state($STATE_BOOLEAN);
262                 push(@{$self->{elements}}, ID3FS::Path::Node->new($self->{db}, "boolean", $name));
263             }
264             else
265             {
266                 my $tag=ID3FS::Path::Node->new($self->{db}, "tag", $name);
267                 if($tag)
268                 {
269                     push(@{$self->{elements}}, $tag);
270                     $tags_seen++;
271                     $self->state($STATE_TAG);
272                 }
273                 else
274                 {
275                     $self->state($STATE_INVALID);
276                 }
277             }
278         }
279         elsif($state==$STATE_ALBUMS)
280         {
281 #           print "SM: ALBUM: $name\n";
282             if($name eq $PATH_ALLTRACKS)
283             {
284                 $self->state($STATE_TRACKLIST);
285             }
286             elsif($name eq $PATH_NOALBUM)
287             {
288                 $self->state($STATE_TRACKLIST);
289             }
290             else
291             {
292                 my $album=ID3FS::Path::Node->new($self->{db}, "album", $name);
293                 if($album)
294                 {
295                     push(@{$self->{elements}}, $album);
296                     $self->state($STATE_TRACKLIST);
297                 }
298                 else
299                 {
300                     $self->state($STATE_INVALID);
301                 }
302             }
303         }
304         elsif($state==$STATE_TRACKLIST)
305         {
306 #           print "SM: TRACKLIST: $name\n";
307             my $track=ID3FS::Path::Node->new($self->{db}, "file", $name);
308             if($track)
309             {
310                 push(@{$self->{elements}}, $track);
311                 $self->state($STATE_FILE);
312             }
313             else
314             {
315                 $self->state($STATE_INVALID);
316             }
317         }
318         elsif($state==$STATE_FILE)
319         {
320 #           print "SM: FILE: $name\n";
321             # Can't have anything after a filename
322             $self->state($STATE_INVALID);
323         }
324         elsif($state==$STATE_ALL)
325         {
326             if($name eq $PATH_ALLTRACKS)
327             {
328                 $self->state($STATE_TRACKLIST);
329             }
330             elsif($name eq $PATH_NOARTIST)
331             {
332                 $self->state($STATE_TRACKLIST);
333             }
334             else
335             {
336                 my $artist=ID3FS::Path::Node->new($self->{db}, "artist", $name);
337                 if($artist)
338                 {
339                     push(@{$self->{elements}}, $artist);
340                     $self->state($STATE_ALBUMS);
341                 }
342                 else
343                 {
344                     $self->state($STATE_INVALID);
345                 }
346             }
347         }
348         else
349         {
350             print "SM: ERROR: UNKNOWN STATE: $self->{state}\n";
351             $self->state($STATE_INVALID);
352         }
353     }
354
355     # remove trailing boolean
356     my @elements=@{$self->{elements}};
357     while(@elements && $self->is("boolean", $elements[$#elements]))
358     {
359         pop @elements;
360     }
361     # sort elements by precedence
362     @elements=$self->sort_elements(@elements);
363     $self->{tagtree}=$self->elements_to_tree(\@elements);
364     if($self->{tagtree})
365     {
366 #       use Data::Dumper;
367 #       print "TREE\n";
368 #       print Dumper $self->{tagtree};
369 #       my ($conditions, @joins)=$self->{tagtree}->to_sql();
370 #       print "CONDITIONS(", scalar(@joins), "): ", $conditions, "\n";
371     }
372 }
373
374 sub state
375 {
376     my($self, $newstate)=@_;
377     if(defined($newstate))
378     {
379         $self->{state}=$newstate;
380         $self->{curtagdepth}++ if($newstate == $STATE_TAG);
381     }
382     return $self->{state};
383 }
384
385 sub elements_to_tree
386 {
387     my($self, $elements)=@_;
388     return undef unless(@$elements);
389     my ($left, $right, $op)=(undef, undef, undef);
390     my $thing=pop @$elements;
391     if($self->is("boolean", $thing))
392     {
393         $right=$self->elements_to_tree($elements);
394         if($thing->{name} ne "NOT")
395         {
396             $left=$self->elements_to_tree($elements);
397         }
398         $thing->left($left);
399         $thing->right($right);
400     }
401     return $thing;
402 }
403
404 # Dijkstra's shunting-yard algorithm
405 sub sort_elements
406 {
407     my ($self, @input)=@_;
408     my @opstack=();
409     my @output=();
410 #    print "INPUT: ", join(', ', map { $_->{name}; } @input), "\n";
411     while(my $thing = shift @input)
412     {
413         if($self->is("tag", $thing))
414         {
415             # Handle tag values by dropping parent
416             if(@input && $self->is("tag", $input[0]))
417             {
418                 $thing=shift @input;
419             }
420             push(@output, $thing);
421         }
422         elsif($self->is("boolean", $thing))
423         {
424             # bool
425             while(@opstack &&
426                   ($priorities{$thing->{name}} <= $priorities{$opstack[$#opstack]->{name}}))
427             {
428                 push(@output, pop(@opstack));
429             }
430             push(@opstack, $thing);
431         }
432     }
433     while(@opstack)
434     {
435         push(@output, pop(@opstack));
436     }
437 #    print "STACK: ", join(', ', map { $_->{name}; } @output), "\n";
438     return @output;
439 }
440
441 sub used_tags
442 {
443     my($self)=@_;
444     return() unless(defined($self->{tagtree}));
445     return($self->{tagtree}->used_tags());
446 }
447
448 sub expecting_values
449 {
450     my($self)=@_;
451     my $tail=$self->tail();
452     if($self->is("tag", $tail))
453     {
454         return($self->{db}->tag_has_values($tail->id()));
455     }
456 }
457
458 sub trailing_tag_id
459 {
460     my($self)=@_;
461     my $tail=$self->tail();
462     if($self->is("tag", $tail))
463     {
464         return($tail->id());
465     }
466     return undef;
467 }
468
469 sub trailing_tag_parent
470 {
471     my($self)=@_;
472     my $tail=$self->tail();
473     if($self->is("tag", $tail))
474     {
475         return($tail->{parents_id});
476     }
477     return undef;
478 }
479
480 sub tail
481 {
482     my($self)=@_;
483     return($self->{elements}->[$#{$self->{elements}}]);
484 }
485
486 sub at
487 {
488     my($self, $type)=@_;
489     return($self->is($type, $self->tail()));
490 }
491
492 sub is
493 {
494     my($self, $type, $thing)=@_;
495     return 0 unless($thing);
496     return 0 unless($thing->type());
497     return 1 if($type eq $thing->type());
498     return 0;
499 }
500
501 # the one before last
502 sub tail_parent
503 {
504     my($self)=@_;
505     return($self->{elements}->[($#{$self->{elements}}) - 1]);
506 }
507
508 ######################################################################
509
510 sub tags
511 {
512     my($self)=@_;
513     if(!$self->{tagtree}) # / or /NOT
514     {
515         my $sql="SELECT DISTINCT name FROM tags WHERE parents_id='';";
516         return($self->{db}->cmd_firstcol($sql));
517     }
518     my $hasvals=$self->expecting_values();
519     my $parent=$self->trailing_tag_parent();
520 #    print "THASVALS: $hasvals\n";
521 #    print "TPARENT: ", (defined($parent)? $parent : "NO"), "\n";
522     my @ids=();
523     my $sql="SELECT tags.name FROM ";
524     if($self->in_or())
525     {
526         $sql .= "files_x_tags\n";
527     }
528     else
529     {
530         $sql .= ("(\n" .
531                  $self->tags_subselect() .
532                  ") AS subselect\n" .
533                  "INNER JOIN files_x_tags ON subselect.files_id=files_x_tags.files_id\n");
534     }
535     $sql .= "INNER JOIN tags ON files_x_tags.tags_id=tags.id\n";
536     my @andclauses=();
537     my $id=$self->trailing_tag_id();
538
539     my $parentclause= "tags.parents_id='";
540     if($hasvals)
541     {
542         $parentclause .= $id;
543     }
544     elsif($parent)
545     {
546         $parentclause .= $parent;
547     }
548     $parentclause .= "'";
549     push(@andclauses, $parentclause);
550
551     my @used=$self->used_tags();
552     if(@used)
553     {
554         push(@andclauses, "tags.id NOT IN (" . join(', ', @used) . ")");
555     }
556     if(@andclauses)
557     {
558         $sql .= "WHERE " . join(' AND ', @andclauses) . "\n";
559     }
560
561     $sql .= "GROUP BY tags.name;";
562     print "SQL(TAGS): $sql\n" if($self->{verbose});
563     my @tagnames=$self->{db}->cmd_firstcol($sql);
564     print("SUBNAMES: ", join(', ', @tagnames), "\n") if($self->{verbose});
565     return(@tagnames);
566 }
567
568 sub artists
569 {
570     my($self)=@_;
571     if(!@{$self->{elements}}) # /ALL
572     {
573         my $sql="SELECT DISTINCT name FROM artists WHERE name!='';";
574         return($self->{db}->cmd_firstcol($sql));
575     }
576     my @ids=();
577     my $sql=$self->sql_start("artists.name");
578     $sql .= ("INNER JOIN artists ON files.artists_id=artists.id\n" .
579              "WHERE artists.name != ''\n" .
580              "GROUP BY artists.name;");
581     print "SQL(ARTISTS): $sql\n" if($self->{verbose});
582     my @tagnames=$self->{db}->cmd_firstcol($sql);
583     print("ARTISTS: ", join(', ', @tagnames), "\n") if($self->{verbose});
584     return(@tagnames);
585 }
586
587 sub albums
588 {
589     my($self)=@_;
590     my @ids=();
591     my $tail=$self->tail();
592     if($self->is("artist", $tail))
593     {
594         return $self->artist_albums($tail->id());
595     }
596     my $sql=$self->sql_start("albums.name");
597     $sql .= ("INNER JOIN albums ON files.albums_id=albums.id\n" .
598              "WHERE albums.name != ''\n" .
599              "GROUP BY albums.name;");
600     print "SQL(ALBUMS): \n$sql\n" if($self->{verbose});
601     my @names=$self->{db}->cmd_firstcol($sql);
602     print("ALBUMS: ", join(', ', @names), "\n") if($self->{verbose});
603     return(@names);
604 }
605
606 sub artist_albums
607 {
608     my($self, $artist_id)=@_;
609     my $sql=$self->sql_start("albums.name");
610     $sql .= ("INNER JOIN albums ON albums.id=files.albums_id\n" .
611              "INNER JOIN artists ON artists.id=files.artists_id\n" .
612              "WHERE artists.id=? and albums.name <> ''\n" .
613              "GROUP BY albums.name\n");
614     print "ARTIST_ALBUMS SQL: $sql\n" if($self->{verbose});
615     my @albums=$self->{db}->cmd_firstcol($sql, $artist_id);
616     print("ALBUMS: ", join(', ', @albums), "\n") if($self->{verbose});
617     return(@albums);
618 }
619
620 sub artist_tracks
621 {
622     my($self, $artist_id)=@_;
623     my $sql=$self->sql_start("files.name");
624     $sql .= ("INNER JOIN artists ON artists.id=files.artists_id\n" .
625              "INNER JOIN albums  ON albums.id=files.albums_id\n" .
626              "WHERE artists.id=? AND albums.name=''\n" .
627              "GROUP BY files.name\n");
628     print "ARTIST_TRACKS SQL: $sql\n" if($self->{verbose});
629     my @names=$self->{db}->cmd_firstcol($sql, $artist_id);
630     print("ARTISTTRACKS: ", join(', ', @names), "\n") if($self->{verbose});
631     return(@names);
632 }
633
634 sub album_tracks
635 {
636     my($self, $artist_id, $album_id)=@_;
637     my $sql=("SELECT files.name FROM files\n" .
638              "INNER JOIN albums  ON albums.id=files.albums_id\n" .
639              "INNER JOIN artists ON artists.id=files.artists_id\n" .
640              "WHERE artists.id=? AND albums.id=?\n" .
641              "GROUP BY files.name\n");
642     print "ALBUM_TRACKS SQL($artist_id, $album_id): $sql\n" if($self->{verbose});
643     my @names=$self->{db}->cmd_firstcol($sql, $artist_id, $album_id);
644     print("TRACKS: ", join(', ', @names), "\n") if($self->{verbose});
645     return(@names);
646 }
647
648 sub tracks
649 {
650     my($self)=@_;
651     my $tail=$self->tail();
652     if($self->is("artist", $tail))
653     {
654         return $self->artist_tracks($tail->id());
655     }
656     elsif($self->is("album", $tail))
657     {
658         my $artist_id=0;
659         my $artist=$self->tail_parent();
660         if($self->is("artist", $artist))
661         {
662             # should always happen
663             $artist_id=$artist->id();
664         }
665         return $self->album_tracks($artist_id, $tail->id());
666     }
667     my $sql=$self->sql_start("files.name");
668     $sql .= "INNER JOIN artists ON files.artists_id=artists.id\n";
669     if($self->{components}->[$#{$self->{components}}] eq $PATH_NOARTIST)
670     {
671         $sql .= "WHERE artists.name =''\n";
672     }
673     $sql .= "GROUP BY files.name;";
674     print "TRACKS SQL($self->{path}): $sql\n" if($self->{verbose});
675     my @names=$self->{db}->cmd_firstcol($sql);
676     print("TRACKS: ", join(', ', @names), "\n") if($self->{verbose});
677     return(@names);
678 }
679
680 sub filename
681 {
682     my($self, $mountpoint)=@_;
683     my $tail=$self->tail();
684     if($self->is("file", $tail))
685     {
686         my $id=$tail->id();
687         my $sql=("SELECT paths.name, files.name FROM files\n" .
688                  "INNER JOIN paths ON files.paths_id=paths.id\n" .
689                  "WHERE files.id=?\n" .
690                  "GROUP BY paths.name, files.name");
691         print "FILENAME SQL: $sql\n" if($self->{verbose});
692         my ($path, $name)=$self->{db}->cmd_onerow($sql, $id);
693         my $id3fs_path=join('/', map { $_->{name}; }  @{$self->{elements}});
694         return($self->{db}->relativise($path, $name, $mountpoint, $self->{path}));
695     }
696     # should never happen
697     return "ERROR";
698 }
699
700 sub tags_subselect
701 {
702     my($self)=@_;
703     my $hasvals=$self->expecting_values();
704     my $tree=$self->{tagtree};
705     my $parent=$self->trailing_tag_parent();
706
707     my $tag=undef;
708     if($hasvals)
709     {
710         $tag=$self->trailing_tag_id();
711 #       print "Trailing id: $tag\n";
712     }
713     my ($sqlclause, @joins)=(undef, ());
714     ($sqlclause, @joins) = $tree->to_sql($hasvals) if($tree);
715 #    print "SQL(" . scalar(@joins) ."): $sqlclause\n";
716     my $sql="\tSELECT fxt1.files_id FROM tags t1";
717     my @crosses=();
718     my @inners=();
719 #    $joinsneeded++ if($tag);
720     for(my $i=0; $i <= $#joins; $i++)
721     {
722         my $cnt=$i+1;
723         my $join=$joins[$i];
724         my $inner=("\t$join JOIN files_x_tags fxt$cnt ON " .
725                    "t${cnt}.id=fxt${cnt}.tags_id");
726         if($i > 0)
727         {
728             push(@crosses, "CROSS JOIN tags t$cnt");
729             $inner .= " AND fxt1.files_id=fxt${cnt}.files_id";
730         }
731         push(@inners, $inner);
732     }
733     $sql .= ("\n\t" . join(" ", @crosses)) if(@crosses);
734     $sql .= ("\n" . join("\n", @inners)) if(@inners);
735     $sql .= "\n\tWHERE $sqlclause" if($sqlclause);
736 #    if($tag)
737 #    {
738 #       $sql .= " AND t${joinsneeded}.parents_id='$tag'";
739 #    }
740     $sql .= "\n\tGROUP BY fxt1.files_id\n";
741     return $sql;
742 }
743
744 sub sql_start
745 {
746     my($self, $tables)=@_;
747     my $sql="SELECT $tables FROM ";
748     if($self->{in_all})
749     {
750         $sql .= "files\n";
751     }
752     else
753     {
754         $sql .= ("(\n" .
755                  $self->tags_subselect() .
756                  ") AS subselect\n" .
757                  "INNER JOIN files ON subselect.files_id=files.id\n");
758     }
759     return $sql;
760 }
761
762 # we just filter $ALLTRACKS, $NOARTIST and $NOALBUM
763 # filtering tags properly requires up to four levels of recursion
764 # (tag/tagval/AND/NOT) and is too slow
765 sub filter
766 {
767     my($self, @dirs)=@_;
768     return(@dirs) unless($ENABLE_FILTER);
769     my $base=$self->{path};
770     my @outdirs=();
771     for my $dir (@dirs)
772     {
773 #       print "\nFILTER (",$self->state(), "): $base / $dir\n";
774         if($self->empty("$base/$dir"))
775         {
776 #           print "empty: $base / $dir\n";
777         }
778         else
779         {
780 #           print "non-empty, accepting: $base / $dir\n";
781             push(@outdirs, $dir);
782         }
783     }
784     return(@outdirs);
785 }
786
787 sub empty
788 {
789     my($self, $dir)=@_;
790     my $path=ID3FS::Path->new($self->{db}, $dir, $self->{verbose},
791                               ($self->{maxtagdepth} - $self->{curtagdepth}));
792     return 1 unless($path->isvalid());
793     my($subdirs,$subfiles)=$path->dirents();
794     return 0 if(@$subfiles || @$subdirs);
795     return 1;
796 }
797
798 # if path is .../OR/ or .../OR/NOT
799 sub in_or
800 {
801     my($self)=@_;
802     my $tail=$self->tail();
803     return 0 unless($tail);
804     return 0 unless($tail->type() eq "boolean");
805     return 1 if($tail->name() eq "OR");
806     return 0 unless($tail->name() eq "NOT");
807     my $parent=$self->tail_parent();
808     return 0 unless($parent);
809     return 0 unless($parent->type() eq "boolean");
810     return 1 if($parent->name() eq "OR");
811     return 0;
812 }
813
814 1;