1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 #ifndef TAR_H
30 #define TAR_H
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <time.h>
35 #include <inttypes.h>
36
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40
41 #define T_ISUID 04000
42 #define T_ISGID 02000
43 #define T_IRUSR 00400
44 #define T_IWUSR 00200
45 #define T_IXUSR 00100
46 #define T_IRGRP 00040
47 #define T_IWGRP 00020
48 #define T_IXGRP 00010
49 #define T_IROTH 00004
50 #define T_IWOTH 00002
51 #define T_IXOTH 00001
52
53 #define TAR_TYPE_FILE '0'
54 #define TAR_TYPE_DIRECTORY '5'
55
56 enum TarError {
57 TAR_OK =
0,
58 TAR_PATH_TOO_LONG,
59 TAR_FILE_TOO_LARGE,
60 TAR_CONTENT_TOO_LARGE,
61 TAR_UNFINISHED_FILE,
62 TAR_CONTENT_BROKEN,
63 TAR_ERROR
64 };
65
66 typedef enum TarError TarError;
67
68 typedef struct TarHeader {
69 char name[
100];
70 char mode[
8];
71 char uid[
8];
72 char gid[
8];
73 char size[
12];
74 char mtime[
12];
75 char chksum[
8];
76 char typeflag;
77 char linkname[
100];
78 char magic[
6];
79 char version[
2];
80 char uname[
32];
81 char gname[
32];
82 char devmajor[
8];
83 char devminor[
8];
84 char prefix[
155];
85 char padding[
12];
86 } TarHeader;
87
88 typedef struct TarOutputStream {
89 FILE *file;
90 uint64_t cur_filesize;
91 uint64_t cur_written;
92 TarError error;
93 } TarOutputStream;
94
95 typedef struct TarEntry {
96 char *path;
97 uint64_t size;
98 int type;
99 } TarEntry;
100
101 typedef struct TarInputStream {
102 FILE *file;
103 TarEntry cur_entry;
104 uint64_t cur_read;
105 TarError error;
106 } TarInputStream;
107
108 const char* tar_error2str(TarError error);
109
110 TarOutputStream* tar_open(
FILE *f);
111 int tar_add_dir(TarOutputStream *tar,
char *path,
uint32_t mode,
time_t mtime);
112 int tar_begin_file(
113 TarOutputStream *tar,
114 char *path,
115 uint32_t mode,
116 uint64_t size,
117 time_t mtime);
118 size_t tar_fwrite(
const void *ptr,
size_t s,
size_t n, TarOutputStream *stream);
119 int tar_end_file(TarOutputStream *tar);
120 int tar_close(TarOutputStream *tar);
121
122 TarInputStream* tar_inputstream_open(
FILE *f);
123 TarEntry* tar_read_entry(TarInputStream *tar);
124 size_t tar_fread(
void *ptr,
size_t s,
size_t n, TarInputStream *stream);
125 int tar_seek(TarInputStream *stream,
long offset,
int whence);
126 int tar_inputstream_close(TarInputStream *tar);
127
128
129 #ifdef __cplusplus
130 }
131 #endif
132
133 #endif
134
135