r3663: If we can't guess a file's type from its name or extended attribute, try
[rox-filer.git] / ROX-Filer / src / diritem.c
blob37f85a890b6a07b8f82d8bd7183bd8de98ed2b5b
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", TRUE);
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)
189 if (item->size == 0)
190 item->mime_type = text_plain;
191 else
192 item->mime_type = mime_type_from_contents(path);
195 if (!item->mime_type)
196 item->mime_type = item->flags & ITEM_FLAG_EXEC_FILE
197 ? application_executable
198 : text_plain;
200 check_globicon(path, item);
202 else
203 check_globicon(path, item);
205 if (!item->mime_type)
206 item->mime_type = mime_type_from_base_type(item->base_type);
208 if (!item->image)
210 if (item->base_type == TYPE_ERROR)
212 item->image = im_error;
213 g_object_ref(im_error);
215 else
216 item->image = type_to_icon(item->mime_type);
220 DirItem *diritem_new(const guchar *leafname)
222 DirItem *item;
224 item = g_new(DirItem, 1);
225 item->leafname = g_strdup(leafname);
226 item->may_delete = FALSE;
227 item->image = NULL;
228 item->base_type = TYPE_UNKNOWN;
229 item->flags = 0;
230 item->mime_type = NULL;
231 item->leafname_collate = collate_key_new(leafname);
233 return item;
236 void diritem_free(DirItem *item)
238 g_return_if_fail(item != NULL);
240 if (item->image)
241 g_object_unref(item->image);
242 item->image = NULL;
243 collate_key_free(item->leafname_collate);
244 g_free(item->leafname);
245 g_free(item);
249 /****************************************************************
250 * INTERNAL FUNCTIONS *
251 ****************************************************************/
253 /* Fill in more details of the DirItem for a directory item.
254 * - Looks for an image (but maybe still NULL on error)
255 * - Updates ITEM_FLAG_APPDIR
257 * KRJW (26 Jan 2003): Pass extra uid var, which will be the uid of
258 * the dir given by `path' (or the uid of the dir pointed to by `path'
259 * if `path' is a symlink).
261 static void examine_dir(const guchar *path, DirItem *item, uid_t uid)
263 struct stat info;
264 static GString *tmp = NULL;
266 if (!tmp)
267 tmp = g_string_new(NULL);
269 check_globicon(path, item);
271 if (item->flags & ITEM_FLAG_MOUNT_POINT)
273 item->mime_type = inode_mountpoint;
274 return; /* Try to avoid automounter problems */
277 /* Finding the icon:
279 * - If it contains a .DirIcon then that's the icon
280 * - If it contains an AppRun then it's an application
281 * - If it contains an AppRun but no .DirIcon then try to
282 * use AppIcon.xpm as the icon.
284 * .DirIcon and AppRun must have the same owner as the
285 * directory itself, to prevent abuse of /tmp, etc.
286 * For symlinks, we want the symlink's owner.
289 g_string_printf(tmp, "%s/.DirIcon", path);
291 if (item->image)
292 goto no_diricon; /* Already got an icon */
294 if (mc_lstat(tmp->str, &info) != 0 || info.st_uid != uid)
295 goto no_diricon; /* Missing, or wrong owner */
297 if (S_ISLNK(info.st_mode) && mc_stat(tmp->str, &info) != 0)
298 goto no_diricon; /* Bad symlink */
300 if (info.st_size > MAX_ICON_SIZE || !S_ISREG(info.st_mode))
301 goto no_diricon; /* Too big, or non-regular file */
303 /* Try to load image; may still get NULL... */
304 item->image = g_fscache_lookup(pixmap_cache, tmp->str);
306 no_diricon:
308 /* Try to find AppRun... */
309 g_string_truncate(tmp, tmp->len - 8);
310 g_string_append(tmp, "AppRun");
312 if (mc_lstat(tmp->str, &info) != 0 || info.st_uid != uid)
313 goto out; /* Missing, or wrong owner */
315 if (!(info.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
316 goto out; /* Not executable */
318 item->flags |= ITEM_FLAG_APPDIR;
320 /* Try to load AppIcon.xpm... */
322 if (item->image)
323 goto out; /* Already got an icon */
325 g_string_truncate(tmp, tmp->len - 3);
326 g_string_append(tmp, "Icon.xpm");
328 /* Note: since AppRun is valid we don't need to check AppIcon.xpm
329 * so carefully.
332 if (mc_stat(tmp->str, &info) != 0)
333 goto out; /* Missing, or broken symlink */
335 if (info.st_size > MAX_ICON_SIZE || !S_ISREG(info.st_mode))
336 goto out; /* Too big, or non-regular file */
338 /* Try to load image; may still get NULL... */
339 item->image = g_fscache_lookup(pixmap_cache, tmp->str);
341 out:
343 if ((item->flags & ITEM_FLAG_APPDIR) && !item->image)
345 /* This is an application without an icon */
346 item->image = im_appdir;
347 g_object_ref(item->image);