sort out DB new interface; search in parent dirs for .id3fs
authorIan Beckwith <ianb@erislabs.net>
Sat, 2 Oct 2010 00:23:30 +0000 (01:23 +0100)
committerIan Beckwith <ianb@erislabs.net>
Sat, 2 Oct 2010 00:23:30 +0000 (01:23 +0100)
bin/id3fs-index
lib/ID3FS/DB.pm
sbin/id3fsd

index 9493f7c..132d81e 100755 (executable)
@@ -15,6 +15,7 @@ my $help=0;
 my $basedir=undef;
 my $dbpath=undef;
 my $list=0;
+my $init=0;
 my @extensions=qw(mp3 flac ogg);
 my $files_pruned;
 
@@ -30,13 +31,16 @@ my $optret=GetOptions(
     );
 
 usage() if(!@ARGV || !$optret || $help);
+$init=1 unless($list);
 
 if(@ARGV > 1 && !defined($basedir))
 {
     die("$me: --basedir must be specified if multiple paths are supplied\n");
 }
 
-my $db=ID3FS::DB->new($me, $dbpath, $basedir, $ARGV[0]);
+my $db=ID3FS::DB->new($me, $verbose, $init, $dbpath, $basedir, $ARGV[0]);
+exit unless($db);
+
 if($list)
 {
     list_tags($db);
@@ -97,9 +101,6 @@ sub prune
     }
 }
 
-
-
-
 sub list_tags
 {
     my($db)=@_;
index 5f08a04..f457905 100644 (file)
@@ -17,20 +17,17 @@ sub new
     bless($self,$class);
 
     $self->{me}=shift;
-    $self->{dbpath}=shift;
+    $self->{verbose}=shift;
+    my $init=shift;
+    my $dbpath=shift;
     $self->{base}=shift;
-    $self->{fallbackdir}=shift;
+    my $fallbackdir=shift;
 
-    if(!defined($self->{base}) &&
-       defined($self->{fallbackdir}) &&
-       -d $self->{fallbackdir})
-    {
-       $self->{base}=$self->{fallbackdir};
-    }
-    $self->{dbpath}="$self->{base}/$dbfile" unless(defined($self->{dbpath}));
+    $dbpath=$self->find_db($init, $dbpath, $fallbackdir);
+    return undef unless($dbpath);
     $self->{absbase}=Cwd::abs_path($self->{base});
 
-    my $connectstr="dbi:SQLite:dbname=$self->{dbpath}";
+    my $connectstr="dbi:SQLite:dbname=$dbpath";
     my ($user, $pass)=("", "");
     if($self->{postgres})
     {
@@ -38,7 +35,7 @@ sub new
        $user="ianb";
        $pass="foo";
     }
-    my $exists=-f $self->{dbpath};
+    my $exists=-f $dbpath;
     $self->{dbh}=DBI->connect($connectstr, $user, $pass,
                              { AutoCommit=>1 } );
     unless(defined($self->{dbh}))
@@ -58,6 +55,45 @@ sub new
     return $self;
 }
 
+sub find_db
+{
+    my($self, $init, $dbpath, $fallbackdir)=@_;
+    my $file=undef;
+    my $base=undef;
+    if(defined($dbpath))
+    {
+       $file=$dbpath;
+    }
+    if(defined ($self->{base}))
+    {
+       $file="$self->{base}/$dbfile" unless defined($file);
+       $base=$self->{base};
+    }
+    elsif(defined($fallbackdir) && -d $fallbackdir)
+    {
+       my $path=Cwd::abs_path($fallbackdir);
+       do
+       {
+           $file="$path/$dbfile";
+           $base=$path;
+           $path=~s/(.*)\/.*/$1/;
+       }
+       while(! -f $file && length($path) && -d $path);
+       if(! -f $file)
+       {
+           $file="$fallbackdir/$dbfile";
+           $base=$fallbackdir;
+       }
+    }
+    if(!-f $file && !$init)
+    {
+       print "$self->{me}: db not found at $file\n";
+       return undef;
+    }
+    $self->{base}=$base;
+    return $file;
+}
+
 sub create
 {
     my($self,$name)=@_;
index 2f8bb76..a5eae12 100755 (executable)
@@ -28,7 +28,9 @@ usage() if(scalar(@ARGV) != 2 || !$optret || $help);
 my $source=shift;
 my $mountpoint=shift;
 
-my $db=ID3FS::DB->new($me, $dbpath, $source);
+my $db=ID3FS::DB->new($me, $verbose, 0, $dbpath, $source);
+exit unless($db);
+
 my $fuse=ID3FS::Fuse->new($db, $source, $mountpoint, $verbose);
 $fuse->run();