r4098: Failure to create a usable default icon theme can be due to a broken link, so
[rox-filer/dt.git] / ROX-Filer / src / xtypes.c
blob3bc3eda008b8cf2d93753bc435aefa2bf0b18f8f
1 /*
2 * $Id$
4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2005, the ROX-Filer team.
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
10 * any later version.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19 * Place, Suite 330, Boston, MA 02111-1307 USA
23 /*
24 * xtypes.c - Extended filesystem attribute support for MIME types
27 #include "config.h"
28 #include <stdio.h>
29 #include <string.h>
30 #include <errno.h>
32 #include <unistd.h>
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <fcntl.h>
37 #include <glib.h>
39 #include "global.h"
40 #include "type.h"
41 #include "xtypes.h"
42 #include "options.h"
44 static Option o_xattr_ignore;
46 #define XTYPE_ATTR "user.mime_type"
48 #define RETURN_IF_IGNORED(val) if(o_xattr_ignore.int_value) return (val)
50 #if defined(HAVE_GETXATTR)
51 /* Linux implementation */
53 #include <dlfcn.h>
55 static int (*dyn_setxattr)(const char *path, const char *name,
56 const void *value, size_t size, int flags) = NULL;
57 static ssize_t (*dyn_getxattr)(const char *path, const char *name,
58 void *value, size_t size) = NULL;
59 static ssize_t (*dyn_listxattr)(const char *path, char *list,
60 size_t size) = NULL;
62 void xattr_init(void)
64 void *libc;
66 libc = dlopen("libc.so.6", RTLD_LAZY | RTLD_NOLOAD);
67 if (!libc)
68 return; /* Give up on xattr support */
70 dyn_setxattr = (void *) dlsym(libc, "setxattr");
71 dyn_getxattr = (void *) dlsym(libc, "getxattr");
72 dyn_listxattr = (void *) dlsym(libc, "listxattr");
74 option_add_int(&o_xattr_ignore, "xattr_ignore", FALSE);
77 int xattr_supported(const char *path)
79 char buf[1];
80 ssize_t nent;
82 RETURN_IF_IGNORED(FALSE);
84 if (!dyn_getxattr)
85 return FALSE;
87 if(path) {
88 errno=0;
89 nent=dyn_getxattr(path, XTYPE_ATTR, buf, sizeof(buf));
91 if(nent<0 && errno==ENOTSUP)
92 return FALSE;
95 return TRUE;
98 int xattr_have(const char *path)
100 char buf[1];
101 ssize_t nent;
103 RETURN_IF_IGNORED(FALSE);
105 if (!dyn_listxattr)
106 return FALSE;
108 errno=0;
109 nent=dyn_listxattr(path, buf, sizeof(buf));
111 if(nent<0 && errno==ERANGE)
112 return TRUE;
114 return (nent>0);
117 gchar *xattr_get(const char *path, const char *attr, int *len)
119 ssize_t size;
120 gchar *buf;
122 RETURN_IF_IGNORED(NULL);
124 if (!dyn_getxattr)
125 return NULL;
127 size = dyn_getxattr(path, attr, "", 0);
128 if (size > 0)
130 int new_size;
132 buf = g_new(gchar, size + 1);
133 new_size = dyn_getxattr(path, attr, buf, size);
135 if(size == new_size)
137 buf[size] = '\0';
139 if(len)
140 *len=(int) size;
142 return buf;
145 g_free(buf);
148 return NULL;
152 /* 0 on success */
153 int xattr_set(const char *path, const char *attr,
154 const char *value, int value_len)
156 int res;
158 if(o_xattr_ignore.int_value)
160 errno = ENOSYS;
161 return 1;
164 if (!dyn_setxattr)
166 errno = ENOSYS;
167 return 1; /* Set attr failed */
170 if(value && value_len<0)
171 value_len = strlen(value);
173 return dyn_setxattr(path, attr, value, value_len, 0);
177 #elif defined(HAVE_ATTROPEN)
179 /* Solaris 9 implementation */
181 void xattr_init(void)
183 option_add_int(&o_xattr_ignore, "xattr_ignore", FALSE);
186 int xattr_supported(const char *path)
188 RETURN_IF_IGNORED(FALSE);
189 #ifdef _PC_XATTR_ENABLED
190 if(!path)
191 return TRUE;
193 return pathconf(path, _PC_XATTR_ENABLED);
194 #else
195 return FALSE;
196 #endif
199 int xattr_have(const char *path)
201 RETURN_IF_IGNORED(FALSE);
202 #ifdef _PC_XATTR_EXISTS
203 return pathconf(path, _PC_XATTR_EXISTS)>0;
204 #else
205 return FALSE;
206 #endif
209 #define MAX_ATTR_SIZE BUFSIZ
210 gchar *xattr_get(const char *path, const char *attr, int *len);
212 int fd;
213 char *buf=NULL;
214 int nb;
215 MIME_type *type=NULL;
217 RETURN_IF_IGNORED(NULL);
219 #ifdef _PC_XATTR_EXISTS
220 if(!pathconf(path, _PC_XATTR_EXISTS))
221 return NULL;
222 #endif
224 fd=attropen(path, attr, O_RDONLY);
226 if(fd>=0) {
227 buf = g_new(gchar, MAX_ATTR_SIZE);
228 nb=read(fd, buf, MAX_ATTR_SIZE);
229 if(nb>0) {
230 buf[nb]=0;
232 close(fd);
234 if(len)
235 *len=nb;
238 return buf;
241 int xattr_set(const char *path, const char *attr,
242 const char *value, int value_len)
244 int fd;
245 int nb;
247 if(o_xattr_ignore.int_value)
249 errno = ENOSYS;
250 return 1;
253 if(value && value_len<0)
254 value_len = strlen(value);
256 fd=attropen(path, attr, O_WRONLY|O_CREAT, 0644);
257 if(fd>0) {
259 nb=write(fd, value, value_len);
260 if(nb==value_len)
261 ftruncate(fd, (off_t) nb);
263 close(fd);
265 if(nb>0)
266 return 0;
269 return 1; /* Set type failed */
272 #else
273 /* No extended attributes available */
275 void xattr_init(void)
279 int xattr_supported(const char *path)
281 return FALSE;
284 int xattr_have(const char *path)
286 return FALSE;
289 gchar *xattr_get(const char *path, const char *attr, int *len)
291 /* Fall back to non-extended */
292 return NULL;
295 int xattr_set(const char *path, const char *attr,
296 const char *value, int value_len)
298 errno = ENOSYS;
299 return 1; /* Set type failed */
302 #endif
304 MIME_type *xtype_get(const char *path)
306 MIME_type *type = NULL;
307 gchar *buf;
308 char *nl;
310 buf = xattr_get(path, XTYPE_ATTR, NULL);
312 if(buf)
314 nl = strchr(buf, '\n');
315 if(nl)
316 *nl = 0;
317 type = mime_type_lookup(buf);
318 g_free(buf);
320 return type;
323 int xtype_set(const char *path, const MIME_type *type)
325 int res;
326 gchar *ttext;
328 if(o_xattr_ignore.int_value)
330 errno = ENOSYS;
331 return 1;
334 ttext = g_strdup_printf("%s/%s", type->media_type, type->subtype);
335 res = xattr_set(path, XTYPE_ATTR, ttext, -1);
336 g_free(ttext);
338 return res;