Fix -Wundef warning for HAVE_OBSTACK
[glibc.git] / hurd / xattr.c
blob7f754004fc43465413ac76fb96f1308c6780d1fc
1 /* Support for *xattr interfaces on GNU/Hurd.
2 Copyright (C) 2006-2014 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 #include <hurd.h>
20 #include <hurd/xattr.h>
21 #include <string.h>
22 #include <sys/mman.h>
24 /* Right now we support only a fixed set of xattr names for Hurd features.
25 There are no RPC interfaces for free-form xattr names and values.
27 Name Value encoding
28 ---- ----- --------
29 gnu.author empty if st_author==st_uid
30 uid_t giving st_author value
31 gnu.translator empty if no passive translator
32 translator and arguments: "/hurd/foo\0arg1\0arg2\0"
35 error_t
36 _hurd_xattr_get (io_t port, const char *name, void *value, size_t *size)
38 if (strncmp (name, "gnu.", 4))
39 return EOPNOTSUPP;
40 name += 4;
42 if (!strcmp (name, "author"))
44 struct stat64 st;
45 error_t err = __io_stat (port, &st);
46 if (err)
47 return err;
48 if (st.st_author == st.st_uid)
49 *size = 0;
50 else if (value)
52 if (*size < sizeof st.st_author)
53 return ERANGE;
54 memcpy (value, &st.st_author, sizeof st.st_author);
56 *size = sizeof st.st_author;
57 return 0;
60 if (!strcmp (name, "translator"))
62 char *buf = value;
63 size_t bufsz = value ? *size : 0;
64 error_t err = __file_get_translator (port, &buf, &bufsz);
65 if (err)
66 return err;
67 if (value != NULL && *size < bufsz)
69 if (buf != value)
70 munmap (buf, bufsz);
71 return -ERANGE;
73 if (buf != value && bufsz > 0)
75 if (value != NULL)
76 memcpy (value, buf, bufsz);
77 munmap (buf, bufsz);
79 *size = bufsz;
80 return 0;
83 return EOPNOTSUPP;
86 error_t
87 _hurd_xattr_set (io_t port, const char *name, const void *value, size_t size,
88 int flags)
90 if (strncmp (name, "gnu.", 4))
91 return EOPNOTSUPP;
92 name += 4;
94 if (!strcmp (name, "author"))
95 switch (size)
97 default:
98 return EINVAL;
99 case 0: /* "Clear" author by setting to st_uid. */
101 struct stat64 st;
102 error_t err = __io_stat (port, &st);
103 if (err)
104 return err;
105 if (st.st_author == st.st_uid)
107 /* Nothing to do. */
108 if (flags & XATTR_REPLACE)
109 return ENODATA;
110 return 0;
112 if (flags & XATTR_CREATE)
113 return EEXIST;
114 return __file_chauthor (port, st.st_uid);
116 case sizeof (uid_t): /* Set the author. */
118 uid_t id;
119 memcpy (&id, value, sizeof id);
120 if (flags & (XATTR_CREATE|XATTR_REPLACE))
122 struct stat64 st;
123 error_t err = __io_stat (port, &st);
124 if (err)
125 return err;
126 if (st.st_author == st.st_uid)
128 if (flags & XATTR_REPLACE)
129 return ENODATA;
131 else if (flags & XATTR_CREATE)
132 return EEXIST;
133 if (st.st_author == id)
134 /* Nothing to do. */
135 return 0;
137 return __file_chauthor (port, id);
141 if (!strcmp (name, "translator"))
143 if (flags & XATTR_REPLACE)
145 /* Must make sure it's already there. */
146 char *buf = NULL;
147 size_t bufsz = 0;
148 error_t err = __file_get_translator (port, &buf, &bufsz);
149 if (err)
150 return err;
151 if (bufsz > 0)
153 munmap (buf, bufsz);
154 return ENODATA;
157 return __file_set_translator (port,
158 FS_TRANS_SET | ((flags & XATTR_CREATE)
159 ? FS_TRANS_EXCL : 0), 0, 0,
160 value, size,
161 MACH_PORT_NULL, MACH_MSG_TYPE_COPY_SEND);
164 return EOPNOTSUPP;
167 error_t
168 _hurd_xattr_remove (io_t port, const char *name)
170 return _hurd_xattr_set (port, name, NULL, 0, XATTR_REPLACE);
173 error_t
174 _hurd_xattr_list (io_t port, void *buffer, size_t *size)
176 size_t total = 0;
177 char *bufp = buffer;
178 inline void add (const char *name, size_t len)
180 total += len;
181 if (bufp != NULL && total <= *size)
182 bufp = __mempcpy (bufp, name, len);
184 #define add(s) add (s, sizeof s)
186 struct stat64 st;
187 error_t err = __io_stat (port, &st);
188 if (err)
189 return err;
191 if (st.st_author != st.st_uid)
192 add ("gnu.author");
193 if (st.st_mode & S_IPTRANS)
194 add ("gnu.translator");
196 if (buffer != NULL && total > *size)
197 return ERANGE;
198 *size = total;
199 return 0;