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;