xref: /freebsd-src/cddl/contrib/opensolaris/lib/libcmdutils/libcmdutils.h (revision c2c014f24c10f90d85126ac5fbd4d8524de32b1c)
1*1c05a6eaSAndriy Gapon /*
2*1c05a6eaSAndriy Gapon  * CDDL HEADER START
3*1c05a6eaSAndriy Gapon  *
4*1c05a6eaSAndriy Gapon  * The contents of this file are subject to the terms of the
5*1c05a6eaSAndriy Gapon  * Common Development and Distribution License (the "License").
6*1c05a6eaSAndriy Gapon  * You may not use this file except in compliance with the License.
7*1c05a6eaSAndriy Gapon  *
8*1c05a6eaSAndriy Gapon  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*1c05a6eaSAndriy Gapon  * or http://www.opensolaris.org/os/licensing.
10*1c05a6eaSAndriy Gapon  * See the License for the specific language governing permissions
11*1c05a6eaSAndriy Gapon  * and limitations under the License.
12*1c05a6eaSAndriy Gapon  *
13*1c05a6eaSAndriy Gapon  * When distributing Covered Code, include this CDDL HEADER in each
14*1c05a6eaSAndriy Gapon  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*1c05a6eaSAndriy Gapon  * If applicable, add the following below this CDDL HEADER, with the
16*1c05a6eaSAndriy Gapon  * fields enclosed by brackets "[]" replaced with your own identifying
17*1c05a6eaSAndriy Gapon  * information: Portions Copyright [yyyy] [name of copyright owner]
18*1c05a6eaSAndriy Gapon  *
19*1c05a6eaSAndriy Gapon  * CDDL HEADER END
20*1c05a6eaSAndriy Gapon  */
21*1c05a6eaSAndriy Gapon /*
22*1c05a6eaSAndriy Gapon  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23*1c05a6eaSAndriy Gapon  * Use is subject to license terms.
24*1c05a6eaSAndriy Gapon  */
25*1c05a6eaSAndriy Gapon /*
26*1c05a6eaSAndriy Gapon  * Copyright (c) 2013 RackTop Systems.
27*1c05a6eaSAndriy Gapon  */
28*1c05a6eaSAndriy Gapon /*
29*1c05a6eaSAndriy Gapon  * Copyright 2017 Joyent, Inc.
30*1c05a6eaSAndriy Gapon  */
31*1c05a6eaSAndriy Gapon 
32*1c05a6eaSAndriy Gapon /*
33*1c05a6eaSAndriy Gapon  * Declarations for the functions in libcmdutils.
34*1c05a6eaSAndriy Gapon  */
35*1c05a6eaSAndriy Gapon 
36*1c05a6eaSAndriy Gapon #ifndef	_LIBCMDUTILS_H
37*1c05a6eaSAndriy Gapon #define	_LIBCMDUTILS_H
38*1c05a6eaSAndriy Gapon 
39*1c05a6eaSAndriy Gapon #ifdef illumos
40*1c05a6eaSAndriy Gapon #if !defined(_LP64) && \
41*1c05a6eaSAndriy Gapon 	!((_FILE_OFFSET_BITS == 64) || defined(_LARGEFILE64_SOURCE))
42*1c05a6eaSAndriy Gapon #error "libcmdutils.h can only be used in a largefile compilation environment"
43*1c05a6eaSAndriy Gapon #endif
44*1c05a6eaSAndriy Gapon #endif
45*1c05a6eaSAndriy Gapon 
46*1c05a6eaSAndriy Gapon /*
47*1c05a6eaSAndriy Gapon  * This is a private header file.  Applications should not directly include
48*1c05a6eaSAndriy Gapon  * this file.
49*1c05a6eaSAndriy Gapon  */
50*1c05a6eaSAndriy Gapon 
51*1c05a6eaSAndriy Gapon #include <stdio.h>
52*1c05a6eaSAndriy Gapon #include <unistd.h>
53*1c05a6eaSAndriy Gapon #include <stdlib.h>
54*1c05a6eaSAndriy Gapon #include <stdarg.h>
55*1c05a6eaSAndriy Gapon #include <errno.h>
56*1c05a6eaSAndriy Gapon #include <fcntl.h>
57*1c05a6eaSAndriy Gapon #include <limits.h>
58*1c05a6eaSAndriy Gapon #include <libintl.h>
59*1c05a6eaSAndriy Gapon #include <string.h>
60*1c05a6eaSAndriy Gapon #include <dirent.h>
61*1c05a6eaSAndriy Gapon #ifdef illumos
62*1c05a6eaSAndriy Gapon #include <attr.h>
63*1c05a6eaSAndriy Gapon #endif
64*1c05a6eaSAndriy Gapon #include <sys/avl.h>
65*1c05a6eaSAndriy Gapon #include <sys/types.h>
66*1c05a6eaSAndriy Gapon #include <sys/stat.h>
67*1c05a6eaSAndriy Gapon #include <sys/mman.h>
68*1c05a6eaSAndriy Gapon #include <libnvpair.h>
69*1c05a6eaSAndriy Gapon 
70*1c05a6eaSAndriy Gapon #ifdef	__cplusplus
71*1c05a6eaSAndriy Gapon extern "C" {
72*1c05a6eaSAndriy Gapon #endif
73*1c05a6eaSAndriy Gapon 
74*1c05a6eaSAndriy Gapon /* extended system attribute support */
75*1c05a6eaSAndriy Gapon #define	_NOT_SATTR	0
76*1c05a6eaSAndriy Gapon #define	_RO_SATTR	1
77*1c05a6eaSAndriy Gapon #define	_RW_SATTR	2
78*1c05a6eaSAndriy Gapon 
79*1c05a6eaSAndriy Gapon #define	MAXMAPSIZE	(1024*1024*8)	/* map at most 8MB */
80*1c05a6eaSAndriy Gapon #define	SMALLFILESIZE	(32*1024)	/* don't use mmap on little file */
81*1c05a6eaSAndriy Gapon 
82*1c05a6eaSAndriy Gapon /* Type used for a node containing a device id and inode number */
83*1c05a6eaSAndriy Gapon 
84*1c05a6eaSAndriy Gapon #if defined(_LP64) || (_FILE_OFFSET_BITS == 64)
85*1c05a6eaSAndriy Gapon typedef struct tree_node {
86*1c05a6eaSAndriy Gapon 	dev_t		node_dev;
87*1c05a6eaSAndriy Gapon 	ino_t		node_ino;
88*1c05a6eaSAndriy Gapon 	avl_node_t	avl_link;
89*1c05a6eaSAndriy Gapon } tree_node_t;
90*1c05a6eaSAndriy Gapon #else
91*1c05a6eaSAndriy Gapon typedef struct tree_node {
92*1c05a6eaSAndriy Gapon 	dev_t		node_dev;
93*1c05a6eaSAndriy Gapon 	ino64_t		node_ino;
94*1c05a6eaSAndriy Gapon 	avl_node_t	avl_link;
95*1c05a6eaSAndriy Gapon } tree_node_t;
96*1c05a6eaSAndriy Gapon #endif
97*1c05a6eaSAndriy Gapon 
98*1c05a6eaSAndriy Gapon 		/* extended system attribute support */
99*1c05a6eaSAndriy Gapon 
100*1c05a6eaSAndriy Gapon /* Determine if a file is the name of an extended system attribute file */
101*1c05a6eaSAndriy Gapon extern int sysattr_type(char *);
102*1c05a6eaSAndriy Gapon 
103*1c05a6eaSAndriy Gapon /* Determine if the underlying file system supports system attributes */
104*1c05a6eaSAndriy Gapon extern int sysattr_support(char *, int);
105*1c05a6eaSAndriy Gapon 
106*1c05a6eaSAndriy Gapon /* Copies the content of the source file to the target file */
107*1c05a6eaSAndriy Gapon #if defined(_LP64) || (_FILE_OFFSET_BITS == 64)
108*1c05a6eaSAndriy Gapon extern int writefile(int, int, char *, char *, char *, char *,
109*1c05a6eaSAndriy Gapon 	struct stat *, struct stat *);
110*1c05a6eaSAndriy Gapon #else
111*1c05a6eaSAndriy Gapon extern int writefile(int, int, char *, char *, char *, char *,
112*1c05a6eaSAndriy Gapon 	struct stat64 *, struct stat64 *);
113*1c05a6eaSAndriy Gapon #endif
114*1c05a6eaSAndriy Gapon 
115*1c05a6eaSAndriy Gapon /* Gets file descriptors of the source and target attribute files */
116*1c05a6eaSAndriy Gapon extern int get_attrdirs(int, int, char *, int *, int *);
117*1c05a6eaSAndriy Gapon 
118*1c05a6eaSAndriy Gapon /* Move extended attribute and extended system attribute */
119*1c05a6eaSAndriy Gapon extern int mv_xattrs(char *, char *, char *, int, int);
120*1c05a6eaSAndriy Gapon 
121*1c05a6eaSAndriy Gapon /* Returns non default extended system attribute list */
122*1c05a6eaSAndriy Gapon extern nvlist_t *sysattr_list(char *, int, char *);
123*1c05a6eaSAndriy Gapon 
124*1c05a6eaSAndriy Gapon 
125*1c05a6eaSAndriy Gapon 
126*1c05a6eaSAndriy Gapon 		/* avltree */
127*1c05a6eaSAndriy Gapon 
128*1c05a6eaSAndriy Gapon /*
129*1c05a6eaSAndriy Gapon  * Used to compare two nodes.  We are attempting to match the 1st
130*1c05a6eaSAndriy Gapon  * argument (node) against the 2nd argument (a node which
131*1c05a6eaSAndriy Gapon  * is already in the search tree).
132*1c05a6eaSAndriy Gapon  */
133*1c05a6eaSAndriy Gapon 
134*1c05a6eaSAndriy Gapon extern int tnode_compare(const void *, const void *);
135*1c05a6eaSAndriy Gapon 
136*1c05a6eaSAndriy Gapon /*
137*1c05a6eaSAndriy Gapon  * Used to add a single node (containing the input device id and
138*1c05a6eaSAndriy Gapon  * inode number) to the specified search tree.  The calling
139*1c05a6eaSAndriy Gapon  * application must set the tree pointer to NULL before calling
140*1c05a6eaSAndriy Gapon  * add_tnode() for the first time.
141*1c05a6eaSAndriy Gapon  */
142*1c05a6eaSAndriy Gapon #if defined(_LP64) || (_FILE_OFFSET_BITS == 64)
143*1c05a6eaSAndriy Gapon extern int add_tnode(avl_tree_t **, dev_t, ino_t);
144*1c05a6eaSAndriy Gapon #else
145*1c05a6eaSAndriy Gapon extern int add_tnode(avl_tree_t **, dev_t, ino64_t);
146*1c05a6eaSAndriy Gapon #endif
147*1c05a6eaSAndriy Gapon 
148*1c05a6eaSAndriy Gapon /*
149*1c05a6eaSAndriy Gapon  * Used to destroy a whole tree (all nodes) without rebalancing.
150*1c05a6eaSAndriy Gapon  * The calling application is responsible for setting the tree
151*1c05a6eaSAndriy Gapon  * pointer to NULL upon return.
152*1c05a6eaSAndriy Gapon  */
153*1c05a6eaSAndriy Gapon extern void destroy_tree(avl_tree_t *);
154*1c05a6eaSAndriy Gapon 
155*1c05a6eaSAndriy Gapon 
156*1c05a6eaSAndriy Gapon 
157*1c05a6eaSAndriy Gapon 		/* user/group id helpers */
158*1c05a6eaSAndriy Gapon 
159*1c05a6eaSAndriy Gapon /*
160*1c05a6eaSAndriy Gapon  * Used to get the next available user id in given range.
161*1c05a6eaSAndriy Gapon  */
162*1c05a6eaSAndriy Gapon extern int findnextuid(uid_t, uid_t, uid_t *);
163*1c05a6eaSAndriy Gapon 
164*1c05a6eaSAndriy Gapon /*
165*1c05a6eaSAndriy Gapon  * Used to get the next available group id in given range.
166*1c05a6eaSAndriy Gapon  */
167*1c05a6eaSAndriy Gapon extern int findnextgid(gid_t, gid_t, gid_t *);
168*1c05a6eaSAndriy Gapon 
169*1c05a6eaSAndriy Gapon 
170*1c05a6eaSAndriy Gapon 
171*1c05a6eaSAndriy Gapon 		/* dynamic string utilities */
172*1c05a6eaSAndriy Gapon 
173*1c05a6eaSAndriy Gapon typedef struct custr custr_t;
174*1c05a6eaSAndriy Gapon 
175*1c05a6eaSAndriy Gapon /*
176*1c05a6eaSAndriy Gapon  * Allocate and free a "custr_t" dynamic string object.  Returns 0 on success
177*1c05a6eaSAndriy Gapon  * and -1 otherwise.
178*1c05a6eaSAndriy Gapon  */
179*1c05a6eaSAndriy Gapon extern int custr_alloc(custr_t **);
180*1c05a6eaSAndriy Gapon extern void custr_free(custr_t *);
181*1c05a6eaSAndriy Gapon 
182*1c05a6eaSAndriy Gapon /*
183*1c05a6eaSAndriy Gapon  * Allocate a "custr_t" dynamic string object that operates on a fixed external
184*1c05a6eaSAndriy Gapon  * buffer.
185*1c05a6eaSAndriy Gapon  */
186*1c05a6eaSAndriy Gapon extern int custr_alloc_buf(custr_t **, void *, size_t);
187*1c05a6eaSAndriy Gapon 
188*1c05a6eaSAndriy Gapon /*
189*1c05a6eaSAndriy Gapon  * Append a single character, or a NUL-terminated string of characters, to a
190*1c05a6eaSAndriy Gapon  * dynamic string.  Returns 0 on success and -1 otherwise.  The dynamic string
191*1c05a6eaSAndriy Gapon  * will be unmodified if the function returns -1.
192*1c05a6eaSAndriy Gapon  */
193*1c05a6eaSAndriy Gapon extern int custr_appendc(custr_t *, char);
194*1c05a6eaSAndriy Gapon extern int custr_append(custr_t *, const char *);
195*1c05a6eaSAndriy Gapon 
196*1c05a6eaSAndriy Gapon /*
197*1c05a6eaSAndriy Gapon  * Append a format string and arguments as though the contents were being parsed
198*1c05a6eaSAndriy Gapon  * through snprintf. Returns 0 on success and -1 otherwise.  The dynamic string
199*1c05a6eaSAndriy Gapon  * will be unmodified if the function returns -1.
200*1c05a6eaSAndriy Gapon  */
201*1c05a6eaSAndriy Gapon extern int custr_append_printf(custr_t *, const char *, ...);
202*1c05a6eaSAndriy Gapon extern int custr_append_vprintf(custr_t *, const char *, va_list);
203*1c05a6eaSAndriy Gapon 
204*1c05a6eaSAndriy Gapon /*
205*1c05a6eaSAndriy Gapon  * Determine the length in bytes, not including the NUL terminator, of the
206*1c05a6eaSAndriy Gapon  * dynamic string.
207*1c05a6eaSAndriy Gapon  */
208*1c05a6eaSAndriy Gapon extern size_t custr_len(custr_t *);
209*1c05a6eaSAndriy Gapon 
210*1c05a6eaSAndriy Gapon /*
211*1c05a6eaSAndriy Gapon  * Clear the contents of a dynamic string.  Does not free the underlying
212*1c05a6eaSAndriy Gapon  * memory.
213*1c05a6eaSAndriy Gapon  */
214*1c05a6eaSAndriy Gapon extern void custr_reset(custr_t *);
215*1c05a6eaSAndriy Gapon 
216*1c05a6eaSAndriy Gapon /*
217*1c05a6eaSAndriy Gapon  * Retrieve a const pointer to a NUL-terminated string version of the contents
218*1c05a6eaSAndriy Gapon  * of the dynamic string.  Storage for this string should not be freed, and
219*1c05a6eaSAndriy Gapon  * the pointer will be invalidated by any mutations to the dynamic string.
220*1c05a6eaSAndriy Gapon  */
221*1c05a6eaSAndriy Gapon extern const char *custr_cstr(custr_t *str);
222*1c05a6eaSAndriy Gapon 
223*1c05a6eaSAndriy Gapon #define	NN_DIVISOR_1000		(1U << 0)
224*1c05a6eaSAndriy Gapon 
225*1c05a6eaSAndriy Gapon /* Minimum size for the output of nicenum, including NULL */
226*1c05a6eaSAndriy Gapon #define	NN_NUMBUF_SZ		(6)
227*1c05a6eaSAndriy Gapon 
228*1c05a6eaSAndriy Gapon void nicenum(uint64_t, char *, size_t);
229*1c05a6eaSAndriy Gapon void nicenum_scale(uint64_t, size_t, char *, size_t, uint32_t);
230*1c05a6eaSAndriy Gapon 
231*1c05a6eaSAndriy Gapon #ifdef	__cplusplus
232*1c05a6eaSAndriy Gapon }
233*1c05a6eaSAndriy Gapon #endif
234*1c05a6eaSAndriy Gapon 
235*1c05a6eaSAndriy Gapon #endif /* _LIBCMDUTILS_H */
236