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)
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
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
24 * xtypes.c - Extended filesystem attribute support for MIME types
33 #include <sys/types.h>
40 #if defined(HAVE_ATTR_XATTR_H)
41 #include <attr/xattr.h>
42 #elif defined(HAVE_SYS_XATTR_H)
43 #include <sys/xattr.h>
45 #error "Where is getxattr defined?"
53 #define XTYPE_ATTR "user.mime_type"
55 #if defined(HAVE_GETXATTR)
56 /* Linux implementation */
58 MIME_type
*xtype_get(const char *path
)
62 MIME_type
*type
= NULL
;
64 size
= getxattr(path
, XTYPE_ATTR
, "", 0);
69 buf
= g_new(gchar
, size
+ 1);
70 new_size
= getxattr(path
, XTYPE_ATTR
, buf
, size
);
75 type
= mime_type_lookup(buf
);
83 /* Fall back to non-extended */
84 return type_from_path(path
);
88 int xtype_set(const char *path
, const MIME_type
*type
)
93 ttext
= g_strdup_printf("%s/%s", type
->media_type
, type
->subtype
);
94 res
= setxattr(path
, XTYPE_ATTR
, ttext
, strlen(ttext
), 0);
100 #elif defined(HAVE_ATTROPEN)
101 /* Solaris 9 implementation */
103 MIME_type
*xtype_get(const char *path
)
108 MIME_type
*type
=NULL
;
110 fd
=attropen(path
, XTYPE_ATTR
, O_RDONLY
);
112 /*printf("%s: fd=%d ", path, fd);*/
114 nb
=read(fd
, buf
, sizeof(buf
));
115 /*printf("nb=%d ", nb);*/
118 /*printf("buf=%s ", buf);*/
119 type
=mime_type_lookup(buf
);
123 /*printf("%s -> %s\n", path, type? mime_type_comment(type): "Unknown");*/
127 /* Fall back to non-extended */
128 return type_from_path(path
);
131 int xtype_set(const char *path
, const MIME_type
*type
)
137 fd
=attropen(path
, XTYPE_ATTR
, O_WRONLY
|O_CREAT
, 0644);
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
);
151 return 1; /* Set type failed */
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
)
166 return 1; /* Set type failed */