Hi.

This patch restores the fdb code in a working state.
It also copies the resulting fdb.gz file to/from the karma.

Keith.
# HG changeset patch
# User Keith Bennett <[EMAIL PROTECTED]>
# Node ID 046940ad8ebc90467d11015affd008adabc2ec58
# Parent  a592d15cf01179f5541cab64626a0b50da4f618c
Re-implemented fdb support.
Added method for uploading files listed in a given fdb.gz file.
Use the fdb.gz file obtained from the karma if present.
Upload the current fdb.gz file to the taxi on completion.

diff -r a592d15cf011 -r 046940ad8ebc src/Jamfile
--- a/src/Jamfile       Thu Aug 17 10:59:11 2006
+++ b/src/Jamfile       Sat Aug 19 16:05:31 2006
@@ -1,7 +1,7 @@
 SubDir TOP src ;
 
 BuildLib karma : ;
-LibMember karma : errors.c hash.c karma.c karmaUsb.c karmaLan.c md5.c 
properties.c ssdp.c util.c playlist.c status.c ;
+LibMember karma : errors.c hash.c karma.c karmaUsb.c karmaLan.c md5.c 
properties.c ssdp.c util.c playlist.c status.c fdb.c ;
 
 LINKFLAGS += -lz ;
 
@@ -18,4 +18,5 @@
 InstallFile $(PREFIX)/include : ssdp.h ;
 InstallFile $(PREFIX)/include : util.h ;
 InstallFile $(PREFIX)/include : playlist.h ;
+InstallFile $(PREFIX)/include : fdb.h ;
 InstallFile $(PREFIX)/include : status.h ;
diff -r a592d15cf011 -r 046940ad8ebc src/Makefile
--- a/src/Makefile      Thu Aug 17 10:59:11 2006
+++ b/src/Makefile      Sat Aug 19 16:05:31 2006
@@ -1,10 +1,10 @@
 NAME=libkarma
 VERSION=0.0.5
 TARGET=$(NAME)
-SRCS= errors.c hash.c karma.c karmaLan.c karmaUsb.c md5.c \
+SRCS= errors.c fdb.c hash.c karma.c karmaLan.c karmaUsb.c md5.c mountSearch.c \
        mp3.c playlist.c properties.c rio_rw.c ssdp.c status.c utf8.c \
-       util.c wav.c mountSearch.c
-INST_HEADERS=lkarma.h utf8.h
+       util.c wav.c
+INST_HEADERS=lkarma.h utf8.h fdb.h
 
 LIBDIR=../lib
 
diff -r a592d15cf011 -r 046940ad8ebc src/karma.c
--- a/src/karma.c       Thu Aug 17 10:59:11 2006
+++ b/src/karma.c       Sat Aug 19 16:05:31 2006
@@ -13,6 +13,7 @@
 #include "karma.h"
 #include "karmaLan.h"
 #include "karmaUsb.h"
+#include "fdb.h"
 
 #ifdef __USE_ISOC99
 #define set_usb_ptr(x) .lk_karma_ ##x = lk_karmaUsb_ ##x
@@ -112,9 +113,12 @@
 int lk_karma_connect(char *ipHostOrPath)
 {
     int using_usb = (ipHostOrPath[0] == '/');
+    int rio;
     lk_ops = using_usb ? &usb_ops : &lan_ops;
 
-    return lk_ops->lk_karma_connect(ipHostOrPath);
+    rio = lk_ops->lk_karma_connect(ipHostOrPath);
+    lk_fdb_set_device(rio);
+    return rio;
 }
 
 int32_t lk_karma_hangup(int rio)
diff -r a592d15cf011 -r 046940ad8ebc src/lkarma.h
--- a/src/lkarma.h      Thu Aug 17 10:59:11 2006
+++ b/src/lkarma.h      Sat Aug 19 16:05:31 2006
@@ -57,6 +57,8 @@
 void     lk_karma_load_database          (int rio);
 
 
+char   **lk_fdb_getlist                  (char *fdb);
+
 /*
  * properties
  */
diff -r a592d15cf011 -r 046940ad8ebc src/properties.c
--- a/src/properties.c  Thu Aug 17 10:59:11 2006
+++ b/src/properties.c  Sat Aug 19 16:05:31 2006
@@ -23,6 +23,7 @@
 #include "properties.h"
 #include "playlist.h"
 #include "util.h"
+#include "fdb.h"
 
 #define HASHSIZE 16
 #define TABLECHUNK 4096
@@ -131,6 +132,7 @@
         }
     }
     hash_destroy(&item);
+    lk_fdb_load();
     return count;
 }
 
@@ -283,6 +285,7 @@
     lk_properties_import(buf);
     free(buf);
     free(fname);
+    lk_fdb_load();
     return 0;
 }
 /*
@@ -312,6 +315,7 @@
     char * buf;
     struct utimbuf time;
 
+    lk_fdb_save();
     fname=lk_path_string("fileinfo");
     if(mk_path(fname) == -1){
         free(fname);
diff -r a592d15cf011 -r 046940ad8ebc src/rio_rw.c
--- a/src/rio_rw.c      Thu Aug 17 10:59:11 2006
+++ b/src/rio_rw.c      Sat Aug 19 16:05:31 2006
@@ -17,6 +17,7 @@
 #include <unistd.h>
 #include <fcntl.h>
 #include <errno.h>
+#include <limits.h>
 
 #include <taglib/tag_c.h>
 
@@ -157,7 +158,7 @@
     }
 
     memset(&mp3, 0, sizeof(mp3info));
-    mp3.filename = (char*)filename;
+    mp3.filename = filename;
     type = get_file_type(&mp3);
 
     rid_fd = open(filename, O_RDONLY);
@@ -211,9 +212,6 @@
     if (usb)
         free(filename);
 
-    if (got != 0)
-        lk_properties_del_property(fid);
-
     return got;
 }
 
@@ -225,20 +223,36 @@
          3. use rio_write as often as you need to :)
          4. release the IO Lock.
 */
-uint32_t lk_rio_write(int rio, const char * filename)
+static uint32_t lk_rio_do_write(int fdb, int rio, const char * filename)
 {
     int got, fd;
-    uint32_t fid;
+    uint32_t fid = 0;
     uint64_t offs=0;
     char *p, tmp[BLOCKSIZE];
     const uint64_t siz=BLOCKSIZE;
+    char *path, fullpath[PATH_MAX];
+    uint32_t *ids;
 
     fd=open(filename, O_RDONLY);
     if (fd == -1)
         return -1;
 
-    fid = lk_properties_new_property(); 
+    if (fdb) {
+        ids = lk_properties_andOrSearch(EXACT|ORS, NULL, "title", "fdb.gz");
+        if (ids) {
+            ids = lk_properties_andOrSearch(EXACT|ANDS, ids, "type", "taxi");
+            if (ids) {
+                fid = *ids;
+                free(ids);
+            }
+        }
+    }
+    if (!fid)
+        fid = lk_properties_new_property(); 
+
     got = lk_rio_update_props_from_tags(rio, fid, filename);
+    if (got != 0)
+        lk_properties_del_property(fid);
 
     if (got != 0) {
         close(fd);
@@ -262,11 +276,26 @@
     }
     close(fd);
 
+    if (!fdb) {
+        path = realpath(filename, fullpath);
+        lk_properties_set_property(fid, "path", path);
+    }
+
     p=lk_properties_export(fid);
     got=lk_karma_update_file_details(rio, fid, p);
     free(p);
 
     return fid;
+}
+
+uint32_t lk_rio_write(int rio, const char * filename)
+{
+    return lk_rio_do_write(0, rio, filename);
+}
+
+uint32_t lk_rio_write_fdb(const char * filename)
+{
+    return lk_rio_do_write(1, 0, filename);
 }
 
 static void add_general(HASH * p, const char *filename)
@@ -418,6 +447,8 @@
     }
 
     tl_tag = taglib_file_tag(tl_file);
+    if (! tl_tag)
+        return 0;
     tl_audio = taglib_file_audioproperties(tl_file);
 
     title  = taglib_tag_title (tl_tag);
@@ -437,7 +468,6 @@
                     return -1;
                 }
             }
-            free(ids);
         }
     }
 
diff -r a592d15cf011 -r 046940ad8ebc src/rio_rw.h
--- a/src/rio_rw.h      Thu Aug 17 10:59:11 2006
+++ b/src/rio_rw.h      Sat Aug 19 16:05:31 2006
@@ -13,4 +13,6 @@
 
 #include "lkarma.h"
 
+uint32_t lk_rio_write_fdb(const char * filename);
+
 #endif /* _RIO_RW_H */
diff -r a592d15cf011 -r 046940ad8ebc tools/riocp.c
--- a/tools/riocp.c     Thu Aug 17 10:59:11 2006
+++ b/tools/riocp.c     Sat Aug 19 16:05:31 2006
@@ -58,7 +58,7 @@
            "             [-u props] [-x props] [-l props] "
                         "[-c charSet] [-e set1 set2 set3]\n"
            "             [-D dummyName] [-q] [-d] [-r] [-R] [-b] [-w] [-f]\n" 
-           "             [files_to_upload ...]\n"
+           "             [-F] [-U fdb.gz] [files_to_upload ...]\n"
            "\n");
     if(!complete) return;
     printf("  \"-s\" AND/OR-search karma DB according to a \"searchString\";\n"
@@ -97,6 +97,8 @@
     printf("  \"-b\" get database by scanning *1 files, instead of smalldb\n");
     printf("  \"-w\" don't rewrite the smalldb database file\n");
     printf("  \"-f\" force the files to be written even if they are dupes\n");
+    printf("  \"-F\" Use filenames in ~/.openrio/.../fdb.gz for download\n");
+    printf("  \"-U\" Upload files listed in the given fdb.gz file\n");
 }
 
 void currentKarma(int i, int *k) 
@@ -108,6 +110,41 @@
         *k=karma;
 }
 
+void fdb_download(void)
+{
+    int karma, i;
+    uint32_t * fdb;
+    char * filename;
+
+    currentKarma(0, &karma);
+    fdb=lk_properties_andOrSearch(EXACT|ORS, NULL, "path", "");
+    if (!fdb)
+        return;
+
+    for (i=0; fdb[i]; i++) {
+        filename=lk_properties_get_property(fdb[i], "path");
+        if (!filename)
+            continue;
+        printf("Downloading fid %i to %s\n", fdb[i], filename);
+        fprintf(stderr, "lk_rio_read %s\n", 
+        CHECK(lk_rio_read(karma, fdb[i], filename)));
+    }
+    free(fdb);
+}
+
+void fdb_upload(char *fdb)
+{
+    char **list, **file;
+
+    list = lk_fdb_getlist(fdb);
+    for (file=list; *file; file++) {
+        CHECK(ftw(*file, treecopy, 1));
+        free(*file);
+    }
+    if (list)
+        free(list);
+}
+
 int treecopy(const char *file, const struct stat *sb, int flag)
 {
     static int first = 1;
@@ -139,7 +176,7 @@
 
     while (keyValue!=NULL) {
         key=utf8_to_codeset(keyValue);
-        fprintf(stderr, "%9s FIDs with \"%-30s:", 
+        fprintf(stderr, "%9s FIDs with \"%-30s\":", 
                 (andOr==-1)?"Searching":(andOr?"ANDing":"ORing"),key);
         free(key);
         if(andOr==-1) andOr = ORS;
@@ -371,6 +408,7 @@
     char *unkName=UNK_NAME;          /* Dummy name for missing properties */
     int  squeezeRepeats=0;
     int  download=0;
+    int  fdbDownload=0;
     int  removeFiles=0;
     int  exact=0;
     int  useSmalldb=1;
@@ -383,8 +421,9 @@
     int dbChanged;
     char *utf;
     char *device=NULL;
+    char *fdb=NULL;
     
-    while ((c = getopt(argc, argv, "dhqrRbwfa:c:e:p:s:S:u:x:l:D:")) != -1) {
+    while ((c = getopt(argc, argv, "dhqrRbwfFa:c:e:p:s:S:u:x:l:D:U:")) != -1) {
         switch(c) {
             case 'h': case '?':
                 usage(1);
@@ -407,6 +446,9 @@
                 break;
             case 'f':
                 lk_karma_write_dupes(1);
+                break;
+            case 'F':
+                fdbDownload=1;
                 break;
             case 'a':
                 host=strdup(optarg);
@@ -445,6 +487,9 @@
             case 'D':
                 unkName=strdup(optarg);
                 break;
+            case 'U':
+                fdb=strdup(optarg);
+                break;
             default:
                 fprintf(stderr, "unknown option %c\n", c);
                 break;
@@ -453,10 +498,10 @@
     if(taxiPropsToUse==NULL) taxiPropsToUse=strdup(tunePropsToUse);
     if(plstPropsToUse==NULL) plstPropsToUse=strdup(taxiPropsToUse);
 
+    lk_errors_autoprint(0);
+
     if(useSmalldb)
         lk_karma_use_smalldb();
-
-    lk_errors_autoprint(0);
 
     if(!host) { /* 'host' means a net host name, an IP, or mount directory */
         /* trying to find a USB/OMFS-mounted Karma */
@@ -525,7 +570,7 @@
     }
     fprintf(stderr, "Using %s codeset.\n", usercodeset);
     lk_errors_autoprint(1);               /* Setting auto-print-errors ON */
-    
+ 
     if(psw){
         fprintf(stderr, "lk_karma_authenticate: %i\n", 
                 lk_karma_authenticate(karma, psw));
@@ -533,6 +578,7 @@
     }else
         fprintf(stderr, "lk_karma_authenticate: %i\n", 
                 lk_karma_authenticate(karma, ""));
+
     r = lk_karma_request_io_lock(karma,IO_LOCK_R);
     fprintf(stderr, "lk_karma_request_io_lock: %s\n", CHECK(r));
     if(r == -1){
@@ -555,6 +601,9 @@
     free(settings);
     if(!r) lk_karma_load_database(karma);
     if(dbChanged) printf("Done!\n");
+
+    if(fdbDownload)
+        fdb_download();
 
     if (searchString != NULL){
         utf=utf8_from_codeset(searchString);
@@ -601,21 +650,30 @@
         }
         for(; optind<argc; optind++)
             ftw(argv[optind], treecopy, 1);
-    }
-    fprintf(stderr, "lk_karma_release_io_lock: %s\n", 
-            CHECK(lk_karma_release_io_lock(karma)));
-
+    } else if (fdb) {
+        fprintf(stderr, "lk_karma_request_io_lock: %s\n", 
+                CHECK(lk_karma_request_io_lock(karma,IO_LOCK_W)));
+        if(lk_synchronize_necessary(karma)){
+            lk_karma_get_all_file_details(karma, &psw);
+            lk_properties_import(psw);
+            free(psw);
+        }
+        fdb_upload(fdb);
+    }
+
+    lk_properties_save();
     if (writeSmalldb) {
         ret = lk_karma_write_smalldb();
         if (ret)
             fprintf(stderr, "error writing smalldb file\n");
     }
+    fprintf(stderr, "lk_karma_release_io_lock: %s\n", 
+            CHECK(lk_karma_release_io_lock(karma)));
 
     fprintf(stderr, "lk_karma_hangup: %s\n", CHECK(lk_karma_hangup(karma)));
     close(karma);
     free(vp);
 
-    lk_properties_save();
     lk_properties_destroy();
     utf8_destroy();
     return ret;
diff -r a592d15cf011 -r 046940ad8ebc src/fdb.c
--- /dev/null   Thu Aug 17 10:59:11 2006
+++ b/src/fdb.c Sat Aug 19 16:05:31 2006
@@ -0,0 +1,196 @@
+/*
+ * libkarma/fdb.h
+ * 
+ * Copyright (c) Frank Zschockelt <[EMAIL PROTECTED]> 2005
+ * 
+ * You may distribute and modify this program under the terms of 
+ * the GNU GPL, version 2 or later.
+ * 
+ */
+#include <stdlib.h>
+#include <string.h>
+#include <zlib.h>
+
+#include "lkarma.h"
+#include "properties.h"
+#include "util.h"
+#include "rio_rw.h"
+#include "fdb.h"
+
+static int rio = 0;
+
+/*
+ * fid:fid_generation path
+ */
+
+int lk_fdb_load(void)
+{
+    gzFile fdb_file;
+    char * fname;
+    char * buf=NULL,*p,*n_tok;
+    int read,size=0;
+    uint32_t fid;
+    uint32_t *db;
+    char * fid_gen;
+    HASH * tmp;
+    
+    /*
+     * Datenbank vom Karma laden
+    */
+    fname=lk_path_string("fdb.gz");
+    if (fname == NULL)
+        return -1;
+
+    db=lk_properties_andOrSearch(EXACT|ORS, NULL, "title", "fdb.gz");
+    if (db) {
+        db=lk_properties_andOrSearch(EXACT|ORS, db, "type", "taxi");
+        if (db) {
+            lk_rio_read(rio, *db, fname);
+            free(db);
+        }
+    }
+
+    fdb_file=gzopen(fname, "r");
+    free(fname);
+    if (fdb_file == NULL)
+        return -1;
+
+    do{
+        buf=realloc(buf, size+8192);
+        read=gzread(fdb_file, &buf[size], 8192);
+        size+=read;
+    }while(read > 0);    
+    gzclose(fdb_file);
+    
+    if((read == -1) || (size == 0)){
+        free(buf);
+        return read;
+    }
+    buf[size]='\0';
+    p=strtok_r(buf, ":", &n_tok);
+    while(p!=NULL){
+        fid=strtoul(p, (char **)NULL, 10);
+        fid_gen=strtok_r(NULL, " ", &n_tok);
+        p=strtok_r(NULL, "\n", &n_tok);
+        if((tmp=lk_properties_idsearch(fid)))
+            if(strcmp(fid_gen, lk_properties_get_property_hash(tmp,
+                      "fid_generation")) == 0)
+                lk_properties_set_property_hash(tmp, "path", p);
+        p=strtok_r(NULL, ":", &n_tok);
+    }
+    free(buf);
+
+    return 0;
+}
+
+char **lk_fdb_getlist(char *fdb)
+{
+    gzFile fdb_file;
+    char **list, **entry;
+    char * buf=NULL,*p,*n_tok;
+    int read,size=0;
+    uint32_t fid;
+    char * fid_gen;
+    HASH * tmp;
+    int nlist, len;
+    
+    fdb_file=gzopen(fdb, "r");
+    if (fdb_file == NULL)
+        return NULL;
+
+    do{
+        buf=realloc(buf, size+8192);
+        read=gzread(fdb_file, &buf[size], 8192);
+        size+=read;
+    }while(read > 0);    
+    gzclose(fdb_file);
+    
+    if((read == -1) || (size == 0)){
+        free(buf);
+        return NULL;
+    }
+    buf[size]='\0';
+    nlist = 0;
+    for(p=buf; *p != '\0'; p++)
+        if (*p == '\n') nlist++;
+    if (nlist == 0)
+        return NULL;
+    nlist++;
+    entry = list = calloc(nlist*sizeof(char *), 1);
+    p=strtok_r(buf, ":", &n_tok);
+    while(p!=NULL){
+        fid=strtoul(p, (char **)NULL, 10);
+        fid_gen=strtok_r(NULL, " ", &n_tok);
+        p=strtok_r(NULL, "\n", &n_tok);
+        len=strlen(p)+1;
+        *entry = malloc(len);
+        memcpy(*entry, p, len);
+        entry++;
+        p=strtok_r(NULL, ":", &n_tok);
+    }
+    free(buf);
+
+    return list;
+}
+
+int lk_fdb_save(void)
+{
+    gzFile fdb_file;
+    char * fname;
+    char * id, * path, * tm;
+    uint32_t * fdb, *db;
+    HASH * tmp;
+    int i=0, first=1;
+    
+    fname=lk_path_string("fdb.gz");
+    fdb=lk_properties_andOrSearch(EXACT|ORS, NULL, "path", "");
+    if(fdb){
+        fdb_file=gzopen(fname, "w");
+        for(i=0; fdb[i]; i++){
+            tmp=lk_properties_idsearch(fdb[i]);
+            if (!tmp)
+                continue;
+            path=lk_properties_get_property_hash(tmp, "path");
+            tm=lk_properties_get_property_hash(tmp, "fid_generation");
+            if (!path || !tm) {
+                if (first) {
+                    db=lk_properties_andOrSearch(EXACT|ORS, NULL, "title",
+                                                                  "fdb.gz");
+                    if (db) {
+                        db=lk_properties_andOrSearch(EXACT|ORS, db, "type",
+                                                                    "taxi");
+                        if (db) {
+                            free(db);
+                            continue;
+                        }
+                    }
+                    lk_errors_set(E_NOPROP);
+                    first = 0;
+                }
+                continue;
+            }
+            id=simple_itoa(fdb[i]);
+            gzwrite(fdb_file, id, strlen(id));
+            gzwrite(fdb_file, ":", 1); 
+            gzwrite(fdb_file, tm, strlen(tm));
+            gzwrite(fdb_file, " ", 1);
+            gzwrite(fdb_file, path, strlen(path));
+            gzwrite(fdb_file, "\n", 1);
+            
+        }
+        free(fdb);
+        gzclose(fdb_file);
+    }
+    lk_rio_write_fdb(fname);
+
+    /*
+     * Datenbank auf Karma als Taxifile kopieren...
+     */
+    free(fname);
+    return 0;
+}
+
+void lk_fdb_set_device(int karma)
+{
+    rio = karma;
+}
diff -r a592d15cf011 -r 046940ad8ebc src/fdb.h
--- /dev/null   Thu Aug 17 10:59:11 2006
+++ b/src/fdb.h Sat Aug 19 16:05:31 2006
@@ -0,0 +1,25 @@
+/*
+ * libkarma/fdb.h
+ * 
+ * Copyright (c) Frank Zschockelt <[EMAIL PROTECTED]> 2005
+ * 
+ * You may distribute and modify this program under the terms of 
+ * the GNU GPL, version 2 or later.
+ * 
+ */
+#ifndef _FDB_H
+#define _FDB_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+int  lk_fdb_load      (void);
+int  lk_fdb_save      (void);
+void lk_fdb_set_device(int karma);
+
+#ifdef __cplusplus
+}
+#endif  /* C++ */
+
+#endif /* _FDB_H */
-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
linux-karma-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-karma-devel

Reply via email to