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("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("boolean", $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);
148 @{$self->{components}}=split(/\//, $self->{path});
149 shift @{$self->{components}}; # drop empty field before leading /
150 # print "PATH: $self->{path}\n";
151 $self->state($STATE_ROOT);
152 return if($self->{path} eq "/");
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}, "boolean", $name));
180 $self->state($STATE_BOOLEAN);
184 $tag=ID3FS::Path::Node->new($self->{db},"tag", $name);
187 push(@{$self->{elements}}, $tag);
189 $self->state($STATE_TAG);
193 $self->state($STATE_INVALID);
197 elsif($state==$STATE_TAG || $state==$STATE_TAGVAL)
199 my $tag=$self->tail();
200 # print "SM: TAG/TAGVAL($state): $name\n";
201 if($state==$STATE_TAG && $self->is("tag", $tag) &&
202 $self->{db}->tag_has_values($tag->id()))
204 # print "Parsing: parent: $tag->id()\n";
205 my $tagval=ID3FS::Path::Node->new($self->{db}, "tag", $name, $tag->id());
208 $self->state($STATE_TAGVAL);
210 push(@{$self->{elements}}, $tagval);
214 $self->state($STATE_INVALID);
217 elsif($name eq $PATH_ALLTRACKS)
219 $self->state($STATE_TRACKLIST);
221 elsif($name eq $PATH_NOARTIST)
223 $self->state($STATE_TRACKLIST);
225 elsif($name eq "AND")
227 $self->state($STATE_BOOLEAN);
228 push(@{$self->{elements}}, ID3FS::Path::Node->new($self->{db}, "boolean", $name));
232 $self->state($STATE_BOOLEAN);
233 push(@{$self->{elements}}, ID3FS::Path::Node->new($self->{db}, "boolean", $name));
237 my $artist=ID3FS::Path::Node->new($self->{db}, "artist", $name);
240 push(@{$self->{elements}}, $artist);
241 $self->state($STATE_ALBUMS);
245 $self->state($STATE_INVALID);
249 elsif($state==$STATE_BOOLEAN)
251 # print "SM: BOOLEAN: $name\n";
252 my $parent=$self->tail();
254 if($self->is("boolean", $parent) &&
255 $parent->{name} eq "NOT")
259 if($allownot && $name eq "NOT")
261 $self->state($STATE_BOOLEAN);
262 push(@{$self->{elements}}, ID3FS::Path::Node->new($self->{db}, "boolean", $name));
266 my $tag=ID3FS::Path::Node->new($self->{db}, "tag", $name);
269 push(@{$self->{elements}}, $tag);
271 $self->state($STATE_TAG);
275 $self->state($STATE_INVALID);
279 elsif($state==$STATE_ALBUMS)
281 # print "SM: ALBUM: $name\n";
282 if($name eq $PATH_ALLTRACKS)
284 $self->state($STATE_TRACKLIST);
286 elsif($name eq $PATH_NOALBUM)
288 $self->state($STATE_TRACKLIST);
292 my $album=ID3FS::Path::Node->new($self->{db}, "album", $name);
295 push(@{$self->{elements}}, $album);
296 $self->state($STATE_TRACKLIST);
300 $self->state($STATE_INVALID);
304 elsif($state==$STATE_TRACKLIST)
306 # print "SM: TRACKLIST: $name\n";
307 my $track=ID3FS::Path::Node->new($self->{db}, "file", $name);
310 push(@{$self->{elements}}, $track);
311 $self->state($STATE_FILE);
315 $self->state($STATE_INVALID);
318 elsif($state==$STATE_FILE)
320 # print "SM: FILE: $name\n";
321 # Can't have anything after a filename
322 $self->state($STATE_INVALID);
324 elsif($state==$STATE_ALL)
326 if($name eq $PATH_ALLTRACKS)
328 $self->state($STATE_TRACKLIST);
330 elsif($name eq $PATH_NOARTIST)
332 $self->state($STATE_TRACKLIST);
336 my $artist=ID3FS::Path::Node->new($self->{db}, "artist", $name);
339 push(@{$self->{elements}}, $artist);
340 $self->state($STATE_ALBUMS);
344 $self->state($STATE_INVALID);
350 print "SM: ERROR: UNKNOWN STATE: $self->{state}\n";
351 $self->state($STATE_INVALID);
355 # remove trailing boolean
356 my @elements=@{$self->{elements}};
357 while(@elements && $self->is("boolean", $elements[$#elements]))
361 # sort elements by precedence
362 @elements=$self->sort_elements(@elements);
363 $self->{tagtree}=$self->elements_to_tree(\@elements);
368 # print Dumper $self->{tagtree};
369 # my ($conditions, @joins)=$self->{tagtree}->to_sql();
370 # print "CONDITIONS(", scalar(@joins), "): ", $conditions, "\n";
376 my($self, $newstate)=@_;
377 if(defined($newstate))
379 $self->{state}=$newstate;
380 $self->{curtagdepth}++ if($newstate == $STATE_TAG);
382 return $self->{state};
387 my($self, $elements)=@_;
388 return undef unless(@$elements);
389 my ($left, $right, $op)=(undef, undef, undef);
390 my $thing=pop @$elements;
391 if($self->is("boolean", $thing))
393 $right=$self->elements_to_tree($elements);
394 if($thing->{name} ne "NOT")
396 $left=$self->elements_to_tree($elements);
399 $thing->right($right);
404 # Dijkstra's shunting-yard algorithm
407 my ($self, @input)=@_;
410 # print "INPUT: ", join(', ', map { $_->{name}; } @input), "\n";
411 while(my $thing = shift @input)
413 if($self->is("tag", $thing))
415 # Handle tag values by dropping parent
416 if(@input && $self->is("tag", $input[0]))
420 push(@output, $thing);
422 elsif($self->is("boolean", $thing))
426 ($priorities{$thing->{name}} <= $priorities{$opstack[$#opstack]->{name}}))
428 push(@output, pop(@opstack));
430 push(@opstack, $thing);
435 push(@output, pop(@opstack));
437 # print "STACK: ", join(', ', map { $_->{name}; } @output), "\n";
444 return() unless(defined($self->{tagtree}));
445 return($self->{tagtree}->used_tags());
451 my $tail=$self->tail();
452 if($self->is("tag", $tail))
454 return($self->{db}->tag_has_values($tail->id()));
461 my $tail=$self->tail();
462 if($self->is("tag", $tail))
469 sub trailing_tag_parent
472 my $tail=$self->tail();
473 if($self->is("tag", $tail))
475 return($tail->{parents_id});
483 return($self->{elements}->[$#{$self->{elements}}]);
489 return($self->is($type, $self->tail()));
494 my($self, $type, $thing)=@_;
495 return 0 unless($thing);
496 return 0 unless($thing->type());
497 return 1 if($type eq $thing->type());
501 # the one before last
505 return($self->{elements}->[($#{$self->{elements}}) - 1]);
508 ######################################################################
513 if(!$self->{tagtree}) # / or /NOT
515 my $sql="SELECT DISTINCT name FROM tags WHERE parents_id='';";
516 return($self->{db}->cmd_firstcol($sql));
518 my $hasvals=$self->expecting_values();
519 my $parent=$self->trailing_tag_parent();
520 # print "THASVALS: $hasvals\n";
521 # print "TPARENT: ", (defined($parent)? $parent : "NO"), "\n";
523 my $sql="SELECT tags.name FROM ";
526 $sql .= "files_x_tags\n";
531 $self->tags_subselect() .
533 "INNER JOIN files_x_tags ON subselect.files_id=files_x_tags.files_id\n");
535 $sql .= "INNER JOIN tags ON files_x_tags.tags_id=tags.id\n";
537 my $id=$self->trailing_tag_id();
539 my $parentclause= "tags.parents_id='";
542 $parentclause .= $id;
546 $parentclause .= $parent;
548 $parentclause .= "'";
549 push(@andclauses, $parentclause);
551 my @used=$self->used_tags();
554 push(@andclauses, "tags.id NOT IN (" . join(', ', @used) . ")");
558 $sql .= "WHERE " . join(' AND ', @andclauses) . "\n";
561 $sql .= "GROUP BY tags.name;";
562 print "SQL(TAGS): $sql\n" if($self->{verbose});
563 my @tagnames=$self->{db}->cmd_firstcol($sql);
564 print("SUBNAMES: ", join(', ', @tagnames), "\n") if($self->{verbose});
571 if(!@{$self->{elements}}) # /ALL
573 my $sql="SELECT DISTINCT name FROM artists WHERE name!='';";
574 return($self->{db}->cmd_firstcol($sql));
577 my $sql=$self->sql_start("artists.name");
578 $sql .= ("INNER JOIN artists ON files.artists_id=artists.id\n" .
579 "WHERE artists.name != ''\n" .
580 "GROUP BY artists.name;");
581 print "SQL(ARTISTS): $sql\n" if($self->{verbose});
582 my @tagnames=$self->{db}->cmd_firstcol($sql);
583 print("ARTISTS: ", join(', ', @tagnames), "\n") if($self->{verbose});
591 my $tail=$self->tail();
592 if($self->is("artist", $tail))
594 return $self->artist_albums($tail->id());
596 my $sql=$self->sql_start("albums.name");
597 $sql .= ("INNER JOIN albums ON files.albums_id=albums.id\n" .
598 "WHERE albums.name != ''\n" .
599 "GROUP BY albums.name;");
600 print "SQL(ALBUMS): \n$sql\n" if($self->{verbose});
601 my @names=$self->{db}->cmd_firstcol($sql);
602 print("ALBUMS: ", join(', ', @names), "\n") if($self->{verbose});
608 my($self, $artist_id)=@_;
609 my $sql=$self->sql_start("albums.name");
610 $sql .= ("INNER JOIN albums ON albums.id=files.albums_id\n" .
611 "INNER JOIN artists ON artists.id=files.artists_id\n" .
612 "WHERE artists.id=? and albums.name <> ''\n" .
613 "GROUP BY albums.name\n");
614 print "ARTIST_ALBUMS SQL: $sql\n" if($self->{verbose});
615 my @albums=$self->{db}->cmd_firstcol($sql, $artist_id);
616 print("ALBUMS: ", join(', ', @albums), "\n") if($self->{verbose});
622 my($self, $artist_id)=@_;
623 my $sql=$self->sql_start("files.name");
624 $sql .= ("INNER JOIN artists ON artists.id=files.artists_id\n" .
625 "INNER JOIN albums ON albums.id=files.albums_id\n" .
626 "WHERE artists.id=? AND albums.name=''\n" .
627 "GROUP BY files.name\n");
628 print "ARTIST_TRACKS SQL: $sql\n" if($self->{verbose});
629 my @names=$self->{db}->cmd_firstcol($sql, $artist_id);
630 print("ARTISTTRACKS: ", join(', ', @names), "\n") if($self->{verbose});
636 my($self, $artist_id, $album_id)=@_;
637 my $sql=("SELECT files.name FROM files\n" .
638 "INNER JOIN albums ON albums.id=files.albums_id\n" .
639 "INNER JOIN artists ON artists.id=files.artists_id\n" .
640 "WHERE artists.id=? AND albums.id=?\n" .
641 "GROUP BY files.name\n");
642 print "ALBUM_TRACKS SQL($artist_id, $album_id): $sql\n" if($self->{verbose});
643 my @names=$self->{db}->cmd_firstcol($sql, $artist_id, $album_id);
644 print("TRACKS: ", join(', ', @names), "\n") if($self->{verbose});
651 my $tail=$self->tail();
652 if($self->is("artist", $tail))
654 return $self->artist_tracks($tail->id());
656 elsif($self->is("album", $tail))
659 my $artist=$self->tail_parent();
660 if($self->is("artist", $artist))
662 # should always happen
663 $artist_id=$artist->id();
665 return $self->album_tracks($artist_id, $tail->id());
667 my $sql=$self->sql_start("files.name");
668 $sql .= "INNER JOIN artists ON files.artists_id=artists.id\n";
669 if($self->{components}->[$#{$self->{components}}] eq $PATH_NOARTIST)
671 $sql .= "WHERE artists.name =''\n";
673 $sql .= "GROUP BY files.name;";
674 print "TRACKS SQL($self->{path}): $sql\n" if($self->{verbose});
675 my @names=$self->{db}->cmd_firstcol($sql);
676 print("TRACKS: ", join(', ', @names), "\n") if($self->{verbose});
682 my($self, $mountpoint)=@_;
683 my $tail=$self->tail();
684 if($self->is("file", $tail))
687 my $sql=("SELECT paths.name, files.name FROM files\n" .
688 "INNER JOIN paths ON files.paths_id=paths.id\n" .
689 "WHERE files.id=?\n" .
690 "GROUP BY paths.name, files.name");
691 print "FILENAME SQL: $sql\n" if($self->{verbose});
692 my ($path, $name)=$self->{db}->cmd_onerow($sql, $id);
693 my $id3fs_path=join('/', map { $_->{name}; } @{$self->{elements}});
694 return($self->{db}->relativise($path, $name, $mountpoint, $self->{path}));
696 # should never happen
703 my $hasvals=$self->expecting_values();
706 # FIXME: is this used?
707 die "IN_ALL CALLED!!\n";
708 return "\tSELECT id FROM files AS files_id\n";
710 my $tree=$self->{tagtree};
712 print "UNDEF!!\n" unless($self->{tagtree});
713 my $parent=$self->trailing_tag_parent();
715 # print "ELEMENTS: ", join('/', map { $_->{name}; } @{$self->{elements}}), "\n";
716 # print "TREE: ", $tree->print(), "\n";
720 $tag=$self->trailing_tag_id();
721 # print "Trailing id: $tag\n";
723 my ($sqlclause, @joins)=(undef, ());
724 ($sqlclause, @joins) = $tree->to_sql($hasvals) if($tree);
725 # print "SQL(" . scalar(@joins) ."): $sqlclause\n";
726 my $sql="\tSELECT fxt1.files_id FROM tags t1";
729 # $joinsneeded++ if($tag);
730 for(my $i=0; $i <= $#joins; $i++)
734 my $inner=("\t$join JOIN files_x_tags fxt$cnt ON " .
735 "t${cnt}.id=fxt${cnt}.tags_id");
738 push(@crosses, "CROSS JOIN tags t$cnt");
739 $inner .= " AND fxt1.files_id=fxt${cnt}.files_id";
741 push(@inners, $inner);
743 $sql .= ("\n\t" . join(" ", @crosses)) if(@crosses);
744 $sql .= ("\n" . join("\n", @inners)) if(@inners);
745 $sql .= "\n\tWHERE $sqlclause" if($sqlclause);
748 # $sql .= " AND t${joinsneeded}.parents_id='$tag'";
750 $sql .= "\n\tGROUP BY fxt1.files_id\n";
756 my($self, $tables)=@_;
757 my $sql="SELECT $tables FROM ";
765 $self->tags_subselect() .
767 "INNER JOIN files ON subselect.files_id=files.id\n");
773 sub constraints_tag_list
775 my($self, @constraints)=@_;
779 for my $constraint (@constraints)
781 # print ref($constraint), ": ", $constraint->{name}, "\n";
782 if($self->is("tag", $constraint))
784 if(defined($lasttag))
787 push(@tags_vals, [$lasttag, $constraint->id()]) if defined($constraint->id());
790 elsif($self->tag_has_values($constraint->id()))
792 # print "HASVALUES\n";
793 $lasttag=$constraint->id() if defined($constraint->id());
797 # print "NOVALUES\n";
798 push(@tags, $constraint->id()) if(defined($constraint->id()));
802 @tags=map{ "\"$_\""; } @tags;
803 @tags_vals=map( { [ map({ "\"$_\""; } @$_ ) ] } @tags_vals);
804 $lasttag="\"$lasttag\"" if defined($lasttag);
805 return(\@tags, \@tags_vals, $lasttag);
808 # we just filter $ALLTRACKS, $NOARTIST and $NOALBUM
809 # filtering tags properly requires up to four levels of recursion
810 # (tag/tagval/AND/NOT) and is too slow
814 return(@dirs) unless($ENABLE_FILTER);
815 my $base=$self->{path};
819 # print "\nFILTER (",$self->state(), "): $base / $dir\n";
820 if($self->empty("$base/$dir"))
822 # print "empty: $base / $dir\n";
826 # print "non-empty, accepting: $base / $dir\n";
827 push(@outdirs, $dir);
836 my $path=ID3FS::Path->new($self->{db}, $dir, $self->{verbose},
837 ($self->{maxtagdepth} - $self->{curtagdepth}));
838 return 1 unless($path->isvalid());
839 my($subdirs,$subfiles)=$path->dirents();
840 return 0 if(@$subfiles || @$subdirs);
844 # if path is .../OR/ or .../OR/NOT
848 my $tail=$self->tail();
849 return 0 unless($tail);
850 return 0 unless($tail->type() eq "boolean");
851 return 1 if($tail->name() eq "OR");
852 return 0 unless($tail->name() eq "NOT");
853 my $parent=$self->tail_parent();
854 return 0 unless($parent);
855 return 0 unless($parent->type() eq "boolean");
856 return 1 if($parent->name() eq "OR");