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