r3768: Updated years.
[rox-filer.git] / ROX-Filer / src / diritem.c
blob9b0d6a15485b673d459db4653730ec3ad8076811
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 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 (xtype_have_attr(path))
113 item->flags |= ITEM_FLAG_HAS_XATTR;
115 if (S_ISLNK(info.st_mode))
117 if (mc_stat(path, &info))
118 item->base_type = TYPE_ERROR;
119 else
120 item->base_type =
121 mode_to_base_type(info.st_mode);
123 item->flags |= ITEM_FLAG_SYMLINK;
125 target_path = readlink_dup(path);
127 else
129 item->base_type = mode_to_base_type(info.st_mode);
130 target_path = (guchar *) path;
133 if (item->base_type == TYPE_DIRECTORY)
135 if (mount_is_mounted(target_path, &info,
136 target_path == path ? parent : NULL))
137 item->flags |= ITEM_FLAG_MOUNT_POINT
138 | ITEM_FLAG_MOUNTED;
139 else if (g_hash_table_lookup(fstab_mounts,
140 target_path))
141 item->flags |= ITEM_FLAG_MOUNT_POINT;
144 if (path != target_path)
145 g_free(target_path);
148 if (item->base_type == TYPE_DIRECTORY)
150 /* KRJW: info.st_uid will be the uid of the dir, regardless
151 * of whether `path' is a dir or a symlink to one. Note that
152 * if path is a symlink to a dir, item->uid will be the uid
153 * of the *symlink*, but we really want the uid of the dir
154 * to which the symlink points.
156 examine_dir(path, item, info.st_uid);
158 else if (item->base_type == TYPE_FILE)
160 /* Type determined from path before checking for executables
161 * because some mounts show everything as executable and we
162 * still want to use the file name to set the mime type.
164 if (item->flags & ITEM_FLAG_SYMLINK)
166 guchar *link_path;
167 link_path = readlink_dup(path);
168 item->mime_type = type_from_path(link_path
169 ? link_path
170 : path);
171 g_free(link_path);
173 else
174 item->mime_type = xtype_get(path);
176 /* Note: for symlinks we need the mode of the target */
177 if (info.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))
179 /* Note that the flag is set for ALL executable
180 * files, but the mime_type is only
181 * application_executable if the file doesn't have a
182 * known extension when that option is in force.
184 item->flags |= ITEM_FLAG_EXEC_FILE;
186 if (!o_ignore_exec.int_value)
187 item->mime_type = application_executable;
190 if (!item->mime_type)
192 if (item->size == 0)
193 item->mime_type = text_plain;
194 else
195 item->mime_type = mime_type_from_contents(path);
198 if (!item->mime_type)
199 item->mime_type = item->flags & ITEM_FLAG_EXEC_FILE
200 ? application_executable
201 : text_plain;
203 check_globicon(path, item);
205 else
206 check_globicon(path, item);
208 if (!item->mime_type)
209 item->mime_type = mime_type_from_base_type(item->base_type);
212 DirItem *diritem_new(const guchar *leafname)
214 DirItem *item;
216 item = g_new(DirItem, 1);
217 item->leafname = g_strdup(leafname);
218 item->may_delete = FALSE;
219 item->_image = NULL;
220 item->base_type = TYPE_UNKNOWN;
221 item->flags = ITEM_FLAG_NEED_RESCAN_QUEUE;
222 item->mime_type = NULL;
223 item->leafname_collate = collate_key_new(leafname);
225 return item;
228 void diritem_free(DirItem *item)
230 g_return_if_fail(item != NULL);
232 if (item->_image)
233 g_object_unref(item->_image);
234 item->_image = NULL;
235 collate_key_free(item->leafname_collate);
236 g_free(item->leafname);
237 g_free(item);
240 /* For use by di_image() only. Sets item->_image. */
241 void _diritem_get_image(DirItem *item)
243 g_return_if_fail(item->_image == NULL);
245 if (item->base_type == TYPE_ERROR)
247 item->_image = im_error;
248 g_object_ref(im_error);
250 else
251 item->_image = type_to_icon(item->mime_type);
254 /****************************************************************
255 * INTERNAL FUNCTIONS *
256 ****************************************************************/
258 /* Fill in more details of the DirItem for a directory item.
259 * - Looks for an image (but maybe still NULL on error)
260 * - Updates ITEM_FLAG_APPDIR
262 * KRJW (26 Jan 2003): Pass extra uid var, which will be the uid of
263 * the dir given by `path' (or the uid of the dir pointed to by `path'
264 * if `path' is a symlink).
266 static void examine_dir(const guchar *path, DirItem *item, uid_t uid)
268 struct stat info;
269 static GString *tmp = NULL;
271 if (!tmp)
272 tmp = g_string_new(NULL);
274 check_globicon(path, item);
276 if (item->flags & ITEM_FLAG_MOUNT_POINT)
278 item->mime_type = inode_mountpoint;
279 return; /* Try to avoid automounter problems */
282 /* Finding the icon:
284 * - If it contains a .DirIcon then that's the icon
285 * - If it contains an AppRun then it's an application
286 * - If it contains an AppRun but no .DirIcon then try to
287 * use AppIcon.xpm as the icon.
289 * .DirIcon and AppRun must have the same owner as the
290 * directory itself, to prevent abuse of /tmp, etc.
291 * For symlinks, we want the symlink's owner.
294 g_string_printf(tmp, "%s/.DirIcon", path);
296 if (item->_image)
297 goto no_diricon; /* Already got an icon */
299 if (mc_lstat(tmp->str, &info) != 0 || info.st_uid != uid)
300 goto no_diricon; /* Missing, or wrong owner */
302 if (S_ISLNK(info.st_mode) && mc_stat(tmp->str, &info) != 0)
303 goto no_diricon; /* Bad symlink */
305 if (info.st_size > MAX_ICON_SIZE || !S_ISREG(info.st_mode))
306 goto no_diricon; /* Too big, or non-regular file */
308 /* Try to load image; may still get NULL... */
309 item->_image = g_fscache_lookup(pixmap_cache, tmp->str);
311 no_diricon:
313 /* Try to find AppRun... */
314 g_string_truncate(tmp, tmp->len - 8);
315 g_string_append(tmp, "AppRun");
317 if (mc_lstat(tmp->str, &info) != 0 || info.st_uid != uid)
318 goto out; /* Missing, or wrong owner */
320 if (!(info.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
321 goto out; /* Not executable */
323 item->flags |= ITEM_FLAG_APPDIR;
325 /* Try to load AppIcon.xpm... */
327 if (item->_image)
328 goto out; /* Already got an icon */
330 g_string_truncate(tmp, tmp->len - 3);
331 g_string_append(tmp, "Icon.xpm");
333 /* Note: since AppRun is valid we don't need to check AppIcon.xpm
334 * so carefully.
337 if (mc_stat(tmp->str, &info) != 0)
338 goto out; /* Missing, or broken symlink */
340 if (info.st_size > MAX_ICON_SIZE || !S_ISREG(info.st_mode))
341 goto out; /* Too big, or non-regular file */
343 /* Try to load image; may still get NULL... */
344 item->_image = g_fscache_lookup(pixmap_cache, tmp->str);
346 out:
348 if ((item->flags & ITEM_FLAG_APPDIR) && !item->_image)
350 /* This is an application without an icon */
351 item->_image = im_appdir;
352 g_object_ref(item->_image);