makefs.h revision 1.7
1/*	$OpenBSD: makefs.h,v 1.7 2016/10/22 18:17:14 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
186#ifndef	DEFAULT_FSTYPE
187#define	DEFAULT_FSTYPE	"ffs"
188#endif
189
190
191/* xmalloc.c */
192void	*emalloc(size_t);
193void	*ecalloc(size_t, size_t);
194void	*erealloc(void *, size_t);
195char	*estrdup(const char *);
196
197#endif	/* _MAKEFS_H */
198