3 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include <sys/types.h>
25 #include <glib/gprintf.h>
27 #include "utility/sync_util.h"
28 #include "utility/fw_compress.h"
29 #include "fsapi/operation.h"
32 #define EXPORT_API __attribute__ ((visibility("default")))
35 #ifndef SYNC_AGENT_LOG
37 #define LOG_TAG "AF_COMPRESS"
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)
44 if (input_directory_path == NULL || output_file_path == NULL) {
45 return SYNC_AGENT_UTIL_COMPRESS_FAIL;
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;
56 case SYNC_AGENT_UTIL_COMPRESS_TYPE_TAR:
58 int command_len = strlen("tar cvzpf ") + strlen(output_file_path) + strlen(" ") + strlen(input_directory_path) + 1;
60 command = (char *)calloc(command_len, sizeof(char));
62 return SYNC_AGENT_UTIL_COMPRESS_FAIL;
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);
69 if (len >= command_len) {
70 _DEBUG_ERROR("command buffer overflow !!");
72 return SYNC_AGENT_UTIL_COMPRESS_FAIL;
77 _DEBUG_ERROR("Fork failed to create a process");
79 return SYNC_AGENT_UTIL_COMPRESS_FAIL;
80 } else if (pid == 0) {
88 execv("/bin/sh", argv);
95 if (waitpid(pid, &status, 0) == -1) {
99 return SYNC_AGENT_UTIL_COMPRESS_SUCCESS;
102 return SYNC_AGENT_UTIL_COMPRESS_FAIL;
108 case SYNC_AGENT_UTIL_COMPRESS_TYPE_ZIP:
116 _DEBUG_ERROR("Not support compress type!!");
120 return SYNC_AGENT_UTIL_COMPRESS_FAIL;
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)
127 if (input_file_path == NULL || output_direcory_path == NULL) {
128 return SYNC_AGENT_UTIL_COMPRESS_FAIL;
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;
136 char *command = NULL;
139 case SYNC_AGENT_UTIL_COMPRESS_TYPE_TAR:
141 int command_len = strlen("tar xvf ") + strlen(input_file_path) + strlen(" -C ") + strlen(output_direcory_path) + 1;
143 command = (char *)calloc(command_len, sizeof(char));
145 return SYNC_AGENT_UTIL_COMPRESS_FAIL;
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);
152 if (len >= command_len) {
153 _DEBUG_ERROR("command buffer overflow !!");
155 return SYNC_AGENT_UTIL_COMPRESS_FAIL;
160 _DEBUG_ERROR("Fork failed to create a process");
162 return SYNC_AGENT_UTIL_COMPRESS_FAIL;
163 } else if (pid == 0) {
171 execv("/bin/sh", argv);
178 if (waitpid(pid, &status, 0) == -1) {
179 if (errno != EINTR) {
182 return SYNC_AGENT_UTIL_COMPRESS_SUCCESS;
185 return SYNC_AGENT_UTIL_COMPRESS_FAIL;
191 case SYNC_AGENT_UTIL_COMPRESS_TYPE_ZIP:
199 _DEBUG_ERROR("Not support compress type!!");
203 return SYNC_AGENT_UTIL_COMPRESS_FAIL;
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)
211 if (input_file_path == NULL || output_direcory_path == NULL) {
212 return SYNC_AGENT_UTIL_COMPRESS_FAIL;
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;
220 char *command = NULL;
223 case SYNC_AGENT_UTIL_COMPRESS_TYPE_TAR:
225 int command_len = strlen("tar xvfp ") + strlen(input_file_path) + strlen(" -C ") + strlen(output_direcory_path) +strlen(" delta.ua")+ 1;
227 command = (char *)calloc(command_len, sizeof(char));
229 return SYNC_AGENT_UTIL_COMPRESS_FAIL;
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);
237 if (len >= command_len) {
238 _DEBUG_ERROR("command buffer overflow !!");
240 return SYNC_AGENT_UTIL_COMPRESS_FAIL;
245 _DEBUG_ERROR("Fork failed to create a process");
247 return SYNC_AGENT_UTIL_COMPRESS_FAIL;
248 } else if (pid == 0) {
256 execv("/bin/sh", argv);
263 if (waitpid(pid, &status, 0) == -1) {
264 if (errno != EINTR) {
267 //return SYNC_AGENT_UTIL_COMPRESS_SUCCESS;
268 _DEBUG_INFO("dalta.ua command success!!");
272 //return SYNC_AGENT_UTIL_COMPRESS_FAIL;
273 _DEBUG_INFO("dalta.ua command failed!!");
280 case SYNC_AGENT_UTIL_COMPRESS_TYPE_ZIP:
288 _DEBUG_ERROR("Not support compress type!!");
293 return SYNC_AGENT_UTIL_COMPRESS_FAIL;
298 int command_len = strlen("mv ") + strlen(input_file_path) + strlen(" ") + strlen(output_direcory_path) +strlen("/delta.tar")+ 1;
300 command = (char *)calloc(command_len, sizeof(char));
302 return SYNC_AGENT_UTIL_COMPRESS_FAIL;
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);
310 if (len >= command_len) {
311 _DEBUG_ERROR("command buffer overflow !!");
313 return SYNC_AGENT_UTIL_COMPRESS_FAIL;
318 _DEBUG_ERROR("Fork failed to create a process");
320 return SYNC_AGENT_UTIL_COMPRESS_FAIL;
321 } else if (pid == 0) {
329 execv("/bin/sh", argv);
336 if (waitpid(pid, &status, 0) == -1) {
337 if (errno != EINTR) {
340 return SYNC_AGENT_UTIL_COMPRESS_SUCCESS;
343 return SYNC_AGENT_UTIL_COMPRESS_FAIL;
350 return SYNC_AGENT_UTIL_COMPRESS_FAIL;