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);
28 our %priorities=( "OR" => 0, "AND" => 1, "NOT" => 2 );
30 our $PATH_ALLTRACKS= "TRACKS";
31 our $PATH_NOARTIST = "NOARTIST";
32 our $PATH_NOALBUM = "NOALBUM";
34 our $ENABLE_FILTER = 1;
39 my $class=ref($proto) || $proto;
46 $self->{verbose}=shift;
47 $self->{maxtagdepth}=shift;
48 $self->{curtagdepth}=0;
49 $self->{path} =~ s/\/\//\//g; # drop doubled slashes
52 # print "STATE: ", $self->state(), "\n";
59 if(($self->state() == $STATE_FILE) ||
60 ($self->state() == $STATE_INVALID))
70 return($self->state() == $STATE_FILE);
76 return($self->state() != $STATE_INVALID);
81 my($self, $mountpoint)=@_;
82 if($self->state() == $STATE_FILE)
84 return $self->filename($mountpoint);
86 return "ERROR"; #should never happen?
94 my $state=$self->state();
95 # print "DIRENTS: STATE: $state\n";
96 # print "DIRENTS: FILE: $self->{path}\n";
97 if($state==$STATE_ALL)
99 @dents=($self->filter($PATH_ALLTRACKS, $PATH_NOARTIST), $self->artists());
101 elsif($state==$STATE_TAG || $state==$STATE_TAGVAL)
103 my $tail=$self->tail();
104 if($state==$STATE_TAG && $self->is($TYPE_TAG, $tail) &&
105 $self->{db}->tag_has_values($tail->id()))
107 @dents=$self->tags();
111 if($self->{maxtagdepth} && ($self->{curtagdepth} < $self->{maxtagdepth}))
115 push(@dents, $self->filter($PATH_ALLTRACKS, $PATH_NOARTIST), $self->artists());
118 elsif($state==$STATE_BOOLEAN)
120 my $parent=$self->tail();
121 unless($self->is($TYPE_BOOL, $parent) && $parent->{name} eq "NOT")
125 push(@dents,$self->tags());
127 elsif($state==$STATE_ROOT)
129 @dents=(qw(ALL NOT), $self->tags());
131 elsif($state==$STATE_ALBUMS)
133 @dents=$self->filter($PATH_ALLTRACKS, $PATH_NOALBUM, $self->albums());
135 elsif($state==$STATE_TRACKLIST)
137 @fents=$self->tracks();
141 print "id3fsd: INTERNAL ERROR: DIRENTS: UNHANDLED STATE: $state\n";
143 return(\@dents, \@fents);
146 # State Machine Of Doom
150 $self->state($STATE_ROOT);
151 return if($self->{path} eq "/");
152 @{$self->{components}}=split(/\//, $self->{path});
153 shift @{$self->{components}}; # drop empty field before leading /
154 # print "PATH: $self->{path}\n";
155 my @parts=@{$self->{components}};
157 $self->{elements}=[];
161 while(defined(my $name=shift @parts))
163 # print "NAME: $name\n";
164 my $state=$self->state();
165 if($state==$STATE_INVALID)
167 # print "SM: INVALID: $name\n";
170 elsif($state==$STATE_ROOT)
172 # print "SM: ROOT: $name\n";
176 $self->state($STATE_ALL);
178 elsif($name eq "NOT")
181 push(@{$self->{elements}}, ID3FS::Path::Node->new($self->{db}, $TYPE_BOOL, $name));
182 $self->state($STATE_BOOLEAN);
186 $tag=ID3FS::Path::Node->new($self->{db}, $TYPE_TAG, $name);
189 push(@{$self->{elements}}, $tag);
191 $self->state($STATE_TAG);
195 $self->state($STATE_INVALID);
199 elsif($state==$STATE_TAG || $state==$STATE_TAGVAL)
201 my $tag=$self->tail();
202 # print "SM: TAG/TAGVAL($state): $name\n";
203 if($state==$STATE_TAG && $self->is($TYPE_TAG, $tag) &&
204 $self->{db}->tag_has_values($tag->id()))
206 # print "Parsing: parent: $tag->id()\n";
207 my $tagval=ID3FS::Path::Node->new($self->{db}, $TYPE_TAG, $name, $tag->id());
210 $self->state($STATE_TAGVAL);
212 push(@{$self->{elements}}, $tagval);
216 $self->state($STATE_INVALID);
219 elsif($name eq $PATH_ALLTRACKS)
221 $self->state($STATE_TRACKLIST);
223 elsif($name eq $PATH_NOARTIST)
225 $self->state($STATE_TRACKLIST);
227 elsif($name eq "AND")
229 $self->state($STATE_BOOLEAN);
230 push(@{$self->{elements}}, ID3FS::Path::Node->new($self->{db}, $TYPE_BOOL, $name));
234 $self->state($STATE_BOOLEAN);
235 push(@{$self->{elements}}, ID3FS::Path::Node->new($self->{db}, $TYPE_BOOL, $name));
239 my $artist=ID3FS::Path::Node->new($self->{db}, $TYPE_ARTIST, $name);
242 push(@{$self->{elements}}, $artist);
243 $self->state($STATE_ALBUMS);
247 $self->state($STATE_INVALID);
251 elsif($state==$STATE_BOOLEAN)
253 # print "SM: BOOLEAN: $name\n";
254 my $parent=$self->tail();
256 if($self->is($TYPE_BOOL, $parent) &&
257 $parent->{name} eq "NOT")
261 if($allownot && $name eq "NOT")
263 $self->state($STATE_BOOLEAN);
264 push(@{$self->{elements}}, ID3FS::Path::Node->new($self->{db}, $TYPE_BOOL, $name));
268 my $tag=ID3FS::Path::Node->new($self->{db}, $TYPE_TAG, $name);
271 push(@{$self->{elements}}, $tag);
273 $self->state($STATE_TAG);
277 $self->state($STATE_INVALID);
281 elsif($state==$STATE_ALBUMS)
283 # print "SM: ALBUM: $name\n";
284 if($name eq $PATH_ALLTRACKS)
286 $self->state($STATE_TRACKLIST);
288 elsif($name eq $PATH_NOALBUM)
290 $self->state($STATE_TRACKLIST);
294 my $album=ID3FS::Path::Node->new($self->{db}, $TYPE_ALBUM, $name);
297 push(@{$self->{elements}}, $album);
298 $self->state($STATE_TRACKLIST);
302 $self->state($STATE_INVALID);
306 elsif($state==$STATE_TRACKLIST)
308 # print "SM: TRACKLIST: $name\n";
309 my $track=ID3FS::Path::Node->new($self->{db}, $TYPE_FILE, $name);
312 push(@{$self->{elements}}, $track);
313 $self->state($STATE_FILE);
317 $self->state($STATE_INVALID);
320 elsif($state==$STATE_FILE)
322 # print "SM: FILE: $name\n";
323 # Can't have anything after a filename
324 $self->state($STATE_INVALID);
326 elsif($state==$STATE_ALL)
328 if($name eq $PATH_ALLTRACKS)
330 $self->state($STATE_TRACKLIST);
332 elsif($name eq $PATH_NOARTIST)
334 $self->state($STATE_TRACKLIST);
338 my $artist=ID3FS::Path::Node->new($self->{db}, $TYPE_ARTIST, $name);
341 push(@{$self->{elements}}, $artist);
342 $self->state($STATE_ALBUMS);
346 $self->state($STATE_INVALID);
352 print "SM: ERROR: UNKNOWN STATE: $self->{state}\n";
353 $self->state($STATE_INVALID);
357 my @elements=@{$self->{elements}};
358 # remove trailing boolean
359 while(@elements && $self->is($TYPE_BOOL, $elements[$#elements]))
363 # sort elements by precedence
364 @elements=$self->sort_elements(@elements);
365 $self->{tagtree}=$self->elements_to_tree(\@elements);
370 my($self, $newstate)=@_;
371 if(defined($newstate))
373 $self->{state}=$newstate;
374 $self->{curtagdepth}++ if($newstate == $STATE_TAG);
376 return $self->{state};
379 # link up precedence-sorted list into a binary tree
382 my($self, $elements)=@_;
383 return undef unless(@$elements);
384 my ($left, $right, $op)=(undef, undef, undef);
385 my $thing=pop @$elements;
386 if($self->is($TYPE_BOOL, $thing))
388 $right=$self->elements_to_tree($elements);
389 if($thing->{name} ne "NOT")
391 $left=$self->elements_to_tree($elements);
394 $thing->right($right);
399 # Dijkstra's shunting-yard algorithm
402 my ($self, @input)=@_;
405 # print "INPUT: ", join(', ', map { $_->{name}; } @input), "\n";
406 while(my $thing = shift @input)
408 if($self->is($TYPE_TAG, $thing))
410 # Handle tag values by dropping parent
411 if(@input && $self->is($TYPE_TAG, $input[0]))
415 push(@output, $thing);
417 elsif($self->is($TYPE_BOOL, $thing))
420 ($priorities{$thing->{name}} <= $priorities{$opstack[$#opstack]->{name}}))
422 push(@output, pop(@opstack));
424 push(@opstack, $thing);
429 push(@output, pop(@opstack));
431 # print "STACK: ", join(', ', map { $_->{name}; } @output), "\n";
438 return() unless(defined($self->{tagtree}));
439 return($self->{tagtree}->used_tags());
445 my $tail=$self->tail();
446 if($self->is($TYPE_TAG, $tail))
448 return($self->{db}->tag_has_values($tail->id()));
455 my $tail=$self->tail();
456 if($self->is($TYPE_TAG, $tail))
463 sub trailing_tag_parent
466 my $tail=$self->tail();
467 if($self->is($TYPE_TAG, $tail))
469 return($tail->{parents_id});
477 return($self->{elements}->[$#{$self->{elements}}]);
482 my($self, $type, $thing)=@_;
483 return 0 unless($thing);
484 return 0 unless($thing->type());
485 return 1 if($type == $thing->type());
489 # the one before last
493 return($self->{elements}->[($#{$self->{elements}}) - 1]);
496 ######################################################################
501 if(!$self->{tagtree}) # / or /NOT
503 my $sql="SELECT DISTINCT name FROM tags WHERE parents_id='';";
504 return($self->{db}->cmd_firstcol($sql));
506 my $hasvals=$self->expecting_values();
507 my $parent=$self->trailing_tag_parent();
508 # print "THASVALS: $hasvals\n";
509 # print "TPARENT: ", (defined($parent)? $parent : "NO"), "\n";
511 my $sql="SELECT tags.name FROM ";
514 $sql .= "files_x_tags\n";
519 $self->tags_subselect() .
521 "INNER JOIN files_x_tags ON subselect.files_id=files_x_tags.files_id\n");
523 $sql .= "INNER JOIN tags ON files_x_tags.tags_id=tags.id\n";
525 my $id=$self->trailing_tag_id();
527 my $parentclause= "tags.parents_id='";
530 $parentclause .= $id;
534 $parentclause .= $parent;
536 $parentclause .= "'";
537 push(@andclauses, $parentclause);
539 my @used=$self->used_tags();
542 push(@andclauses, "tags.id NOT IN (" . join(', ', @used) . ")");
546 $sql .= "WHERE " . join(' AND ', @andclauses) . "\n";
549 $sql .= "GROUP BY tags.name;";
550 print "SQL(TAGS): $sql\n" if($self->{verbose});
551 my @tagnames=$self->{db}->cmd_firstcol($sql);
552 print("SUBNAMES: ", join(', ', @tagnames), "\n") if($self->{verbose});
559 if(!@{$self->{elements}}) # /ALL
561 my $sql="SELECT DISTINCT name FROM artists WHERE name!='';";
562 return($self->{db}->cmd_firstcol($sql));
565 my $sql=$self->sql_start("artists.name");
566 $sql .= ("INNER JOIN artists ON files.artists_id=artists.id\n" .
567 "WHERE artists.name != ''\n" .
568 "GROUP BY artists.name;");
569 print "SQL(ARTISTS): $sql\n" if($self->{verbose});
570 my @tagnames=$self->{db}->cmd_firstcol($sql);
571 print("ARTISTS: ", join(', ', @tagnames), "\n") if($self->{verbose});
579 my $tail=$self->tail();
580 if($self->is($TYPE_ARTIST, $tail))
582 return $self->artist_albums($tail->id());
584 my $sql=$self->sql_start("albums.name");
585 $sql .= ("INNER JOIN albums ON files.albums_id=albums.id\n" .
586 "WHERE albums.name != ''\n" .
587 "GROUP BY albums.name;");
588 print "SQL(ALBUMS): \n$sql\n" if($self->{verbose});
589 my @names=$self->{db}->cmd_firstcol($sql);
590 print("ALBUMS: ", join(', ', @names), "\n") if($self->{verbose});
596 my($self, $artist_id)=@_;
597 my $sql=$self->sql_start("albums.name");
598 $sql .= ("INNER JOIN albums ON albums.id=files.albums_id\n" .
599 "INNER JOIN artists ON artists.id=files.artists_id\n" .
600 "WHERE artists.id=? and albums.name <> ''\n" .
601 "GROUP BY albums.name\n");
602 print "ARTIST_ALBUMS SQL: $sql\n" if($self->{verbose});
603 my @albums=$self->{db}->cmd_firstcol($sql, $artist_id);
604 print("ALBUMS: ", join(', ', @albums), "\n") if($self->{verbose});
610 my($self, $artist_id)=@_;
611 my $sql=$self->sql_start("files.name");
612 $sql .= ("INNER JOIN artists ON artists.id=files.artists_id\n" .
613 "INNER JOIN albums ON albums.id=files.albums_id\n" .
614 "WHERE artists.id=? AND albums.name=''\n" .
615 "GROUP BY files.name\n");
616 print "ARTIST_TRACKS SQL: $sql\n" if($self->{verbose});
617 my @names=$self->{db}->cmd_firstcol($sql, $artist_id);
618 print("ARTISTTRACKS: ", join(', ', @names), "\n") if($self->{verbose});
624 my($self, $artist_id, $album_id)=@_;
625 my $sql=("SELECT files.name FROM files\n" .
626 "INNER JOIN albums ON albums.id=files.albums_id\n" .
627 "INNER JOIN artists ON artists.id=files.artists_id\n" .
628 "WHERE artists.id=? AND albums.id=?\n" .
629 "GROUP BY files.name\n");
630 print "ALBUM_TRACKS SQL($artist_id, $album_id): $sql\n" if($self->{verbose});
631 my @names=$self->{db}->cmd_firstcol($sql, $artist_id, $album_id);
632 print("TRACKS: ", join(', ', @names), "\n") if($self->{verbose});
639 my $tail=$self->tail();
640 if($self->is($TYPE_ARTIST, $tail))
642 return $self->artist_tracks($tail->id());
644 elsif($self->is($TYPE_ALBUM, $tail))
647 my $artist=$self->tail_parent();
648 if($self->is($TYPE_ARTIST, $artist))
650 # should always happen
651 $artist_id=$artist->id();
653 return $self->album_tracks($artist_id, $tail->id());
655 my $sql=$self->sql_start("files.name");
656 $sql .= "INNER JOIN artists ON files.artists_id=artists.id\n";
657 if($self->{components}->[$#{$self->{components}}] eq $PATH_NOARTIST)
659 $sql .= "WHERE artists.name =''\n";
661 $sql .= "GROUP BY files.name;";
662 print "TRACKS SQL($self->{path}): $sql\n" if($self->{verbose});
663 my @names=$self->{db}->cmd_firstcol($sql);
664 print("TRACKS: ", join(', ', @names), "\n") if($self->{verbose});
670 my($self, $mountpoint)=@_;
671 my $tail=$self->tail();
672 if($self->is($TYPE_FILE, $tail))
675 my $sql=("SELECT paths.name, files.name FROM files\n" .
676 "INNER JOIN paths ON files.paths_id=paths.id\n" .
677 "WHERE files.id=?\n" .
678 "GROUP BY paths.name, files.name");
679 print "FILENAME SQL: $sql\n" if($self->{verbose});
680 my ($path, $name)=$self->{db}->cmd_onerow($sql, $id);
681 return($self->{db}->relativise($path, $name, $mountpoint, $self->{path}));
683 # should never happen
690 my $hasvals=$self->expecting_values();
691 my $tree=$self->{tagtree};
692 my $parent=$self->trailing_tag_parent();
697 $tag=$self->trailing_tag_id();
698 # print "Trailing id: $tag\n";
700 my ($sqlclause, @joins)=(undef, ());
701 ($sqlclause, @joins) = $tree->to_sql($hasvals) if($tree);
702 # print "SQL(" . scalar(@joins) ."): $sqlclause\n";
703 my $sql="\tSELECT fxt1.files_id FROM tags t1";
706 # $joinsneeded++ if($tag);
707 for(my $i=0; $i <= $#joins; $i++)
711 my $inner=("\t$join JOIN files_x_tags fxt$cnt ON " .
712 "t${cnt}.id=fxt${cnt}.tags_id");
715 push(@crosses, "CROSS JOIN tags t$cnt");
716 $inner .= " AND fxt1.files_id=fxt${cnt}.files_id";
718 push(@inners, $inner);
720 $sql .= ("\n\t" . join(" ", @crosses)) if(@crosses);
721 $sql .= ("\n" . join("\n", @inners)) if(@inners);
722 $sql .= "\n\tWHERE $sqlclause" if($sqlclause);
725 # $sql .= " AND t${joinsneeded}.parents_id='$tag'";
727 $sql .= "\n\tGROUP BY fxt1.files_id\n";
733 my($self, $tables)=@_;
734 my $sql="SELECT $tables FROM ";
742 $self->tags_subselect() .
744 "INNER JOIN files ON subselect.files_id=files.id\n");
749 # we just filter $ALLTRACKS, $NOARTIST and $NOALBUM
750 # filtering tags properly requires up to four levels of recursion
751 # (tag/tagval/AND/NOT) and is too slow
755 return(@dirs) unless($ENABLE_FILTER);
756 my $base=$self->{path};
760 push(@outdirs, $dir) unless($self->empty("$base/$dir"));
768 my $path=ID3FS::Path->new($self->{db}, $dir, $self->{verbose},
769 ($self->{maxtagdepth} - $self->{curtagdepth}));
770 return 1 unless($path->isvalid());
771 my($subdirs,$subfiles)=$path->dirents();
772 return 0 if(@$subfiles || @$subdirs);
776 # if path is .../OR/ or .../OR/NOT
780 my $tail=$self->tail();
781 return 0 unless($tail);
782 return 0 unless($tail->type() == $TYPE_BOOL);
783 return 1 if($tail->name() eq "OR");
784 return 0 unless($tail->name() eq "NOT");
785 my $parent=$self->tail_parent();
786 return 0 unless($parent);
787 return 0 unless($parent->type() == $TYPE_BOOL);
788 return 1 if($parent->name() eq "OR");