tizen 2.3 release
[framework/system/deviced.git] / src / auto-test / archive.c
1 /*
2  * test
3  *
4  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
5  *
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
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
18
19 #include <sys/stat.h>
20 #include <fcntl.h>
21 #include <archive.h>
22 #include <archive_entry.h>
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26
27 #include "test.h"
28
29 #define DEFAULT_READ_BLOCK_SIZE 10240
30
31 struct archive_data {
32         ssize_t len;
33         char *buff;
34 };
35
36 static mode_t default_mode = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
37
38 static int file_is_dir(const char *file)
39 {
40         struct stat st;
41
42         if (stat(file, &st) < 0)
43                 return 0;
44         if (S_ISDIR(st.st_mode))
45                 return 1;
46         return 0;
47 }
48
49 static struct archive_data* get_data_from_archive(char *path, char *item)
50 {
51         struct archive *arch;
52         struct archive_entry *entry;
53         struct archive_data *data = NULL;
54         int fd;
55         int ret;
56
57         if (!path || !item)
58                 return NULL;
59
60         _R("%s comp path %s, file name %s", __func__, path, item);
61
62         fd = open(path, O_RDONLY);
63         if (fd < 0) {
64                 _E("open fail");
65                 return NULL;
66         }
67         arch = archive_read_new();
68         if (arch == NULL) {
69                 _E("Couldn't create archive reader.");
70                 goto out;
71         }
72         if (archive_read_support_compression_all(arch) != ARCHIVE_OK) {
73                 _E("%s(%d)", archive_error_string( arch ), archive_errno( arch ));
74                 goto out;
75         }
76         if (archive_read_support_format_all(arch) != ARCHIVE_OK) {
77                 _E("%s(%d)", archive_error_string( arch ), archive_errno( arch ));
78                 goto out;
79         }
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 ));
82                 goto out;
83         }
84
85         while ((ret = archive_read_next_header(arch, &entry)) == ARCHIVE_OK)  {
86                 if (!S_ISREG(archive_entry_mode(entry)))
87                         continue;
88                 if (strncmp(archive_entry_pathname(entry), item, strlen(item)))
89                         continue;
90                 data = (struct archive_data *)malloc(sizeof(struct archive_data));
91                 if (!data) {
92                         _E("malloc fail");
93                         goto finish;
94                 }
95                 data->len = archive_entry_size(entry);
96                 data->buff = (char *)malloc(sizeof(char)*data->len);
97                 if (!data->buff) {
98                         _E("malloc fail");
99                         goto finish;
100                 }
101                 _R("%s file %s size %d", __func__, archive_entry_pathname(entry), data->len);
102                 ret = archive_read_data(arch, data->buff, data->len);
103                 if (ret < 0)
104                         _E("read fail");
105                 break;
106         }
107 finish:
108         /* Close the archives.  */
109         if (archive_read_finish(arch) != ARCHIVE_OK)
110                 _E("Error closing input archive");
111 out:
112         close(fd);
113         return data;
114 }
115
116 static void test_save_data_to_file(char *name, struct archive_data *data)
117 {
118         char path[PATH_MAX];
119         char ss[PATH_MAX];
120         struct stat st;
121         int fd;
122         int ret;
123         int i;
124
125         if (!name || !data) {
126                 _E("input fail");
127                 return;
128         }
129         snprintf(path, sizeof(path), "/tmp/%s", name);
130         if (file_is_dir(path))
131                 return;
132         for (i = 0; path[i] != '\0'; ss[i] = path[i], i++)
133         {
134                 if (i == sizeof(ss) - 1) {
135                         _E("fail");
136                         return;
137                 }
138                 if (((path[i] == '/') || (path[i] == '\\')) && (i > 0)) {
139                         ss[i] = '\0';
140                         if (stat(ss, &st) < 0) {
141                                 ret = mkdir(ss, default_mode);
142                                 if (ret < 0) {
143                                         _E("Make Directory is failed");
144                                         return;
145                                 }
146                         } else if (!S_ISDIR(st.st_mode)) {
147                                 _E("fail to check dir %s", ss);
148                                 return;
149                         }
150                 }
151         }
152         ss[i] = '\0';
153         fd = open(ss, O_CREAT | O_TRUNC | O_WRONLY, 0644);
154         if (fd < 0) {
155                 _E("open fail(%s)", strerror(errno));
156                 return;
157         }
158         ret = write(fd, data->buff, data->len);
159         if (ret < 0)
160                 _E("write fail");
161         close(fd);
162         _R("%s %s", __func__, path);
163 }
164
165 static int archive_unit(int argc, char **argv)
166 {
167         struct archive_data *data = NULL;
168         struct timespec start;
169         struct timespec end;
170         long delta;
171
172         if (argc != 4) {
173                 _E("arg fail");
174                 goto out;
175         }
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));
181
182         _R("%s operation time %ldms", __func__, delta);
183         if (!data)
184                 goto out;
185         if (!data->buff) {
186                 free(data);
187                 goto out;
188         }
189         test_save_data_to_file(argv[3], data);
190         free(data->buff);
191         free(data);
192 out:
193         return 0;
194 }
195
196 static void unit(char *unit, char *status)
197 {
198 }
199
200 static void archive_init(void *data)
201 {
202 }
203
204 static void archive_exit(void *data)
205 {
206 }
207
208 static const struct test_ops archive_test_ops = {
209         .priority = TEST_PRIORITY_NORMAL,
210         .name     = "archive",
211         .init     = archive_init,
212         .exit    = archive_exit,
213         .unit    = archive_unit,
214 };
215
216 TEST_OPS_REGISTER(&archive_test_ops)