Changed treestore functions to handle vfs_path_t objects.
[midnight-commander.git] / src / filemanager / dir.c
blobe47109345ec51d000856c78101cc259e333bb485
1 /*
2 Directory routines
4 Copyright (C) 1994, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
5 2006, 2007, 2011
6 The Free Software Foundation, Inc.
8 This file is part of the Midnight Commander.
10 The Midnight Commander is free software: you can redistribute it
11 and/or modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation, either version 3 of the License,
13 or (at your option) any later version.
15 The Midnight Commander is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 /** \file dir.c
25 * \brief Source: directory routines
28 #include <config.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <sys/stat.h>
35 #include "lib/global.h"
36 #include "lib/tty/tty.h"
37 #include "lib/search.h"
38 #include "lib/vfs/vfs.h"
39 #include "lib/fs.h"
40 #include "lib/strutil.h"
41 #include "lib/util.h"
42 #include "lib/widget.h" /* message() */
44 #include "src/setup.h" /* panels_options */
46 #include "treestore.h"
47 #include "dir.h"
48 #include "layout.h" /* rotate_dash() */
50 /*** global variables ****************************************************************************/
52 /*** file scope macro definitions ****************************************************************/
54 #define MY_ISDIR(x) (\
55 (is_exe (x->st.st_mode) && !(S_ISDIR (x->st.st_mode) || x->f.link_to_dir) && exec_first) \
56 ? 1 \
57 : ( (S_ISDIR (x->st.st_mode) || x->f.link_to_dir) ? 2 : 0) )
59 /*** file scope type declarations ****************************************************************/
61 /*** file scope variables ************************************************************************/
63 /* Reverse flag */
64 static int reverse = 1;
66 /* Are the files sorted case sensitively? */
67 static int case_sensitive = OS_SORT_CASE_SENSITIVE_DEFAULT;
69 /* Are the exec_bit files top in list */
70 static gboolean exec_first = TRUE;
72 static dir_list dir_copy = { 0, 0 };
74 /*** file scope functions ************************************************************************/
75 /* --------------------------------------------------------------------------------------------- */
78 sort_orders_t sort_orders [SORT_TYPES_TOTAL] = {
79 { N_("&Unsorted"), unsorted },
80 { N_("&Name"), sort_name },
81 { N_("&Extension"), sort_ext },
82 { N_("&Modify time"), sort_time },
83 { N_("&Access time"), sort_atime },
84 { N_("C&Hange time"), sort_ctime },
85 { N_("&Size"), sort_size },
86 { N_("&Inode"), sort_inode },
90 static inline int
91 key_collate (const char *t1, const char *t2)
93 int dotdot = 0;
94 int ret;
96 dotdot = (t1[0] == '.' ? 1 : 0) | ((t2[0] == '.' ? 1 : 0) << 1);
98 switch (dotdot)
100 case 0:
101 case 3:
102 ret = str_key_collate (t1, t2, case_sensitive) * reverse;
103 break;
104 case 1:
105 ret = -1; /* t1 < t2 */
106 break;
107 case 2:
108 ret = 1; /* t1 > t2 */
109 break;
110 default:
111 ret = 0; /* it must not happen */
114 return ret;
117 /* --------------------------------------------------------------------------------------------- */
118 /** clear keys, should be call after sorting is finished */
120 static void
121 clean_sort_keys (dir_list * list, int start, int count)
123 int i;
125 for (i = 0; i < count; i++)
127 str_release_key (list->list[i + start].sort_key, case_sensitive);
128 list->list[i + start].sort_key = NULL;
129 str_release_key (list->list[i + start].second_sort_key, case_sensitive);
130 list->list[i + start].second_sort_key = NULL;
134 /* --------------------------------------------------------------------------------------------- */
136 * If you change handle_dirent then check also handle_path.
137 * @returns -1 = failure, 0 = don't add, 1 = add to the list
140 static int
141 handle_dirent (dir_list * list, const char *fltr, struct dirent *dp,
142 struct stat *buf1, int next_free, int *link_to_dir, int *stale_link)
144 vfs_path_t *vpath;
146 if (dp->d_name[0] == '.' && dp->d_name[1] == 0)
147 return 0;
148 if (dp->d_name[0] == '.' && dp->d_name[1] == '.' && dp->d_name[2] == 0)
149 return 0;
150 if (!panels_options.show_dot_files && (dp->d_name[0] == '.'))
151 return 0;
152 if (!panels_options.show_backups && dp->d_name[NLENGTH (dp) - 1] == '~')
153 return 0;
155 vpath = vfs_path_from_str (dp->d_name);
156 if (mc_lstat (vpath, buf1) == -1)
159 * lstat() fails - such entries should be identified by
160 * buf1->st_mode being 0.
161 * It happens on QNX Neutrino for /fs/cd0 if no CD is inserted.
163 memset (buf1, 0, sizeof (*buf1));
166 if (S_ISDIR (buf1->st_mode))
167 tree_store_mark_checked (dp->d_name);
169 /* A link to a file or a directory? */
170 *link_to_dir = 0;
171 *stale_link = 0;
172 if (S_ISLNK (buf1->st_mode))
174 struct stat buf2;
175 if (mc_stat (vpath, &buf2) == 0)
176 *link_to_dir = S_ISDIR (buf2.st_mode) != 0;
177 else
178 *stale_link = 1;
180 vfs_path_free (vpath);
181 if (!(S_ISDIR (buf1->st_mode) || *link_to_dir) && (fltr != NULL)
182 && !mc_search (fltr, dp->d_name, MC_SEARCH_T_GLOB))
183 return 0;
185 /* Need to grow the *list? */
186 if (next_free == list->size)
188 list->list = g_try_realloc (list->list, sizeof (file_entry) * (list->size + RESIZE_STEPS));
189 if (list->list == NULL)
190 return -1;
191 list->size += RESIZE_STEPS;
193 return 1;
196 /* --------------------------------------------------------------------------------------------- */
197 /** get info about ".." */
199 static gboolean
200 get_dotdot_dir_stat (const char *path, struct stat *st)
202 gboolean ret = FALSE;
204 if ((path != NULL) && (path[0] != '\0') && (st != NULL))
206 vfs_path_t *vpath;
207 struct stat s;
209 vpath = vfs_path_build_filename (path, "..", NULL);
210 ret = mc_stat (vpath, &s) == 0;
211 vfs_path_free (vpath);
212 *st = s;
215 return ret;
218 /* --------------------------------------------------------------------------------------------- */
220 static void
221 alloc_dir_copy (int size)
223 if (dir_copy.size < size)
225 if (dir_copy.list)
227 int i;
228 for (i = 0; i < dir_copy.size; i++)
229 g_free (dir_copy.list[i].fname);
230 g_free (dir_copy.list);
233 dir_copy.list = g_new0 (file_entry, size);
234 dir_copy.size = size;
238 /* --------------------------------------------------------------------------------------------- */
239 /*** public functions ****************************************************************************/
240 /* --------------------------------------------------------------------------------------------- */
243 unsorted (file_entry * a, file_entry * b)
245 (void) a;
246 (void) b;
247 return 0;
250 /* --------------------------------------------------------------------------------------------- */
253 sort_name (file_entry * a, file_entry * b)
255 int ad = MY_ISDIR (a);
256 int bd = MY_ISDIR (b);
258 if (ad == bd || panels_options.mix_all_files)
260 /* create key if does not exist, key will be freed after sorting */
261 if (a->sort_key == NULL)
262 a->sort_key = str_create_key_for_filename (a->fname, case_sensitive);
263 if (b->sort_key == NULL)
264 b->sort_key = str_create_key_for_filename (b->fname, case_sensitive);
266 return key_collate (a->sort_key, b->sort_key);
268 return bd - ad;
271 /* --------------------------------------------------------------------------------------------- */
274 sort_vers (file_entry * a, file_entry * b)
276 int ad = MY_ISDIR (a);
277 int bd = MY_ISDIR (b);
279 if (ad == bd || panels_options.mix_all_files)
281 return str_verscmp (a->fname, b->fname) * reverse;
283 else
285 return bd - ad;
289 /* --------------------------------------------------------------------------------------------- */
292 sort_ext (file_entry * a, file_entry * b)
294 int r;
295 int ad = MY_ISDIR (a);
296 int bd = MY_ISDIR (b);
298 if (ad == bd || panels_options.mix_all_files)
300 if (a->second_sort_key == NULL)
301 a->second_sort_key = str_create_key (extension (a->fname), case_sensitive);
302 if (b->second_sort_key == NULL)
303 b->second_sort_key = str_create_key (extension (b->fname), case_sensitive);
305 r = str_key_collate (a->second_sort_key, b->second_sort_key, case_sensitive);
306 if (r)
307 return r * reverse;
308 else
309 return sort_name (a, b);
311 else
312 return bd - ad;
315 /* --------------------------------------------------------------------------------------------- */
318 sort_time (file_entry * a, file_entry * b)
320 int ad = MY_ISDIR (a);
321 int bd = MY_ISDIR (b);
323 if (ad == bd || panels_options.mix_all_files)
325 int result = a->st.st_mtime < b->st.st_mtime ? -1 : a->st.st_mtime > b->st.st_mtime;
326 if (result != 0)
327 return result * reverse;
328 else
329 return sort_name (a, b);
331 else
332 return bd - ad;
335 /* --------------------------------------------------------------------------------------------- */
338 sort_ctime (file_entry * a, file_entry * b)
340 int ad = MY_ISDIR (a);
341 int bd = MY_ISDIR (b);
343 if (ad == bd || panels_options.mix_all_files)
345 int result = a->st.st_ctime < b->st.st_ctime ? -1 : a->st.st_ctime > b->st.st_ctime;
346 if (result != 0)
347 return result * reverse;
348 else
349 return sort_name (a, b);
351 else
352 return bd - ad;
355 /* --------------------------------------------------------------------------------------------- */
358 sort_atime (file_entry * a, file_entry * b)
360 int ad = MY_ISDIR (a);
361 int bd = MY_ISDIR (b);
363 if (ad == bd || panels_options.mix_all_files)
365 int result = a->st.st_atime < b->st.st_atime ? -1 : a->st.st_atime > b->st.st_atime;
366 if (result != 0)
367 return result * reverse;
368 else
369 return sort_name (a, b);
371 else
372 return bd - ad;
375 /* --------------------------------------------------------------------------------------------- */
378 sort_inode (file_entry * a, file_entry * b)
380 int ad = MY_ISDIR (a);
381 int bd = MY_ISDIR (b);
383 if (ad == bd || panels_options.mix_all_files)
384 return (a->st.st_ino - b->st.st_ino) * reverse;
385 else
386 return bd - ad;
389 /* --------------------------------------------------------------------------------------------- */
392 sort_size (file_entry * a, file_entry * b)
394 int ad = MY_ISDIR (a);
395 int bd = MY_ISDIR (b);
396 int result = 0;
398 if (ad != bd && !panels_options.mix_all_files)
399 return bd - ad;
401 result = a->st.st_size < b->st.st_size ? -1 : a->st.st_size > b->st.st_size;
402 if (result != 0)
403 return result * reverse;
404 else
405 return sort_name (a, b);
408 /* --------------------------------------------------------------------------------------------- */
410 void
411 do_sort (dir_list * list, sortfn * sort, int top, gboolean reverse_f, gboolean case_sensitive_f,
412 gboolean exec_first_f)
414 int dot_dot_found = 0;
416 if (top == 0)
417 return;
419 /* If there is an ".." entry the caller must take care to
420 ensure that it occupies the first list element. */
421 if (strcmp (list->list[0].fname, "..") == 0)
422 dot_dot_found = 1;
424 reverse = reverse_f ? -1 : 1;
425 case_sensitive = case_sensitive_f ? 1 : 0;
426 exec_first = exec_first_f;
427 qsort (&(list->list)[dot_dot_found], top + 1 - dot_dot_found, sizeof (file_entry), sort);
429 clean_sort_keys (list, dot_dot_found, top + 1 - dot_dot_found);
432 /* --------------------------------------------------------------------------------------------- */
434 void
435 clean_dir (dir_list * list, int count)
437 int i;
439 for (i = 0; i < count; i++)
441 g_free (list->list[i].fname);
442 list->list[i].fname = NULL;
446 /* --------------------------------------------------------------------------------------------- */
447 /** Used to set up a directory list when there is no access to a directory */
449 gboolean
450 set_zero_dir (dir_list * list)
452 /* Need to grow the *list? */
453 if (list->size == 0)
455 list->list = g_try_realloc (list->list, sizeof (file_entry) * (list->size + RESIZE_STEPS));
456 if (list->list == NULL)
457 return FALSE;
459 list->size += RESIZE_STEPS;
462 memset (&(list->list)[0], 0, sizeof (file_entry));
463 list->list[0].fnamelen = 2;
464 list->list[0].fname = g_strndup ("..", list->list[0].fnamelen);
465 list->list[0].f.link_to_dir = 0;
466 list->list[0].f.stale_link = 0;
467 list->list[0].f.dir_size_computed = 0;
468 list->list[0].f.marked = 0;
469 list->list[0].st.st_mode = 040755;
470 return TRUE;
473 /* --------------------------------------------------------------------------------------------- */
475 handle_path is a simplified handle_dirent. The difference is that
476 handle_path doesn't pay attention to panels_options.show_dot_files
477 and panels_options.show_backups.
478 Moreover handle_path can't be used with a filemask.
479 If you change handle_path then check also handle_dirent. */
480 /* Return values: -1 = failure, 0 = don't add, 1 = add to the list */
483 handle_path (dir_list * list, const char *path,
484 struct stat *buf1, int next_free, int *link_to_dir, int *stale_link)
486 vfs_path_t *vpath;
488 if (path[0] == '.' && path[1] == 0)
489 return 0;
490 if (path[0] == '.' && path[1] == '.' && path[2] == 0)
491 return 0;
493 vpath = vfs_path_from_str (path);
494 if (mc_lstat (vpath, buf1) == -1)
496 vfs_path_free (vpath);
497 return 0;
500 if (S_ISDIR (buf1->st_mode))
501 tree_store_mark_checked (path);
503 /* A link to a file or a directory? */
504 *link_to_dir = 0;
505 *stale_link = 0;
506 if (S_ISLNK (buf1->st_mode))
508 struct stat buf2;
509 if (mc_stat (vpath, &buf2) == 0)
510 *link_to_dir = S_ISDIR (buf2.st_mode) != 0;
511 else
512 *stale_link = 1;
515 vfs_path_free (vpath);
517 /* Need to grow the *list? */
518 if (next_free == list->size)
520 list->list = g_try_realloc (list->list, sizeof (file_entry) * (list->size + RESIZE_STEPS));
521 return -1;
522 list->size += RESIZE_STEPS;
524 return 1;
527 /* --------------------------------------------------------------------------------------------- */
530 do_load_dir (const char *path, dir_list * list, sortfn * sort, gboolean lc_reverse,
531 gboolean lc_case_sensitive, gboolean exec_ff, const char *fltr)
533 DIR *dirp;
534 struct dirent *dp;
535 int status, link_to_dir, stale_link;
536 int next_free = 0;
537 struct stat st;
538 vfs_path_t *vpath;
540 /* ".." (if any) must be the first entry in the list */
541 if (!set_zero_dir (list))
542 return next_free;
544 vpath = vfs_path_from_str (path);
545 if (get_dotdot_dir_stat (path, &st))
546 list->list[next_free].st = st;
547 next_free++;
549 dirp = mc_opendir (vpath);
550 if (dirp == NULL)
552 message (D_ERROR, MSG_ERROR, _("Cannot read directory contents"));
553 goto ret;
556 tree_store_start_check (vpath);
558 /* Do not add a ".." entry to the root directory */
559 if ((path[0] == PATH_SEP) && (path[1] == '\0'))
560 next_free--;
562 while ((dp = mc_readdir (dirp)))
564 status = handle_dirent (list, fltr, dp, &st, next_free, &link_to_dir, &stale_link);
565 if (status == 0)
566 continue;
567 if (status == -1)
569 tree_store_end_check ();
570 mc_closedir (dirp);
571 goto ret;
573 list->list[next_free].fnamelen = NLENGTH (dp);
574 list->list[next_free].fname = g_strndup (dp->d_name, list->list[next_free].fnamelen);
575 list->list[next_free].f.marked = 0;
576 list->list[next_free].f.link_to_dir = link_to_dir;
577 list->list[next_free].f.stale_link = stale_link;
578 list->list[next_free].f.dir_size_computed = 0;
579 list->list[next_free].st = st;
580 list->list[next_free].sort_key = NULL;
581 list->list[next_free].second_sort_key = NULL;
582 next_free++;
584 if ((next_free & 31) == 0)
585 rotate_dash ();
588 if (next_free != 0)
589 do_sort (list, sort, next_free - 1, lc_reverse, lc_case_sensitive, exec_ff);
591 mc_closedir (dirp);
592 tree_store_end_check ();
593 ret:
594 vfs_path_free (vpath);
595 return next_free;
599 /* --------------------------------------------------------------------------------------------- */
601 gboolean
602 if_link_is_exe (const char *full_name, const file_entry * file)
604 struct stat b;
605 vfs_path_t *vpath = vfs_path_from_str (full_name);
607 if (S_ISLNK (file->st.st_mode) && mc_stat (vpath, &b) == 0)
609 vfs_path_free (vpath);
610 return is_exe (b.st_mode);
612 vfs_path_free (vpath);
613 return TRUE;
616 /* --------------------------------------------------------------------------------------------- */
617 /** If fltr is null, then it is a match */
620 do_reload_dir (const vfs_path_t * vpath, dir_list * list, sortfn * sort, int count,
621 gboolean lc_reverse, gboolean lc_case_sensitive, gboolean exec_ff, const char *fltr)
623 DIR *dirp;
624 struct dirent *dp;
625 int next_free = 0;
626 int i, status, link_to_dir, stale_link;
627 struct stat st;
628 int marked_cnt;
629 GHashTable *marked_files;
630 char *tmp_path;
632 dirp = mc_opendir (vpath);
633 if (dirp == NULL)
635 message (D_ERROR, MSG_ERROR, _("Cannot read directory contents"));
636 clean_dir (list, count);
637 return set_zero_dir (list) ? 1 : 0;
640 tree_store_start_check (vpath);
642 marked_files = g_hash_table_new (g_str_hash, g_str_equal);
643 alloc_dir_copy (list->size);
644 for (marked_cnt = i = 0; i < count; i++)
646 dir_copy.list[i].fnamelen = list->list[i].fnamelen;
647 dir_copy.list[i].fname = list->list[i].fname;
648 dir_copy.list[i].f.marked = list->list[i].f.marked;
649 dir_copy.list[i].f.dir_size_computed = list->list[i].f.dir_size_computed;
650 dir_copy.list[i].f.link_to_dir = list->list[i].f.link_to_dir;
651 dir_copy.list[i].f.stale_link = list->list[i].f.stale_link;
652 dir_copy.list[i].sort_key = NULL;
653 dir_copy.list[i].second_sort_key = NULL;
654 if (list->list[i].f.marked)
656 g_hash_table_insert (marked_files, dir_copy.list[i].fname, &dir_copy.list[i]);
657 marked_cnt++;
661 /* Add ".." except to the root directory. The ".." entry
662 (if any) must be the first in the list. */
663 tmp_path = vfs_path_get_by_index (vpath, 0)->path;
664 if (!
665 (vfs_path_elements_count (vpath) == 1 && (tmp_path[0] == PATH_SEP)
666 && (tmp_path[1] == '\0')))
668 if (!set_zero_dir (list))
670 clean_dir (list, count);
671 clean_dir (&dir_copy, count);
672 return next_free;
675 if (get_dotdot_dir_stat (tmp_path, &st))
676 list->list[next_free].st = st;
678 next_free++;
681 while ((dp = mc_readdir (dirp)))
683 status = handle_dirent (list, fltr, dp, &st, next_free, &link_to_dir, &stale_link);
684 if (status == 0)
685 continue;
686 if (status == -1)
688 mc_closedir (dirp);
689 /* Norbert (Feb 12, 1997):
690 Just in case someone finds this memory leak:
691 -1 means big trouble (at the moment no memory left),
692 I don't bother with further cleanup because if one gets to
693 this point he will have more problems than a few memory
694 leaks and because one 'clean_dir' would not be enough (and
695 because I don't want to spent the time to make it working,
696 IMHO it's not worthwhile).
697 clean_dir (&dir_copy, count);
699 tree_store_end_check ();
700 g_hash_table_destroy (marked_files);
701 return next_free;
704 list->list[next_free].f.marked = 0;
707 * If we have marked files in the copy, scan through the copy
708 * to find matching file. Decrease number of remaining marks if
709 * we copied one.
711 if (marked_cnt > 0)
713 if ((g_hash_table_lookup (marked_files, dp->d_name)))
715 list->list[next_free].f.marked = 1;
716 marked_cnt--;
720 list->list[next_free].fnamelen = NLENGTH (dp);
721 list->list[next_free].fname = g_strndup (dp->d_name, list->list[next_free].fnamelen);
722 list->list[next_free].f.link_to_dir = link_to_dir;
723 list->list[next_free].f.stale_link = stale_link;
724 list->list[next_free].f.dir_size_computed = 0;
725 list->list[next_free].st = st;
726 list->list[next_free].sort_key = NULL;
727 list->list[next_free].second_sort_key = NULL;
728 next_free++;
729 if (!(next_free % 16))
730 rotate_dash ();
732 mc_closedir (dirp);
733 tree_store_end_check ();
734 g_hash_table_destroy (marked_files);
735 if (next_free)
737 do_sort (list, sort, next_free - 1, lc_reverse, lc_case_sensitive, exec_ff);
739 clean_dir (&dir_copy, count);
740 return next_free;
743 /* --------------------------------------------------------------------------------------------- */