xref: /openbsd/usr.sbin/makefs/makefs.h (revision 4874b543)
1 /*	$OpenBSD: makefs.h,v 1.13 2021/10/06 00:40:39 deraadt 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 
74 enum 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 
80 typedef 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 
88 typedef 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  */
109 typedef 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 
120 typedef struct {
121 	const char	*name;		/* option name */
122 	void		*value;		/* where to stuff the value */
123 	opttype_t	type;		/* type of entry */
124 	long long	minimum;	/* minimum for value */
125 	long long	maximum;	/* maximum for value */
126 } option_t;
127 
128 /*
129  * fsinfo_t - contains various settings and parameters pertaining to
130  * the image, including current settings, global options, and fs
131  * specific options
132  */
133 typedef struct makefs_fsinfo {
134 		/* current settings */
135 	off_t	size;		/* total size */
136 	off_t	inodes;		/* number of inodes */
137 	uint32_t curinode;	/* current inode */
138 
139 		/* image settings */
140 	int	fd;		/* file descriptor of image */
141 	void	*superblock;	/* superblock */
142 
143 		/* global options */
144 	off_t	minsize;	/* minimum size image should be */
145 	off_t	maxsize;	/* maximum size image can be */
146 	off_t	freefiles;	/* free file entries to leave */
147 	off_t	freeblocks;	/* free blocks to leave */
148 	off_t	offset;		/* offset from start of file */
149 	int	freefilepc;	/* free file % */
150 	int	freeblockpc;	/* free block % */
151 	int	sectorsize;	/* sector size */
152 
153 	void	*fs_specific;	/* File system specific additions. */
154 	option_t *fs_options;	/* File system specific options */
155 } fsinfo_t;
156 
157 
158 
159 
160 const char *	inode_type(mode_t);
161 int		set_option(const option_t *, const char *, char *, size_t);
162 int		set_option_var(const option_t *, const char *, const char *,
163     char *, size_t);
164 fsnode *	walk_dir(const char *, const char *, fsnode *, fsnode *);
165 void		free_fsnodes(fsnode *);
166 option_t *	copy_opts(const option_t *);
167 
168 #define DECLARE_FUN(fs)							\
169 void		fs ## _prep_opts(fsinfo_t *);				\
170 int		fs ## _parse_opts(const char *, fsinfo_t *);		\
171 void		fs ## _cleanup_opts(fsinfo_t *);			\
172 void		fs ## _makefs(const char *, const char *, fsnode *, fsinfo_t *)
173 
174 DECLARE_FUN(ffs);
175 DECLARE_FUN(cd9660);
176 DECLARE_FUN(msdos);
177 
178 extern	int Tflag;
179 extern	time_t stampts;
180 extern	struct timespec	start_time;
181 
182 
183 #ifndef	DEFAULT_FSTYPE
184 #define	DEFAULT_FSTYPE	"ffs"
185 #endif
186 
187 #define MINIMUM(a, b)	(((a) < (b)) ? (a) : (b))
188 #define MAXIMUM(a, b)	(((a) > (b)) ? (a) : (b))
189 
190 /* xmalloc.c */
191 void	*emalloc(size_t);
192 void	*ecalloc(size_t, size_t);
193 void	*erealloc(void *, size_t);
194 char	*estrdup(const char *);
195 
196 #endif	/* _MAKEFS_H */
197