enable filter
[id3fs.git] / lib / ID3FS / Fuse.pm
index 2a02276..62dd3a8 100644 (file)
@@ -4,6 +4,7 @@ use strict;
 use warnings;
 use ID3FS::Path;
 use Fuse;
+use Cwd;
 use POSIX qw(EINVAL EROFS ENOENT EOPNOTSUPP S_IRUSR S_IRGRP S_IROTH S_IXUSR S_IXGRP S_IXOTH);
 our ($TYPE_DIR, $TYPE_SYMLINK)=(0040, 0120);
 
@@ -16,7 +17,7 @@ sub new
 
     $self->{db}=shift;
     $self->{source}=shift;
-    $self->{mountpoint}=shift;
+    $self->{mountpoint}=Cwd::abs_path(shift);
     $self->{debug}=shift;
     $self->{perms} = S_IRUSR() | S_IXUSR() | S_IRGRP() | S_IXGRP() | S_IROTH() | S_IXOTH();
 
@@ -34,6 +35,7 @@ sub run
        getattr     => sub { $self->getattr(@_); },
        readlink    => sub { $self->readlink(@_); },
        getdir      => sub { $self->getdir(@_); },
+
        # Not used
 #      mknod       => sub { $self->mknod(@_);       },
 #      mkdir       => sub { $self->mkdir(@_);       },
@@ -83,7 +85,7 @@ sub readlink
 #    print "**READLINK: $filename\n";
     my $path=ID3FS::Path->new($self->{db}, $filename);
     return(-EINVAL()) unless($path->isfile());
-    return $path->dest();
+    return $path->dest($self->{mountpoint});
 }
 
 sub getdir
@@ -92,11 +94,54 @@ sub getdir
 #    print "**GETDIR: $filename\n";
     my $path=ID3FS::Path->new($self->{db}, $filename);
     return(-ENOENT()) unless($path->isvalid());
-    if($path->isdir())
+    return(-ENOTDIR()) unless($path->isdir());
+    my @dents=();
+    my($dirs, $files)=$path->dirents();
+    push(@dents, $self->filter($filename, @$dirs));
+    push(@dents, @$files);
+    if(@dents)
+    {
+       return( (".", "..", @dents, 0) );
+    }
+    return(0);
+}
+
+sub filter
+{
+    my($self, $base, @dirs)=@_;
+    my @outdirs=();
+    for my $dir (@dirs)
     {
-       return(".", "..", $path->dirents(), 0);
+       print "hascontents: $base / $dir\n";
+       my $path=ID3FS::Path->new($self->{db}, "$base/$dir");
+       next unless($path->isvalid());
+       my($subdirs,$subfiles)=$path->dirents();
+       print "SUBDENTS: ", join(", ", @$subdirs, @$subfiles), "\n";
+       next unless(@$subdirs || @$subfiles);
+       if(!@$subfiles || scalar(@$subdirs) < 3)
+       {
+           my $subdirents=0;
+           for my $subdir (@$subdirs)
+           {
+               if(grep { $_ eq $subdir; } qw(AND OR NOT))
+               {
+                   my $subsubpath=ID3FS::Path->new($self->{db}, "$base/$dir/$subdir");
+                   if($path->isvalid())
+                   {
+                       my($subsubdirs,$subsubfiles)=$subsubpath->dirents();
+                       $subdirents++ if(@$subsubdirs || @$subsubfiles);
+                   }
+               }
+               else
+               {
+                   $subdirents++;
+               }
+           }
+           next unless($subdirents);
+       }
+       push(@outdirs, $dir);
     }
-    return -ENOTDIR();
+    return(@outdirs)
 }
 
 # unused stubs