2 Copyright (C) 1994, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3 2006, 2007 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
20 * \brief Source: directory routines
30 #include "lib/global.h"
31 #include "lib/tty/tty.h"
32 #include "lib/search.h"
33 #include "lib/vfs/mc-vfs/vfs.h"
35 #include "lib/strutil.h"
38 #include "treestore.h"
41 /* If true show files starting with a dot */
42 int show_dot_files
= 1;
44 /* If true show files ending in ~ */
47 /* If false then directories are shown separately from files */
48 int mix_all_files
= 0;
51 static int reverse
= 1;
53 /* Are the files sorted case sensitively? */
54 static int case_sensitive
= OS_SORT_CASE_SENSITIVE_DEFAULT
;
56 /* Are the exec_bit files top in list*/
57 static int exec_first
= 1;
59 #define MY_ISDIR(x) ( (is_exe (x->st.st_mode) && !(S_ISDIR (x->st.st_mode) || x->f.link_to_dir) && (exec_first == 1)) ? 1 : ( (S_ISDIR (x->st.st_mode) || x->f.link_to_dir) ? 2 : 0) )
61 sort_orders_t sort_orders [SORT_TYPES_TOTAL] = {
62 { N_("&Unsorted"), unsorted },
63 { N_("&Name"), sort_name },
64 { N_("&Extension"), sort_ext },
65 { N_("&Modify time"), sort_time },
66 { N_("&Access time"), sort_atime },
67 { N_("C&Hange time"), sort_ctime },
68 { N_("&Size"), sort_size },
69 { N_("&Inode"), sort_inode },
74 unsorted (file_entry
*a
, file_entry
*b
)
82 sort_name (file_entry
*a
, file_entry
*b
)
84 int ad
= MY_ISDIR (a
);
85 int bd
= MY_ISDIR (b
);
87 if (ad
== bd
|| mix_all_files
) {
88 /* create key if does not exist, key will be freed after sorting */
89 if (a
->sort_key
== NULL
)
90 a
->sort_key
= str_create_key_for_filename (a
->fname
, case_sensitive
);
91 if (b
->sort_key
== NULL
)
92 b
->sort_key
= str_create_key_for_filename (b
->fname
, case_sensitive
);
94 return str_key_collate (a
->sort_key
, b
->sort_key
, case_sensitive
)
101 sort_vers (file_entry
*a
, file_entry
*b
)
103 int ad
= MY_ISDIR (a
);
104 int bd
= MY_ISDIR (b
);
106 if (ad
== bd
|| mix_all_files
) {
107 return str_verscmp(a
->fname
, b
->fname
) * reverse
;
114 sort_ext (file_entry
*a
, file_entry
*b
)
117 int ad
= MY_ISDIR (a
);
118 int bd
= MY_ISDIR (b
);
120 if (ad
== bd
|| mix_all_files
){
121 if (a
->second_sort_key
== NULL
)
122 a
->second_sort_key
= str_create_key (extension (a
->fname
), case_sensitive
);
123 if (b
->second_sort_key
== NULL
)
124 b
->second_sort_key
= str_create_key (extension (b
->fname
), case_sensitive
);
126 r
= str_key_collate (a
->second_sort_key
, b
->second_sort_key
, case_sensitive
);
130 return sort_name (a
, b
);
136 sort_time (file_entry
*a
, file_entry
*b
)
138 int ad
= MY_ISDIR (a
);
139 int bd
= MY_ISDIR (b
);
141 if (ad
== bd
|| mix_all_files
) {
142 int result
= a
->st
.st_mtime
< b
->st
.st_mtime
? -1 :
143 a
->st
.st_mtime
> b
->st
.st_mtime
;
145 return result
* reverse
;
147 return sort_name (a
, b
);
154 sort_ctime (file_entry
*a
, file_entry
*b
)
156 int ad
= MY_ISDIR (a
);
157 int bd
= MY_ISDIR (b
);
159 if (ad
== bd
|| mix_all_files
) {
160 int result
= a
->st
.st_ctime
< b
->st
.st_ctime
? -1 :
161 a
->st
.st_ctime
> b
->st
.st_ctime
;
163 return result
* reverse
;
165 return sort_name (a
, b
);
172 sort_atime (file_entry
*a
, file_entry
*b
)
174 int ad
= MY_ISDIR (a
);
175 int bd
= MY_ISDIR (b
);
177 if (ad
== bd
|| mix_all_files
) {
178 int result
= a
->st
.st_atime
< b
->st
.st_atime
? -1 :
179 a
->st
.st_atime
> b
->st
.st_atime
;
181 return result
* reverse
;
183 return sort_name (a
, b
);
190 sort_inode (file_entry
*a
, file_entry
*b
)
192 int ad
= MY_ISDIR (a
);
193 int bd
= MY_ISDIR (b
);
195 if (ad
== bd
|| mix_all_files
)
196 return (a
->st
.st_ino
- b
->st
.st_ino
) * reverse
;
202 sort_size (file_entry
*a
, file_entry
*b
)
204 int ad
= MY_ISDIR (a
);
205 int bd
= MY_ISDIR (b
);
208 if (ad
!= bd
&& !mix_all_files
)
211 result
= a
->st
.st_size
< b
->st
.st_size
? -1 :
212 a
->st
.st_size
> b
->st
.st_size
;
214 return result
* reverse
;
216 return sort_name (a
, b
);
219 /* clear keys, should be call after sorting is finished */
221 clean_sort_keys (dir_list
*list
, int start
, int count
)
225 for (i
= 0; i
< count
; i
++){
226 str_release_key (list
->list
[i
+ start
].sort_key
, case_sensitive
);
227 list
->list
[i
+ start
].sort_key
= NULL
;
228 str_release_key (list
->list
[i
+ start
].second_sort_key
, case_sensitive
);
229 list
->list
[i
+ start
].second_sort_key
= NULL
;
235 do_sort (dir_list
*list
, sortfn
*sort
, int top
, int reverse_f
, int case_sensitive_f
, int exec_first_f
)
237 int dot_dot_found
= 0;
242 /* If there is an ".." entry the caller must take care to
243 ensure that it occupies the first list element. */
244 if (!strcmp (list
->list
[0].fname
, ".."))
247 reverse
= reverse_f
? -1 : 1;
248 case_sensitive
= case_sensitive_f
;
249 exec_first
= exec_first_f
;
250 qsort (&(list
->list
) [dot_dot_found
],
251 top
+ 1 - dot_dot_found
, sizeof (file_entry
), sort
);
253 clean_sort_keys (list
, dot_dot_found
, top
+ 1 - dot_dot_found
);
257 clean_dir (dir_list
*list
, int count
)
261 for (i
= 0; i
< count
; i
++){
262 g_free (list
->list
[i
].fname
);
263 list
->list
[i
].fname
= NULL
;
267 /* Used to set up a directory list when there is no access to a directory */
269 set_zero_dir (dir_list
*list
)
271 /* Need to grow the *list? */
272 if (list
->size
== 0) {
273 list
->list
= g_try_realloc (list
->list
, sizeof (file_entry
) *
274 (list
->size
+ RESIZE_STEPS
));
275 if (list
->list
== NULL
)
278 list
->size
+= RESIZE_STEPS
;
281 memset (&(list
->list
) [0], 0, sizeof(file_entry
));
282 list
->list
[0].fnamelen
= 2;
283 list
->list
[0].fname
= g_strdup ("..");
284 list
->list
[0].f
.link_to_dir
= 0;
285 list
->list
[0].f
.stale_link
= 0;
286 list
->list
[0].f
.dir_size_computed
= 0;
287 list
->list
[0].f
.marked
= 0;
288 list
->list
[0].st
.st_mode
= 040755;
292 /* If you change handle_dirent then check also handle_path. */
293 /* Return values: -1 = failure, 0 = don't add, 1 = add to the list */
295 handle_dirent (dir_list
*list
, const char *fltr
, struct dirent
*dp
,
296 struct stat
*buf1
, int next_free
, int *link_to_dir
,
299 if (dp
->d_name
[0] == '.' && dp
->d_name
[1] == 0)
301 if (dp
->d_name
[0] == '.' && dp
->d_name
[1] == '.' && dp
->d_name
[2] == 0)
303 if (!show_dot_files
&& (dp
->d_name
[0] == '.'))
305 if (!show_backups
&& dp
->d_name
[NLENGTH (dp
) - 1] == '~')
307 if (mc_lstat (dp
->d_name
, buf1
) == -1) {
309 * lstat() fails - such entries should be identified by
310 * buf1->st_mode being 0.
311 * It happens on QNX Neutrino for /fs/cd0 if no CD is inserted.
313 memset (buf1
, 0, sizeof (*buf1
));
316 if (S_ISDIR (buf1
->st_mode
))
317 tree_store_mark_checked (dp
->d_name
);
319 /* A link to a file or a directory? */
322 if (S_ISLNK (buf1
->st_mode
)) {
324 if (!mc_stat (dp
->d_name
, &buf2
))
325 *link_to_dir
= S_ISDIR (buf2
.st_mode
) != 0;
329 if (!(S_ISDIR (buf1
->st_mode
) || *link_to_dir
) && (fltr
!= NULL
)
330 && !mc_search (fltr
, dp
->d_name
, MC_SEARCH_T_GLOB
))
333 /* Need to grow the *list? */
334 if (next_free
== list
->size
) {
335 list
->list
= g_try_realloc (list
->list
, sizeof (file_entry
) *
336 (list
->size
+ RESIZE_STEPS
));
337 if (list
->list
== NULL
)
339 list
->size
+= RESIZE_STEPS
;
344 /* get info about ".." */
346 get_dotdot_dir_stat (const char *path
, struct stat
*st
)
348 gboolean ret
= FALSE
;
350 if ((path
!= NULL
) && (path
[0] != '\0') && (st
!= NULL
)) {
354 dotdot_dir
= g_strdup_printf ("%s/../", path
);
355 canonicalize_pathname (dotdot_dir
);
356 ret
= mc_stat (dotdot_dir
, &s
) == 0;
364 /* handle_path is a simplified handle_dirent. The difference is that
365 handle_path doesn't pay attention to show_dot_files and show_backups.
366 Moreover handle_path can't be used with a filemask.
367 If you change handle_path then check also handle_dirent. */
368 /* Return values: -1 = failure, 0 = don't add, 1 = add to the list */
370 handle_path (dir_list
*list
, const char *path
,
371 struct stat
*buf1
, int next_free
, int *link_to_dir
,
374 if (path
[0] == '.' && path
[1] == 0)
376 if (path
[0] == '.' && path
[1] == '.' && path
[2] == 0)
378 if (mc_lstat (path
, buf1
) == -1)
381 if (S_ISDIR (buf1
->st_mode
))
382 tree_store_mark_checked (path
);
384 /* A link to a file or a directory? */
387 if (S_ISLNK(buf1
->st_mode
)){
389 if (!mc_stat (path
, &buf2
))
390 *link_to_dir
= S_ISDIR(buf2
.st_mode
) != 0;
395 /* Need to grow the *list? */
396 if (next_free
== list
->size
){
397 list
->list
= g_try_realloc (list
->list
, sizeof (file_entry
) *
398 (list
->size
+ RESIZE_STEPS
));
399 if (list
->list
== NULL
)
401 list
->size
+= RESIZE_STEPS
;
407 do_load_dir (const char *path
, dir_list
*list
, sortfn
*sort
, int lc_reverse
,
408 int lc_case_sensitive
, int exec_ff
, const char *fltr
)
412 int status
, link_to_dir
, stale_link
;
416 /* ".." (if any) must be the first entry in the list */
417 if (!set_zero_dir (list
))
420 if (get_dotdot_dir_stat (path
, &st
))
421 list
->list
[next_free
].st
= st
;
424 dirp
= mc_opendir (path
);
426 message (D_ERROR
, MSG_ERROR
, _("Cannot read directory contents"));
430 tree_store_start_check (path
);
432 /* Do not add a ".." entry to the root directory */
433 if ((path
[0] == PATH_SEP
) && (path
[1] == '\0'))
436 while ((dp
= mc_readdir (dirp
))) {
438 handle_dirent (list
, fltr
, dp
, &st
, next_free
, &link_to_dir
,
443 tree_store_end_check ();
447 list
->list
[next_free
].fnamelen
= NLENGTH (dp
);
448 list
->list
[next_free
].fname
= g_strdup (dp
->d_name
);
449 list
->list
[next_free
].f
.marked
= 0;
450 list
->list
[next_free
].f
.link_to_dir
= link_to_dir
;
451 list
->list
[next_free
].f
.stale_link
= stale_link
;
452 list
->list
[next_free
].f
.dir_size_computed
= 0;
453 list
->list
[next_free
].st
= st
;
454 list
->list
[next_free
].sort_key
= NULL
;
455 list
->list
[next_free
].second_sort_key
= NULL
;
458 if ((next_free
& 31) == 0)
463 do_sort (list
, sort
, next_free
- 1, lc_reverse
, lc_case_sensitive
, exec_ff
);
466 tree_store_end_check ();
471 link_isdir (const file_entry
*file
)
473 if (file
->f
.link_to_dir
)
480 if_link_is_exe (const char *full_name
, const file_entry
*file
)
484 if (S_ISLNK (file
->st
.st_mode
) && !mc_stat (full_name
, &b
)) {
485 return is_exe (b
.st_mode
);
490 static dir_list dir_copy
= { 0, 0 };
493 alloc_dir_copy (int size
)
495 if (dir_copy
.size
< size
) {
498 for (i
= 0; i
< dir_copy
.size
; i
++)
499 g_free (dir_copy
.list
[i
].fname
);
500 g_free (dir_copy
.list
);
503 dir_copy
.list
= g_new0 (file_entry
, size
);
504 dir_copy
.size
= size
;
508 /* If fltr is null, then it is a match */
510 do_reload_dir (const char *path
, dir_list
*list
, sortfn
*sort
, int count
,
511 int rev
, int lc_case_sensitive
, int exec_ff
, const char *fltr
)
516 int i
, status
, link_to_dir
, stale_link
;
519 GHashTable
*marked_files
;
521 dirp
= mc_opendir (path
);
523 message (D_ERROR
, MSG_ERROR
, _("Cannot read directory contents"));
524 clean_dir (list
, count
);
525 return set_zero_dir (list
) ? 1 : 0;
528 tree_store_start_check (path
);
529 marked_files
= g_hash_table_new (g_str_hash
, g_str_equal
);
530 alloc_dir_copy (list
->size
);
531 for (marked_cnt
= i
= 0; i
< count
; i
++) {
532 dir_copy
.list
[i
].fnamelen
= list
->list
[i
].fnamelen
;
533 dir_copy
.list
[i
].fname
= list
->list
[i
].fname
;
534 dir_copy
.list
[i
].f
.marked
= list
->list
[i
].f
.marked
;
535 dir_copy
.list
[i
].f
.dir_size_computed
=
536 list
->list
[i
].f
.dir_size_computed
;
537 dir_copy
.list
[i
].f
.link_to_dir
= list
->list
[i
].f
.link_to_dir
;
538 dir_copy
.list
[i
].f
.stale_link
= list
->list
[i
].f
.stale_link
;
539 dir_copy
.list
[i
].sort_key
= NULL
;
540 dir_copy
.list
[i
].second_sort_key
= NULL
;
541 if (list
->list
[i
].f
.marked
) {
542 g_hash_table_insert (marked_files
, dir_copy
.list
[i
].fname
,
548 /* Add ".." except to the root directory. The ".." entry
549 (if any) must be the first in the list. */
550 if (!((path
[0] == PATH_SEP
) && (path
[1] == '\0'))) {
551 if (!set_zero_dir (list
)) {
552 clean_dir (list
, count
);
553 clean_dir (&dir_copy
, count
);
557 if (get_dotdot_dir_stat (path
, &st
))
558 list
->list
[next_free
].st
= st
;
563 while ((dp
= mc_readdir (dirp
))) {
565 handle_dirent (list
, fltr
, dp
, &st
, next_free
, &link_to_dir
,
571 /* Norbert (Feb 12, 1997):
572 Just in case someone finds this memory leak:
573 -1 means big trouble (at the moment no memory left),
574 I don't bother with further cleanup because if one gets to
575 this point he will have more problems than a few memory
576 leaks and because one 'clean_dir' would not be enough (and
577 because I don't want to spent the time to make it working,
578 IMHO it's not worthwhile).
579 clean_dir (&dir_copy, count);
581 tree_store_end_check ();
582 g_hash_table_destroy (marked_files
);
586 list
->list
[next_free
].f
.marked
= 0;
589 * If we have marked files in the copy, scan through the copy
590 * to find matching file. Decrease number of remaining marks if
593 if (marked_cnt
> 0) {
594 if ((g_hash_table_lookup (marked_files
, dp
->d_name
))) {
595 list
->list
[next_free
].f
.marked
= 1;
600 list
->list
[next_free
].fnamelen
= NLENGTH (dp
);
601 list
->list
[next_free
].fname
= g_strdup (dp
->d_name
);
602 list
->list
[next_free
].f
.link_to_dir
= link_to_dir
;
603 list
->list
[next_free
].f
.stale_link
= stale_link
;
604 list
->list
[next_free
].f
.dir_size_computed
= 0;
605 list
->list
[next_free
].st
= st
;
606 list
->list
[next_free
].sort_key
= NULL
;
607 list
->list
[next_free
].second_sort_key
= NULL
;
609 if (!(next_free
% 16))
613 tree_store_end_check ();
614 g_hash_table_destroy (marked_files
);
616 do_sort (list
, sort
, next_free
- 1, rev
, lc_case_sensitive
, exec_ff
);
618 clean_dir (&dir_copy
, count
);