Clean up repository
[platform/core/appfw/app2sd.git] / plugin / app2sd / common / src / app2sd_utils.c
1 /*
2  * app2ext
3  *
4  * Copyright (c) 2016 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 <stdio.h>
21 #include <stdlib.h>
22
23 #include "app2sd_utils.h"
24
25 int _is_global(uid_t uid)
26 {
27         if (uid == OWNER_ROOT || uid == GLOBAL_USER)
28                 return 1;
29         else
30                 return 0;
31 }
32
33 char *_app2sd_get_encoded_name(const char *pkgid, uid_t uid)
34 {
35         char *new_name = NULL;
36         char *temp_string = NULL;
37         char source_name[FILENAME_MAX] = { 0, };
38         GChecksum *checksum;
39
40         snprintf(source_name, FILENAME_MAX - 1, "%s_%d", pkgid, uid);
41         checksum = g_checksum_new(G_CHECKSUM_MD5);
42         g_checksum_update(checksum, (const guchar *)source_name, strlen(source_name));
43         temp_string = (char *)g_checksum_get_string(checksum);
44         new_name = strdup(temp_string);
45         g_checksum_free(checksum);
46
47         return new_name;
48 }
49
50 int _app2sd_delete_directory(const char *dirname)
51 {
52         DIR *dp = NULL;
53         struct dirent ep;
54         struct dirent *er = NULL;
55         char abs_filename[FILENAME_MAX] = { 0, };
56         int ret = 0;
57
58         dp = opendir(dirname);
59         if (dp != NULL) {
60                 while (readdir_r(dp, &ep, &er) == 0 && er != NULL) {
61                         struct stat stFileInfo;
62
63                         snprintf(abs_filename, FILENAME_MAX, "%s/%s", dirname,
64                                 ep.d_name);
65
66                         if (lstat(abs_filename, &stFileInfo) < 0) {
67                                 perror(abs_filename);
68                                 (void)closedir(dp);
69                                 return -1;
70                         }
71
72                         if (S_ISDIR(stFileInfo.st_mode)) {
73                                 if (strcmp(ep.d_name, ".")
74                                     && strcmp(ep.d_name, "..")) {
75                                         ret = _app2sd_delete_directory(abs_filename);
76                                         if (ret < 0) {
77                                                 (void)closedir(dp);
78                                                 return -1;
79                                         }
80                                 }
81                         } else {
82                                 ret = remove(abs_filename);
83                                 if (ret < 0) {
84                                         (void)closedir(dp);
85                                         return -1;
86                                 }
87                         }
88                 }
89                 (void)closedir(dp);
90                 ret = remove(dirname);
91                 if (ret < 0)
92                         return -1;
93         } else {
94                 _W("couldn't open the directory[%s]", dirname);
95         }
96         return 0;
97 }