add zip module 82/182482/2
authorJeonghoon Park <jh1979.park@samsung.com>
Mon, 25 Jun 2018 10:48:54 +0000 (19:48 +0900)
committerJeonghoon Park <jh1979.park@samsung.com>
Tue, 26 Jun 2018 04:19:44 +0000 (13:19 +0900)
Change-Id: Id5c0aec4f6ea45029a7630eb683d02e2d13ff706

daemon/CMakeLists.txt
daemon/include/ttd-zip.h [new file with mode: 0644]
daemon/src/ttd-zip.c [new file with mode: 0644]
packaging/tizen-things-daemon.spec

index fb381a3..fdead40 100644 (file)
@@ -17,6 +17,7 @@ pkg_check_modules(DAEMON_PKGS REQUIRED
        capi-network-softap
        capi-network-wifi-manager
        uuid
+       minizip
 )
 
 FOREACH (flag ${DAEMON_PKGS_CFLAGS})
diff --git a/daemon/include/ttd-zip.h b/daemon/include/ttd-zip.h
new file mode 100644 (file)
index 0000000..f657a51
--- /dev/null
@@ -0,0 +1,28 @@
+/*
+ * Copyright (c) 2018 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Flora License, Version 1.1 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an AS IS BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __TT_DAEMON_ZIP_H__
+#define __TT_DAEMON_ZIP_H__
+
+typedef struct _ttd_zip_s ttd_zip;
+
+ttd_zip *ttd_zip_open(const char *zip_name);
+void ttd_zip_close(ttd_zip *zip);
+int ttd_zip_append_file(
+       ttd_zip *zip, const char *src_name, const char *dest_name);
+const char *ttd_zip_get_name(ttd_zip *zip);
+
+#endif /* __TT_DAEMON_ZIP_H__ */
diff --git a/daemon/src/ttd-zip.c b/daemon/src/ttd-zip.c
new file mode 100644 (file)
index 0000000..e982fe0
--- /dev/null
@@ -0,0 +1,167 @@
+/*
+ * Copyright (c) 2018 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Flora License, Version 1.1 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an AS IS BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <zip.h>
+#include <glib.h>
+#include <stdio.h>
+#include "ttd-log.h"
+#include "ttd-zip.h"
+
+struct _ttd_zip_s {
+       zipFile zf;
+       char *zip_name;
+};
+
+static int __append_file(zipFile zf, const char *src, const char *dest)
+{
+       zip_fileinfo zfi = { 0 };
+       FILE *fp = NULL;
+       char buf[1024];
+       int ret = ZIP_OK;
+       int size_read = 0;
+
+       retv_if(!zf, -1);
+       retv_if(!src, -1);
+       retv_if(!dest, -1);
+
+       fp = fopen(src, "rb");
+       retv_if(!fp, -1);
+
+       ret = zipOpenNewFileInZip(zf, dest, &zfi,
+               NULL, 0, NULL, 0, NULL,
+               Z_DEFLATED, Z_DEFAULT_COMPRESSION);
+       retvm_if(ret != ZIP_OK, -1, "failed to zipOpenNewFileInZip() for %s", dest);
+
+       do
+       {
+               ret = ZIP_OK;
+               size_read = (int)fread(buf, 1, 1024, fp);
+               if (size_read  <  1024) {
+                       if (feof(fp) == 0) {
+                               _E("error in reading %s", dest);
+                               ret = ZIP_ERRNO;
+                       }
+               }
+
+               if (size_read > 0) {
+                       ret = zipWriteInFileInZip(zf, buf, size_read);
+                       if (ret < 0)
+                               _E("error in writing %s in the zipfile", dest);
+               }
+       } while ((ret == ZIP_OK) && (size_read > 0));
+
+       zipCloseFileInZip(zf);
+
+       fclose(fp);
+
+       return 0;
+}
+
+ttd_zip *ttd_zip_open(const char *zip_name)
+{
+       ttd_zip *zip = NULL;
+       zipFile zf = NULL;
+       retv_if(!zip_name, NULL);
+
+       zip = g_try_malloc(sizeof(ttd_zip));
+
+       zf = zipOpen(zip_name, APPEND_STATUS_CREATE);
+       if (!zf) {
+               _E("failed to open zip file - %s", zip_name);
+               g_free(zip);
+               return NULL;
+       }
+
+       zip->zf = zf;
+       zip->zip_name = g_strdup(zip_name);
+
+       return zip;
+}
+
+void ttd_zip_close(ttd_zip *zip)
+{
+       ret_if(!zip);
+
+       _D("closing file[%s]", zip->zip_name);
+
+       if (zip->zf)
+               zipClose(zip->zf, NULL);
+
+       g_free(zip->zip_name);
+       g_free(zip);
+}
+
+const char *ttd_zip_get_name(ttd_zip *zip)
+{
+       retv_if(!zip, NULL);
+
+       return zip->zip_name;
+}
+
+int ttd_zip_append_file(
+       ttd_zip *zip, const char *src_name, const char *dest_name)
+{
+       gboolean is_dir = FALSE;
+       int ret = 0;
+
+       retv_if(!zip, -1);
+       retv_if(!src_name, -1);
+       retv_if(!dest_name, -1);
+
+       is_dir = g_file_test(src_name, G_FILE_TEST_IS_DIR);
+       if (is_dir) {
+               const char *name = NULL;
+               unsigned int count = 0;
+               char *dir_name = NULL;
+               GDir *gdir = NULL;
+               GError *error = NULL;
+
+               _D("appending directory[%s] to [%s]", src_name, zip->zip_name);
+               gdir = g_dir_open(src_name, 0, &error);
+               if (!gdir) {
+                       _E("failed to open directory[%s] - %s", src_name, error->message);
+                       g_error_free(error);
+                       return -1;
+               }
+
+               while ((name = g_dir_read_name(gdir))) {
+                       char *s_name = NULL;
+                       char *d_name = NULL;
+                       int ret_d = 0;
+
+                       s_name = g_build_filename(src_name, name, NULL);
+                       d_name = g_build_filename(dest_name, name, NULL);
+
+                       ret_d = ttd_zip_append_file(zip, s_name, d_name);
+                       if (!ret_d)
+                               count++;
+
+                       g_free(s_name);
+                       g_free(d_name);
+               }
+
+               g_free(dir_name);
+               if (count == 0)
+                       _W("nothing in the dir[%s]", src_name);
+       } else {
+               _D("appending file[%s] to [%s]", src_name, zip->zip_name);
+               ret = __append_file(zip->zf, src_name, dest_name);
+               if (ret)
+                       _E("failed to append file[%s] to [%s]", src_name, zip->zip_name);
+       }
+
+       return ret;
+}
index b5efd74..dde512f 100644 (file)
@@ -24,6 +24,7 @@ BuildRequires:  pkgconfig(vconf)
 BuildRequires:  pkgconfig(uuid)
 BuildRequires:  pkgconfig(capi-network-softap)
 BuildRequires:  pkgconfig(capi-network-wifi-manager)
+BuildRequires:  pkgconfig(minizip)
 
 %description
 Tizen Things daemon