Git init
[framework/api/application.git] / src / app_resource.c
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License. 
15  */
16
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <unistd.h>
21 #include <string.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <fcntl.h>
25
26 #include <bundle.h>
27 #include <appcore-common.h>
28 #include <aul.h>
29 #include <dlog.h>
30
31 #include <app_private.h>
32 #include <app_service_private.h>
33
34 #ifdef LOG_TAG
35 #undef LOG_TAG
36 #endif
37
38 #define LOG_TAG "TIZEN_N_APPLICATION"
39
40 static char* app_get_resource_directory()
41 {
42         static char *resource_directory = NULL;
43
44         if (resource_directory == NULL)
45         {
46                 char *resource_directory_tmp;
47                 char *package = NULL;
48         
49                 if (app_get_package(&package) != APP_ERROR_NONE)
50                 {
51                         LOGE("[%s] INVALID_CONTEXT(0x%08x)", __FUNCTION__, APP_ERROR_INVALID_CONTEXT);
52                         return NULL;
53                 }
54
55                 resource_directory_tmp = calloc(1, sizeof(char) * TIZEN_PATH_MAX);
56
57                 if (resource_directory_tmp == NULL)
58                 {
59                         LOGE("[%s] OUT_OF_MEMORY(0x%08x)", __FUNCTION__, APP_ERROR_OUT_OF_MEMORY);
60                         return NULL;
61                 }
62
63                 snprintf(resource_directory_tmp, TIZEN_PATH_MAX, PATH_FMT_RES_DIR, package);
64
65                 if (package != NULL)
66                 {
67                         free(package);
68                 }
69
70                 resource_directory = resource_directory_tmp;
71         }
72
73         return resource_directory;
74 }
75
76 char* app_get_resource(const char *resource, char *buffer, int size)
77 {
78         char *resource_directory;
79         int abs_path_size;
80
81         if (resource == NULL)
82         {
83                 LOGE("[%s] INVALID_PARAMETER(0x%08x) : invalid resource", __FUNCTION__, APP_ERROR_INVALID_PARAMETER);
84                 return NULL;
85         }
86
87         if (buffer == NULL || size <= 0)
88         {
89                 LOGE("[%s] INVALID_PARAMETER(0x%08x) : invalid buffer", __FUNCTION__, APP_ERROR_INVALID_PARAMETER);
90                 return NULL;
91         }
92
93         resource_directory = app_get_resource_directory();
94
95         if (resource_directory == NULL)
96         {
97                 LOGE("[%s] INVALID_CONTEXT(0x%08x) : failed to get the path to the resource directory", __FUNCTION__, APP_ERROR_INVALID_CONTEXT);
98                 return NULL;
99         }
100
101         abs_path_size = strlen(resource_directory)+ strlen("/") + strlen(resource);
102
103         if (size <= abs_path_size)
104         {
105                 LOGE("[%s] INVALID_PARAMETER(0x%08x) : the buffer is not big enough", __FUNCTION__, APP_ERROR_INVALID_PARAMETER);
106                 return NULL;
107         }
108
109         snprintf(buffer, size, "%s/%s", resource_directory, resource);
110
111         return buffer;
112 }
113
114 char* app_get_data_directory(char *buffer, int size)
115 {
116         static char *abs_path = NULL;
117
118         if (buffer == NULL || size <= 0)
119         {
120                 LOGE("[%s] INVALID_PARAMETER(0x%08x) : invalid buffer", __FUNCTION__, APP_ERROR_INVALID_PARAMETER);
121                 return NULL;
122         }
123
124         if (abs_path == NULL)
125         {
126                 char *package;
127                 char *abs_path_tmp;
128
129                 if (app_get_package(&package) != 0)
130                 {
131                         LOGE("[%s] INVALID_CONTEXT(0x%08x)", __FUNCTION__, APP_ERROR_INVALID_CONTEXT);
132                         return NULL;
133                 }
134
135                 abs_path_tmp = calloc(1, sizeof(char) * TIZEN_PATH_MAX);
136
137                 if (abs_path_tmp == NULL)
138                 {
139                         LOGE("[%s] OUT_OF_MEMORY(0x%08x)", __FUNCTION__, APP_ERROR_OUT_OF_MEMORY);
140                         return NULL;
141                 }
142
143                 snprintf(abs_path_tmp, TIZEN_PATH_MAX, PATH_FMT_DATA_DIR, package);
144
145                 if (package != NULL)
146                 {
147                         free(package);
148                 }
149
150                 abs_path = abs_path_tmp;
151         }
152
153         if (abs_path == NULL)
154         {
155                 LOGE("[%s] INVALID_CONTEXT(0x%08x) : failed to get the absolute path to the data directory", __FUNCTION__, APP_ERROR_INVALID_CONTEXT);
156                 return NULL;
157         }
158
159         if (size <= strlen(abs_path))
160         {
161                 LOGE("[%s] INVALID_PARAMETER(0x%08x) : the buffer is not big enough", __FUNCTION__, APP_ERROR_INVALID_PARAMETER);
162                 return NULL;
163         }
164
165         snprintf(buffer, size, "%s", abs_path);
166
167         return buffer;
168 }
169