Path: blob/main/cddl/contrib/opensolaris/lib/libcmdutils/libcmdutils.h
39507 views
/*1* CDDL HEADER START2*3* The contents of this file are subject to the terms of the4* Common Development and Distribution License (the "License").5* You may not use this file except in compliance with the License.6*7* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE8* or http://www.opensolaris.org/os/licensing.9* See the License for the specific language governing permissions10* and limitations under the License.11*12* When distributing Covered Code, include this CDDL HEADER in each13* file and include the License file at usr/src/OPENSOLARIS.LICENSE.14* If applicable, add the following below this CDDL HEADER, with the15* fields enclosed by brackets "[]" replaced with your own identifying16* information: Portions Copyright [yyyy] [name of copyright owner]17*18* CDDL HEADER END19*/20/*21* Copyright 2007 Sun Microsystems, Inc. All rights reserved.22* Use is subject to license terms.23*/24/*25* Copyright (c) 2013 RackTop Systems.26*/27/*28* Copyright 2017 Joyent, Inc.29*/3031/*32* Declarations for the functions in libcmdutils.33*/3435#ifndef _LIBCMDUTILS_H36#define _LIBCMDUTILS_H3738#ifdef illumos39#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#endif43#endif4445/*46* This is a private header file. Applications should not directly include47* this file.48*/4950#include <stdio.h>51#include <unistd.h>52#include <stdlib.h>53#include <stdarg.h>54#include <errno.h>55#include <fcntl.h>56#include <limits.h>57#include <libintl.h>58#include <string.h>59#include <dirent.h>60#ifdef illumos61#include <attr.h>62#endif63#include <sys/avl.h>64#include <sys/types.h>65#include <sys/stat.h>66#include <sys/mman.h>67#include <libnvpair.h>6869#ifdef __cplusplus70extern "C" {71#endif7273/* extended system attribute support */74#define _NOT_SATTR 075#define _RO_SATTR 176#define _RW_SATTR 27778#define MAXMAPSIZE (1024*1024*8) /* map at most 8MB */79#define SMALLFILESIZE (32*1024) /* don't use mmap on little file */8081/* Type used for a node containing a device id and inode number */8283#if defined(_LP64) || (_FILE_OFFSET_BITS == 64)84typedef struct tree_node {85dev_t node_dev;86ino_t node_ino;87avl_node_t avl_link;88} tree_node_t;89#else90typedef struct tree_node {91dev_t node_dev;92ino64_t node_ino;93avl_node_t avl_link;94} tree_node_t;95#endif9697/* extended system attribute support */9899/* Determine if a file is the name of an extended system attribute file */100extern int sysattr_type(char *);101102/* Determine if the underlying file system supports system attributes */103extern int sysattr_support(char *, int);104105/* Copies the content of the source file to the target file */106#if defined(_LP64) || (_FILE_OFFSET_BITS == 64)107extern int writefile(int, int, char *, char *, char *, char *,108struct stat *, struct stat *);109#else110extern int writefile(int, int, char *, char *, char *, char *,111struct stat64 *, struct stat64 *);112#endif113114/* Gets file descriptors of the source and target attribute files */115extern int get_attrdirs(int, int, char *, int *, int *);116117/* Move extended attribute and extended system attribute */118extern int mv_xattrs(char *, char *, char *, int, int);119120/* Returns non default extended system attribute list */121extern nvlist_t *sysattr_list(char *, int, char *);122123124125/* avltree */126127/*128* Used to compare two nodes. We are attempting to match the 1st129* argument (node) against the 2nd argument (a node which130* is already in the search tree).131*/132133extern int tnode_compare(const void *, const void *);134135/*136* Used to add a single node (containing the input device id and137* inode number) to the specified search tree. The calling138* application must set the tree pointer to NULL before calling139* add_tnode() for the first time.140*/141#if defined(_LP64) || (_FILE_OFFSET_BITS == 64)142extern int add_tnode(avl_tree_t **, dev_t, ino_t);143#else144extern int add_tnode(avl_tree_t **, dev_t, ino64_t);145#endif146147/*148* Used to destroy a whole tree (all nodes) without rebalancing.149* The calling application is responsible for setting the tree150* pointer to NULL upon return.151*/152extern void destroy_tree(avl_tree_t *);153154155156/* user/group id helpers */157158/*159* Used to get the next available user id in given range.160*/161extern int findnextuid(uid_t, uid_t, uid_t *);162163/*164* Used to get the next available group id in given range.165*/166extern int findnextgid(gid_t, gid_t, gid_t *);167168169170/* dynamic string utilities */171172typedef struct custr custr_t;173174/*175* Allocate and free a "custr_t" dynamic string object. Returns 0 on success176* and -1 otherwise.177*/178extern int custr_alloc(custr_t **);179extern void custr_free(custr_t *);180181/*182* Allocate a "custr_t" dynamic string object that operates on a fixed external183* buffer.184*/185extern int custr_alloc_buf(custr_t **, void *, size_t);186187/*188* Append a single character, or a NUL-terminated string of characters, to a189* dynamic string. Returns 0 on success and -1 otherwise. The dynamic string190* will be unmodified if the function returns -1.191*/192extern int custr_appendc(custr_t *, char);193extern int custr_append(custr_t *, const char *);194195/*196* Append a format string and arguments as though the contents were being parsed197* through snprintf. Returns 0 on success and -1 otherwise. The dynamic string198* will be unmodified if the function returns -1.199*/200extern int custr_append_printf(custr_t *, const char *, ...);201extern int custr_append_vprintf(custr_t *, const char *, va_list);202203/*204* Determine the length in bytes, not including the NUL terminator, of the205* dynamic string.206*/207extern size_t custr_len(custr_t *);208209/*210* Clear the contents of a dynamic string. Does not free the underlying211* memory.212*/213extern void custr_reset(custr_t *);214215/*216* Retrieve a const pointer to a NUL-terminated string version of the contents217* of the dynamic string. Storage for this string should not be freed, and218* the pointer will be invalidated by any mutations to the dynamic string.219*/220extern const char *custr_cstr(custr_t *str);221222#define NN_DIVISOR_1000 (1U << 0)223224/* Minimum size for the output of nicenum, including NULL */225#define NN_NUMBUF_SZ (6)226227void nicenum(uint64_t, char *, size_t);228void nicenum_scale(uint64_t, size_t, char *, size_t, uint32_t);229230#ifdef __cplusplus231}232#endif233234#endif /* _LIBCMDUTILS_H */235236237