1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 /*
26  * Copyright (c) 2013 RackTop Systems.
27  */
28 /*
29  * Copyright 2019 Joyent, Inc.
30  */
31 
32 /*
33  * Declarations for the functions in libcmdutils.
34  */
35 
36 #ifndef	_LIBCMDUTILS_H
37 #define	_LIBCMDUTILS_H
38 
39 #if !defined(_LP64) && \
40 	!((_FILE_OFFSET_BITS == 64) || defined(_LARGEFILE64_SOURCE))
41 #error "libcmdutils.h can only be used in a largefile compilation environment"
42 #endif
43 
44 /*
45  * This is a private header file.  Applications should not directly include
46  * this file.
47  */
48 
49 #include <stdio.h>
50 #include <unistd.h>
51 #include <stdlib.h>
52 #include <stdarg.h>
53 #include <errno.h>
54 #include <fcntl.h>
55 #include <limits.h>
56 #include <libintl.h>
57 #include <string.h>
58 #include <dirent.h>
59 #include <attr.h>
60 #include <sys/avl.h>
61 #include <sys/types.h>
62 #include <sys/stat.h>
63 #include <sys/mman.h>
64 #include <libnvpair.h>
65 
66 #ifdef	__cplusplus
67 extern "C" {
68 #endif
69 
70 /* extended system attribute support */
71 #define	_NOT_SATTR	0
72 #define	_RO_SATTR	1
73 #define	_RW_SATTR	2
74 
75 #define	MAXMAPSIZE	(1024*1024*8)	/* map at most 8MB */
76 #define	SMALLFILESIZE	(32*1024)	/* don't use mmap on little file */
77 
78 /* Type used for a node containing a device id and inode number */
79 
80 #if defined(_LP64) || (_FILE_OFFSET_BITS == 64)
81 typedef struct tree_node {
82 	dev_t		node_dev;
83 	ino_t		node_ino;
84 	avl_node_t	avl_link;
85 } tree_node_t;
86 #else
87 typedef struct tree_node {
88 	dev_t		node_dev;
89 	ino64_t		node_ino;
90 	avl_node_t	avl_link;
91 } tree_node_t;
92 #endif
93 
94 		/* extended system attribute support */
95 
96 /* Determine if a file is the name of an extended system attribute file */
97 extern int sysattr_type(char *);
98 
99 /* Determine if the underlying file system supports system attributes */
100 extern int sysattr_support(char *, int);
101 
102 /* Copies the content of the source file to the target file */
103 #if defined(_LP64) || (_FILE_OFFSET_BITS == 64)
104 extern int writefile(int, int, char *, char *, char *, char *,
105 	struct stat *, struct stat *);
106 #else
107 extern int writefile(int, int, char *, char *, char *, char *,
108 	struct stat64 *, struct stat64 *);
109 #endif
110 
111 /* Gets file descriptors of the source and target attribute files */
112 extern int get_attrdirs(int, int, char *, int *, int *);
113 
114 /* Move extended attribute and extended system attribute */
115 extern int mv_xattrs(char *, char *, char *, int, int);
116 
117 /* Returns non default extended system attribute list */
118 extern nvlist_t *sysattr_list(char *, int, char *);
119 
120 
121 
122 		/* avltree */
123 
124 /*
125  * Used to compare two nodes.  We are attempting to match the 1st
126  * argument (node) against the 2nd argument (a node which
127  * is already in the search tree).
128  */
129 
130 extern int tnode_compare(const void *, const void *);
131 
132 /*
133  * Used to add a single node (containing the input device id and
134  * inode number) to the specified search tree.  The calling
135  * application must set the tree pointer to NULL before calling
136  * add_tnode() for the first time.
137  */
138 #if defined(_LP64) || (_FILE_OFFSET_BITS == 64)
139 extern int add_tnode(avl_tree_t **, dev_t, ino_t);
140 #else
141 extern int add_tnode(avl_tree_t **, dev_t, ino64_t);
142 #endif
143 
144 /*
145  * Used to destroy a whole tree (all nodes) without rebalancing.
146  * The calling application is responsible for setting the tree
147  * pointer to NULL upon return.
148  */
149 extern void destroy_tree(avl_tree_t *);
150 
151 
152 
153 		/* user/group id helpers */
154 
155 /*
156  * Used to get the next available user id in given range.
157  */
158 extern int findnextuid(uid_t, uid_t, uid_t *);
159 
160 /*
161  * Used to get the next available group id in given range.
162  */
163 extern int findnextgid(gid_t, gid_t, gid_t *);
164 
165 #define	NN_DIVISOR_1000		(1U << 0)
166 #define	NN_UNIT_SPACE		(1U << 1)
167 
168 /* Minimum size for the output of nicenum, including NULL */
169 #define	NN_NUMBUF_SZ		(6)
170 
171 void nicenum(uint64_t, char *, size_t);
172 void nicenum_scale(uint64_t, size_t, char *, size_t, uint32_t);
173 
174 #ifdef	__cplusplus
175 }
176 #endif
177 
178 #endif /* _LIBCMDUTILS_H */
179