1 /*
2  * libdpkg - Debian packaging suite library routines
3  * ar.h - primitives for ar handling
4  *
5  * Copyright © 2010 Guillem Jover <guillem@debian.org>
6  *
7  * This is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  */
20 
21 #ifndef LIBDPKG_AR_H
22 #define LIBDPKG_AR_H
23 
24 #include <sys/types.h>
25 
26 #include <stdbool.h>
27 #include <ar.h>
28 
29 #include <dpkg/macros.h>
30 
31 DPKG_BEGIN_DECLS
32 
33 /**
34  * @defgroup ar Ar archive handling
35  * @ingroup dpkg-public
36  * @{
37  */
38 
39 #define DPKG_AR_MAGIC "!<arch>\n"
40 #define DPKG_AR_FMAG  "`\n"
41 
42 /**
43  * An on-disk archive header.
44  */
45 struct dpkg_ar_hdr {
46 	char ar_name[16];	   /* Member file name, sometimes / terminated. */
47 	char ar_date[12];	   /* File date, decimal seconds since Epoch.  */
48 	char ar_uid[6], ar_gid[6]; /* User and group IDs, in ASCII decimal.  */
49 	char ar_mode[8];	   /* File mode, in ASCII octal.  */
50 	char ar_size[10];	   /* File size, in ASCII decimal.  */
51 	char ar_fmag[2];
52 };
53 
54 /**
55  * An archive (Unix ar) file.
56  */
57 struct dpkg_ar {
58 	const char *name;
59 	mode_t mode;
60 	time_t time;
61 	off_t size;
62 	int fd;
63 };
64 
65 /**
66  * In-memory archive member information.
67  */
68 struct dpkg_ar_member {
69 	struct dpkg_ar_member *next;
70 	const char *name;
71 	off_t offset;
72 	off_t size;
73 	time_t time;
74 	mode_t mode;
75 	uid_t uid;
76 	gid_t gid;
77 };
78 
79 struct dpkg_ar *
80 dpkg_ar_fdopen(const char *filename, int fd);
81 struct dpkg_ar *dpkg_ar_open(const char *filename);
82 struct dpkg_ar *dpkg_ar_create(const char *filename, mode_t mode);
83 void dpkg_ar_set_mtime(struct dpkg_ar *ar, time_t mtime);
84 void dpkg_ar_close(struct dpkg_ar *ar);
85 
86 void dpkg_ar_normalize_name(struct dpkg_ar_hdr *arh);
87 bool dpkg_ar_member_is_illegal(struct dpkg_ar_hdr *arh);
88 
89 void dpkg_ar_put_magic(struct dpkg_ar *ar);
90 void dpkg_ar_member_put_header(struct dpkg_ar *ar,
91                                struct dpkg_ar_member *member);
92 void dpkg_ar_member_put_file(struct dpkg_ar *ar, const char *name,
93                              int fd, off_t size);
94 void dpkg_ar_member_put_mem(struct dpkg_ar *ar, const char *name,
95                             const void *data, size_t size);
96 off_t dpkg_ar_member_get_size(struct dpkg_ar *ar, struct dpkg_ar_hdr *arh);
97 
98 /** @} */
99 
100 DPKG_END_DECLS
101 
102 #endif /* LIBDPKG_AR_H */
103