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_ext (file_entry
*a
, file_entry
*b
)
104 int ad
= MY_ISDIR (a
);
105 int bd
= MY_ISDIR (b
);
107 if (ad
== bd
|| mix_all_files
){
108 if (a
->second_sort_key
== NULL
)
109 a
->second_sort_key
= str_create_key (extension (a
->fname
), case_sensitive
);
110 if (b
->second_sort_key
== NULL
)
111 b
->second_sort_key
= str_create_key (extension (b
->fname
), case_sensitive
);
113 r
= str_key_collate (a
->second_sort_key
, b
->second_sort_key
, case_sensitive
);
117 return sort_name (a
, b
);
123 sort_time (file_entry
*a
, file_entry
*b
)
125 int ad
= MY_ISDIR (a
);
126 int bd
= MY_ISDIR (b
);
128 if (ad
== bd
|| mix_all_files
) {
129 int result
= a
->st
.st_mtime
< b
->st
.st_mtime
? -1 :
130 a
->st
.st_mtime
> b
->st
.st_mtime
;
132 return result
* reverse
;
134 return sort_name (a
, b
);
141 sort_ctime (file_entry
*a
, file_entry
*b
)
143 int ad
= MY_ISDIR (a
);
144 int bd
= MY_ISDIR (b
);
146 if (ad
== bd
|| mix_all_files
) {
147 int result
= a
->st
.st_ctime
< b
->st
.st_ctime
? -1 :
148 a
->st
.st_ctime
> b
->st
.st_ctime
;
150 return result
* reverse
;
152 return sort_name (a
, b
);
159 sort_atime (file_entry
*a
, file_entry
*b
)
161 int ad
= MY_ISDIR (a
);
162 int bd
= MY_ISDIR (b
);
164 if (ad
== bd
|| mix_all_files
) {
165 int result
= a
->st
.st_atime
< b
->st
.st_atime
? -1 :
166 a
->st
.st_atime
> b
->st
.st_atime
;
168 return result
* reverse
;
170 return sort_name (a
, b
);
177 sort_inode (file_entry
*a
, file_entry
*b
)
179 int ad
= MY_ISDIR (a
);
180 int bd
= MY_ISDIR (b
);
182 if (ad
== bd
|| mix_all_files
)
183 return (a
->st
.st_ino
- b
->st
.st_ino
) * reverse
;
189 sort_size (file_entry
*a
, file_entry
*b
)
191 int ad
= MY_ISDIR (a
);
192 int bd
= MY_ISDIR (b
);
195 if (ad
!= bd
&& !mix_all_files
)
198 result
= a
->st
.st_size
< b
->st
.st_size
? -1 :
199 a
->st
.st_size
> b
->st
.st_size
;
201 return result
* reverse
;
203 return sort_name (a
, b
);
206 /* clear keys, should be call after sorting is finished */
208 clean_sort_keys (dir_list
*list
, int start
, int count
)
212 for (i
= 0; i
< count
; i
++){
213 str_release_key (list
->list
[i
+ start
].sort_key
, case_sensitive
);
214 list
->list
[i
+ start
].sort_key
= NULL
;
215 str_release_key (list
->list
[i
+ start
].second_sort_key
, case_sensitive
);
216 list
->list
[i
+ start
].second_sort_key
= NULL
;
222 do_sort (dir_list
*list
, sortfn
*sort
, int top
, int reverse_f
, int case_sensitive_f
, int exec_first_f
)
224 int dot_dot_found
= 0;
229 /* If there is an ".." entry the caller must take care to
230 ensure that it occupies the first list element. */
231 if (!strcmp (list
->list
[0].fname
, ".."))
234 reverse
= reverse_f
? -1 : 1;
235 case_sensitive
= case_sensitive_f
;
236 exec_first
= exec_first_f
;
237 qsort (&(list
->list
) [dot_dot_found
],
238 top
+ 1 - dot_dot_found
, sizeof (file_entry
), sort
);
240 clean_sort_keys (list
, dot_dot_found
, top
+ 1 - dot_dot_found
);
244 clean_dir (dir_list
*list
, int count
)
248 for (i
= 0; i
< count
; i
++){
249 g_free (list
->list
[i
].fname
);
250 list
->list
[i
].fname
= NULL
;
254 /* Used to set up a directory list when there is no access to a directory */
256 set_zero_dir (dir_list
*list
)
258 /* Need to grow the *list? */
259 if (list
->size
== 0) {
260 list
->list
= g_try_realloc (list
->list
, sizeof (file_entry
) *
261 (list
->size
+ RESIZE_STEPS
));
262 if (list
->list
== NULL
)
265 list
->size
+= RESIZE_STEPS
;
268 memset (&(list
->list
) [0], 0, sizeof(file_entry
));
269 list
->list
[0].fnamelen
= 2;
270 list
->list
[0].fname
= g_strdup ("..");
271 list
->list
[0].f
.link_to_dir
= 0;
272 list
->list
[0].f
.stale_link
= 0;
273 list
->list
[0].f
.dir_size_computed
= 0;
274 list
->list
[0].f
.marked
= 0;
275 list
->list
[0].st
.st_mode
= 040755;
279 /* If you change handle_dirent then check also handle_path. */
280 /* Return values: -1 = failure, 0 = don't add, 1 = add to the list */
282 handle_dirent (dir_list
*list
, const char *fltr
, struct dirent
*dp
,
283 struct stat
*buf1
, int next_free
, int *link_to_dir
,
286 if (dp
->d_name
[0] == '.' && dp
->d_name
[1] == 0)
288 if (dp
->d_name
[0] == '.' && dp
->d_name
[1] == '.' && dp
->d_name
[2] == 0)
290 if (!show_dot_files
&& (dp
->d_name
[0] == '.'))
292 if (!show_backups
&& dp
->d_name
[NLENGTH (dp
) - 1] == '~')
294 if (mc_lstat (dp
->d_name
, buf1
) == -1) {
296 * lstat() fails - such entries should be identified by
297 * buf1->st_mode being 0.
298 * It happens on QNX Neutrino for /fs/cd0 if no CD is inserted.
300 memset (buf1
, 0, sizeof (*buf1
));
303 if (S_ISDIR (buf1
->st_mode
))
304 tree_store_mark_checked (dp
->d_name
);
306 /* A link to a file or a directory? */
309 if (S_ISLNK (buf1
->st_mode
)) {
311 if (!mc_stat (dp
->d_name
, &buf2
))
312 *link_to_dir
= S_ISDIR (buf2
.st_mode
) != 0;
316 if (!(S_ISDIR (buf1
->st_mode
) || *link_to_dir
) && (fltr
!= NULL
)
317 && !mc_search (fltr
, dp
->d_name
, MC_SEARCH_T_GLOB
))
320 /* Need to grow the *list? */
321 if (next_free
== list
->size
) {
322 list
->list
= g_try_realloc (list
->list
, sizeof (file_entry
) *
323 (list
->size
+ RESIZE_STEPS
));
324 if (list
->list
== NULL
)
326 list
->size
+= RESIZE_STEPS
;
331 /* get info about ".." */
333 get_dotdot_dir_stat (const char *path
, struct stat
*st
)
335 gboolean ret
= FALSE
;
337 if ((path
!= NULL
) && (path
[0] != '\0') && (st
!= NULL
)) {
341 dotdot_dir
= g_strdup_printf ("%s/../", path
);
342 canonicalize_pathname (dotdot_dir
);
343 ret
= mc_stat (dotdot_dir
, &s
) == 0;
351 /* handle_path is a simplified handle_dirent. The difference is that
352 handle_path doesn't pay attention to show_dot_files and show_backups.
353 Moreover handle_path can't be used with a filemask.
354 If you change handle_path then check also handle_dirent. */
355 /* Return values: -1 = failure, 0 = don't add, 1 = add to the list */
357 handle_path (dir_list
*list
, const char *path
,
358 struct stat
*buf1
, int next_free
, int *link_to_dir
,
361 if (path
[0] == '.' && path
[1] == 0)
363 if (path
[0] == '.' && path
[1] == '.' && path
[2] == 0)
365 if (mc_lstat (path
, buf1
) == -1)
368 if (S_ISDIR (buf1
->st_mode
))
369 tree_store_mark_checked (path
);
371 /* A link to a file or a directory? */
374 if (S_ISLNK(buf1
->st_mode
)){
376 if (!mc_stat (path
, &buf2
))
377 *link_to_dir
= S_ISDIR(buf2
.st_mode
) != 0;
382 /* Need to grow the *list? */
383 if (next_free
== list
->size
){
384 list
->list
= g_try_realloc (list
->list
, sizeof (file_entry
) *
385 (list
->size
+ RESIZE_STEPS
));
386 if (list
->list
== NULL
)
388 list
->size
+= RESIZE_STEPS
;
394 do_load_dir (const char *path
, dir_list
*list
, sortfn
*sort
, int lc_reverse
,
395 int lc_case_sensitive
, int exec_ff
, const char *fltr
)
399 int status
, link_to_dir
, stale_link
;
403 /* ".." (if any) must be the first entry in the list */
404 if (!set_zero_dir (list
))
407 if (get_dotdot_dir_stat (path
, &st
))
408 list
->list
[next_free
].st
= st
;
411 dirp
= mc_opendir (path
);
413 message (D_ERROR
, MSG_ERROR
, _("Cannot read directory contents"));
417 tree_store_start_check (path
);
419 /* Do not add a ".." entry to the root directory */
420 if ((path
[0] == PATH_SEP
) && (path
[1] == '\0'))
423 while ((dp
= mc_readdir (dirp
))) {
425 handle_dirent (list
, fltr
, dp
, &st
, next_free
, &link_to_dir
,
430 tree_store_end_check ();
434 list
->list
[next_free
].fnamelen
= NLENGTH (dp
);
435 list
->list
[next_free
].fname
= g_strdup (dp
->d_name
);
436 list
->list
[next_free
].f
.marked
= 0;
437 list
->list
[next_free
].f
.link_to_dir
= link_to_dir
;
438 list
->list
[next_free
].f
.stale_link
= stale_link
;
439 list
->list
[next_free
].f
.dir_size_computed
= 0;
440 list
->list
[next_free
].st
= st
;
441 list
->list
[next_free
].sort_key
= NULL
;
442 list
->list
[next_free
].second_sort_key
= NULL
;
445 if ((next_free
& 31) == 0)
450 do_sort (list
, sort
, next_free
- 1, lc_reverse
, lc_case_sensitive
, exec_ff
);
453 tree_store_end_check ();
458 link_isdir (const file_entry
*file
)
460 if (file
->f
.link_to_dir
)
467 if_link_is_exe (const char *full_name
, const file_entry
*file
)
471 if (S_ISLNK (file
->st
.st_mode
) && !mc_stat (full_name
, &b
)) {
472 return is_exe (b
.st_mode
);
477 static dir_list dir_copy
= { 0, 0 };
480 alloc_dir_copy (int size
)
482 if (dir_copy
.size
< size
) {
485 for (i
= 0; i
< dir_copy
.size
; i
++)
486 g_free (dir_copy
.list
[i
].fname
);
487 g_free (dir_copy
.list
);
490 dir_copy
.list
= g_new0 (file_entry
, size
);
491 dir_copy
.size
= size
;
495 /* If fltr is null, then it is a match */
497 do_reload_dir (const char *path
, dir_list
*list
, sortfn
*sort
, int count
,
498 int rev
, int lc_case_sensitive
, int exec_ff
, const char *fltr
)
503 int i
, status
, link_to_dir
, stale_link
;
506 GHashTable
*marked_files
;
508 dirp
= mc_opendir (path
);
510 message (D_ERROR
, MSG_ERROR
, _("Cannot read directory contents"));
511 clean_dir (list
, count
);
512 return set_zero_dir (list
) ? 1 : 0;
515 tree_store_start_check (path
);
516 marked_files
= g_hash_table_new (g_str_hash
, g_str_equal
);
517 alloc_dir_copy (list
->size
);
518 for (marked_cnt
= i
= 0; i
< count
; i
++) {
519 dir_copy
.list
[i
].fnamelen
= list
->list
[i
].fnamelen
;
520 dir_copy
.list
[i
].fname
= list
->list
[i
].fname
;
521 dir_copy
.list
[i
].f
.marked
= list
->list
[i
].f
.marked
;
522 dir_copy
.list
[i
].f
.dir_size_computed
=
523 list
->list
[i
].f
.dir_size_computed
;
524 dir_copy
.list
[i
].f
.link_to_dir
= list
->list
[i
].f
.link_to_dir
;
525 dir_copy
.list
[i
].f
.stale_link
= list
->list
[i
].f
.stale_link
;
526 dir_copy
.list
[i
].sort_key
= NULL
;
527 dir_copy
.list
[i
].second_sort_key
= NULL
;
528 if (list
->list
[i
].f
.marked
) {
529 g_hash_table_insert (marked_files
, dir_copy
.list
[i
].fname
,
535 /* Add ".." except to the root directory. The ".." entry
536 (if any) must be the first in the list. */
537 if (!((path
[0] == PATH_SEP
) && (path
[1] == '\0'))) {
538 if (!set_zero_dir (list
)) {
539 clean_dir (list
, count
);
540 clean_dir (&dir_copy
, count
);
544 if (get_dotdot_dir_stat (path
, &st
))
545 list
->list
[next_free
].st
= st
;
550 while ((dp
= mc_readdir (dirp
))) {
552 handle_dirent (list
, fltr
, dp
, &st
, next_free
, &link_to_dir
,
558 /* Norbert (Feb 12, 1997):
559 Just in case someone finds this memory leak:
560 -1 means big trouble (at the moment no memory left),
561 I don't bother with further cleanup because if one gets to
562 this point he will have more problems than a few memory
563 leaks and because one 'clean_dir' would not be enough (and
564 because I don't want to spent the time to make it working,
565 IMHO it's not worthwhile).
566 clean_dir (&dir_copy, count);
568 tree_store_end_check ();
569 g_hash_table_destroy (marked_files
);
573 list
->list
[next_free
].f
.marked
= 0;
576 * If we have marked files in the copy, scan through the copy
577 * to find matching file. Decrease number of remaining marks if
580 if (marked_cnt
> 0) {
581 if ((g_hash_table_lookup (marked_files
, dp
->d_name
))) {
582 list
->list
[next_free
].f
.marked
= 1;
587 list
->list
[next_free
].fnamelen
= NLENGTH (dp
);
588 list
->list
[next_free
].fname
= g_strdup (dp
->d_name
);
589 list
->list
[next_free
].f
.link_to_dir
= link_to_dir
;
590 list
->list
[next_free
].f
.stale_link
= stale_link
;
591 list
->list
[next_free
].f
.dir_size_computed
= 0;
592 list
->list
[next_free
].st
= st
;
593 list
->list
[next_free
].sort_key
= NULL
;
594 list
->list
[next_free
].second_sort_key
= NULL
;
596 if (!(next_free
% 16))
600 tree_store_end_check ();
601 g_hash_table_destroy (marked_files
);
603 do_sort (list
, sort
, next_free
- 1, rev
, lc_case_sensitive
, exec_ff
);
605 clean_dir (&dir_copy
, count
);