14b88c807SRodney W. Grimes /*- 2*8a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 3*8a16b7a1SPedro F. Giffuni * 44b88c807SRodney W. Grimes * Copyright (c) 1992 Keith Muller. 54b88c807SRodney W. Grimes * Copyright (c) 1992, 1993 64b88c807SRodney W. Grimes * The Regents of the University of California. All rights reserved. 74b88c807SRodney W. Grimes * 84b88c807SRodney W. Grimes * This code is derived from software contributed to Berkeley by 94b88c807SRodney W. Grimes * Keith Muller of the University of California, San Diego. 104b88c807SRodney W. Grimes * 114b88c807SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 124b88c807SRodney W. Grimes * modification, are permitted provided that the following conditions 134b88c807SRodney W. Grimes * are met: 144b88c807SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 154b88c807SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 164b88c807SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 174b88c807SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 184b88c807SRodney W. Grimes * documentation and/or other materials provided with the distribution. 19fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors 204b88c807SRodney W. Grimes * may be used to endorse or promote products derived from this software 214b88c807SRodney W. Grimes * without specific prior written permission. 224b88c807SRodney W. Grimes * 234b88c807SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 244b88c807SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 254b88c807SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 264b88c807SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 274b88c807SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 284b88c807SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 294b88c807SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 304b88c807SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 314b88c807SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 324b88c807SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 334b88c807SRodney W. Grimes * SUCH DAMAGE. 344b88c807SRodney W. Grimes */ 354b88c807SRodney W. Grimes 364b88c807SRodney W. Grimes /* 374b88c807SRodney W. Grimes * data structures and constants used by the different databases kept by pax 384b88c807SRodney W. Grimes */ 394b88c807SRodney W. Grimes 404b88c807SRodney W. Grimes /* 414b88c807SRodney W. Grimes * Hash Table Sizes MUST BE PRIME, if set too small performance suffers. 424b88c807SRodney W. Grimes * Probably safe to expect 500000 inodes per tape. Assuming good key 434b88c807SRodney W. Grimes * distribution (inodes) chains of under 50 long (worse case) is ok. 444b88c807SRodney W. Grimes */ 454b88c807SRodney W. Grimes #define L_TAB_SZ 2503 /* hard link hash table size */ 464b88c807SRodney W. Grimes #define F_TAB_SZ 50503 /* file time hash table size */ 474b88c807SRodney W. Grimes #define N_TAB_SZ 541 /* interactive rename hash table */ 484b88c807SRodney W. Grimes #define D_TAB_SZ 317 /* unique device mapping table */ 494b88c807SRodney W. Grimes #define A_TAB_SZ 317 /* ftree dir access time reset table */ 504b88c807SRodney W. Grimes #define MAXKEYLEN 64 /* max number of chars for hash */ 514b88c807SRodney W. Grimes 524b88c807SRodney W. Grimes /* 534b88c807SRodney W. Grimes * file hard link structure (hashed by dev/ino and chained) used to find the 544b88c807SRodney W. Grimes * hard links in a file system or with some archive formats (cpio) 554b88c807SRodney W. Grimes */ 564b88c807SRodney W. Grimes typedef struct hrdlnk { 574b88c807SRodney W. Grimes char *name; /* name of first file seen with this ino/dev */ 584b88c807SRodney W. Grimes dev_t dev; /* files device number */ 594b88c807SRodney W. Grimes ino_t ino; /* files inode number */ 604b88c807SRodney W. Grimes u_long nlink; /* expected link count */ 614b88c807SRodney W. Grimes struct hrdlnk *fow; 624b88c807SRodney W. Grimes } HRDLNK; 634b88c807SRodney W. Grimes 644b88c807SRodney W. Grimes /* 654b88c807SRodney W. Grimes * Archive write update file time table (the -u, -C flag), hashed by filename. 664b88c807SRodney W. Grimes * Filenames are stored in a scratch file at seek offset into the file. The 674b88c807SRodney W. Grimes * file time (mod time) and the file name length (for a quick check) are 684b88c807SRodney W. Grimes * stored in a hash table node. We were forced to use a scratch file because 694b88c807SRodney W. Grimes * with -u, the mtime for every node in the archive must always be available 704b88c807SRodney W. Grimes * to compare against (and this data can get REALLY large with big archives). 714b88c807SRodney W. Grimes * By being careful to read only when we have a good chance of a match, the 724b88c807SRodney W. Grimes * performance loss is not measurable (and the size of the archive we can 734b88c807SRodney W. Grimes * handle is greatly increased). 744b88c807SRodney W. Grimes */ 754b88c807SRodney W. Grimes typedef struct ftm { 764b88c807SRodney W. Grimes int namelen; /* file name length */ 774b88c807SRodney W. Grimes time_t mtime; /* files last modification time */ 7846be34b9SKris Kennaway off_t seek; /* location in scratch file */ 794b88c807SRodney W. Grimes struct ftm *fow; 804b88c807SRodney W. Grimes } FTM; 814b88c807SRodney W. Grimes 824b88c807SRodney W. Grimes /* 834b88c807SRodney W. Grimes * Interactive rename table (-i flag), hashed by orig filename. 844b88c807SRodney W. Grimes * We assume this will not be a large table as this mapping data can only be 854b88c807SRodney W. Grimes * obtained through interactive input by the user. Nobody is going to type in 864b88c807SRodney W. Grimes * changes for 500000 files? We use chaining to resolve collisions. 874b88c807SRodney W. Grimes */ 884b88c807SRodney W. Grimes 894b88c807SRodney W. Grimes typedef struct namt { 904b88c807SRodney W. Grimes char *oname; /* old name */ 914b88c807SRodney W. Grimes char *nname; /* new name typed in by the user */ 924b88c807SRodney W. Grimes struct namt *fow; 934b88c807SRodney W. Grimes } NAMT; 944b88c807SRodney W. Grimes 954b88c807SRodney W. Grimes /* 964b88c807SRodney W. Grimes * Unique device mapping tables. Some protocols (e.g. cpio) require that the 974b88c807SRodney W. Grimes * <c_dev,c_ino> pair will uniquely identify a file in an archive unless they 984b88c807SRodney W. Grimes * are links to the same file. Appending to archives can break this. For those 994b88c807SRodney W. Grimes * protocols that have this requirement we map c_dev to a unique value not seen 1004b88c807SRodney W. Grimes * in the archive when we append. We also try to handle inode truncation with 1014b88c807SRodney W. Grimes * this table. (When the inode field in the archive header are too small, we 1024b88c807SRodney W. Grimes * remap the dev on writes to remove accidental collisions). 1034b88c807SRodney W. Grimes * 1044b88c807SRodney W. Grimes * The list is hashed by device number using chain collision resolution. Off of 1054b88c807SRodney W. Grimes * each DEVT are linked the various remaps for this device based on those bits 1064b88c807SRodney W. Grimes * in the inode which were truncated. For example if we are just remapping to 1074b88c807SRodney W. Grimes * avoid a device number during an update append, off the DEVT we would have 1084b88c807SRodney W. Grimes * only a single DLIST that has a truncation id of 0 (no inode bits were 1094b88c807SRodney W. Grimes * stripped for this device so far). When we spot inode truncation we create 1104b88c807SRodney W. Grimes * a new mapping based on the set of bits in the inode which were stripped off. 1114b88c807SRodney W. Grimes * so if the top four bits of the inode are stripped and they have a pattern of 1124b88c807SRodney W. Grimes * 0110...... (where . are those bits not truncated) we would have a mapping 1134b88c807SRodney W. Grimes * assigned for all inodes that has the same 0110.... pattern (with this dev 1144b88c807SRodney W. Grimes * number of course). This keeps the mapping sparse and should be able to store 1154b88c807SRodney W. Grimes * close to the limit of files which can be represented by the optimal 1164b88c807SRodney W. Grimes * combination of dev and inode bits, and without creating a fouled up archive. 1174b88c807SRodney W. Grimes * Note we also remap truncated devs in the same way (an exercise for the 1184b88c807SRodney W. Grimes * dedicated reader; always wanted to say that...:) 1194b88c807SRodney W. Grimes */ 1204b88c807SRodney W. Grimes 1214b88c807SRodney W. Grimes typedef struct devt { 1224b88c807SRodney W. Grimes dev_t dev; /* the orig device number we now have to map */ 1234b88c807SRodney W. Grimes struct devt *fow; /* new device map list */ 1244b88c807SRodney W. Grimes struct dlist *list; /* map list based on inode truncation bits */ 1254b88c807SRodney W. Grimes } DEVT; 1264b88c807SRodney W. Grimes 1274b88c807SRodney W. Grimes typedef struct dlist { 1284b88c807SRodney W. Grimes ino_t trunc_bits; /* truncation pattern for a specific map */ 1294b88c807SRodney W. Grimes dev_t dev; /* the new device id we use */ 1304b88c807SRodney W. Grimes struct dlist *fow; 1314b88c807SRodney W. Grimes } DLIST; 1324b88c807SRodney W. Grimes 1334b88c807SRodney W. Grimes /* 1344b88c807SRodney W. Grimes * ftree directory access time reset table. When we are done with with a 1354b88c807SRodney W. Grimes * subtree we reset the access and mod time of the directory when the tflag is 1364b88c807SRodney W. Grimes * set. Not really explicitly specified in the pax spec, but easy and fast to 1374b88c807SRodney W. Grimes * do (and this may have even been intended in the spec, it is not clear). 1384b88c807SRodney W. Grimes * table is hashed by inode with chaining. 1394b88c807SRodney W. Grimes */ 1404b88c807SRodney W. Grimes 1414b88c807SRodney W. Grimes typedef struct atdir { 1424b88c807SRodney W. Grimes char *name; /* name of directory to reset */ 1434b88c807SRodney W. Grimes dev_t dev; /* dev and inode for fast lookup */ 1444b88c807SRodney W. Grimes ino_t ino; 1454b88c807SRodney W. Grimes time_t mtime; /* access and mod time to reset to */ 1464b88c807SRodney W. Grimes time_t atime; 1474b88c807SRodney W. Grimes struct atdir *fow; 1484b88c807SRodney W. Grimes } ATDIR; 1494b88c807SRodney W. Grimes 1504b88c807SRodney W. Grimes /* 1514b88c807SRodney W. Grimes * created directory time and mode storage entry. After pax is finished during 1524b88c807SRodney W. Grimes * extraction or copy, we must reset directory access modes and times that 1534b88c807SRodney W. Grimes * may have been modified after creation (they no longer have the specified 1544b88c807SRodney W. Grimes * times and/or modes). We must reset time in the reverse order of creation, 1554b88c807SRodney W. Grimes * because entries are added from the top of the file tree to the bottom. 1564b88c807SRodney W. Grimes * We MUST reset times from leaf to root (it will not work the other 1574b88c807SRodney W. Grimes * direction). Entries are recorded into a spool file to make reverse 1584b88c807SRodney W. Grimes * reading faster. 1594b88c807SRodney W. Grimes */ 1604b88c807SRodney W. Grimes 1614b88c807SRodney W. Grimes typedef struct dirdata { 1624b88c807SRodney W. Grimes int nlen; /* length of the directory name (includes \0) */ 1634b88c807SRodney W. Grimes off_t npos; /* position in file where this dir name starts */ 1644b88c807SRodney W. Grimes mode_t mode; /* file mode to restore */ 1654b88c807SRodney W. Grimes time_t mtime; /* mtime to set */ 1664b88c807SRodney W. Grimes time_t atime; /* atime to set */ 1674b88c807SRodney W. Grimes int frc_mode; /* do we force mode settings? */ 1684b88c807SRodney W. Grimes } DIRDATA; 169