4 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
6 * Licensed under the Apache License, Version 2.0 (the License);
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
22 #include <archive_entry.h>
29 #define DEFAULT_READ_BLOCK_SIZE 10240
36 static mode_t default_mode = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
38 static int file_is_dir(const char *file)
42 if (stat(file, &st) < 0)
44 if (S_ISDIR(st.st_mode))
49 static struct archive_data* get_data_from_archive(char *path, char *item)
52 struct archive_entry *entry;
53 struct archive_data *data = NULL;
60 _R("%s comp path %s, file name %s", __func__, path, item);
62 fd = open(path, O_RDONLY);
67 arch = archive_read_new();
69 _E("Couldn't create archive reader.");
72 if (archive_read_support_compression_all(arch) != ARCHIVE_OK) {
73 _E("%s(%d)", archive_error_string( arch ), archive_errno( arch ));
76 if (archive_read_support_format_all(arch) != ARCHIVE_OK) {
77 _E("%s(%d)", archive_error_string( arch ), archive_errno( arch ));
80 if (archive_read_open_fd(arch, fd, DEFAULT_READ_BLOCK_SIZE) != ARCHIVE_OK) {
81 _E("%s(%d)", archive_error_string( arch ), archive_errno( arch ));
85 while ((ret = archive_read_next_header(arch, &entry)) == ARCHIVE_OK) {
86 if (!S_ISREG(archive_entry_mode(entry)))
88 if (strncmp(archive_entry_pathname(entry), item, strlen(item)))
90 data = (struct archive_data *)malloc(sizeof(struct archive_data));
95 data->len = archive_entry_size(entry);
96 data->buff = (char *)malloc(sizeof(char)*data->len);
101 _R("%s file %s size %d", __func__, archive_entry_pathname(entry), data->len);
102 ret = archive_read_data(arch, data->buff, data->len);
108 /* Close the archives. */
109 if (archive_read_finish(arch) != ARCHIVE_OK)
110 _E("Error closing input archive");
116 static void test_save_data_to_file(char *name, struct archive_data *data)
125 if (!name || !data) {
129 snprintf(path, sizeof(path), "/tmp/%s", name);
130 if (file_is_dir(path))
132 for (i = 0; path[i] != '\0'; ss[i] = path[i], i++)
134 if (i == sizeof(ss) - 1) {
138 if (((path[i] == '/') || (path[i] == '\\')) && (i > 0)) {
140 if (stat(ss, &st) < 0) {
141 ret = mkdir(ss, default_mode);
143 _E("Make Directory is failed");
146 } else if (!S_ISDIR(st.st_mode)) {
147 _E("fail to check dir %s", ss);
153 fd = open(ss, O_CREAT | O_TRUNC | O_WRONLY, 0644);
155 _E("open fail(%s)", strerror(errno));
158 ret = write(fd, data->buff, data->len);
162 _R("%s %s", __func__, path);
165 static int archive_unit(int argc, char **argv)
167 struct archive_data *data = NULL;
168 struct timespec start;
176 clock_gettime(CLOCK_REALTIME, &start);
177 data = get_data_from_archive(argv[2], argv[3]);
178 clock_gettime(CLOCK_REALTIME, &end);
179 delta = (long)((end.tv_sec*1000 + end.tv_nsec/1000000) -
180 (start.tv_sec*1000 + start.tv_nsec/1000000));
182 _R("%s operation time %ldms", __func__, delta);
189 test_save_data_to_file(argv[3], data);
196 static void unit(char *unit, char *status)
200 static void archive_init(void *data)
204 static void archive_exit(void *data)
208 static const struct test_ops archive_test_ops = {
209 .priority = TEST_PRIORITY_NORMAL,
211 .init = archive_init,
212 .exit = archive_exit,
213 .unit = archive_unit,
216 TEST_OPS_REGISTER(&archive_test_ops)