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
51 # print "STATE: ", $self->state(), "\n";
58 if(($self->state() == $STATE_FILE) ||
59 ($self->state() == $STATE_INVALID))
69 return($self->state() == $STATE_FILE);
75 return($self->state() != $STATE_INVALID);
80 my($self, $mountpoint)=@_;
81 if($self->state() == $STATE_FILE)
83 return $self->filename($mountpoint);
85 return "ERROR"; #should never happen?
93 my $state=$self->state();
94 # print "DIRENTS: STATE: $state\n";
95 # print "DIRENTS: FILE: $self->{path}\n";
96 if($state==$STATE_ALL)
98 @dents=($self->filter($PATH_ALLTRACKS, $PATH_NOARTIST), $self->artists());
100 elsif($state==$STATE_TAG)
102 my $tail=$self->tail();
103 if($self->is($TYPE_TAG, $tail) && $self->{db}->tag_has_values($tail->id()))
105 @dents=$self->tags();
109 if($self->{maxtagdepth} && ($self->{curtagdepth} < $self->{maxtagdepth}))
113 push(@dents, $self->filter($PATH_ALLTRACKS, $PATH_NOARTIST), $self->artists());
116 elsif($state==$STATE_BOOLEAN)
118 my $parent=$self->tail();
119 unless($self->is($TYPE_BOOL, $parent) && $parent->{name} eq "NOT")
123 push(@dents,$self->tags());
125 elsif($state==$STATE_ROOT)
127 @dents=(qw(ALL NOT), $self->tags());
129 elsif($state==$STATE_ALBUMS)
131 @dents=$self->filter($PATH_ALLTRACKS, $PATH_NOALBUM, $self->albums());
133 elsif($state==$STATE_TRACKLIST)
135 @fents=$self->tracks();
139 print "id3fsd: INTERNAL ERROR: DIRENTS: UNHANDLED STATE: $state\n";
141 return(\@dents, \@fents);
144 # State Machine Of Doom
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}};
155 $self->{elements}=[];
159 while(defined(my $name=shift @parts))
161 # print "NAME: $name\n";
162 my $state=$self->state();
163 if($state==$STATE_INVALID)
165 # print "SM: INVALID: $name\n";
168 elsif($state==$STATE_ROOT)
170 # print "SM: ROOT: $name\n";
174 $self->state($STATE_ALL);
176 elsif($name eq "NOT")
179 push(@{$self->{elements}}, ID3FS::Path::Node->new($self->{db}, $TYPE_BOOL, $name));
180 $self->state($STATE_BOOLEAN);
184 $tag=ID3FS::Path::Node->new($self->{db}, $TYPE_TAG, $name);
187 push(@{$self->{elements}}, $tag);
189 $self->state($STATE_TAG);
193 $self->state($STATE_INVALID);
197 elsif($state==$STATE_TAG)
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()))
203 # print "Parsing: parent: $tag->id()\n";
204 my $tagval=ID3FS::Path::Node->new($self->{db}, $TYPE_TAG, $name, $tag->id());
208 push(@{$self->{elements}}, $tagval);
212 $self->state($STATE_INVALID);
215 elsif($name eq $PATH_ALLTRACKS)
217 $self->state($STATE_TRACKLIST);
219 elsif($name eq $PATH_NOARTIST)
221 $self->state($STATE_TRACKLIST);
223 elsif($name eq "AND")
225 $self->state($STATE_BOOLEAN);
226 push(@{$self->{elements}}, ID3FS::Path::Node->new($self->{db}, $TYPE_BOOL, $name));
230 $self->state($STATE_BOOLEAN);
231 push(@{$self->{elements}}, ID3FS::Path::Node->new($self->{db}, $TYPE_BOOL, $name));
235 my $artist=ID3FS::Path::Node->new($self->{db}, $TYPE_ARTIST, $name);
238 push(@{$self->{elements}}, $artist);
239 $self->state($STATE_ALBUMS);
243 $self->state($STATE_INVALID);
247 elsif($state==$STATE_BOOLEAN)
249 # print "SM: BOOLEAN: $name\n";
250 my $parent=$self->tail();
252 if($self->is($TYPE_BOOL, $parent) &&
253 $parent->{name} eq "NOT")
257 if($allownot && $name eq "NOT")
259 $self->state($STATE_BOOLEAN);
260 push(@{$self->{elements}}, ID3FS::Path::Node->new($self->{db}, $TYPE_BOOL, $name));
264 my $tag=ID3FS::Path::Node->new($self->{db}, $TYPE_TAG, $name);
267 push(@{$self->{elements}}, $tag);
269 $self->state($STATE_TAG);
273 $self->state($STATE_INVALID);
277 elsif($state==$STATE_ALBUMS)
279 # print "SM: ALBUM: $name\n";
280 if($name eq $PATH_ALLTRACKS)
282 $self->state($STATE_TRACKLIST);
284 elsif($name eq $PATH_NOALBUM)
286 $self->state($STATE_TRACKLIST);
290 my $album=ID3FS::Path::Node->new($self->{db}, $TYPE_ALBUM, $name);
293 push(@{$self->{elements}}, $album);
294 $self->state($STATE_TRACKLIST);
298 $self->state($STATE_INVALID);
302 elsif($state==$STATE_TRACKLIST)
304 # print "SM: TRACKLIST: $name\n";
305 my $track=ID3FS::Path::Node->new($self->{db}, $TYPE_FILE, $name);
308 push(@{$self->{elements}}, $track);
309 $self->state($STATE_FILE);
313 $self->state($STATE_INVALID);
316 elsif($state==$STATE_FILE)
318 # print "SM: FILE: $name\n";
319 # Can't have anything after a filename
320 $self->state($STATE_INVALID);
322 elsif($state==$STATE_ALL)
324 if($name eq $PATH_ALLTRACKS)
326 $self->state($STATE_TRACKLIST);
328 elsif($name eq $PATH_NOARTIST)
330 $self->state($STATE_TRACKLIST);
334 my $artist=ID3FS::Path::Node->new($self->{db}, $TYPE_ARTIST, $name);
337 push(@{$self->{elements}}, $artist);
338 $self->state($STATE_ALBUMS);
342 $self->state($STATE_INVALID);
348 print "SM: ERROR: UNKNOWN STATE: $self->{state}\n";
349 $self->state($STATE_INVALID);
353 my @elements=@{$self->{elements}};
354 # remove trailing boolean
355 while(@elements && $self->is($TYPE_BOOL, $elements[$#elements]))
359 # print "\nELEMENTS: ", join(' ', map { $_->name(); } @elements), "\n";
360 my @joins=$self->number_joins(@elements);
361 @joins=qw(INNER) unless(@joins);
362 $self->{joins}=\@joins;
363 # print "AFTER: ", join(' ', map { $_->name() . "(" . $_->{table} . ")"; } @elements), "\n";
364 # print "JOINS: ", join(', ', @joins), "\n";
365 # sort elements by precedence
366 @elements=$self->sort_elements(@elements);
367 $self->{tagtree}=$self->elements_to_tree(\@elements);
372 my($self, @elements)=@_;
377 return (@joins) unless(@elements);
378 while(my $thing=shift @elements)
380 if($thing->type() == $TYPE_BOOL)
382 my $op=$thing->name();
393 # NOT/foo/OR needs an extra join
394 $nextjoin="INNER" if($lastop && $lastop eq "NOT");
402 # if tag has a value, eat the tag, shifting to the value
403 $thing=shift(@elements) if($elements[0]->type() == $TYPE_TAG);
405 elsif($self->{db}->tag_has_values($thing->id()))
407 # if the expression ends in a tag that has a value
408 # (ie we have the tag and want the value)
409 # use an INNER join even if we were in a NOT
410 $nextjoin="INNER" if($nextjoin);
415 push(@joins, $nextjoin);
418 $thing->table($table);
426 my($self, $newstate)=@_;
427 if(defined($newstate))
429 $self->{state}=$newstate;
430 $self->{curtagdepth}++ if($newstate == $STATE_TAG);
432 return $self->{state};
435 # link up precedence-sorted list into a binary tree
438 my($self, $elements)=@_;
439 return undef unless(@$elements);
440 my ($left, $right, $op)=(undef, undef, undef);
441 my $thing=pop @$elements;
442 if($self->is($TYPE_BOOL, $thing))
444 $right=$self->elements_to_tree($elements);
445 if($thing->{name} ne "NOT")
447 $left=$self->elements_to_tree($elements);
450 $thing->right($right);
455 # Dijkstra's shunting-yard algorithm
458 my ($self, @input)=@_;
461 while(my $thing = shift @input)
463 if($self->is($TYPE_TAG, $thing))
465 # Handle tag values by dropping parent
466 if(@input && $self->is($TYPE_TAG, $input[0]))
470 push(@output, $thing);
472 elsif($self->is($TYPE_BOOL, $thing))
475 ($priorities{$thing->{name}} <= $priorities{$opstack[$#opstack]->{name}}))
477 push(@output, pop(@opstack));
479 push(@opstack, $thing);
484 push(@output, pop(@opstack));
492 return() unless(defined($self->{tagtree}));
493 return($self->{tagtree}->used_tags());
499 my $tail=$self->tail();
500 if($self->is($TYPE_TAG, $tail))
502 return($self->{db}->tag_has_values($tail->id()));
509 my $tail=$self->tail();
510 if($self->is($TYPE_TAG, $tail))
517 sub trailing_tag_parent
520 my $tail=$self->tail();
521 if($self->is($TYPE_TAG, $tail))
523 return($tail->{parents_id});
531 return($self->{elements}->[$#{$self->{elements}}]);
536 my($self, $type, $thing)=@_;
537 return 0 unless($thing);
538 return 0 unless($thing->type());
539 return 1 if($type == $thing->type());
543 # the one before last
547 return($self->{elements}->[($#{$self->{elements}}) - 1]);
550 ######################################################################
555 if(!$self->{tagtree}) # / or /NOT
557 my $sql="SELECT DISTINCT name FROM tags WHERE parents_id='';";
558 return($self->{db}->cmd_firstcol($sql));
560 my $hasvals=$self->expecting_values();
561 my $sql="SELECT tags.name FROM ";
562 if($self->want_all_tags())
564 $sql .= "files_x_tags\n";
569 $self->tags_subselect() .
571 "INNER JOIN files_x_tags ON subselect.files_id=files_x_tags.files_id\n");
573 $sql .= "INNER JOIN tags ON files_x_tags.tags_id=tags.id\n";
575 my $id=$self->trailing_tag_id();
577 my $parentclause= "tags.parents_id='";
578 $parentclause .= $id if($hasvals);
579 $parentclause .= "'";
580 push(@andclauses, $parentclause);
582 my @used=$self->used_tags();
585 push(@andclauses, "tags.id NOT IN (" . join(', ', @used) . ")");
589 $sql .= "WHERE " . join(' AND ', @andclauses) . "\n";
592 $sql .= "GROUP BY tags.name;";
593 print "SQL(TAGS): $sql\n" if($self->{verbose});
594 my @tagnames=$self->{db}->cmd_firstcol($sql);
595 print("SUBNAMES: ", join(', ', @tagnames), "\n") if($self->{verbose});
602 if(!@{$self->{elements}}) # /ALL
604 my $sql="SELECT DISTINCT name FROM artists WHERE name!='';";
605 return($self->{db}->cmd_firstcol($sql));
607 my $sql=$self->sql_start("artists.name");
608 $sql .= ("INNER JOIN artists ON files.artists_id=artists.id\n" .
609 "WHERE artists.name != ''\n" .
610 "GROUP BY artists.name;");
611 print "SQL(ARTISTS): $sql\n" if($self->{verbose});
612 my @tagnames=$self->{db}->cmd_firstcol($sql);
613 print("ARTISTS: ", join(', ', @tagnames), "\n") if($self->{verbose});
620 my $tail=$self->tail();
621 if($self->is($TYPE_ARTIST, $tail))
623 return $self->artist_albums($tail->id());
625 my $sql=$self->sql_start("albums.name");
626 $sql .= ("INNER JOIN albums ON files.albums_id=albums.id\n" .
627 "WHERE albums.name != ''\n" .
628 "GROUP BY albums.name;");
629 print "SQL(ALBUMS): \n$sql\n" if($self->{verbose});
630 my @names=$self->{db}->cmd_firstcol($sql);
631 print("ALBUMS: ", join(', ', @names), "\n") if($self->{verbose});
637 my($self, $artist_id)=@_;
638 my $sql=$self->sql_start("albums.name");
639 $sql .= ("INNER JOIN albums ON albums.id=files.albums_id\n" .
640 "INNER JOIN artists ON artists.id=files.artists_id\n" .
641 "WHERE artists.id=? and albums.name <> ''\n" .
642 "GROUP BY albums.name\n");
643 print "ARTIST_ALBUMS SQL: $sql\n" if($self->{verbose});
644 my @albums=$self->{db}->cmd_firstcol($sql, $artist_id);
645 print("ALBUMS: ", join(', ', @albums), "\n") if($self->{verbose});
651 my($self, $artist_id)=@_;
652 my $sql=$self->sql_start("files.name");
653 $sql .= ("INNER JOIN artists ON artists.id=files.artists_id\n" .
654 "INNER JOIN albums ON albums.id=files.albums_id\n" .
655 "WHERE artists.id=? AND albums.name=''\n" .
656 "GROUP BY files.name\n");
657 print "ARTIST_TRACKS SQL: $sql\n" if($self->{verbose});
658 my @names=$self->{db}->cmd_firstcol($sql, $artist_id);
659 print("ARTISTTRACKS: ", join(', ', @names), "\n") if($self->{verbose});
665 my($self, $artist_id, $album_id)=@_;
666 my $sql=("SELECT files.name FROM files\n" .
667 "INNER JOIN albums ON albums.id=files.albums_id\n" .
668 "INNER JOIN artists ON artists.id=files.artists_id\n" .
669 "WHERE artists.id=? AND albums.id=?\n" .
670 "GROUP BY files.name\n");
671 print "ALBUM_TRACKS SQL($artist_id, $album_id): $sql\n" if($self->{verbose});
672 my @names=$self->{db}->cmd_firstcol($sql, $artist_id, $album_id);
673 print("TRACKS: ", join(', ', @names), "\n") if($self->{verbose});
680 my $tail=$self->tail();
681 if($self->is($TYPE_ARTIST, $tail))
683 return $self->artist_tracks($tail->id());
685 elsif($self->is($TYPE_ALBUM, $tail))
688 my $artist=$self->tail_parent();
689 if($self->is($TYPE_ARTIST, $artist))
691 # should always happen
692 $artist_id=$artist->id();
694 return $self->album_tracks($artist_id, $tail->id());
696 my $sql=$self->sql_start("files.name");
697 $sql .= "INNER JOIN artists ON files.artists_id=artists.id\n";
698 if($self->{components}->[$#{$self->{components}}] eq $PATH_NOARTIST)
700 $sql .= "WHERE artists.name =''\n";
702 $sql .= "GROUP BY files.name;";
703 print "TRACKS SQL($self->{path}): $sql\n" if($self->{verbose});
704 my @names=$self->{db}->cmd_firstcol($sql);
705 print("TRACKS: ", join(', ', @names), "\n") if($self->{verbose});
711 my($self, $mountpoint)=@_;
712 my $tail=$self->tail();
713 if($self->is($TYPE_FILE, $tail))
716 my $sql=("SELECT paths.name, files.name FROM files\n" .
717 "INNER JOIN paths ON files.paths_id=paths.id\n" .
718 "WHERE files.id=?\n" .
719 "GROUP BY paths.name, files.name");
720 print "FILENAME SQL: $sql\n" if($self->{verbose});
721 my ($path, $name)=$self->{db}->cmd_onerow($sql, $id);
722 return($self->{db}->relativise($path, $name, $mountpoint, $self->{path}));
724 # should never happen
731 my $hasvals=$self->expecting_values();
732 my $tree=$self->{tagtree};
734 # print Dumper $tree;
735 my ($sqlclause, @joins)=$tree->to_sql($hasvals) if($tree);
736 my $sql="\tSELECT fxt1.files_id FROM tags t1";
739 @joins=@{$self->{joins}};
740 for(my $i=0; $i <= $#joins; $i++)
744 my $inner=("\t$join JOIN files_x_tags fxt$cnt ON " .
745 "t${cnt}.id=fxt${cnt}.tags_id");
748 push(@crosses, "CROSS JOIN tags t$cnt");
749 $inner .= " AND fxt1.files_id=fxt${cnt}.files_id";
751 push(@inners, $inner);
753 $sql .= ("\n\t" . join(" ", @crosses)) if(@crosses);
754 $sql .= ("\n" . join("\n", @inners)) if(@inners);
755 $sql .= "\n\tWHERE $sqlclause" if($sqlclause);
756 $sql .= "\n\tGROUP BY fxt1.files_id\n";
762 my($self, $tables)=@_;
763 my $sql="SELECT $tables FROM ";
771 $self->tags_subselect() .
773 "INNER JOIN files ON subselect.files_id=files.id\n");
778 # we just filter $ALLTRACKS, $NOARTIST and $NOALBUM
779 # filtering tags properly requires up to four levels of recursion
780 # (tag/tagval/AND/NOT) and is too slow
784 return(@dirs) unless($ENABLE_FILTER);
785 my $base=$self->{path};
789 push(@outdirs, $dir) unless($self->empty("$base/$dir"));
797 my $path=ID3FS::Path->new($self->{db}, $dir, $self->{verbose},
798 ($self->{maxtagdepth} - $self->{curtagdepth}));
799 return 1 unless($path->isvalid());
800 my($subdirs,$subfiles)=$path->dirents();
801 return 0 if(@$subfiles || @$subdirs);
805 # if path is .../OR/ or .../OR/NOT or .../AND/NOT
809 my $tail=$self->tail();
810 return 0 unless($tail);
811 my $parent=$self->tail_parent();
812 my $parent_valid = ($parent && $parent->type() == $TYPE_BOOL);
813 if($tail->type() == $TYPE_BOOL)
815 return 1 if($tail->name() eq "OR");
816 return 0 unless($tail->name() eq "NOT");
817 return 0 unless($parent_valid);
818 return 1 if($parent->name() eq "OR");
819 return 1 if($parent->name() eq "AND");
821 elsif($tail->type() == $TYPE_TAG)
823 return 0 unless($parent_valid);
824 return 1 if($parent->name() eq "NOT");