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*0ba00e7aSJason King * Copyright 2017 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 39*0ba00e7aSJason King #if !defined(_LP64) && \ 40*0ba00e7aSJason King !((_FILE_OFFSET_BITS == 64) || defined(_LARGEFILE64_SOURCE)) 41*0ba00e7aSJason King #error "libcmdutils.h can only be used in a largefile compilation environment" 42*0ba00e7aSJason King #endif 43*0ba00e7aSJason King 447c478bd9Sstevel@tonic-gate /* 457c478bd9Sstevel@tonic-gate * This is a private header file. Applications should not directly include 467c478bd9Sstevel@tonic-gate * this file. 477c478bd9Sstevel@tonic-gate */ 487c478bd9Sstevel@tonic-gate 49da6c28aaSamw #include <stdio.h> 50da6c28aaSamw #include <unistd.h> 51da6c28aaSamw #include <stdlib.h> 52dcdfe824SRobert Mustacchi #include <stdarg.h> 53da6c28aaSamw #include <errno.h> 54da6c28aaSamw #include <fcntl.h> 55da6c28aaSamw #include <limits.h> 56da6c28aaSamw #include <libintl.h> 57da6c28aaSamw #include <string.h> 58da6c28aaSamw #include <dirent.h> 59da6c28aaSamw #include <attr.h> 607c478bd9Sstevel@tonic-gate #include <sys/avl.h> 617c478bd9Sstevel@tonic-gate #include <sys/types.h> 62da6c28aaSamw #include <sys/stat.h> 63da6c28aaSamw #include <sys/mman.h> 64da6c28aaSamw #include <libnvpair.h> 657c478bd9Sstevel@tonic-gate 667c478bd9Sstevel@tonic-gate #ifdef __cplusplus 677c478bd9Sstevel@tonic-gate extern "C" { 687c478bd9Sstevel@tonic-gate #endif 697c478bd9Sstevel@tonic-gate 70da6c28aaSamw /* extended system attribute support */ 71da6c28aaSamw #define _NOT_SATTR 0 72da6c28aaSamw #define _RO_SATTR 1 73da6c28aaSamw #define _RW_SATTR 2 74da6c28aaSamw 75da6c28aaSamw #define MAXMAPSIZE (1024*1024*8) /* map at most 8MB */ 76da6c28aaSamw #define SMALLFILESIZE (32*1024) /* don't use mmap on little file */ 77da6c28aaSamw 787c478bd9Sstevel@tonic-gate /* Type used for a node containing a device id and inode number */ 79*0ba00e7aSJason King 80*0ba00e7aSJason King #if defined(_LP64) || (_FILE_OFFSET_BITS == 64) 817c478bd9Sstevel@tonic-gate typedef struct tree_node { 827c478bd9Sstevel@tonic-gate dev_t node_dev; 837c478bd9Sstevel@tonic-gate ino_t node_ino; 847c478bd9Sstevel@tonic-gate avl_node_t avl_link; 857c478bd9Sstevel@tonic-gate } tree_node_t; 86*0ba00e7aSJason King #else 87*0ba00e7aSJason King typedef struct tree_node { 88*0ba00e7aSJason King dev_t node_dev; 89*0ba00e7aSJason King ino64_t node_ino; 90*0ba00e7aSJason King avl_node_t avl_link; 91*0ba00e7aSJason King } tree_node_t; 92*0ba00e7aSJason King #endif 93da6c28aaSamw 94da6c28aaSamw /* extended system attribute support */ 95da6c28aaSamw 96da6c28aaSamw /* Determine if a file is the name of an extended system attribute file */ 97da6c28aaSamw extern int sysattr_type(char *); 98da6c28aaSamw 99da6c28aaSamw /* Determine if the underlying file system supports system attributes */ 100da6c28aaSamw extern int sysattr_support(char *, int); 101da6c28aaSamw 102da6c28aaSamw /* Copies the content of the source file to the target file */ 103*0ba00e7aSJason King #if defined(_LP64) || (_FILE_OFFSET_BITS == 64) 104da6c28aaSamw extern int writefile(int, int, char *, char *, char *, char *, 105da6c28aaSamw struct stat *, struct stat *); 106*0ba00e7aSJason King #else 107*0ba00e7aSJason King extern int writefile(int, int, char *, char *, char *, char *, 108*0ba00e7aSJason King struct stat64 *, struct stat64 *); 109*0ba00e7aSJason King #endif 110da6c28aaSamw 111da6c28aaSamw /* Gets file descriptors of the source and target attribute files */ 112da6c28aaSamw extern int get_attrdirs(int, int, char *, int *, int *); 113da6c28aaSamw 114da6c28aaSamw /* Move extended attribute and extended system attribute */ 115da6c28aaSamw extern int mv_xattrs(char *, char *, char *, int, int); 116da6c28aaSamw 117da6c28aaSamw /* Returns non default extended system attribute list */ 118da6c28aaSamw extern nvlist_t *sysattr_list(char *, int, char *); 119da6c28aaSamw 120da6c28aaSamw 121da6c28aaSamw 122da6c28aaSamw /* avltree */ 123da6c28aaSamw 1247c478bd9Sstevel@tonic-gate /* 1257c478bd9Sstevel@tonic-gate * Used to compare two nodes. We are attempting to match the 1st 1267c478bd9Sstevel@tonic-gate * argument (node) against the 2nd argument (a node which 1277c478bd9Sstevel@tonic-gate * is already in the search tree). 1287c478bd9Sstevel@tonic-gate */ 129da6c28aaSamw 1307c478bd9Sstevel@tonic-gate extern int tnode_compare(const void *, const void *); 1317c478bd9Sstevel@tonic-gate 1327c478bd9Sstevel@tonic-gate /* 1337c478bd9Sstevel@tonic-gate * Used to add a single node (containing the input device id and 1347c478bd9Sstevel@tonic-gate * inode number) to the specified search tree. The calling 1357c478bd9Sstevel@tonic-gate * application must set the tree pointer to NULL before calling 1367c478bd9Sstevel@tonic-gate * add_tnode() for the first time. 1377c478bd9Sstevel@tonic-gate */ 138*0ba00e7aSJason King #if defined(_LP64) || (_FILE_OFFSET_BITS == 64) 1397c478bd9Sstevel@tonic-gate extern int add_tnode(avl_tree_t **, dev_t, ino_t); 140*0ba00e7aSJason King #else 141*0ba00e7aSJason King extern int add_tnode(avl_tree_t **, dev_t, ino64_t); 142*0ba00e7aSJason King #endif 1437c478bd9Sstevel@tonic-gate 1447c478bd9Sstevel@tonic-gate /* 1457c478bd9Sstevel@tonic-gate * Used to destroy a whole tree (all nodes) without rebalancing. 1467c478bd9Sstevel@tonic-gate * The calling application is responsible for setting the tree 1477c478bd9Sstevel@tonic-gate * pointer to NULL upon return. 1487c478bd9Sstevel@tonic-gate */ 1497c478bd9Sstevel@tonic-gate extern void destroy_tree(avl_tree_t *); 1507c478bd9Sstevel@tonic-gate 151a7fe1d5bSAndy Stormont 152a7fe1d5bSAndy Stormont 153a7fe1d5bSAndy Stormont /* user/group id helpers */ 154a7fe1d5bSAndy Stormont 155a7fe1d5bSAndy Stormont /* 156a7fe1d5bSAndy Stormont * Used to get the next available user id in given range. 157a7fe1d5bSAndy Stormont */ 158a7fe1d5bSAndy Stormont extern int findnextuid(uid_t, uid_t, uid_t *); 159a7fe1d5bSAndy Stormont 160a7fe1d5bSAndy Stormont /* 161a7fe1d5bSAndy Stormont * Used to get the next available group id in given range. 162a7fe1d5bSAndy Stormont */ 163a7fe1d5bSAndy Stormont extern int findnextgid(gid_t, gid_t, gid_t *); 164a7fe1d5bSAndy Stormont 165196c7f05SJoshua M. Clulow 166196c7f05SJoshua M. Clulow 167196c7f05SJoshua M. Clulow /* dynamic string utilities */ 168196c7f05SJoshua M. Clulow 169196c7f05SJoshua M. Clulow typedef struct custr custr_t; 170196c7f05SJoshua M. Clulow 171196c7f05SJoshua M. Clulow /* 172196c7f05SJoshua M. Clulow * Allocate and free a "custr_t" dynamic string object. Returns 0 on success 173196c7f05SJoshua M. Clulow * and -1 otherwise. 174196c7f05SJoshua M. Clulow */ 175196c7f05SJoshua M. Clulow extern int custr_alloc(custr_t **); 176196c7f05SJoshua M. Clulow extern void custr_free(custr_t *); 177196c7f05SJoshua M. Clulow 178196c7f05SJoshua M. Clulow /* 179dcdfe824SRobert Mustacchi * Allocate a "custr_t" dynamic string object that operates on a fixed external 180dcdfe824SRobert Mustacchi * buffer. 181dcdfe824SRobert Mustacchi */ 182dcdfe824SRobert Mustacchi extern int custr_alloc_buf(custr_t **, void *, size_t); 183dcdfe824SRobert Mustacchi 184dcdfe824SRobert Mustacchi /* 185196c7f05SJoshua M. Clulow * Append a single character, or a NUL-terminated string of characters, to a 186196c7f05SJoshua M. Clulow * dynamic string. Returns 0 on success and -1 otherwise. The dynamic string 187196c7f05SJoshua M. Clulow * will be unmodified if the function returns -1. 188196c7f05SJoshua M. Clulow */ 189196c7f05SJoshua M. Clulow extern int custr_appendc(custr_t *, char); 190196c7f05SJoshua M. Clulow extern int custr_append(custr_t *, const char *); 191196c7f05SJoshua M. Clulow 192196c7f05SJoshua M. Clulow /* 193dcdfe824SRobert Mustacchi * Append a format string and arguments as though the contents were being parsed 194dcdfe824SRobert Mustacchi * through snprintf. Returns 0 on success and -1 otherwise. The dynamic string 195dcdfe824SRobert Mustacchi * will be unmodified if the function returns -1. 196dcdfe824SRobert Mustacchi */ 197dcdfe824SRobert Mustacchi extern int custr_append_printf(custr_t *, const char *, ...); 198dcdfe824SRobert Mustacchi extern int custr_append_vprintf(custr_t *, const char *, va_list); 199dcdfe824SRobert Mustacchi 200dcdfe824SRobert Mustacchi /* 201196c7f05SJoshua M. Clulow * Determine the length in bytes, not including the NUL terminator, of the 202196c7f05SJoshua M. Clulow * dynamic string. 203196c7f05SJoshua M. Clulow */ 204196c7f05SJoshua M. Clulow extern size_t custr_len(custr_t *); 205196c7f05SJoshua M. Clulow 206196c7f05SJoshua M. Clulow /* 207196c7f05SJoshua M. Clulow * Clear the contents of a dynamic string. Does not free the underlying 208196c7f05SJoshua M. Clulow * memory. 209196c7f05SJoshua M. Clulow */ 210196c7f05SJoshua M. Clulow extern void custr_reset(custr_t *); 211196c7f05SJoshua M. Clulow 212196c7f05SJoshua M. Clulow /* 213196c7f05SJoshua M. Clulow * Retrieve a const pointer to a NUL-terminated string version of the contents 214196c7f05SJoshua M. Clulow * of the dynamic string. Storage for this string should not be freed, and 215196c7f05SJoshua M. Clulow * the pointer will be invalidated by any mutations to the dynamic string. 216196c7f05SJoshua M. Clulow */ 217196c7f05SJoshua M. Clulow extern const char *custr_cstr(custr_t *str); 218196c7f05SJoshua M. Clulow 2197c478bd9Sstevel@tonic-gate #ifdef __cplusplus 2207c478bd9Sstevel@tonic-gate } 2217c478bd9Sstevel@tonic-gate #endif 2227c478bd9Sstevel@tonic-gate 2237c478bd9Sstevel@tonic-gate #endif /* _LIBCMDUTILS_H */ 224