2 * ROX-Filer, filer for the ROX desktop project
3 * Copyright (C) 2006, Thomas Leonard and others (see changelog for details).
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the Free
7 * Software Foundation; either version 2 of the License, or (at your option)
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
17 * Place, Suite 330, Boston, MA 02111-1307 USA
20 /* diritem.c - get details about files */
22 /* Don't load icons larger than 400K (this is rather excessive, basically
23 * we just want to stop people crashing the filer with huge icons).
25 #define MAX_ICON_SIZE (400 * 1024)
39 #include "gui_support.h"
42 #include "usericons.h"
48 #define RECENT_DELAY (5 * 60) /* Time in seconds to consider a file recent */
49 #define ABOUT_NOW(time) (diritem_recent_time - time < RECENT_DELAY)
50 /* If you want to make use of the RECENT flag, make sure this is set to
51 * the current time before calling diritem_restat().
53 time_t diritem_recent_time
;
55 /* Static prototypes */
56 static void examine_dir(const guchar
*path
, DirItem
*item
,
57 struct stat
*link_target
);
59 /****************************************************************
60 * EXTERNAL INTERFACE *
61 ****************************************************************/
63 void diritem_init(void)
68 /* Bring this item's structure uptodate.
69 * 'parent' is optional; it saves one stat() for directories.
71 void diritem_restat(const guchar
*path
, DirItem
*item
, struct stat
*parent
)
77 g_object_unref(item
->_image
);
81 item
->mime_type
= NULL
;
83 if (mc_lstat(path
, &info
) == -1)
85 item
->lstat_errno
= errno
;
86 item
->base_type
= TYPE_ERROR
;
89 item
->mtime
= item
->ctime
= item
->atime
= 0;
90 item
->uid
= (uid_t
) -1;
91 item
->gid
= (gid_t
) -1;
97 item
->lstat_errno
= 0;
98 item
->size
= info
.st_size
;
99 item
->mode
= info
.st_mode
;
100 item
->atime
= info
.st_atime
;
101 item
->ctime
= info
.st_ctime
;
102 item
->mtime
= info
.st_mtime
;
103 item
->uid
= info
.st_uid
;
104 item
->gid
= info
.st_gid
;
105 if (ABOUT_NOW(item
->mtime
) || ABOUT_NOW(item
->ctime
))
106 item
->flags
|= ITEM_FLAG_RECENT
;
108 if (xattr_have(path
))
109 item
->flags
|= ITEM_FLAG_HAS_XATTR
;
111 if (S_ISLNK(info
.st_mode
))
113 if (mc_stat(path
, &info
))
114 item
->base_type
= TYPE_ERROR
;
117 mode_to_base_type(info
.st_mode
);
119 item
->flags
|= ITEM_FLAG_SYMLINK
;
121 target_path
= readlink_dup(path
);
125 item
->base_type
= mode_to_base_type(info
.st_mode
);
126 target_path
= (guchar
*) path
;
129 if (item
->base_type
== TYPE_DIRECTORY
)
131 if (mount_is_mounted(target_path
, &info
,
132 target_path
== path
? parent
: NULL
))
133 item
->flags
|= ITEM_FLAG_MOUNT_POINT
135 else if (g_hash_table_lookup(fstab_mounts
,
137 item
->flags
|= ITEM_FLAG_MOUNT_POINT
;
140 if (path
!= target_path
)
144 if (item
->base_type
== TYPE_DIRECTORY
)
146 /* KRJW: info.st_uid will be the uid of the dir, regardless
147 * of whether `path' is a dir or a symlink to one. Note that
148 * if path is a symlink to a dir, item->uid will be the uid
149 * of the *symlink*, but we really want the uid of the dir
150 * to which the symlink points.
152 examine_dir(path
, item
, &info
);
154 else if (item
->base_type
== TYPE_FILE
)
157 item
->mime_type
= text_plain
;
158 else if (item
->flags
& ITEM_FLAG_SYMLINK
)
161 link_path
= pathdup(path
);
162 item
->mime_type
= type_from_path(link_path
168 item
->mime_type
= type_from_path(path
);
170 /* Note: for symlinks we need the mode of the target */
171 if (info
.st_mode
& (S_IXUSR
| S_IXGRP
| S_IXOTH
))
173 /* Note that the flag is set for ALL executable
174 * files, but the mime_type must also be executable
175 * for clicking on the file to run it.
177 item
->flags
|= ITEM_FLAG_EXEC_FILE
;
179 if (item
->mime_type
== NULL
||
180 item
->mime_type
== application_octet_stream
)
182 item
->mime_type
= application_executable
;
184 else if (item
->mime_type
== text_plain
&&
185 !strchr(item
->leafname
, '.'))
187 item
->mime_type
= application_x_shellscript
;
190 else if (item
->mime_type
== application_x_desktop
)
192 item
->flags
|= ITEM_FLAG_EXEC_FILE
;
195 if (!item
->mime_type
)
196 item
->mime_type
= text_plain
;
198 check_globicon(path
, item
);
200 if (item
->mime_type
== application_x_desktop
&& item
->_image
== NULL
)
202 item
->_image
= g_fscache_lookup(desktop_icon_cache
, path
);
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
)
216 item
= g_new(DirItem
, 1);
217 item
->leafname
= g_strdup(leafname
);
218 item
->may_delete
= FALSE
;
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
);
228 void diritem_free(DirItem
*item
)
230 g_return_if_fail(item
!= NULL
);
233 g_object_unref(item
->_image
);
235 collate_key_free(item
->leafname_collate
);
236 g_free(item
->leafname
);
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
);
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 * link_target contains stat info for the link target for symlinks (or for the
263 * item itself if not a link).
265 static void examine_dir(const guchar
*path
, DirItem
*item
,
266 struct stat
*link_target
)
269 static GString
*tmp
= NULL
;
270 uid_t uid
= link_target
->st_uid
;
273 tmp
= g_string_new(NULL
);
275 check_globicon(path
, item
);
277 if (item
->flags
& ITEM_FLAG_MOUNT_POINT
)
279 item
->mime_type
= inode_mountpoint
;
280 return; /* Try to avoid automounter problems */
283 if (link_target
->st_mode
& S_IWOTH
)
284 return; /* Don't trust world-writable dirs */
288 * - If it contains a .DirIcon then that's the icon
289 * - If it contains an AppRun then it's an application
290 * - If it contains an AppRun but no .DirIcon then try to
291 * use AppIcon.xpm as the icon.
293 * .DirIcon and AppRun must have the same owner as the
294 * directory itself, to prevent abuse of /tmp, etc.
295 * For symlinks, we want the symlink's owner.
298 g_string_printf(tmp
, "%s/.DirIcon", path
);
301 goto no_diricon
; /* Already got an icon */
303 if (mc_lstat(tmp
->str
, &info
) != 0 || info
.st_uid
!= uid
)
304 goto no_diricon
; /* Missing, or wrong owner */
306 if (S_ISLNK(info
.st_mode
) && mc_stat(tmp
->str
, &info
) != 0)
307 goto no_diricon
; /* Bad symlink */
309 if (info
.st_size
> MAX_ICON_SIZE
|| !S_ISREG(info
.st_mode
))
310 goto no_diricon
; /* Too big, or non-regular file */
312 /* Try to load image; may still get NULL... */
313 item
->_image
= g_fscache_lookup(pixmap_cache
, tmp
->str
);
317 /* Try to find AppRun... */
318 g_string_truncate(tmp
, tmp
->len
- 8);
319 g_string_append(tmp
, "AppRun");
321 if (mc_lstat(tmp
->str
, &info
) != 0 || info
.st_uid
!= uid
)
322 goto out
; /* Missing, or wrong owner */
324 if (!(info
.st_mode
& (S_IXUSR
| S_IXGRP
| S_IXOTH
)))
325 goto out
; /* Not executable */
327 item
->flags
|= ITEM_FLAG_APPDIR
;
329 /* Try to load AppIcon.xpm... */
332 goto out
; /* Already got an icon */
334 g_string_truncate(tmp
, tmp
->len
- 3);
335 g_string_append(tmp
, "Icon.xpm");
337 /* Note: since AppRun is valid we don't need to check AppIcon.xpm
341 if (mc_stat(tmp
->str
, &info
) != 0)
342 goto out
; /* Missing, or broken symlink */
344 if (info
.st_size
> MAX_ICON_SIZE
|| !S_ISREG(info
.st_mode
))
345 goto out
; /* Too big, or non-regular file */
347 /* Try to load image; may still get NULL... */
348 item
->_image
= g_fscache_lookup(pixmap_cache
, tmp
->str
);
352 if ((item
->flags
& ITEM_FLAG_APPDIR
) && !item
->_image
)
354 /* This is an application without an icon */
355 item
->_image
= im_appdir
;
356 g_object_ref(item
->_image
);