r3247: Experimental extended attributes support for setting MIME types
[rox-filer.git] / ROX-Filer / src / diritem.c
blob1c813f6663691caed4f3cdbfcb04240cd05348d3
1 /*
2 * $Id$
4 * Copyright (C) 2003, the ROX-Filer team.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
9 * any later version.
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
16 * You should have received a copy of the GNU General Public License along with
17 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
18 * Place, Suite 330, Boston, MA 02111-1307 USA
21 /* diritem.c - get details about files */
23 /* Don't load icons larger than 400K (this is rather excessive, basically
24 * we just want to stop people crashing the filer with huge icons).
26 #define MAX_ICON_SIZE (400 * 1024)
28 #include "config.h"
30 #include <gtk/gtk.h>
31 #include <errno.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <ctype.h>
36 #include "global.h"
38 #include "diritem.h"
39 #include "support.h"
40 #include "gui_support.h"
41 #include "mount.h"
42 #include "type.h"
43 #include "usericons.h"
44 #include "options.h"
45 #include "fscache.h"
46 #include "pixmaps.h"
47 #include "xtypes.h"
49 static Option o_ignore_exec;
51 #define RECENT_DELAY (5 * 60) /* Time in seconds to consider a file recent */
52 #define ABOUT_NOW(time) (diritem_recent_time - time < RECENT_DELAY)
53 /* If you want to make use of the RECENT flag, make sure this is set to
54 * the current time before calling diritem_restat().
56 time_t diritem_recent_time;
58 /* Static prototypes */
59 static void examine_dir(const guchar *path, DirItem *item, uid_t uid);
61 /****************************************************************
62 * EXTERNAL INTERFACE *
63 ****************************************************************/
65 void diritem_init(void)
67 option_add_int(&o_ignore_exec, "display_ignore_exec", FALSE);
69 read_globicons();
72 /* Bring this item's structure uptodate.
73 * 'parent' is optional; it saves one stat() for directories.
75 void diritem_restat(const guchar *path, DirItem *item, struct stat *parent)
77 struct stat info;
79 if (item->image)
81 g_object_unref(item->image);
82 item->image = NULL;
84 item->flags = 0;
85 item->mime_type = NULL;
87 if (mc_lstat(path, &info) == -1)
89 item->lstat_errno = errno;
90 item->base_type = TYPE_ERROR;
91 item->size = 0;
92 item->mode = 0;
93 item->mtime = item->ctime = item->atime = 0;
94 item->uid = (uid_t) -1;
95 item->gid = (gid_t) -1;
97 else
99 guchar *target_path;
101 item->lstat_errno = 0;
102 item->size = info.st_size;
103 item->mode = info.st_mode;
104 item->atime = info.st_atime;
105 item->ctime = info.st_ctime;
106 item->mtime = info.st_mtime;
107 item->uid = info.st_uid;
108 item->gid = info.st_gid;
109 if (ABOUT_NOW(item->mtime) || ABOUT_NOW(item->ctime))
110 item->flags |= ITEM_FLAG_RECENT;
112 if (S_ISLNK(info.st_mode))
114 if (mc_stat(path, &info))
115 item->base_type = TYPE_ERROR;
116 else
117 item->base_type =
118 mode_to_base_type(info.st_mode);
120 item->flags |= ITEM_FLAG_SYMLINK;
122 target_path = readlink_dup(path);
124 else
126 item->base_type = mode_to_base_type(info.st_mode);
127 target_path = (guchar *) path;
130 if (item->base_type == TYPE_DIRECTORY)
132 if (mount_is_mounted(target_path, &info,
133 target_path == path ? parent : NULL))
134 item->flags |= ITEM_FLAG_MOUNT_POINT
135 | ITEM_FLAG_MOUNTED;
136 else if (g_hash_table_lookup(fstab_mounts,
137 target_path))
138 item->flags |= ITEM_FLAG_MOUNT_POINT;
141 if (path != target_path)
142 g_free(target_path);
145 if (item->base_type == TYPE_DIRECTORY)
147 /* KRJW: info.st_uid will be the uid of the dir, regardless
148 * of whether `path' is a dir or a symlink to one. Note that
149 * if path is a symlink to a dir, item->uid will be the uid
150 * of the *symlink*, but we really want the uid of the dir
151 * to which the symlink points.
153 examine_dir(path, item, info.st_uid);
155 else if (item->base_type == TYPE_FILE)
157 /* Type determined from path before checking for executables
158 * because some mounts show everything as executable and we
159 * still want to use the file name to set the mime type.
161 if (item->flags & ITEM_FLAG_SYMLINK)
163 guchar *link_path;
164 link_path = readlink_dup(path);
165 item->mime_type = type_from_path(link_path
166 ? link_path
167 : path);
168 g_free(link_path);
170 else
171 item->mime_type = xtype_get(path);
173 /* Note: for symlinks we need the mode of the target */
174 if (info.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))
176 /* Note that the flag is set for ALL executable
177 * files, but the mime_type is only
178 * application_executable if the file doesn't have a
179 * known extension when that option is in force.
181 item->flags |= ITEM_FLAG_EXEC_FILE;
183 if (!o_ignore_exec.int_value)
184 item->mime_type = application_executable;
187 if (!item->mime_type)
188 item->mime_type = item->flags & ITEM_FLAG_EXEC_FILE
189 ? application_executable
190 : text_plain;
192 check_globicon(path, item);
194 else
195 check_globicon(path, item);
198 if (!item->mime_type)
199 item->mime_type = mime_type_from_base_type(item->base_type);
201 if (!item->image)
203 if (item->base_type == TYPE_ERROR)
205 item->image = im_error;
206 g_object_ref(im_error);
208 else
209 item->image = type_to_icon(item->mime_type);
213 DirItem *diritem_new(const guchar *leafname)
215 DirItem *item;
217 item = g_new(DirItem, 1);
218 item->leafname = g_strdup(leafname);
219 item->may_delete = FALSE;
220 item->image = NULL;
221 item->base_type = TYPE_UNKNOWN;
222 item->flags = 0;
223 item->mime_type = NULL;
224 item->leafname_collate = collate_key_new(leafname);
226 return item;
229 void diritem_free(DirItem *item)
231 g_return_if_fail(item != NULL);
233 if (item->image)
234 g_object_unref(item->image);
235 item->image = NULL;
236 collate_key_free(item->leafname_collate);
237 g_free(item->leafname);
238 g_free(item);
242 /****************************************************************
243 * INTERNAL FUNCTIONS *
244 ****************************************************************/
246 /* Fill in more details of the DirItem for a directory item.
247 * - Looks for an image (but maybe still NULL on error)
248 * - Updates ITEM_FLAG_APPDIR
250 * KRJW (26 Jan 2003): Pass extra uid var, which will be the uid of
251 * the dir given by `path' (or the uid of the dir pointed to by `path'
252 * if `path' is a symlink).
254 static void examine_dir(const guchar *path, DirItem *item, uid_t uid)
256 struct stat info;
257 static GString *tmp = NULL;
259 if (!tmp)
260 tmp = g_string_new(NULL);
262 check_globicon(path, item);
264 if (item->flags & ITEM_FLAG_MOUNT_POINT)
266 item->mime_type = inode_mountpoint;
267 return; /* Try to avoid automounter problems */
270 /* Finding the icon:
272 * - If it contains a .DirIcon then that's the icon
273 * - If it contains an AppRun then it's an application
274 * - If it contains an AppRun but no .DirIcon then try to
275 * use AppIcon.xpm as the icon.
277 * .DirIcon and AppRun must have the same owner as the
278 * directory itself, to prevent abuse of /tmp, etc.
279 * For symlinks, we want the symlink's owner.
282 g_string_printf(tmp, "%s/.DirIcon", path);
284 if (item->image)
285 goto no_diricon; /* Already got an icon */
287 if (mc_lstat(tmp->str, &info) != 0 || info.st_uid != uid)
288 goto no_diricon; /* Missing, or wrong owner */
290 if (S_ISLNK(info.st_mode) && mc_stat(tmp->str, &info) != 0)
291 goto no_diricon; /* Bad symlink */
293 if (info.st_size > MAX_ICON_SIZE || !S_ISREG(info.st_mode))
294 goto no_diricon; /* Too big, or non-regular file */
296 /* Try to load image; may still get NULL... */
297 item->image = g_fscache_lookup(pixmap_cache, tmp->str);
299 no_diricon:
301 /* Try to find AppRun... */
302 g_string_truncate(tmp, tmp->len - 8);
303 g_string_append(tmp, "AppRun");
305 if (mc_lstat(tmp->str, &info) != 0 || info.st_uid != uid)
306 goto out; /* Missing, or wrong owner */
308 if (!(info.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
309 goto out; /* Not executable */
311 item->flags |= ITEM_FLAG_APPDIR;
313 /* Try to load AppIcon.xpm... */
315 if (item->image)
316 goto out; /* Already got an icon */
318 g_string_truncate(tmp, tmp->len - 3);
319 g_string_append(tmp, "Icon.xpm");
321 /* Note: since AppRun is valid we don't need to check AppIcon.xpm
322 * so carefully.
325 if (mc_stat(tmp->str, &info) != 0)
326 goto out; /* Missing, or broken symlink */
328 if (info.st_size > MAX_ICON_SIZE || !S_ISREG(info.st_mode))
329 goto out; /* Too big, or non-regular file */
331 /* Try to load image; may still get NULL... */
332 item->image = g_fscache_lookup(pixmap_cache, tmp->str);
334 out:
336 if ((item->flags & ITEM_FLAG_APPDIR) && !item->image)
338 /* This is an application without an icon */
339 item->image = im_appdir;
340 g_object_ref(item->image);