makefs.h revision 1.6
1/* $OpenBSD: makefs.h,v 1.6 2016/10/17 07:54:17 natano Exp $ */ 2/* $NetBSD: makefs.h,v 1.36 2015/11/25 00:48:49 christos Exp $ */ 3 4/* 5 * Copyright (c) 2001 Wasabi Systems, Inc. 6 * All rights reserved. 7 * 8 * Written by Luke Mewburn for Wasabi Systems, Inc. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed for the NetBSD Project by 21 * Wasabi Systems, Inc. 22 * 4. The name of Wasabi Systems, Inc. may not be used to endorse 23 * or promote products derived from this software without specific prior 24 * written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39#ifndef _MAKEFS_H 40#define _MAKEFS_H 41 42#include <sys/stat.h> 43#include <err.h> 44 45/* 46 * fsnode - 47 * a component of the tree; contains a filename, a pointer to 48 * fsinode, optional symlink name, and tree pointers 49 * 50 * fsinode - 51 * equivalent to an inode, containing target file system inode number, 52 * refcount (nlink), and stat buffer 53 * 54 * A tree of fsnodes looks like this: 55 * 56 * name "." "bin" "netbsd" 57 * type S_IFDIR S_IFDIR S_IFREG 58 * next > > NULL 59 * parent NULL NULL NULL 60 * child NULL v 61 * 62 * name "." "ls" 63 * type S_IFDIR S_IFREG 64 * next > NULL 65 * parent ^ ^ (to "bin") 66 * child NULL NULL 67 * 68 * Notes: 69 * - first always points to first entry, at current level, which 70 * must be "." when the tree has been built; during build it may 71 * not be if "." hasn't yet been found by readdir(2). 72 */ 73 74enum fi_flags { 75 FI_SIZED = 1<<0, /* inode sized */ 76 FI_ALLOCATED = 1<<1, /* fsinode->ino allocated */ 77 FI_WRITTEN = 1<<2, /* inode written */ 78}; 79 80typedef struct { 81 uint32_t ino; /* inode number used on target fs */ 82 uint32_t nlink; /* number of links to this entry */ 83 enum fi_flags flags; /* flags used by fs specific code */ 84 struct stat st; /* stat entry */ 85 void *fsuse; /* for storing FS dependent info */ 86} fsinode; 87 88typedef struct _fsnode { 89 struct _fsnode *parent; /* parent (NULL if root) */ 90 struct _fsnode *child; /* child (if type == S_IFDIR) */ 91 struct _fsnode *next; /* next */ 92 struct _fsnode *first; /* first node of current level (".") */ 93 uint32_t type; /* type of entry */ 94 fsinode *inode; /* actual inode data */ 95 char *symlink; /* symlink target */ 96 const char *root; /* root path */ 97 char *path; /* directory name */ 98 char *name; /* file name */ 99 int flags; /* misc flags */ 100} fsnode; 101 102#define FSNODE_F_HASSPEC 0x01 /* fsnode has a spec entry */ 103 104/* 105 * option_t - contains option name, description, pointer to location to store 106 * result, and range checks for the result. Used to simplify fs specific 107 * option setting 108 */ 109typedef enum { 110 OPT_STRARRAY, 111 OPT_STRPTR, 112 OPT_STRBUF, 113 OPT_BOOL, 114 OPT_INT8, 115 OPT_INT16, 116 OPT_INT32, 117 OPT_INT64 118} opttype_t; 119 120typedef struct { 121 char letter; /* option letter NUL for none */ 122 const char *name; /* option name */ 123 void *value; /* where to stuff the value */ 124 opttype_t type; /* type of entry */ 125 long long minimum; /* minimum for value */ 126 long long maximum; /* maximum for value */ 127 const char *desc; /* option description */ 128} option_t; 129 130/* 131 * fsinfo_t - contains various settings and parameters pertaining to 132 * the image, including current settings, global options, and fs 133 * specific options 134 */ 135typedef struct makefs_fsinfo { 136 /* current settings */ 137 off_t size; /* total size */ 138 off_t inodes; /* number of inodes */ 139 uint32_t curinode; /* current inode */ 140 141 /* image settings */ 142 int fd; /* file descriptor of image */ 143 void *superblock; /* superblock */ 144 145 /* global options */ 146 off_t minsize; /* minimum size image should be */ 147 off_t maxsize; /* maximum size image can be */ 148 off_t freefiles; /* free file entries to leave */ 149 off_t freeblocks; /* free blocks to leave */ 150 off_t offset; /* offset from start of file */ 151 int freefilepc; /* free file % */ 152 int freeblockpc; /* free block % */ 153 int sectorsize; /* sector size */ 154 155 void *fs_specific; /* File system specific additions. */ 156 option_t *fs_options; /* File system specific options */ 157} fsinfo_t; 158 159 160 161 162void dump_fsnodes(fsnode *); 163const char * inode_type(mode_t); 164int set_option(const option_t *, const char *, char *, size_t); 165int set_option_var(const option_t *, const char *, const char *, 166 char *, size_t); 167fsnode * walk_dir(const char *, const char *, fsnode *, fsnode *); 168void free_fsnodes(fsnode *); 169option_t * copy_opts(const option_t *); 170 171#define DECLARE_FUN(fs) \ 172void fs ## _prep_opts(fsinfo_t *); \ 173int fs ## _parse_opts(const char *, fsinfo_t *); \ 174void fs ## _cleanup_opts(fsinfo_t *); \ 175void fs ## _makefs(const char *, const char *, fsnode *, fsinfo_t *) 176 177DECLARE_FUN(ffs); 178DECLARE_FUN(cd9660); 179DECLARE_FUN(msdos); 180 181extern u_int debug; 182extern struct timespec start_time; 183extern struct stat stampst; 184 185#define DEBUG_TIME 0x00000001 186 /* debug bits 1..3 unused at this time */ 187#define DEBUG_WALK_DIR 0x00000010 188#define DEBUG_WALK_DIR_NODE 0x00000020 189#define DEBUG_WALK_DIR_LINKCHECK 0x00000040 190#define DEBUG_DUMP_FSNODES 0x00000080 191#define DEBUG_DUMP_FSNODES_VERBOSE 0x00000100 192#define DEBUG_FS_PARSE_OPTS 0x00000200 193#define DEBUG_FS_MAKEFS 0x00000400 194#define DEBUG_FS_VALIDATE 0x00000800 195#define DEBUG_FS_CREATE_IMAGE 0x00001000 196#define DEBUG_FS_SIZE_DIR 0x00002000 197#define DEBUG_FS_SIZE_DIR_NODE 0x00004000 198#define DEBUG_FS_SIZE_DIR_ADD_DIRENT 0x00008000 199#define DEBUG_FS_POPULATE 0x00010000 200#define DEBUG_FS_POPULATE_DIRBUF 0x00020000 201#define DEBUG_FS_POPULATE_NODE 0x00040000 202#define DEBUG_FS_WRITE_FILE 0x00080000 203#define DEBUG_FS_WRITE_FILE_BLOCK 0x00100000 204#define DEBUG_FS_MAKE_DIRBUF 0x00200000 205#define DEBUG_FS_WRITE_INODE 0x00400000 206#define DEBUG_BUF_BREAD 0x00800000 207#define DEBUG_BUF_BWRITE 0x01000000 208#define DEBUG_BUF_GETBLK 0x02000000 209#define DEBUG_APPLY_SPECFILE 0x04000000 210#define DEBUG_APPLY_SPECENTRY 0x08000000 211#define DEBUG_APPLY_SPECONLY 0x10000000 212 213 214#define TIMER_START(x) \ 215 if (debug & DEBUG_TIME) \ 216 gettimeofday(&(x), NULL) 217 218#define TIMER_RESULTS(x,d) \ 219 if (debug & DEBUG_TIME) { \ 220 struct timeval end, td; \ 221 gettimeofday(&end, NULL); \ 222 timersub(&end, &(x), &td); \ 223 printf("%s took %lld.%06ld seconds\n", \ 224 (d), (long long)td.tv_sec, \ 225 (long)td.tv_usec); \ 226 } 227 228 229#ifndef DEFAULT_FSTYPE 230#define DEFAULT_FSTYPE "ffs" 231#endif 232 233 234/* xmalloc.c */ 235void *emalloc(size_t); 236void *ecalloc(size_t, size_t); 237void *erealloc(void *, size_t); 238char *estrdup(const char *); 239 240#endif /* _MAKEFS_H */ 241