1da6c28aaSamw /*
2da6c28aaSamw  * CDDL HEADER START
3da6c28aaSamw  *
4da6c28aaSamw  * 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.
7da6c28aaSamw  *
8da6c28aaSamw  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9da6c28aaSamw  * or http://www.opensolaris.org/os/licensing.
10da6c28aaSamw  * See the License for the specific language governing permissions
11da6c28aaSamw  * and limitations under the License.
12da6c28aaSamw  *
13da6c28aaSamw  * When distributing Covered Code, include this CDDL HEADER in each
14da6c28aaSamw  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15da6c28aaSamw  * If applicable, add the following below this CDDL HEADER, with the
16da6c28aaSamw  * fields enclosed by brackets "[]" replaced with your own identifying
17da6c28aaSamw  * information: Portions Copyright [yyyy] [name of copyright owner]
18da6c28aaSamw  *
19da6c28aaSamw  * CDDL HEADER END
20da6c28aaSamw  */
21*7257d1b4Sraf 
22da6c28aaSamw /*
23575bd8a2Smarks  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24da6c28aaSamw  * Use is subject to license terms.
25da6c28aaSamw  */
26da6c28aaSamw 
27da6c28aaSamw #include <unistd.h>
28da6c28aaSamw #include <stdlib.h>
29da6c28aaSamw #include <string.h>
30da6c28aaSamw #include <libgen.h>
31da6c28aaSamw #include <attr.h>
32da6c28aaSamw #include <fcntl.h>
33da6c28aaSamw #include <errno.h>
34da6c28aaSamw #include "libcmdutils.h"
35da6c28aaSamw 
36da6c28aaSamw /*
37da6c28aaSamw  * Returns the status of attempting to obtain the extended system
38da6c28aaSamw  * attributes in the specified view.
39da6c28aaSamw  *
40da6c28aaSamw  * Note: If obtaining status for an extended attribute file, the caller must
41da6c28aaSamw  * chdir into the hidden directory prior to calling sysattr_status().
42da6c28aaSamw  *
43da6c28aaSamw  * Returns 1 if the extended system attributes were obtained, otherwise
44da6c28aaSamw  * returns 0.
45da6c28aaSamw  */
46da6c28aaSamw int
sysattr_status(char * file,xattr_view_t view)47da6c28aaSamw sysattr_status(char *file, xattr_view_t view)
48da6c28aaSamw {
49575bd8a2Smarks 	nvlist_t	*response = NULL;
50da6c28aaSamw 	int		saveerrno;
51da6c28aaSamw 	int		status;
52da6c28aaSamw 
53da6c28aaSamw 	status = getattrat(AT_FDCWD, view, file, &response);
54da6c28aaSamw 
55da6c28aaSamw 	saveerrno = errno;
56575bd8a2Smarks 	if (response)
57575bd8a2Smarks 		(void) nvlist_free(response);
58da6c28aaSamw 	errno = saveerrno;
59da6c28aaSamw 
60da6c28aaSamw 	return (status == 0);
61da6c28aaSamw }
62da6c28aaSamw 
63da6c28aaSamw /*
64da6c28aaSamw  * Returns the type of the specified in file.  If the file name matches
65da6c28aaSamw  * the name of either a read-only or read-write extended system attribute
66da6c28aaSamw  * file then sysattr_type() returns the type of file:
67da6c28aaSamw  * 	return value	file type
68da6c28aaSamw  *	------------	---------
69da6c28aaSamw  *	_RO_SATTR	read-only extended system attribute file
70da6c28aaSamw  *	_RW_SATTR	read-write extended system attribute file
71da6c28aaSamw  *	_NOT_SATTR	neither a read-only or read-write extended system
72da6c28aaSamw  *			attribute file.
73da6c28aaSamw  */
74da6c28aaSamw int
sysattr_type(char * file)75da6c28aaSamw sysattr_type(char *file)
76da6c28aaSamw {
77da6c28aaSamw 	if (file == NULL) {
78da6c28aaSamw 		errno = ENOENT;
79da6c28aaSamw 		return (_NOT_SATTR);
80da6c28aaSamw 	}
81da6c28aaSamw 
82da6c28aaSamw 	if (strcmp(basename(file), file) != 0) {
83da6c28aaSamw 		errno = EINVAL;
84da6c28aaSamw 		return (_NOT_SATTR);
85da6c28aaSamw 	}
86da6c28aaSamw 
87da6c28aaSamw 	errno = 0;
88da6c28aaSamw 	if (strcmp(file, VIEW_READONLY) == 0) {
89da6c28aaSamw 		return (_RO_SATTR);
90da6c28aaSamw 	} else if (strcmp(file, VIEW_READWRITE) == 0) {
91da6c28aaSamw 		return (_RW_SATTR);
92da6c28aaSamw 	} else {
93da6c28aaSamw 		return (_NOT_SATTR);
94da6c28aaSamw 	}
95da6c28aaSamw }
96da6c28aaSamw 
97da6c28aaSamw /*
98da6c28aaSamw  * Call sysattr_support() instead of pathconf(file, _PC_SATTR_ENABLED) or
99da6c28aaSamw  * pathconf(file, _PC_SATTR_EXISTS) so that if pathconf() fails over NFS, we
100da6c28aaSamw  * can still try to figure out if extended system attributes are supported by
101da6c28aaSamw  * testing for a valid extended system attribute file.
102da6c28aaSamw  *
103da6c28aaSamw  * 'name' can have the values _PC_SATTR_ENABLED or _PC_SATTR_EXISTS.
104da6c28aaSamw  *
105da6c28aaSamw  * Returns 1 if the underlying file system supports extended system attributes,
106da6c28aaSamw  * otherwise, returns -1.
107da6c28aaSamw  */
108da6c28aaSamw int
sysattr_support(char * file,int name)109da6c28aaSamw sysattr_support(char *file, int name)
110da6c28aaSamw {
111da6c28aaSamw 	int	rc;
112da6c28aaSamw 
113da6c28aaSamw 	errno = 0;
114da6c28aaSamw 	if ((name != _PC_SATTR_ENABLED) &&
115da6c28aaSamw 	    (name != _PC_SATTR_EXISTS)) {
116da6c28aaSamw 		errno = EINVAL;
117da6c28aaSamw 		return (-1);
118da6c28aaSamw 	}
119da6c28aaSamw 	if (((rc = pathconf(file, name)) == 1) || (errno != EINVAL)) {
120da6c28aaSamw 		return (rc);
121da6c28aaSamw 	}
122da6c28aaSamw 	return (sysattr_status(file, XATTR_VIEW_READONLY));
123da6c28aaSamw }
124