[Internal: merge sync-agent]
[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 }
205
206
207 EXPORT_API sync_agent_util_compress_result_e sync_agent_ua_uncompress(sync_agent_util_compress_type_e type, const char *input_file_path, const char *output_direcory_path)
208 {
209         _EXTERN_FUNC_ENTER;
210
211         if (input_file_path == NULL || output_direcory_path == NULL) {
212                 return SYNC_AGENT_UTIL_COMPRESS_FAIL;
213         }
214
215         if (!sync_agent_is_existing_fs(input_file_path)) {
216                 _DEBUG_ERROR("tar input file not existing !!");
217                 return SYNC_AGENT_UTIL_COMPRESS_FAIL;
218         }
219
220         char *command = NULL;
221
222         switch (type) {
223         case SYNC_AGENT_UTIL_COMPRESS_TYPE_TAR:
224                 {
225                         int command_len = strlen("tar xvfp ") + strlen(input_file_path) + strlen(" -C ") + strlen(output_direcory_path) +strlen(" delta.ua")+ 1;
226                         int len = 0;
227                         command = (char *)calloc(command_len, sizeof(char));
228                         if (command == NULL)
229                                 return SYNC_AGENT_UTIL_COMPRESS_FAIL;
230
231                         len = g_strlcat(command, "tar xvfp ", command_len);
232                         len = g_strlcat(command, input_file_path, command_len);
233                         len = g_strlcat(command, " -C ", command_len);
234                         len = g_strlcat(command, output_direcory_path, command_len);
235                         len = g_strlcat(command, " delta.ua", command_len);
236
237                         if (len >= command_len) {
238                                 _DEBUG_ERROR("command buffer overflow !!");
239                                 free(command);
240                                 return SYNC_AGENT_UTIL_COMPRESS_FAIL;
241                         }
242
243                         int pid = fork();
244                         if (pid == -1) {
245                                 _DEBUG_ERROR("Fork failed to create a process");
246                                 free(command);
247                                 return SYNC_AGENT_UTIL_COMPRESS_FAIL;
248                         } else if (pid == 0) {
249                                 char *argv[4];
250
251                                 argv[0] = "sh";
252                                 argv[1] = "-c";
253                                 argv[2] = command;
254                                 argv[3] = 0;
255
256                                 execv("/bin/sh", argv);
257
258                                 abort();
259                         }
260
261                         int status = 0;
262                         do {
263                                 if (waitpid(pid, &status, 0) == -1) {
264                                         if (errno != EINTR) {
265                                                 free(command);
266                                                 //_EXTERN_FUNC_EXIT;
267                                                 //return SYNC_AGENT_UTIL_COMPRESS_SUCCESS;
268                                                 _DEBUG_INFO("dalta.ua command success!!");
269                                                 goto TAR_MV_EXECUTE;
270                                         } else {
271                                                 free(command);
272                                                 //return SYNC_AGENT_UTIL_COMPRESS_FAIL;
273                                                 _DEBUG_INFO("dalta.ua command failed!!");
274                                                 goto TAR_MV_EXECUTE;
275                                         }
276                                 }
277                         } while (1);
278                 }
279                 break;
280         case SYNC_AGENT_UTIL_COMPRESS_TYPE_ZIP:
281                 {
282                         /*
283                          * Not yet support
284                          */
285                 }
286                 break;
287         default:
288                 _DEBUG_ERROR("Not support compress type!!");
289                 break;
290         }
291
292         _EXTERN_FUNC_EXIT;
293         return SYNC_AGENT_UTIL_COMPRESS_FAIL;
294
295
296 TAR_MV_EXECUTE:
297         {
298                 int command_len = strlen("mv ") + strlen(input_file_path) + strlen(" ") + strlen(output_direcory_path) +strlen("/delta.tar")+ 1;
299                 int len = 0;
300                 command = (char *)calloc(command_len, sizeof(char));
301                 if (command == NULL)
302                         return SYNC_AGENT_UTIL_COMPRESS_FAIL;
303
304                 len = g_strlcat(command, "mv ", command_len);
305                 len = g_strlcat(command, input_file_path, command_len);
306                 len = g_strlcat(command, " ", command_len);
307                 len = g_strlcat(command, output_direcory_path, command_len);
308                 len = g_strlcat(command, "/delta.tar", command_len);
309
310                 if (len >= command_len) {
311                         _DEBUG_ERROR("command buffer overflow !!");
312                         free(command);
313                         return SYNC_AGENT_UTIL_COMPRESS_FAIL;
314                 }
315
316                 int pid = fork();
317                 if (pid == -1) {
318                         _DEBUG_ERROR("Fork failed to create a process");
319                         free(command);
320                         return SYNC_AGENT_UTIL_COMPRESS_FAIL;
321                 } else if (pid == 0) {
322                         char *argv[4];
323
324                         argv[0] = "sh";
325                         argv[1] = "-c";
326                         argv[2] = command;
327                         argv[3] = 0;
328
329                         execv("/bin/sh", argv);
330
331                         abort();
332                 }
333
334                 int status = 0;
335                 do {
336                         if (waitpid(pid, &status, 0) == -1) {
337                                 if (errno != EINTR) {
338                                         free(command);
339                                         _EXTERN_FUNC_EXIT;
340                                         return SYNC_AGENT_UTIL_COMPRESS_SUCCESS;
341                                 } else {
342                                         free(command);
343                                         return SYNC_AGENT_UTIL_COMPRESS_FAIL;
344                                 }
345                         }
346                 } while (1);
347         }
348
349         _EXTERN_FUNC_EXIT;
350         return SYNC_AGENT_UTIL_COMPRESS_FAIL;
351 }