r3313: Updated French manual translation (Arnaud Calvo).
[rox-filer.git] / ROX-Filer / src / xtypes.c
blob842d77f1a90e127fdb09148ed68d7bd6cd628b82
1 /*
2 * $Id$
4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2003, 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 #ifdef HAVE_GETXATTR
40 #if defined(HAVE_ATTR_XATTR_H)
41 #include <attr/xattr.h>
42 #elif defined(HAVE_SYS_XATTR_H)
43 #include <sys/xattr.h>
44 #else
45 #error "Where is getxattr defined?"
46 #endif
47 #endif
49 #include "global.h"
50 #include "type.h"
51 #include "xtypes.h"
53 #define XTYPE_ATTR "user.mime_type"
55 #if defined(HAVE_GETXATTR)
56 /* Linux implementation */
58 MIME_type *xtype_get(const char *path)
60 ssize_t size;
61 gchar *buf;
62 MIME_type *type = NULL;
64 size = getxattr(path, XTYPE_ATTR, "", 0);
65 if (size > 0)
67 int new_size;
69 buf = g_new(gchar, size + 1);
70 new_size = getxattr(path, XTYPE_ATTR, buf, size);
72 if (size == new_size)
74 buf[size] = '\0';
75 type = mime_type_lookup(buf);
77 g_free(buf);
80 if (type)
81 return type;
83 /* Fall back to non-extended */
84 return type_from_path(path);
87 /* 0 on success */
88 int xtype_set(const char *path, const MIME_type *type)
90 int res;
91 gchar *ttext;
93 ttext = g_strdup_printf("%s/%s", type->media_type, type->subtype);
94 res = setxattr(path, XTYPE_ATTR, ttext, strlen(ttext), 0);
95 g_free(ttext);
97 return res;
100 #elif defined(HAVE_ATTROPEN)
101 /* Solaris 9 implementation */
103 MIME_type *xtype_get(const char *path)
105 int fd;
106 char buf[1024];
107 int nb;
108 MIME_type *type=NULL;
110 fd=attropen(path, XTYPE_ATTR, O_RDONLY);
112 /*printf("%s: fd=%d ", path, fd);*/
113 if(fd>0) {
114 nb=read(fd, buf, sizeof(buf));
115 /*printf("nb=%d ", nb);*/
116 if(nb>0) {
117 buf[nb]=0;
118 /*printf("buf=%s ", buf);*/
119 type=mime_type_lookup(buf);
121 close(fd);
123 /*printf("%s -> %s\n", path, type? mime_type_comment(type): "Unknown");*/
124 if(type)
125 return type;
127 /* Fall back to non-extended */
128 return type_from_path(path);
131 int xtype_set(const char *path, const MIME_type *type)
133 int fd;
134 gchar *ttext;
135 int nb;
137 fd=attropen(path, XTYPE_ATTR, O_WRONLY|O_CREAT, 0644);
138 if(fd>0) {
139 ttext=g_strdup_printf("%s/%s", type->media_type, type->subtype);
140 nb=write(fd, ttext, strlen(ttext));
141 if(nb==strlen(ttext))
142 ftruncate(fd, (off_t) nb);
143 g_free(ttext);
145 close(fd);
147 if(nb>0)
148 return 0;
151 return 1; /* Set type failed */
154 #else
155 /* No extended attricutes available */
157 MIME_type *xtype_get(const char *path)
159 /* Fall back to non-extended */
160 return type_from_path(path);
163 int xtype_set(const char *path, const MIME_type *type)
165 errno = ENOSYS;
166 return 1; /* Set type failed */
169 #endif