From 9653e75353718d1df65c7eb749dd9ad1052f5898 Mon Sep 17 00:00:00 2001 From: Jean-Benoit MARTIN Date: Tue, 26 Aug 2014 16:09:37 +0200 Subject: [PATCH] Multiuser feature Create a database peer user Use User UID to write or read into database Use Unix socket Fix-Tizen: TC-1482 Change-Id: I07b9ee1e2444ef117bc3f476573a0d6b912ae233 Signed-off-by: Jean-Benoit MARTIN --- Makefile.am | 6 +- configure.ac | 9 +- lib/include/media-server-ipc.h | 2 + lib/include/media-util-db.h | 12 +- lib/include/media-util-register.h | 6 +- lib/include/media-util.h | 4 +- lib/media-util-db.c | 271 +++++++++++++++++++++++------ lib/media-util-ipc.c | 137 ++++++++++++--- lib/media-util-register.c | 78 +++++++-- packaging/media-server.spec | 21 ++- src/common/include/media-common-utils.h | 2 +- src/common/media-common-utils.c | 53 +++++- src/mediadb-update.c | 6 +- src/scanner/include/media-scanner-db-svc.h | 44 ++--- src/scanner/media-scanner-db-svc.c | 54 +++--- src/scanner/media-scanner-scan.c | 58 +++--- src/scanner/media-scanner-socket.c | 3 +- src/server/include/media-server-db-svc.h | 8 +- src/server/media-server-db-svc.c | 10 +- src/server/media-server-db.c | 11 -- src/server/media-server-main.c | 29 ++- src/server/media-server-socket.c | 16 +- src/server/media-server-thumb.c | 3 +- 23 files changed, 613 insertions(+), 230 deletions(-) diff --git a/Makefile.am b/Makefile.am index d068e30..e8b55b1 100755 --- a/Makefile.am +++ b/Makefile.am @@ -39,7 +39,8 @@ libmedia_utils_la_CFLAGS = -I${srcdir}/lib/include \ $(SQLITE3_CFLAGS) \ $(DB_UTIL_CFLAGS) \ $(PHONESTATUS_CFLAGS) \ - $(TZ_PLATFORM_CONFIG_CFLAGS) + $(TZ_PLATFORM_CONFIG_CFLAGS) \ + $(LIBSMACK_CFLAGS) libmedia_utils_la_LIBADD = $(GLIB_LIBS) \ $(DLOG_LIBS) \ @@ -47,7 +48,8 @@ libmedia_utils_la_LIBADD = $(GLIB_LIBS) \ $(SQLITE3_LIBS) \ $(DB_UTIL_LIBS) \ $(PHONESTATUS_LIBS) \ - $(TZ_PLATFORM_CONFIG_LIBS) + $(TZ_PLATFORM_CONFIG_LIBS) \ + $(LIBSMACK_LIBS) ### file-manager-server ### diff --git a/configure.ac b/configure.ac index fa4630c..12282de 100755 --- a/configure.ac +++ b/configure.ac @@ -36,8 +36,8 @@ fi CPPFLAGS="${CPPFLAGS} -DRND_LINUX" #using UDS socket -#CPPFLAGS="${CPPFLAGS} -D_USE_UDS_SOCKET_" -CPPFLAGS="${CPPFLAGS} -D_USE_UDS_SOCKET_TCP_" +CPPFLAGS="${CPPFLAGS} -D_USE_UDS_SOCKET_" +#CPPFLAGS="${CPPFLAGS} -D_USE_UDS_SOCKET_TCP_" # FMS_DEBUG - File Manager Service debug options # To open debug options: @@ -107,6 +107,11 @@ PKG_CHECK_MODULES(TZ_PLATFORM_CONFIG, libtzplatform-config) AC_SUBST(TZ_PLATFORM_CONFIG_CFLAGS) AC_SUBST(TZ_PLATFORM_CONFIG_LIBS) +#smack lib +PKG_CHECK_MODULES(LIBSMACK, libsmack) +AC_SUBST(LIBSMACK_CFLAGS) +AC_SUBST(LIBSMACK_LIBS) + #ticker noti library PKG_CHECK_MODULES(STATUS, notification) AC_SUBST(STATUS_CFLAGS) diff --git a/lib/include/media-server-ipc.h b/lib/include/media-server-ipc.h index 1cfeb76..4badb1e 100755 --- a/lib/include/media-server-ipc.h +++ b/lib/include/media-server-ipc.h @@ -91,6 +91,7 @@ typedef struct { ms_msg_type_e msg_type; int pid; + uid_t uid; int result; size_t msg_size; /*this is size of message below and this does not include the terminationg null byte ('\0'). */ char msg[MAX_MSG_SIZE]; @@ -115,6 +116,7 @@ typedef struct _thumbMsg{ int thumb_type; int status; int pid; + uid_t uid; int thumb_size; int thumb_width; int thumb_height; diff --git a/lib/include/media-util-db.h b/lib/include/media-util-db.h index 435f74a..dae1811 100755 --- a/lib/include/media-util-db.h +++ b/lib/include/media-util-db.h @@ -36,19 +36,19 @@ extern "C" { typedef void MediaDBHandle; /**< Handle */ -int media_db_connect(MediaDBHandle **handle); +int media_db_connect(MediaDBHandle **handle, uid_t uid); int media_db_disconnect(MediaDBHandle *handle); -int media_db_request_update_db(const char *query_str); +int media_db_request_update_db(const char *query_str, uid_t uid); -int media_db_request_update_db_batch_start(const char *query_str); +int media_db_request_update_db_batch_start(const char *query_str, uid_t uid); -int media_db_request_update_db_batch(const char *query_str); +int media_db_request_update_db_batch(const char *query_str, uid_t uid); -int media_db_request_update_db_batch_end(const char *query_str); +int media_db_request_update_db_batch_end(const char *query_str, uid_t uid); -int media_db_request_directory_scan(const char *directory_path); +int media_db_request_directory_scan(const char *directory_path , uid_t uid); /** * @} diff --git a/lib/include/media-util-register.h b/lib/include/media-util-register.h index cc6e16e..d94c3fd 100755 --- a/lib/include/media-util-register.h +++ b/lib/include/media-util-register.h @@ -54,11 +54,11 @@ typedef struct typedef void (*scan_complete_cb)(media_request_result_s *, void *); typedef void (*insert_complete_cb)(media_request_result_s *, void *); -int media_directory_scanning_async(const char *directory_path, bool recursive_on, scan_complete_cb user_callback, void *user_data); +int media_directory_scanning_async(const char *directory_path, bool recursive_on, scan_complete_cb user_callback, void *user_data, uid_t uid); -int media_files_register(const char *list_path, insert_complete_cb user_callback, void *user_data); +int media_files_register(const char *list_path, insert_complete_cb user_callback, void *user_data, uid_t uid); -int media_burstshot_register(const char *list_path, insert_complete_cb user_callback, void *user_data); +int media_burstshot_register(const char *list_path, insert_complete_cb user_callback, void *user_data, uid_t uid); /** * @} diff --git a/lib/include/media-util.h b/lib/include/media-util.h index e672b4b..a4d326b 100755 --- a/lib/include/media-util.h +++ b/lib/include/media-util.h @@ -30,9 +30,9 @@ #include -#define MEDIA_ROOT_PATH_INTERNAL tzplatform_mkpath(TZ_SYS_DATA,"data-media") +#define MEDIA_ROOT_PATH_INTERNAL tzplatform_getenv(TZ_USER_HOME) #define MEDIA_ROOT_PATH_SDCARD tzplatform_mkpath(TZ_SYS_STORAGE, "sdcard") #define MEDIA_DATA_PATH tzplatform_mkpath(TZ_SYS_DATA, "file-manager-service") -#define MEDIA_DB_NAME tzplatform_mkpath(TZ_SYS_DB, ".media.db") /**< media db name*/ +#define MEDIA_DB_NAME tzplatform_mkpath(TZ_USER_DB, ".media.db") /**< media db name*/ #endif /*_MEDIA_UTIL_H_*/ diff --git a/lib/media-util-db.c b/lib/media-util-db.c index 63c9304..e50ad2e 100755 --- a/lib/media-util-db.c +++ b/lib/media-util-db.c @@ -33,18 +33,48 @@ #include #include #include +#include +#include +#include #include "media-server-ipc.h" #include "media-util-dbg.h" #include "media-util-internal.h" #include "media-util.h" +#define GLOBAL_USER 0 //#define tzplatform_getenv(TZ_GLOBAL) //TODO +#define BUFSIZE 4096 + +#define QUERY_ATTACH "attach database '%s' as Global" +#define QUERY_CREATE_VIEW_ALBUM "CREATE temp VIEW album as select distinct * from (select * from main.album m union select * from Global.album g)" +#define QUERY_CREATE_VIEW_FOLDER "CREATE temp VIEW folder as select distinct * from (select * from main.folder m union select * from Global.folder g)" +#define QUERY_CREATE_VIEW_PLAYLIST "CREATE temp VIEW playlist as select distinct * from (select * from main.playlist m union select * from Global.playlist g)" +#define QUERY_CREATE_VIEW_PLAYLIST_VIEW "CREATE temp VIEW playlist_view as select distinct * from (select * from main.playlist_view m union select * from Global.playlist_view g)" +#define QUERY_CREATE_VIEW_TAG_MAP "CREATE temp VIEW tag_map as select distinct * from (select * from main.tag_map m union select * from Global.tag_map g)" +#define QUERY_CREATE_VIEW_BOOKMARK "CREATE temp VIEW bookmark as select distinct * from (select * from main.bookmark m union select * from Global.bookmark g)" +#define QUERY_CREATE_VIEW_MEDIA "CREATE temp VIEW media as select distinct * from (select * from main.media m union select * from Global.media g)" +#define QUERY_CREATE_VIEW_PLAYLIST_MAP "CREATE temp VIEW playlist_map as select distinct * from (select * from main.playlist_map m union select * from Global.playlist_map g)" +#define QUERY_CREATE_VIEW_TAG "CREATE temp VIEW tag as select distinct * from (select * from main.tag m union select * from Global.tag g)" +#define QUERY_CREATE_VIEW_TAG_VIEW "CREATE temp VIEW tag_view as select distinct * from (select * from main.tag_view m union select * from Global.tag_view g)" + +#define QUERY_DETACH "detach database Global" +#define QUERY_DROP_VIEW_ALBUM "DROP VIEW album" +#define QUERY_DROP_VIEW_FOLDER "DROP VIEW folder" +#define QUERY_DROP_VIEW_PLAYLIST "DROP VIEW playlist" +#define QUERY_DROP_VIEW_PLAYLIST_VIEW "DROP VIEW playlist_view" +#define QUERY_DROP_VIEW_TAG_MAP "DROP VIEW tag_map" +#define QUERY_DROP_VIEW_BOOKMARK "DROP VIEW bookmark" +#define QUERY_DROP_VIEW_MEDIA "DROP VIEW media" +#define QUERY_DROP_VIEW_PLAYLIST_MAP "DROP VIEW playlist_map" +#define QUERY_DROP_VIEW_TAG "DROP VIEW tag" +#define QUERY_DROP_VIEW_TAG_VIEW "DROP VIEW tag_view" + static __thread char **sql_list = NULL; static __thread int g_list_idx = 0; static int __media_db_busy_handler(void *pData, int count); -static int __media_db_connect_db_with_handle(sqlite3 **db_handle); +static int __media_db_connect_db_with_handle(sqlite3 **db_handle, uid_t uid); static int __media_db_disconnect_db_with_handle(sqlite3 *db_handle); -static int __media_db_request_update(ms_msg_type_e msg_type, const char *request_msg); +static int __media_db_request_update(ms_msg_type_e msg_type, const char *request_msg, uid_t uid); void __media_db_destroy_sql_list() { @@ -101,21 +131,177 @@ int _xsystem(const char *argv[]) } return WEXITSTATUS(status); } -#define SCRIPT_INIT_DB "/usr/bin/media-data-sdk_create_db.sh" -static int __media_db_connect_db_with_handle(sqlite3 **db_handle) + +static char* __media_get_media_DB(uid_t uid) +{ + char *result_psswd = NULL; + struct group *grpinfo = NULL; + char * dir = NULL; + if(uid == getuid()) + { + result_psswd = strdup(MEDIA_DB_NAME); + grpinfo = getgrnam("users"); + if(grpinfo == NULL) { + MSAPI_DBG_ERR("getgrnam(users) returns NULL !"); + return NULL; + } + } + else + { + struct passwd *userinfo = getpwuid(uid); + if(userinfo == NULL) { + MSAPI_DBG_ERR("getpwuid(%d) returns NULL !", uid); + return NULL; + } + grpinfo = getgrnam("users"); + if(grpinfo == NULL) { + MSAPI_DBG_ERR("getgrnam(users) returns NULL !"); + return NULL; + } + // Compare git_t type and not group name + if (grpinfo->gr_gid != userinfo->pw_gid) { + MSAPI_DBG_ERR("UID [%d] does not belong to 'users' group!", uid); + return NULL; + } + asprintf(&result_psswd, "%s/.applications/dbspace/.media.db", userinfo->pw_dir); + } + + dir = strrchr(result_psswd, '/'); + if(!dir) + return result_psswd; + + //Control if db exist create otherwise + if(access(dir + 1, F_OK)) { + int ret; + mkdir(dir + 1, S_IRWXU | S_IRGRP | S_IXGRP | S_IXOTH); + ret = chown(dir + 1, uid, grpinfo->gr_gid); + if (ret == -1) { + char buf[BUFSIZE]; + strerror_r(errno, buf, sizeof(buf)); + MSAPI_DBG_ERR("FAIL : chown %s %d.%d, because %s", dir + 1, uid, grpinfo->gr_gid, buf); + } + } + + return result_psswd; +} + +static int __media_db_update_directly(sqlite3 *db_handle, const char *sql_str) { int ret = MS_MEDIA_ERR_NONE; + char *zErrMsg = NULL; + MSAPI_DBG_INFO("SQL = [%s]", sql_str); + + ret = sqlite3_exec(db_handle, sql_str, NULL, NULL, &zErrMsg); + + if (SQLITE_OK != ret) { + MSAPI_DBG_ERR("DB Update Fail SQL:%s [%s], err[%d]", sql_str, zErrMsg, ret); + if (ret == SQLITE_BUSY) + ret = MS_MEDIA_ERR_DB_BUSY_FAIL; + else + ret = MS_MEDIA_ERR_DB_UPDATE_FAIL; + } else { + MSAPI_DBG("DB Update Success"); + } + + if (zErrMsg) + sqlite3_free (zErrMsg); + + return ret; +} + +static int __media_db_connect_db_with_handle(sqlite3 **db_handle,uid_t uid) +{ + int i; + int ret = MS_MEDIA_ERR_NONE; + char *mediadb_path = NULL; + char *mediadbjournal_path = NULL; + /*Init DB*/ - struct stat sts; - /* Check if the DB exists; if not, create it and initialize it */ - ret = stat(MEDIA_DB_NAME, &sts); - if (ret == -1 && errno == ENOENT) - { - const char *argv_script[] = {"/bin/sh", SCRIPT_INIT_DB, NULL }; - ret = _xsystem(argv_script); + const char *tbls[46] = { + "CREATE TABLE IF NOT EXISTS album (album_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, artist TEXT, album_art TEXT, unique(name, artist));", + "CREATE TABLE IF NOT EXISTS bookmark (bookmark_id INTEGER PRIMARY KEY AUTOINCREMENT, media_uuid TEXT NOT NULL, marked_time INTEGER DEFAULT 0, thumbnail_path TEXT, unique(media_uuid, marked_time));", + "CREATE TABLE IF NOT EXISTS folder (folder_uuid TEXT PRIMARY KEY, path TEXT NOT NULL UNIQUE, name TEXT NOT NULL, modified_time INTEGER DEFAULT 0, storage_type INTEGER, unique(path, name, storage_type));", + "CREATE TABLE IF NOT EXISTS media (media_uuid TEXT PRIMARY KEY, path TEXT NOT NULL UNIQUE, file_name TEXT NOT NULL, media_type INTEGER, mime_type TEXT, size INTEGER DEFAULT 0, added_time INTEGER DEFAULT 0, modified_time INTEGER DEFAULT 0, folder_uuid TEXT NOT NULL, thumbnail_path TEXT, title TEXT, album_id INTEGER DEFAULT 0, album TEXT, artist TEXT, genre TEXT, composer TEXT, year TEXT, recorded_date TEXT, copyright TEXT, track_num TEXT, description TEXT, bitrate INTEGER DEFAULT -1, samplerate INTEGER DEFAULT -1, channel INTEGER DEFAULT -1, duration INTEGER DEFAULT -1, longitude DOUBLE DEFAULT 0, latitude DOUBLE DEFAULT 0, altitude DOUBLE DEFAULT 0, width INTEGER DEFAULT -1, height INTEGER DEFAULT -1, datetaken TEXT, orientation INTEGER DEFAULT -1, burst_id TEXT, played_count INTEGER DEFAULT 0, last_played_time INTEGER DEFAULT 0, last_played_position INTEGER DEFAULT 0, rating INTEGER DEFAULT 0, favourite INTEGER DEFAULT 0, author TEXT, provider TEXT, content_name TEXT, category TEXT, location_tag TEXT, age_rating TEXT, keyword TEXT, is_drm INTEGER DEFAULT 0, storage_type INTEGER, validity INTEGER DEFAULT 1, unique(path, file_name) );", + "CREATE TABLE IF NOT EXISTS playlist ( playlist_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL UNIQUE, thumbnail_path TEXT );", + "CREATE TABLE IF NOT EXISTS playlist_map ( _id INTEGER PRIMARY KEY AUTOINCREMENT, playlist_id INTEGER NOT NULL, media_uuid TEXT NOT NULL, play_order INTEGER NOT NULL );", + "CREATE TABLE IF NOT EXISTS tag ( tag_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL UNIQUE );", + "CREATE TABLE IF NOT EXISTS tag_map ( _id INTEGER PRIMARY KEY AUTOINCREMENT, tag_id INTEGER NOT NULL, media_uuid TEXT NOT NULL, unique(tag_id, media_uuid) );", + "CREATE INDEX folder_folder_uuid_idx on folder (folder_uuid);", + "CREATE INDEX folder_uuid_idx on media (folder_uuid);", + "CREATE INDEX media_album_idx on media (album);", + "CREATE INDEX media_artist_idx on media (artist);", + "CREATE INDEX media_author_idx on media (author);", + "CREATE INDEX media_category_idx on media (category);", + "CREATE INDEX media_composer_idx on media (composer);", + "CREATE INDEX media_content_name_idx on media (content_name);", + "CREATE INDEX media_file_name_idx on media (file_name);", + "CREATE INDEX media_genre_idx on media (genre);", + "CREATE INDEX media_location_tag_idx on media (location_tag);", + "CREATE INDEX media_media_type_idx on media (media_type);", + "CREATE INDEX media_media_uuid_idx on media (media_uuid);", + "CREATE INDEX media_modified_time_idx on media (modified_time);", + "CREATE INDEX media_path_idx on media (path);", + "CREATE INDEX media_provider_idx on media (provider);", + "CREATE INDEX media_title_idx on media (title);", + "CREATE VIEW IF NOT EXISTS playlist_view AS " + "SELECT " + "p.playlist_id, p.name, p.thumbnail_path, media_count, pm._id as pm_id, pm.play_order, m.media_uuid, path, file_name, media_type, mime_type, size, added_time, modified_time, m.thumbnail_path, description, rating, favourite, author, provider, content_name, category, location_tag, age_rating, keyword, is_drm, storage_type, longitude, latitude, altitude, width, height, datetaken, orientation, title, album, artist, genre, composer, year, recorded_date, copyright, track_num, bitrate, duration, played_count, last_played_time, last_played_position, samplerate, channel FROM playlist AS p " + "INNER JOIN playlist_map AS pm " + "INNER JOIN media AS m " + "INNER JOIN (SELECT count(playlist_id) as media_count, playlist_id FROM playlist_map group by playlist_id) as cnt_tbl " + "ON (p.playlist_id=pm.playlist_id AND pm.media_uuid = m.media_uuid AND cnt_tbl.playlist_id=pm.playlist_id AND m.validity=1) " + "UNION " + "SELECT " + "playlist_id, name, thumbnail_path, 0, 0, -1, NULL, NULL, -1, -1, -1, -1, -1, NULL, NULL, -1, -1, NULL, NULL, NULL, NULL, NULL, NULL, NULL, -1, -1, -1, -1, -1, -1, -1, -1, -1, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, -1, -1, -1, -1, -1, -1, NULL, -1 FROM playlist " + "WHERE playlist_id " + "NOT IN (select playlist_id from playlist_map);", + "CREATE VIEW IF NOT EXISTS tag_view AS " + "SELECT " + "t.tag_id, t.name, media_count, tm._id as tm_id, m.media_uuid, path, file_name, media_type, mime_type, size, added_time, modified_time, thumbnail_path, description, rating, favourite, author, provider, content_name, category, location_tag, age_rating, keyword, is_drm, storage_type, longitude, latitude, altitude, width, height, datetaken, orientation, title, album, artist, genre, composer, year, recorded_date, copyright, track_num, bitrate, duration, played_count, last_played_time, last_played_position, samplerate, channel FROM tag AS t " + "INNER JOIN tag_map AS tm " + "INNER JOIN media AS m " + "INNER JOIN (SELECT count(tag_id) as media_count, tag_id FROM tag_map group by tag_id) as cnt_tbl " + "ON (t.tag_id=tm.tag_id AND tm.media_uuid = m.media_uuid AND cnt_tbl.tag_id=tm.tag_id AND m.validity=1)" + "UNION " + "SELECT " + "tag_id, name, 0, 0, NULL, NULL, -1, -1, -1, -1, -1, NULL, NULL, -1, -1, NULL, NULL, NULL, NULL, NULL, NULL, NULL, -1, -1, -1, -1, -1, -1, -1, -1, -1, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, -1, -1, -1, -1, -1, -1, NULL, -1 FROM tag " + "WHERE tag_id " + "NOT IN (select tag_id from tag_map); ", + "CREATE TRIGGER album_cleanup DELETE ON media BEGIN DELETE FROM album WHERE (SELECT count(*) FROM media WHERE album_id=old.album_id)=1 AND album_id=old.album_id;END;", + "CREATE TRIGGER bookmark_cleanup DELETE ON media BEGIN DELETE FROM bookmark WHERE media_uuid=old.media_uuid;END;", + "CREATE TRIGGER folder_cleanup DELETE ON media BEGIN DELETE FROM folder WHERE (SELECT count(*) FROM media WHERE folder_uuid=old.folder_uuid)=1 AND folder_uuid=old.folder_uuid;END;", + "CREATE TRIGGER playlist_map_cleanup DELETE ON media BEGIN DELETE FROM playlist_map WHERE media_uuid=old.media_uuid;END;", + "CREATE TRIGGER playlist_map_cleanup_1 DELETE ON playlist BEGIN DELETE FROM playlist_map WHERE playlist_id=old.playlist_id;END;", + "CREATE TRIGGER tag_map_cleanup DELETE ON media BEGIN DELETE FROM tag_map WHERE media_uuid=old.media_uuid;END;", + "CREATE TRIGGER tag_map_cleanup_1 DELETE ON tag BEGIN DELETE FROM tag_map WHERE tag_id=old.tag_id;END;", + NULL + }; + + mediadb_path = __media_get_media_DB(uid); + + if (access(mediadb_path, F_OK)) { + if (MS_MEDIA_ERR_NONE == db_util_open_with_options(mediadb_path, db_handle, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL)) { + for (i = 0; tbls[i] != NULL; i++) { + ret = __media_db_update_directly(*db_handle, tbls[i]); + } + if(smack_setlabel(__media_get_media_DB(uid), "User", SMACK_LABEL_ACCESS)){ + MSAPI_DBG_ERR("failed chsmack -a \"User\" %s", mediadb_path); + } else { + MSAPI_DBG_ERR("chsmack -a \"User\" %s", mediadb_path); + } + asprintf(&mediadbjournal_path, "%s-journal", mediadb_path); + if(smack_setlabel(mediadbjournal_path, "User", SMACK_LABEL_ACCESS)){ + MSAPI_DBG_ERR("failed chsmack -a \"User\" %s", mediadbjournal_path); + } else { + MSAPI_DBG_ERR("chsmack -a \"User\" %s", mediadbjournal_path); + } + } else { + MSAPI_DBG_ERR("Failed to create table %s\n",__media_get_media_DB(uid)); + } } - ret = db_util_open(MEDIA_DB_NAME, db_handle, DB_UTIL_REGISTER_HOOK_METHOD); + + ret = db_util_open(__media_get_media_DB(uid), db_handle, DB_UTIL_REGISTER_HOOK_METHOD); if (SQLITE_OK != ret) { MSAPI_DBG_ERR("error when db open"); @@ -162,7 +348,7 @@ static int __media_db_disconnect_db_with_handle(sqlite3 *db_handle) return MS_MEDIA_ERR_NONE; } -static int __media_db_request_update(ms_msg_type_e msg_type, const char *request_msg) +static int __media_db_request_update(ms_msg_type_e msg_type, const char *request_msg, uid_t uid) { int ret = MS_MEDIA_ERR_NONE; int request_msg_size = 0; @@ -202,6 +388,7 @@ static int __media_db_request_update(ms_msg_type_e msg_type, const char *request send_msg.msg_type = msg_type; send_msg.msg_size = request_msg_size; strncpy(send_msg.msg, request_msg, request_msg_size); + send_msg.uid = uid; /*Create Socket*/ #ifdef _USE_UDS_SOCKET_ @@ -245,7 +432,7 @@ static int __media_db_get_client_tcp_sock() } #ifdef _USE_UDS_SOCKET_ -extern char MEDIA_IPC_PATH[][50]; +extern char MEDIA_IPC_PATH[][70]; #elif defined(_USE_UDS_SOCKET_TCP_) extern char MEDIA_IPC_PATH[][50]; #endif @@ -312,7 +499,7 @@ static int __media_db_close_tcp_client_socket() return 0; } -static int __media_db_request_batch_update(ms_msg_type_e msg_type, const char *request_msg) +static int __media_db_request_batch_update(ms_msg_type_e msg_type, const char *request_msg, uid_t uid) { int ret = MS_MEDIA_ERR_NONE; int request_msg_size = 0; @@ -337,6 +524,7 @@ static int __media_db_request_batch_update(ms_msg_type_e msg_type, const char *r send_msg.msg_type = msg_type; send_msg.msg_size = request_msg_size; + send_msg.uid = uid; strncpy(send_msg.msg, request_msg, request_msg_size); sockfd = __media_db_get_client_tcp_sock(); @@ -375,39 +563,14 @@ static int __media_db_request_batch_update(ms_msg_type_e msg_type, const char *r return ret; } -static int _media_db_update_directly(sqlite3 *db_handle, const char *sql_str) -{ - int ret = MS_MEDIA_ERR_NONE; - char *zErrMsg = NULL; - - MSAPI_DBG_INFO("SQL = [%s]", sql_str); - - ret = sqlite3_exec(db_handle, sql_str, NULL, NULL, &zErrMsg); - - if (SQLITE_OK != ret) { - MSAPI_DBG_ERR("DB Update Fail SQL:%s [%s], err[%d]", sql_str, zErrMsg, ret); - if (ret == SQLITE_BUSY) - ret = MS_MEDIA_ERR_DB_BUSY_FAIL; - else - ret = MS_MEDIA_ERR_DB_UPDATE_FAIL; - } else { - MSAPI_DBG("DB Update Success"); - } - - if (zErrMsg) - sqlite3_free (zErrMsg); - - return ret; -} - -int media_db_connect(MediaDBHandle **handle) +int media_db_connect(MediaDBHandle **handle, uid_t uid) { int ret = MS_MEDIA_ERR_NONE; sqlite3 * db_handle = NULL; MSAPI_DBG_FUNC(); - ret = __media_db_connect_db_with_handle(&db_handle); + ret = __media_db_connect_db_with_handle(&db_handle,uid); MSAPI_RETV_IF(ret != MS_MEDIA_ERR_NONE, ret); *handle = db_handle; @@ -425,7 +588,7 @@ int media_db_disconnect(MediaDBHandle *handle) return __media_db_disconnect_db_with_handle(db_handle); } -int media_db_request_update_db(const char *query_str) +int media_db_request_update_db(const char *query_str, uid_t uid) { int ret = MS_MEDIA_ERR_NONE; @@ -433,12 +596,12 @@ int media_db_request_update_db(const char *query_str) MSAPI_RETVM_IF(!MS_STRING_VALID(query_str), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid Query"); - ret = __media_db_request_update(MS_MSG_DB_UPDATE, query_str); + ret = __media_db_request_update(MS_MSG_DB_UPDATE, query_str ,uid); return ret; } -int media_db_request_update_db_batch_start(const char *query_str) +int media_db_request_update_db_batch_start(const char *query_str, uid_t uid) { int ret = MS_MEDIA_ERR_NONE; @@ -454,12 +617,12 @@ int media_db_request_update_db_batch_start(const char *query_str) return ret; } - ret = __media_db_request_batch_update(MS_MSG_DB_UPDATE_BATCH_START, query_str); + ret = __media_db_request_batch_update(MS_MSG_DB_UPDATE_BATCH_START, query_str, uid); return ret; } -int media_db_request_update_db_batch(const char *query_str) +int media_db_request_update_db_batch(const char *query_str, uid_t uid) { int ret = MS_MEDIA_ERR_NONE; @@ -467,12 +630,12 @@ int media_db_request_update_db_batch(const char *query_str) MSAPI_RETVM_IF(!MS_STRING_VALID(query_str), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid Query"); - ret = __media_db_request_batch_update(MS_MSG_DB_UPDATE_BATCH, query_str); + ret = __media_db_request_batch_update(MS_MSG_DB_UPDATE_BATCH, query_str, uid); return ret; } -int media_db_request_update_db_batch_end(const char *query_str) +int media_db_request_update_db_batch_end(const char *query_str, uid_t uid) { int ret = MS_MEDIA_ERR_NONE; @@ -484,20 +647,20 @@ int media_db_request_update_db_batch_end(const char *query_str) return ret; } - ret = __media_db_request_batch_update(MS_MSG_DB_UPDATE_BATCH_END, query_str); + ret = __media_db_request_batch_update(MS_MSG_DB_UPDATE_BATCH_END, query_str, uid); __media_db_close_tcp_client_socket(); return ret; } -int media_db_request_directory_scan(const char *directory_path) +int media_db_request_directory_scan(const char *directory_path, uid_t uid) { int ret = MS_MEDIA_ERR_NONE; MSAPI_RETVM_IF(!MS_STRING_VALID(directory_path), MS_MEDIA_ERR_INVALID_PARAMETER, "Directory Path is NULL"); - ret = __media_db_request_update(MS_MSG_DIRECTORY_SCANNING, directory_path); + ret = __media_db_request_update(MS_MSG_DIRECTORY_SCANNING, directory_path, uid); return ret; } @@ -510,7 +673,7 @@ int media_db_update_db(MediaDBHandle *handle, const char *query_str) MSAPI_RETVM_IF(db_handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Handle is NULL"); MSAPI_RETVM_IF(!MS_STRING_VALID(query_str), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid Query"); - ret = _media_db_update_directly(db_handle, query_str); + ret = __media_db_update_directly(db_handle, query_str); return ret; } @@ -578,7 +741,7 @@ int media_db_update_db_batch_end(MediaDBHandle *handle, const char *query_str) char *current_sql = NULL; for (i = 0; i < g_list_idx; i++) { current_sql = sql_list[i]; - ret = _media_db_update_directly(db_handle, current_sql); + ret = __media_db_update_directly(db_handle, current_sql); if (ret < 0) { if (i == 0) { /* This is fail of "BEGIN" */ diff --git a/lib/media-util-ipc.c b/lib/media-util-ipc.c index eb49c12..0c3374d 100755 --- a/lib/media-util-ipc.c +++ b/lib/media-util-ipc.c @@ -33,32 +33,48 @@ #include #include #include +#include +#include +#include +#include #include "media-util-dbg.h" #include "media-util.h" #ifdef _USE_UDS_SOCKET_ -char MEDIA_IPC_PATH[][50] ={ - {"/tmp/media_ipc_dbbatchupdate.dat"}, - {"/tmp/media_ipc_scandaemon.dat"}, - {"/tmp/media_ipc_scancomm.dat"}, - {"/tmp/media_ipc_scanner.dat"}, - {"/tmp/media_ipc_dbupdate.dat"}, - {"/tmp/media_ipc_thumbcreator.dat"}, - {"/tmp/media_ipc_thumbcomm.dat"}, - {"/tmp/media_ipc_thumbdaemon.dat"}, +char MEDIA_IPC_PATH[][70] ={ + {"/var/run/media-server/media_ipc_dbbatchupdate.socket"}, + {"/var/run/media-server/media_ipc_scandaemon.socket"}, + {"/var/run/media-server/media_ipc_scancomm.socket"}, + {"/var/run/media-server/media_ipc_scanner.socket"}, + {"/var/run/media-server/media_ipc_dbupdate.socket"}, + {"/var/run/media-server/media_ipc_thumbcreator.socket"}, + {"/var/run/media-server/media_ipc_thumbcomm.socket"}, + {"/var/run/media-server/media_ipc_thumbdaemon.socket"}, +}; + +char MEDIA_IPC_PATH_CLIENT[][80] ={ + {"/var/run/user/%i/media-server/media_ipc_dbbatchupdate_client%i.socket"}, + {"/var/run/user/%i/media-server/media_ipc_scandaemon_client%i.socket"}, + {"/var/run/user/%i/media-server/media_ipc_scancomm_client%i.socket"}, + {"/var/run/user/%i/media-server/media_ipc_scanner_client%i.socket"}, + {"/var/run/user/%i/media-server/media_ipc_dbupdate_client%i.socket"}, + {"/var/run/user/%i/media-server/media_ipc_thumbcreator_client%i.socket"}, + {"/var/run/user/%i/media-server/media_ipc_thumbcomm_client%i.socket"}, + {"/var/run/user/%i/media-server/media_ipc_thumbdaemon_client%i.socket"}, }; -char MEDIA_IPC_PATH_CLIENT[][50] ={ - {"/tmp/media_ipc_dbbatchupdate_client.dat"}, - {"/tmp/media_ipc_scandaemon_client.dat"}, - {"/tmp/media_ipc_scancomm_client.dat"}, - {"/tmp/media_ipc_scanner_client.dat"}, - {"/tmp/media_ipc_dbupdate_client.dat"}, - {"/tmp/media_ipc_thumbcreator_client.dat"}, - {"/tmp/media_ipc_thumbcomm_client.dat"}, - {"/tmp/media_ipc_thumbdaemon_client.dat"}, +char MEDIA_IPC_PATH_CLIENT_ROOT[][80] ={ + {"/var/run/media-server/media_ipc_dbbatchupdate_client%i.socket"}, + {"/var/run/media-server/media_ipc_scandaemon_client%i.socket"}, + {"/var/run/media-server/media_ipc_scancomm_client%i.socket"}, + {"/var/run/media-server/media_ipc_scanner_client%i.socket"}, + {"/var/run/media-server/media_ipc_dbupdate_client%i.socket"}, + {"/var/run/media-server/media_ipc_thumbcreator_client%i.socket"}, + {"/var/run/media-server/media_ipc_thumbcomm_client%i.socket"}, + {"/var/run/media-server/media_ipc_thumbdaemon_client%i.socket"}, }; + #elif defined(_USE_UDS_SOCKET_TCP_) char MEDIA_IPC_PATH[][50] ={ {"/tmp/media_ipc_dbbatchupdate.dat"}, @@ -66,6 +82,24 @@ char MEDIA_IPC_PATH[][50] ={ }; #endif +static int _mkdir(const char *dir, mode_t mode) { + char tmp[256]; + char *p = NULL; + size_t len; + + snprintf(tmp, sizeof(tmp),"%s",dir); + len = strlen(tmp); + if(tmp[len - 1] == '/') + tmp[len - 1] = 0; + for(p = tmp + 1; *p; p++) + if(*p == '/') { + *p = 0; + mkdir(tmp, mode); + *p = '/'; + } + return mkdir(tmp, mode); +} + #ifdef _USE_UDS_SOCKET_ int ms_ipc_create_client_socket(ms_protocol_e protocol, int timeout_sec, int *sock_fd, int port) #else @@ -76,6 +110,30 @@ int ms_ipc_create_client_socket(ms_protocol_e protocol, int timeout_sec, int *so struct timeval tv_timeout = { timeout_sec, 0 }; + char *buffer; + char *path; + int cx,len; + +#ifdef _USE_UDS_SOCKET_ + mode_t orig_mode; + + if (tzplatform_getuid(TZ_USER_NAME) == 0 ){ + cx = snprintf ( NULL, 0, MEDIA_IPC_PATH_CLIENT_ROOT[port],getpid()); + buffer = (char*)malloc((cx + 1 )*sizeof(char)); + snprintf ( buffer, cx + 1, MEDIA_IPC_PATH_CLIENT_ROOT[port],getpid()); + } else { + len = snprintf ( NULL, 0, "/var/run/user/%i/media-server", tzplatform_getuid(TZ_USER_NAME)); + path = (char*)malloc((len + 1 )*sizeof(char)); + snprintf ( path, len + 1, "/var/run/user/%i/media-server", tzplatform_getuid(TZ_USER_NAME)); + _mkdir(path, 0777); + free(path); + cx = snprintf ( NULL, 0, MEDIA_IPC_PATH_CLIENT[port], tzplatform_getuid(TZ_USER_NAME),getpid()); + buffer = (char*)malloc((cx + 1 )*sizeof(char)); + snprintf ( buffer, cx + 1, MEDIA_IPC_PATH_CLIENT[port],tzplatform_getuid(TZ_USER_NAME),getpid()); + } + orig_mode = umask(0111); +#endif + if(protocol == MS_PROTOCOL_UDP) { #ifdef _USE_UDS_SOCKET_ @@ -95,14 +153,18 @@ int ms_ipc_create_client_socket(ms_protocol_e protocol, int timeout_sec, int *so #ifdef _USE_UDS_SOCKET_ memset(&serv_addr, 0, sizeof(serv_addr)); serv_addr.sun_family = AF_UNIX; - MSAPI_DBG("%s", MEDIA_IPC_PATH_CLIENT[port]); - unlink(MEDIA_IPC_PATH_CLIENT[port]); - strcpy(serv_addr.sun_path, MEDIA_IPC_PATH_CLIENT[port]); - + MSAPI_DBG("%s", buffer); + unlink(buffer); + strcpy(serv_addr.sun_path, buffer); + /* Bind to the local address */ if (bind(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) { MSAPI_DBG_ERR("bind failed : %s", strerror(errno)); close(sock); +#ifdef _USE_UDS_SOCKET_ + free(buffer); + umask(orig_mode); +#endif return MS_MEDIA_ERR_SOCKET_CONN; } #endif @@ -116,6 +178,10 @@ int ms_ipc_create_client_socket(ms_protocol_e protocol, int timeout_sec, int *so if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) { #endif MSAPI_DBG_ERR("socket failed: %s", strerror(errno)); +#ifdef _USE_UDS_SOCKET_ + free(buffer); + umask(orig_mode); +#endif return MS_MEDIA_ERR_SOCKET_CONN; } } @@ -124,11 +190,19 @@ int ms_ipc_create_client_socket(ms_protocol_e protocol, int timeout_sec, int *so if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv_timeout, sizeof(tv_timeout)) == -1) { MSAPI_DBG_ERR("setsockopt failed: %s", strerror(errno)); close(sock); +#ifdef _USE_UDS_SOCKET_ + free(buffer); + umask(orig_mode); +#endif return MS_MEDIA_ERR_SOCKET_CONN; } } *sock_fd = sock; +#ifdef _USE_UDS_SOCKET_ + free(buffer); + umask(orig_mode); +#endif return MS_MEDIA_ERR_NONE; } @@ -167,7 +241,7 @@ int ms_ipc_create_server_tcp_socket(ms_protocol_e protocol, int port, int *sock_ struct sockaddr_un serv_addr; mode_t orig_mode; - orig_mode = umask(0); + orig_mode = umask(0111); /* Create a TCP socket */ if ((sock = socket(PF_FILE, SOCK_STREAM, 0)) < 0) { @@ -225,6 +299,8 @@ int ms_ipc_create_server_socket(ms_protocol_e protocol, int port, int *sock_fd) int n_reuse = 1; #ifdef _USE_UDS_SOCKET_ struct sockaddr_un serv_addr; + mode_t orig_mode; + orig_mode = umask(0111); #else struct sockaddr_in serv_addr; #endif @@ -241,6 +317,9 @@ int ms_ipc_create_server_socket(ms_protocol_e protocol, int port, int *sock_fd) if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) { #endif MSAPI_DBG_ERR("socket failed: %s", strerror(errno)); +#ifdef _USE_UDS_SOCKET_ + umask(orig_mode); +#endif return MS_MEDIA_ERR_SOCKET_CONN; } } @@ -253,6 +332,9 @@ int ms_ipc_create_server_socket(ms_protocol_e protocol, int port, int *sock_fd) if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) { #endif MSAPI_DBG_ERR("socket failed: %s", strerror(errno)); +#ifdef _USE_UDS_SOCKET_ + umask(orig_mode); +#endif return MS_MEDIA_ERR_SOCKET_CONN; } } @@ -289,6 +371,9 @@ int ms_ipc_create_server_socket(ms_protocol_e protocol, int port, int *sock_fd) if (bind_success == false) { MSAPI_DBG_ERR("bind failed : %s %d_", strerror(errno), errno); close(sock); +#ifdef _USE_UDS_SOCKET_ + umask(orig_mode); +#endif return MS_MEDIA_ERR_SOCKET_CONN; } @@ -299,6 +384,9 @@ int ms_ipc_create_server_socket(ms_protocol_e protocol, int port, int *sock_fd) if (listen(sock, SOMAXCONN) < 0) { MSAPI_DBG_ERR("listen failed : %s", strerror(errno)); close(sock); +#ifdef _USE_UDS_SOCKET_ + umask(orig_mode); +#endif return MS_MEDIA_ERR_SOCKET_CONN; } @@ -306,6 +394,9 @@ int ms_ipc_create_server_socket(ms_protocol_e protocol, int port, int *sock_fd) } *sock_fd = sock; +#ifdef _USE_UDS_SOCKET_ + umask(orig_mode); +#endif return MS_MEDIA_ERR_NONE; } diff --git a/lib/media-util-register.c b/lib/media-util-register.c index b2146cd..d1b61ab 100755 --- a/lib/media-util-register.c +++ b/lib/media-util-register.c @@ -43,6 +43,8 @@ #include #include #include +#include +#include #include "media-server-ipc.h" #include "media-util-internal.h" @@ -55,12 +57,57 @@ typedef struct media_callback_data{ void *user_data; } media_callback_data; -static bool _is_valid_path(const char *path) +#define GLOBAL_USER 0 //#define tzplatform_getenv(TZ_GLOBAL) //TODO + +static char* __media_get_path(uid_t uid) +{ + char *result_psswd = NULL; + struct group *grpinfo = NULL; + if(uid == getuid()) + { + result_psswd = strdup(MEDIA_ROOT_PATH_INTERNAL); + grpinfo = getgrnam("users"); + if(grpinfo == NULL) { + MSAPI_DBG_ERR("getgrnam(users) returns NULL !"); + return NULL; + } + } + else + { + struct passwd *userinfo = getpwuid(uid); + if(userinfo == NULL) { + MSAPI_DBG_ERR("getpwuid(%d) returns NULL !", uid); + return NULL; + } + grpinfo = getgrnam("users"); + if(grpinfo == NULL) { + MSAPI_DBG_ERR("getgrnam(users) returns NULL !"); + return NULL; + } + // Compare git_t type and not group name + if (grpinfo->gr_gid != userinfo->pw_gid) { + MSAPI_DBG_ERR("UID [%d] does not belong to 'users' group!", uid); + return NULL; + } + asprintf(&result_psswd, "%s", userinfo->pw_dir); + } + + return result_psswd; +} + +static bool _is_valid_path(const char *path, uid_t uid) { - if (path == NULL) - return false; + + int lenght_path; + char * user_path = NULL; + + if (path == NULL) + return false; + + user_path = __media_get_path(uid); + lenght_path = strlen(user_path); - if (strncmp(path, MEDIA_ROOT_PATH_INTERNAL, strlen(MEDIA_ROOT_PATH_INTERNAL)) == 0) { + if (strncmp(path, user_path, lenght_path) == 0) { return true; } else if (strncmp(path, MEDIA_ROOT_PATH_SDCARD, strlen(MEDIA_ROOT_PATH_SDCARD)) == 0) { return true; @@ -70,12 +117,12 @@ static bool _is_valid_path(const char *path) return true; } -static int _check_dir_path(const char *dir_path) +static int _check_dir_path(const char *dir_path, uid_t uid) { struct stat sb; DIR *dp = NULL; - if (!_is_valid_path(dir_path)) { + if (!_is_valid_path(dir_path,uid)) { MSAPI_DBG("Invalid path : %s", dir_path); return MS_MEDIA_ERR_INVALID_PATH; } @@ -193,7 +240,7 @@ static int _attach_callback(int *sockfd, scan_complete_cb user_callback, void *u return MS_MEDIA_ERR_NONE; } -static int __media_db_request_update_async(ms_msg_type_e msg_type, const char *request_msg, scan_complete_cb user_callback, void *user_data) +static int __media_db_request_update_async(ms_msg_type_e msg_type, const char *request_msg, scan_complete_cb user_callback, void *user_data, uid_t uid) { int ret = MS_MEDIA_ERR_NONE; int request_msg_size = 0; @@ -222,6 +269,7 @@ static int __media_db_request_update_async(ms_msg_type_e msg_type, const char *r send_msg.msg_type = msg_type; send_msg.pid = syscall(__NR_getpid); send_msg.msg_size = request_msg_size; + send_msg.uid = uid; strncpy(send_msg.msg, request_msg, request_msg_size); /*Create Socket*/ @@ -247,38 +295,38 @@ static int __media_db_request_update_async(ms_msg_type_e msg_type, const char *r } -int media_directory_scanning_async(const char *directory_path, bool recursive_on, scan_complete_cb user_callback, void *user_data) +int media_directory_scanning_async(const char *directory_path, bool recursive_on, scan_complete_cb user_callback, void *user_data, uid_t uid) { int ret; - ret = _check_dir_path(directory_path); + ret = _check_dir_path(directory_path,uid); if(ret != MS_MEDIA_ERR_NONE) return ret; if (recursive_on == TRUE) - ret = __media_db_request_update_async(MS_MSG_DIRECTORY_SCANNING, directory_path, user_callback, user_data); + ret = __media_db_request_update_async(MS_MSG_DIRECTORY_SCANNING, directory_path, user_callback, user_data, uid); else - ret = __media_db_request_update_async(MS_MSG_DIRECTORY_SCANNING_NON_RECURSIVE, directory_path, user_callback, user_data); + ret = __media_db_request_update_async(MS_MSG_DIRECTORY_SCANNING_NON_RECURSIVE, directory_path, user_callback, user_data, uid); return ret; } -int media_files_register(const char *list_path, insert_complete_cb user_callback, void *user_data) +int media_files_register(const char *list_path, insert_complete_cb user_callback, void *user_data, uid_t uid) { int ret; - ret = __media_db_request_update_async(MS_MSG_BULK_INSERT, list_path, user_callback, user_data); + ret = __media_db_request_update_async(MS_MSG_BULK_INSERT, list_path, user_callback, user_data, uid); MSAPI_DBG("client receive: %d", ret); return ret; } -int media_burstshot_register(const char *list_path, insert_complete_cb user_callback, void *user_data) +int media_burstshot_register(const char *list_path, insert_complete_cb user_callback, void *user_data, uid_t uid) { int ret; - ret = __media_db_request_update_async(MS_MSG_BURSTSHOT_INSERT, list_path, user_callback, user_data); + ret = __media_db_request_update_async(MS_MSG_BURSTSHOT_INSERT, list_path, user_callback, user_data, uid); MSAPI_DBG("client receive: %d", ret); diff --git a/packaging/media-server.spec b/packaging/media-server.spec index 2194f11..5d8f598 100755 --- a/packaging/media-server.spec +++ b/packaging/media-server.spec @@ -1,6 +1,6 @@ Name: media-server Summary: File manager service server. -Version: 0.2.46 +Version: 0.2.47 Release: 0 Group: Multimedia/Service License: Apache-2.0 @@ -22,6 +22,7 @@ BuildRequires: pkgconfig(dbus-glib-1) BuildRequires: pkgconfig(sqlite3) BuildRequires: pkgconfig(db-util) BuildRequires: pkgconfig(notification) +BuildRequires: pkgconfig(libsmack) BuildRequires: pkgconfig(libtzplatform-config) %description @@ -66,14 +67,14 @@ mkdir -p %{buildroot}%{TZ_SYS_DATA}/data-media/Downloads mkdir -p %{buildroot}%{TZ_SYS_DATA}/data-media/Images mkdir -p %{buildroot}%{TZ_SYS_DATA}/data-media/Sounds/Voice\ recorder mkdir -p %{buildroot}%{TZ_SYS_DATA}/data-media/Videos -mkdir -p %{buildroot}%{TZ_SYS_DATA}/data-media/file-manager-service/.thumb/mmc -mkdir -p %{buildroot}%{TZ_SYS_DATA}/data-media/file-manager-service/.thumb/phone +mkdir -p %{buildroot}%{TZ_SYS_DATA}/file-manager-service/.thumb/mmc +mkdir -p %{buildroot}%{TZ_SYS_DATA}/file-manager-service/.thumb/phone install -m 0775 ./data-media/Images/* %{buildroot}%{TZ_SYS_DATA}/data-media/Images/ -install -m 0775 ./data-media/dbspace/file-manager-service/.thumb/thumb_default.png %{buildroot}%{TZ_SYS_DATA}/data-media/file-manager-service/.thumb/ -install -D -m 0775 ./data-media/dbspace/file-manager-service/.thumb/mmc/* %{buildroot}%{TZ_SYS_DATA}/data-media/file-manager-service/.thumb/mmc/ -install -m 0775 ./data-media/dbspace/file-manager-service/.thumb/phone/PHONE_THUMB_HERE %{buildroot}%{TZ_SYS_DATA}/data-media/file-manager-service/.thumb/phone/ -install -m 0775 ./data-media/dbspace/file-manager-service/.thumb/phone/.jpg* %{buildroot}%{TZ_SYS_DATA}/data-media/file-manager-service/.thumb/phone/ -install -D -m 0775 ./data-media/dbspace/file-manager-service/* %{buildroot}%{TZ_SYS_DATA}/data-media/file-manager-service/ +install -m 0775 ./data-media/dbspace/file-manager-service/.thumb/thumb_default.png %{buildroot}%{TZ_SYS_DATA}/file-manager-service/.thumb/ +install -D -m 0775 ./data-media/dbspace/file-manager-service/.thumb/mmc/* %{buildroot}%{TZ_SYS_DATA}/file-manager-service/.thumb/mmc/ +install -m 0775 ./data-media/dbspace/file-manager-service/.thumb/phone/PHONE_THUMB_HERE %{buildroot}%{TZ_SYS_DATA}/file-manager-service/.thumb/phone/ +install -m 0775 ./data-media/dbspace/file-manager-service/.thumb/phone/.jpg* %{buildroot}%{TZ_SYS_DATA}/file-manager-service/.thumb/phone/ +install -D -m 0775 ./data-media/dbspace/file-manager-service/* %{buildroot}%{TZ_SYS_DATA}/file-manager-service/ install -m 0775 %{SOURCE1004} %{buildroot}%{_bindir}/media-data-sdk_create_db.sh %post @@ -84,6 +85,7 @@ vconftool set -t int file/private/mediaserver/scan_internal "1" -f vconftool set -t int file/private/mediaserver/scan_directory "1" -f chgrp %TZ_SYS_USER_GROUP %{_bindir}/media-data-sdk_create_db.sh chgrp -R %TZ_SYS_USER_GROUP %{TZ_SYS_DATA}/data-media +chgrp -R %TZ_SYS_USER_GROUP %{TZ_SYS_DATA}/file-manager-service %post -n libmedia-utils -p /sbin/ldconfig %postun -n libmedia-utils -p /sbin/ldconfig @@ -102,7 +104,8 @@ chgrp -R %TZ_SYS_USER_GROUP %{TZ_SYS_DATA}/data-media %license LICENSE.APLv2.0 %{_bindir}/media-data-sdk_create_db.sh %{TZ_SYS_DATA}/data-media/* -%{TZ_SYS_DATA}/data-media/file-manager-service/.thumb/* +%{TZ_SYS_DATA}/file-manager-service/.thumb/* +%{TZ_SYS_DATA}/file-manager-service/plugin-config %files -n libmedia-utils %manifest libmedia-utils.manifest diff --git a/src/common/include/media-common-utils.h b/src/common/include/media-common-utils.h index df2cfda..02bc3e5 100755 --- a/src/common/include/media-common-utils.h +++ b/src/common/include/media-common-utils.h @@ -49,7 +49,7 @@ void ms_end(void); ms_storage_type_t -ms_get_storage_type_by_full(const char *path); +ms_get_storage_type_by_full(const char *path, uid_t uid); int ms_get_mime(const char *path, char *mimetype); diff --git a/src/common/media-common-utils.c b/src/common/media-common-utils.c index 274dd00..9027f3d 100755 --- a/src/common/media-common-utils.c +++ b/src/common/media-common-utils.c @@ -28,9 +28,13 @@ * @brief This file implements main database operation. */ +#define _GNU_SOURCE + #include #include #include +#include +#include #include "media-util.h" #include "media-server-ipc.h" @@ -46,6 +50,7 @@ struct timeval g_mmc_end_time; #endif #define MS_DRM_CONTENT_TYPE_LENGTH 100 +#define GLOBAL_USER 0 //#define tzplatform_getenv(TZ_GLOBAL) //TODO #ifdef FMS_PERF void @@ -85,11 +90,55 @@ ms_is_mmc_inserted(void) return true; } } +static char* __media_get_path(uid_t uid) +{ + char *result_psswd = NULL; + struct group *grpinfo = NULL; + if(uid == getuid()) + { + result_psswd = strdup(MEDIA_ROOT_PATH_INTERNAL); + grpinfo = getgrnam("users"); + if(grpinfo == NULL) { + MS_DBG_ERR("getgrnam(users) returns NULL !"); + return NULL; + } + } + else + { + struct passwd *userinfo = getpwuid(uid); + if(userinfo == NULL) { + MS_DBG_ERR("getpwuid(%d) returns NULL !", uid); + return NULL; + } + grpinfo = getgrnam("users"); + if(grpinfo == NULL) { + MS_DBG_ERR("getgrnam(users) returns NULL !"); + return NULL; + } + // Compare git_t type and not group name + if (grpinfo->gr_gid != userinfo->pw_gid) { + MS_DBG_ERR("UID [%d] does not belong to 'users' group!", uid); + return NULL; + } + asprintf(&result_psswd, "%s", userinfo->pw_dir); + } + + return result_psswd; +} ms_storage_type_t -ms_get_storage_type_by_full(const char *path) +ms_get_storage_type_by_full(const char *path, uid_t uid) { - if (strncmp(path, MEDIA_ROOT_PATH_INTERNAL, strlen(MEDIA_ROOT_PATH_INTERNAL)) == 0) { + int lenght_path; + char * user_path = NULL; + + if (path == NULL) + return false; + + user_path = __media_get_path(uid); + lenght_path = strlen(user_path); + + if (strncmp(path, user_path, lenght_path) == 0) { return MS_STORAGE_INTERNAL; } else if (strncmp(path, MEDIA_ROOT_PATH_SDCARD, strlen(MEDIA_ROOT_PATH_SDCARD)) == 0) { return MS_STORAGE_EXTERNAL; diff --git a/src/mediadb-update.c b/src/mediadb-update.c index fefeaf3..8968840 100644 --- a/src/mediadb-update.c +++ b/src/mediadb-update.c @@ -53,7 +53,7 @@ void print_help() 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_mkpath(TZ_SYS_DATA,"data-media"),tzplatform_mkpath(TZ_SYS_STORAGE,"sdcard")); + printf("A file or directory must exists under %s or %s.\n",tzplatform_getenv(TZ_USER_HOME),tzplatform_mkpath(TZ_SYS_STORAGE,"sdcard")); printf("Using %s is allowed SD card is mounted.\n",tzplatform_mkpath(TZ_SYS_STORAGE,"sdcard")); printf("\n"); printf("=======================================================================================\n"); @@ -61,12 +61,12 @@ void print_help() int dir_scan_non_recursive(char *path) { - return media_directory_scanning_async(path, FALSE, callback, NULL); + return media_directory_scanning_async(path, FALSE, callback, NULL, tzplatform_getuid(TZ_USER_NAME)); } int dir_scan_recursive(char *path) { - return media_directory_scanning_async(path, TRUE, callback, NULL); + return media_directory_scanning_async(path, TRUE, callback, NULL, tzplatform_getuid(TZ_USER_NAME)); } typedef enum { diff --git a/src/scanner/include/media-scanner-db-svc.h b/src/scanner/include/media-scanner-db-svc.h index 1e0df36..b70e64b 100755 --- a/src/scanner/include/media-scanner-db-svc.h +++ b/src/scanner/include/media-scanner-db-svc.h @@ -33,23 +33,23 @@ #include "media-common-types.h" typedef int (*CHECK_ITEM)(const char*, char **); -typedef int (*CONNECT)(void**, char **); +typedef int (*CONNECT)(void**, uid_t, char **); typedef int (*DISCONNECT)(void*, char **); typedef int (*CHECK_ITEM_EXIST)(void*, const char*, int, char **); typedef int (*INSERT_ITEM_BEGIN)(void*, int, int, int, char **); -typedef int (*INSERT_ITEM_END)(void*, char **); -typedef int (*INSERT_ITEM)(void*, const char*, int, char **); -typedef int (*SET_ALL_STORAGE_ITEMS_VALIDITY)(void*, int, int, char **); +typedef int (*INSERT_ITEM_END)(void*, uid_t, char **); +typedef int (*INSERT_ITEM)(void*, const char*, int, uid_t, char **); +typedef int (*SET_ALL_STORAGE_ITEMS_VALIDITY)(void*, int, int, uid_t, char **); typedef int (*SET_ITEM_VALIDITY_BEGIN)(void*, int, char **); -typedef int (*SET_ITEM_VALIDITY_END)(void*, char **); +typedef int (*SET_ITEM_VALIDITY_END)(void*, uid_t, char **); typedef int (*SET_ITEM_VALIDITY)(void*, const char*, int, int, char **); -typedef int (*DELETE_ALL_ITEMS_IN_STORAGE)(void*, int, char **); -typedef int (*DELETE_ALL_INVALID_ITMES_IN_STORAGE)(void*, int, char **); +typedef int (*DELETE_ALL_ITEMS_IN_STORAGE)(void*, int, uid_t, char **); +typedef int (*DELETE_ALL_INVALID_ITMES_IN_STORAGE)(void*, int, uid_t, char **); typedef int (*UPDATE_BEGIN)(void); -typedef int (*UPDATE_END)(void); -typedef int (*SET_FOLDER_ITEM_VALIDITY)(void*, const char*, int, int, char**); -typedef int (*DELETE_ALL_INVALID_ITEMS_IN_FOLDER)(void*, const char*, char**); -typedef int (*INSERT_BURST_ITEM)(void *, const char *, int , char **); +typedef int (*UPDATE_END)(uid_t); +typedef int (*SET_FOLDER_ITEM_VALIDITY)(void*, const char*, int, int, uid_t, char**); +typedef int (*DELETE_ALL_INVALID_ITEMS_IN_FOLDER)(void*, const char*, uid_t, char**); +typedef int (*INSERT_BURST_ITEM)(void *, const char *, int, uid_t, char **); typedef int (*SEND_DIR_UPDATE_NOTI)(void *, const char *, char **); typedef int (*COUNT_DELETE_ITEMS_IN_FOLDER)(void *, const char *, int *, char **); @@ -60,34 +60,34 @@ void msc_unload_functions(void); int -msc_connect_db(void ***handle); +msc_connect_db(void ***handle, uid_t uid); int msc_disconnect_db(void ***handle); int -msc_validate_item(void **handle, const char *path); +msc_validate_item(void **handle, const char *path, uid_t uid); int -msc_insert_item_batch(void **handle, const char *path); +msc_insert_item_batch(void **handle, const char *path, uid_t uid); int -msc_insert_burst_item(void **handle, const char *path); +msc_insert_burst_item(void **handle, const char *path, uid_t uid); bool -msc_delete_all_items(void **handle, ms_storage_type_t store_type); +msc_delete_all_items(void **handle, ms_storage_type_t store_type, uid_t uid); int -msc_invalidate_all_items(void **handle, ms_storage_type_t store_type); +msc_invalidate_all_items(void **handle, ms_storage_type_t store_type, uid_t uid); bool -msc_delete_invalid_items(void **handle, ms_storage_type_t store_type); +msc_delete_invalid_items(void **handle, ms_storage_type_t store_type, uid_t uid); int -msc_set_folder_validity(void **handle, const char *path, int validity, int recursive); +msc_set_folder_validity(void **handle, const char *path, int validity, int recursive, uid_t uid); int -msc_delete_invalid_items_in_folder(void **handle, const char*path); +msc_delete_invalid_items_in_folder(void **handle, const char*path, uid_t uid); int msc_send_dir_update_noti(void **handle, const char*path); @@ -107,12 +107,12 @@ void msc_register_start(void **handle, ms_noti_status_e noti_status, int pid); void -msc_register_end(void **handle); +msc_register_end(void **handle, uid_t uid); void msc_validate_start(void **handle); void -msc_validate_end(void **handle); +msc_validate_end(void **handle, uid_t uid); #endif /*_MEDIA_SCANNER_DB_SVC_H_*/ diff --git a/src/scanner/media-scanner-db-svc.c b/src/scanner/media-scanner-db-svc.c index 60dd0ee..68ab521 100755 --- a/src/scanner/media-scanner-db-svc.c +++ b/src/scanner/media-scanner-db-svc.c @@ -39,7 +39,7 @@ #include -#define CONFIG_PATH tzplatform_mkpath(TZ_SYS_DATA,"data-media/file-manager-service/plugin-config") +#define CONFIG_PATH tzplatform_mkpath(TZ_SYS_DATA,"file-manager-service/plugin-config") #define EXT ".so" #define EXT_LEN 3 #define MSC_REGISTER_COUNT 100 /*For bundle commit*/ @@ -268,7 +268,7 @@ msc_unload_functions(void) } int -msc_connect_db(void ***handle) +msc_connect_db(void ***handle, uid_t uid) { int lib_index; int ret; @@ -280,7 +280,7 @@ msc_connect_db(void ***handle) MS_MALLOC(*handle, sizeof (void*) * lib_num); for (lib_index = 0; lib_index < lib_num; lib_index++) { - ret = ((CONNECT)func_array[lib_index][eCONNECT])(&((*handle)[lib_index]), &err_msg); /*dlopen*/ + ret = ((CONNECT)func_array[lib_index][eCONNECT])(&((*handle)[lib_index]), uid, &err_msg); /*dlopen*/ if (ret != 0) { MSC_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg); MS_SAFE_FREE(err_msg); @@ -321,7 +321,7 @@ msc_disconnect_db(void ***handle) } int -msc_validate_item(void **handle, const char *path) +msc_validate_item(void **handle, const char *path, uid_t uid) { int lib_index; int res = MS_MEDIA_ERR_NONE; @@ -329,7 +329,7 @@ msc_validate_item(void **handle, const char *path) char *err_msg = NULL; ms_storage_type_t storage_type; - storage_type = ms_get_storage_type_by_full(path); + storage_type = ms_get_storage_type_by_full(path,uid); for (lib_index = 0; lib_index < lib_num; lib_index++) { if (!_msc_check_category(path, lib_index)) { @@ -339,7 +339,7 @@ msc_validate_item(void **handle, const char *path) MSC_DBG_ERR("not exist in %d. insert data", lib_index); MS_SAFE_FREE(err_msg); - ret = ((INSERT_ITEM)func_array[lib_index][eINSERT_BATCH])(handle[lib_index], path, storage_type, &err_msg); /*dlopen*/ + ret = ((INSERT_ITEM)func_array[lib_index][eINSERT_BATCH])(handle[lib_index], path, storage_type, uid, &err_msg); /*dlopen*/ if (ret != 0) { MSC_DBG_ERR("error : %s [%s] %s", g_array_index(so_array, char*, lib_index), err_msg, path); MSC_DBG_ERR("[%s]", path); @@ -372,7 +372,7 @@ msc_validate_item(void **handle, const char *path) } int -msc_invalidate_all_items(void **handle, ms_storage_type_t store_type) +msc_invalidate_all_items(void **handle, ms_storage_type_t store_type , uid_t uid) { int lib_index; int res = MS_MEDIA_ERR_NONE; @@ -380,7 +380,7 @@ msc_invalidate_all_items(void **handle, ms_storage_type_t store_type) char *err_msg = NULL; for (lib_index = 0; lib_index < lib_num; lib_index++) { - ret = ((SET_ALL_STORAGE_ITEMS_VALIDITY)func_array[lib_index][eSET_ALL_VALIDITY])(handle[lib_index], store_type, false, &err_msg); /*dlopen*/ + ret = ((SET_ALL_STORAGE_ITEMS_VALIDITY)func_array[lib_index][eSET_ALL_VALIDITY])(handle[lib_index], store_type, false, uid, &err_msg); /*dlopen*/ if (ret != 0) { MSC_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg); MS_SAFE_FREE(err_msg); @@ -392,7 +392,7 @@ msc_invalidate_all_items(void **handle, ms_storage_type_t store_type) } int -msc_insert_item_batch(void **handle, const char *path) +msc_insert_item_batch(void **handle, const char *path, uid_t uid) { int lib_index; int res = MS_MEDIA_ERR_NONE; @@ -400,11 +400,11 @@ msc_insert_item_batch(void **handle, const char *path) char *err_msg = NULL; ms_storage_type_t storage_type; - storage_type = ms_get_storage_type_by_full(path); + storage_type = ms_get_storage_type_by_full(path,uid); for (lib_index = 0; lib_index < lib_num; lib_index++) { if (!_msc_check_category(path, lib_index)) { - ret = ((INSERT_ITEM)func_array[lib_index][eINSERT_BATCH])(handle[lib_index], path, storage_type, &err_msg); /*dlopen*/ + ret = ((INSERT_ITEM)func_array[lib_index][eINSERT_BATCH])(handle[lib_index], path, storage_type, uid, &err_msg); /*dlopen*/ if (ret != 0) { MSC_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg); MSC_DBG_ERR("[%s]", path); @@ -426,7 +426,7 @@ msc_insert_item_batch(void **handle, const char *path) } int -msc_insert_burst_item(void **handle, const char *path) +msc_insert_burst_item(void **handle, const char *path , uid_t uid) { int lib_index; int res = MS_MEDIA_ERR_NONE; @@ -434,11 +434,11 @@ msc_insert_burst_item(void **handle, const char *path) char *err_msg = NULL; ms_storage_type_t storage_type; - storage_type = ms_get_storage_type_by_full(path); + storage_type = ms_get_storage_type_by_full(path,uid); for (lib_index = 0; lib_index < lib_num; lib_index++) { if (!_msc_check_category(path, lib_index)) { - ret = ((INSERT_BURST_ITEM)func_array[lib_index][eINSERT_BURST])(handle[lib_index], path, storage_type, &err_msg); /*dlopen*/ + ret = ((INSERT_BURST_ITEM)func_array[lib_index][eINSERT_BURST])(handle[lib_index], uid, path, storage_type, &err_msg); /*dlopen*/ if (ret != 0) { MSC_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg); MSC_DBG_ERR("[%s]", path); @@ -460,7 +460,7 @@ msc_insert_burst_item(void **handle, const char *path) } bool -msc_delete_all_items(void **handle, ms_storage_type_t store_type) +msc_delete_all_items(void **handle, ms_storage_type_t store_type, uid_t uid) { int lib_index; int ret = 0; @@ -468,7 +468,7 @@ msc_delete_all_items(void **handle, ms_storage_type_t store_type) /* To reset media db when differnet mmc is inserted. */ for (lib_index = 0; lib_index < lib_num; lib_index++) { - ret = ((DELETE_ALL_ITEMS_IN_STORAGE)func_array[lib_index][eDELETE_ALL])(handle[lib_index], store_type, &err_msg); /*dlopen*/ + ret = ((DELETE_ALL_ITEMS_IN_STORAGE)func_array[lib_index][eDELETE_ALL])(handle[lib_index], store_type, uid, &err_msg); /*dlopen*/ if (ret != 0) { MSC_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg); MS_SAFE_FREE(err_msg); @@ -480,14 +480,14 @@ msc_delete_all_items(void **handle, ms_storage_type_t store_type) } bool -msc_delete_invalid_items(void **handle, ms_storage_type_t store_type) +msc_delete_invalid_items(void **handle, ms_storage_type_t store_type, uid_t uid) { int lib_index; int ret; char *err_msg = NULL; for (lib_index = 0; lib_index < lib_num; lib_index++) { - ret = ((DELETE_ALL_INVALID_ITMES_IN_STORAGE)func_array[lib_index][eDELETE_INVALID_ITEMS])(handle[lib_index], store_type, &err_msg); /*dlopen*/ + ret = ((DELETE_ALL_INVALID_ITMES_IN_STORAGE)func_array[lib_index][eDELETE_INVALID_ITEMS])(handle[lib_index], store_type, uid, &err_msg); /*dlopen*/ if (ret != 0) { MSC_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg); MS_SAFE_FREE(err_msg); @@ -499,14 +499,14 @@ msc_delete_invalid_items(void **handle, ms_storage_type_t store_type) } int -msc_set_folder_validity(void **handle, const char *path, int validity, int recursive) +msc_set_folder_validity(void **handle, const char *path, int validity, int recursive, uid_t uid) { int lib_index; int ret; char *err_msg = NULL; for (lib_index = 0; lib_index < lib_num; lib_index++) { - ret = ((SET_FOLDER_ITEM_VALIDITY)func_array[lib_index][eSET_FOLDER_VALIDITY])(handle[lib_index], path, validity, recursive,&err_msg); /*dlopen*/ + ret = ((SET_FOLDER_ITEM_VALIDITY)func_array[lib_index][eSET_FOLDER_VALIDITY])(handle[lib_index], path, validity, recursive, uid, &err_msg); /*dlopen*/ if (ret != 0) { MSC_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg); MS_SAFE_FREE(err_msg); @@ -518,14 +518,14 @@ msc_set_folder_validity(void **handle, const char *path, int validity, int recur } int -msc_delete_invalid_items_in_folder(void **handle, const char*path) +msc_delete_invalid_items_in_folder(void **handle, const char*path, uid_t uid) { int lib_index; int ret; char *err_msg = NULL; for (lib_index = 0; lib_index < lib_num; lib_index++) { - ret = ((DELETE_ALL_INVALID_ITEMS_IN_FOLDER)func_array[lib_index][eDELETE_FOLDER])(handle[lib_index], path, &err_msg); /*dlopen*/ + ret = ((DELETE_ALL_INVALID_ITEMS_IN_FOLDER)func_array[lib_index][eDELETE_FOLDER])(handle[lib_index], path, uid, &err_msg); /*dlopen*/ if (ret != 0) { MSC_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg); MS_SAFE_FREE(err_msg); @@ -596,14 +596,14 @@ msc_register_start(void **handle, ms_noti_status_e noti_status, int pid) } void -msc_register_end(void **handle) +msc_register_end(void **handle, uid_t uid) { int lib_index; int ret = 0; char *err_msg = NULL; for (lib_index = 0; lib_index < lib_num; lib_index++) { - ret = ((INSERT_ITEM_END)func_array[lib_index][eINSERT_END])(handle[lib_index], &err_msg);/*dlopen*/ + ret = ((INSERT_ITEM_END)func_array[lib_index][eINSERT_END])(handle[lib_index], uid, &err_msg);/*dlopen*/ if (ret != 0) { MSC_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg); MS_SAFE_FREE(err_msg); @@ -611,7 +611,7 @@ msc_register_end(void **handle) } for (lib_index = 0; lib_index < lib_num; lib_index++) { - ret = ((UPDATE_END)func_array[lib_index][eUPDATE_END])();/*dlopen*/ + ret = ((UPDATE_END)func_array[lib_index][eUPDATE_END])(uid);/*dlopen*/ if (ret != 0) { MSC_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg); MS_SAFE_FREE(err_msg); @@ -636,14 +636,14 @@ msc_validate_start(void **handle) } void -msc_validate_end(void **handle) +msc_validate_end(void **handle, uid_t uid) { int lib_index; int ret = 0; char *err_msg = NULL; for (lib_index = 0; lib_index < lib_num; lib_index++) { - ret = ((SET_ITEM_VALIDITY_END)func_array[lib_index][eSET_VALIDITY_END])(handle[lib_index], &err_msg);/*dlopen*/ + ret = ((SET_ITEM_VALIDITY_END)func_array[lib_index][eSET_VALIDITY_END])(handle[lib_index], uid, &err_msg);/*dlopen*/ if (ret != 0) { MSC_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg); MS_SAFE_FREE(err_msg); diff --git a/src/scanner/media-scanner-scan.c b/src/scanner/media-scanner-scan.c index cf9f6f2..540bac5 100755 --- a/src/scanner/media-scanner-scan.c +++ b/src/scanner/media-scanner-scan.c @@ -34,6 +34,8 @@ #include #include #include +#include +#include #include "media-util.h" #include "media-server-ipc.h" @@ -44,6 +46,8 @@ #include "media-scanner-socket.h" #include "media-scanner-scan.h" +#define GLOBAL_USER 0 //#define tzplatform_getenv(TZ_GLOBAL) //TODO + typedef struct msc_scan_data { char *name; struct msc_scan_data *next; @@ -169,7 +173,7 @@ void _msc_check_dir_path(char *dir_path) dir_path[len -1] = '\0'; } -static int _msc_dir_scan(void **handle, const char*start_path, ms_storage_type_t storage_type, int scan_type) +static int _msc_dir_scan(void **handle, const char*start_path, ms_storage_type_t storage_type, int scan_type, uid_t uid) { DIR *dp = NULL; GArray *dir_array = NULL; @@ -180,7 +184,7 @@ static int _msc_dir_scan(void **handle, const char*start_path, ms_storage_type_t char *new_path = NULL; char *current_path = NULL; char path[MS_FILE_PATH_LEN_MAX] = { 0 }; - int (*scan_function)(void **, const char*) = NULL; + int (*scan_function)(void **, const char*, uid_t) = NULL; /* make new array for storing directory */ dir_array = g_array_new (FALSE, FALSE, sizeof (char*)); @@ -235,7 +239,7 @@ static int _msc_dir_scan(void **handle, const char*start_path, ms_storage_type_t continue; } /* insert into media DB */ - if (scan_function(handle,path) != MS_MEDIA_ERR_NONE) { + if (scan_function(handle,path,uid) != MS_MEDIA_ERR_NONE) { MSC_DBG_ERR("failed to update db : %d\n", scan_type); continue; } @@ -289,7 +293,7 @@ static int _msc_db_update(void **handle, const ms_comm_msg_s * scan_data) char *start_path = NULL; ms_storage_type_t storage_type; - storage_type = ms_get_storage_type_by_full(scan_data->msg); + storage_type = ms_get_storage_type_by_full(scan_data->msg,scan_data->uid); scan_type = scan_data->msg_type; /*if scan type is not MS_SCAN_NONE, check data in db. */ @@ -298,7 +302,7 @@ static int _msc_db_update(void **handle, const ms_comm_msg_s * scan_data) start_path = strndup(scan_data->msg, scan_data->msg_size); scan_type = scan_data->msg_type; - err = _msc_dir_scan(handle, start_path, storage_type, scan_type); + err = _msc_dir_scan(handle, start_path, storage_type, scan_type,scan_data->uid); if (err != MS_MEDIA_ERR_NONE) { MSC_DBG_ERR("error : %d", err); } @@ -306,7 +310,7 @@ static int _msc_db_update(void **handle, const ms_comm_msg_s * scan_data) MSC_DBG_INFO("INVALID"); /*In this case, update just validation record*/ /*update just valid type*/ - err = msc_invalidate_all_items(handle, storage_type); + err = msc_invalidate_all_items(handle, storage_type,scan_data->uid); if (err != MS_MEDIA_ERR_NONE) { MSC_DBG_ERR("error : %d", err); } @@ -325,7 +329,6 @@ gboolean msc_directory_scan_thread(void *data) int err; int ret; void **handle = NULL; - ms_storage_type_t storage_type; int scan_type; char *noti_path = NULL; @@ -339,11 +342,10 @@ gboolean msc_directory_scan_thread(void *data) MSC_DBG_INFO("DIRECTORY SCAN START"); /*connect to media db, if conneting is failed, db updating is stopped*/ - err = msc_connect_db(&handle); + err = msc_connect_db(&handle, scan_data->uid); if (err != MS_MEDIA_ERR_NONE) continue; - storage_type = ms_get_storage_type_by_full(scan_data->msg); scan_type = scan_data->msg_type; if (scan_type != MS_MSG_DIRECTORY_SCANNING @@ -357,9 +359,9 @@ gboolean msc_directory_scan_thread(void *data) /*change validity before scanning*/ if (scan_type == MS_MSG_DIRECTORY_SCANNING) - err = msc_set_folder_validity(handle, scan_data->msg, MS_INVALID, MS_RECURSIVE); + err = msc_set_folder_validity(handle, scan_data->msg, MS_INVALID, MS_RECURSIVE,scan_data->uid); else - err = msc_set_folder_validity(handle, scan_data->msg, MS_INVALID, MS_NON_RECURSIVE); + err = msc_set_folder_validity(handle, scan_data->msg, MS_INVALID, MS_NON_RECURSIVE,scan_data->uid); if (err != MS_MEDIA_ERR_NONE) MSC_DBG_ERR("error : %d", err); @@ -369,10 +371,10 @@ gboolean msc_directory_scan_thread(void *data) /*insert data into media db */ ret = _msc_db_update(handle, scan_data); - + /*call for bundle commit*/ - msc_register_end(handle); - msc_validate_end(handle); + msc_register_end(handle,scan_data->uid); + msc_validate_end(handle,scan_data->uid); if (ret == MS_MEDIA_ERR_NONE) { MSC_DBG_INFO("working normally"); @@ -384,7 +386,7 @@ gboolean msc_directory_scan_thread(void *data) MSC_DBG_INFO("delete count %d", count); MSC_DBG_INFO("insert count %d", insert_count); - msc_delete_invalid_items_in_folder(handle, scan_data->msg); + msc_delete_invalid_items_in_folder(handle, scan_data->msg,scan_data->uid); if ( !(count == 0 && insert_count == 0)) { msc_send_dir_update_noti(handle, noti_path); @@ -449,11 +451,11 @@ gboolean msc_storage_scan_thread(void *data) goto NEXT; } - storage_type = ms_get_storage_type_by_full(scan_data->msg); + storage_type = ms_get_storage_type_by_full(scan_data->msg,scan_data->uid); MSC_DBG_INFO("%d", storage_type); /*connect to media db, if conneting is failed, db updating is stopped*/ - err = msc_connect_db(&handle); + err = msc_connect_db(&handle,scan_data->uid); if (err != MS_MEDIA_ERR_NONE) continue; @@ -462,12 +464,12 @@ gboolean msc_storage_scan_thread(void *data) /*Delete all data before full scanning*/ if (scan_type == MS_MSG_STORAGE_ALL) { - res = msc_delete_all_items(handle, storage_type); + res = msc_delete_all_items(handle, storage_type,scan_data->uid); if (res != true) { MSC_DBG_ERR("msc_delete_all_record fails"); } } else if (scan_type == MS_MSG_STORAGE_PARTIAL) { - err = msc_invalidate_all_items(handle, storage_type); + err = msc_invalidate_all_items(handle, storage_type,scan_data->uid); if (err != MS_MEDIA_ERR_NONE) MSC_DBG_ERR("error : %d", err); } @@ -492,13 +494,13 @@ gboolean msc_storage_scan_thread(void *data) ret = _msc_db_update(handle, scan_data); /*call for bundle commit*/ - msc_register_end(handle); + msc_register_end(handle,scan_data->uid); if (scan_type == MS_MSG_STORAGE_PARTIAL) { /*disable bundle commit*/ - msc_validate_end(handle); + msc_validate_end(handle,scan_data->uid); if (ret == MS_MEDIA_ERR_NONE) { MSC_DBG_INFO("working normally"); - msc_delete_invalid_items(handle, storage_type); + msc_delete_invalid_items(handle, storage_type,scan_data->uid); } } @@ -523,7 +525,7 @@ gboolean msc_storage_scan_thread(void *data) } /*disconnect form media db*/ - if (handle) msc_disconnect_db(&handle); + if (handle) msc_disconnect_db(&handle); NEXT: /*Active flush */ @@ -626,7 +628,7 @@ gboolean msc_register_thread(void *data) int ret; void **handle = NULL; ms_msg_type_e current_msg = MS_MSG_MAX; - int (*insert_function)(void **, const char*) = NULL; + int (*insert_function)(void **, uid_t, const char*) = NULL; /*create array for processing overlay data*/ register_array = g_array_new (FALSE, FALSE, sizeof (ms_comm_msg_s *)); @@ -694,7 +696,7 @@ gboolean msc_register_thread(void *data) } /* connect to media db, if conneting is failed, db updating is stopped */ - err = msc_connect_db(&handle); + err = msc_connect_db(&handle,register_data->uid); if (err != MS_MEDIA_ERR_NONE) goto FREE_RESOURCE; @@ -728,18 +730,18 @@ gboolean msc_register_thread(void *data) } /* insert to db */ - err = insert_function(handle, insert_path); + err = insert_function(handle, insert_path, register_data->uid); if (power_off) { MSC_DBG_INFO("power off"); /*call for bundle commit*/ - msc_register_end(handle); + msc_register_end(handle, register_data->uid); goto _POWEROFF; } } /*call for bundle commit*/ - msc_register_end(handle); + msc_register_end(handle, register_data->uid); /*disconnect form media db*/ if (handle) msc_disconnect_db(&handle); diff --git a/src/scanner/media-scanner-socket.c b/src/scanner/media-scanner-socket.c index 9f2bd49..b6639bd 100755 --- a/src/scanner/media-scanner-socket.c +++ b/src/scanner/media-scanner-socket.c @@ -77,7 +77,7 @@ gboolean msc_receive_request(GIOChannel *src, GIOCondition condition, gpointer d return TRUE; } - MSC_DBG_INFO("receive msg from [%d] %d, %s", recv_msg->pid, recv_msg->msg_type, recv_msg->msg); + MSC_DBG_INFO("receive msg from [%d] %d, %s, uid %d", recv_msg->pid, recv_msg->msg_type, recv_msg->msg, recv_msg->uid); /* copy from recived data */ req_num = recv_msg->msg_type; @@ -128,6 +128,7 @@ int msc_send_scan_result(int result, ms_comm_msg_s *scan_data) send_msg.pid = scan_data->pid; send_msg.result = result; send_msg.msg_size = strlen(scan_data->msg); + send_msg.uid = scan_data->uid; strncpy(send_msg.msg, scan_data->msg, send_msg.msg_size); ret = ms_ipc_send_msg_to_server(sockfd, MS_SCAN_COMM_PORT, &send_msg, NULL); diff --git a/src/server/include/media-server-db-svc.h b/src/server/include/media-server-db-svc.h index d076e49..91b22f6 100755 --- a/src/server/include/media-server-db-svc.h +++ b/src/server/include/media-server-db-svc.h @@ -32,9 +32,9 @@ #include "media-common-types.h" -typedef int (*CONNECT)(void**, char **); +typedef int (*CONNECT)(void**, uid_t, char **); typedef int (*DISCONNECT)(void*, char **); -typedef int (*SET_ALL_STORAGE_ITEMS_VALIDITY)(void*, int, int, char **); +typedef int (*SET_ALL_STORAGE_ITEMS_VALIDITY)(void*, int, int, uid_t,char **); int ms_load_functions(void); @@ -43,12 +43,12 @@ void ms_unload_functions(void); int -ms_connect_db(void ***handle); +ms_connect_db(void ***handle, uid_t uid); int ms_disconnect_db(void ***handle); int -ms_invalidate_all_items(void **handle, ms_storage_type_t store_type); +ms_invalidate_all_items(void **handle, ms_storage_type_t store_type, uid_t uid); #endif /*_MEDIA_SERVER_DB_SVC_H_*/ diff --git a/src/server/media-server-db-svc.c b/src/server/media-server-db-svc.c index ba22ac3..6f8ca8d 100755 --- a/src/server/media-server-db-svc.c +++ b/src/server/media-server-db-svc.c @@ -39,7 +39,7 @@ #include -#define CONFIG_PATH tzplatform_mkpath(TZ_SYS_DATA,"data-media/file-manager-service/plugin-config") +#define CONFIG_PATH tzplatform_mkpath(TZ_SYS_DATA,"file-manager-service/plugin-config") #define EXT ".so" #define EXT_LEN 3 @@ -218,7 +218,7 @@ ms_unload_functions(void) } int -ms_connect_db(void ***handle) +ms_connect_db(void ***handle, uid_t uid) { int lib_index; int ret; @@ -227,7 +227,7 @@ ms_connect_db(void ***handle) MS_MALLOC(*handle, sizeof (void*) * lib_num); for (lib_index = 0; lib_index < lib_num; lib_index++) { - ret = ((CONNECT)func_array[lib_index][eCONNECT])(&((*handle)[lib_index]), &err_msg); /*dlopen*/ + ret = ((CONNECT)func_array[lib_index][eCONNECT])(&((*handle)[lib_index]), uid, &err_msg); /*dlopen*/ if (ret != 0) { MS_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg); MS_SAFE_FREE(err_msg); @@ -264,7 +264,7 @@ ms_disconnect_db(void ***handle) } int -ms_invalidate_all_items(void **handle, ms_storage_type_t store_type) +ms_invalidate_all_items(void **handle, ms_storage_type_t store_type, uid_t uid) { int lib_index; int res = MS_MEDIA_ERR_NONE; @@ -272,7 +272,7 @@ ms_invalidate_all_items(void **handle, ms_storage_type_t store_type) char *err_msg = NULL; MS_DBG(""); for (lib_index = 0; lib_index < lib_num; lib_index++) { - ret = ((SET_ALL_STORAGE_ITEMS_VALIDITY)func_array[lib_index][eSET_ALL_VALIDITY])(handle[lib_index], store_type, false, &err_msg); /*dlopen*/ + ret = ((SET_ALL_STORAGE_ITEMS_VALIDITY)func_array[lib_index][eSET_ALL_VALIDITY])(handle[lib_index], store_type, false, uid, &err_msg); /*dlopen*/ if (ret != 0) { MS_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg); MS_SAFE_FREE(err_msg); diff --git a/src/server/media-server-db.c b/src/server/media-server-db.c index 23b0bd1..a60dfd8 100755 --- a/src/server/media-server-db.c +++ b/src/server/media-server-db.c @@ -62,17 +62,10 @@ gboolean ms_db_thread(void *data) GMainContext *context = NULL; MediaDBHandle *db_handle = NULL; - /* Connect Media DB*/ - if(media_db_connect(&db_handle) != MS_MEDIA_ERR_NONE) { - MS_DBG_ERR("Failed to connect DB\n"); - return FALSE; - } /* Create Socket*/ ret = ms_ipc_create_server_socket(MS_PROTOCOL_UDP, MS_DB_UPDATE_PORT, &sockfd); if(ret != MS_MEDIA_ERR_NONE) { - /* Disconnect DB*/ - media_db_disconnect(db_handle); MS_DBG_ERR("Failed to create socket\n"); return FALSE; @@ -85,8 +78,6 @@ gboolean ms_db_thread(void *data) ret = ms_ipc_create_server_socket(MS_PROTOCOL_TCP, MS_DB_BATCH_UPDATE_PORT, &tcp_sockfd); #endif if(ret != MS_MEDIA_ERR_NONE) { - /* Disconnect DB*/ - media_db_disconnect(db_handle); close(sockfd); MS_DBG_ERR("Failed to create socket\n"); return FALSE; @@ -130,8 +121,6 @@ gboolean ms_db_thread(void *data) g_io_channel_shutdown(channel, FALSE, NULL); g_io_channel_unref(channel); - /* Disconnect DB*/ - media_db_disconnect(db_handle); /*close socket*/ close(sockfd); diff --git a/src/server/media-server-main.c b/src/server/media-server-main.c index 41f45e4..ce1a8f6 100755 --- a/src/server/media-server-main.c +++ b/src/server/media-server-main.c @@ -35,6 +35,7 @@ #include #include #include +#include #include "media-util.h" #include "media-common-utils.h" @@ -152,10 +153,10 @@ static void _db_clear(void) } /*connect to media db, if conneting is failed, db updating is stopped*/ - ms_connect_db(&handle); + ms_connect_db(&handle,tzplatform_getuid(TZ_USER_NAME)); /*update just valid type*/ - if (ms_invalidate_all_items(handle, MS_STORAGE_EXTERNAL) != MS_MEDIA_ERR_NONE) + if (ms_invalidate_all_items(handle, MS_STORAGE_EXTERNAL, tzplatform_getuid(TZ_USER_NAME)) != MS_MEDIA_ERR_NONE) MS_DBG_ERR("ms_change_valid_type fail"); /*disconnect form media db*/ @@ -253,6 +254,23 @@ _ms_mmc_vconf_cb(void *data) return; } +static int _mkdir(const char *dir, mode_t mode) { + char tmp[256]; + char *p = NULL; + size_t len; + + snprintf(tmp, sizeof(tmp),"%s",dir); + len = strlen(tmp); + if(tmp[len - 1] == '/') + tmp[len - 1] = 0; + for(p = tmp + 1; *p; p++) + if(*p == '/') { + *p = 0; + mkdir(tmp, mode); + *p = '/'; + } + return mkdir(tmp, mode); +} int main(int argc, char **argv) { @@ -294,6 +312,9 @@ int main(int argc, char **argv) _ms_new_global_variable(); /*prepare socket*/ + /* create dir socket */ + _mkdir("/var/run/media-server",S_IRWXU | S_IRWXG | S_IRWXO); + /* Create and bind new UDP socket */ if (ms_ipc_create_server_socket(MS_PROTOCOL_UDP, MS_SCANNER_PORT, &sockfd) != MS_MEDIA_ERR_NONE) { @@ -342,10 +363,6 @@ int main(int argc, char **argv) sleep(1); } - _db_clear(); - - ms_send_storage_scan_request(MS_STORAGE_INTERNAL, MS_SCAN_PART); - if (ms_is_mmc_inserted()) { if (!ms_drm_insert_ext_memory()) MS_DBG_ERR("ms_drm_insert_ext_memory failed"); diff --git a/src/server/media-server-socket.c b/src/server/media-server-socket.c index 7fb8e11..d1753e0 100755 --- a/src/server/media-server-socket.c +++ b/src/server/media-server-socket.c @@ -27,6 +27,7 @@ * @version 1.0 * @brief */ + #include #include #ifdef _USE_UDS_SOCKET_ @@ -162,7 +163,7 @@ gboolean ms_read_socket(GIOChannel *src, GIOCondition condition, gpointer data) return TRUE; } - MS_DBG("receive msg from [%d] %d, %s", recv_msg.pid, recv_msg.msg_type, recv_msg.msg); + MS_DBG("receive msg from [%d] %d, %s, uid %d", recv_msg.pid, recv_msg.msg_type, recv_msg.msg, recv_msg.uid); if (recv_msg.msg_size > 0 && recv_msg.msg_size < MS_FILE_PATH_LEN_MAX) { msg_size = recv_msg.msg_size; @@ -213,6 +214,7 @@ gboolean ms_read_socket(GIOChannel *src, GIOCondition condition, gpointer data) scan_msg.msg_type = req_num; scan_msg.pid = pid; scan_msg.msg_size = msg_size; + scan_msg.uid = recv_msg.uid; ms_strcopy(scan_msg.msg, path_size, "%s", recv_msg.msg); g_mutex_unlock(scanner_mutex); @@ -332,6 +334,8 @@ int ms_send_storage_scan_request(ms_storage_type_t storage_type, ms_dir_scan_typ .msg = {0}, }; + scan_msg.uid = tzplatform_getuid(TZ_USER_NAME); + /* msg_type */ switch (scan_type) { case MS_SCAN_PART: @@ -400,7 +404,7 @@ gboolean ms_read_db_socket(GIOChannel *src, GIOCondition condition, gpointer dat int send_msg = MS_MEDIA_ERR_NONE; int sockfd = MS_SOCK_NOT_ALLOCATE; int ret = MS_MEDIA_ERR_NONE; - MediaDBHandle *db_handle = (MediaDBHandle *)data; + MediaDBHandle *db_handle = NULL; ms_comm_msg_s msg; char * sql_query = NULL; @@ -425,6 +429,8 @@ gboolean ms_read_db_socket(GIOChannel *src, GIOCondition condition, gpointer dat return TRUE; } + media_db_connect(&db_handle, recv_msg.uid); + sql_query = strndup(recv_msg.msg, recv_msg.msg_size); if (sql_query != NULL) { ret = media_db_update_db(db_handle, sql_query); @@ -442,6 +448,8 @@ gboolean ms_read_db_socket(GIOChannel *src, GIOCondition condition, gpointer dat ms_ipc_send_msg_to_client(sockfd, &msg, &client_addr); + media_db_disconnect(db_handle); + /*Active flush */ malloc_trim(0); @@ -466,7 +474,7 @@ gboolean ms_read_db_tcp_socket(GIOChannel *src, GIOCondition condition, gpointer int recv_msg_size = -1; int ret = MS_MEDIA_ERR_NONE; char * sql_query = NULL; - MediaDBHandle *db_handle = (MediaDBHandle *)data; + MediaDBHandle *db_handle = NULL; sock = g_io_channel_unix_get_fd(src); if (sock < 0) { @@ -507,6 +515,7 @@ gboolean ms_read_db_tcp_socket(GIOChannel *src, GIOCondition condition, gpointer sql_query = strndup(recv_msg.msg, recv_msg.msg_size); if (sql_query != NULL) { + media_db_connect(&db_handle, recv_msg.uid); if (recv_msg.msg_type == MS_MSG_DB_UPDATE_BATCH_START) { ret = media_db_update_db_batch_start(sql_query); } else if(recv_msg.msg_type == MS_MSG_DB_UPDATE_BATCH_END) { @@ -516,6 +525,7 @@ gboolean ms_read_db_tcp_socket(GIOChannel *src, GIOCondition condition, gpointer } else { } + media_db_disconnect(db_handle); MS_SAFE_FREE(sql_query); send_msg = ret; diff --git a/src/server/media-server-thumb.c b/src/server/media-server-thumb.c index 23bfbdf..db449c4 100755 --- a/src/server/media-server-thumb.c +++ b/src/server/media-server-thumb.c @@ -58,7 +58,8 @@ typedef struct { } thumbRequest; #ifdef _USE_UDS_SOCKET_ -extern char MEDIA_IPC_PATH[][50]; +extern char MEDIA_IPC_PATH[][70]; + #endif gboolean _ms_thumb_agent_start_jobs(gpointer data) -- 2.7.4