xref: /netbsd-src/usr.sbin/makefs/makefs.h (revision dc0564c197aed1acb883e38062870256bcb51f77)
1945e1caaSreinoud /*	$nEtBSD: makefs.h,v 1.38 2022/04/09 10:05:35 riastradh Exp $	*/
2009121cfSlukem 
3009121cfSlukem /*
41226af2bSlukem  * Copyright (c) 2001 Wasabi Systems, Inc.
5009121cfSlukem  * All rights reserved.
6009121cfSlukem  *
7009121cfSlukem  * Written by Luke Mewburn for Wasabi Systems, Inc.
8009121cfSlukem  *
9009121cfSlukem  * Redistribution and use in source and binary forms, with or without
10009121cfSlukem  * modification, are permitted provided that the following conditions
11009121cfSlukem  * are met:
12009121cfSlukem  * 1. Redistributions of source code must retain the above copyright
13009121cfSlukem  *    notice, this list of conditions and the following disclaimer.
14009121cfSlukem  * 2. Redistributions in binary form must reproduce the above copyright
15009121cfSlukem  *    notice, this list of conditions and the following disclaimer in the
16009121cfSlukem  *    documentation and/or other materials provided with the distribution.
17009121cfSlukem  * 3. All advertising materials mentioning features or use of this software
18009121cfSlukem  *    must display the following acknowledgement:
19009121cfSlukem  *      This product includes software developed for the NetBSD Project by
20009121cfSlukem  *      Wasabi Systems, Inc.
21009121cfSlukem  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22009121cfSlukem  *    or promote products derived from this software without specific prior
23009121cfSlukem  *    written permission.
24009121cfSlukem  *
25009121cfSlukem  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26009121cfSlukem  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27009121cfSlukem  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28009121cfSlukem  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
29009121cfSlukem  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30009121cfSlukem  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31009121cfSlukem  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32009121cfSlukem  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33009121cfSlukem  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34009121cfSlukem  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35009121cfSlukem  * POSSIBILITY OF SUCH DAMAGE.
36009121cfSlukem  */
37009121cfSlukem 
38d74b2fc0Slukem #ifndef	_MAKEFS_H
39d74b2fc0Slukem #define	_MAKEFS_H
40d74b2fc0Slukem 
41171d6532Slukem #if HAVE_NBTOOL_CONFIG_H
42171d6532Slukem #include "nbtool_config.h"
439fbd8888Stv #else
449fbd8888Stv #define HAVE_STRUCT_STAT_ST_FLAGS 1
459fbd8888Stv #define HAVE_STRUCT_STAT_ST_GEN 1
469fbd8888Stv #define HAVE_STRUCT_STAT_ST_MTIMENSEC 1
47bb9c7676Sreinoud #define HAVE_STRUCT_STATVFS_F_IOSIZE 1
4842614ed3Sfvdl #define HAVE_STRUCT_STAT_BIRTHTIME 1
497a47cf14Sjmc #define HAVE_FSTATVFS 1
509fbd8888Stv #endif
51009121cfSlukem 
52855b7dceSchristos #include <stdio.h>
539fbd8888Stv #include <sys/stat.h>
549fbd8888Stv #include <err.h>
55009121cfSlukem 
56009121cfSlukem /*
57ebbf3dddSlukem  * fsnode -
58ebbf3dddSlukem  *	a component of the tree; contains a filename, a pointer to
59ebbf3dddSlukem  *	fsinode, optional symlink name, and tree pointers
60009121cfSlukem  *
61ebbf3dddSlukem  * fsinode -
62ebbf3dddSlukem  *	equivalent to an inode, containing target file system inode number,
63ebbf3dddSlukem  *	refcount (nlink), and stat buffer
64ebbf3dddSlukem  *
65ebbf3dddSlukem  * A tree of fsnodes looks like this:
66009121cfSlukem  *
67009121cfSlukem  *	name	"."		"bin"		"netbsd"
68009121cfSlukem  *	type	S_IFDIR		S_IFDIR		S_IFREG
69009121cfSlukem  *	next 	  >		  >		NULL
70009121cfSlukem  *	parent	NULL		NULL		NULL
71009121cfSlukem  *	child	NULL		  v
72009121cfSlukem  *
73009121cfSlukem  *	name			"."		"ls"
74009121cfSlukem  *	type			S_IFDIR		S_IFREG
75009121cfSlukem  *	next			  >		NULL
76009121cfSlukem  *	parent			  ^		^ (to "bin")
77009121cfSlukem  *	child			NULL		NULL
78009121cfSlukem  *
79009121cfSlukem  * Notes:
80009121cfSlukem  *	-   first always points to first entry, at current level, which
81009121cfSlukem  *	    must be "." when the tree has been built; during build it may
82009121cfSlukem  *	    not be if "." hasn't yet been found by readdir(2).
83009121cfSlukem  */
84ebbf3dddSlukem 
85ebbf3dddSlukem enum fi_flags {
86ebbf3dddSlukem 	FI_SIZED =	1<<0,		/* inode sized */
87ebbf3dddSlukem 	FI_ALLOCATED =	1<<1,		/* fsinode->ino allocated */
88ebbf3dddSlukem 	FI_WRITTEN =	1<<2,		/* inode written */
89ebbf3dddSlukem };
90ebbf3dddSlukem 
91ebbf3dddSlukem typedef struct {
922501d4dfSreinoud 	uint64_t	 ino;		/* inode number used on target fs */
93ebbf3dddSlukem 	uint32_t	 nlink;		/* number of links to this entry */
94ebbf3dddSlukem 	enum fi_flags	 flags;		/* flags used by fs specific code */
95ebbf3dddSlukem 	struct stat	 st;		/* stat entry */
96e2036ad8Sreinoud 	void		*fsuse;		/* for storing FS dependent info */
97ebbf3dddSlukem } fsinode;
98ebbf3dddSlukem 
99009121cfSlukem typedef struct _fsnode {
100009121cfSlukem 	struct _fsnode	*parent;	/* parent (NULL if root) */
101009121cfSlukem 	struct _fsnode	*child;		/* child (if type == S_IFDIR) */
102009121cfSlukem 	struct _fsnode	*next;		/* next */
103009121cfSlukem 	struct _fsnode	*first;		/* first node of current level (".") */
104ebbf3dddSlukem 	uint32_t	 type;		/* type of entry */
105ebbf3dddSlukem 	fsinode		*inode;		/* actual inode data */
106009121cfSlukem 	char		*symlink;	/* symlink target */
107f1cc0951Schristos 	const char	*root;		/* root path */
108f1cc0951Schristos 	char		*path;		/* directory name */
109009121cfSlukem 	char		*name;		/* file name */
1101bcb9d76Sthorpej 	int		flags;		/* misc flags */
111009121cfSlukem } fsnode;
112009121cfSlukem 
1131bcb9d76Sthorpej #define	FSNODE_F_HASSPEC	0x01	/* fsnode has a spec entry */
114009121cfSlukem 
115009121cfSlukem /*
116e4989541Schristos  * option_t - contains option name, description, pointer to location to store
117e4989541Schristos  * result, and range checks for the result. Used to simplify fs specific
118e4989541Schristos  * option setting
119e4989541Schristos  */
120e4989541Schristos typedef enum {
121e4989541Schristos 	OPT_STRARRAY,
122e4989541Schristos 	OPT_STRPTR,
12350d02345Schristos 	OPT_STRBUF,
124e4989541Schristos 	OPT_BOOL,
125e4989541Schristos 	OPT_INT8,
126e4989541Schristos 	OPT_INT16,
127e4989541Schristos 	OPT_INT32,
128e4989541Schristos 	OPT_INT64
129e4989541Schristos } opttype_t;
130e4989541Schristos 
131e4989541Schristos typedef struct {
132e4989541Schristos 	char		letter;		/* option letter NUL for none */
133e4989541Schristos 	const char	*name;		/* option name */
134e4989541Schristos 	void		*value;		/* where to stuff the value */
135e4989541Schristos 	opttype_t	type;		/* type of entry */
136e4989541Schristos 	long long	minimum;	/* minimum for value */
137e4989541Schristos 	long long	maximum;	/* maximum for value */
138e4989541Schristos 	const char	*desc;		/* option description */
139e4989541Schristos } option_t;
140e4989541Schristos 
141e4989541Schristos /*
142009121cfSlukem  * fsinfo_t - contains various settings and parameters pertaining to
143009121cfSlukem  * the image, including current settings, global options, and fs
144009121cfSlukem  * specific options
145009121cfSlukem  */
146d84c38aeSchristos typedef struct makefs_fsinfo {
147009121cfSlukem 		/* current settings */
148009121cfSlukem 	off_t	size;		/* total size */
149009121cfSlukem 	off_t	inodes;		/* number of inodes */
150009121cfSlukem 	uint32_t curinode;	/* current inode */
151009121cfSlukem 
152009121cfSlukem 		/* image settings */
153009121cfSlukem 	int	fd;		/* file descriptor of image */
154009121cfSlukem 	void	*superblock;	/* superblock */
1556ddeaceaSlukem 	int	onlyspec;	/* only add entries in specfile */
156009121cfSlukem 
157009121cfSlukem 
158009121cfSlukem 		/* global options */
159009121cfSlukem 	off_t	minsize;	/* minimum size image should be */
160009121cfSlukem 	off_t	maxsize;	/* maximum size image can be */
161009121cfSlukem 	off_t	freefiles;	/* free file entries to leave */
162009121cfSlukem 	off_t	freeblocks;	/* free blocks to leave */
1632d692a95Schristos 	off_t	offset;		/* offset from start of file */
1642d692a95Schristos 	int	freefilepc;	/* free file % */
165009121cfSlukem 	int	freeblockpc;	/* free block % */
166009121cfSlukem 	int	needswap;	/* non-zero if byte swapping needed */
167009121cfSlukem 	int	sectorsize;	/* sector size */
16807f6254fSsjg 	int	sparse;		/* sparse image, don't fill it with zeros */
169c06c93b2Schristos 	int	replace;	/* replace files when merging */
170ba42c78dSsimonb 	int	follow;		/* follow symlinks */
171009121cfSlukem 
1722406596eSjmc 	void	*fs_specific;	/* File system specific additions. */
173e4989541Schristos 	option_t *fs_options;	/* File system specific options */
174009121cfSlukem } fsinfo_t;
175009121cfSlukem 
176009121cfSlukem 
177009121cfSlukem 
178009121cfSlukem 
179d0c4ff45Sdbj void		apply_specfile(const char *, const char *, fsnode *, int);
180f1cc0951Schristos void		dump_fsnodes(fsnode *);
181009121cfSlukem const char *	inode_type(mode_t);
18250d02345Schristos int		set_option(const option_t *, const char *, char *, size_t);
183855b7dceSchristos void		print_options(FILE *, const option_t *);
18450d02345Schristos int		set_option_var(const option_t *, const char *, const char *,
18550d02345Schristos     char *, size_t);
186ba42c78dSsimonb fsnode *	walk_dir(const char *, const char *, fsnode *, fsnode *, int,
187ba42c78dSsimonb     int);
1880e392af9Sdbj void		free_fsnodes(fsnode *);
189e4989541Schristos option_t *	copy_opts(const option_t *);
190009121cfSlukem 
1913d364f54Schristos #define DECLARE_FUN(fs)							\
1923d364f54Schristos void		fs ## _prep_opts(fsinfo_t *);				\
1933d364f54Schristos int		fs ## _parse_opts(const char *, fsinfo_t *);		\
1943d364f54Schristos void		fs ## _cleanup_opts(fsinfo_t *);			\
1953d364f54Schristos void		fs ## _makefs(const char *, const char *, fsnode *, fsinfo_t *)
196009121cfSlukem 
1973d364f54Schristos DECLARE_FUN(ffs);
1983d364f54Schristos DECLARE_FUN(cd9660);
1993d364f54Schristos DECLARE_FUN(chfs);
2003d364f54Schristos DECLARE_FUN(v7fs);
2013d364f54Schristos DECLARE_FUN(msdos);
202e2036ad8Sreinoud DECLARE_FUN(udf);
203009121cfSlukem 
204b2f78261Sjmc extern	u_int		debug;
205009121cfSlukem extern	struct timespec	start_time;
206799916c0Schristos extern	struct stat stampst;
207009121cfSlukem 
2081bcb9d76Sthorpej /*
2091bcb9d76Sthorpej  * If -x is specified, we want to exclude nodes which do not appear
2101bcb9d76Sthorpej  * in the spec file.
2111bcb9d76Sthorpej  */
2126ddeaceaSlukem #define	FSNODE_EXCLUDE_P(opts, fsnode)	\
2136ddeaceaSlukem 	((opts)->onlyspec != 0 && ((fsnode)->flags & FSNODE_F_HASSPEC) == 0)
2141bcb9d76Sthorpej 
215009121cfSlukem #define	DEBUG_TIME			0x00000001
216ae62d656Slukem 		/* debug bits 1..3 unused at this time */
217009121cfSlukem #define	DEBUG_WALK_DIR			0x00000010
218009121cfSlukem #define	DEBUG_WALK_DIR_NODE		0x00000020
219009121cfSlukem #define	DEBUG_WALK_DIR_LINKCHECK	0x00000040
220009121cfSlukem #define	DEBUG_DUMP_FSNODES		0x00000080
221009121cfSlukem #define	DEBUG_DUMP_FSNODES_VERBOSE	0x00000100
222009121cfSlukem #define	DEBUG_FS_PARSE_OPTS		0x00000200
223009121cfSlukem #define	DEBUG_FS_MAKEFS			0x00000400
224009121cfSlukem #define	DEBUG_FS_VALIDATE		0x00000800
225009121cfSlukem #define	DEBUG_FS_CREATE_IMAGE		0x00001000
226009121cfSlukem #define	DEBUG_FS_SIZE_DIR		0x00002000
227009121cfSlukem #define	DEBUG_FS_SIZE_DIR_NODE		0x00004000
228009121cfSlukem #define	DEBUG_FS_SIZE_DIR_ADD_DIRENT	0x00008000
229009121cfSlukem #define	DEBUG_FS_POPULATE		0x00010000
230009121cfSlukem #define	DEBUG_FS_POPULATE_DIRBUF	0x00020000
231009121cfSlukem #define	DEBUG_FS_POPULATE_NODE		0x00040000
232009121cfSlukem #define	DEBUG_FS_WRITE_FILE		0x00080000
233009121cfSlukem #define	DEBUG_FS_WRITE_FILE_BLOCK	0x00100000
234009121cfSlukem #define	DEBUG_FS_MAKE_DIRBUF		0x00200000
235009121cfSlukem #define	DEBUG_FS_WRITE_INODE		0x00400000
236009121cfSlukem #define	DEBUG_BUF_BREAD			0x00800000
237009121cfSlukem #define	DEBUG_BUF_BWRITE		0x01000000
238009121cfSlukem #define	DEBUG_BUF_GETBLK		0x02000000
239009121cfSlukem #define	DEBUG_APPLY_SPECFILE		0x04000000
240009121cfSlukem #define	DEBUG_APPLY_SPECENTRY		0x08000000
241d0c4ff45Sdbj #define	DEBUG_APPLY_SPECONLY		0x10000000
242009121cfSlukem 
243*dc0564c1Schristos #define DEBUG_STRINGS \
244*dc0564c1Schristos 	{ "time",	DEBUG_TIME }, \
245*dc0564c1Schristos 	{ "walk_dir",	DEBUG_WALK_DIR }, \
246*dc0564c1Schristos 	{ "walk_dir_node",	DEBUG_WALK_DIR_NODE }, \
247*dc0564c1Schristos 	{ "walk_dir_linkcheck",	DEBUG_WALK_DIR_LINKCHECK }, \
248*dc0564c1Schristos 	{ "dump_fsnodes",	DEBUG_DUMP_FSNODES }, \
249*dc0564c1Schristos 	{ "dump_fsnodes_verbose",	DEBUG_DUMP_FSNODES_VERBOSE }, \
250*dc0564c1Schristos 	{ "fs_parse_opts",	DEBUG_FS_PARSE_OPTS }, \
251*dc0564c1Schristos 	{ "fs_makefs",	DEBUG_FS_MAKEFS }, \
252*dc0564c1Schristos 	{ "fs_validate",	DEBUG_FS_VALIDATE }, \
253*dc0564c1Schristos 	{ "fs_create_image",	DEBUG_FS_CREATE_IMAGE }, \
254*dc0564c1Schristos 	{ "fs_size_dir",	DEBUG_FS_SIZE_DIR }, \
255*dc0564c1Schristos 	{ "fs_size_dir_node",	DEBUG_FS_SIZE_DIR_NODE }, \
256*dc0564c1Schristos 	{ "fs_size_dir_add_dirent",	DEBUG_FS_SIZE_DIR_ADD_DIRENT }, \
257*dc0564c1Schristos 	{ "fs_populate",	DEBUG_FS_POPULATE }, \
258*dc0564c1Schristos 	{ "fs_populate_dirbuf",	DEBUG_FS_POPULATE_DIRBUF }, \
259*dc0564c1Schristos 	{ "fs_populate_node",	DEBUG_FS_POPULATE_NODE }, \
260*dc0564c1Schristos 	{ "fs_write_file",	DEBUG_FS_WRITE_FILE }, \
261*dc0564c1Schristos 	{ "fs_write_file_block",	DEBUG_FS_WRITE_FILE_BLOCK }, \
262*dc0564c1Schristos 	{ "fs_make_dirbuf",	DEBUG_FS_MAKE_DIRBUF }, \
263*dc0564c1Schristos 	{ "fs_write_inode",	DEBUG_FS_WRITE_INODE }, \
264*dc0564c1Schristos 	{ "buf_bread",	DEBUG_BUF_BREAD }, \
265*dc0564c1Schristos 	{ "buf_bwrite",	DEBUG_BUF_BWRITE }, \
266*dc0564c1Schristos 	{ "buf_getblk",	DEBUG_BUF_GETBLK }, \
267*dc0564c1Schristos 	{ "apply_specfile",	DEBUG_APPLY_SPECFILE }, \
268*dc0564c1Schristos 	{ "apply_specentry",	DEBUG_APPLY_SPECENTRY }, \
269*dc0564c1Schristos 	{ "apply_speconly",	DEBUG_APPLY_SPECONLY },
270009121cfSlukem 
271009121cfSlukem #define	TIMER_START(x)				\
272009121cfSlukem 	if (debug & DEBUG_TIME)			\
273009121cfSlukem 		gettimeofday(&(x), NULL)
274009121cfSlukem 
275009121cfSlukem #define	TIMER_RESULTS(x,d)				\
276009121cfSlukem 	if (debug & DEBUG_TIME) {			\
277009121cfSlukem 		struct timeval end, td;			\
278009121cfSlukem 		gettimeofday(&end, NULL);		\
279009121cfSlukem 		timersub(&end, &(x), &td);		\
28033cb72e1Schristos 		printf("%s took %lld.%06ld seconds\n",	\
28133cb72e1Schristos 		    (d), (long long)td.tv_sec,		\
28233cb72e1Schristos 		    (long)td.tv_usec);			\
283009121cfSlukem 	}
284009121cfSlukem 
285009121cfSlukem 
286009121cfSlukem #ifndef	DEFAULT_FSTYPE
287009121cfSlukem #define	DEFAULT_FSTYPE	"ffs"
288009121cfSlukem #endif
289d74b2fc0Slukem 
290d74b2fc0Slukem 
291d74b2fc0Slukem /*
292d74b2fc0Slukem  *	ffs specific settings
293d74b2fc0Slukem  *	---------------------
294d74b2fc0Slukem  */
295d74b2fc0Slukem 
296d74b2fc0Slukem #define	FFS_EI		/* for opposite endian support in ffs headers */
297d74b2fc0Slukem 
298d74b2fc0Slukem 
299d74b2fc0Slukem #endif	/* _MAKEFS_H */
300