r4032: Improvements for file type guessing (Thomas Leonard):
[rox-filer/translations.git] / ROX-Filer / src / diritem.c
blob1d5fc9ce5846ccf2c402dc47982efb262135ccb0
1 /*
2 * $Id$
4 * Copyright (C) 2005, 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 #define RECENT_DELAY (5 * 60) /* Time in seconds to consider a file recent */
50 #define ABOUT_NOW(time) (diritem_recent_time - time < RECENT_DELAY)
51 /* If you want to make use of the RECENT flag, make sure this is set to
52 * the current time before calling diritem_restat().
54 time_t diritem_recent_time;
56 /* Static prototypes */
57 static void examine_dir(const guchar *path, DirItem *item,
58 struct stat *link_target);
60 /****************************************************************
61 * EXTERNAL INTERFACE *
62 ****************************************************************/
64 void diritem_init(void)
66 read_globicons();
69 /* Bring this item's structure uptodate.
70 * 'parent' is optional; it saves one stat() for directories.
72 void diritem_restat(const guchar *path, DirItem *item, struct stat *parent)
74 struct stat info;
76 if (item->_image)
78 g_object_unref(item->_image);
79 item->_image = NULL;
81 item->flags = 0;
82 item->mime_type = NULL;
84 if (mc_lstat(path, &info) == -1)
86 item->lstat_errno = errno;
87 item->base_type = TYPE_ERROR;
88 item->size = 0;
89 item->mode = 0;
90 item->mtime = item->ctime = item->atime = 0;
91 item->uid = (uid_t) -1;
92 item->gid = (gid_t) -1;
94 else
96 guchar *target_path;
98 item->lstat_errno = 0;
99 item->size = info.st_size;
100 item->mode = info.st_mode;
101 item->atime = info.st_atime;
102 item->ctime = info.st_ctime;
103 item->mtime = info.st_mtime;
104 item->uid = info.st_uid;
105 item->gid = info.st_gid;
106 if (ABOUT_NOW(item->mtime) || ABOUT_NOW(item->ctime))
107 item->flags |= ITEM_FLAG_RECENT;
109 if (xtype_have_attr(path))
110 item->flags |= ITEM_FLAG_HAS_XATTR;
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);
155 else if (item->base_type == TYPE_FILE)
157 if (item->size == 0)
158 item->mime_type = text_plain;
159 else if (item->flags & ITEM_FLAG_SYMLINK)
161 guchar *link_path;
162 link_path = pathdup(path);
163 item->mime_type = type_from_path(link_path
164 ? link_path
165 : path);
166 g_free(link_path);
168 else
169 item->mime_type = type_from_path(path);
171 /* Note: for symlinks we need the mode of the target */
172 if (info.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))
174 /* Note that the flag is set for ALL executable
175 * files, but the mime_type must also be executable
176 * for clicking on the file to run it.
178 item->flags |= ITEM_FLAG_EXEC_FILE;
180 if (item->mime_type == NULL || item->mime_type == application_octet_stream)
181 item->mime_type = application_executable;
184 if (!item->mime_type)
185 item->mime_type = text_plain;
187 check_globicon(path, item);
189 else
190 check_globicon(path, item);
192 if (!item->mime_type)
193 item->mime_type = mime_type_from_base_type(item->base_type);
196 DirItem *diritem_new(const guchar *leafname)
198 DirItem *item;
200 item = g_new(DirItem, 1);
201 item->leafname = g_strdup(leafname);
202 item->may_delete = FALSE;
203 item->_image = NULL;
204 item->base_type = TYPE_UNKNOWN;
205 item->flags = ITEM_FLAG_NEED_RESCAN_QUEUE;
206 item->mime_type = NULL;
207 item->leafname_collate = collate_key_new(leafname);
209 return item;
212 void diritem_free(DirItem *item)
214 g_return_if_fail(item != NULL);
216 if (item->_image)
217 g_object_unref(item->_image);
218 item->_image = NULL;
219 collate_key_free(item->leafname_collate);
220 g_free(item->leafname);
221 g_free(item);
224 /* For use by di_image() only. Sets item->_image. */
225 void _diritem_get_image(DirItem *item)
227 g_return_if_fail(item->_image == NULL);
229 if (item->base_type == TYPE_ERROR)
231 item->_image = im_error;
232 g_object_ref(im_error);
234 else
235 item->_image = type_to_icon(item->mime_type);
238 /****************************************************************
239 * INTERNAL FUNCTIONS *
240 ****************************************************************/
242 /* Fill in more details of the DirItem for a directory item.
243 * - Looks for an image (but maybe still NULL on error)
244 * - Updates ITEM_FLAG_APPDIR
246 * link_target contains stat info for the link target for symlinks (or for the
247 * item itself if not a link).
249 static void examine_dir(const guchar *path, DirItem *item,
250 struct stat *link_target)
252 struct stat info;
253 static GString *tmp = NULL;
254 uid_t uid = link_target->st_uid;
256 if (!tmp)
257 tmp = g_string_new(NULL);
259 check_globicon(path, item);
261 if (item->flags & ITEM_FLAG_MOUNT_POINT)
263 item->mime_type = inode_mountpoint;
264 return; /* Try to avoid automounter problems */
267 if (link_target->st_mode & S_IWOTH)
268 return; /* Don't trust world-writable dirs */
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);