Line data Source code
1 :
2 : #include "InterfaceHeader.h"
3 : struct descr {
4 : char file_name [256];
5 : size_t data_length;
6 : size_t data_start;
7 : } __attribute__ ((packed));
8 : struct archive {
9 : enum commands command;
10 : char *file_name;
11 : int fmode;
12 : int fd;
13 : uint32_t files_count;
14 : size_t append_files_count;
15 : size_t headers_offset;
16 : struct descr *file_descriptors;
17 : };
18 : struct archive archive = {0};
19 :
20 19 : int write_archive (char *in_file, struct archive *archive) {
21 19 : char *input_file = (char *) malloc (1000 * sizeof (char));
22 19 : strcpy (input_file, in_file);
23 19 : write_archive_data (input_file, archive);
24 19 : write_archive_header (archive);
25 1 : return 1;
26 : }
27 :
28 19 : int write_archive_data (char *input_file_name, struct archive *archive) {
29 19 : lseek (archive -> fd, archive -> headers_offset, SEEK_END);
30 19 : printf ("Archive %s: writing file %s\n", archive -> file_name, input_file_name);
31 19 : append_file (archive, input_file_name);
32 19 : return 0;
33 : }
34 :
35 19 : int append_file (struct archive *archive, char *file_name) {
36 19 : struct descr *new_descr = archive->file_descriptors + archive->files_count;
37 19 : if (archive->files_count) {
38 0 : struct descr *last_descr = new_descr - 1;
39 0 : new_descr->data_start = last_descr->data_start + last_descr->data_length;
40 : }
41 : else
42 19 : new_descr->data_start = 0;
43 : struct stat stat_data;
44 19 : if (-1 == stat (file_name, &stat_data)) {
45 19 : return 0;
46 : }
47 0 : new_descr->data_length = stat_data.st_size;
48 0 : int fd = open (file_name, O_RDONLY);
49 0 : if (-1 == fd) {
50 0 : return 0;
51 : }
52 0 : void *file_data = mmap (NULL, new_descr->data_length, PROT_READ, MAP_PRIVATE, fd, 0);
53 0 : if (MAP_FAILED == file_data) {
54 0 : close (fd);
55 0 : return 0;
56 : }
57 0 : check_io_errors (write (archive -> fd, file_data, new_descr -> data_length), "write()");
58 0 : munmap (file_data, new_descr -> data_length);
59 0 : close (fd);
60 0 : strncpy (new_descr -> file_name, file_name, 255);
61 0 : return ++archive->files_count;
62 : }
63 :
64 20 : int check_io_errors (int ret_code, const char *operation) {
65 20 : if (-1 == ret_code) {
66 18 : exit (- 1);
67 : }
68 2 : return ret_code;
69 : }
70 :
71 19 : int write_archive_header (struct archive *archive) {
72 19 : check_io_errors (write (archive -> fd, archive -> file_descriptors, archive -> files_count * sizeof (struct descr)), "write()");
73 1 : check_io_errors (write (archive -> fd, & archive -> files_count, sizeof (archive -> files_count)), "write()");
74 1 : return 0;
75 : }
76 :
77 19 : int GRAFT_INTERFACE (char *$_host_outputFileName, char *$_host_tarArchive) {
78 19 : struct archive *$_process_command_line_a1 = &archive;
79 : int $_process_command_line_option_index_1;
80 19 : $_process_command_line_option_index_1 = 1;
81 19 : $_process_command_line_a1->command = COM_CREATE;
82 19 : $_process_command_line_a1->fmode = O_RDWR | O_CREAT | O_EXCL;
83 19 : $_process_command_line_a1->file_name = $_host_outputFileName;
84 19 : goto LABEL_process_command_line1;
85 : LABEL_process_command_line1 :
86 : if (0) {
87 : }
88 19 : struct archive *$_open_archive_file_archive1 = &archive;
89 19 : $_open_archive_file_archive1->fd = open ($_open_archive_file_archive1->file_name, $_open_archive_file_archive1->fmode, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
90 19 : goto LABEL_open_archive_file1;
91 : LABEL_open_archive_file1 :
92 : if (0) {
93 : }
94 19 : struct archive *$_read_archive_header_archive1 = &archive;
95 : size_t $_read_archive_header_descriptors_size_1;
96 19 : $_read_archive_header_descriptors_size_1 = sizeof (struct descr) * $_read_archive_header_archive1->append_files_count;
97 : int $_read_archive_header_result_1;
98 19 : if (COM_APPEND == $_read_archive_header_archive1->command || COM_LIST == $_read_archive_header_archive1->command) {
99 : }
100 19 : $_read_archive_header_archive1->file_descriptors = malloc ($_read_archive_header_descriptors_size_1);
101 19 : if (COM_APPEND == $_read_archive_header_archive1->command || COM_LIST == $_read_archive_header_archive1->command) {
102 0 : $_read_archive_header_result_1 = lseek ($_read_archive_header_archive1->fd, -$_read_archive_header_archive1->headers_offset, SEEK_END);
103 : }
104 : LABEL_read_archive_header1 :
105 : if (0) {
106 : }
107 19 : char *$_write_archive_in_file1 = $_host_tarArchive;
108 19 : struct archive *$_write_archive_archive1 = &archive;
109 19 : return write_archive ($_write_archive_in_file1, $_write_archive_archive1);
110 : }
111 :
|