diff options
Diffstat (limited to 'src/ipc/media-thumb-ipc.c')
-rwxr-xr-x | src/ipc/media-thumb-ipc.c | 357 |
1 files changed, 9 insertions, 348 deletions
diff --git a/src/ipc/media-thumb-ipc.c b/src/ipc/media-thumb-ipc.c index 64c4b46..e98d629 100755 --- a/src/ipc/media-thumb-ipc.c +++ b/src/ipc/media-thumb-ipc.c @@ -49,61 +49,7 @@ typedef struct { thumbRawUserData *userData; } thumbRawReq; -int _media_thumb_create_socket(int sock_type, int *sock) -{ - int sock_fd = 0; - - if ((sock_fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) { - thumb_err("socket failed: %s", strerror(errno)); - return MS_MEDIA_ERR_SOCKET_CONN; - } - - if (sock_type == CLIENT_SOCKET) { - struct timeval tv_timeout = { MS_TIMEOUT_SEC_10, 0 }; - - if (setsockopt(sock_fd, SOL_SOCKET, SO_RCVTIMEO, &tv_timeout, sizeof(tv_timeout)) == -1) { - thumb_err("setsockopt failed: %s", strerror(errno)); - close(sock_fd); - return MS_MEDIA_ERR_SOCKET_CONN; - } - } else if (sock_type == SERVER_SOCKET) { - - int n_reuse = 1; - - if (setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, &n_reuse, sizeof(n_reuse)) == -1) { - thumb_err("setsockopt failed: %s", strerror(errno)); - close(sock_fd); - return MS_MEDIA_ERR_SOCKET_CONN; - } - } - - *sock = sock_fd; - return MS_MEDIA_ERR_NONE; -} - - -int _media_thumb_create_udp_socket(int *sock) -{ - int sock_fd = 0; - - if ((sock_fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) { - thumb_err("socket failed: %s", strerror(errno)); - return MS_MEDIA_ERR_SOCKET_CONN; - } - - struct timeval tv_timeout = { MS_TIMEOUT_SEC_10, 0 }; - - if (setsockopt(sock_fd, SOL_SOCKET, SO_RCVTIMEO, &tv_timeout, sizeof(tv_timeout)) == -1) { - thumb_err("setsockopt failed: %s", strerror(errno)); - close(sock_fd); - return MS_MEDIA_ERR_SOCKET_CONN; - } - - *sock = sock_fd; - - return MS_MEDIA_ERR_NONE; -} int _media_thumb_get_error() { @@ -209,7 +155,6 @@ int __media_thumb_check_req_queue(const char *path) { int req_len = 0, i; - if (g_request_queue == NULL) return MS_MEDIA_ERR_NONE; req_len = g_queue_get_length(g_request_queue); // thumb_dbg("Queue length : %d", req_len); @@ -248,7 +193,6 @@ _media_thumb_recv_msg(int sock, int header_size, thumbMsg *msg) buf = (unsigned char*)malloc(header_size * sizeof(unsigned char)); block_buf = (unsigned char*)malloc(THUMB_SOCK_BLOCK_SIZE * sizeof(unsigned char)); - if (buf == NULL || block_buf == NULL) { thumb_err("memory allocation failed"); SAFE_FREE(buf); @@ -323,7 +267,6 @@ _media_thumb_recv_udp_msg(int sock, int header_size, thumbMsg *msg, struct socka } strncpy(msg->org_path, (char*)buf + header_size, msg->origin_path_size); - //thumb_dbg("original path : %s", msg->org_path); if (msg->dest_path_size <= 0 || msg->dest_path_size > MAX_PATH_SIZE) { SAFE_FREE(buf); @@ -332,7 +275,6 @@ _media_thumb_recv_udp_msg(int sock, int header_size, thumbMsg *msg, struct socka } strncpy(msg->dst_path, (char*)buf + header_size + msg->origin_path_size, msg->dest_path_size); - //thumb_dbg("destination path : %s", msg->dst_path); SAFE_FREE(buf); *from_size = from_addr_size; @@ -553,285 +495,6 @@ _media_thumb_request(int msg_type, media_thumb_type thumb_type, const char *orig return MS_MEDIA_ERR_NONE; } -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); -} - -static char* _media_thumb_get_default_path(uid_t uid) -{ - char *result_psswd = NULL; - struct group *grpinfo = NULL; - if(uid == getuid()) - { - result_psswd = strdup(THUMB_DEFAULT_PATH); - grpinfo = getgrnam("users"); - if(grpinfo == NULL) { - thumb_err("getgrnam(users) returns NULL !"); - if(result_psswd) - free(result_psswd); - return NULL; - } - } - else - { - struct passwd *userinfo = getpwuid(uid); - if(userinfo == NULL) { - thumb_err("getpwuid(%d) returns NULL !", uid); - return NULL; - } - grpinfo = getgrnam("users"); - if(grpinfo == NULL) { - thumb_err("getgrnam(users) returns NULL !"); - return NULL; - } - // Compare git_t type and not group name - if (grpinfo->gr_gid != userinfo->pw_gid) { - thumb_err("UID [%d] does not belong to 'users' group!", uid); - return NULL; - } - asprintf(&result_psswd, "%s/data/file-manager-service/.thumb/phone", userinfo->pw_dir); - } - - /* create dir */ - _mkdir(result_psswd,S_IRWXU | S_IRWXG | S_IRWXO); - - return result_psswd; -} - -int _media_thumb_process(thumbMsg *req_msg, thumbMsg *res_msg, uid_t uid) -{ - int err = -1; - GdkPixbuf *gdkdata = NULL; - int thumb_size = 0; - int thumb_w = 0; - int thumb_h = 0; - int origin_w = 0; - int origin_h = 0; - int max_length = 0; - char *thumb_path = NULL; - int need_update_db = 0; - int alpha = 0; - - if (req_msg == NULL || res_msg == NULL) { - thumb_err("Invalid msg!"); - return MS_MEDIA_ERR_INVALID_PARAMETER; - } - - int msg_type = req_msg->msg_type; - media_thumb_type thumb_type = req_msg->thumb_type; - const char *origin_path = req_msg->org_path; - - // Currently, The color space that is supported by the gdk-pixbuf is only RGB. - media_thumb_format thumb_format = MEDIA_THUMB_RGB888; - - thumb_path = res_msg->dst_path; - thumb_path[0] = '\0'; - max_length = sizeof(res_msg->dst_path); - - err = _media_thumb_db_connect(uid); - if (err != MS_MEDIA_ERR_NONE) { - thumb_err("_media_thumb_mb_svc_connect failed: %d", err); - return MS_MEDIA_ERR_DB_CONNECT_FAIL; - } - - if (msg_type == THUMB_REQUEST_DB_INSERT) { - err = _media_thumb_get_thumb_from_db_with_size(origin_path, thumb_path, max_length, &need_update_db, &origin_w, &origin_h); - if (err == MS_MEDIA_ERR_NONE) { - res_msg->origin_width = origin_w; - res_msg->origin_height = origin_h; - _media_thumb_db_disconnect(); - return MS_MEDIA_ERR_NONE; - } else { - if (strlen(thumb_path) == 0) { - err = _media_thumb_get_hash_name(origin_path, thumb_path, max_length,uid); - if (err != MS_MEDIA_ERR_NONE) { - char *default_path = _media_thumb_get_default_path(uid); - if(default_path) { - thumb_err("_media_thumb_get_hash_name failed - %d\n", err); - strncpy(thumb_path, default_path, max_length); - free(default_path); - } - _media_thumb_db_disconnect(); - return err; - } - - thumb_path[strlen(thumb_path)] = '\0'; - } - } - - } else if (msg_type == THUMB_REQUEST_SAVE_FILE) { - strncpy(thumb_path, req_msg->dst_path, max_length); - - } else if (msg_type == THUMB_REQUEST_ALL_MEDIA) { - err = _media_thumb_get_hash_name(origin_path, thumb_path, max_length,uid); - if (err < 0) { - char *default_path = _media_thumb_get_default_path(uid); - if(default_path) { - thumb_err("_media_thumb_get_hash_name failed - %d\n", err); - strncpy(thumb_path, default_path, max_length); - free(default_path); - } - _media_thumb_db_disconnect(); - return err; - } - - thumb_path[strlen(thumb_path)] = '\0'; - } - - if (g_file_test(thumb_path, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) { - thumb_warn("thumb path already exists in file system.. remove the existed file"); - _media_thumb_remove_file(thumb_path); - } - - err = _thumbnail_get_data(origin_path, thumb_type, thumb_format, &gdkdata, &thumb_size, &thumb_w, &thumb_h, &origin_w, &origin_h, &alpha, uid); - if (err != MS_MEDIA_ERR_NONE) { - char *default_path = _media_thumb_get_default_path(uid); - thumb_err("_thumbnail_get_data failed - %d\n", err); - if ( gdkdata != NULL ){ - g_object_unref(gdkdata); - } - if(default_path) { - strncpy(thumb_path, default_path, max_length); - free(default_path); - } - _media_thumb_db_disconnect(); - return err; - } - - //thumb_dbg("Size : %d, W:%d, H:%d", thumb_size, thumb_w, thumb_h); - //thumb_dbg("Origin W:%d, Origin H:%d\n", origin_w, origin_h); - //thumb_dbg("Thumb : %s", thumb_path); - - res_msg->msg_type = THUMB_RESPONSE; - res_msg->thumb_size = thumb_size; - res_msg->thumb_width = thumb_w; - res_msg->thumb_height = thumb_h; - res_msg->origin_width = origin_w; - res_msg->origin_height = origin_h; - - /* If the image is transparent PNG format, make png file as thumbnail of this image */ - if (alpha) { - char file_ext[10]; - err = _media_thumb_get_file_ext(origin_path, file_ext, sizeof(file_ext)); - if (strncasecmp(file_ext, "png", 3) == 0) { - int len = strlen(thumb_path); - thumb_path[len - 3] = 'p'; - thumb_path[len - 2] = 'n'; - thumb_path[len - 1] = 'g'; - } - thumb_dbg("Thumb path is changed : %s", thumb_path); - } - - err = _media_thumb_save_to_file_with_gdk(gdkdata, thumb_w, thumb_h, alpha, thumb_path); - if (err < 0) { - char *default_path = _media_thumb_get_default_path(uid); - thumb_err("save_to_file_with_gdk failed - %d\n", err); - if ( gdkdata != NULL ){ - g_object_unref(gdkdata); - } - - if (msg_type == THUMB_REQUEST_DB_INSERT || msg_type == THUMB_REQUEST_ALL_MEDIA) { - if(default_path) { - strncpy(thumb_path, default_path, max_length); - free(default_path); - } - } - _media_thumb_db_disconnect(); - return err; - } else { - thumb_dbg("file save success\n"); - } - - /* fsync */ - int fd = 0; - fd = open(thumb_path, O_WRONLY); - if (fd < 0) { - thumb_warn("open failed"); - } else { - err = fsync(fd); - if (err == -1) { - thumb_warn("fsync failed"); - } - close(fd); - } - /* End of fsync */ - - g_object_unref(gdkdata); - - /* DB update if needed */ - if (need_update_db == 1) { - err = _media_thumb_update_db(origin_path, thumb_path, res_msg->origin_width, res_msg->origin_height, uid); - if (err != MS_MEDIA_ERR_NONE) { - thumb_err("_media_thumb_update_db failed : %d", err); - } - } - - _media_thumb_db_disconnect(); - - return MS_MEDIA_ERR_NONE; -} - -int -_media_thumb_process_raw(thumbMsg *req_msg, thumbMsg *res_msg, thumbRawAddMsg *res_raw_msg, uid_t uid) -{ - int err = MS_MEDIA_ERR_NONE; - unsigned char *data = NULL; - int thumb_size = 0; - int thumb_w = 0; - int thumb_h = 0; - - if (req_msg == NULL || res_msg == NULL) { - thumb_err("Invalid msg!"); - return MS_MEDIA_ERR_INVALID_PARAMETER; - } - - const char *origin_path = req_msg->org_path; - - media_thumb_format thumb_format = MEDIA_THUMB_BGRA; - thumb_w = req_msg->thumb_width; - thumb_h = req_msg->thumb_height; - - //thumb_dbg("origin_path : %s, thumb_w : %d, thumb_h : %d ", origin_path, thumb_w, thumb_h); - - if (!g_file_test(origin_path, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) { - thumb_err("origin_path does not exist in file system."); - return MS_MEDIA_ERR_FILE_NOT_EXIST; - } - - err = _thumbnail_get_raw_data(origin_path, thumb_format, &data, &thumb_size, &thumb_w, &thumb_h, uid); - - if (err != MS_MEDIA_ERR_NONE) { - thumb_err("_thumbnail_get_data failed - %d", err); - SAFE_FREE(data); - } - - res_msg->msg_type = THUMB_RESPONSE_RAW_DATA; - res_msg->thumb_width = thumb_w; - res_msg->thumb_height = thumb_h; - res_raw_msg->thumb_size = thumb_size; - res_raw_msg->thumb_data = malloc(thumb_size * sizeof(unsigned char)); - memcpy(res_raw_msg->thumb_data, data, thumb_size); - res_msg->thumb_size = thumb_size + sizeof(thumbRawAddMsg) - sizeof(unsigned char*); - - //thumb_dbg("Size : %d, W:%d, H:%d", thumb_size, thumb_w, thumb_h); - - return MS_MEDIA_ERR_NONE; -} - gboolean _media_thumb_write_socket(GIOChannel *src, GIOCondition condition, gpointer data) { thumbMsg recv_msg; @@ -945,17 +608,16 @@ int _media_thumb_request_async(int msg_type, media_thumb_type thumb_type, const thumb_err("ms_ipc_create_client_socket failed"); return err; } - GIOChannel *channel = NULL; - channel = g_io_channel_unix_new(sock); - int source_id = -1; - memset(&serv_addr, 0, sizeof(serv_addr)); sock = sock_info.sock_fd; serv_addr.sun_family = AF_UNIX; - strcpy(serv_addr.sun_path, "/var/run/media-server/media_ipc_thumbcreator.socket"); + GIOChannel *channel = NULL; + channel = g_io_channel_unix_new(sock); + int source_id = -1; + /* Connecting to the thumbnail server */ if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) { thumb_stderror("connect"); @@ -1050,7 +712,7 @@ int _media_thumb_request_async(int msg_type, media_thumb_type thumb_type, const int _media_thumb_request_raw_data_async(int msg_type, int request_id, const char *origin_path, int width, int height, thumbRawUserData *userData, uid_t uid) { int err = MS_MEDIA_ERR_NONE; - int sock; + int sock = -1; struct sockaddr_un serv_addr; ms_sock_info_s sock_info; int pid; @@ -1062,17 +724,16 @@ int _media_thumb_request_raw_data_async(int msg_type, int request_id, const char thumb_err("ms_ipc_create_client_socket failed"); return err; } - GIOChannel *channel = NULL; - channel = g_io_channel_unix_new(sock); - int source_id = -1; - memset(&serv_addr, 0, sizeof(serv_addr)); sock = sock_info.sock_fd; serv_addr.sun_family = AF_UNIX; - strcpy(serv_addr.sun_path, "/var/run/media-server/media_ipc_thumbcreator.socket"); + GIOChannel *channel = NULL; + channel = g_io_channel_unix_new(sock); + int source_id = -1; + /* Connecting to the thumbnail server */ if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) { thumb_err("connect error : %s", strerror(errno)); |