sql formatting tweaks
[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_BOOLEAN,
24      $STATE_ALBUMS, $STATE_TRACKLIST, $STATE_FILE, $STATE_ALL)=(0..7);
25
26 # operator precedence
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)
101     {
102         my $tail=$self->tail();
103         if($self->is($TYPE_TAG, $tail) && $self->{db}->tag_has_values($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($TYPE_BOOL, $parent) && $parent->{name} eq "NOT")
120         {
121             @dents=("NOT");
122         }
123         push(@dents,$self->tags());
124     }
125     elsif($state==$STATE_ROOT)
126     {
127         @dents=(qw(ALL NOT), $self->tags());
128     }
129     elsif($state==$STATE_ALBUMS)
130     {
131         @dents=$self->filter($PATH_ALLTRACKS, $PATH_NOALBUM, $self->albums());
132     }
133     elsif($state==$STATE_TRACKLIST)
134     {
135         @fents=$self->tracks();
136     }
137     else
138     {
139         print "id3fsd: INTERNAL ERROR: DIRENTS: UNHANDLED STATE: $state\n";
140     }
141     return(\@dents, \@fents);
142 }
143
144 # State Machine Of Doom
145 sub parse
146 {
147     my($self)=@_;
148     $self->state($STATE_ROOT);
149     return if($self->{path} eq "/");
150     @{$self->{components}}=split(/\//, $self->{path});
151     shift @{$self->{components}}; # drop empty field before leading /
152 #    print "PATH: $self->{path}\n";
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}, $TYPE_BOOL, $name));
180                 $self->state($STATE_BOOLEAN);
181             }
182             else
183             {
184                 $tag=ID3FS::Path::Node->new($self->{db}, $TYPE_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)
198         {
199             my $tag=$self->tail();
200 #           print "SM: TAG/TAGVAL($state): $name\n";
201             if($self->is($TYPE_TAG, $tag) && $self->{db}->tag_has_values($tag->id()))
202             {
203 #               print "Parsing: parent: $tag->id()\n";
204                 my $tagval=ID3FS::Path::Node->new($self->{db}, $TYPE_TAG, $name, $tag->id());
205                 if(defined($tagval))
206                 {
207                     # stay in tag state
208                     push(@{$self->{elements}}, $tagval);
209                 }
210                 else
211                 {
212                     $self->state($STATE_INVALID);
213                 }
214             }
215             elsif($name eq $PATH_ALLTRACKS)
216             {
217                 $self->state($STATE_TRACKLIST);
218             }
219             elsif($name eq $PATH_NOARTIST)
220             {
221                 $self->state($STATE_TRACKLIST);
222             }
223             elsif($name eq "AND")
224             {
225                 $self->state($STATE_BOOLEAN);
226                 push(@{$self->{elements}}, ID3FS::Path::Node->new($self->{db}, $TYPE_BOOL, $name));
227             }
228             elsif($name eq "OR")
229             {
230                 $self->state($STATE_BOOLEAN);
231                 push(@{$self->{elements}}, ID3FS::Path::Node->new($self->{db}, $TYPE_BOOL, $name));
232             }
233             else
234             {
235                 my $artist=ID3FS::Path::Node->new($self->{db}, $TYPE_ARTIST, $name);
236                 if($artist)
237                 {
238                     push(@{$self->{elements}}, $artist);
239                     $self->state($STATE_ALBUMS);
240                 }
241                 else
242                 {
243                     $self->state($STATE_INVALID);
244                 }
245             }
246         }
247         elsif($state==$STATE_BOOLEAN)
248         {
249 #           print "SM: BOOLEAN: $name\n";
250             my $parent=$self->tail();
251             my $allownot=1;
252             if($self->is($TYPE_BOOL, $parent) &&
253                $parent->{name} eq "NOT")
254             {
255                 $allownot=0;
256             }
257             if($allownot && $name eq "NOT")
258             {
259                 $self->state($STATE_BOOLEAN);
260                 push(@{$self->{elements}}, ID3FS::Path::Node->new($self->{db}, $TYPE_BOOL, $name));
261             }
262             else
263             {
264                 my $tag=ID3FS::Path::Node->new($self->{db}, $TYPE_TAG, $name);
265                 if($tag)
266                 {
267                     push(@{$self->{elements}}, $tag);
268                     $tags_seen++;
269                     $self->state($STATE_TAG);
270                 }
271                 else
272                 {
273                     $self->state($STATE_INVALID);
274                 }
275             }
276         }
277         elsif($state==$STATE_ALBUMS)
278         {
279 #           print "SM: ALBUM: $name\n";
280             if($name eq $PATH_ALLTRACKS)
281             {
282                 $self->state($STATE_TRACKLIST);
283             }
284             elsif($name eq $PATH_NOALBUM)
285             {
286                 $self->state($STATE_TRACKLIST);
287             }
288             else
289             {
290                 my $album=ID3FS::Path::Node->new($self->{db}, $TYPE_ALBUM, $name);
291                 if($album)
292                 {
293                     push(@{$self->{elements}}, $album);
294                     $self->state($STATE_TRACKLIST);
295                 }
296                 else
297                 {
298                     $self->state($STATE_INVALID);
299                 }
300             }
301         }
302         elsif($state==$STATE_TRACKLIST)
303         {
304 #           print "SM: TRACKLIST: $name\n";
305             my $track=ID3FS::Path::Node->new($self->{db}, $TYPE_FILE, $name);
306             if($track)
307             {
308                 push(@{$self->{elements}}, $track);
309                 $self->state($STATE_FILE);
310             }
311             else
312             {
313                 $self->state($STATE_INVALID);
314             }
315         }
316         elsif($state==$STATE_FILE)
317         {
318 #           print "SM: FILE: $name\n";
319             # Can't have anything after a filename
320             $self->state($STATE_INVALID);
321         }
322         elsif($state==$STATE_ALL)
323         {
324             if($name eq $PATH_ALLTRACKS)
325             {
326                 $self->state($STATE_TRACKLIST);
327             }
328             elsif($name eq $PATH_NOARTIST)
329             {
330                 $self->state($STATE_TRACKLIST);
331             }
332             else
333             {
334                 my $artist=ID3FS::Path::Node->new($self->{db}, $TYPE_ARTIST, $name);
335                 if($artist)
336                 {
337                     push(@{$self->{elements}}, $artist);
338                     $self->state($STATE_ALBUMS);
339                 }
340                 else
341                 {
342                     $self->state($STATE_INVALID);
343                 }
344             }
345         }
346         else
347         {
348             print "SM: ERROR: UNKNOWN STATE: $self->{state}\n";
349             $self->state($STATE_INVALID);
350         }
351     }
352
353     my @elements=@{$self->{elements}};
354     # remove trailing boolean
355     while(@elements && $self->is($TYPE_BOOL, $elements[$#elements]))
356     {
357         pop @elements;
358     }
359     # sort elements by precedence
360     @elements=$self->sort_elements(@elements);
361     $self->{tagtree}=$self->elements_to_tree(\@elements);
362 }
363
364 sub state
365 {
366     my($self, $newstate)=@_;
367     if(defined($newstate))
368     {
369         $self->{state}=$newstate;
370         $self->{curtagdepth}++ if($newstate == $STATE_TAG);
371     }
372     return $self->{state};
373 }
374
375 # link up precedence-sorted list into a binary tree
376 sub elements_to_tree
377 {
378     my($self, $elements)=@_;
379     return undef unless(@$elements);
380     my ($left, $right, $op)=(undef, undef, undef);
381     my $thing=pop @$elements;
382     if($self->is($TYPE_BOOL, $thing))
383     {
384         $right=$self->elements_to_tree($elements);
385         if($thing->{name} ne "NOT")
386         {
387             $left=$self->elements_to_tree($elements);
388         }
389         $thing->left($left);
390         $thing->right($right);
391     }
392     return $thing;
393 }
394
395 # Dijkstra's shunting-yard algorithm
396 sub sort_elements
397 {
398     my ($self, @input)=@_;
399     my @opstack=();
400     my @output=();
401 #    print "INPUT: ", join(', ', map { $_->{name}; } @input), "\n";
402     while(my $thing = shift @input)
403     {
404         if($self->is($TYPE_TAG, $thing))
405         {
406             # Handle tag values by dropping parent
407             if(@input && $self->is($TYPE_TAG, $input[0]))
408             {
409                 $thing=shift @input;
410             }
411             push(@output, $thing);
412         }
413         elsif($self->is($TYPE_BOOL, $thing))
414         {
415             while(@opstack &&
416                   ($priorities{$thing->{name}} <= $priorities{$opstack[$#opstack]->{name}}))
417             {
418                 push(@output, pop(@opstack));
419             }
420             push(@opstack, $thing);
421         }
422     }
423     while(@opstack)
424     {
425         push(@output, pop(@opstack));
426     }
427 #    print "STACK: ", join(', ', map { $_->{name}; } @output), "\n";
428     return @output;
429 }
430
431 sub used_tags
432 {
433     my($self)=@_;
434     return() unless(defined($self->{tagtree}));
435     return($self->{tagtree}->used_tags());
436 }
437
438 sub expecting_values
439 {
440     my($self)=@_;
441     my $tail=$self->tail();
442     if($self->is($TYPE_TAG, $tail))
443     {
444         return($self->{db}->tag_has_values($tail->id()));
445     }
446 }
447
448 sub trailing_tag_id
449 {
450     my($self)=@_;
451     my $tail=$self->tail();
452     if($self->is($TYPE_TAG, $tail))
453     {
454         return($tail->id());
455     }
456     return undef;
457 }
458
459 sub trailing_tag_parent
460 {
461     my($self)=@_;
462     my $tail=$self->tail();
463     if($self->is($TYPE_TAG, $tail))
464     {
465         return($tail->{parents_id});
466     }
467     return undef;
468 }
469
470 sub tail
471 {
472     my($self)=@_;
473     return($self->{elements}->[$#{$self->{elements}}]);
474 }
475
476 sub is
477 {
478     my($self, $type, $thing)=@_;
479     return 0 unless($thing);
480     return 0 unless($thing->type());
481     return 1 if($type == $thing->type());
482     return 0;
483 }
484
485 # the one before last
486 sub tail_parent
487 {
488     my($self)=@_;
489     return($self->{elements}->[($#{$self->{elements}}) - 1]);
490 }
491
492 ######################################################################
493
494 sub tags
495 {
496     my($self)=@_;
497     if(!$self->{tagtree}) # / or /NOT
498     {
499         my $sql="SELECT DISTINCT name FROM tags WHERE parents_id='';";
500         return($self->{db}->cmd_firstcol($sql));
501     }
502     my $hasvals=$self->expecting_values();
503     my $parent=$self->trailing_tag_parent();
504 #    print "THASVALS: $hasvals\n";
505 #    print "TPARENT: ", (defined($parent)? $parent : "NO"), "\n";
506     my @ids=();
507     my $sql="SELECT tags.name FROM ";
508     if($self->in_or())
509     {
510         $sql .= "files_x_tags\n";
511     }
512     else
513     {
514         $sql .= ("(\n" .
515                  $self->tags_subselect() .
516                  ") AS subselect\n" .
517                  "INNER JOIN files_x_tags ON subselect.files_id=files_x_tags.files_id\n");
518     }
519     $sql .= "INNER JOIN tags ON files_x_tags.tags_id=tags.id\n";
520     my @andclauses=();
521     my $id=$self->trailing_tag_id();
522
523     my $parentclause= "tags.parents_id='";
524     if($hasvals)
525     {
526         $parentclause .= $id;
527     }
528     elsif($parent)
529     {
530         $parentclause .= $parent;
531     }
532     $parentclause .= "'";
533     push(@andclauses, $parentclause);
534
535     my @used=$self->used_tags();
536     if(@used)
537     {
538         push(@andclauses, "tags.id NOT IN (" . join(', ', @used) . ")");
539     }
540     if(@andclauses)
541     {
542         $sql .= "WHERE " . join(' AND ', @andclauses) . "\n";
543     }
544
545     $sql .= "GROUP BY tags.name;";
546     print "SQL(TAGS): $sql\n" if($self->{verbose});
547     my @tagnames=$self->{db}->cmd_firstcol($sql);
548     print("SUBNAMES: ", join(', ', @tagnames), "\n") if($self->{verbose});
549     return(@tagnames);
550 }
551
552 sub artists
553 {
554     my($self)=@_;
555     if(!@{$self->{elements}}) # /ALL
556     {
557         my $sql="SELECT DISTINCT name FROM artists WHERE name!='';";
558         return($self->{db}->cmd_firstcol($sql));
559     }
560     my @ids=();
561     my $sql=$self->sql_start("artists.name");
562     $sql .= ("INNER JOIN artists ON files.artists_id=artists.id\n" .
563              "WHERE artists.name != ''\n" .
564              "GROUP BY artists.name;");
565     print "SQL(ARTISTS): $sql\n" if($self->{verbose});
566     my @tagnames=$self->{db}->cmd_firstcol($sql);
567     print("ARTISTS: ", join(', ', @tagnames), "\n") if($self->{verbose});
568     return(@tagnames);
569 }
570
571 sub albums
572 {
573     my($self)=@_;
574     my @ids=();
575     my $tail=$self->tail();
576     if($self->is($TYPE_ARTIST, $tail))
577     {
578         return $self->artist_albums($tail->id());
579     }
580     my $sql=$self->sql_start("albums.name");
581     $sql .= ("INNER JOIN albums ON files.albums_id=albums.id\n" .
582              "WHERE albums.name != ''\n" .
583              "GROUP BY albums.name;");
584     print "SQL(ALBUMS): \n$sql\n" if($self->{verbose});
585     my @names=$self->{db}->cmd_firstcol($sql);
586     print("ALBUMS: ", join(', ', @names), "\n") if($self->{verbose});
587     return(@names);
588 }
589
590 sub artist_albums
591 {
592     my($self, $artist_id)=@_;
593     my $sql=$self->sql_start("albums.name");
594     $sql .= ("INNER JOIN albums ON albums.id=files.albums_id\n" .
595              "INNER JOIN artists ON artists.id=files.artists_id\n" .
596              "WHERE artists.id=? and albums.name <> ''\n" .
597              "GROUP BY albums.name\n");
598     print "ARTIST_ALBUMS SQL: $sql\n" if($self->{verbose});
599     my @albums=$self->{db}->cmd_firstcol($sql, $artist_id);
600     print("ALBUMS: ", join(', ', @albums), "\n") if($self->{verbose});
601     return(@albums);
602 }
603
604 sub artist_tracks
605 {
606     my($self, $artist_id)=@_;
607     my $sql=$self->sql_start("files.name");
608     $sql .= ("INNER JOIN artists ON artists.id=files.artists_id\n" .
609              "INNER JOIN albums  ON albums.id=files.albums_id\n" .
610              "WHERE artists.id=? AND albums.name=''\n" .
611              "GROUP BY files.name\n");
612     print "ARTIST_TRACKS SQL: $sql\n" if($self->{verbose});
613     my @names=$self->{db}->cmd_firstcol($sql, $artist_id);
614     print("ARTISTTRACKS: ", join(', ', @names), "\n") if($self->{verbose});
615     return(@names);
616 }
617
618 sub album_tracks
619 {
620     my($self, $artist_id, $album_id)=@_;
621     my $sql=("SELECT files.name FROM files\n" .
622              "INNER JOIN albums  ON albums.id=files.albums_id\n" .
623              "INNER JOIN artists ON artists.id=files.artists_id\n" .
624              "WHERE artists.id=? AND albums.id=?\n" .
625              "GROUP BY files.name\n");
626     print "ALBUM_TRACKS SQL($artist_id, $album_id): $sql\n" if($self->{verbose});
627     my @names=$self->{db}->cmd_firstcol($sql, $artist_id, $album_id);
628     print("TRACKS: ", join(', ', @names), "\n") if($self->{verbose});
629     return(@names);
630 }
631
632 sub tracks
633 {
634     my($self)=@_;
635     my $tail=$self->tail();
636     if($self->is($TYPE_ARTIST, $tail))
637     {
638         return $self->artist_tracks($tail->id());
639     }
640     elsif($self->is($TYPE_ALBUM, $tail))
641     {
642         my $artist_id=0;
643         my $artist=$self->tail_parent();
644         if($self->is($TYPE_ARTIST, $artist))
645         {
646             # should always happen
647             $artist_id=$artist->id();
648         }
649         return $self->album_tracks($artist_id, $tail->id());
650     }
651     my $sql=$self->sql_start("files.name");
652     $sql .= "INNER JOIN artists ON files.artists_id=artists.id\n";
653     if($self->{components}->[$#{$self->{components}}] eq $PATH_NOARTIST)
654     {
655         $sql .= "WHERE artists.name =''\n";
656     }
657     $sql .= "GROUP BY files.name;";
658     print "TRACKS SQL($self->{path}): $sql\n" if($self->{verbose});
659     my @names=$self->{db}->cmd_firstcol($sql);
660     print("TRACKS: ", join(', ', @names), "\n") if($self->{verbose});
661     return(@names);
662 }
663
664 sub filename
665 {
666     my($self, $mountpoint)=@_;
667     my $tail=$self->tail();
668     if($self->is($TYPE_FILE, $tail))
669     {
670         my $id=$tail->id();
671         my $sql=("SELECT paths.name, files.name FROM files\n" .
672                  "INNER JOIN paths ON files.paths_id=paths.id\n" .
673                  "WHERE files.id=?\n" .
674                  "GROUP BY paths.name, files.name");
675         print "FILENAME SQL: $sql\n" if($self->{verbose});
676         my ($path, $name)=$self->{db}->cmd_onerow($sql, $id);
677         return($self->{db}->relativise($path, $name, $mountpoint, $self->{path}));
678     }
679     # should never happen
680     return "ERROR";
681 }
682
683 sub tags_subselect
684 {
685     my($self)=@_;
686     my $hasvals=$self->expecting_values();
687     my $tree=$self->{tagtree};
688     my $parent=$self->trailing_tag_parent();
689
690     my $tag=undef;
691     if($hasvals)
692     {
693         $tag=$self->trailing_tag_id();
694 #       print "Trailing id: $tag\n";
695     }
696     my ($sqlclause, @joins)=(undef, ());
697     ($sqlclause, @joins) = $tree->to_sql($hasvals) if($tree);
698 #    use Data::Dumper;
699 #    print Dumper $tree if($tree);
700 #    print "SQL(" . scalar(@joins) ."): $sqlclause\n";
701     my $sql="\tSELECT fxt1.files_id FROM tags t1";
702     my @crosses=();
703     my @inners=();
704 #    $joinsneeded++ if($tag);
705     for(my $i=0; $i <= $#joins; $i++)
706     {
707         my $cnt=$i+1;
708         my $join=$joins[$i];
709         my $inner=("\t$join JOIN files_x_tags fxt$cnt ON " .
710                    "t${cnt}.id=fxt${cnt}.tags_id");
711         if($i > 0)
712         {
713             push(@crosses, "CROSS JOIN tags t$cnt");
714             $inner .= " AND fxt1.files_id=fxt${cnt}.files_id";
715         }
716         push(@inners, $inner);
717     }
718     $sql .= ("\n\t" . join(" ", @crosses)) if(@crosses);
719     $sql .= ("\n" . join("\n", @inners)) if(@inners);
720     $sql .= "\n\tWHERE $sqlclause" if($sqlclause);
721 #    if($tag)
722 #    {
723 #       $sql .= " AND t${joinsneeded}.parents_id='$tag'";
724 #    }
725     $sql .= "\n\tGROUP BY fxt1.files_id\n";
726     return $sql;
727 }
728
729 sub sql_start
730 {
731     my($self, $tables)=@_;
732     my $sql="SELECT $tables FROM ";
733     if($self->{in_all})
734     {
735         $sql .= "files\n";
736     }
737     else
738     {
739         $sql .= ("(\n" .
740                  $self->tags_subselect() .
741                  ") AS subselect\n" .
742                  "INNER JOIN files ON subselect.files_id=files.id\n");
743     }
744     return $sql;
745 }
746
747 # we just filter $ALLTRACKS, $NOARTIST and $NOALBUM
748 # filtering tags properly requires up to four levels of recursion
749 # (tag/tagval/AND/NOT) and is too slow
750 sub filter
751 {
752     my($self, @dirs)=@_;
753     return(@dirs) unless($ENABLE_FILTER);
754     my $base=$self->{path};
755     my @outdirs=();
756     for my $dir (@dirs)
757     {
758         push(@outdirs, $dir) unless($self->empty("$base/$dir"));
759     }
760     return(@outdirs);
761 }
762
763 sub empty
764 {
765     my($self, $dir)=@_;
766     my $path=ID3FS::Path->new($self->{db}, $dir, $self->{verbose},
767                               ($self->{maxtagdepth} - $self->{curtagdepth}));
768     return 1 unless($path->isvalid());
769     my($subdirs,$subfiles)=$path->dirents();
770     return 0 if(@$subfiles || @$subdirs);
771     return 1;
772 }
773
774 # if path is .../OR/ or .../OR/NOT
775 sub in_or
776 {
777     my($self)=@_;
778     my $tail=$self->tail();
779     return 0 unless($tail);
780     return 0 unless($tail->type() == $TYPE_BOOL);
781     return 1 if($tail->name() eq "OR");
782     return 0 unless($tail->name() eq "NOT");
783     my $parent=$self->tail_parent();
784     return 0 unless($parent);
785     return 0 unless($parent->type() == $TYPE_BOOL);
786     return 1 if($parent->name() eq "OR");
787     return 0;
788 }
789
790 1;