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 */ 28196c7f05SJoshua M. Clulow /* 29*dcdfe824SRobert Mustacchi * Copyright 2016 Joyent, Inc. 30196c7f05SJoshua 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> 47*dcdfe824SRobert Mustacchi #include <stdarg.h> 48da6c28aaSamw #include <errno.h> 49da6c28aaSamw #include <fcntl.h> 50da6c28aaSamw #include <limits.h> 51da6c28aaSamw #include <libintl.h> 52da6c28aaSamw #include <string.h> 53da6c28aaSamw #include <dirent.h> 54da6c28aaSamw #include <attr.h> 557c478bd9Sstevel@tonic-gate #include <sys/avl.h> 567c478bd9Sstevel@tonic-gate #include <sys/types.h> 57da6c28aaSamw #include <sys/stat.h> 58da6c28aaSamw #include <sys/mman.h> 59da6c28aaSamw #include <libnvpair.h> 607c478bd9Sstevel@tonic-gate 617c478bd9Sstevel@tonic-gate #ifdef __cplusplus 627c478bd9Sstevel@tonic-gate extern "C" { 637c478bd9Sstevel@tonic-gate #endif 647c478bd9Sstevel@tonic-gate 65da6c28aaSamw /* extended system attribute support */ 66da6c28aaSamw #define _NOT_SATTR 0 67da6c28aaSamw #define _RO_SATTR 1 68da6c28aaSamw #define _RW_SATTR 2 69da6c28aaSamw 70da6c28aaSamw #define MAXMAPSIZE (1024*1024*8) /* map at most 8MB */ 71da6c28aaSamw #define SMALLFILESIZE (32*1024) /* don't use mmap on little file */ 72da6c28aaSamw 737c478bd9Sstevel@tonic-gate /* avltree */ 747c478bd9Sstevel@tonic-gate #define OFFSETOF(s, m) ((size_t)(&(((s *)0)->m))) 757c478bd9Sstevel@tonic-gate 767c478bd9Sstevel@tonic-gate /* Type used for a node containing a device id and inode number */ 777c478bd9Sstevel@tonic-gate typedef struct tree_node { 787c478bd9Sstevel@tonic-gate dev_t node_dev; 797c478bd9Sstevel@tonic-gate ino_t node_ino; 807c478bd9Sstevel@tonic-gate avl_node_t avl_link; 817c478bd9Sstevel@tonic-gate } tree_node_t; 827c478bd9Sstevel@tonic-gate 83da6c28aaSamw 84da6c28aaSamw /* extended system attribute support */ 85da6c28aaSamw 86da6c28aaSamw /* Determine if a file is the name of an extended system attribute file */ 87da6c28aaSamw extern int sysattr_type(char *); 88da6c28aaSamw 89da6c28aaSamw /* Determine if the underlying file system supports system attributes */ 90da6c28aaSamw extern int sysattr_support(char *, int); 91da6c28aaSamw 92da6c28aaSamw /* Copies the content of the source file to the target file */ 93da6c28aaSamw extern int writefile(int, int, char *, char *, char *, char *, 94da6c28aaSamw struct stat *, struct stat *); 95da6c28aaSamw 96da6c28aaSamw /* Gets file descriptors of the source and target attribute files */ 97da6c28aaSamw extern int get_attrdirs(int, int, char *, int *, int *); 98da6c28aaSamw 99da6c28aaSamw /* Move extended attribute and extended system attribute */ 100da6c28aaSamw extern int mv_xattrs(char *, char *, char *, int, int); 101da6c28aaSamw 102da6c28aaSamw /* Returns non default extended system attribute list */ 103da6c28aaSamw extern nvlist_t *sysattr_list(char *, int, char *); 104da6c28aaSamw 105da6c28aaSamw 106da6c28aaSamw 107da6c28aaSamw /* avltree */ 108da6c28aaSamw 1097c478bd9Sstevel@tonic-gate /* 1107c478bd9Sstevel@tonic-gate * Used to compare two nodes. We are attempting to match the 1st 1117c478bd9Sstevel@tonic-gate * argument (node) against the 2nd argument (a node which 1127c478bd9Sstevel@tonic-gate * is already in the search tree). 1137c478bd9Sstevel@tonic-gate */ 114da6c28aaSamw 1157c478bd9Sstevel@tonic-gate extern int tnode_compare(const void *, const void *); 1167c478bd9Sstevel@tonic-gate 1177c478bd9Sstevel@tonic-gate /* 1187c478bd9Sstevel@tonic-gate * Used to add a single node (containing the input device id and 1197c478bd9Sstevel@tonic-gate * inode number) to the specified search tree. The calling 1207c478bd9Sstevel@tonic-gate * application must set the tree pointer to NULL before calling 1217c478bd9Sstevel@tonic-gate * add_tnode() for the first time. 1227c478bd9Sstevel@tonic-gate */ 1237c478bd9Sstevel@tonic-gate extern int add_tnode(avl_tree_t **, dev_t, ino_t); 1247c478bd9Sstevel@tonic-gate 1257c478bd9Sstevel@tonic-gate /* 1267c478bd9Sstevel@tonic-gate * Used to destroy a whole tree (all nodes) without rebalancing. 1277c478bd9Sstevel@tonic-gate * The calling application is responsible for setting the tree 1287c478bd9Sstevel@tonic-gate * pointer to NULL upon return. 1297c478bd9Sstevel@tonic-gate */ 1307c478bd9Sstevel@tonic-gate extern void destroy_tree(avl_tree_t *); 1317c478bd9Sstevel@tonic-gate 132a7fe1d5bSAndy Stormont 133a7fe1d5bSAndy Stormont 134a7fe1d5bSAndy Stormont /* user/group id helpers */ 135a7fe1d5bSAndy Stormont 136a7fe1d5bSAndy Stormont /* 137a7fe1d5bSAndy Stormont * Used to get the next available user id in given range. 138a7fe1d5bSAndy Stormont */ 139a7fe1d5bSAndy Stormont extern int findnextuid(uid_t, uid_t, uid_t *); 140a7fe1d5bSAndy Stormont 141a7fe1d5bSAndy Stormont /* 142a7fe1d5bSAndy Stormont * Used to get the next available group id in given range. 143a7fe1d5bSAndy Stormont */ 144a7fe1d5bSAndy Stormont extern int findnextgid(gid_t, gid_t, gid_t *); 145a7fe1d5bSAndy Stormont 146196c7f05SJoshua M. Clulow 147196c7f05SJoshua M. Clulow 148196c7f05SJoshua M. Clulow /* dynamic string utilities */ 149196c7f05SJoshua M. Clulow 150196c7f05SJoshua M. Clulow typedef struct custr custr_t; 151196c7f05SJoshua M. Clulow 152196c7f05SJoshua M. Clulow /* 153196c7f05SJoshua M. Clulow * Allocate and free a "custr_t" dynamic string object. Returns 0 on success 154196c7f05SJoshua M. Clulow * and -1 otherwise. 155196c7f05SJoshua M. Clulow */ 156196c7f05SJoshua M. Clulow extern int custr_alloc(custr_t **); 157196c7f05SJoshua M. Clulow extern void custr_free(custr_t *); 158196c7f05SJoshua M. Clulow 159196c7f05SJoshua M. Clulow /* 160*dcdfe824SRobert Mustacchi * Allocate a "custr_t" dynamic string object that operates on a fixed external 161*dcdfe824SRobert Mustacchi * buffer. 162*dcdfe824SRobert Mustacchi */ 163*dcdfe824SRobert Mustacchi extern int custr_alloc_buf(custr_t **, void *, size_t); 164*dcdfe824SRobert Mustacchi 165*dcdfe824SRobert Mustacchi /* 166196c7f05SJoshua M. Clulow * Append a single character, or a NUL-terminated string of characters, to a 167196c7f05SJoshua M. Clulow * dynamic string. Returns 0 on success and -1 otherwise. The dynamic string 168196c7f05SJoshua M. Clulow * will be unmodified if the function returns -1. 169196c7f05SJoshua M. Clulow */ 170196c7f05SJoshua M. Clulow extern int custr_appendc(custr_t *, char); 171196c7f05SJoshua M. Clulow extern int custr_append(custr_t *, const char *); 172196c7f05SJoshua M. Clulow 173196c7f05SJoshua M. Clulow /* 174*dcdfe824SRobert Mustacchi * Append a format string and arguments as though the contents were being parsed 175*dcdfe824SRobert Mustacchi * through snprintf. Returns 0 on success and -1 otherwise. The dynamic string 176*dcdfe824SRobert Mustacchi * will be unmodified if the function returns -1. 177*dcdfe824SRobert Mustacchi */ 178*dcdfe824SRobert Mustacchi extern int custr_append_printf(custr_t *, const char *, ...); 179*dcdfe824SRobert Mustacchi extern int custr_append_vprintf(custr_t *, const char *, va_list); 180*dcdfe824SRobert Mustacchi 181*dcdfe824SRobert Mustacchi /* 182196c7f05SJoshua M. Clulow * Determine the length in bytes, not including the NUL terminator, of the 183196c7f05SJoshua M. Clulow * dynamic string. 184196c7f05SJoshua M. Clulow */ 185196c7f05SJoshua M. Clulow extern size_t custr_len(custr_t *); 186196c7f05SJoshua M. Clulow 187196c7f05SJoshua M. Clulow /* 188196c7f05SJoshua M. Clulow * Clear the contents of a dynamic string. Does not free the underlying 189196c7f05SJoshua M. Clulow * memory. 190196c7f05SJoshua M. Clulow */ 191196c7f05SJoshua M. Clulow extern void custr_reset(custr_t *); 192196c7f05SJoshua M. Clulow 193196c7f05SJoshua M. Clulow /* 194196c7f05SJoshua M. Clulow * Retrieve a const pointer to a NUL-terminated string version of the contents 195196c7f05SJoshua M. Clulow * of the dynamic string. Storage for this string should not be freed, and 196196c7f05SJoshua M. Clulow * the pointer will be invalidated by any mutations to the dynamic string. 197196c7f05SJoshua M. Clulow */ 198196c7f05SJoshua M. Clulow extern const char *custr_cstr(custr_t *str); 199196c7f05SJoshua M. Clulow 2007c478bd9Sstevel@tonic-gate #ifdef __cplusplus 2017c478bd9Sstevel@tonic-gate } 2027c478bd9Sstevel@tonic-gate #endif 2037c478bd9Sstevel@tonic-gate 2047c478bd9Sstevel@tonic-gate #endif /* _LIBCMDUTILS_H */ 205