Modify null pointer dereferenced in _media_thumb_get_thumb_path_from_db
[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_with_size(const char *origin_path, char *thumb_path, int max_length, int *origin_width, int *origin_height, uid_t uid)
31 {
32         int err = MS_MEDIA_ERR_NONE;
33         media_thumb_info thumb_info;
34
35         if (origin_path == NULL || thumb_path == NULL) {
36                 thumb_err("Invalid parameter");
37                 return MS_MEDIA_ERR_INVALID_PARAMETER;
38         }
39
40         if (origin_width == NULL || origin_height == NULL) {
41                 thumb_err("Invalid parameter ( width or height )");
42                 return MS_MEDIA_ERR_INVALID_PARAMETER;
43         }
44
45         if (!g_file_test(origin_path, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) {
46                         thumb_err("Original path(%s) doesn't exist.", origin_path);
47                         return MS_MEDIA_ERR_INVALID_PARAMETER;
48         }
49
50         if (max_length <= 0) {
51                 thumb_err("Length is invalid");
52                 return MS_MEDIA_ERR_INVALID_PARAMETER;
53         }
54
55         int store_type = -1;
56         store_type = _media_thumb_get_store_type_by_path(origin_path);
57
58         if ((store_type != THUMB_PHONE) && (store_type != THUMB_MMC)) {
59                 thumb_err("origin path(%s) is invalid", origin_path);
60                 return MS_MEDIA_ERR_INVALID_PARAMETER;
61         }
62
63         thumb_dbg_slog("Path : %s", origin_path);
64
65         /* Request for thumb file to the daemon "Thumbnail generator" */
66         err = _media_thumb_request(THUMB_REQUEST_DB_INSERT, origin_path, thumb_path, max_length, &thumb_info, uid);
67         if (err != MS_MEDIA_ERR_NONE) {
68                 thumb_err("_media_thumb_request failed : %d", err);
69                 return err;
70         }
71
72         *origin_width = thumb_info.origin_width;
73         *origin_height = thumb_info.origin_height;
74
75         return MS_MEDIA_ERR_NONE;
76 }
77
78 int thumbnail_request_extract_all_thumbs(uid_t uid)
79 {
80         int err = MS_MEDIA_ERR_NONE;
81
82         media_thumb_info thumb_info;
83         char tmp_origin_path[MAX_PATH_SIZE] = {0,};
84         char tmp_thumb_path[MAX_PATH_SIZE] = {0,};
85
86         /* Request for thumb file to the daemon "Thumbnail generator" */
87         err = _media_thumb_request(THUMB_REQUEST_ALL_MEDIA, tmp_origin_path, tmp_thumb_path, sizeof(tmp_thumb_path), &thumb_info, uid);
88         if (err != MS_MEDIA_ERR_NONE) {
89                 thumb_err("_media_thumb_request failed : %d", err);
90                 return err;
91         }
92
93         return MS_MEDIA_ERR_NONE;
94 }
95
96 int thumbnail_request_from_db_async(unsigned int request_id, const char *origin_path, ThumbFunc func, void *user_data, uid_t uid)
97 {
98         int err = MS_MEDIA_ERR_NONE;
99
100         if (origin_path == NULL) {
101                 thumb_err("Invalid parameter");
102                 return MS_MEDIA_ERR_INVALID_PARAMETER;
103         }
104
105         if (!g_file_test(origin_path, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) {
106                         thumb_err("Original path(%s) doesn't exist.", origin_path);
107                         return MS_MEDIA_ERR_INVALID_PARAMETER;
108         }
109
110         int store_type = -1;
111         store_type = _media_thumb_get_store_type_by_path(origin_path);
112
113         if ((store_type != THUMB_PHONE) && (store_type != THUMB_MMC)) {
114                 thumb_err("origin path(%s) is invalid", origin_path);
115                 return MS_MEDIA_ERR_INVALID_PARAMETER;
116         }
117
118         thumb_dbg_slog("Path : %s", origin_path);
119
120         thumbUserData *userData = (thumbUserData*)malloc(sizeof(thumbUserData));
121         if (userData == NULL) {
122                 thumb_err("memory allocation failed");
123                 return MS_MEDIA_ERR_OUT_OF_MEMORY;
124         }
125         userData->func = (ThumbFunc)func;
126         userData->user_data = user_data;
127
128         /* Request for thumb file to the daemon "Thumbnail generator" */
129         err = _media_thumb_request_async(THUMB_REQUEST_DB_INSERT, request_id, origin_path, userData, uid);
130         if (err != MS_MEDIA_ERR_NONE) {
131                 thumb_err("_media_thumb_request failed : %d", err);
132                 SAFE_FREE(userData);
133                 return err;
134         }
135
136         return MS_MEDIA_ERR_NONE;
137 }
138
139 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)
140 {
141         int err = MS_MEDIA_ERR_NONE;
142         int exist = 0;
143
144         if (origin_path == NULL || request_id == 0) {
145                 thumb_err("original path is NULL. Or there is an error in request_id.");
146                 return MS_MEDIA_ERR_INVALID_PARAMETER;
147         }
148
149         /* check the file exits actually */
150         exist = open(origin_path, O_RDONLY);
151         if (exist < 0) {
152                 thumb_err("Fail to open original_path[%s]", origin_path);
153                 if (errno == EACCES || errno == EPERM)
154                         return  MS_MEDIA_ERR_PERMISSION_DENIED;
155                 else
156                         return MS_MEDIA_ERR_INVALID_PARAMETER;
157         }
158         close(exist);
159
160         thumb_dbg_slog("Path : %s", origin_path);
161
162         thumbRawUserData *userData = (thumbRawUserData*)malloc(sizeof(thumbRawUserData));
163         if (userData == NULL) {
164                 thumb_err("userData malloc failed : %d", err);
165                 return MS_MEDIA_ERR_OUT_OF_MEMORY;
166         }
167         userData->func = func;
168         userData->user_data = user_data;
169
170         err = _media_thumb_request_raw_data_async(THUMB_REQUEST_RAW_DATA, request_id, origin_path, width, height, userData, uid);
171         if (err != MS_MEDIA_ERR_NONE) {
172                 thumb_err("_media_raw_thumb_request failed : %d", err);
173                 SAFE_FREE(userData);
174                 return err;
175         }
176
177         return MS_MEDIA_ERR_NONE;
178 }
179
180 int thumbnail_request_cancel_media(unsigned int request_id, const char *origin_path)
181 {
182         int err = MS_MEDIA_ERR_NONE;
183
184         if (origin_path == NULL) {
185                 thumb_err("Invalid parameter");
186                 return MS_MEDIA_ERR_INVALID_PARAMETER;
187         }
188
189         err = _media_thumb_request_async(THUMB_REQUEST_CANCEL_MEDIA, request_id, origin_path, NULL, 0);
190         if (err != MS_MEDIA_ERR_NONE) {
191                 thumb_err("_media_thumb_request failed : %d", err);
192                 return err;
193         }
194
195         return MS_MEDIA_ERR_NONE;
196 }
197
198 int thumbnail_request_cancel_raw_data(int request_id)
199 {
200         int err = MS_MEDIA_ERR_NONE;
201
202         if (request_id == 0) {
203                 thumb_err("Invalid parameter");
204                 return MS_MEDIA_ERR_INVALID_PARAMETER;
205         }
206
207         err = _media_thumb_request_raw_data_async(THUMB_REQUEST_CANCEL_RAW_DATA, request_id, NULL, 0, 0, NULL, 0);
208         if (err != MS_MEDIA_ERR_NONE) {
209                 thumb_err("_media_thumb_request failed : %d", err);
210                 return err;
211         }
212
213         return MS_MEDIA_ERR_NONE;
214 }
215
216 int thumbnail_request_cancel_all(bool is_raw_data)
217 {
218         int err = MS_MEDIA_ERR_NONE;
219
220         if (is_raw_data) {
221                 err = _media_thumb_request_cancel_all(true);
222         } else {
223                 err = _media_thumb_request_cancel_all(false);
224         }
225
226         return err;
227 }