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);
367 # print Dumper $self->{tagtree};
368 # my ($conditions, @joins)=$self->{tagtree}->to_sql();
369 # print "CONDITIONS(", scalar(@joins), "): ", $conditions, "\n";
375 my($self, $newstate)=@_;
376 if(defined($newstate))
378 $self->{state}=$newstate;
379 $self->{curtagdepth}++ if($newstate == $STATE_TAG);
381 return $self->{state};
386 my($self, $elements)=@_;
387 return undef unless(@$elements);
388 my ($left, $right, $op)=(undef, undef, undef);
389 my $thing=pop @$elements;
390 if($self->is("boolean", $thing))
392 $right=$self->elements_to_tree($elements);
393 if($thing->{name} ne "NOT")
395 $left=$self->elements_to_tree($elements);
398 $thing->right($right);
403 # Dijkstra's shunting-yard algorithm
406 my ($self, @input)=@_;
409 # print "INPUT: ", join(', ', map { $_->{name}; } @input), "\n";
410 while(my $thing = shift @input)
412 if($self->is("tag", $thing))
414 # Handle tag values by dropping parent
415 if(@input && $self->is("tag", $input[0]))
419 push(@output, $thing);
421 elsif($self->is("boolean", $thing))
425 ($priorities{$thing->{name}} <= $priorities{$opstack[$#opstack]->{name}}))
427 push(@output, pop(@opstack));
429 push(@opstack, $thing);
434 push(@output, pop(@opstack));
436 # print "STACK: ", join(', ', map { $_->{name}; } @output), "\n";
443 return() unless(defined($self->{tagtree}));
444 return($self->{tagtree}->used_tags());
450 my $tail=$self->tail();
451 if($self->is("tag", $tail))
453 return($self->{db}->tag_has_values($tail->id()));
460 my $tail=$self->tail();
461 if($self->is("tag", $tail))
468 sub trailing_tag_parent
471 my $tail=$self->tail();
472 if($self->is("tag", $tail))
474 return($tail->{parents_id});
482 return($self->{elements}->[$#{$self->{elements}}]);
488 return($self->is($type, $self->tail()));
493 my($self, $type, $thing)=@_;
494 return 0 unless($thing);
495 return 0 unless($thing->type());
496 return 1 if($type eq $thing->type());
500 # the one before last
504 return($self->{elements}->[($#{$self->{elements}}) - 1]);
507 ######################################################################
512 if(!$self->{tagtree}) # / or /NOT
514 my $sql="SELECT DISTINCT name FROM tags WHERE parents_id='';";
515 return($self->{db}->cmd_firstcol($sql));
517 my $hasvals=$self->expecting_values();
518 my $parent=$self->trailing_tag_parent();
519 # print "THASVALS: $hasvals\n";
520 # print "TPARENT: ", (defined($parent)? $parent : "NO"), "\n";
522 my $sql="SELECT tags.name FROM ";
525 $sql .= "files_x_tags\n";
530 $self->tags_subselect() .
532 "INNER JOIN files_x_tags ON subselect.files_id=files_x_tags.files_id\n");
534 $sql .= "INNER JOIN tags ON files_x_tags.tags_id=tags.id\n";
536 my $id=$self->trailing_tag_id();
538 my $parentclause= "tags.parents_id='";
541 $parentclause .= $id;
545 $parentclause .= $parent;
547 $parentclause .= "'";
548 push(@andclauses, $parentclause);
550 my @used=$self->used_tags();
553 push(@andclauses, "tags.id NOT IN (" . join(', ', @used) . ")");
557 $sql .= "WHERE " . join(' AND ', @andclauses) . "\n";
560 $sql .= "GROUP BY tags.name;";
561 print "SQL(TAGS): $sql\n" if($self->{verbose});
562 my @tagnames=$self->{db}->cmd_firstcol($sql);
563 print("SUBNAMES: ", join(', ', @tagnames), "\n") if($self->{verbose});
570 if(!@{$self->{elements}}) # /ALL
572 my $sql="SELECT DISTINCT name FROM artists WHERE name!='';";
573 return($self->{db}->cmd_firstcol($sql));
576 my $sql=$self->sql_start("artists.name");
577 $sql .= ("INNER JOIN artists ON files.artists_id=artists.id\n" .
578 "WHERE artists.name != ''\n" .
579 "GROUP BY artists.name;");
580 print "SQL(ARTISTS): $sql\n" if($self->{verbose});
581 my @tagnames=$self->{db}->cmd_firstcol($sql);
582 print("ARTISTS: ", join(', ', @tagnames), "\n") if($self->{verbose});
590 my $tail=$self->tail();
591 if($self->is("artist", $tail))
593 return $self->artist_albums($tail->id());
595 my $sql=$self->sql_start("albums.name");
596 $sql .= ("INNER JOIN albums ON files.albums_id=albums.id\n" .
597 "WHERE albums.name != ''\n" .
598 "GROUP BY albums.name;");
599 print "SQL(ALBUMS): \n$sql\n" if($self->{verbose});
600 my @names=$self->{db}->cmd_firstcol($sql);
601 print("ALBUMS: ", join(', ', @names), "\n") if($self->{verbose});
607 my($self, $artist_id)=@_;
608 my $sql=$self->sql_start("albums.name");
609 $sql .= ("INNER JOIN albums ON albums.id=files.albums_id\n" .
610 "INNER JOIN artists ON artists.id=files.artists_id\n" .
611 "WHERE artists.id=? and albums.name <> ''\n" .
612 "GROUP BY albums.name\n");
613 print "ARTIST_ALBUMS SQL: $sql\n" if($self->{verbose});
614 my @albums=$self->{db}->cmd_firstcol($sql, $artist_id);
615 print("ALBUMS: ", join(', ', @albums), "\n") if($self->{verbose});
621 my($self, $artist_id)=@_;
622 my $sql=$self->sql_start("files.name");
623 $sql .= ("INNER JOIN artists ON artists.id=files.artists_id\n" .
624 "INNER JOIN albums ON albums.id=files.albums_id\n" .
625 "WHERE artists.id=? AND albums.name=''\n" .
626 "GROUP BY files.name\n");
627 print "ARTIST_TRACKS SQL: $sql\n" if($self->{verbose});
628 my @names=$self->{db}->cmd_firstcol($sql, $artist_id);
629 print("ARTISTTRACKS: ", join(', ', @names), "\n") if($self->{verbose});
635 my($self, $artist_id, $album_id)=@_;
636 my $sql=("SELECT files.name FROM files\n" .
637 "INNER JOIN albums ON albums.id=files.albums_id\n" .
638 "INNER JOIN artists ON artists.id=files.artists_id\n" .
639 "WHERE artists.id=? AND albums.id=?\n" .
640 "GROUP BY files.name\n");
641 print "ALBUM_TRACKS SQL($artist_id, $album_id): $sql\n" if($self->{verbose});
642 my @names=$self->{db}->cmd_firstcol($sql, $artist_id, $album_id);
643 print("TRACKS: ", join(', ', @names), "\n") if($self->{verbose});
650 my $tail=$self->tail();
651 if($self->is("artist", $tail))
653 return $self->artist_tracks($tail->id());
655 elsif($self->is("album", $tail))
658 my $artist=$self->tail_parent();
659 if($self->is("artist", $artist))
661 # should always happen
662 $artist_id=$artist->id();
664 return $self->album_tracks($artist_id, $tail->id());
666 my $sql=$self->sql_start("files.name");
667 $sql .= "INNER JOIN artists ON files.artists_id=artists.id\n";
668 if($self->{components}->[$#{$self->{components}}] eq $PATH_NOARTIST)
670 $sql .= "WHERE artists.name =''\n";
672 $sql .= "GROUP BY files.name;";
673 print "TRACKS SQL($self->{path}): $sql\n" if($self->{verbose});
674 my @names=$self->{db}->cmd_firstcol($sql);
675 print("TRACKS: ", join(', ', @names), "\n") if($self->{verbose});
681 my($self, $mountpoint)=@_;
682 my $tail=$self->tail();
683 if($self->is("file", $tail))
686 my $sql=("SELECT paths.name, files.name FROM files\n" .
687 "INNER JOIN paths ON files.paths_id=paths.id\n" .
688 "WHERE files.id=?\n" .
689 "GROUP BY paths.name, files.name");
690 print "FILENAME SQL: $sql\n" if($self->{verbose});
691 my ($path, $name)=$self->{db}->cmd_onerow($sql, $id);
692 my $id3fs_path=join('/', map { $_->{name}; } @{$self->{elements}});
693 return($self->{db}->relativise($path, $name, $mountpoint, $self->{path}));
695 # should never happen
702 my $hasvals=$self->expecting_values();
705 # FIXME: is this used?
706 die "IN_ALL CALLED!!\n";
707 return "\tSELECT id FROM files AS files_id\n";
709 my $tree=$self->{tagtree};
711 print "UNDEF!!\n" unless($self->{tagtree});
712 my $parent=$self->trailing_tag_parent();
714 # print "ELEMENTS: ", join('/', map { $_->{name}; } @{$self->{elements}}), "\n";
715 # print "TREE: ", $tree->print(), "\n";
719 $tag=$self->trailing_tag_id();
720 # print "Trailing id: $tag\n";
722 my ($sqlclause, @joins)=(undef, ());
723 ($sqlclause, @joins) = $tree->to_sql($hasvals) if($tree);
724 # print "SQL(" . scalar(@joins) ."): $sqlclause\n";
725 my $sql="\tSELECT fxt1.files_id FROM tags t1";
728 # $joinsneeded++ if($tag);
729 for(my $i=0; $i <= $#joins; $i++)
733 my $inner=("\t$join JOIN files_x_tags fxt$cnt ON " .
734 "t${cnt}.id=fxt${cnt}.tags_id");
737 push(@crosses, "CROSS JOIN tags t$cnt");
738 $inner .= " AND fxt1.files_id=fxt${cnt}.files_id";
740 push(@inners, $inner);
742 $sql .= ("\n\t" . join(" ", @crosses)) if(@crosses);
743 $sql .= ("\n" . join("\n", @inners)) if(@inners);
744 $sql .= "\n\tWHERE $sqlclause" if($sqlclause);
747 # $sql .= " AND t${joinsneeded}.parents_id='$tag'";
749 $sql .= "\n\tGROUP BY fxt1.files_id\n";
755 my($self, $tables)=@_;
756 my $sql="SELECT $tables FROM ";
764 $self->tags_subselect() .
766 "INNER JOIN files ON subselect.files_id=files.id\n");
772 sub constraints_tag_list
774 my($self, @constraints)=@_;
778 for my $constraint (@constraints)
780 # print ref($constraint), ": ", $constraint->{name}, "\n";
781 if($self->is("tag", $constraint))
783 if(defined($lasttag))
786 push(@tags_vals, [$lasttag, $constraint->id()]) if defined($constraint->id());
789 elsif($self->tag_has_values($constraint->id()))
791 # print "HASVALUES\n";
792 $lasttag=$constraint->id() if defined($constraint->id());
796 # print "NOVALUES\n";
797 push(@tags, $constraint->id()) if(defined($constraint->id()));
801 @tags=map{ "\"$_\""; } @tags;
802 @tags_vals=map( { [ map({ "\"$_\""; } @$_ ) ] } @tags_vals);
803 $lasttag="\"$lasttag\"" if defined($lasttag);
804 return(\@tags, \@tags_vals, $lasttag);
807 # we just filter $ALLTRACKS, $NOARTIST and $NOALBUM
808 # filtering tags properly requires up to four levels of recursion
809 # (tag/tagval/AND/NOT) and is too slow
813 return(@dirs) unless($ENABLE_FILTER);
814 my $base=$self->{path};
818 # print "\nFILTER (",$self->state(), "): $base / $dir\n";
819 if($self->empty("$base/$dir"))
821 # print "empty: $base / $dir\n";
825 # print "non-empty, accepting: $base / $dir\n";
826 push(@outdirs, $dir);
835 my $path=ID3FS::Path->new($self->{db}, $dir, $self->{verbose},
836 ($self->{maxtagdepth} - $self->{curtagdepth}));
837 return 1 unless($path->isvalid());
838 my($subdirs,$subfiles)=$path->dirents();
839 return 0 if(@$subfiles || @$subdirs);
843 # if path is .../OR/ or .../OR/NOT
847 my $tail=$self->tail();
848 return 0 unless($tail);
849 return 0 unless($tail->type() eq "boolean");
850 return 1 if($tail->name() eq "OR");
851 return 0 unless($tail->name() eq "NOT");
852 my $parent=$self->tail_parent();
853 return 0 unless($parent);
854 return 0 unless($parent->type() eq "boolean");
855 return 1 if($parent->name() eq "OR");