11bc2362eSRobert Watson /*-
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
3d915a14eSPedro F. Giffuni *
41bc2362eSRobert Watson * Copyright (c) 2001 Robert N. M. Watson
51bc2362eSRobert Watson * All rights reserved.
61bc2362eSRobert Watson *
71bc2362eSRobert Watson * Redistribution and use in source and binary forms, with or without
81bc2362eSRobert Watson * modification, are permitted provided that the following conditions
91bc2362eSRobert Watson * are met:
101bc2362eSRobert Watson * 1. Redistributions of source code must retain the above copyright
111bc2362eSRobert Watson * notice, this list of conditions and the following disclaimer.
121bc2362eSRobert Watson * 2. Redistributions in binary form must reproduce the above copyright
131bc2362eSRobert Watson * notice, this list of conditions and the following disclaimer in the
141bc2362eSRobert Watson * documentation and/or other materials provided with the distribution.
151bc2362eSRobert Watson *
161bc2362eSRobert Watson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
171bc2362eSRobert Watson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
181bc2362eSRobert Watson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
191bc2362eSRobert Watson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
201bc2362eSRobert Watson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
211bc2362eSRobert Watson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
221bc2362eSRobert Watson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
231bc2362eSRobert Watson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
241bc2362eSRobert Watson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
251bc2362eSRobert Watson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
261bc2362eSRobert Watson * SUCH DAMAGE.
271bc2362eSRobert Watson */
281bc2362eSRobert Watson /*
291bc2362eSRobert Watson * TrustedBSD: Utility functions for extended attributes.
301bc2362eSRobert Watson */
311bc2362eSRobert Watson
321bc2362eSRobert Watson #include <sys/types.h>
331bc2362eSRobert Watson #include <sys/extattr.h>
341bc2362eSRobert Watson
351bc2362eSRobert Watson #include <errno.h>
3661310091SStefan Farfeleder #include <libutil.h>
371bc2362eSRobert Watson #include <string.h>
381bc2362eSRobert Watson
391bc2362eSRobert Watson int
extattr_namespace_to_string(int attrnamespace,char ** string)40d51048c9SRobert Watson extattr_namespace_to_string(int attrnamespace, char **string)
411bc2362eSRobert Watson {
421bc2362eSRobert Watson
43d51048c9SRobert Watson switch(attrnamespace) {
441bc2362eSRobert Watson case EXTATTR_NAMESPACE_USER:
4592369d84SBrian Feldman if (string != NULL)
461bc2362eSRobert Watson *string = strdup(EXTATTR_NAMESPACE_USER_STRING);
471bc2362eSRobert Watson return (0);
481bc2362eSRobert Watson
491bc2362eSRobert Watson case EXTATTR_NAMESPACE_SYSTEM:
5092369d84SBrian Feldman if (string != NULL)
511bc2362eSRobert Watson *string = strdup(EXTATTR_NAMESPACE_SYSTEM_STRING);
521bc2362eSRobert Watson return (0);
531bc2362eSRobert Watson
541bc2362eSRobert Watson default:
551bc2362eSRobert Watson errno = EINVAL;
561bc2362eSRobert Watson return (-1);
571bc2362eSRobert Watson }
581bc2362eSRobert Watson }
591bc2362eSRobert Watson
601bc2362eSRobert Watson int
extattr_string_to_namespace(const char * string,int * attrnamespace)61d51048c9SRobert Watson extattr_string_to_namespace(const char *string, int *attrnamespace)
621bc2362eSRobert Watson {
631bc2362eSRobert Watson
641bc2362eSRobert Watson if (!strcmp(string, EXTATTR_NAMESPACE_USER_STRING)) {
65d51048c9SRobert Watson if (attrnamespace != NULL)
66d51048c9SRobert Watson *attrnamespace = EXTATTR_NAMESPACE_USER;
671bc2362eSRobert Watson return (0);
681bc2362eSRobert Watson } else if (!strcmp(string, EXTATTR_NAMESPACE_SYSTEM_STRING)) {
69d51048c9SRobert Watson if (attrnamespace != NULL)
70d51048c9SRobert Watson *attrnamespace = EXTATTR_NAMESPACE_SYSTEM;
711bc2362eSRobert Watson return (0);
721bc2362eSRobert Watson } else {
731bc2362eSRobert Watson errno = EINVAL;
741bc2362eSRobert Watson return (-1);
751bc2362eSRobert Watson }
761bc2362eSRobert Watson }
77