Unlink list file with sqlite

master
Luca Bruno 2010-03-15 14:21:59 +01:00
parent cabf1ed7fe
commit 6abce3de01
3 changed files with 46 additions and 0 deletions

View File

@ -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)
{

View File

@ -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

18
tdpkg.c
View File

@ -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)
{