Tizen 2.1 base
[platform/core/system/sync-agent.git] / src / framework / utility / fw_compress.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 <stdlib.h>
19 #include <string.h>
20 #include <sys/types.h>
21 #include <sys/wait.h>
22 #include <unistd.h>
23 #include <errno.h>
24 #include <glib.h>
25 #include <glib/gprintf.h>
26
27 #include "utility/sync_util.h"
28 #include "utility/fw_compress.h"
29 #include "fsapi/operation.h"
30
31 #ifndef EXPORT_API
32 #define EXPORT_API __attribute__ ((visibility("default")))
33 #endif
34
35 #ifndef SYNC_AGENT_LOG
36 #undef LOG_TAG
37 #define LOG_TAG "AF_COMPRESS"
38 #endif
39
40 EXPORT_API sync_agent_util_compress_result_e sync_agent_compress(sync_agent_util_compress_type_e type, const char *input_directory_path, const char *output_file_path)
41 {
42         _EXTERN_FUNC_ENTER;
43
44         if (input_directory_path == NULL || output_file_path == NULL) {
45                 return SYNC_AGENT_UTIL_COMPRESS_FAIL;
46         }
47
48         if (!sync_agent_is_existing_fs(input_directory_path)) {
49                 _DEBUG_ERROR("tar input file not existing !!");
50                 return SYNC_AGENT_UTIL_COMPRESS_FAIL;
51         }
52
53         char *command = NULL;
54
55         switch (type) {
56         case SYNC_AGENT_UTIL_COMPRESS_TYPE_TAR:
57                 {
58                         int command_len = strlen("tar cvzpf ") + strlen(output_file_path) + strlen(" ") + strlen(input_directory_path) + 1;
59                         int len = 0;
60                         command = (char *)calloc(command_len, sizeof(char));
61                         if (command == NULL)
62                                 return SYNC_AGENT_UTIL_COMPRESS_FAIL;
63
64                         len = g_strlcat(command, "tar cvzpf ", command_len);
65                         len = g_strlcat(command, output_file_path, command_len);
66                         len = g_strlcat(command, " ", command_len);
67                         len = g_strlcat(command, input_directory_path, command_len);
68
69                         if (len >= command_len) {
70                                 _DEBUG_ERROR("command buffer overflow !!");
71                                 free(command);
72                                 return SYNC_AGENT_UTIL_COMPRESS_FAIL;
73                         }
74
75                         int pid = fork();
76                         if (pid == -1) {
77                                 _DEBUG_ERROR("Fork failed to create a process");
78                                 free(command);
79                                 return SYNC_AGENT_UTIL_COMPRESS_FAIL;
80                         } else if (pid == 0) {
81                                 char *argv[4];
82
83                                 argv[0] = "sh";
84                                 argv[1] = "-c";
85                                 argv[2] = command;
86                                 argv[3] = 0;
87
88                                 execv("/bin/sh", argv);
89
90                                 abort();
91                         }
92
93                         int status = 0;
94                         do {
95                                 if (waitpid(pid, &status, 0) == -1) {
96                                         if (errno != EINTR) {
97                                                 free(command);
98                                                 _EXTERN_FUNC_EXIT;
99                                                 return SYNC_AGENT_UTIL_COMPRESS_SUCCESS;
100                                         } else {
101                                                 free(command);
102                                                 return SYNC_AGENT_UTIL_COMPRESS_FAIL;
103                                         }
104                                 }
105                         } while (1);
106                 }
107                 break;
108         case SYNC_AGENT_UTIL_COMPRESS_TYPE_ZIP:
109                 {
110                         /*
111                          * Not yet support
112                          */
113                 }
114                 break;
115         default:
116                 _DEBUG_ERROR("Not support compress type!!");
117                 break;
118         }
119
120         return SYNC_AGENT_UTIL_COMPRESS_FAIL;
121 }
122
123 EXPORT_API sync_agent_util_compress_result_e sync_agent_uncompress(sync_agent_util_compress_type_e type, const char *input_file_path, const char *output_direcory_path)
124 {
125         _EXTERN_FUNC_ENTER;
126
127         if (input_file_path == NULL || output_direcory_path == NULL) {
128                 return SYNC_AGENT_UTIL_COMPRESS_FAIL;
129         }
130
131         if (!sync_agent_is_existing_fs(input_file_path)) {
132                 _DEBUG_ERROR("tar input file not existing !!");
133                 return SYNC_AGENT_UTIL_COMPRESS_FAIL;
134         }
135
136         char *command = NULL;
137
138         switch (type) {
139         case SYNC_AGENT_UTIL_COMPRESS_TYPE_TAR:
140                 {
141                         int command_len = strlen("tar xvf ") + strlen(input_file_path) + strlen(" -C ") + strlen(output_direcory_path) + 1;
142                         int len = 0;
143                         command = (char *)calloc(command_len, sizeof(char));
144                         if (command == NULL)
145                                 return SYNC_AGENT_UTIL_COMPRESS_FAIL;
146
147                         len = g_strlcat(command, "tar xvf ", command_len);
148                         len = g_strlcat(command, input_file_path, command_len);
149                         len = g_strlcat(command, " -C ", command_len);
150                         len = g_strlcat(command, output_direcory_path, command_len);
151
152                         if (len >= command_len) {
153                                 _DEBUG_ERROR("command buffer overflow !!");
154                                 free(command);
155                                 return SYNC_AGENT_UTIL_COMPRESS_FAIL;
156                         }
157
158                         int pid = fork();
159                         if (pid == -1) {
160                                 _DEBUG_ERROR("Fork failed to create a process");
161                                 free(command);
162                                 return SYNC_AGENT_UTIL_COMPRESS_FAIL;
163                         } else if (pid == 0) {
164                                 char *argv[4];
165
166                                 argv[0] = "sh";
167                                 argv[1] = "-c";
168                                 argv[2] = command;
169                                 argv[3] = 0;
170
171                                 execv("/bin/sh", argv);
172
173                                 abort();
174                         }
175
176                         int status = 0;
177                         do {
178                                 if (waitpid(pid, &status, 0) == -1) {
179                                         if (errno != EINTR) {
180                                                 free(command);
181                                                 _EXTERN_FUNC_EXIT;
182                                                 return SYNC_AGENT_UTIL_COMPRESS_SUCCESS;
183                                         } else {
184                                                 free(command);
185                                                 return SYNC_AGENT_UTIL_COMPRESS_FAIL;
186                                         }
187                                 }
188                         } while (1);
189                 }
190                 break;
191         case SYNC_AGENT_UTIL_COMPRESS_TYPE_ZIP:
192                 {
193                         /*
194                          * Not yet support
195                          */
196                 }
197                 break;
198         default:
199                 _DEBUG_ERROR("Not support compress type!!");
200                 break;
201         }
202
203         return SYNC_AGENT_UTIL_COMPRESS_FAIL;
204 }