1 /*
2  * libdpkg - Debian packaging suite library routines
3  * tarfn.h - tar archive extraction functions
4  *
5  * Copyright © 1995 Bruce Perens
6  * Copyright © 2009-2014, 2017 Guillem Jover <guillem@debian.org>
7  *
8  * This is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
20  */
21 
22 #ifndef LIBDPKG_TARFN_H
23 #define LIBDPKG_TARFN_H
24 
25 #include <sys/types.h>
26 
27 #include <stdint.h>
28 
29 #include <dpkg/error.h>
30 #include <dpkg/file.h>
31 
32 /**
33  * @defgroup tar Tar archive handling
34  * @ingroup dpkg-public
35  * @{
36  */
37 
38 #define TARBLKSZ	512
39 
40 enum tar_format {
41 	TAR_FORMAT_UNKNOWN,
42 	TAR_FORMAT_OLD,
43 	TAR_FORMAT_GNU,
44 	TAR_FORMAT_USTAR,
45 	TAR_FORMAT_PAX,
46 };
47 
48 enum tar_filetype {
49 	/** For compatibility with decades-old bug. */
50 	TAR_FILETYPE_FILE0 = '\0',
51 	TAR_FILETYPE_FILE = '0',
52 	TAR_FILETYPE_HARDLINK = '1',
53 	TAR_FILETYPE_SYMLINK = '2',
54 	TAR_FILETYPE_CHARDEV = '3',
55 	TAR_FILETYPE_BLOCKDEV = '4',
56 	TAR_FILETYPE_DIR = '5',
57 	TAR_FILETYPE_FIFO = '6',
58 	TAR_FILETYPE_CONTIG = '7',
59 	TAR_FILETYPE_GNU_LONGLINK = 'K',
60 	TAR_FILETYPE_GNU_LONGNAME = 'L',
61 	TAR_FILETYPE_GNU_VOLUME = 'V',
62 	TAR_FILETYPE_GNU_MULTIVOL = 'M',
63 	TAR_FILETYPE_GNU_DUMPDIR = 'D',
64 	TAR_FILETYPE_GNU_SPARSE = 'S',
65 	TAR_FILETYPE_PAX_GLOBAL = 'g',
66 	TAR_FILETYPE_PAX_EXTENDED = 'x',
67 	TAR_FILETYPE_SOLARIS_EXTENDED = 'X',
68 	TAR_FILETYPE_SOLARIS_ACL = 'A',
69 };
70 
71 struct tar_entry {
72 	/** Tar entry format. */
73 	enum tar_format format;
74 	/** File type. */
75 	enum tar_filetype type;
76 	/** File name. */
77 	char *name;
78 	/** Symlink or hardlink name. */
79 	char *linkname;
80 	/** File size. */
81 	off_t size;
82 	/** Last-modified time. */
83 	time_t mtime;
84 	/** Special device for mknod(). */
85 	dev_t dev;
86 
87 	struct file_stat stat;
88 };
89 
90 struct tar_archive;
91 
92 typedef int tar_read_func(struct tar_archive *tar, char *buffer, int length);
93 typedef int tar_make_func(struct tar_archive *tar, struct tar_entry *h);
94 
95 struct tar_operations {
96 	tar_read_func *read;
97 
98 	tar_make_func *extract_file;
99 	tar_make_func *link;
100 	tar_make_func *symlink;
101 	tar_make_func *mkdir;
102 	tar_make_func *mknod;
103 };
104 
105 struct tar_archive {
106 	/* Global tar archive error. */
107 	struct dpkg_error err;
108 
109 	/** Tar archive format. */
110 	enum tar_format format;
111 
112 	/* Operation functions and context. */
113 	const struct tar_operations *ops;
114 	void *ctx;
115 };
116 
117 uintmax_t
118 tar_atoul(const char *s, size_t size, uintmax_t max);
119 intmax_t
120 tar_atosl(const char *s, size_t size, intmax_t min, intmax_t max);
121 
122 void
123 tar_entry_update_from_system(struct tar_entry *te);
124 
125 int
126 tar_extractor(struct tar_archive *tar);
127 
128 /** @} */
129 
130 #endif
131