r4033: An executable text file of unknown type is marked as executable if its name
[rox-filer.git] / ROX-Filer / src / diritem.c
blob11372f2846b30f2c450dd19ff9838af7ce10b4eb
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 ||
181 item->mime_type == application_octet_stream ||
182 (item->mime_type == text_plain &&
183 !strchr(item->leafname, '.')))
185 item->mime_type = application_executable;
189 if (!item->mime_type)
190 item->mime_type = text_plain;
192 check_globicon(path, item);
194 else
195 check_globicon(path, item);
197 if (!item->mime_type)
198 item->mime_type = mime_type_from_base_type(item->base_type);
201 DirItem *diritem_new(const guchar *leafname)
203 DirItem *item;
205 item = g_new(DirItem, 1);
206 item->leafname = g_strdup(leafname);
207 item->may_delete = FALSE;
208 item->_image = NULL;
209 item->base_type = TYPE_UNKNOWN;
210 item->flags = ITEM_FLAG_NEED_RESCAN_QUEUE;
211 item->mime_type = NULL;
212 item->leafname_collate = collate_key_new(leafname);
214 return item;
217 void diritem_free(DirItem *item)
219 g_return_if_fail(item != NULL);
221 if (item->_image)
222 g_object_unref(item->_image);
223 item->_image = NULL;
224 collate_key_free(item->leafname_collate);
225 g_free(item->leafname);
226 g_free(item);
229 /* For use by di_image() only. Sets item->_image. */
230 void _diritem_get_image(DirItem *item)
232 g_return_if_fail(item->_image == NULL);
234 if (item->base_type == TYPE_ERROR)
236 item->_image = im_error;
237 g_object_ref(im_error);
239 else
240 item->_image = type_to_icon(item->mime_type);
243 /****************************************************************
244 * INTERNAL FUNCTIONS *
245 ****************************************************************/
247 /* Fill in more details of the DirItem for a directory item.
248 * - Looks for an image (but maybe still NULL on error)
249 * - Updates ITEM_FLAG_APPDIR
251 * link_target contains stat info for the link target for symlinks (or for the
252 * item itself if not a link).
254 static void examine_dir(const guchar *path, DirItem *item,
255 struct stat *link_target)
257 struct stat info;
258 static GString *tmp = NULL;
259 uid_t uid = link_target->st_uid;
261 if (!tmp)
262 tmp = g_string_new(NULL);
264 check_globicon(path, item);
266 if (item->flags & ITEM_FLAG_MOUNT_POINT)
268 item->mime_type = inode_mountpoint;
269 return; /* Try to avoid automounter problems */
272 if (link_target->st_mode & S_IWOTH)
273 return; /* Don't trust world-writable dirs */
275 /* Finding the icon:
277 * - If it contains a .DirIcon then that's the icon
278 * - If it contains an AppRun then it's an application
279 * - If it contains an AppRun but no .DirIcon then try to
280 * use AppIcon.xpm as the icon.
282 * .DirIcon and AppRun must have the same owner as the
283 * directory itself, to prevent abuse of /tmp, etc.
284 * For symlinks, we want the symlink's owner.
287 g_string_printf(tmp, "%s/.DirIcon", path);
289 if (item->_image)
290 goto no_diricon; /* Already got an icon */
292 if (mc_lstat(tmp->str, &info) != 0 || info.st_uid != uid)
293 goto no_diricon; /* Missing, or wrong owner */
295 if (S_ISLNK(info.st_mode) && mc_stat(tmp->str, &info) != 0)
296 goto no_diricon; /* Bad symlink */
298 if (info.st_size > MAX_ICON_SIZE || !S_ISREG(info.st_mode))
299 goto no_diricon; /* Too big, or non-regular file */
301 /* Try to load image; may still get NULL... */
302 item->_image = g_fscache_lookup(pixmap_cache, tmp->str);
304 no_diricon:
306 /* Try to find AppRun... */
307 g_string_truncate(tmp, tmp->len - 8);
308 g_string_append(tmp, "AppRun");
310 if (mc_lstat(tmp->str, &info) != 0 || info.st_uid != uid)
311 goto out; /* Missing, or wrong owner */
313 if (!(info.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
314 goto out; /* Not executable */
316 item->flags |= ITEM_FLAG_APPDIR;
318 /* Try to load AppIcon.xpm... */
320 if (item->_image)
321 goto out; /* Already got an icon */
323 g_string_truncate(tmp, tmp->len - 3);
324 g_string_append(tmp, "Icon.xpm");
326 /* Note: since AppRun is valid we don't need to check AppIcon.xpm
327 * so carefully.
330 if (mc_stat(tmp->str, &info) != 0)
331 goto out; /* Missing, or broken symlink */
333 if (info.st_size > MAX_ICON_SIZE || !S_ISREG(info.st_mode))
334 goto out; /* Too big, or non-regular file */
336 /* Try to load image; may still get NULL... */
337 item->_image = g_fscache_lookup(pixmap_cache, tmp->str);
339 out:
341 if ((item->flags & ITEM_FLAG_APPDIR) && !item->_image)
343 /* This is an application without an icon */
344 item->_image = im_appdir;
345 g_object_ref(item->_image);