Revert "Remove unused code(not use uds)."
[platform/core/multimedia/media-server.git] / src / common / media-common-utils.c
1 /*
2  *  Media Server
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Yong Yeon Kim <yy9875.kim@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 /**
23  * This file defines api utilities of contents manager engines.
24  *
25  * @file                media-server-utils.c
26  * @author      Yong Yeon Kim(yy9875.kim@samsung.com)
27  * @version     1.0
28  * @brief       This file implements main database operation.
29  */
30
31 #define _GNU_SOURCE
32
33 #include <errno.h>
34 #include <vconf.h>
35 #include <aul/aul.h>
36 #include <grp.h>
37 #include <pwd.h>
38
39 #include "media-util.h"
40 #include "media-server-ipc.h"
41 #include "media-common-dbg.h"
42 #include "media-common-drm.h"
43 #include "media-common-utils.h"
44
45 #ifdef FMS_PERF
46 #include <sys/time.h>
47 #define MILLION 1000000L
48 struct timeval g_mmc_start_time;
49 struct timeval g_mmc_end_time;
50 #endif
51
52 #define MS_DRM_CONTENT_TYPE_LENGTH 100
53 #define GLOBAL_USER     0 //#define     tzplatform_getenv(TZ_GLOBAL) //TODO
54
55 #ifdef FMS_PERF
56 void
57 ms_check_start_time(struct timeval *start_time)
58 {
59         gettimeofday(start_time, NULL);
60 }
61
62 void
63 ms_check_end_time(struct timeval *end_time)
64 {
65         gettimeofday(end_time, NULL);
66 }
67
68 void
69 ms_check_time_diff(struct timeval *start_time, struct timeval *end_time)
70 {
71         struct timeval time;
72         long difftime;
73
74         time.tv_sec = end_time->tv_sec - start_time->tv_sec;
75         time.tv_usec = end_time->tv_usec - start_time->tv_usec;
76         difftime = MILLION * time.tv_sec + time.tv_usec;
77         MS_DBG("The function_to_time took %ld microseconds or %f seconds.",
78                difftime, difftime / (double)MILLION);
79 }
80 #endif
81
82 bool
83 ms_is_mmc_inserted(void)
84 {
85         int data = -1;
86         ms_config_get_int(VCONFKEY_SYSMAN_MMC_STATUS, &data);
87         if (data != VCONFKEY_SYSMAN_MMC_MOUNTED) {
88                 return false;
89         } else {
90                 return true;
91         }
92 }
93 static char* __media_get_path(uid_t uid)
94 {
95         char *result_psswd = NULL;
96         struct group *grpinfo = NULL;
97         if(uid == getuid())
98         {
99                 result_psswd = strdup(MEDIA_ROOT_PATH_INTERNAL);
100                 grpinfo = getgrnam("users");
101                 if(grpinfo == NULL) {
102                         MS_DBG_ERR("getgrnam(users) returns NULL !");
103                         return NULL;
104                 }
105     }
106         else
107         {
108                 struct passwd *userinfo = getpwuid(uid);
109                 if(userinfo == NULL) {
110                         MS_DBG_ERR("getpwuid(%d) returns NULL !", uid);
111                         return NULL;
112                 }
113                 grpinfo = getgrnam("users");
114                 if(grpinfo == NULL) {
115                         MS_DBG_ERR("getgrnam(users) returns NULL !");
116                         return NULL;
117                 }
118                 // Compare git_t type and not group name
119                 if (grpinfo->gr_gid != userinfo->pw_gid) {
120                         MS_DBG_ERR("UID [%d] does not belong to 'users' group!", uid);
121                         return NULL;
122                 }
123                 asprintf(&result_psswd, "%s", userinfo->pw_dir);
124         }
125
126         return result_psswd;
127 }
128
129 ms_storage_type_t
130 ms_get_storage_type_by_full(const char *path, uid_t uid)
131 {
132         int lenght_path;
133         char * user_path = NULL;
134         
135         if (path == NULL)
136                 return false;
137
138         user_path = __media_get_path(uid);
139         lenght_path = strlen(user_path);
140
141         if (strncmp(path, user_path, lenght_path) == 0) {
142                 return MS_STORAGE_INTERNAL;
143         } else if (strncmp(path, MEDIA_ROOT_PATH_SDCARD, strlen(MEDIA_ROOT_PATH_SDCARD)) == 0) {
144                 return MS_STORAGE_EXTERNAL;
145         } else
146        { free(user_path);
147                 return MS_MEDIA_ERR_INVALID_PATH;
148     }
149 }
150
151 int
152 ms_strappend(char *res, const int size, const char *pattern,
153              const char *str1, const char *str2)
154 {
155         int len = 0;
156         int real_size = size - 1;
157
158         if (!res ||!pattern || !str1 ||!str2 )
159                 return MS_MEDIA_ERR_INVALID_PARAMETER;
160
161         if (real_size < (strlen(str1) + strlen(str2)))
162                 return MS_MEDIA_ERR_INVALID_PARAMETER;
163
164         len = snprintf(res, real_size, pattern, str1, str2);
165         if (len < 0) {
166                 return MS_MEDIA_ERR_INVALID_PARAMETER;
167         }
168
169         res[len] = '\0';
170
171         return MS_MEDIA_ERR_NONE;
172 }
173
174 int
175 ms_strcopy(char *res, const int size, const char *pattern, const char *str1)
176 {
177         int len = 0;
178         int real_size = size;
179
180         if (!res || !pattern || !str1) {
181                 MS_DBG_ERR("parameta is invalid");
182                 return MS_MEDIA_ERR_INVALID_PARAMETER;
183         }
184
185         if (real_size < strlen(str1)) {
186                 MS_DBG_ERR("size is wrong");
187                 return MS_MEDIA_ERR_INVALID_PARAMETER;
188         }
189
190         len = snprintf(res, real_size, pattern, str1);
191         if (len < 0) {
192                 MS_DBG_ERR("snprintf failed");
193                 return MS_MEDIA_ERR_INVALID_PARAMETER;
194         }
195
196         res[len] = '\0';
197
198         return MS_MEDIA_ERR_NONE;
199 }
200
201 bool
202 ms_config_get_int(const char *key, int *value)
203 {
204         int err;
205
206         if (!key || !value) {
207                 MS_DBG_ERR("Arguments key or value is NULL");
208                 return false;
209         }
210
211         err = vconf_get_int(key, value);
212         if (err == 0)
213                 return true;
214         else if (err == -1)
215                 return false;
216         else
217                 MS_DBG_ERR("Unexpected error code: %d", err);
218
219         return false;
220 }
221
222 bool
223 ms_config_set_int(const char *key, int value)
224 {
225         int err;
226
227         if (!key) {
228                 MS_DBG_ERR("Arguments key is NULL");
229                 return false;
230         }
231
232         err = vconf_set_int(key, value);
233         if (err == 0)
234                 return true;
235         else if (err == -1)
236                 return false;
237         else
238                 MS_DBG_ERR("Unexpected error code: %d", err);
239
240         return false;
241 }
242
243 bool
244 ms_config_get_str(const char *key, char *value)
245 {
246         char *res;
247         if (!key || !value) {
248                 MS_DBG_ERR("Arguments key or value is NULL");
249                 return false;
250         }
251
252         res = vconf_get_str(key);
253         if (res) {
254                 strncpy(value, res, strlen(res) + 1);
255                 return true;
256         }
257
258         return false;
259 }
260
261 bool
262 ms_config_set_str(const char *key, const char *value)
263 {
264         int err;
265
266         if (!key || !value) {
267                 MS_DBG_ERR("Arguments key or value is NULL");
268                 return false;
269         }
270
271         err = vconf_set_str(key, value);
272         if (err == 0)
273                 return true;
274         else
275                 MS_DBG_ERR("fail to vconf_set_str %d", err);
276
277         return false;
278 }
279