4 * Copyright (C) 2002, 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)
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
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)
40 #include "gui_support.h"
43 #include "usericons.h"
48 static Option o_ignore_exec
;
50 /* Static prototypes */
51 static void examine_dir(const guchar
*path
, DirItem
*item
);
54 /****************************************************************
55 * EXTERNAL INTERFACE *
56 ****************************************************************/
58 void diritem_init(void)
60 option_add_int(&o_ignore_exec
, "display_ignore_exec", FALSE
);
65 /* Bring this item's structure uptodate.
66 * 'parent' is optional; it saves one stat() for directories.
68 void diritem_restat(const guchar
*path
, DirItem
*item
, struct stat
*parent
)
74 g_object_unref(item
->image
);
78 item
->mime_type
= NULL
;
80 if (mc_lstat(path
, &info
) == -1)
82 item
->lstat_errno
= errno
;
83 item
->base_type
= TYPE_ERROR
;
86 item
->mtime
= item
->ctime
= item
->atime
= 0;
87 item
->uid
= (uid_t
) -1;
88 item
->gid
= (gid_t
) -1;
92 item
->lstat_errno
= 0;
93 item
->size
= info
.st_size
;
94 item
->mode
= info
.st_mode
;
95 item
->atime
= info
.st_atime
;
96 item
->ctime
= info
.st_ctime
;
97 item
->mtime
= info
.st_mtime
;
98 item
->uid
= info
.st_uid
;
99 item
->gid
= info
.st_gid
;
101 if (S_ISLNK(info
.st_mode
))
103 if (mc_stat(path
, &info
))
104 item
->base_type
= TYPE_ERROR
;
107 mode_to_base_type(info
.st_mode
);
109 item
->flags
|= ITEM_FLAG_SYMLINK
;
113 item
->base_type
= mode_to_base_type(info
.st_mode
);
115 if (item
->base_type
== TYPE_DIRECTORY
)
117 if (mount_is_mounted(path
, &info
, parent
))
118 item
->flags
|= ITEM_FLAG_MOUNT_POINT
120 else if (g_hash_table_lookup(fstab_mounts
,
122 item
->flags
|= ITEM_FLAG_MOUNT_POINT
;
127 if (item
->base_type
== TYPE_DIRECTORY
)
128 examine_dir(path
, item
);
129 else if (item
->base_type
== TYPE_FILE
)
131 /* Type determined from path before checking for executables
132 * because some mounts show everything as executable and we
133 * still want to use the file name to set the mime type.
135 if (item
->flags
& ITEM_FLAG_SYMLINK
)
138 link_path
= readlink_dup(path
);
139 item
->mime_type
= type_from_path(link_path
145 item
->mime_type
= type_from_path(path
);
147 /* Note: for symlinks we need the mode of the target */
148 if (info
.st_mode
& (S_IXUSR
| S_IXGRP
| S_IXOTH
))
150 /* Note that the flag is set for ALL executable
151 * files, but the mime_type is only
152 * application_executable if the file doesn't have a
153 * known extension when that option is in force.
155 item
->flags
|= ITEM_FLAG_EXEC_FILE
;
157 if (!o_ignore_exec
.int_value
)
158 item
->mime_type
= application_executable
;
161 if (!item
->mime_type
)
162 item
->mime_type
= item
->flags
& ITEM_FLAG_EXEC_FILE
163 ? application_executable
166 check_globicon(path
, item
);
169 check_globicon(path
, item
);
172 if (!item
->mime_type
)
173 item
->mime_type
= mime_type_from_base_type(item
->base_type
);
177 if (item
->base_type
== TYPE_ERROR
)
179 item
->image
= im_error
;
180 g_object_ref(im_error
);
183 item
->image
= type_to_icon(item
->mime_type
);
187 DirItem
*diritem_new(const guchar
*leafname
)
191 item
= g_new(DirItem
, 1);
192 item
->leafname
= g_strdup(leafname
);
193 item
->may_delete
= FALSE
;
195 item
->base_type
= TYPE_UNKNOWN
;
197 item
->mime_type
= NULL
;
198 item
->leafname_collate
= collate_key_new(leafname
);
203 void diritem_free(DirItem
*item
)
205 g_return_if_fail(item
!= NULL
);
208 g_object_unref(item
->image
);
210 collate_key_free(item
->leafname_collate
);
211 g_free(item
->leafname
);
216 /****************************************************************
217 * INTERNAL FUNCTIONS *
218 ****************************************************************/
220 /* Fill in more details of the DirItem for a directory item.
221 * - Looks for an image (but maybe still NULL on error)
222 * - Updates ITEM_FLAG_APPDIR
224 static void examine_dir(const guchar
*path
, DirItem
*item
)
226 uid_t uid
= item
->uid
;
228 static GString
*tmp
= NULL
;
231 tmp
= g_string_new(NULL
);
233 check_globicon(path
, item
);
235 if (item
->flags
& ITEM_FLAG_MOUNT_POINT
)
236 return; /* Try to avoid automounter problems */
240 * - If it contains a .DirIcon then that's the icon
241 * - If it contains an AppRun then it's an application
242 * - If it contains an AppRun but no .DirIcon then try to
243 * use AppIcon.xpm as the icon.
245 * .DirIcon and AppRun must have the same owner as the
246 * directory itself, to prevent abuse of /tmp, etc.
247 * For symlinks, we want the symlink's owner.
250 g_string_printf(tmp
, "%s/.DirIcon", path
);
253 goto no_diricon
; /* Already got an icon */
255 if (mc_lstat(tmp
->str
, &info
) != 0 || info
.st_uid
!= uid
)
256 goto no_diricon
; /* Missing, or wrong owner */
258 if (S_ISLNK(info
.st_mode
) && mc_stat(tmp
->str
, &info
) != 0)
259 goto no_diricon
; /* Bad symlink */
261 if (info
.st_size
> MAX_ICON_SIZE
|| !S_ISREG(info
.st_mode
))
262 goto no_diricon
; /* Too big, or non-regular file */
264 /* Try to load image; may still get NULL... */
265 item
->image
= g_fscache_lookup(pixmap_cache
, tmp
->str
);
269 /* Try to find AppRun... */
270 g_string_truncate(tmp
, tmp
->len
- 8);
271 g_string_append(tmp
, "AppRun");
273 if (mc_lstat(tmp
->str
, &info
) != 0 || info
.st_uid
!= uid
)
274 goto out
; /* Missing, or wrong owner */
276 if (!(info
.st_mode
& (S_IXUSR
| S_IXGRP
| S_IXOTH
)))
277 goto out
; /* Not executable */
279 item
->flags
|= ITEM_FLAG_APPDIR
;
281 /* Try to load AppIcon.xpm... */
284 goto out
; /* Already got an icon */
286 g_string_truncate(tmp
, tmp
->len
- 3);
287 g_string_append(tmp
, "Icon.xpm");
289 /* Note: since AppRun is valid we don't need to check AppIcon.xpm
293 if (mc_stat(tmp
->str
, &info
) != 0)
294 goto out
; /* Missing, or broken symlink */
296 if (info
.st_size
> MAX_ICON_SIZE
|| !S_ISREG(info
.st_mode
))
297 goto out
; /* Too big, or non-regular file */
299 /* Try to load image; may still get NULL... */
300 item
->image
= g_fscache_lookup(pixmap_cache
, tmp
->str
);
304 if ((item
->flags
& ITEM_FLAG_APPDIR
) && !item
->image
)
306 /* This is an application without an icon */
307 item
->image
= im_appdir
;
308 g_object_ref(item
->image
);