[SATIZENVUL-926] Check allocation
[platform/core/multimedia/libmedia-thumbnail.git] / src / media-thumbnail.c
1 /*
2  * libmedia-thumbnail
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Hyunjun Ko <zzoon.ko@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 #include "media-thumbnail.h"
23 #include "media-thumb-debug.h"
24 #include "media-thumb-util.h"
25 #include "media-thumb-internal.h"
26 #include "media-thumb-ipc.h"
27
28 #include <glib.h>
29
30 int thumbnail_request_from_db(const char *origin_path, char *thumb_path, int max_length, uid_t uid)
31 {
32         int err = MS_MEDIA_ERR_NONE;
33
34         if (origin_path == NULL || thumb_path == NULL) {
35                 thumb_err("Invalid parameter");
36                 return MS_MEDIA_ERR_INVALID_PARAMETER;
37         }
38
39         if (!g_file_test(origin_path, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) {
40                         thumb_err("Original path(%s) doesn't exist.", origin_path);
41                         return MS_MEDIA_ERR_INVALID_PARAMETER;
42         }
43
44         if (max_length <= 0) {
45                 thumb_err("Length is invalid");
46                 return MS_MEDIA_ERR_INVALID_PARAMETER;
47         }
48
49         ms_user_storage_type_t store_type = -1;
50         err = ms_user_get_storage_type(uid, origin_path, &store_type);
51
52         if((err != MS_MEDIA_ERR_NONE) || ((store_type != MS_USER_STORAGE_INTERNAL) && (store_type != MS_USER_STORAGE_EXTERNAL))) {
53                 thumb_err_slog("origin path(%s) is invalid. err : [%d] store_type [%d]", origin_path, err, store_type);
54                 return MS_MEDIA_ERR_INVALID_PARAMETER;
55         }
56
57         thumb_dbg_slog("Path : %s", origin_path);
58
59         /* Request for thumb file to the daemon "Thumbnail generator" */
60         err = _media_thumb_request(THUMB_REQUEST_DB_INSERT, origin_path, thumb_path, max_length, uid);
61         if (err != MS_MEDIA_ERR_NONE) {
62                 thumb_err("_media_thumb_request failed : %d", err);
63                 return err;
64         }
65
66         return MS_MEDIA_ERR_NONE;
67 }
68
69 int thumbnail_request_from_db_async(unsigned int request_id, const char *origin_path, ThumbFunc func, void *user_data, uid_t uid)
70 {
71         int err = MS_MEDIA_ERR_NONE;
72
73         if (origin_path == NULL) {
74                 thumb_err("Invalid parameter");
75                 return MS_MEDIA_ERR_INVALID_PARAMETER;
76         }
77
78         if (!g_file_test(origin_path, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) {
79                         thumb_err("Original path(%s) doesn't exist.", origin_path);
80                         return MS_MEDIA_ERR_INVALID_PARAMETER;
81         }
82
83         ms_user_storage_type_t store_type = -1;
84         err = ms_user_get_storage_type(uid, origin_path, &store_type);
85
86         if((err != MS_MEDIA_ERR_NONE) || ((store_type != MS_USER_STORAGE_INTERNAL) && (store_type != MS_USER_STORAGE_EXTERNAL))) {
87                 thumb_err_slog("origin path(%s) is invalid. err : [%d] store_type [%d]", origin_path, err, store_type);
88                 return MS_MEDIA_ERR_INVALID_PARAMETER;
89         }
90
91         thumb_dbg_slog("Path : %s", origin_path);
92
93         thumbUserData *userData = (thumbUserData*)malloc(sizeof(thumbUserData));
94         if (userData == NULL) {
95                 thumb_err("memory allocation failed");
96                 return MS_MEDIA_ERR_OUT_OF_MEMORY;
97         }
98         userData->func = (ThumbFunc)func;
99         userData->user_data = user_data;
100
101         /* Request for thumb file to the daemon "Thumbnail generator" */
102         err = _media_thumb_request_async(THUMB_REQUEST_DB_INSERT, request_id, origin_path, userData, uid);
103         if (err != MS_MEDIA_ERR_NONE) {
104                 thumb_err("_media_thumb_request failed : %d", err);
105                 SAFE_FREE(userData);
106                 return err;
107         }
108
109         return MS_MEDIA_ERR_NONE;
110 }
111
112 int thumbnail_request_extract_raw_data_async(int request_id, const char *origin_path, int width, int height, ThumbRawFunc func, void *user_data, uid_t uid)
113 {
114         int err = MS_MEDIA_ERR_NONE;
115         int exist = 0;
116
117         if (origin_path == NULL || request_id == 0) {
118                 thumb_err("original path is NULL. Or there is an error in request_id.");
119                 return MS_MEDIA_ERR_INVALID_PARAMETER;
120         }
121
122         /* check the file exits actually */
123         exist = open(origin_path, O_RDONLY);
124         if (exist < 0) {
125                 thumb_err("Fail to open original_path[%s]", origin_path);
126                 if (errno == EACCES || errno == EPERM)
127                         return  MS_MEDIA_ERR_PERMISSION_DENIED;
128                 else
129                         return MS_MEDIA_ERR_INVALID_PARAMETER;
130         }
131         close(exist);
132
133         thumb_dbg_slog("Path : %s", origin_path);
134
135         thumbRawUserData *userData = (thumbRawUserData*)malloc(sizeof(thumbRawUserData));
136         if (userData == NULL) {
137                 thumb_err("userData malloc failed : %d", err);
138                 return MS_MEDIA_ERR_OUT_OF_MEMORY;
139         }
140         userData->func = func;
141         userData->user_data = user_data;
142
143         err = _media_thumb_request_raw_data_async(THUMB_REQUEST_RAW_DATA, request_id, origin_path, width, height, userData, uid);
144         if (err != MS_MEDIA_ERR_NONE) {
145                 thumb_err("_media_raw_thumb_request failed : %d", err);
146                 SAFE_FREE(userData);
147                 return err;
148         }
149
150         return MS_MEDIA_ERR_NONE;
151 }
152
153 int thumbnail_request_cancel_media(unsigned int request_id, const char *origin_path)
154 {
155         int err = MS_MEDIA_ERR_NONE;
156
157         if (origin_path == NULL) {
158                 thumb_err("Invalid parameter");
159                 return MS_MEDIA_ERR_INVALID_PARAMETER;
160         }
161
162         err = _media_thumb_request_async(THUMB_REQUEST_CANCEL_MEDIA, request_id, origin_path, NULL, 0);
163         if (err != MS_MEDIA_ERR_NONE) {
164                 thumb_err("_media_thumb_request failed : %d", err);
165                 return err;
166         }
167
168         return MS_MEDIA_ERR_NONE;
169 }
170
171 int thumbnail_request_cancel_raw_data(int request_id)
172 {
173         int err = MS_MEDIA_ERR_NONE;
174
175         if (request_id == 0) {
176                 thumb_err("Invalid parameter");
177                 return MS_MEDIA_ERR_INVALID_PARAMETER;
178         }
179
180         err = _media_thumb_request_raw_data_async(THUMB_REQUEST_CANCEL_RAW_DATA, request_id, NULL, 0, 0, NULL, 0);
181         if (err != MS_MEDIA_ERR_NONE) {
182                 thumb_err("_media_thumb_request failed : %d", err);
183                 return err;
184         }
185
186         return MS_MEDIA_ERR_NONE;
187 }
188
189 int thumbnail_request_cancel_all(bool is_raw_data)
190 {
191         int err = MS_MEDIA_ERR_NONE;
192
193         if (is_raw_data) {
194                 err = _media_thumb_request_cancel_all(true);
195         } else {
196                 err = _media_thumb_request_cancel_all(false);
197         }
198
199         return err;
200 }