From f03530d96048b56393ba08b52349c39c6059a759 Mon Sep 17 00:00:00 2001 From: HeavenVolkoff Date: Thu, 19 Aug 2021 01:14:55 -0300 Subject: [PATCH] Improve password logic for newly detected files (fix #14) --- monitor.c | 24 ++++++++++++++++++++---- scanner.c | 2 +- scanner.h | 3 +++ 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/monitor.c b/monitor.c index 80dd1f7..5744db6 100644 --- a/monitor.c +++ b/monitor.c @@ -356,7 +356,10 @@ monitor_insert_file(const char *name, const char *path) char *id = NULL; char video[PATH_MAX]; const char *tbl = "DETAILS"; - char * password = NULL; + char * dir_password = NULL; + char password_buf[11]; + char file_password[11] = {0}; + char password_path[PATH_MAX]; int depth = 1; int ts; media_types dir_types; @@ -432,16 +435,29 @@ monitor_insert_file(const char *name, const char *path) //DEBUG DPRINTF(E_DEBUG, L_INOTIFY, "Checking %s\n", parent_buf); id = sql_get_text_field(db, "SELECT OBJECT_ID from OBJECTS o left join DETAILS d on (d.ID = o.DETAIL_ID)" " where d.PATH = '%q' and REF_ID is NULL", parent_buf); + + snprintf(password_path, PATH_MAX, "%s/.password", parent_buf); + if( access(password_path, 0) == 0 ) + { + readPassword(password_path, password_buf, 11); + dir_password = password_buf; + } + else if( id ) + dir_password = sql_get_text_field(db, "select PASSWORD from OBJECTS where OBJECT_ID='%s'", id); + + // Save first directory password seen as file password + if (dir_password != NULL && file_password[0] == '\0'); + strcpy(file_password, dir_password); + if( id ) { if( !depth ) break; - password = sql_get_text_field(db, "select PASSWORD from OBJECTS where OBJECT_ID='%s'", id); DPRINTF(E_DEBUG, L_INOTIFY, "Found first known parentID: %s [%s]\n", id, parent_buf); /* Insert newly-found directory */ strcpy(base_name, last_dir); base_copy = basename(base_name); - insert_directory(base_copy, last_dir, BROWSEDIR_ID, id+2, get_next_available_id("OBJECTS", id), password); + insert_directory(base_copy, last_dir, BROWSEDIR_ID, id+2, get_next_available_id("OBJECTS", id), dir_password); sqlite3_free(id); break; } @@ -466,7 +482,7 @@ monitor_insert_file(const char *name, const char *path) if( !depth ) { //DEBUG DPRINTF(E_DEBUG, L_INOTIFY, "Inserting %s\n", name); - int ret = insert_file(name, path, id+2, get_next_available_id("OBJECTS", id), dir_types, password); + int ret = insert_file(name, path, id+2, get_next_available_id("OBJECTS", id), dir_types, file_password[0] == '\0' ? NULL : file_password); if (ret == 1 && (mtype & TYPE_PLAYLIST)) { next_pl_fill = time(NULL) + 120; // Schedule a playlist scan for 2 minutes from now. diff --git a/scanner.c b/scanner.c index 6fa6d57..6503fc9 100644 --- a/scanner.c +++ b/scanner.c @@ -718,7 +718,7 @@ filter_avp(scan_filter *d) ); } -static void +void readPassword(const char *dir, char *password, int size) { FILE *pFile = NULL; diff --git a/scanner.h b/scanner.h index 4bd8cbf..b52f0db 100644 --- a/scanner.h +++ b/scanner.h @@ -80,6 +80,9 @@ insert_file(const char *name, const char *path, const char *parentID, int object int CreateDatabase(void); +void +readPassword(const char *dir, char *password, int size); + void start_scanner();