Linux getifaddrs might return entries with ->ifa_addr being NULL.
[glibc.git] / hurd / xattr.c
blobcf3e22d98231aa4b5fbae98071159cc1bac0c4ff
1 /* Support for *xattr interfaces on GNU/Hurd.
2 Copyright (C) 2006 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <hurd.h>
21 #include <hurd/xattr.h>
22 #include <string.h>
23 #include <sys/mman.h>
25 /* Right now we support only a fixed set of xattr names for Hurd features.
26 There are no RPC interfaces for free-form xattr names and values.
28 Name Value encoding
29 ---- ----- --------
30 gnu.author empty if st_author==st_uid
31 uid_t giving st_author value
32 gnu.translator empty if no passive translator
33 translator and arguments: "/hurd/foo\0arg1\0arg2\0"
36 error_t
37 _hurd_xattr_get (io_t port, const char *name, void *value, size_t *size)
39 if (strncmp (name, "gnu.", 4))
40 return EOPNOTSUPP;
41 name += 4;
43 if (!strcmp (name, "author"))
45 struct stat64 st;
46 error_t err = __io_stat (port, &st);
47 if (err)
48 return err;
49 if (st.st_author == st.st_uid)
50 *size = 0;
51 else if (value)
53 if (*size < sizeof st.st_author)
54 return ERANGE;
55 memcpy (value, &st.st_author, sizeof st.st_author);
57 *size = sizeof st.st_author;
58 return 0;
61 if (!strcmp (name, "translator"))
63 char *buf = value;
64 size_t bufsz = value ? *size : 0;
65 error_t err = __file_get_translator (port, &buf, &bufsz);
66 if (err)
67 return err;
68 if (value != NULL && *size < bufsz)
70 if (buf != value)
71 munmap (buf, bufsz);
72 return -ERANGE;
74 if (buf != value && bufsz > 0)
76 if (value != NULL)
77 memcpy (value, buf, bufsz);
78 munmap (buf, bufsz);
80 *size = bufsz;
81 return 0;
84 return EOPNOTSUPP;
87 error_t
88 _hurd_xattr_set (io_t port, const char *name, const void *value, size_t size,
89 int flags)
91 if (strncmp (name, "gnu.", 4))
92 return EOPNOTSUPP;
93 name += 4;
95 if (!strcmp (name, "author"))
96 switch (size)
98 default:
99 return EINVAL;
100 case 0: /* "Clear" author by setting to st_uid. */
102 struct stat64 st;
103 error_t err = __io_stat (port, &st);
104 if (err)
105 return err;
106 if (st.st_author == st.st_uid)
108 /* Nothing to do. */
109 if (flags & XATTR_REPLACE)
110 return ENODATA;
111 return 0;
113 if (flags & XATTR_CREATE)
114 return EEXIST;
115 return __file_chauthor (port, st.st_uid);
117 case sizeof (uid_t): /* Set the author. */
119 uid_t id;
120 memcpy (&id, value, sizeof id);
121 if (flags & (XATTR_CREATE|XATTR_REPLACE))
123 struct stat64 st;
124 error_t err = __io_stat (port, &st);
125 if (err)
126 return err;
127 if (st.st_author == st.st_uid)
129 if (flags & XATTR_REPLACE)
130 return ENODATA;
132 else if (flags & XATTR_CREATE)
133 return EEXIST;
134 if (st.st_author == id)
135 /* Nothing to do. */
136 return 0;
138 return __file_chauthor (port, id);
142 if (!strcmp (name, "translator"))
144 if (flags & XATTR_REPLACE)
146 /* Must make sure it's already there. */
147 char *buf = NULL;
148 size_t bufsz = 0;
149 error_t err = __file_get_translator (port, &buf, &bufsz);
150 if (err)
151 return err;
152 if (bufsz > 0)
154 munmap (buf, bufsz);
155 return ENODATA;
158 return __file_set_translator (port,
159 FS_TRANS_SET | ((flags & XATTR_CREATE)
160 ? FS_TRANS_EXCL : 0), 0, 0,
161 value, size,
162 MACH_PORT_NULL, MACH_MSG_TYPE_COPY_SEND);
165 return EOPNOTSUPP;
168 error_t
169 _hurd_xattr_remove (io_t port, const char *name)
171 return _hurd_xattr_set (port, name, NULL, 0, XATTR_REPLACE);
174 error_t
175 _hurd_xattr_list (io_t port, void *buffer, size_t *size)
177 size_t total = 0;
178 char *bufp = buffer;
179 inline void add (const char *name, size_t len)
181 total += len;
182 if (bufp != NULL && total <= *size)
183 bufp = __mempcpy (bufp, name, len);
185 #define add(s) add (s, sizeof s)
187 struct stat64 st;
188 error_t err = __io_stat (port, &st);
189 if (err)
190 return err;
192 if (st.st_author != st.st_uid)
193 add ("gnu.author");
194 if (st.st_mode & S_IPTRANS)
195 add ("gnu.translator");
197 if (buffer != NULL && total > *size)
198 return ERANGE;
199 *size = total;
200 return 0;