tAdd pack_creat() to create tar.bz2 - pm - barely a pack manager
HTML git clone git://z3bra.org/pm
DIR Log
DIR Files
DIR Refs
DIR README
DIR LICENSE
---
DIR commit 8ef8e1103b492188efea63c94b4ba538dff30aec
DIR parent 96d5fa5f539e7b9efff7b2e243a652c0ef12a830
HTML Author: z3bra <willyatmailoodotorg>
Date: Fri, 18 Dec 2015 20:03:54 +0100
Add pack_creat() to create tar.bz2
Diffstat:
M pack.c | 72 ++++++++++++++++++++++++++++---
1 file changed, 66 insertions(+), 6 deletions(-)
---
DIR diff --git a/pack.c b/pack.c
t@@ -1,7 +1,13 @@
+#include <fcntl.h>
#include <stdio.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+
#include <archive.h>
#include <archive_entry.h>
+#define PACKAGE_BUFF_SIZE 8192
+
/*
* output the content of an archive
*/
t@@ -11,25 +17,73 @@ pack_list(char *filename)
struct archive *a;
struct archive_entry *e;
int r;
-
+
+ /* configure archive to support all types */
a = archive_read_new();
archive_read_support_filter_all(a);
archive_read_support_format_all(a);
/* set blocksize to 0 as libarchive will choose the best size anyway */
r = archive_read_open_filename(a, filename, 0);
-
if (r != ARCHIVE_OK)
- return 1;
+ return r;
+ /* output each filename to stdout */
while(archive_read_next_header(a, &e) == ARCHIVE_OK) {
puts(archive_entry_pathname(e));
}
r = archive_read_free(a);
+ if (r != ARCHIVE_OK)
+ return r;
+ return 0;
+}
+
+/*
+ * create an archive out of a file name (or directory
+ */
+int
+pack_creat(const char *out, char **filename)
+{
+ struct archive *a;
+ struct archive_entry *e;
+ struct stat st;
+ char buf[PACKAGE_BUFF_SIZE];
+ size_t len;
+ int fd, r;
+
+ a = archive_write_new();
+ archive_write_add_filter_bzip2(a);
+ archive_write_set_format_pax_restricted(a);
+
+ r = archive_write_open_filename(a, out);
if (r != ARCHIVE_OK)
- return 1;
+ return r;
+
+ while (*filename) {
+ stat(*filename, &st);
+ e = archive_entry_new();
+
+ archive_entry_set_pathname(e, *filename);
+ archive_entry_set_size(e, st.st_size);
+ archive_entry_set_filetype(e, AE_IFREG);
+ archive_entry_set_mode(e, st.st_mode);
+ archive_write_header(a, e);
+
+ fd = open(*filename, O_RDONLY);
+ if (fd < 0)
+ return fd;
+
+ while ((len = read(fd, buf, sizeof(buf))) > 0)
+ archive_write_data(a, buf, len);
+
+ close(fd);
+ archive_entry_free(e);
+ filename++;
+ }
+
+ archive_write_free(a);
return 0;
}
t@@ -37,8 +91,14 @@ pack_list(char *filename)
int
main (int argc, char **argv)
{
- while (*(++argv))
- pack_list(*argv);
+ const char *out;
+
+ if (argc == 2)
+ pack_list(*(++argv));
+ if (argc > 2) {
+ out = *(++argv);
+ pack_creat(out, ++argv);
+ }
return 0;
}
\ No newline at end of file