]> pd.if.org Git - zpackage/blobdiff - lib/zpm.c
add libelf_soneed function
[zpackage] / lib / zpm.c
index 99984fbef7d645864fa235fdfdd072dd1a9a0682..30ec7a669cae61ae2f8e50c2cb43f2c4f7b427b4 100644 (file)
--- a/lib/zpm.c
+++ b/lib/zpm.c
@@ -183,6 +183,11 @@ int zpm_commit(struct zpm *z) {
        return 1;
 }
 
+/* wrapper for sqlite3_exec */
+int zpm_exec(struct zpm *z, const char *sql, int(*callback)(void *, int, char **, char**), void *arg, char **errmsg) {
+       return sqlite3_exec(z->db, sql, callback, arg, errmsg);
+}
+
 int zpm_rollback(struct zpm *z) {
        char *errstr = 0;
        sqlite3_exec(z->db, "rollback;", NULL, NULL, &errstr);
@@ -349,6 +354,22 @@ int zpm_close(struct zpm *pkg) {
        return 1;
 }
 
+static int zpm_sqlite_vercmp(void *not_used, int lena, const void *a,
+               int lenb, const void *b) {
+       /* not sure what the ints are, possibly string lengths */
+       if (not_used != 0) fprintf(stderr, "sqlite vercmp not_used = %p\n",
+                       not_used);
+       if (lena == 0 && lenb > 0) return 1;
+       return zpm_vercmp(a, b);
+}
+
+int zpm_addvercmp(struct zpm *pkg) {
+       return sqlite3_create_collation(
+                       pkg->db, "vercmp", SQLITE_UTF8, NULL,
+                       zpm_sqlite_vercmp
+                       );
+}
+
 /* set package struct variables, database, environment, then command line */
 int zpm_readopts(struct zpm *pkg, int ac, char **av) {
        char *ev;
@@ -372,7 +393,7 @@ int zpm_readopts(struct zpm *pkg, int ac, char **av) {
 
        /* now, parse the options, return optind so the caller can adjust if needed */
 
-       return 1;
+       return av ? ac : 1;
 }
 
 int zpm_extract(struct zpm *pkg, char *hash, char *path, int mode) {
@@ -404,7 +425,7 @@ int zpm_extract(struct zpm *pkg, char *hash, char *path, int mode) {
                /* didn't find a row */
                sqlite3_finalize(ifile);
                sqlite3_close(db);
-               fprintf(stderr, "no such hash\n");
+               fprintf(stderr, "no such hash: %s\n", hash);
                return 0;
        }
        /* either way we're done with this now */
@@ -450,6 +471,7 @@ int zpm_extract(struct zpm *pkg, char *hash, char *path, int mode) {
        //fprintf(stderr, "uncompressing %d bytes at %p, expect %lld\n", blobsize, xzdata, (long long int)size);
        uncompresslzma(xzdata, blobsize, out);
        fclose(out);
+       chmod(path, mode);
 
        sqlite3_finalize(ifile);
 
@@ -461,7 +483,7 @@ int zpm_hash(char *path, char *hash, uint32_t flags) {
        int fd;
        void *content;
        struct stat sbuf;
-       hash_state md;
+       struct sha256_state md;
        int j;
        unsigned char tmp[32];
 
@@ -498,7 +520,7 @@ int zpm_hash(char *path, char *hash, uint32_t flags) {
        }
        hash[64] = 0;
        munmap(content, sbuf.st_size);
-       return 1;
+       return flags ? fd : 1;
 }
 
 static sqlite3_stmt *run_for_hash(sqlite3 *db, char *sql, char *hash) {
@@ -580,57 +602,22 @@ static int set_elf_info(sqlite3 *db, char *hash, char *content, size_t length) {
                hdr = libelf_header(content);
                /* if lib, set soname */
                if (libelf_type(content) == ET_DYN) {
-                       char *elf;
-                       Elf64_Shdr *shdr, *dynsect, *dynstrtab = 0;
-
-                       elf = (char *)content;
-                       for (i = 0; i < hdr->e_shnum; i++) {
-                               shdr = (Elf64_Shdr *)(elf + hdr->e_shoff + i * hdr->e_shentsize);
-                               if (shdr->sh_type == SHT_DYNAMIC) {
-                                       dynsect = shdr;
-                               } else if (shdr->sh_type == SHT_STRTAB && i == hdr->e_shstrndx) {
-                                       dynstrtab = shdr;
-                               }
-                       }
-                       if (!dynstrtab) {
-                               exit(8);
-                       }
-                       if (!dynsect) {
-                               exit(9);
-                       }
-
-                       char *name;
-                       Elf64_Shdr *dyntab;
-                       name = elf + dynstrtab->sh_offset;
-                       for (i = 0; i < hdr->e_shnum; i++) {
-                               shdr = (Elf64_Shdr *)(elf + hdr->e_shoff + i * hdr->e_shentsize);
-                               if (shdr->sh_type == SHT_STRTAB && !strcmp(".dynstr", name+shdr->sh_name)) {
-                                       dyntab = shdr;
-                               }
-                       }
-                       if (!dyntab) {
-                               exit(10);
-                       }
-
-                       char *dynname;
-                       Elf64_Dyn *dent;
-                       dynname = elf + dyntab->sh_offset;
-
-                       for (dent = (Elf64_Dyn *)(elf + dynsect->sh_offset); dent->d_tag != DT_NULL; dent++) {
-                               if (dent->d_tag == DT_SONAME) {
-                                       char *soname = dynname + dent->d_un.d_val;
-                                       sqlite3_prepare_v2(db, "insert into elflibraries (file,soname) values (?,?)",-1, &ifile, 0);
-                                       sqlite3_bind_text(ifile,1,hash,64,SQLITE_STATIC);
-                                       sqlite3_bind_text(ifile,2,soname,-1,SQLITE_STATIC);
-                                       rc = sqlite3_step(ifile);
-                                       if (rc != SQLITE_DONE) {
-                                               SQLERROR(sqlite3_errmsg(db));
-                                               sqlite3_finalize(ifile);
-                                               fprintf(stderr, "error setting library soname\n");
-                                               return 0;
-                                       }
+                       char *soname = libelf_soname(content);
+                       if (soname) {
+
+                               sqlite3_prepare_v2(db, "insert into elflibraries (file,soname) values (?,?)",-1, &ifile, 0);
+                               sqlite3_bind_text(ifile,1,hash,64,SQLITE_STATIC);
+                               sqlite3_bind_text(ifile,2,soname,-1,SQLITE_STATIC);
+                               rc = sqlite3_step(ifile);
+                               if (rc != SQLITE_DONE) {
+                                       SQLERROR(sqlite3_errmsg(db));
                                        sqlite3_finalize(ifile);
+                                       fprintf(stderr, "error setting library soname\n");
+                                       return 0;
                                }
+                               sqlite3_finalize(ifile);
+                       } else {
+                               fprintf(stderr, "can't find soname\n");
                        }
                }
 
@@ -693,7 +680,7 @@ int zpm_import(struct zpm *pkg, char *path, uint32_t flags, char *hash) {
        void *content;
        struct stat sbuf;
        unsigned char tmp[32];
-       hash_state md;
+       struct sha256_state md;
        sqlite3_stmt *ifile;
        int haverow = 0,havedata = 0;
        int j,rc,type;
@@ -712,6 +699,9 @@ int zpm_import(struct zpm *pkg, char *path, uint32_t flags, char *hash) {
                hash = hashbuf;
        }
 
+       if (flags) {
+               fprintf(stderr, "zpm_import unused flags = %d\n", flags);
+       }
        /* mmap the file */
        fd = open(path, O_RDONLY);
        if (fd == -1) {