From 0acc3112535f0c2249e38dba38067d92767df221 Mon Sep 17 00:00:00 2001 From: Minje Ahn Date: Mon, 2 May 2022 11:28:59 +0900 Subject: [PATCH] Update directory scan Used dirent instead GDir to reduce the frequency of stat() calls. Change-Id: I835382ceaaa5440d0924da1dc2241cf949231bb7 Signed-off-by: Minje Ahn --- packaging/media-server.spec | 2 +- src/common/include/media-common-utils.h | 2 +- src/common/media-common-utils.c | 10 +++++++ src/scanner/media-scanner-scan.c | 52 +++++++++++++++------------------ 4 files changed, 36 insertions(+), 30 deletions(-) diff --git a/packaging/media-server.spec b/packaging/media-server.spec index ebd4eb9..68bbd35 100755 --- a/packaging/media-server.spec +++ b/packaging/media-server.spec @@ -1,6 +1,6 @@ Name: media-server Summary: A server for media content management -Version: 0.4.22 +Version: 0.4.23 Release: 0 Group: Multimedia/Service License: Apache-2.0 diff --git a/src/common/include/media-common-utils.h b/src/common/include/media-common-utils.h index 9b888f6..c96b053 100755 --- a/src/common/include/media-common-utils.h +++ b/src/common/include/media-common-utils.h @@ -31,6 +31,7 @@ bool ms_config_set_int(const char *key, int value); bool ms_config_get_str(const char *key, char **value); int ms_get_remain_space(double *free_space); +bool ms_is_valid_symlink(const char *path); int ms_check_file_path(const char *file_path, uid_t uid); int ms_check_ignore_dir(const char *full_path, uid_t uid); int ms_check_scan_ignore(char * path, uid_t uid); @@ -45,4 +46,3 @@ int ms_set_vip_process(void); #endif #endif/*_MEDIA_SERVER_UTILS_H__*/ - diff --git a/src/common/media-common-utils.c b/src/common/media-common-utils.c index 66d1616..0af71d5 100644 --- a/src/common/media-common-utils.c +++ b/src/common/media-common-utils.c @@ -130,6 +130,16 @@ bool ms_is_support_pvr(void) } #endif +bool ms_is_valid_symlink(const char *path) +{ + g_autofree char *real_path = realpath(path, NULL); + + if (!real_path) + return false; + + return (g_strcmp0(real_path, MEDIA_SHARE_PATH) == 0); +} + int ms_check_file_path(const char *file_path, uid_t uid) { ms_user_storage_type_e storage_type = -1; diff --git a/src/scanner/media-scanner-scan.c b/src/scanner/media-scanner-scan.c index 05af954..f4a6bcd 100644 --- a/src/scanner/media-scanner-scan.c +++ b/src/scanner/media-scanner-scan.c @@ -20,6 +20,7 @@ */ #include +#include #include "media-util.h" #include "media-server-ipc.h" @@ -48,12 +49,11 @@ static bool __msc_is_power_off(void) static int __msc_dir_scan(sqlite3 *handle, const char *storage_id, char *start_path, bool check_exists, bool is_recursive, uid_t uid) { int ret = MS_MEDIA_ERR_NONE; - GDir *dir = NULL; - GError *error = NULL; - const char *name; + DIR *dir = NULL; + struct dirent64 *dir_entry = NULL; GQueue *dir_queue = NULL; char *current_path = NULL; - char *path = NULL; + char path[MS_FILE_PATH_LEN_MAX] = {0, }; int (*scan_function)(sqlite3 *, const char*, const char*, uid_t) = NULL; dir_queue = g_queue_new(); @@ -74,64 +74,60 @@ static int __msc_dir_scan(sqlite3 *handle, const char *storage_id, char *start_p continue; } - dir = g_dir_open(current_path, 0, &error); - if (error != NULL) { - MS_DBG_ERR("g_dir_open fails [%s]", current_path); + dir = opendir(current_path); + if (!dir) { + MS_DBG_SERR("opendir fails [%s]", current_path); MS_SAFE_FREE(current_path); - g_error_free(error); - error = NULL; continue; } if (ms_insert_folder(handle, storage_id, current_path, uid) != MS_MEDIA_ERR_NONE) MS_DBG_ERR("insert folder failed"); - while ((name = g_dir_read_name(dir))) { + while ((dir_entry = readdir64(dir))) { if (__msc_is_power_off()) { ret = MS_MEDIA_ERR_SCANNER_FORCE_STOP; break; } - if (name[0] == '.') + if (dir_entry->d_name[0] == '.') continue; - path = g_build_path(G_DIR_SEPARATOR_S, current_path, name, NULL); - - if (g_file_test(path, G_FILE_TEST_IS_REGULAR)) { - /* Check symbolic link file */ - if (g_file_test(path, G_FILE_TEST_IS_SYMLINK)) { - MS_DBG_WARN("Symbolic link.. Skip this file"); - MS_SAFE_FREE(path); - continue; - } + snprintf(path, MS_FILE_PATH_LEN_MAX, "%s/%s", current_path, dir_entry->d_name); + switch (dir_entry->d_type) { + case DT_REG: /* Check content.scanning.others feature */ if (!ms_check_support_media_type(path)) { MS_DBG("Unsupported media type"); - MS_SAFE_FREE(path); continue; } if (scan_function(handle, storage_id, path, uid) != MS_MEDIA_ERR_NONE) MS_DBG_ERR("failed to update db"); + break; + case DT_LNK: + if (!ms_is_valid_symlink(path)) + break; - MS_SAFE_FREE(path); - } else { + // fall-through + case DT_DIR: if (is_recursive) { - g_queue_push_tail(dir_queue, path); + g_queue_push_tail(dir_queue, g_strdup(path)); } else { ret = ms_insert_folder(handle, storage_id, path, uid); if (ret != MS_MEDIA_ERR_NONE) MS_DBG_ERR("ms_insert_folder failed"); - MS_SAFE_FREE(path); } + break; + default: + break; } } MS_SAFE_FREE(current_path); - - if (dir) - g_dir_close(dir); + closedir(dir); + dir = NULL; } g_queue_free_full(dir_queue, g_free); -- 2.7.4