+# id3fs - a FUSE-based filesystem for browsing audio metadata
+# Copyright (C) 2010 Ian Beckwith <ianb@erislabs.net>
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
package ID3FS::Path;
use strict;
our $PATH_NOARTIST = "NOARTIST";
our $PATH_NOALBUM = "NOALBUM";
+our $ENABLE_FILTER = 1;
+
sub new
{
my $proto=shift;
# print "DIRENTS: FILE: $self->{path}\n";
if($state==$STATE_ALL)
{
- @dents=($PATH_ALLTRACKS, $PATH_NOARTIST, $self->artists());
+ @dents=($self->filter($PATH_ALLTRACKS, $PATH_NOARTIST), $self->artists());
}
elsif($state==$STATE_TAG || $state==$STATE_TAGVAL)
{
- if($state==$STATE_TAG && $self->at("tag") &&
+ my $tail=$self->tail();
+ if($state==$STATE_TAG && $self->is($TYPE_TAG, $tail) &&
$self->{db}->tag_has_values($self->tail()->id()))
{
@dents=$self->tags();
{
@dents=qw(AND OR);
}
- push(@dents, $self->filter($PATH_ALLTRACKS, $PATH_NOARTIST));
- push(@dents, $self->artists());
+ push(@dents, $self->filter($PATH_ALLTRACKS, $PATH_NOARTIST), $self->artists());
}
}
elsif($state==$STATE_BOOLEAN)
{
my $parent=$self->tail();
- unless($self->is("boolean", $parent) &&
+ unless($self->is($TYPE_BOOL, $parent) &&
$parent->{name} eq "NOT")
{
@dents=("NOT");
}
elsif($state==$STATE_ALBUMS)
{
- @dents=($self->filter($PATH_ALLTRACKS, $PATH_NOALBUM), $self->albums());
+ @dents=$self->filter($PATH_ALLTRACKS, $PATH_NOALBUM, $self->albums());
}
elsif($state==$STATE_TRACKLIST)
{
return(\@dents, \@fents);
}
+# State Machine Of Doom
sub parse
{
my($self)=@_;
elsif($name eq "NOT")
{
$root_not=1;
- push(@{$self->{elements}}, ID3FS::Path::Node->new($self->{db}, "boolean", $name));
+ push(@{$self->{elements}}, ID3FS::Path::Node->new($self->{db}, $TYPE_BOOL, $name));
$self->state($STATE_BOOLEAN);
}
else
{
- $tag=ID3FS::Path::Node->new($self->{db},"tag", $name);
+ $tag=ID3FS::Path::Node->new($self->{db}, $TYPE_TAG, $name);
if($tag)
{
push(@{$self->{elements}}, $tag);
{
my $tag=$self->tail();
# print "SM: TAG/TAGVAL($state): $name\n";
- if($state==$STATE_TAG && $self->is("tag", $tag) &&
+ if($state==$STATE_TAG && $self->is($TYPE_TAG, $tag) &&
$self->{db}->tag_has_values($tag->id()))
{
# print "Parsing: parent: $tag->id()\n";
- my $tagval=ID3FS::Path::Node->new($self->{db}, "tag", $name, $tag->id());
+ my $tagval=ID3FS::Path::Node->new($self->{db}, $TYPE_TAG, $name, $tag->id());
if(defined($tagval))
{
$self->state($STATE_TAGVAL);
elsif($name eq "AND")
{
$self->state($STATE_BOOLEAN);
- push(@{$self->{elements}}, ID3FS::Path::Node->new($self->{db}, "boolean", $name));
+ push(@{$self->{elements}}, ID3FS::Path::Node->new($self->{db}, $TYPE_BOOL, $name));
}
elsif($name eq "OR")
{
$self->state($STATE_BOOLEAN);
- push(@{$self->{elements}}, ID3FS::Path::Node->new($self->{db}, "boolean", $name));
+ push(@{$self->{elements}}, ID3FS::Path::Node->new($self->{db}, $TYPE_BOOL, $name));
}
else
{
- my $artist=ID3FS::Path::Node->new($self->{db}, "artist", $name);
+ my $artist=ID3FS::Path::Node->new($self->{db}, $TYPE_ARTIST, $name);
if($artist)
{
push(@{$self->{elements}}, $artist);
# print "SM: BOOLEAN: $name\n";
my $parent=$self->tail();
my $allownot=1;
- if($self->is("boolean", $parent) &&
+ if($self->is($TYPE_BOOL, $parent) &&
$parent->{name} eq "NOT")
{
$allownot=0;
if($allownot && $name eq "NOT")
{
$self->state($STATE_BOOLEAN);
- push(@{$self->{elements}}, ID3FS::Path::Node->new($self->{db}, "boolean", $name));
+ push(@{$self->{elements}}, ID3FS::Path::Node->new($self->{db}, $TYPE_BOOL, $name));
}
else
{
- my $tag=ID3FS::Path::Node->new($self->{db}, "tag", $name);
+ my $tag=ID3FS::Path::Node->new($self->{db}, $TYPE_TAG, $name);
if($tag)
{
push(@{$self->{elements}}, $tag);
}
else
{
- my $album=ID3FS::Path::Node->new($self->{db}, "album", $name);
+ my $album=ID3FS::Path::Node->new($self->{db}, $TYPE_ALBUM, $name);
if($album)
{
push(@{$self->{elements}}, $album);
elsif($state==$STATE_TRACKLIST)
{
# print "SM: TRACKLIST: $name\n";
- my $track=ID3FS::Path::Node->new($self->{db}, "file", $name);
+ my $track=ID3FS::Path::Node->new($self->{db}, $TYPE_FILE, $name);
if($track)
{
push(@{$self->{elements}}, $track);
}
else
{
- my $artist=ID3FS::Path::Node->new($self->{db}, "artist", $name);
+ my $artist=ID3FS::Path::Node->new($self->{db}, $TYPE_ARTIST, $name);
if($artist)
{
push(@{$self->{elements}}, $artist);
# remove trailing boolean
my @elements=@{$self->{elements}};
- while(@elements && $self->is("boolean", $elements[$#elements]))
+ while(@elements && $self->is($TYPE_BOOL, $elements[$#elements]))
{
pop @elements;
}
$self->{tagtree}=$self->elements_to_tree(\@elements);
if($self->{tagtree})
{
+# use Data::Dumper;
+# print "TREE\n";
+# print Dumper $self->{tagtree};
# my ($conditions, @joins)=$self->{tagtree}->to_sql();
# print "CONDITIONS(", scalar(@joins), "): ", $conditions, "\n";
}
return undef unless(@$elements);
my ($left, $right, $op)=(undef, undef, undef);
my $thing=pop @$elements;
- if($self->is("boolean", $thing))
+ if($self->is($TYPE_BOOL, $thing))
{
$right=$self->elements_to_tree($elements);
if($thing->{name} ne "NOT")
# print "INPUT: ", join(', ', map { $_->{name}; } @input), "\n";
while(my $thing = shift @input)
{
- if($self->is("tag", $thing))
+ if($self->is($TYPE_TAG, $thing))
{
# Handle tag values by dropping parent
- if(@input && $self->is("tag", $input[0]))
+ if(@input && $self->is($TYPE_TAG, $input[0]))
{
$thing=shift @input;
}
push(@output, $thing);
}
- elsif($self->is("boolean", $thing))
+ elsif($self->is($TYPE_BOOL, $thing))
{
# bool
while(@opstack &&
{
my($self)=@_;
my $tail=$self->tail();
- if($self->is("tag", $tail))
+ if($self->is($TYPE_TAG, $tail))
{
return($self->{db}->tag_has_values($tail->id()));
}
{
my($self)=@_;
my $tail=$self->tail();
- if($self->is("tag", $tail))
+ if($self->is($TYPE_TAG, $tail))
{
return($tail->id());
}
{
my($self)=@_;
my $tail=$self->tail();
- if($self->is("tag", $tail))
+ if($self->is($TYPE_TAG, $tail))
{
return($tail->{parents_id});
}
return($self->{elements}->[$#{$self->{elements}}]);
}
-sub at
-{
- my($self, $type)=@_;
- return($self->is($type, $self->tail()));
-}
-
sub is
{
my($self, $type, $thing)=@_;
return 0 unless($thing);
return 0 unless($thing->type());
- return 1 if($type eq $thing->type());
+ return 1 if($type == $thing->type());
return 0;
}
# print "THASVALS: $hasvals\n";
# print "TPARENT: ", (defined($parent)? $parent : "NO"), "\n";
my @ids=();
- my $sql=("SELECT tags.name FROM (\n" .
- $self->tags_subselect() .
- ") AS subselect\n" .
- "INNER JOIN files_x_tags ON subselect.files_id=files_x_tags.files_id\n" .
- "INNER JOIN tags ON files_x_tags.tags_id=tags.id\n");
+ my $sql="SELECT tags.name FROM ";
+ if($self->in_or())
+ {
+ $sql .= "files_x_tags\n";
+ }
+ else
+ {
+ $sql .= ("(\n" .
+ $self->tags_subselect() .
+ ") AS subselect\n" .
+ "INNER JOIN files_x_tags ON subselect.files_id=files_x_tags.files_id\n");
+ }
+ $sql .= "INNER JOIN tags ON files_x_tags.tags_id=tags.id\n";
my @andclauses=();
my $id=$self->trailing_tag_id();
my($self)=@_;
my @ids=();
my $tail=$self->tail();
- if($self->is("artist", $tail))
+ if($self->is($TYPE_ARTIST, $tail))
{
return $self->artist_albums($tail->id());
}
{
my($self)=@_;
my $tail=$self->tail();
- if($self->is("artist", $tail))
+ if($self->is($TYPE_ARTIST, $tail))
{
return $self->artist_tracks($tail->id());
}
- elsif($self->is("album", $tail))
+ elsif($self->is($TYPE_ALBUM, $tail))
{
my $artist_id=0;
my $artist=$self->tail_parent();
- if($self->is("artist", $artist))
+ if($self->is($TYPE_ARTIST, $artist))
{
# should always happen
$artist_id=$artist->id();
{
my($self, $mountpoint)=@_;
my $tail=$self->tail();
- if($self->is("file", $tail))
+ if($self->is($TYPE_FILE, $tail))
{
my $id=$tail->id();
my $sql=("SELECT paths.name, files.name FROM files\n" .
print "FILENAME SQL: $sql\n" if($self->{verbose});
my ($path, $name)=$self->{db}->cmd_onerow($sql, $id);
my $id3fs_path=join('/', map { $_->{name}; } @{$self->{elements}});
- return($self->{db}->relativise($path, $name, $mountpoint));
+ return($self->{db}->relativise($path, $name, $mountpoint, $self->{path}));
}
# should never happen
return "ERROR";
{
my($self)=@_;
my $hasvals=$self->expecting_values();
- if($self->{in_all})
- {
- return "\tSELECT id FROM files AS files_id\n";
- }
my $tree=$self->{tagtree};
- print "UNDEF!!\n" unless($self->{tagtree});
my $parent=$self->trailing_tag_parent();
-# print "ELEMENTS: ", join('/', map { $_->{name}; } @{$self->{elements}}), "\n";
-# print "TREE: ", $tree->print(), "\n";
my $tag=undef;
if($hasvals)
{
}
my ($sqlclause, @joins)=(undef, ());
($sqlclause, @joins) = $tree->to_sql($hasvals) if($tree);
-# print "SQL(" . scalar(@joins) .": $sqlclause\n";
+# print "SQL(" . scalar(@joins) ."): $sqlclause\n";
my $sql="\tSELECT fxt1.files_id FROM tags t1";
my @crosses=();
my @inners=();
return $sql;
}
-
-sub constraints_tag_list
-{
- my($self, @constraints)=@_;
- my $lasttag=undef;
- my @tags=();
- my @tags_vals=();
- for my $constraint (@constraints)
- {
-# print ref($constraint), ": ", $constraint->{name}, "\n";
- if($self->is("tag", $constraint))
- {
- if(defined($lasttag))
- {
-# print "TAGVAL\n";
- push(@tags_vals, [$lasttag, $constraint->id()]) if defined($constraint->id());
- $lasttag=undef;
- }
- elsif($self->tag_has_values($constraint->id()))
- {
-# print "HASVALUES\n";
- $lasttag=$constraint->id() if defined($constraint->id());
- }
- else
- {
-# print "NOVALUES\n";
- push(@tags, $constraint->id()) if(defined($constraint->id()));
- }
- }
- }
- @tags=map{ "\"$_\""; } @tags;
- @tags_vals=map( { [ map({ "\"$_\""; } @$_ ) ] } @tags_vals);
- $lasttag="\"$lasttag\"" if defined($lasttag);
- return(\@tags, \@tags_vals, $lasttag);
-}
-
# we just filter $ALLTRACKS, $NOARTIST and $NOALBUM
# filtering tags properly requires up to four levels of recursion
# (tag/tagval/AND/NOT) and is too slow
sub filter
{
my($self, @dirs)=@_;
- # FIXME: disabled - too slow
- return @dirs;
+ return(@dirs) unless($ENABLE_FILTER);
my $base=$self->{path};
my @outdirs=();
for my $dir (@dirs)
return 1;
}
+# if path is .../OR/ or .../OR/NOT
+sub in_or
+{
+ my($self)=@_;
+ my $tail=$self->tail();
+ return 0 unless($tail);
+ return 0 unless($tail->type() == $TYPE_BOOL);
+ return 1 if($tail->name() eq "OR");
+ return 0 unless($tail->name() eq "NOT");
+ my $parent=$self->tail_parent();
+ return 0 unless($parent);
+ return 0 unless($parent->type() == $TYPE_BOOL);
+ return 1 if($parent->name() eq "OR");
+ return 0;
+}
+
1;