Remove hardcoded path for multiuser support
[platform/core/system/sync-agent.git] / src / framework / utility / fw_file.c
1 /*
2  * sync-agent
3  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
4  *
5  * Licensed under the Apache License, Version 2.0 (the License);
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include <stdio.h>
19 #include <stdlib.h>
20
21 #include "utility/sync_util.h"
22 #include "utility/fw_file.h"
23
24 #include "fsapi/operation.h"
25
26 #include <tzplatform_config.h>
27
28 #ifndef EXPORT_API
29 #define EXPORT_API __attribute__ ((visibility("default")))
30 #endif
31
32 #ifndef SYNC_AGENT_LOG
33 #undef LOG_TAG
34 #define LOG_TAG "AF_UTIL_FILE"
35 #endif
36
37 #define UTIL_DEFAULT_ACCOUNT_PLUGIN_FOLDER tzplatform_mkpath(TZ_SYS_DATA,"sync-agent/db")
38
39 EXPORT_API int sync_agent_set_int_into_file(char *file_path, int value)
40 {
41         _EXTERN_FUNC_ENTER;
42
43         retvm_if(file_path == NULL, 0, "file_path parameter is NULL !!");
44
45         char path[128];
46         char *temp_value = NULL;
47
48         if (!sync_agent_is_existing_fs(UTIL_DEFAULT_ACCOUNT_PLUGIN_FOLDER)) {
49                 if (!sync_agent_create_directory(UTIL_DEFAULT_ACCOUNT_PLUGIN_FOLDER)) {
50                         _DEBUG_ERROR("Failed to create default folder");
51                         _EXTERN_FUNC_EXIT;
52                         return 0;
53                 }
54         }
55
56         snprintf(path, sizeof(path), "%s/%s", UTIL_DEFAULT_ACCOUNT_PLUGIN_FOLDER, file_path);
57         temp_value = g_strdup_printf("%d", value);
58
59         if (temp_value != NULL) {
60                 _DEBUG_INFO("path = %s", path);
61                 _DEBUG_INFO("value = %s", temp_value);
62
63                 if (!sync_agent_write_whole_file(path, temp_value, strlen(temp_value), true)) {
64                         _DEBUG_ERROR("Failed write data");
65                         _EXTERN_FUNC_EXIT;
66                         return 0;
67                 }
68
69                 free(temp_value);
70         } else {
71                 _DEBUG_ERROR("strdup() FAIL !!!");
72                 _EXTERN_FUNC_EXIT;
73                 return 0;
74         }
75
76         _EXTERN_FUNC_EXIT;
77
78         return 1;
79 }
80
81 EXPORT_API int sync_agent_get_int_from_file(char *file_path, int *value)
82 {
83         _EXTERN_FUNC_ENTER;
84
85         retvm_if(file_path == NULL, 0, "file_path parameter is NULL !!");
86         retvm_if(value == NULL, 0, "value parameter is NULL !!");
87
88         char path[128];
89         char *temp_value = NULL;
90         unsigned long size;
91
92         snprintf(path, sizeof(path), "%s/%s", UTIL_DEFAULT_ACCOUNT_PLUGIN_FOLDER, file_path);
93
94         _DEBUG_INFO("path = %s", path);
95         if (!sync_agent_read_whole_file(path, &temp_value, &size)) {
96                 _DEBUG_ERROR("Failed to read file");
97                 *value = 0;
98                 return 0;
99         }
100
101         *value = atoi(temp_value);
102
103         _DEBUG_INFO("value = %d", *value);
104
105 //      if (temp_value != NULL)
106         free(temp_value);
107
108         _EXTERN_FUNC_EXIT;
109
110         return 1;
111 }
112
113 EXPORT_API int sync_agent_unset_file(char *file_path)
114 {
115         _EXTERN_FUNC_ENTER;
116
117         retvm_if(file_path == NULL, 0, "file_path parameter is NULL !!");
118
119         char path[128];
120
121         snprintf(path, sizeof(path), "%s/%s", UTIL_DEFAULT_ACCOUNT_PLUGIN_FOLDER, file_path);
122         _DEBUG_INFO("path = %s", path);
123
124         _EXTERN_FUNC_EXIT;
125
126         return sync_agent_delete_file(path);
127 }