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_TAGVAL,
24 $STATE_BOOLEAN, $STATE_ALBUMS, $STATE_TRACKLIST,
25 $STATE_FILE, $STATE_ALL)=(0..8);
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 = 1;
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 || $state==$STATE_TAGVAL)
102 if($state==$STATE_TAG && $self->at($TYPE_TAG) &&
103 $self->{db}->tag_has_values($self->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) &&
120 $parent->{name} eq "NOT")
124 push(@dents,$self->tags());
126 elsif($state==$STATE_ROOT)
128 @dents=(qw(ALL NOT), $self->tags());
130 elsif($state==$STATE_ALBUMS)
132 @dents=$self->filter($PATH_ALLTRACKS, $PATH_NOALBUM, $self->albums());
134 elsif($state==$STATE_TRACKLIST)
136 @fents=$self->tracks();
140 print "DIRENTS: UNHANDLED STATE: $state\n";
142 return(\@dents, \@fents);
145 # State Machine Of Doom
149 @{$self->{components}}=split(/\//, $self->{path});
150 shift @{$self->{components}}; # drop empty field before leading /
151 # print "PATH: $self->{path}\n";
152 $self->state($STATE_ROOT);
153 return if($self->{path} eq "/");
154 my @parts=@{$self->{components}};
156 $self->{elements}=[];
160 while(defined(my $name=shift @parts))
162 # print "NAME: $name\n";
163 my $state=$self->state();
164 if($state==$STATE_INVALID)
166 # print "SM: INVALID: $name\n";
169 elsif($state==$STATE_ROOT)
171 # print "SM: ROOT: $name\n";
175 $self->state($STATE_ALL);
177 elsif($name eq "NOT")
180 push(@{$self->{elements}}, ID3FS::Path::Node->new($self->{db}, $TYPE_BOOL, $name));
181 $self->state($STATE_BOOLEAN);
185 $tag=ID3FS::Path::Node->new($self->{db}, $TYPE_TAG, $name);
188 push(@{$self->{elements}}, $tag);
190 $self->state($STATE_TAG);
194 $self->state($STATE_INVALID);
198 elsif($state==$STATE_TAG || $state==$STATE_TAGVAL)
200 my $tag=$self->tail();
201 # print "SM: TAG/TAGVAL($state): $name\n";
202 if($state==$STATE_TAG && $self->is($TYPE_TAG, $tag) &&
203 $self->{db}->tag_has_values($tag->id()))
205 # print "Parsing: parent: $tag->id()\n";
206 my $tagval=ID3FS::Path::Node->new($self->{db}, $TYPE_TAG, $name, $tag->id());
209 $self->state($STATE_TAGVAL);
211 push(@{$self->{elements}}, $tagval);
215 $self->state($STATE_INVALID);
218 elsif($name eq $PATH_ALLTRACKS)
220 $self->state($STATE_TRACKLIST);
222 elsif($name eq $PATH_NOARTIST)
224 $self->state($STATE_TRACKLIST);
226 elsif($name eq "AND")
228 $self->state($STATE_BOOLEAN);
229 push(@{$self->{elements}}, ID3FS::Path::Node->new($self->{db}, $TYPE_BOOL, $name));
233 $self->state($STATE_BOOLEAN);
234 push(@{$self->{elements}}, ID3FS::Path::Node->new($self->{db}, $TYPE_BOOL, $name));
238 my $artist=ID3FS::Path::Node->new($self->{db}, $TYPE_ARTIST, $name);
241 push(@{$self->{elements}}, $artist);
242 $self->state($STATE_ALBUMS);
246 $self->state($STATE_INVALID);
250 elsif($state==$STATE_BOOLEAN)
252 # print "SM: BOOLEAN: $name\n";
253 my $parent=$self->tail();
255 if($self->is($TYPE_BOOL, $parent) &&
256 $parent->{name} eq "NOT")
260 if($allownot && $name eq "NOT")
262 $self->state($STATE_BOOLEAN);
263 push(@{$self->{elements}}, ID3FS::Path::Node->new($self->{db}, $TYPE_BOOL, $name));
267 my $tag=ID3FS::Path::Node->new($self->{db}, $TYPE_TAG, $name);
270 push(@{$self->{elements}}, $tag);
272 $self->state($STATE_TAG);
276 $self->state($STATE_INVALID);
280 elsif($state==$STATE_ALBUMS)
282 # print "SM: ALBUM: $name\n";
283 if($name eq $PATH_ALLTRACKS)
285 $self->state($STATE_TRACKLIST);
287 elsif($name eq $PATH_NOALBUM)
289 $self->state($STATE_TRACKLIST);
293 my $album=ID3FS::Path::Node->new($self->{db}, $TYPE_ALBUM, $name);
296 push(@{$self->{elements}}, $album);
297 $self->state($STATE_TRACKLIST);
301 $self->state($STATE_INVALID);
305 elsif($state==$STATE_TRACKLIST)
307 # print "SM: TRACKLIST: $name\n";
308 my $track=ID3FS::Path::Node->new($self->{db}, $TYPE_FILE, $name);
311 push(@{$self->{elements}}, $track);
312 $self->state($STATE_FILE);
316 $self->state($STATE_INVALID);
319 elsif($state==$STATE_FILE)
321 # print "SM: FILE: $name\n";
322 # Can't have anything after a filename
323 $self->state($STATE_INVALID);
325 elsif($state==$STATE_ALL)
327 if($name eq $PATH_ALLTRACKS)
329 $self->state($STATE_TRACKLIST);
331 elsif($name eq $PATH_NOARTIST)
333 $self->state($STATE_TRACKLIST);
337 my $artist=ID3FS::Path::Node->new($self->{db}, $TYPE_ARTIST, $name);
340 push(@{$self->{elements}}, $artist);
341 $self->state($STATE_ALBUMS);
345 $self->state($STATE_INVALID);
351 print "SM: ERROR: UNKNOWN STATE: $self->{state}\n";
352 $self->state($STATE_INVALID);
356 # remove trailing boolean
357 my @elements=@{$self->{elements}};
358 while(@elements && $self->is($TYPE_BOOL, $elements[$#elements]))
362 # sort elements by precedence
363 @elements=$self->sort_elements(@elements);
364 $self->{tagtree}=$self->elements_to_tree(\@elements);
369 # print Dumper $self->{tagtree};
370 # my ($conditions, @joins)=$self->{tagtree}->to_sql();
371 # print "CONDITIONS(", scalar(@joins), "): ", $conditions, "\n";
377 my($self, $newstate)=@_;
378 if(defined($newstate))
380 $self->{state}=$newstate;
381 $self->{curtagdepth}++ if($newstate == $STATE_TAG);
383 return $self->{state};
388 my($self, $elements)=@_;
389 return undef unless(@$elements);
390 my ($left, $right, $op)=(undef, undef, undef);
391 my $thing=pop @$elements;
392 if($self->is($TYPE_BOOL, $thing))
394 $right=$self->elements_to_tree($elements);
395 if($thing->{name} ne "NOT")
397 $left=$self->elements_to_tree($elements);
400 $thing->right($right);
405 # Dijkstra's shunting-yard algorithm
408 my ($self, @input)=@_;
411 # print "INPUT: ", join(', ', map { $_->{name}; } @input), "\n";
412 while(my $thing = shift @input)
414 if($self->is($TYPE_TAG, $thing))
416 # Handle tag values by dropping parent
417 if(@input && $self->is($TYPE_TAG, $input[0]))
421 push(@output, $thing);
423 elsif($self->is($TYPE_BOOL, $thing))
427 ($priorities{$thing->{name}} <= $priorities{$opstack[$#opstack]->{name}}))
429 push(@output, pop(@opstack));
431 push(@opstack, $thing);
436 push(@output, pop(@opstack));
438 # print "STACK: ", join(', ', map { $_->{name}; } @output), "\n";
445 return() unless(defined($self->{tagtree}));
446 return($self->{tagtree}->used_tags());
452 my $tail=$self->tail();
453 if($self->is($TYPE_TAG, $tail))
455 return($self->{db}->tag_has_values($tail->id()));
462 my $tail=$self->tail();
463 if($self->is($TYPE_TAG, $tail))
470 sub trailing_tag_parent
473 my $tail=$self->tail();
474 if($self->is($TYPE_TAG, $tail))
476 return($tail->{parents_id});
484 return($self->{elements}->[$#{$self->{elements}}]);
490 return($self->is($type, $self->tail()));
495 my($self, $type, $thing)=@_;
496 return 0 unless($thing);
497 return 0 unless($thing->type());
498 return 1 if($type == $thing->type());
502 # the one before last
506 return($self->{elements}->[($#{$self->{elements}}) - 1]);
509 ######################################################################
514 if(!$self->{tagtree}) # / or /NOT
516 my $sql="SELECT DISTINCT name FROM tags WHERE parents_id='';";
517 return($self->{db}->cmd_firstcol($sql));
519 my $hasvals=$self->expecting_values();
520 my $parent=$self->trailing_tag_parent();
521 # print "THASVALS: $hasvals\n";
522 # print "TPARENT: ", (defined($parent)? $parent : "NO"), "\n";
524 my $sql="SELECT tags.name FROM ";
527 $sql .= "files_x_tags\n";
532 $self->tags_subselect() .
534 "INNER JOIN files_x_tags ON subselect.files_id=files_x_tags.files_id\n");
536 $sql .= "INNER JOIN tags ON files_x_tags.tags_id=tags.id\n";
538 my $id=$self->trailing_tag_id();
540 my $parentclause= "tags.parents_id='";
543 $parentclause .= $id;
547 $parentclause .= $parent;
549 $parentclause .= "'";
550 push(@andclauses, $parentclause);
552 my @used=$self->used_tags();
555 push(@andclauses, "tags.id NOT IN (" . join(', ', @used) . ")");
559 $sql .= "WHERE " . join(' AND ', @andclauses) . "\n";
562 $sql .= "GROUP BY tags.name;";
563 print "SQL(TAGS): $sql\n" if($self->{verbose});
564 my @tagnames=$self->{db}->cmd_firstcol($sql);
565 print("SUBNAMES: ", join(', ', @tagnames), "\n") if($self->{verbose});
572 if(!@{$self->{elements}}) # /ALL
574 my $sql="SELECT DISTINCT name FROM artists WHERE name!='';";
575 return($self->{db}->cmd_firstcol($sql));
578 my $sql=$self->sql_start("artists.name");
579 $sql .= ("INNER JOIN artists ON files.artists_id=artists.id\n" .
580 "WHERE artists.name != ''\n" .
581 "GROUP BY artists.name;");
582 print "SQL(ARTISTS): $sql\n" if($self->{verbose});
583 my @tagnames=$self->{db}->cmd_firstcol($sql);
584 print("ARTISTS: ", join(', ', @tagnames), "\n") if($self->{verbose});
592 my $tail=$self->tail();
593 if($self->is($TYPE_ARTIST, $tail))
595 return $self->artist_albums($tail->id());
597 my $sql=$self->sql_start("albums.name");
598 $sql .= ("INNER JOIN albums ON files.albums_id=albums.id\n" .
599 "WHERE albums.name != ''\n" .
600 "GROUP BY albums.name;");
601 print "SQL(ALBUMS): \n$sql\n" if($self->{verbose});
602 my @names=$self->{db}->cmd_firstcol($sql);
603 print("ALBUMS: ", join(', ', @names), "\n") if($self->{verbose});
609 my($self, $artist_id)=@_;
610 my $sql=$self->sql_start("albums.name");
611 $sql .= ("INNER JOIN albums ON albums.id=files.albums_id\n" .
612 "INNER JOIN artists ON artists.id=files.artists_id\n" .
613 "WHERE artists.id=? and albums.name <> ''\n" .
614 "GROUP BY albums.name\n");
615 print "ARTIST_ALBUMS SQL: $sql\n" if($self->{verbose});
616 my @albums=$self->{db}->cmd_firstcol($sql, $artist_id);
617 print("ALBUMS: ", join(', ', @albums), "\n") if($self->{verbose});
623 my($self, $artist_id)=@_;
624 my $sql=$self->sql_start("files.name");
625 $sql .= ("INNER JOIN artists ON artists.id=files.artists_id\n" .
626 "INNER JOIN albums ON albums.id=files.albums_id\n" .
627 "WHERE artists.id=? AND albums.name=''\n" .
628 "GROUP BY files.name\n");
629 print "ARTIST_TRACKS SQL: $sql\n" if($self->{verbose});
630 my @names=$self->{db}->cmd_firstcol($sql, $artist_id);
631 print("ARTISTTRACKS: ", join(', ', @names), "\n") if($self->{verbose});
637 my($self, $artist_id, $album_id)=@_;
638 my $sql=("SELECT files.name FROM files\n" .
639 "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.id=?\n" .
642 "GROUP BY files.name\n");
643 print "ALBUM_TRACKS SQL($artist_id, $album_id): $sql\n" if($self->{verbose});
644 my @names=$self->{db}->cmd_firstcol($sql, $artist_id, $album_id);
645 print("TRACKS: ", join(', ', @names), "\n") if($self->{verbose});
652 my $tail=$self->tail();
653 if($self->is($TYPE_ARTIST, $tail))
655 return $self->artist_tracks($tail->id());
657 elsif($self->is($TYPE_ALBUM, $tail))
660 my $artist=$self->tail_parent();
661 if($self->is($TYPE_ARTIST, $artist))
663 # should always happen
664 $artist_id=$artist->id();
666 return $self->album_tracks($artist_id, $tail->id());
668 my $sql=$self->sql_start("files.name");
669 $sql .= "INNER JOIN artists ON files.artists_id=artists.id\n";
670 if($self->{components}->[$#{$self->{components}}] eq $PATH_NOARTIST)
672 $sql .= "WHERE artists.name =''\n";
674 $sql .= "GROUP BY files.name;";
675 print "TRACKS SQL($self->{path}): $sql\n" if($self->{verbose});
676 my @names=$self->{db}->cmd_firstcol($sql);
677 print("TRACKS: ", join(', ', @names), "\n") if($self->{verbose});
683 my($self, $mountpoint)=@_;
684 my $tail=$self->tail();
685 if($self->is($TYPE_FILE, $tail))
688 my $sql=("SELECT paths.name, files.name FROM files\n" .
689 "INNER JOIN paths ON files.paths_id=paths.id\n" .
690 "WHERE files.id=?\n" .
691 "GROUP BY paths.name, files.name");
692 print "FILENAME SQL: $sql\n" if($self->{verbose});
693 my ($path, $name)=$self->{db}->cmd_onerow($sql, $id);
694 my $id3fs_path=join('/', map { $_->{name}; } @{$self->{elements}});
695 return($self->{db}->relativise($path, $name, $mountpoint, $self->{path}));
697 # should never happen
704 my $hasvals=$self->expecting_values();
705 my $tree=$self->{tagtree};
706 my $parent=$self->trailing_tag_parent();
711 $tag=$self->trailing_tag_id();
712 # print "Trailing id: $tag\n";
714 my ($sqlclause, @joins)=(undef, ());
715 ($sqlclause, @joins) = $tree->to_sql($hasvals) if($tree);
716 # print "SQL(" . scalar(@joins) ."): $sqlclause\n";
717 my $sql="\tSELECT fxt1.files_id FROM tags t1";
720 # $joinsneeded++ if($tag);
721 for(my $i=0; $i <= $#joins; $i++)
725 my $inner=("\t$join JOIN files_x_tags fxt$cnt ON " .
726 "t${cnt}.id=fxt${cnt}.tags_id");
729 push(@crosses, "CROSS JOIN tags t$cnt");
730 $inner .= " AND fxt1.files_id=fxt${cnt}.files_id";
732 push(@inners, $inner);
734 $sql .= ("\n\t" . join(" ", @crosses)) if(@crosses);
735 $sql .= ("\n" . join("\n", @inners)) if(@inners);
736 $sql .= "\n\tWHERE $sqlclause" if($sqlclause);
739 # $sql .= " AND t${joinsneeded}.parents_id='$tag'";
741 $sql .= "\n\tGROUP BY fxt1.files_id\n";
747 my($self, $tables)=@_;
748 my $sql="SELECT $tables FROM ";
756 $self->tags_subselect() .
758 "INNER JOIN files ON subselect.files_id=files.id\n");
763 # we just filter $ALLTRACKS, $NOARTIST and $NOALBUM
764 # filtering tags properly requires up to four levels of recursion
765 # (tag/tagval/AND/NOT) and is too slow
769 return(@dirs) unless($ENABLE_FILTER);
770 my $base=$self->{path};
774 # print "\nFILTER (",$self->state(), "): $base / $dir\n";
775 if($self->empty("$base/$dir"))
777 # print "empty: $base / $dir\n";
781 # print "non-empty, accepting: $base / $dir\n";
782 push(@outdirs, $dir);
791 my $path=ID3FS::Path->new($self->{db}, $dir, $self->{verbose},
792 ($self->{maxtagdepth} - $self->{curtagdepth}));
793 return 1 unless($path->isvalid());
794 my($subdirs,$subfiles)=$path->dirents();
795 return 0 if(@$subfiles || @$subdirs);
799 # if path is .../OR/ or .../OR/NOT
803 my $tail=$self->tail();
804 return 0 unless($tail);
805 return 0 unless($tail->type() == $TYPE_BOOL);
806 return 1 if($tail->name() eq "OR");
807 return 0 unless($tail->name() eq "NOT");
808 my $parent=$self->tail_parent();
809 return 0 unless($parent);
810 return 0 unless($parent->type() == $TYPE_BOOL);
811 return 1 if($parent->name() eq "OR");