Tizen 2.1 base
[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 #ifndef EXPORT_API
27 #define EXPORT_API __attribute__ ((visibility("default")))
28 #endif
29
30 #ifndef SYNC_AGENT_LOG
31 #undef LOG_TAG
32 #define LOG_TAG "AF_UTIL_FILE"
33 #endif
34
35 #define UTIL_DEFAULT_ACCOUNT_PLUGIN_FOLDER "/opt/data/sync-agent/db"
36
37 EXPORT_API int sync_agent_set_int_into_file(char *file_path, int value)
38 {
39         _EXTERN_FUNC_ENTER;
40
41         retvm_if(file_path == NULL, 0, "file_path parameter is NULL !!");
42
43         char path[128];
44         char *temp_value = NULL;
45
46         if (!sync_agent_is_existing_fs(UTIL_DEFAULT_ACCOUNT_PLUGIN_FOLDER)) {
47                 if (!sync_agent_create_directory(UTIL_DEFAULT_ACCOUNT_PLUGIN_FOLDER)) {
48                         _DEBUG_ERROR("Failed to create default folder");
49                         _EXTERN_FUNC_EXIT;
50                         return 0;
51                 }
52         }
53
54         snprintf(path, sizeof(path), "%s/%s", UTIL_DEFAULT_ACCOUNT_PLUGIN_FOLDER, file_path);
55         temp_value = g_strdup_printf("%d", value);
56
57         if (temp_value != NULL) {
58                 _DEBUG_INFO("path = %s", path);
59                 _DEBUG_INFO("value = %s", temp_value);
60
61                 if (!sync_agent_write_whole_file(path, temp_value, strlen(temp_value), true)) {
62                         _DEBUG_ERROR("Failed write data");
63                         _EXTERN_FUNC_EXIT;
64                         return 0;
65                 }
66
67                 free(temp_value);
68         } else {
69                 _DEBUG_ERROR("strdup() FAIL !!!");
70                 _EXTERN_FUNC_EXIT;
71                 return 0;
72         }
73
74         _EXTERN_FUNC_EXIT;
75
76         return 1;
77 }
78
79 EXPORT_API int sync_agent_get_int_from_file(char *file_path, int *value)
80 {
81         _EXTERN_FUNC_ENTER;
82
83         retvm_if(file_path == NULL, 0, "file_path parameter is NULL !!");
84         retvm_if(value == NULL, 0, "value parameter is NULL !!");
85
86         char path[128];
87         char *temp_value = NULL;
88         unsigned long size;
89
90         snprintf(path, sizeof(path), "%s/%s", UTIL_DEFAULT_ACCOUNT_PLUGIN_FOLDER, file_path);
91
92         _DEBUG_INFO("path = %s", path);
93         if (!sync_agent_read_whole_file(path, &temp_value, &size)) {
94                 _DEBUG_ERROR("Failed to read file");
95                 *value = 0;
96                 return 0;
97         }
98
99         *value = atoi(temp_value);
100
101         _DEBUG_INFO("value = %d", *value);
102
103 //      if (temp_value != NULL)
104         free(temp_value);
105
106         _EXTERN_FUNC_EXIT;
107
108         return 1;
109 }
110
111 EXPORT_API int sync_agent_unset_file(char *file_path)
112 {
113         _EXTERN_FUNC_ENTER;
114
115         retvm_if(file_path == NULL, 0, "file_path parameter is NULL !!");
116
117         char path[128];
118
119         snprintf(path, sizeof(path), "%s/%s", UTIL_DEFAULT_ACCOUNT_PLUGIN_FOLDER, file_path);
120         _DEBUG_INFO("path = %s", path);
121
122         _EXTERN_FUNC_EXIT;
123
124         return sync_agent_delete_file(path);
125 }