1 # id3fs - a FUSE-based filesystem for browsing audio metadata
2 # Copyright (C) 2010 Ian Beckwith <ianb@erislabs.net>
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.
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.
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/>.
21 use ID3FS::Path::Node;
23 our ($STATE_INVALID, $STATE_ROOT, $STATE_TAG, $STATE_BOOLEAN,
24 $STATE_ALBUMS, $STATE_TRACKLIST, $STATE_FILE, $STATE_ALL)=(0..7);
27 our %priorities=( "OR" => 0, "AND" => 1, "NOT" => 2 );
29 our $PATH_ALLTRACKS= "TRACKS";
30 our $PATH_NOARTIST = "NOARTIST";
31 our $PATH_NOALBUM = "NOALBUM";
33 our $ENABLE_FILTER = 0;
38 my $class=ref($proto) || $proto;
45 $self->{verbose}=shift;
46 $self->{maxtagdepth}=shift;
47 $self->{curtagdepth}=0;
48 $self->{path} =~ s/\/\//\//g; # drop doubled slashes
57 if(($self->state() == $STATE_FILE) ||
58 ($self->state() == $STATE_INVALID))
68 return($self->state() == $STATE_FILE);
74 return($self->state() != $STATE_INVALID);
79 my($self, $mountpoint)=@_;
80 if($self->state() == $STATE_FILE)
82 return $self->filename($mountpoint);
84 return "ERROR"; #should never happen?
92 my $state=$self->state();
93 if($state==$STATE_ALL)
95 @dents=($self->filter($PATH_ALLTRACKS, $PATH_NOARTIST), $self->artists());
97 elsif($state==$STATE_TAG)
99 my $tail=$self->tail();
100 if($self->is($TYPE_TAG, $tail) && $self->{db}->tag_has_values($tail->id()))
102 @dents=$self->tags();
106 if($self->{maxtagdepth} && ($self->{curtagdepth} < $self->{maxtagdepth}))
110 push(@dents, $self->filter($PATH_ALLTRACKS, $PATH_NOARTIST), $self->artists());
113 elsif($state==$STATE_BOOLEAN)
115 my $parent=$self->tail();
116 unless($self->is($TYPE_BOOL, $parent) && $parent->name() eq "NOT")
120 push(@dents,$self->tags());
122 elsif($state==$STATE_ROOT)
124 @dents=(qw(ALL NOT), $self->tags());
126 elsif($state==$STATE_ALBUMS)
128 @dents=$self->filter($PATH_ALLTRACKS, $PATH_NOALBUM, $self->albums());
130 elsif($state==$STATE_TRACKLIST)
132 @fents=$self->tracks();
136 print "id3fsd: INTERNAL ERROR: DIRENTS: UNHANDLED STATE: $state\n";
138 return(\@dents, \@fents);
141 # State Machine Of Doom
145 $self->state($STATE_ROOT);
146 return if($self->{path} eq "/");
147 @{$self->{components}}=split(/\//, $self->{path});
148 shift @{$self->{components}}; # drop empty field before leading /
149 my @parts=@{$self->{components}};
151 $self->{elements}=[];
155 while(defined(my $name=shift @parts))
157 my $state=$self->state();
158 if($state==$STATE_INVALID)
162 elsif($state==$STATE_ROOT)
167 $self->state($STATE_ALL);
169 elsif($name eq "NOT")
172 push(@{$self->{elements}}, ID3FS::Path::Node->new($self->{db}, $TYPE_BOOL, $name));
173 $self->state($STATE_BOOLEAN);
177 $tag=ID3FS::Path::Node->new($self->{db}, $TYPE_TAG, $name);
180 push(@{$self->{elements}}, $tag);
182 $self->state($STATE_TAG);
186 $self->state($STATE_INVALID);
190 elsif($state==$STATE_TAG)
192 my $tag=$self->tail();
193 if($self->is($TYPE_TAG, $tag) && $self->{db}->tag_has_values($tag->id()))
195 my $tagval=ID3FS::Path::Node->new($self->{db}, $TYPE_TAG, $name, $tag->id());
199 push(@{$self->{elements}}, $tagval);
203 $self->state($STATE_INVALID);
206 elsif($name eq $PATH_ALLTRACKS)
208 $self->state($STATE_TRACKLIST);
210 elsif($name eq $PATH_NOARTIST)
212 $self->state($STATE_TRACKLIST);
214 elsif($name eq "AND")
216 $self->state($STATE_BOOLEAN);
217 push(@{$self->{elements}}, ID3FS::Path::Node->new($self->{db}, $TYPE_BOOL, $name));
221 $self->state($STATE_BOOLEAN);
222 push(@{$self->{elements}}, ID3FS::Path::Node->new($self->{db}, $TYPE_BOOL, $name));
226 my $artist=ID3FS::Path::Node->new($self->{db}, $TYPE_ARTIST, $name);
229 push(@{$self->{elements}}, $artist);
230 $self->state($STATE_ALBUMS);
234 $self->state($STATE_INVALID);
238 elsif($state==$STATE_BOOLEAN)
240 my $parent=$self->tail();
242 if($self->is($TYPE_BOOL, $parent) &&
243 $parent->name() eq "NOT")
247 if($allownot && $name eq "NOT")
249 $self->state($STATE_BOOLEAN);
250 push(@{$self->{elements}}, ID3FS::Path::Node->new($self->{db}, $TYPE_BOOL, $name));
254 my $tag=ID3FS::Path::Node->new($self->{db}, $TYPE_TAG, $name);
257 push(@{$self->{elements}}, $tag);
259 $self->state($STATE_TAG);
263 $self->state($STATE_INVALID);
267 elsif($state==$STATE_ALBUMS)
269 if($name eq $PATH_ALLTRACKS)
271 $self->state($STATE_TRACKLIST);
273 elsif($name eq $PATH_NOALBUM)
275 $self->state($STATE_TRACKLIST);
279 my $album=ID3FS::Path::Node->new($self->{db}, $TYPE_ALBUM, $name);
282 push(@{$self->{elements}}, $album);
283 $self->state($STATE_TRACKLIST);
287 $self->state($STATE_INVALID);
291 elsif($state==$STATE_TRACKLIST)
293 my $track=ID3FS::Path::Node->new($self->{db}, $TYPE_FILE, $name);
296 push(@{$self->{elements}}, $track);
297 $self->state($STATE_FILE);
301 $self->state($STATE_INVALID);
304 elsif($state==$STATE_FILE)
306 # Can't have anything after a filename
307 $self->state($STATE_INVALID);
309 elsif($state==$STATE_ALL)
311 if($name eq $PATH_ALLTRACKS)
313 $self->state($STATE_TRACKLIST);
315 elsif($name eq $PATH_NOARTIST)
317 $self->state($STATE_TRACKLIST);
321 my $artist=ID3FS::Path::Node->new($self->{db}, $TYPE_ARTIST, $name);
324 push(@{$self->{elements}}, $artist);
325 $self->state($STATE_ALBUMS);
329 $self->state($STATE_INVALID);
335 print "SM: ERROR: UNKNOWN STATE: $self->{state}\n";
336 $self->state($STATE_INVALID);
340 my @elements=@{$self->{elements}};
341 # remove trailing boolean
342 while(@elements && $self->is($TYPE_BOOL, $elements[$#elements]))
347 # calculate joins and assign table numbers to nodes
348 $self->{joins}=[$self->number_joins(@elements)];
350 # always need at least one join
351 $self->{joins}=[qw(INNER)] unless(@{$self->{joins}});
353 # sort elements by precedence
354 @elements=$self->sort_elements(@elements);
356 # convert to binary tree
357 $self->{tagtree}=$self->elements_to_tree(\@elements);
362 my($self, @elements)=@_;
367 return (@joins) unless(@elements);
368 while(my $node=shift @elements)
370 if($node->type() == $TYPE_BOOL)
372 my $op=$node->name();
383 # NOT/foo/OR needs an extra join
384 $nextjoin="INNER" if($lastop && $lastop eq "NOT");
393 # if tag has a value, eat the tag, shifting to the value
394 if($elements[0]->type() == $TYPE_TAG)
397 $node=shift(@elements);
400 elsif($self->{db}->tag_has_values($node->id()))
402 # if the expression ends in a tag that has a value
403 # (ie we have the tag and want the value)
404 # use an INNER join even if we were in a NOT
405 $nextjoin="INNER" if($nextjoin);
410 push(@joins, $nextjoin);
413 $node->table($table);
421 my($self, $newstate)=@_;
422 if(defined($newstate))
424 $self->{state}=$newstate;
425 $self->{curtagdepth}++ if($newstate == $STATE_TAG);
427 return $self->{state};
430 # link up precedence-sorted list into a binary tree
433 my($self, $elements)=@_;
434 return undef unless(@$elements);
435 my ($left, $right, $op)=(undef, undef, undef);
436 my $node=pop @$elements;
437 if($self->is($TYPE_BOOL, $node))
439 $right=$self->elements_to_tree($elements);
440 if($node->name() ne "NOT")
442 $left=$self->elements_to_tree($elements);
445 $node->right($right);
450 # Dijkstra's shunting-yard algorithm
453 my ($self, @input)=@_;
456 while(my $node = shift @input)
458 if($self->is($TYPE_TAG, $node))
460 # Handle tag values by dropping parent
461 if(@input && $self->is($TYPE_TAG, $input[0]))
465 push(@output, $node);
467 elsif($self->is($TYPE_BOOL, $node))
470 ($priorities{$node->name()} <= $priorities{$opstack[$#opstack]->name()}))
472 push(@output, pop(@opstack));
474 push(@opstack, $node);
479 push(@output, pop(@opstack));
487 return() unless(defined($self->{tagtree}));
488 return($self->{tagtree}->used_tags());
494 my $tail=$self->tail();
495 if($self->is($TYPE_TAG, $tail))
497 return($self->{db}->tag_has_values($tail->id()));
504 my $tail=$self->tail();
505 if($self->is($TYPE_TAG, $tail))
512 sub trailing_tag_parent
515 my $tail=$self->tail();
516 if($self->is($TYPE_TAG, $tail))
518 return($tail->{parents_id});
526 return($self->{elements}->[$#{$self->{elements}}]);
531 my($self, $type, $node)=@_;
532 return 0 unless($node);
533 return 0 unless($node->type());
534 return 1 if($type == $node->type());
538 # the one before last
542 return($self->{elements}->[($#{$self->{elements}}) - 1]);
545 ######################################################################
550 if(!$self->{tagtree}) # / or /NOT
552 my $sql="SELECT DISTINCT name FROM tags WHERE parents_id='';";
553 return($self->{db}->cmd_firstcol($sql));
555 my $sql="SELECT tags.name FROM ";
556 if($self->want_all_tags())
558 $sql .= "files_x_tags\n";
563 $self->tags_subselect() .
565 "INNER JOIN files_x_tags ON subselect.files_id=files_x_tags.files_id\n");
567 $sql .= "INNER JOIN tags ON files_x_tags.tags_id=tags.id\n";
569 my $id=$self->trailing_tag_id();
571 my $parentclause= "tags.parents_id='";
572 $parentclause .= $id if($self->expecting_values());
573 $parentclause .= "'";
574 push(@andclauses, $parentclause);
576 my @used=$self->used_tags();
579 push(@andclauses, "tags.id NOT IN (" . join(', ', @used) . ")");
583 $sql .= "WHERE " . join(' AND ', @andclauses) . "\n";
586 $sql .= "GROUP BY tags.name;";
587 print "SQL(TAGS): $sql\n" if($self->{verbose});
588 my @tagnames=$self->{db}->cmd_firstcol($sql);
589 print("SUBNAMES: ", join(', ', @tagnames), "\n") if($self->{verbose});
596 if(!@{$self->{elements}}) # /ALL
598 my $sql="SELECT DISTINCT name FROM artists WHERE name!='';";
599 return($self->{db}->cmd_firstcol($sql));
601 my $sql=$self->sql_start("artists.name");
602 $sql .= ("INNER JOIN artists ON files.artists_id=artists.id\n" .
603 "WHERE artists.name != ''\n" .
604 "GROUP BY artists.name;");
605 print "SQL(ARTISTS): $sql\n" if($self->{verbose});
606 my @tagnames=$self->{db}->cmd_firstcol($sql);
607 print("ARTISTS: ", join(', ', @tagnames), "\n") if($self->{verbose});
614 my $tail=$self->tail();
615 if($self->is($TYPE_ARTIST, $tail))
617 return $self->artist_albums($tail->id());
619 my $sql=$self->sql_start("albums.name");
620 $sql .= ("INNER JOIN albums ON files.albums_id=albums.id\n" .
621 "WHERE albums.name != ''\n" .
622 "GROUP BY albums.name;");
623 print "SQL(ALBUMS): \n$sql\n" if($self->{verbose});
624 my @names=$self->{db}->cmd_firstcol($sql);
625 print("ALBUMS: ", join(', ', @names), "\n") if($self->{verbose});
631 my($self, $artist_id)=@_;
632 my $sql=$self->sql_start("albums.name");
633 $sql .= ("INNER JOIN albums ON albums.id=files.albums_id\n" .
634 "INNER JOIN artists ON artists.id=files.artists_id\n" .
635 "WHERE artists.id=? and albums.name <> ''\n" .
636 "GROUP BY albums.name\n");
637 print "ARTIST_ALBUMS SQL: $sql\n" if($self->{verbose});
638 my @albums=$self->{db}->cmd_firstcol($sql, $artist_id);
639 print("ALBUMS: ", join(', ', @albums), "\n") if($self->{verbose});
645 my($self, $artist_id)=@_;
646 my $sql=$self->sql_start("files.name");
647 $sql .= ("INNER JOIN artists ON artists.id=files.artists_id\n" .
648 "INNER JOIN albums ON albums.id=files.albums_id\n" .
649 "WHERE artists.id=? AND albums.name=''\n" .
650 "GROUP BY files.name\n");
651 print "ARTIST_TRACKS SQL: $sql\n" if($self->{verbose});
652 my @names=$self->{db}->cmd_firstcol($sql, $artist_id);
653 print("ARTISTTRACKS: ", join(', ', @names), "\n") if($self->{verbose});
659 my($self, $artist_id, $album_id)=@_;
660 my $sql=("SELECT files.name FROM files\n" .
661 "INNER JOIN albums ON albums.id=files.albums_id\n" .
662 "INNER JOIN artists ON artists.id=files.artists_id\n" .
663 "WHERE artists.id=? AND albums.id=?\n" .
664 "GROUP BY files.name\n");
665 print "ALBUM_TRACKS SQL($artist_id, $album_id): $sql\n" if($self->{verbose});
666 my @names=$self->{db}->cmd_firstcol($sql, $artist_id, $album_id);
667 print("TRACKS: ", join(', ', @names), "\n") if($self->{verbose});
674 my $tail=$self->tail();
675 if($self->is($TYPE_ARTIST, $tail))
677 return $self->artist_tracks($tail->id());
679 elsif($self->is($TYPE_ALBUM, $tail))
682 my $artist=$self->tail_parent();
683 if($self->is($TYPE_ARTIST, $artist))
685 # should always happen
686 $artist_id=$artist->id();
688 return $self->album_tracks($artist_id, $tail->id());
690 my $sql=$self->sql_start("files.name");
691 $sql .= "INNER JOIN artists ON files.artists_id=artists.id\n";
692 if($self->{components}->[$#{$self->{components}}] eq $PATH_NOARTIST)
694 $sql .= "WHERE artists.name =''\n";
696 $sql .= "GROUP BY files.name;";
697 print "TRACKS SQL($self->{path}): $sql\n" if($self->{verbose});
698 my @names=$self->{db}->cmd_firstcol($sql);
699 print("TRACKS: ", join(', ', @names), "\n") if($self->{verbose});
705 my($self, $mountpoint)=@_;
706 my $tail=$self->tail();
707 if($self->is($TYPE_FILE, $tail))
710 my $sql=("SELECT paths.name, files.name FROM files\n" .
711 "INNER JOIN paths ON files.paths_id=paths.id\n" .
712 "WHERE files.id=?\n" .
713 "GROUP BY paths.name, files.name");
714 print "FILENAME SQL: $sql\n" if($self->{verbose});
715 my ($path, $name)=$self->{db}->cmd_onerow($sql, $id);
716 return($self->{db}->relativise($path, $name, $mountpoint, $self->{path}));
718 # should never happen
725 my $tree=$self->{tagtree};
726 my ($sqlclause, @joins)=$tree->to_sql() if($tree);
727 my $sql="\tSELECT fxt1.files_id FROM tags t1";
730 @joins=@{$self->{joins}};
731 for(my $i=0; $i <= $#joins; $i++)
735 my $inner=("\t$join JOIN files_x_tags fxt$cnt ON " .
736 "t${cnt}.id=fxt${cnt}.tags_id");
739 push(@crosses, "CROSS JOIN tags t$cnt");
740 $inner .= " AND fxt1.files_id=fxt${cnt}.files_id";
742 push(@inners, $inner);
744 $sql .= ("\n\t" . join(" ", @crosses)) if(@crosses);
745 $sql .= ("\n" . join("\n", @inners)) if(@inners);
746 $sql .= "\n\tWHERE $sqlclause" if($sqlclause);
747 $sql .= "\n\tGROUP BY fxt1.files_id\n";
753 my($self, $tables)=@_;
754 my $sql="SELECT $tables FROM ";
762 $self->tags_subselect() .
764 "INNER JOIN files ON subselect.files_id=files.id\n");
769 # we just filter $ALLTRACKS, $NOARTIST and $NOALBUM
770 # filtering tags properly requires up to four levels of recursion
771 # (tag/tagval/AND/NOT) and is too slow
775 return(@dirs) unless($ENABLE_FILTER);
776 my $base=$self->{path};
780 push(@outdirs, $dir) unless($self->empty("$base/$dir"));
788 my $path=ID3FS::Path->new($self->{db}, $dir, $self->{verbose},
789 ($self->{maxtagdepth} - $self->{curtagdepth}));
790 return 1 unless($path->isvalid());
791 my($subdirs,$subfiles)=$path->dirents();
792 return 0 if(@$subfiles || @$subdirs);
796 # if path is .../OR/ or .../OR/NOT or .../AND/NOT
800 my $tail=$self->tail();
801 return 0 unless($tail);
802 my $parent=$self->tail_parent();
803 my $parent_valid = ($parent && $parent->type() == $TYPE_BOOL);
804 if($tail->type() == $TYPE_BOOL)
806 return 1 if($tail->name() eq "OR");
807 return 0 unless($tail->name() eq "NOT");
808 return 0 unless($parent_valid);
809 return 1 if($parent->name() eq "OR");
810 return 1 if($parent->name() eq "AND");
812 elsif($tail->type() == $TYPE_TAG)
814 return 0 unless($parent_valid);
815 return 1 if($parent->name() eq "NOT");