summaryrefslogtreecommitdiff
path: root/src/app_resource.c
blob: 4a78ee32fa1dbd5415ed2495d6554d7d5d314c49 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the License);
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an AS IS BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. 
 */


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include <bundle.h>
#include <appcore-common.h>
#include <aul.h>
#include <dlog.h>

#include <app_private.h>
#include <app_service_private.h>

#ifdef LOG_TAG
#undef LOG_TAG
#endif

#define LOG_TAG "TIZEN_N_APPLICATION"

static char* app_get_resource_directory()
{
	static char *resource_directory = NULL;

	if (resource_directory == NULL)
	{
		char *resource_directory_tmp;
		char *package = NULL;
	
		if (app_get_package(&package) != APP_ERROR_NONE)
		{
			LOGE("[%s] INVALID_CONTEXT(0x%08x)", __FUNCTION__, APP_ERROR_INVALID_CONTEXT);
			return NULL;
		}

		resource_directory_tmp = calloc(1, sizeof(char) * TIZEN_PATH_MAX);

		if (resource_directory_tmp == NULL)
		{
			LOGE("[%s] OUT_OF_MEMORY(0x%08x)", __FUNCTION__, APP_ERROR_OUT_OF_MEMORY);
			return NULL;
		}

		snprintf(resource_directory_tmp, TIZEN_PATH_MAX, PATH_FMT_RES_DIR, package);

		if (package != NULL)
		{
			free(package);
		}

		resource_directory = resource_directory_tmp;
	}

	return resource_directory;
}

char* app_get_resource(const char *resource, char *buffer, int size)
{
	char *resource_directory;
	int abs_path_size;

	if (resource == NULL)
	{
		LOGE("[%s] INVALID_PARAMETER(0x%08x) : invalid resource", __FUNCTION__, APP_ERROR_INVALID_PARAMETER);
		return NULL;
	}

	if (buffer == NULL || size <= 0)
	{
		LOGE("[%s] INVALID_PARAMETER(0x%08x) : invalid buffer", __FUNCTION__, APP_ERROR_INVALID_PARAMETER);
		return NULL;
	}

	resource_directory = app_get_resource_directory();

	if (resource_directory == NULL)
	{
		LOGE("[%s] INVALID_CONTEXT(0x%08x) : failed to get the path to the resource directory", __FUNCTION__, APP_ERROR_INVALID_CONTEXT);
		return NULL;
	}

	abs_path_size = strlen(resource_directory)+ strlen("/") + strlen(resource);

	if (size <= abs_path_size)
	{
		LOGE("[%s] INVALID_PARAMETER(0x%08x) : the buffer is not big enough", __FUNCTION__, APP_ERROR_INVALID_PARAMETER);
		return NULL;
	}

	snprintf(buffer, size, "%s/%s", resource_directory, resource);

	return buffer;
}

char* app_get_data_directory(char *buffer, int size)
{
	static char *abs_path = NULL;

	if (buffer == NULL || size <= 0)
	{
		LOGE("[%s] INVALID_PARAMETER(0x%08x) : invalid buffer", __FUNCTION__, APP_ERROR_INVALID_PARAMETER);
		return NULL;
	}

	if (abs_path == NULL)
	{
		char *package;
		char *abs_path_tmp;

		if (app_get_package(&package) != 0)
		{
			LOGE("[%s] INVALID_CONTEXT(0x%08x)", __FUNCTION__, APP_ERROR_INVALID_CONTEXT);
			return NULL;
		}

		abs_path_tmp = calloc(1, sizeof(char) * TIZEN_PATH_MAX);

		if (abs_path_tmp == NULL)
		{
			LOGE("[%s] OUT_OF_MEMORY(0x%08x)", __FUNCTION__, APP_ERROR_OUT_OF_MEMORY);
			return NULL;
		}

		snprintf(abs_path_tmp, TIZEN_PATH_MAX, PATH_FMT_DATA_DIR, package);

		if (package != NULL)
		{
			free(package);
		}

		abs_path = abs_path_tmp;
	}

	if (abs_path == NULL)
	{
		LOGE("[%s] INVALID_CONTEXT(0x%08x) : failed to get the absolute path to the data directory", __FUNCTION__, APP_ERROR_INVALID_CONTEXT);
		return NULL;
	}

	if (size <= strlen(abs_path))
	{
		LOGE("[%s] INVALID_PARAMETER(0x%08x) : the buffer is not big enough", __FUNCTION__, APP_ERROR_INVALID_PARAMETER);
		return NULL;
	}

	snprintf(buffer, size, "%s", abs_path);

	return buffer;
}