replace value to macro for HTTP OK
[apps/native/tizen-things-daemon.git] / daemon / src / ttd-zip.c
1 /*
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Flora License, Version 1.1 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://floralicense.org/license/
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <zip.h>
18 #include <glib.h>
19 #include <stdio.h>
20 #include "ttd-log.h"
21 #include "ttd-zip.h"
22
23 struct _ttd_zip_s {
24         zipFile zf;
25         char *zip_name;
26 };
27
28 static int __append_file(zipFile zf, const char *src, const char *dest)
29 {
30         zip_fileinfo zfi = { 0 };
31         FILE *fp = NULL;
32         char buf[1024];
33         int ret = ZIP_OK;
34         int size_read = 0;
35
36         retv_if(!zf, -1);
37         retv_if(!src, -1);
38         retv_if(!dest, -1);
39
40         fp = fopen(src, "rb");
41         retv_if(!fp, -1);
42
43         ret = zipOpenNewFileInZip(zf, dest, &zfi,
44                 NULL, 0, NULL, 0, NULL,
45                 Z_DEFLATED, Z_DEFAULT_COMPRESSION);
46         retvm_if(ret != ZIP_OK, -1, "failed to zipOpenNewFileInZip() for %s", dest);
47
48         do
49         {
50                 ret = ZIP_OK;
51                 size_read = (int)fread(buf, 1, 1024, fp);
52                 if (size_read  <  1024) {
53                         if (feof(fp) == 0) {
54                                 _E("error in reading %s", dest);
55                                 ret = ZIP_ERRNO;
56                         }
57                 }
58
59                 if (size_read > 0) {
60                         ret = zipWriteInFileInZip(zf, buf, size_read);
61                         if (ret < 0)
62                                 _E("error in writing %s in the zipfile", dest);
63                 }
64         } while ((ret == ZIP_OK) && (size_read > 0));
65
66         zipCloseFileInZip(zf);
67
68         fclose(fp);
69
70         return 0;
71 }
72
73 ttd_zip *ttd_zip_open(const char *zip_name)
74 {
75         ttd_zip *zip = NULL;
76         zipFile zf = NULL;
77         retv_if(!zip_name, NULL);
78
79         zip = g_try_malloc(sizeof(ttd_zip));
80
81         zf = zipOpen(zip_name, APPEND_STATUS_CREATE);
82         if (!zf) {
83                 _E("failed to open zip file - %s", zip_name);
84                 g_free(zip);
85                 return NULL;
86         }
87
88         zip->zf = zf;
89         zip->zip_name = g_strdup(zip_name);
90
91         return zip;
92 }
93
94 void ttd_zip_close(ttd_zip *zip)
95 {
96         ret_if(!zip);
97
98         _D("closing file[%s]", zip->zip_name);
99
100         if (zip->zf)
101                 zipClose(zip->zf, NULL);
102
103         g_free(zip->zip_name);
104         g_free(zip);
105 }
106
107 const char *ttd_zip_get_name(ttd_zip *zip)
108 {
109         retv_if(!zip, NULL);
110
111         return zip->zip_name;
112 }
113
114 int ttd_zip_append_file(
115         ttd_zip *zip, const char *src_name, const char *dest_name)
116 {
117         gboolean is_dir = FALSE;
118         int ret = 0;
119
120         retv_if(!zip, -1);
121         retv_if(!src_name, -1);
122         retv_if(!dest_name, -1);
123
124         is_dir = g_file_test(src_name, G_FILE_TEST_IS_DIR);
125         if (is_dir) {
126                 const char *name = NULL;
127                 unsigned int count = 0;
128                 char *dir_name = NULL;
129                 GDir *gdir = NULL;
130                 GError *error = NULL;
131
132                 _D("appending directory[%s] to [%s]", src_name, zip->zip_name);
133                 gdir = g_dir_open(src_name, 0, &error);
134                 if (!gdir) {
135                         _E("failed to open directory[%s] - %s", src_name, error->message);
136                         g_error_free(error);
137                         return -1;
138                 }
139
140                 while ((name = g_dir_read_name(gdir))) {
141                         char *s_name = NULL;
142                         char *d_name = NULL;
143                         int ret_d = 0;
144
145                         s_name = g_build_filename(src_name, name, NULL);
146                         d_name = g_build_filename(dest_name, name, NULL);
147
148                         ret_d = ttd_zip_append_file(zip, s_name, d_name);
149                         if (!ret_d)
150                                 count++;
151
152                         g_free(s_name);
153                         g_free(d_name);
154                 }
155
156                 g_free(dir_name);
157                 if (count == 0)
158                         _W("nothing in the dir[%s]", src_name);
159         } else {
160                 _D("appending file[%s] to [%s]", src_name, zip->zip_name);
161                 ret = __append_file(zip->zf, src_name, dest_name);
162                 if (ret)
163                         _E("failed to append file[%s] to [%s]", src_name, zip->zip_name);
164         }
165
166         return ret;
167 }