1*4874b543Sderaadt /* $OpenBSD: makefs.h,v 1.13 2021/10/06 00:40:39 deraadt Exp $ */ 26163fc9cSnatano /* $NetBSD: makefs.h,v 1.36 2015/11/25 00:48:49 christos Exp $ */ 36163fc9cSnatano 46163fc9cSnatano /* 56163fc9cSnatano * Copyright (c) 2001 Wasabi Systems, Inc. 66163fc9cSnatano * All rights reserved. 76163fc9cSnatano * 86163fc9cSnatano * Written by Luke Mewburn for Wasabi Systems, Inc. 96163fc9cSnatano * 106163fc9cSnatano * Redistribution and use in source and binary forms, with or without 116163fc9cSnatano * modification, are permitted provided that the following conditions 126163fc9cSnatano * are met: 136163fc9cSnatano * 1. Redistributions of source code must retain the above copyright 146163fc9cSnatano * notice, this list of conditions and the following disclaimer. 156163fc9cSnatano * 2. Redistributions in binary form must reproduce the above copyright 166163fc9cSnatano * notice, this list of conditions and the following disclaimer in the 176163fc9cSnatano * documentation and/or other materials provided with the distribution. 186163fc9cSnatano * 3. All advertising materials mentioning features or use of this software 196163fc9cSnatano * must display the following acknowledgement: 206163fc9cSnatano * This product includes software developed for the NetBSD Project by 216163fc9cSnatano * Wasabi Systems, Inc. 226163fc9cSnatano * 4. The name of Wasabi Systems, Inc. may not be used to endorse 236163fc9cSnatano * or promote products derived from this software without specific prior 246163fc9cSnatano * written permission. 256163fc9cSnatano * 266163fc9cSnatano * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND 276163fc9cSnatano * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 286163fc9cSnatano * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 296163fc9cSnatano * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC 306163fc9cSnatano * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 316163fc9cSnatano * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 326163fc9cSnatano * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 336163fc9cSnatano * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 346163fc9cSnatano * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 356163fc9cSnatano * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 366163fc9cSnatano * POSSIBILITY OF SUCH DAMAGE. 376163fc9cSnatano */ 386163fc9cSnatano 396163fc9cSnatano #ifndef _MAKEFS_H 406163fc9cSnatano #define _MAKEFS_H 416163fc9cSnatano 426163fc9cSnatano #include <sys/stat.h> 436163fc9cSnatano #include <err.h> 446163fc9cSnatano 456163fc9cSnatano /* 466163fc9cSnatano * fsnode - 476163fc9cSnatano * a component of the tree; contains a filename, a pointer to 486163fc9cSnatano * fsinode, optional symlink name, and tree pointers 496163fc9cSnatano * 506163fc9cSnatano * fsinode - 516163fc9cSnatano * equivalent to an inode, containing target file system inode number, 526163fc9cSnatano * refcount (nlink), and stat buffer 536163fc9cSnatano * 546163fc9cSnatano * A tree of fsnodes looks like this: 556163fc9cSnatano * 566163fc9cSnatano * name "." "bin" "netbsd" 576163fc9cSnatano * type S_IFDIR S_IFDIR S_IFREG 586163fc9cSnatano * next > > NULL 596163fc9cSnatano * parent NULL NULL NULL 606163fc9cSnatano * child NULL v 616163fc9cSnatano * 626163fc9cSnatano * name "." "ls" 636163fc9cSnatano * type S_IFDIR S_IFREG 646163fc9cSnatano * next > NULL 656163fc9cSnatano * parent ^ ^ (to "bin") 666163fc9cSnatano * child NULL NULL 676163fc9cSnatano * 686163fc9cSnatano * Notes: 696163fc9cSnatano * - first always points to first entry, at current level, which 706163fc9cSnatano * must be "." when the tree has been built; during build it may 716163fc9cSnatano * not be if "." hasn't yet been found by readdir(2). 726163fc9cSnatano */ 736163fc9cSnatano 746163fc9cSnatano enum fi_flags { 756163fc9cSnatano FI_SIZED = 1<<0, /* inode sized */ 766163fc9cSnatano FI_ALLOCATED = 1<<1, /* fsinode->ino allocated */ 776163fc9cSnatano FI_WRITTEN = 1<<2, /* inode written */ 786163fc9cSnatano }; 796163fc9cSnatano 806163fc9cSnatano typedef struct { 816163fc9cSnatano uint32_t ino; /* inode number used on target fs */ 826163fc9cSnatano uint32_t nlink; /* number of links to this entry */ 836163fc9cSnatano enum fi_flags flags; /* flags used by fs specific code */ 846163fc9cSnatano struct stat st; /* stat entry */ 856163fc9cSnatano void *fsuse; /* for storing FS dependent info */ 866163fc9cSnatano } fsinode; 876163fc9cSnatano 886163fc9cSnatano typedef struct _fsnode { 896163fc9cSnatano struct _fsnode *parent; /* parent (NULL if root) */ 906163fc9cSnatano struct _fsnode *child; /* child (if type == S_IFDIR) */ 916163fc9cSnatano struct _fsnode *next; /* next */ 926163fc9cSnatano struct _fsnode *first; /* first node of current level (".") */ 936163fc9cSnatano uint32_t type; /* type of entry */ 946163fc9cSnatano fsinode *inode; /* actual inode data */ 956163fc9cSnatano char *symlink; /* symlink target */ 966163fc9cSnatano const char *root; /* root path */ 976163fc9cSnatano char *path; /* directory name */ 986163fc9cSnatano char *name; /* file name */ 996163fc9cSnatano int flags; /* misc flags */ 1006163fc9cSnatano } fsnode; 1016163fc9cSnatano 1026163fc9cSnatano #define FSNODE_F_HASSPEC 0x01 /* fsnode has a spec entry */ 1036163fc9cSnatano 1046163fc9cSnatano /* 1056163fc9cSnatano * option_t - contains option name, description, pointer to location to store 1066163fc9cSnatano * result, and range checks for the result. Used to simplify fs specific 1076163fc9cSnatano * option setting 1086163fc9cSnatano */ 1096163fc9cSnatano typedef enum { 1106163fc9cSnatano OPT_STRARRAY, 1116163fc9cSnatano OPT_STRPTR, 1126163fc9cSnatano OPT_STRBUF, 1136163fc9cSnatano OPT_BOOL, 1146163fc9cSnatano OPT_INT8, 1156163fc9cSnatano OPT_INT16, 1166163fc9cSnatano OPT_INT32, 1176163fc9cSnatano OPT_INT64 1186163fc9cSnatano } opttype_t; 1196163fc9cSnatano 1206163fc9cSnatano typedef struct { 1216163fc9cSnatano const char *name; /* option name */ 1226163fc9cSnatano void *value; /* where to stuff the value */ 1236163fc9cSnatano opttype_t type; /* type of entry */ 1246163fc9cSnatano long long minimum; /* minimum for value */ 1256163fc9cSnatano long long maximum; /* maximum for value */ 1266163fc9cSnatano } option_t; 1276163fc9cSnatano 1286163fc9cSnatano /* 1296163fc9cSnatano * fsinfo_t - contains various settings and parameters pertaining to 1306163fc9cSnatano * the image, including current settings, global options, and fs 1316163fc9cSnatano * specific options 1326163fc9cSnatano */ 1336163fc9cSnatano typedef struct makefs_fsinfo { 1346163fc9cSnatano /* current settings */ 1356163fc9cSnatano off_t size; /* total size */ 1366163fc9cSnatano off_t inodes; /* number of inodes */ 1376163fc9cSnatano uint32_t curinode; /* current inode */ 1386163fc9cSnatano 1396163fc9cSnatano /* image settings */ 1406163fc9cSnatano int fd; /* file descriptor of image */ 1416163fc9cSnatano void *superblock; /* superblock */ 1426163fc9cSnatano 1436163fc9cSnatano /* global options */ 1446163fc9cSnatano off_t minsize; /* minimum size image should be */ 1456163fc9cSnatano off_t maxsize; /* maximum size image can be */ 1466163fc9cSnatano off_t freefiles; /* free file entries to leave */ 1476163fc9cSnatano off_t freeblocks; /* free blocks to leave */ 1486163fc9cSnatano off_t offset; /* offset from start of file */ 1496163fc9cSnatano int freefilepc; /* free file % */ 1506163fc9cSnatano int freeblockpc; /* free block % */ 1516163fc9cSnatano int sectorsize; /* sector size */ 1526163fc9cSnatano 1536163fc9cSnatano void *fs_specific; /* File system specific additions. */ 1546163fc9cSnatano option_t *fs_options; /* File system specific options */ 1556163fc9cSnatano } fsinfo_t; 1566163fc9cSnatano 1576163fc9cSnatano 1586163fc9cSnatano 1596163fc9cSnatano 1606163fc9cSnatano const char * inode_type(mode_t); 1616163fc9cSnatano int set_option(const option_t *, const char *, char *, size_t); 1626163fc9cSnatano int set_option_var(const option_t *, const char *, const char *, 1636163fc9cSnatano char *, size_t); 1644a445526Snatano fsnode * walk_dir(const char *, const char *, fsnode *, fsnode *); 1656163fc9cSnatano void free_fsnodes(fsnode *); 1666163fc9cSnatano option_t * copy_opts(const option_t *); 1676163fc9cSnatano 1686163fc9cSnatano #define DECLARE_FUN(fs) \ 1696163fc9cSnatano void fs ## _prep_opts(fsinfo_t *); \ 1706163fc9cSnatano int fs ## _parse_opts(const char *, fsinfo_t *); \ 1716163fc9cSnatano void fs ## _cleanup_opts(fsinfo_t *); \ 1726163fc9cSnatano void fs ## _makefs(const char *, const char *, fsnode *, fsinfo_t *) 1736163fc9cSnatano 1746163fc9cSnatano DECLARE_FUN(ffs); 1756163fc9cSnatano DECLARE_FUN(cd9660); 1766163fc9cSnatano DECLARE_FUN(msdos); 1776163fc9cSnatano 178bc67c994Snatano extern int Tflag; 179bc67c994Snatano extern time_t stampts; 1806163fc9cSnatano extern struct timespec start_time; 1816163fc9cSnatano 1826163fc9cSnatano 1836163fc9cSnatano #ifndef DEFAULT_FSTYPE 1846163fc9cSnatano #define DEFAULT_FSTYPE "ffs" 1856163fc9cSnatano #endif 1866163fc9cSnatano 187*4874b543Sderaadt #define MINIMUM(a, b) (((a) < (b)) ? (a) : (b)) 188*4874b543Sderaadt #define MAXIMUM(a, b) (((a) > (b)) ? (a) : (b)) 1896163fc9cSnatano 1906163fc9cSnatano /* xmalloc.c */ 1916163fc9cSnatano void *emalloc(size_t); 1926163fc9cSnatano void *ecalloc(size_t, size_t); 1936163fc9cSnatano void *erealloc(void *, size_t); 1946163fc9cSnatano char *estrdup(const char *); 1956163fc9cSnatano 1966163fc9cSnatano #endif /* _MAKEFS_H */ 197