AFLOW
 
Loading...
Searching...
No Matches
apack.c
Go to the documentation of this file.
1// ***************************************************************************
2// * *
3// * Aflow STEFANO CURTAROLO - Duke University 2003-2024 *
4// * *
5// ***************************************************************************
6// This is a minimal tool to create xz compressed tar archives to prepare data
7// that fot embedding in src/AUROSTD/aurostd_data.cpp
8// note: should only be utilized by the build process
9// usage: apack ARCHIVE_NAME [FILES_TO_PACK ...]
10// author: hagen.eckert@duke.edu
11
12#include <archive.h>
13#include <archive_entry.h>
14
15#include <fcntl.h>
16#include <stdio.h>
17#include <unistd.h>
18
19static char buff[16384];
20
21int main(int argc, const char **argv) {
22 struct archive *a;
23 struct archive *disk;
24 struct archive_entry *entry;
25 int r;
26 int fd;
27 ssize_t len;
28
29 // initialized the new archive
30 a = archive_write_new();
31 archive_write_set_format_pax_restricted(a);
32 archive_write_add_filter_xz(a);
33 archive_write_set_options(a, "compression-level=5,threads=1");
34
35 // open the archive to write
36 r = archive_write_open_filename(a, argv[1]);
37 if (r != ARCHIVE_OK) {
38 printf("%s\n", archive_error_string(a));
39 return (1);
40 }
41
42 // iterate over files to pack
43 for (int i = 2; i < argc; ++i) {
44 disk = archive_read_disk_new();
45 archive_read_disk_set_standard_lookup(disk);
46 r = archive_read_disk_open(disk, argv[i]);
47 if (r != ARCHIVE_OK) {
48 printf("%s\n", archive_error_string(a));
49 return (1);
50 }
51 for (;;) {
52 entry = archive_entry_new();
53 r = archive_read_next_header2(disk, entry);
54 if (r == ARCHIVE_EOF) {
55 break;
56 }
57 if (r != ARCHIVE_OK) {
58 printf("%s\n", archive_error_string(disk));
59 return (1);
60 }
61 archive_read_disk_descend(disk);
62 r = archive_write_header(a, entry);
63 if (r < ARCHIVE_OK) {
64 printf("%s\n", archive_error_string(a));
65 }
66 if (r == ARCHIVE_FATAL) {
67 printf("FATAL error - unable to write entry to archive.");
68 return (1);
69 }
70 if (r > ARCHIVE_FAILED) {
71 fd = open(archive_entry_sourcepath(entry), O_RDONLY);
72 len = read(fd, buff, sizeof(buff));
73 while (len > 0) {
74 archive_write_data(a, buff, len);
75 len = read(fd, buff, sizeof(buff));
76 }
77 close(fd);
78 }
79 archive_entry_free(entry);
80 }
81 archive_write_free(disk);
82 }
83 archive_write_close(a);
84 archive_write_free(a);
85 return (0);
86}
int main(int argc, const char **argv)
Definition apack.c:21
static char buff[16384]
Definition apack.c:19