1 /* @(#)table.c	1.30 17/02/08 Copyright 1994-96 2000-2017 J. Schilling */
2 #include <schily/mconfig.h>
3 #ifndef lint
4 static	UConst char sccsid[] =
5 	"@(#)table.c	1.30 17/02/08 Copyright 1994-96 2000-2017 J. Schilling";
6 #endif
7 /*
8  *	Conversion tables for efficient conversion
9  *	of different file type representations
10  *
11  *	Copyright (c) 1994-96 2000-2017 J. Schilling
12  */
13 /*
14  * The contents of this file are subject to the terms of the
15  * Common Development and Distribution License, Version 1.0 only
16  * (the "License").  You may not use this file except in compliance
17  * with the License.
18  *
19  * See the file CDDL.Schily.txt in this distribution for details.
20  * A copy of the CDDL is also available via the Internet at
21  * http://www.opensource.org/licenses/cddl1.txt
22  *
23  * When distributing Covered Code, include this CDDL HEADER in each
24  * file and include the License file CDDL.Schily.txt from this distribution.
25  */
26 
27 #include <schily/mconfig.h>
28 #include "star.h"
29 #include "table.h"
30 #include <schily/stat.h>
31 
32 #ifndef	S_IFIFO			/* If system knows no fifo's		*/
33 #define	S_IFIFO		S_IFREG	/* Map fifo's to regular files 		*/
34 #endif
35 #ifndef	S_IFCHR			/* If system knows no character special	*/
36 #define	S_IFCHR		S_IFREG	/* Map character spec. to regular files	*/
37 #endif
38 #ifndef	S_IFMPC			/* If system knows no multiplexed c	*/
39 #define	S_IFMPC		S_IFREG	/* Map multiplexed c to regular files	*/
40 #endif
41 #ifndef	S_IFNAM			/* If system knows no named files	*/
42 #define	S_IFNAM		S_IFREG	/* Map named files to regular files 	*/
43 #endif
44 #ifndef	S_IFBLK			/* If system knows no block special	*/
45 #define	S_IFBLK		S_IFREG	/* Map block spec. to regular files	*/
46 #endif
47 #ifndef	S_IFMPB			/* If system knows no multiplexed b	*/
48 #define	S_IFMPB		S_IFREG	/* Map multiplexed b to regular files	*/
49 #endif
50 #ifndef	S_IFCTG			/* If system knows no contiguous files	*/
51 #define	S_IFCTG		S_IFREG	/* Map contiguous file to regular files */
52 #endif
53 #ifndef	S_IFLNK			/* If system knows no symbolic links	*/
54 #define	S_IFLNK		S_IFREG	/* Map symbolic links to regular files */
55 #endif
56 #ifndef	S_IFSOCK		/* If system knows no fifo's		*/
57 #define	S_IFSOCK	S_IFREG	/* Map fifo's to regular files 		*/
58 #endif
59 #ifndef	S_IFDOOR		/* If system knows no door's		*/
60 #define	S_IFDOOR	S_IFREG	/* Map door's to regular files		*/
61 #endif
62 #ifndef	S_IFWHT			/* If system knows no whiteout's	*/
63 #define	S_IFWHT		S_IFREG	/* Map whiteout's to regular files	*/
64 #endif
65 #ifndef	S_IFEVC			/* If system knows no eventcount's	*/
66 #define	S_IFEVC		S_IFREG	/* Map eventcount's to regular files	*/
67 #endif
68 #define	S_IFBAD	S_IFMT		/* XXX Have to use another val if someone */
69 				/* XXX starts to use S_IFMT for a */
70 				/* XXX useful file type */
71 #define	XT_NAM	XT_BAD		/* XXX JS has not seen it yet */
72 				/* XXX if we use it, we have to change */
73 				/* XXX table.h and some of the tables below */
74 
75 /*
76  * UNIX File type to XT_ table
77  *
78  * Maps the 16 UNIX (S_XXX >> 12) filetypes to star's internal XT_ types.
79  * Note that this only works if all S_XXX defines are the same on all
80  * UNIX versions. Fortunately, this is currently the case and there is no
81  * big chance that somebody will do it differently.
82  *
83  * XXX If really somebody creates a different OS we will need to change this
84  * XXX table and the associated macros or make this table dynamically
85  * XXX created at startup of star.
86  *
87  * UNIX file types (see table.h):
88  *	0 Unallocated	1 FIFO		2 Chr special	3 MPX chr
89  *	4 Directory	5 NAM special	6 BLK special	7 MPX blk
90  *	8 Regular File	9 Contig File	10 Symlink	11 Sol Shadow ino
91  *	12 Socket	13 DOOR special	14 Whiteout	15 UNOS event count
92  *
93  * No bound checking in hope that S_IFMT will never hold more than 4 bits.
94  */
95 /* BEGIN CSTYLED */
96 UInt8_t	iftoxt_tab[] = {
97 		/* 0 */	XT_NONE, XT_FIFO, XT_CHR,   XT_MPC,
98 		/* 4 */	XT_DIR,  XT_NAM,  XT_BLK,   XT_MPB,
99 		/* 8 */	XT_FILE, XT_CONT, XT_SLINK, XT_BAD,
100 		/*12 */	XT_SOCK, XT_DOOR, XT_WHT,   XT_BAD,
101 			};
102 /* END CSTYLED */
103 
104 /*
105  * Ustar File type to XT_ table
106  *
107  * Maps the ustar 0..7 filetypes to star's internal XT_ types.
108  * Bound checking is done via ustoxt().
109  */
110 UInt8_t	ustoxt_tab[] = {
111 		/* 0 */	XT_FILE, XT_LINK, XT_SLINK, XT_CHR,
112 		/* 4 */	XT_BLK,  XT_DIR,  XT_FIFO,  XT_CONT,
113 		/* 8 */	XT_BAD,  XT_BAD,
114 };
115 
116 /*
117  * Vendor unique File type to XT_ table
118  *
119  * Maps the filetypes 'A'..'Z' to star's internal XT_ types.
120  * Fortunately, the different vendor unique extensions are disjunct.
121  * External code does bound checking.
122  */
123 /* BEGIN CSTYLED */
124 UInt8_t	vttoxt_tab[] = {
125 		/* A */	XT_NONE,     XT_NONE,   XT_NONE,     XT_DUMPDIR,
126 		/* E */	XT_NONE,     XT_NONE,   XT_NONE,     XT_NONE,
127 		/* I */	XT_META,     XT_NONE,   XT_LONGLINK, XT_LONGNAME,
128 		/* M */	XT_MULTIVOL, XT_NAMES,  XT_NONE,     XT_NONE,
129 		/* Q */	XT_NONE,     XT_NONE,   XT_SPARSE,   XT_NONE,
130 		/* U */	XT_NONE,     XT_VOLHDR, XT_NONE,     XT_NONE,
131 		/* Y */	XT_NONE,     XT_NONE,
132 };
133 /* END CSTYLED */
134 
135 /*
136  * XT_* codes used (see table.h):
137  *				 0..16	Real file types and hard links
138  *				20..26	Speudo file types (POSIX 'A' .. 'Z'
139  *				31	XT_BAD (illegal file type)
140  */
141 
142 /*
143  * XT_ to UNIX File type table
144  *
145  * XT_SPARSE and XT_META are just other (tar specific) views of regular files.
146  */
147 /* BEGIN CSTYLED */
148 mode_t	xttoif_tab[] = {
149 		/* 0 */	0,       S_IFREG,  S_IFCTG, S_IFREG,
150 		/* 4 */	S_IFLNK, S_IFDIR,  S_IFCHR, S_IFBLK,
151 		/* 8 */	S_IFIFO, S_IFSOCK, S_IFMPC, S_IFMPB,
152 		/*12 */	S_IFNAM, S_IFNAM,  S_IFDOOR, S_IFEVC,
153 		/*16 */	S_IFWHT, S_IFBAD,  S_IFBAD, S_IFBAD,
154 		/*20 */	S_IFDIR, S_IFBAD,  S_IFBAD, S_IFBAD,
155 		/*24 */	S_IFBAD, S_IFREG,  S_IFBAD, S_IFREG,
156 		/*28 */	S_IFBAD, S_IFBAD,  S_IFBAD, S_IFBAD,
157 };
158 /* END CSTYLED */
159 
160 /*
161  * XT_ to Star-1985 File type table
162  */
163 /* BEGIN CSTYLED */
164 UInt8_t	xttost_tab[] = {
165 		/* 0 */	0,       F_FILE, F_FILE, F_FILE,
166 		/* 4 */	F_SLINK, F_DIR,  F_SPEC, F_SPEC,
167 		/* 8 */	F_SPEC,  F_SPEC, F_SPEC, F_SPEC,
168 		/*12 */	F_SPEC,  F_SPEC, F_SPEC, F_SPEC,
169 		/*16 */	F_SPEC,  F_SPEC, F_SPEC, F_SPEC,
170 		/*20 */	F_DIR,   F_FILE, F_FILE, F_FILE,
171 		/*24 */	F_FILE,  F_FILE, F_SPEC, F_FILE,
172 		/*28 */	F_SPEC,  F_SPEC, F_SPEC, F_SPEC,
173 };
174 /* END CSTYLED */
175 
176 /*
177  * XT_ Old UNIX V7 tar supported File type table
178  */
179 /* BEGIN CSTYLED */
180 UInt8_t	xtv7tar_tab[] = {
181 		/* 0 */	0,	1,	1,	1,
182 		/* 4 */	0,	0,	0,	0,
183 		/* 8 */	0,	0,	0,	0,
184 		/*12 */	0,	0,	0,	0,
185 		/*16 */	0,	0,	0,	0,
186 		/*20 */	0,	0,	0,	0,
187 		/*24 */	0,	0,	0,	0,
188 		/*28 */	0,	0,	0,	0,
189 };
190 /* END CSTYLED */
191 
192 /*
193  * XT_ Old BSD tar supported File type table
194  */
195 /* BEGIN CSTYLED */
196 UInt8_t	xttar_tab[] = {
197 		/* 0 */	0,	1,	1,	1,
198 		/* 4 */	1,	1,	0,	0,
199 		/* 8 */	0,	0,	0,	0,
200 		/*12 */	0,	0,	0,	0,
201 		/*16 */	0,	0,	0,	0,
202 		/*20 */	0,	0,	0,	0,
203 		/*24 */	0,	0,	0,	0,
204 		/*28 */	0,	0,	0,	0,
205 			};
206 /* END CSTYLED */
207 
208 /*
209  * XT_ Star-1985 supported File type table
210  */
211 /* BEGIN CSTYLED */
212 UInt8_t	xtstar_tab[] = {
213 		/* 0 */	0,	1,	1,	1,
214 		/* 4 */	1,	1,	1,	1,
215 		/* 8 */	1,	1,	1,	1,
216 		/*12 */	1,	1,	1,	0,
217 		/*16 */	0,	0,	0,	0,
218 		/*20 */	0,	0,	0,	0,
219 		/*24 */	0,	1,	0,	1,
220 		/*28 */	0,	0,	0,	0,
221 };
222 /* END CSTYLED */
223 
224 /*
225  * XT_ Ustar-1988 supported File type table
226  */
227 /* BEGIN CSTYLED */
228 UInt8_t	xtustar_tab[] = {
229 		/* 0 */	0,	1,	1,	1,
230 		/* 4 */	1,	1,	1,	1,
231 		/* 8 */	1,	0,	0,	0,
232 		/*12 */	0,	0,	0,	0,
233 		/*16 */	0,	0,	0,	0,
234 		/*20 */	0,	0,	0,	0,
235 		/*24 */	0,	0,	0,	0,
236 		/*28 */	0,	0,	0,	0,
237 			};
238 /* END CSTYLED */
239 
240 /*
241  * XT_ Extended PAX-2001 'exustar' supported File type table
242  */
243 /* BEGIN CSTYLED */
244 UInt8_t	xtexustar_tab[] = {
245 		/* 0 */	0,	1,	1,	1,
246 		/* 4 */	1,	1,	1,	1,
247 		/* 8 */	1,	1,	0,	0,
248 		/*12 */	0,	0,	1,	0,
249 		/*16 */	0,	0,	0,	0,
250 		/*20 */	0,	0,	0,	0,
251 		/*24 */	0,	1,	0,	1,
252 		/*28 */	0,	0,	0,	0,
253 };
254 /* END CSTYLED */
255 
256 /*
257  * XT_ CPIO-1988 supported File type table
258  */
259 /* BEGIN CSTYLED */
260 UInt8_t	xtcpio_tab[] = {
261 		/* 0 */	0,	1,	1,	1,
262 		/* 4 */	1,	1,	1,	1,
263 		/* 8 */	1,	1,	0,	0,
264 		/*12 */	0,	0,	0,	0,
265 		/*16 */	0,	0,	0,	0,
266 		/*20 */	0,	0,	0,	0,
267 		/*24 */	0,	0,	0,	0,
268 		/*28 */	0,	0,	0,	0,
269 };
270 /* END CSTYLED */
271 
272 /*
273  * XT_ to Ustar (including Vendor Unique extensions) File type table
274  *
275  * sockets cannot be handled in ansi tar, they are handled as regular files :-(
276  */
277 /* BEGIN CSTYLED */
278 UInt8_t	xttous_tab[] = {
279 		/* 0 */	0,       REGTYPE, CONTTYPE, LNKTYPE,
280 		/* 4 */	SYMTYPE, DIRTYPE, CHRTYPE,  BLKTYPE,
281 		/* 8 */	FIFOTYPE,REGTYPE/* socket */, 0/* bad */, 0/* bad */,
282 		/*12 */	0,       0,       0,        0,
283 		/*16 */	0,       0,       0,        0,
284 		/*20 */	LF_DUMPDIR, LF_LONGLINK, LF_LONGNAME, LF_MULTIVOL,
285 		/*24 */	LF_NAMES,   LF_SPARSE,   LF_VOLHDR,   LF_META,
286 		/*28 */	0,       0,       0,        0,
287 };
288 /* END CSTYLED */
289 
290 /*
291  * XT_ to String table
292  */
293 /* BEGIN CSTYLED */
294 char	*xttostr_tab[] = {
295 #define	XT_DEBUG
296 #ifdef	XT_DEBUG
297 		/* 0 */	"U",	"-",	"C",	"H",
298 #else
299 		/* 0 */	"-",	"-",	"-",	"-",
300 #endif
301 		/* 4 */	"l",	"d",	"c",	"b",
302 		/* 8 */	"p",	"s",	"~",	"~",
303 		/*12 */	"~",	"~",	"D",	"~",
304 		/*16 */	"%",	"~",	"~",	"~",
305 
306 		/*20 */	"D",	"K",	"L",	"M",
307 #ifdef	XT_DEBUG
308 		/*24 */	"N",	"S",	"V",	"m",
309 #else
310 		/*24 */	"N",	"-",	"V",	"-",
311 #endif
312 
313 		/*28 */	"~",	"~",	"~",	"~",
314 };
315 /* END CSTYLED */
316 
317 /*
318  * XT_ to named file type text
319  */
320 /* BEGIN CSTYLED */
321 char	*xttoname_tab[] = {
322 		/* 0 */	"unallocated",	"regular",	"contiguous",		"hardlink",
323 		/* 4 */	"symlink",	"directory",	"character special",	"block special",
324 		/* 8 */	"fifo",		"socket",	"mpx character special", "mpx block special",
325 		/*12 */	"XENIX nsem",	"XENIX nshd",	"door",		        "eventcount",
326 		/*16 */	"whiteout",	"reserved",	"reserved",		"reserved",
327 		/*20 */	"dumpdir",	"longlink",	"longname",		"multivol continuation",
328 		/*24 */	"names",	"sparse",	"volheader",		"meta",
329 		/*28 */	"reserved",	"reserved",	"reserved",		"unknown/bad",
330 };
331 /* END CSTYLED */
332 
333 /*
334  * String lengths for above table
335  */
336 UInt8_t	xtnamelen_tab[] = {
337 		/*  0 */	11,  7, 10,  8,
338 		/*  4 */	 7,  9, 17, 13,
339 		/*  8 */	 4,  6, 21, 17,
340 		/* 12 */	10, 10,  4, 10,
341 		/* 16 */	 8,  8,  8,  8,
342 		/* 20 */	 7,  8,  8, 21,
343 		/* 24 */	 5,  6,  9,  4,
344 		/* 28 */	 8,  8,  8, 11,
345 };
346