From 6abce3de0147062e75945876171acf7856cf71f2 Mon Sep 17 00:00:00 2001 From: Luca Bruno Date: Mon, 15 Mar 2010 14:21:59 +0100 Subject: [PATCH] Unlink list file with sqlite --- cache-sqlite.c | 27 +++++++++++++++++++++++++++ cache.h | 1 + tdpkg.c | 18 ++++++++++++++++++ 3 files changed, 46 insertions(+) diff --git a/cache-sqlite.c b/cache-sqlite.c index d639dd8..2821e90 100644 --- a/cache-sqlite.c +++ b/cache-sqlite.c @@ -35,10 +35,12 @@ #define CREATE_TABLE_SQL "CREATE TABLE IF NOT EXISTS files (filename varchar(255) PRIMARY KEY ON CONFLICT REPLACE, contents text);" #define READ_FILE_SQL "SELECT contents FROM files WHERE filename=?" #define INSERT_FILE_SQL "INSERT INTO files (filename, contents) VALUES (?, ?)" +#define DELETE_FILE_SQL "DELETE FROM files WHERE filename=?" static sqlite3* db = NULL; static sqlite3_stmt* read_file_stmt = NULL; static sqlite3_stmt* insert_file_stmt = NULL; +static sqlite3_stmt* delete_file_stmt = NULL; static int _sqlite_exec (const char* sql) @@ -107,6 +109,12 @@ _sqlite_init (void) sqlite_error (-1); } + if (sqlite3_prepare (db, DELETE_FILE_SQL, -1, &delete_file_stmt, NULL) != SQLITE_OK) + { + tdpkg_cache_finalize (); + sqlite_error (-1); + } + /* ensure cache consistency with the file system */ struct stat stat_buf; if (tdpkg_stat (CACHE_FILE, &stat_buf)) @@ -250,6 +258,25 @@ tdpkg_cache_write_filename (const char* filename) return 0; } +int +tdpkg_cache_delete_filename (const char* filename) +{ + if (_sqlite_init ()) + return -1; + + if (sqlite3_reset (delete_file_stmt) != SQLITE_OK) + sqlite_error (-1); + + if (sqlite3_bind_text (delete_file_stmt, 1, filename, -1, SQLITE_STATIC) != SQLITE_OK) + sqlite_error (-1); + + if (sqlite3_step (delete_file_stmt) != SQLITE_DONE) + sqlite_error (-1); + + return 0; +} + + int tdpkg_cache_rebuild (void) { diff --git a/cache.h b/cache.h index 4d2e28e..44521b2 100644 --- a/cache.h +++ b/cache.h @@ -5,6 +5,7 @@ int tdpkg_cache_initialize (void); void tdpkg_cache_finalize (void); char* tdpkg_cache_read_filename (const char* filename); int tdpkg_cache_write_filename (const char* filename); +int tdpkg_cache_delete_filename (const char* filename); int tdpkg_cache_rebuild (void); #endif diff --git a/tdpkg.c b/tdpkg.c index 3ba7550..9c06d4e 100644 --- a/tdpkg.c +++ b/tdpkg.c @@ -40,6 +40,7 @@ static int (*real__fxstat64)(int ver, int fd, struct stat64* buf); static ssize_t (*realread)(int fildes, void *buf, size_t nbyte); static int (*realclose)(int fd); static int (*realrename)(const char *old, const char *new); +static int (*realunlink)(const char* pathname); /* handle open() of dpkg/src/filesdb.c */ #define FAKE_FD 4321 @@ -84,6 +85,7 @@ void _init (void) realread = dlsym (RTLD_NEXT, "read"); realclose = dlsym (RTLD_NEXT, "close"); realrename = dlsym (RTLD_NEXT, "rename"); + realunlink = dlsym (RTLD_NEXT, "unlink"); if (!tdpkg_cache_initialize ()) cache_initialized = 1; @@ -113,6 +115,22 @@ rename (const char *old, const char *new) return result; } +int +unlink (const char* pathname) +{ + int result = realunlink (pathname); + if (!result && is_list_file (pathname)) + { + if (tdpkg_cache_delete_filename (pathname)) + { + fprintf (stderr, "tdpkg: can't delete %s from cache, no wrapping\n", pathname); + tdpkg_cache_finalize (); + cache_initialized = 0; + } + } + return result; +} + static int _tdpkg_open (const char *path, int oflag, int mode) {