184190ffd759531205408b3479bc062927355e5a
[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  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 #include <device/power.h>
21
22 #include "media-util.h"
23 #include "media-common-dbg.h"
24 #include "media-common-system.h"
25 #include "media-common-utils.h"
26
27 int ms_verify_all_parent_dirs(const char *full_path, uid_t uid)
28 {
29         int ret = MS_MEDIA_ERR_NONE;
30         char *dir_path = NULL;
31         char *next = NULL;
32         int next_pos = 0;
33         ms_user_storage_type_e storage_type = MS_USER_STORAGE_INTERNAL;
34
35         ret = ms_user_get_storage_type(uid, full_path, &storage_type);
36         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "Invalid path");
37
38         if (storage_type == MS_USER_STORAGE_INTERNAL) {
39                 ret = ms_user_get_internal_root_path(uid, &dir_path);
40                 MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "ms_user_get_internal_root_path() fail");
41
42                 next_pos = strlen(dir_path);
43                 g_free(dir_path);
44                 dir_path = NULL;
45         } else {
46                 next_pos = strlen(MEDIA_ROOT_PATH_USB) + 1;
47         }
48
49         while ((next = strstr(full_path + next_pos, "/"))) {
50                 next_pos = (next - full_path);
51                 dir_path = g_strndup(full_path, next_pos);
52                 next_pos++;
53
54                 ret = ms_check_scan_ignore(dir_path, uid);
55                 g_free(dir_path);
56                 if (ret != MS_MEDIA_ERR_NONE)
57                         break;
58         }
59
60         return ret;
61 }
62
63 int ms_set_power_mode(ms_db_status_type_t status)
64 {
65         int res = MS_MEDIA_ERR_NONE;
66         int err;
67
68         switch (status) {
69         case MS_DB_UPDATING:
70                 err = device_power_request_lock(POWER_LOCK_CPU, 0);
71                 if (err != 0)
72                         res = MS_MEDIA_ERR_INTERNAL;
73                 break;
74         case MS_DB_UPDATED:
75                 err = device_power_release_lock(POWER_LOCK_CPU);
76                 if (err != 0)
77                         res = MS_MEDIA_ERR_INTERNAL;
78                 break;
79         default:
80                 MS_DBG_ERR("Unacceptable type : %d", status);
81                 break;
82         }
83
84         return res;
85 }
86
87 void ms_trim_dir_path(char *dir_path)
88 {
89         /* need implementation */
90         /* if dir_path is not NULL terminated, this function will occure crash */
91         int len = strlen(dir_path);
92
93         if (dir_path[len -1] == '/')
94                 dir_path[len -1] = '\0';
95 }
96
97 #ifdef _USE_TVPD_MODE
98 /* See media-common-utils-tv.c for common functions with a TV product implementation */
99 #else
100 /* Public implementation */
101 bool ms_is_valid_symlink(const char *path)
102 {
103         g_autofree char *real_path = realpath(path, NULL);
104
105         if (!real_path)
106                 return false;
107
108         return (g_strcmp0(real_path, MEDIA_SHARE_PATH) == 0);
109 }
110
111 int ms_check_scan_ignore(char *path, uid_t uid)
112 {
113         int ret = MS_MEDIA_ERR_NONE;
114         const char *ignore_file = ".scan_ignore";
115         char *tmp_path = NULL;
116         char *org_path = NULL;
117         char ignore_path[MS_FILE_PATH_LEN_MAX] = {0, };
118
119         char replace[MS_FILE_PATH_LEN_MAX] = {0, };
120         char *mediashared = NULL;
121
122         /* Check for symbolic link */
123         tmp_path = realpath(path, NULL);
124         /* Get trimmed path */
125         org_path = g_canonicalize_filename(path, NULL);
126
127         if (g_str_has_prefix(tmp_path, MEDIA_SHARE_PATH)) {
128                 ms_user_get_mediashared_path(uid, &mediashared);
129                 snprintf(replace, MS_FILE_PATH_LEN_MAX, "%s%s", mediashared, tmp_path + strlen(MEDIA_SHARE_PATH));
130                 MS_SAFE_FREE(mediashared);
131                 if (g_strcmp0(replace, org_path) != 0) {
132                         MS_SAFE_FREE(tmp_path);
133                         g_free(org_path);
134                         MS_DBG_ERR("symbolic link(directory)");
135                         return MS_MEDIA_ERR_INVALID_PARAMETER;
136                 }
137         } else {
138                 if (g_strcmp0(tmp_path, org_path) != 0) {
139                         MS_SAFE_FREE(tmp_path);
140                         g_free(org_path);
141                         MS_DBG_ERR("symbolic link(directory)");
142                         return MS_MEDIA_ERR_INVALID_PARAMETER;
143                 }
144         }
145
146         MS_SAFE_FREE(tmp_path);
147         g_free(org_path);
148
149         if (g_file_test(path, G_FILE_TEST_IS_DIR)) {
150                 snprintf(ignore_path, sizeof(ignore_path), "%s/%s", path, ignore_file);
151
152                 if (g_file_test(ignore_path, G_FILE_TEST_EXISTS)) {
153                         MS_DBG_WARN("scan ignore file exist [%s]", ignore_path);
154                         return MS_MEDIA_ERR_INVALID_PARAMETER;
155                 }
156         } else {
157                 MS_DBG_ERR("g_file_test fails[%s]", path);
158                 ret = MS_MEDIA_ERR_INVALID_PARAMETER;
159         }
160
161         return ret;
162 }
163 #endif /* _USE_TVPD_MODE */