Modify mediadb-update 93/310493/3 accepted/tizen/unified/20240503.115748 accepted/tizen/unified/toolchain/20240508.012336 accepted/tizen/unified/x/20240507.051001
authorMinje Ahn <minje.ahn@samsung.com>
Tue, 30 Apr 2024 03:30:53 +0000 (12:30 +0900)
committerMinje Ahn <minje.ahn@samsung.com>
Thu, 2 May 2024 22:34:16 +0000 (07:34 +0900)
1. Remove non-recursive type.
2. Change to query directly instead of dlopen.
3. Code cleanup

Change-Id: Id4e4793b9cf1152847720f9ce3190ea94d553394
Signed-off-by: Minje Ahn <minje.ahn@samsung.com>
packaging/media-server-user.service
packaging/media-server.spec
src/mediadb-update.c

index 4a8b644..31056be 100644 (file)
@@ -5,7 +5,7 @@ Description=Media server user
 Type=simple
 CPUAccounting=yes
 CPUQuota=10%
-ExecStart=/usr/bin/sh -c "/usr/bin/mediadb-update check_db;/usr/bin/mediadb-update -r `/usr/bin/tzplatform-get TZ_USER_CONTENT | /usr/bin/sed -e 's/^.*=//g'`"
+ExecStart=/usr/bin/sh -c "/usr/bin/mediadb-update check_db;/usr/bin/mediadb-update `/usr/bin/tzplatform-get TZ_USER_CONTENT | /usr/bin/sed -e 's/^.*=//g'`"
 
 [Install]
 WantedBy=default.target
index ece0d0f..c89b1a6 100644 (file)
@@ -1,6 +1,6 @@
 Name:       media-server
 Summary:    A server for media content management
-Version:    0.6.0
+Version:    0.6.1
 Release:    0
 Group:      Multimedia/Service
 License:    Apache-2.0
index e896e6f..81fb4b7 100644 (file)
@@ -19,7 +19,6 @@
 
 #include <stdio.h>
 #include <stdlib.h>
-#include <string.h>
 #include <dlfcn.h>
 #include <glib.h>
 #include <sys/time.h>
 
 #include "media-util.h"
 
-#define PATH_PLUGIN_LIB                                PATH_LIBDIR"/libmedia-content-plugin.so"
+#define PATH_PLUGIN_LIB                PATH_LIBDIR"/libmedia-content-plugin.so"
 
-static GMainLoop * mainloop = NULL;
-static struct timeval start_time;
+static GMainLoop *g_loop = NULL;
+static long long int g_start = 0;
 
-static int (*svc_check_db)                     (sqlite3 * handle, uid_t uid);
-static int (*svc_get_storage_id)               (sqlite3 * handle, const char *path, char *storage_id, uid_t uid);
+static int (*svc_check_db)                     (sqlite3 *, uid_t);
 
-static void callback(media_request_result_s * result, void *user_data)
+static long long int __make_timestamp(void)
 {
-       long long ms_time = 0;
-       struct timeval end_time;
+       struct timeval t;
+       gettimeofday(&t, NULL);
+       return (t.tv_sec * 1000LL + t.tv_usec / 1000);
+}
 
-       if (result->result != MEDIA_REQUEST_SCAN_PARTIAL) {
-               gettimeofday(&end_time, NULL);
-               ms_time = (end_time.tv_sec * 1000LL + end_time.tv_usec / 1000) - (start_time.tv_sec * 1000LL + start_time.tv_usec/ 1000);
+static void __result_cb(media_request_result_s *result, void *user_data)
+{
+       if (result->result == MEDIA_REQUEST_SCAN_PARTIAL)
+               return;
 
-               printf("db updating done. Time [%lld]\n", ms_time);
-               g_main_loop_quit(mainloop);
-       }
+       printf("Done\nElapsed time: %lld msec\n", (__make_timestamp() - g_start));
+       g_main_loop_quit(g_loop);
 }
 
 static void __print_help(void)
 {
        printf("=======================================================================================\n");
        printf("\n");
-       printf("db-update [option] <directory path> \n");
-       printf("\n");
-       printf("[option]\n");
-       printf("        -r : [only directory] update all directory recursivly under <directory path>\n");
+       printf("mediadb-update {directory path} \n");
        printf("\n");
-       printf("db-update --help for check this messages.\n");
-       printf("\n");
-       printf("A file or directory must exists under %s or %s.\n",
-               tzplatform_getenv(TZ_USER_CONTENT),
-               tzplatform_getenv(TZ_SYS_STORAGE));
+       printf("\tdirectory path: Under %s or %s\n", tzplatform_getenv(TZ_USER_CONTENT), tzplatform_getenv(TZ_SYS_STORAGE));
        printf("\n");
        printf("=======================================================================================\n");
 }
 
-static void __check_media_db(void)
+static void __check_media_db(uid_t uid)
 {
        void *funcHandle = NULL;
        sqlite3 *db_handle = NULL;
-       int ret = 0;
 
        funcHandle = dlopen(PATH_PLUGIN_LIB, RTLD_LAZY);
        if (funcHandle == NULL) {
@@ -86,156 +78,90 @@ static void __check_media_db(void)
                return;
        }
 
-       ret = media_db_connect(&db_handle, tzplatform_getuid(TZ_USER_NAME), false);
-       if (ret < 0)
+       if (media_db_connect(&db_handle, uid, false) < 0)
                printf("Error svc_connect\n");
 
-       ret = svc_check_db(db_handle, tzplatform_getuid(TZ_USER_NAME));
-       if (ret < 0)
+       if (svc_check_db(db_handle, uid) < 0)
                printf("Error svc_check_db\n");
 
        media_db_disconnect(db_handle);
-
        printf("Check media db done\n");
 
        dlclose(funcHandle);
 }
 
-static int __get_storage_id(const char *path, char *storage_id, uid_t uid)
+static char * __get_storage_id(const char *path, uid_t uid)
 {
-       void *funcHandle = NULL;
-       sqlite3 *db_handle = NULL;
-       int ret = 0;
+       sqlite3 *handle = NULL;
+       sqlite3_stmt *stmt = NULL;
+       gchar *storage_id = NULL;
 
-       if (strncmp(tzplatform_getenv(TZ_USER_CONTENT), path, strlen(tzplatform_getenv(TZ_USER_CONTENT))) != 0 && strncmp(tzplatform_getenv(TZ_SYS_STORAGE), path, strlen(tzplatform_getenv(TZ_SYS_STORAGE))) != 0) {
-               printf("Not support path[%s]\n", path);
-               return -1;
-       }
-
-       funcHandle = dlopen(PATH_PLUGIN_LIB, RTLD_LAZY);
-       if (funcHandle == NULL) {
-               printf("Error when open plug-in\n");
-               return -1;
-       }
-
-       svc_get_storage_id = dlsym(funcHandle, "get_storage_id");
-       if (svc_get_storage_id == NULL) {
-               printf("find get_storage_id failed\n");
-               dlclose(funcHandle);
-               return -1;
-       }
+       if (g_str_has_prefix(path, tzplatform_getenv(TZ_USER_CONTENT)))
+               return g_strdup("media");
 
-       ret = media_db_connect(&db_handle, uid, false);
-       if (ret < 0) {
+       if (media_db_connect(&handle, uid, false) < 0) {
                printf("Error svc_connect\n");
-               dlclose(funcHandle);
-               return -1;
+               return NULL;
        }
 
-       ret = svc_get_storage_id(db_handle, path, storage_id, tzplatform_getuid(TZ_USER_NAME));
-       if (ret < 0) {
-               printf("Error svc_get_storage_id\n");
-               dlclose(funcHandle);
-               return -1;
+       char *q = sqlite3_mprintf("SELECT storage_id FROM storage WHERE instr(%Q, storage_path)", path);
+       if (media_db_get_result(handle, q, &stmt) < 0) {
+               printf("Failed to get storage id\n");
+               return NULL;
        }
-       media_db_disconnect(db_handle);
-       printf("Start Scanning for [%s][%s]\n", path, storage_id);
 
-       dlclose(funcHandle);
+       if (sqlite3_step(stmt) == SQLITE_ROW)
+               storage_id = g_strdup((const char *)sqlite3_column_text(stmt, 0));
 
-       return 0;
+       sqlite3_finalize(stmt);
+       media_db_disconnect(handle);
+
+       return storage_id;
 }
 
-int dir_scan(const char *path, bool is_recursive)
+static int __dir_scan(const char *path, uid_t uid)
 {
-       int ret = 0;
-       char storage_id[36+1] = {0,};
-
-       ret = __get_storage_id(path, storage_id, tzplatform_getuid(TZ_USER_NAME));
-       if (ret < 0)
+       g_autofree gchar *storage_id = __get_storage_id(path, uid);
+       if (!storage_id) {
+               printf("storage_id is NULL\n");
                return -1;
+       }
 
-       gettimeofday(&start_time, NULL);
-       return media_directory_scanning_async(path, storage_id, is_recursive, callback, NULL, tzplatform_getuid(TZ_USER_NAME));
+       printf("Updating [%s][%s]\n", path, storage_id);
+       g_start = __make_timestamp();
+       return media_directory_scanning_async(path, storage_id, true, __result_cb, NULL, uid);
 }
 
 int main(int argc, char **argv)
 {
-       int ret;
-       int len;
-       char *argv1 = NULL;
-       char *argv2 = NULL;
-       char *req_path = NULL;
+       g_autofree gchar *path = NULL;
+       uid_t uid = tzplatform_getuid(TZ_USER_NAME);
 
-       if (argc > 3 || argc < 2) {
+       if (argc != 2) {
                __print_help();
                exit(0);
        }
 
-       argv1 = argv[1];
-
-       mainloop = g_main_loop_new(NULL, FALSE);
-
-       if (argc == 2) {
-               if (strcmp(argv1 , "--help") == 0) {
-                       __print_help();
-                       exit(0);
-               }
-
-               if (strcmp(argv1 , "check_db") == 0) {
-                       __check_media_db();
-                       exit(0);
-               }
-
-               if (g_file_test(argv1, G_FILE_TEST_IS_DIR)) {
-                       len = strlen(argv1);
-                       if (argv1[len - 1] == '/')
-                               req_path = g_strndup(argv1, len - 1);
-                       else
-                               req_path = g_strdup(argv1);
-
-                       ret = dir_scan(req_path, false);
-                       g_free(req_path);
-
-                       if (ret != 0) {
-                               printf("error : %d\n", ret);
-                               exit(0);
-                       }
-               } else {
-                       printf("[%d]invalid path\n", __LINE__);
-                       __print_help();
-                       exit(0);
-               }
-       } else if (argc == 3) {
-               argv2 = argv[2];
-               if (strcmp(argv1, "-r") == 0) {
-                       if ((argv2 != NULL) && (g_file_test(argv2, G_FILE_TEST_IS_DIR))) {
-                               len = strlen(argv2);
-                               if (argv2[len - 1] == '/')
-                                       req_path = g_strndup(argv2, len - 1);
-                               else
-                                       req_path = g_strdup(argv2);
-
-                               ret = dir_scan(req_path, true);
-                               g_free(req_path);
-
-                               if (ret != 0) {
-                                       printf("error : %d\n", ret);
-                                       exit(0);
-                               }
-                       } else {
-                               printf("[%d]invalid path\n", __LINE__);
-                               __print_help();
-                               exit(0);
-                       }
-               } else {
-                       printf("[%d] invalid option\n", __LINE__);
-                       __print_help();
-                       exit(0);
-               }
+       if (strcmp(argv[1] , "check_db") == 0) {
+               __check_media_db(uid);
+               exit(0);
+       }
+
+       if (!g_file_test(argv[1], G_FILE_TEST_IS_DIR)) {
+               printf("Invalid path\n");
+               __print_help();
+               exit(0);
+       }
+
+       path = g_canonicalize_filename(argv[1], NULL);
+       if (__dir_scan(path, uid) != 0) {
+               printf("Fail to scan directory\n");
+               exit(0);
        }
 
-       g_main_loop_run(mainloop);
+       g_loop = g_main_loop_new(NULL, FALSE);
+       g_main_loop_run(g_loop);
+       g_main_loop_unref(g_loop);
 
        exit(0);
 }