xref: /titanic_41/usr/src/lib/libcmdutils/libcmdutils.h (revision 6c9e17859f07daef4c2a848835bd029c9798c9e3)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5da6c28aaSamw  * Common Development and Distribution License (the "License").
6da6c28aaSamw  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22da6c28aaSamw  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
25a7fe1d5bSAndy Stormont /*
26a7fe1d5bSAndy Stormont  * Copyright (c) 2013 RackTop Systems.
27a7fe1d5bSAndy Stormont  */
28*61d1e6c5SJoshua M. Clulow /*
29*61d1e6c5SJoshua M. Clulow  * Copyright 2014 Joyent, Inc.
30*61d1e6c5SJoshua M. Clulow  */
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate /*
337c478bd9Sstevel@tonic-gate  * Declarations for the functions in libcmdutils.
347c478bd9Sstevel@tonic-gate  */
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #ifndef	_LIBCMDUTILS_H
377c478bd9Sstevel@tonic-gate #define	_LIBCMDUTILS_H
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate /*
407c478bd9Sstevel@tonic-gate  * This is a private header file.  Applications should not directly include
417c478bd9Sstevel@tonic-gate  * this file.
427c478bd9Sstevel@tonic-gate  */
437c478bd9Sstevel@tonic-gate 
44da6c28aaSamw #include <stdio.h>
45da6c28aaSamw #include <unistd.h>
46da6c28aaSamw #include <stdlib.h>
47da6c28aaSamw #include <errno.h>
48da6c28aaSamw #include <fcntl.h>
49da6c28aaSamw #include <limits.h>
50da6c28aaSamw #include <libintl.h>
51da6c28aaSamw #include <string.h>
52da6c28aaSamw #include <dirent.h>
53da6c28aaSamw #include <attr.h>
547c478bd9Sstevel@tonic-gate #include <sys/avl.h>
557c478bd9Sstevel@tonic-gate #include <sys/types.h>
56da6c28aaSamw #include <sys/stat.h>
57da6c28aaSamw #include <sys/mman.h>
58da6c28aaSamw #include <libnvpair.h>
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
617c478bd9Sstevel@tonic-gate extern "C" {
627c478bd9Sstevel@tonic-gate #endif
637c478bd9Sstevel@tonic-gate 
64da6c28aaSamw /* extended system attribute support */
65da6c28aaSamw #define	_NOT_SATTR	0
66da6c28aaSamw #define	_RO_SATTR	1
67da6c28aaSamw #define	_RW_SATTR	2
68da6c28aaSamw 
69da6c28aaSamw #define	MAXMAPSIZE	(1024*1024*8)	/* map at most 8MB */
70da6c28aaSamw #define	SMALLFILESIZE	(32*1024)	/* don't use mmap on little file */
71da6c28aaSamw 
727c478bd9Sstevel@tonic-gate /* avltree */
737c478bd9Sstevel@tonic-gate #define	OFFSETOF(s, m)	((size_t)(&(((s *)0)->m)))
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate /* Type used for a node containing a device id and inode number */
767c478bd9Sstevel@tonic-gate typedef struct tree_node {
777c478bd9Sstevel@tonic-gate 	dev_t		node_dev;
787c478bd9Sstevel@tonic-gate 	ino_t		node_ino;
797c478bd9Sstevel@tonic-gate 	avl_node_t	avl_link;
807c478bd9Sstevel@tonic-gate } tree_node_t;
817c478bd9Sstevel@tonic-gate 
82da6c28aaSamw 
83da6c28aaSamw 		/* extended system attribute support */
84da6c28aaSamw 
85da6c28aaSamw /* Determine if a file is the name of an extended system attribute file */
86da6c28aaSamw extern int sysattr_type(char *);
87da6c28aaSamw 
88da6c28aaSamw /* Determine if the underlying file system supports system attributes */
89da6c28aaSamw extern int sysattr_support(char *, int);
90da6c28aaSamw 
91da6c28aaSamw /* Copies the content of the source file to the target file */
92da6c28aaSamw extern int writefile(int, int, char *, char *, char *, char *,
93da6c28aaSamw struct stat *, struct stat *);
94da6c28aaSamw 
95da6c28aaSamw /* Gets file descriptors of the source and target attribute files */
96da6c28aaSamw extern int get_attrdirs(int, int, char *, int *, int *);
97da6c28aaSamw 
98da6c28aaSamw /* Move extended attribute and extended system attribute */
99da6c28aaSamw extern int mv_xattrs(char *, char *, char *, int, int);
100da6c28aaSamw 
101da6c28aaSamw /* Returns non default extended system attribute list */
102da6c28aaSamw extern nvlist_t *sysattr_list(char *, int, char *);
103da6c28aaSamw 
104da6c28aaSamw 
105da6c28aaSamw 
106da6c28aaSamw 		/* avltree */
107da6c28aaSamw 
1087c478bd9Sstevel@tonic-gate /*
1097c478bd9Sstevel@tonic-gate  * Used to compare two nodes.  We are attempting to match the 1st
1107c478bd9Sstevel@tonic-gate  * argument (node) against the 2nd argument (a node which
1117c478bd9Sstevel@tonic-gate  * is already in the search tree).
1127c478bd9Sstevel@tonic-gate  */
113da6c28aaSamw 
1147c478bd9Sstevel@tonic-gate extern int tnode_compare(const void *, const void *);
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate /*
1177c478bd9Sstevel@tonic-gate  * Used to add a single node (containing the input device id and
1187c478bd9Sstevel@tonic-gate  * inode number) to the specified search tree.  The calling
1197c478bd9Sstevel@tonic-gate  * application must set the tree pointer to NULL before calling
1207c478bd9Sstevel@tonic-gate  * add_tnode() for the first time.
1217c478bd9Sstevel@tonic-gate  */
1227c478bd9Sstevel@tonic-gate extern int add_tnode(avl_tree_t **, dev_t, ino_t);
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate /*
1257c478bd9Sstevel@tonic-gate  * Used to destroy a whole tree (all nodes) without rebalancing.
1267c478bd9Sstevel@tonic-gate  * The calling application is responsible for setting the tree
1277c478bd9Sstevel@tonic-gate  * pointer to NULL upon return.
1287c478bd9Sstevel@tonic-gate  */
1297c478bd9Sstevel@tonic-gate extern void destroy_tree(avl_tree_t *);
1307c478bd9Sstevel@tonic-gate 
131a7fe1d5bSAndy Stormont 
132a7fe1d5bSAndy Stormont 
133a7fe1d5bSAndy Stormont 		/* user/group id helpers */
134a7fe1d5bSAndy Stormont 
135a7fe1d5bSAndy Stormont /*
136a7fe1d5bSAndy Stormont  * Used to get the next available user id in given range.
137a7fe1d5bSAndy Stormont  */
138a7fe1d5bSAndy Stormont extern int findnextuid(uid_t, uid_t, uid_t *);
139a7fe1d5bSAndy Stormont 
140a7fe1d5bSAndy Stormont /*
141a7fe1d5bSAndy Stormont  * Used to get the next available group id in given range.
142a7fe1d5bSAndy Stormont  */
143a7fe1d5bSAndy Stormont extern int findnextgid(gid_t, gid_t, gid_t *);
144a7fe1d5bSAndy Stormont 
145*61d1e6c5SJoshua M. Clulow 
146*61d1e6c5SJoshua M. Clulow 
147*61d1e6c5SJoshua M. Clulow 		/* dynamic string utilities */
148*61d1e6c5SJoshua M. Clulow 
149*61d1e6c5SJoshua M. Clulow typedef struct custr custr_t;
150*61d1e6c5SJoshua M. Clulow 
151*61d1e6c5SJoshua M. Clulow /*
152*61d1e6c5SJoshua M. Clulow  * Allocate and free a "custr_t" dynamic string object.  Returns 0 on success
153*61d1e6c5SJoshua M. Clulow  * and -1 otherwise.
154*61d1e6c5SJoshua M. Clulow  */
155*61d1e6c5SJoshua M. Clulow extern int custr_alloc(custr_t **);
156*61d1e6c5SJoshua M. Clulow extern void custr_free(custr_t *);
157*61d1e6c5SJoshua M. Clulow 
158*61d1e6c5SJoshua M. Clulow /*
159*61d1e6c5SJoshua M. Clulow  * Append a single character, or a NUL-terminated string of characters, to a
160*61d1e6c5SJoshua M. Clulow  * dynamic string.  Returns 0 on success and -1 otherwise.  The dynamic string
161*61d1e6c5SJoshua M. Clulow  * will be unmodified if the function returns -1.
162*61d1e6c5SJoshua M. Clulow  */
163*61d1e6c5SJoshua M. Clulow extern int custr_appendc(custr_t *, char);
164*61d1e6c5SJoshua M. Clulow extern int custr_append(custr_t *, const char *);
165*61d1e6c5SJoshua M. Clulow 
166*61d1e6c5SJoshua M. Clulow /*
167*61d1e6c5SJoshua M. Clulow  * Determine the length in bytes, not including the NUL terminator, of the
168*61d1e6c5SJoshua M. Clulow  * dynamic string.
169*61d1e6c5SJoshua M. Clulow  */
170*61d1e6c5SJoshua M. Clulow extern size_t custr_len(custr_t *);
171*61d1e6c5SJoshua M. Clulow 
172*61d1e6c5SJoshua M. Clulow /*
173*61d1e6c5SJoshua M. Clulow  * Clear the contents of a dynamic string.  Does not free the underlying
174*61d1e6c5SJoshua M. Clulow  * memory.
175*61d1e6c5SJoshua M. Clulow  */
176*61d1e6c5SJoshua M. Clulow extern void custr_reset(custr_t *);
177*61d1e6c5SJoshua M. Clulow 
178*61d1e6c5SJoshua M. Clulow /*
179*61d1e6c5SJoshua M. Clulow  * Retrieve a const pointer to a NUL-terminated string version of the contents
180*61d1e6c5SJoshua M. Clulow  * of the dynamic string.  Storage for this string should not be freed, and
181*61d1e6c5SJoshua M. Clulow  * the pointer will be invalidated by any mutations to the dynamic string.
182*61d1e6c5SJoshua M. Clulow  */
183*61d1e6c5SJoshua M. Clulow extern const char *custr_cstr(custr_t *str);
184*61d1e6c5SJoshua M. Clulow 
1857c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
1867c478bd9Sstevel@tonic-gate }
1877c478bd9Sstevel@tonic-gate #endif
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate #endif /* _LIBCMDUTILS_H */
190