93c3029d88afeb3792a0ec07aa8216be134b9a33
[midnight-commander.git] / src / filemanager / dir.c
blob93c3029d88afeb3792a0ec07aa8216be134b9a33
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 src/filemanager/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 /* --------------------------------------------------------------------------------------------- */
119 * clear keys, should be call after sorting is finished.
122 static void
123 clean_sort_keys (dir_list * list, int start, int count)
125 int i;
127 for (i = 0; i < count; i++)
129 str_release_key (list->list[i + start].sort_key, case_sensitive);
130 list->list[i + start].sort_key = NULL;
131 str_release_key (list->list[i + start].second_sort_key, case_sensitive);
132 list->list[i + start].second_sort_key = NULL;
136 /* --------------------------------------------------------------------------------------------- */
138 * Increase directory list by RESIZE_STEPS
140 * @param list directory list
141 * @returns FALSE = failure, TRUE = success
144 static gboolean
145 grow_list (dir_list * list)
147 if (list == NULL)
148 return FALSE;
150 list->list = g_try_realloc (list->list, sizeof (file_entry) * (list->size + RESIZE_STEPS));
152 if (list->list == NULL)
153 return FALSE;
155 list->size += RESIZE_STEPS;
157 return TRUE;
160 /* --------------------------------------------------------------------------------------------- */
162 * If you change handle_dirent then check also handle_path.
163 * @returns -1 = failure, 0 = don't add, 1 = add to the list
166 static int
167 handle_dirent (dir_list * list, const char *fltr, struct dirent *dp,
168 struct stat *buf1, int next_free, int *link_to_dir, int *stale_link)
170 vfs_path_t *vpath;
172 if (dp->d_name[0] == '.' && dp->d_name[1] == 0)
173 return 0;
174 if (dp->d_name[0] == '.' && dp->d_name[1] == '.' && dp->d_name[2] == 0)
175 return 0;
176 if (!panels_options.show_dot_files && (dp->d_name[0] == '.'))
177 return 0;
178 if (!panels_options.show_backups && dp->d_name[NLENGTH (dp) - 1] == '~')
179 return 0;
181 vpath = vfs_path_from_str (dp->d_name);
182 if (mc_lstat (vpath, buf1) == -1)
185 * lstat() fails - such entries should be identified by
186 * buf1->st_mode being 0.
187 * It happens on QNX Neutrino for /fs/cd0 if no CD is inserted.
189 memset (buf1, 0, sizeof (*buf1));
192 if (S_ISDIR (buf1->st_mode))
193 tree_store_mark_checked (dp->d_name);
195 /* A link to a file or a directory? */
196 *link_to_dir = 0;
197 *stale_link = 0;
198 if (S_ISLNK (buf1->st_mode))
200 struct stat buf2;
201 if (mc_stat (vpath, &buf2) == 0)
202 *link_to_dir = S_ISDIR (buf2.st_mode) != 0;
203 else
204 *stale_link = 1;
206 vfs_path_free (vpath);
207 if (!(S_ISDIR (buf1->st_mode) || *link_to_dir) && (fltr != NULL)
208 && !mc_search (fltr, dp->d_name, MC_SEARCH_T_GLOB))
209 return 0;
211 /* Need to grow the *list? */
212 if (next_free == list->size && !grow_list (list))
213 return -1;
215 return 1;
218 /* --------------------------------------------------------------------------------------------- */
219 /** get info about ".." */
221 static gboolean
222 get_dotdot_dir_stat (const vfs_path_t * vpath, struct stat *st)
224 gboolean ret = FALSE;
226 if ((vpath != NULL) && (st != NULL))
228 const char *path;
230 path = vfs_path_get_by_index (vpath, 0)->path;
231 if (path != NULL && *path != '\0')
233 vfs_path_t *tmp_vpath;
234 struct stat s;
236 tmp_vpath = vfs_path_append_new (vpath, "..", NULL);
237 ret = mc_stat (tmp_vpath, &s) == 0;
238 vfs_path_free (tmp_vpath);
239 *st = s;
243 return ret;
246 /* --------------------------------------------------------------------------------------------- */
248 static void
249 alloc_dir_copy (int size)
251 if (dir_copy.size < size)
253 if (dir_copy.list)
255 int i;
256 for (i = 0; i < dir_copy.size; i++)
257 g_free (dir_copy.list[i].fname);
258 g_free (dir_copy.list);
261 dir_copy.list = g_new0 (file_entry, size);
262 dir_copy.size = size;
266 /* --------------------------------------------------------------------------------------------- */
267 /*** public functions ****************************************************************************/
268 /* --------------------------------------------------------------------------------------------- */
271 unsorted (file_entry * a, file_entry * b)
273 (void) a;
274 (void) b;
275 return 0;
278 /* --------------------------------------------------------------------------------------------- */
281 sort_name (file_entry * a, file_entry * b)
283 int ad = MY_ISDIR (a);
284 int bd = MY_ISDIR (b);
286 if (ad == bd || panels_options.mix_all_files)
288 /* create key if does not exist, key will be freed after sorting */
289 if (a->sort_key == NULL)
290 a->sort_key = str_create_key_for_filename (a->fname, case_sensitive);
291 if (b->sort_key == NULL)
292 b->sort_key = str_create_key_for_filename (b->fname, case_sensitive);
294 return key_collate (a->sort_key, b->sort_key);
296 return bd - ad;
299 /* --------------------------------------------------------------------------------------------- */
302 sort_vers (file_entry * a, file_entry * b)
304 int ad = MY_ISDIR (a);
305 int bd = MY_ISDIR (b);
307 if (ad == bd || panels_options.mix_all_files)
309 return str_verscmp (a->fname, b->fname) * reverse;
311 else
313 return bd - ad;
317 /* --------------------------------------------------------------------------------------------- */
320 sort_ext (file_entry * a, file_entry * b)
322 int r;
323 int ad = MY_ISDIR (a);
324 int bd = MY_ISDIR (b);
326 if (ad == bd || panels_options.mix_all_files)
328 if (a->second_sort_key == NULL)
329 a->second_sort_key = str_create_key (extension (a->fname), case_sensitive);
330 if (b->second_sort_key == NULL)
331 b->second_sort_key = str_create_key (extension (b->fname), case_sensitive);
333 r = str_key_collate (a->second_sort_key, b->second_sort_key, case_sensitive);
334 if (r)
335 return r * reverse;
336 else
337 return sort_name (a, b);
339 else
340 return bd - ad;
343 /* --------------------------------------------------------------------------------------------- */
346 sort_time (file_entry * a, file_entry * b)
348 int ad = MY_ISDIR (a);
349 int bd = MY_ISDIR (b);
351 if (ad == bd || panels_options.mix_all_files)
353 int result = a->st.st_mtime < b->st.st_mtime ? -1 : a->st.st_mtime > b->st.st_mtime;
354 if (result != 0)
355 return result * reverse;
356 else
357 return sort_name (a, b);
359 else
360 return bd - ad;
363 /* --------------------------------------------------------------------------------------------- */
366 sort_ctime (file_entry * a, file_entry * b)
368 int ad = MY_ISDIR (a);
369 int bd = MY_ISDIR (b);
371 if (ad == bd || panels_options.mix_all_files)
373 int result = a->st.st_ctime < b->st.st_ctime ? -1 : a->st.st_ctime > b->st.st_ctime;
374 if (result != 0)
375 return result * reverse;
376 else
377 return sort_name (a, b);
379 else
380 return bd - ad;
383 /* --------------------------------------------------------------------------------------------- */
386 sort_atime (file_entry * a, file_entry * b)
388 int ad = MY_ISDIR (a);
389 int bd = MY_ISDIR (b);
391 if (ad == bd || panels_options.mix_all_files)
393 int result = a->st.st_atime < b->st.st_atime ? -1 : a->st.st_atime > b->st.st_atime;
394 if (result != 0)
395 return result * reverse;
396 else
397 return sort_name (a, b);
399 else
400 return bd - ad;
403 /* --------------------------------------------------------------------------------------------- */
406 sort_inode (file_entry * a, file_entry * b)
408 int ad = MY_ISDIR (a);
409 int bd = MY_ISDIR (b);
411 if (ad == bd || panels_options.mix_all_files)
412 return (a->st.st_ino - b->st.st_ino) * reverse;
413 else
414 return bd - ad;
417 /* --------------------------------------------------------------------------------------------- */
420 sort_size (file_entry * a, file_entry * b)
422 int ad = MY_ISDIR (a);
423 int bd = MY_ISDIR (b);
424 int result = 0;
426 if (ad != bd && !panels_options.mix_all_files)
427 return bd - ad;
429 result = a->st.st_size < b->st.st_size ? -1 : a->st.st_size > b->st.st_size;
430 if (result != 0)
431 return result * reverse;
432 else
433 return sort_name (a, b);
436 /* --------------------------------------------------------------------------------------------- */
438 void
439 do_sort (dir_list * list, sortfn * sort, int top, gboolean reverse_f, gboolean case_sensitive_f,
440 gboolean exec_first_f)
442 int dot_dot_found = 0;
444 if (top == 0)
445 return;
447 /* If there is an ".." entry the caller must take care to
448 ensure that it occupies the first list element. */
449 if (strcmp (list->list[0].fname, "..") == 0)
450 dot_dot_found = 1;
452 reverse = reverse_f ? -1 : 1;
453 case_sensitive = case_sensitive_f ? 1 : 0;
454 exec_first = exec_first_f;
455 qsort (&(list->list)[dot_dot_found], top + 1 - dot_dot_found, sizeof (file_entry), sort);
457 clean_sort_keys (list, dot_dot_found, top + 1 - dot_dot_found);
460 /* --------------------------------------------------------------------------------------------- */
462 void
463 clean_dir (dir_list * list, int count)
465 int i;
467 for (i = 0; i < count; i++)
469 g_free (list->list[i].fname);
470 list->list[i].fname = NULL;
474 /* --------------------------------------------------------------------------------------------- */
475 /** Used to set up a directory list when there is no access to a directory */
477 gboolean
478 set_zero_dir (dir_list * list)
480 /* Need to grow the *list? */
481 if (list->size == 0 && !grow_list (list))
482 return FALSE;
484 memset (&(list->list)[0], 0, sizeof (file_entry));
485 list->list[0].fnamelen = 2;
486 list->list[0].fname = g_strndup ("..", list->list[0].fnamelen);
487 list->list[0].f.link_to_dir = 0;
488 list->list[0].f.stale_link = 0;
489 list->list[0].f.dir_size_computed = 0;
490 list->list[0].f.marked = 0;
491 list->list[0].st.st_mode = 040755;
492 return TRUE;
495 /* --------------------------------------------------------------------------------------------- */
497 handle_path is a simplified handle_dirent. The difference is that
498 handle_path doesn't pay attention to panels_options.show_dot_files
499 and panels_options.show_backups.
500 Moreover handle_path can't be used with a filemask.
501 If you change handle_path then check also handle_dirent. */
502 /* Return values: -1 = failure, 0 = don't add, 1 = add to the list */
505 handle_path (dir_list * list, const char *path,
506 struct stat *buf1, int next_free, int *link_to_dir, int *stale_link)
508 vfs_path_t *vpath;
510 if (path[0] == '.' && path[1] == 0)
511 return 0;
512 if (path[0] == '.' && path[1] == '.' && path[2] == 0)
513 return 0;
515 vpath = vfs_path_from_str (path);
516 if (mc_lstat (vpath, buf1) == -1)
518 vfs_path_free (vpath);
519 return 0;
522 if (S_ISDIR (buf1->st_mode))
523 tree_store_mark_checked (path);
525 /* A link to a file or a directory? */
526 *link_to_dir = 0;
527 *stale_link = 0;
528 if (S_ISLNK (buf1->st_mode))
530 struct stat buf2;
531 if (mc_stat (vpath, &buf2) == 0)
532 *link_to_dir = S_ISDIR (buf2.st_mode) != 0;
533 else
534 *stale_link = 1;
537 vfs_path_free (vpath);
539 /* Need to grow the *list? */
540 if (next_free == list->size && !grow_list (list))
541 return -1;
543 return 1;
546 /* --------------------------------------------------------------------------------------------- */
549 do_load_dir (const vfs_path_t * vpath, dir_list * list, sortfn * sort, gboolean lc_reverse,
550 gboolean lc_case_sensitive, gboolean exec_ff, const char *fltr)
552 DIR *dirp;
553 struct dirent *dp;
554 int status, link_to_dir, stale_link;
555 int next_free = 0;
556 struct stat st;
557 char *path;
559 /* ".." (if any) must be the first entry in the list */
560 if (!set_zero_dir (list))
561 return next_free;
563 if (get_dotdot_dir_stat (vpath, &st))
564 list->list[next_free].st = st;
565 next_free++;
567 dirp = mc_opendir (vpath);
568 if (dirp == NULL)
570 message (D_ERROR, MSG_ERROR, _("Cannot read directory contents"));
571 return next_free;
574 tree_store_start_check (vpath);
576 /* Do not add a ".." entry to the root directory */
577 path = vfs_path_to_str (vpath);
578 if ((path[0] == PATH_SEP) && (path[1] == '\0'))
579 next_free--;
580 g_free (path);
582 while ((dp = mc_readdir (dirp)) != NULL)
584 status = handle_dirent (list, fltr, dp, &st, next_free, &link_to_dir, &stale_link);
585 if (status == 0)
586 continue;
587 if (status == -1)
588 goto ret;
590 list->list[next_free].fnamelen = NLENGTH (dp);
591 list->list[next_free].fname = g_strndup (dp->d_name, list->list[next_free].fnamelen);
592 list->list[next_free].f.marked = 0;
593 list->list[next_free].f.link_to_dir = link_to_dir;
594 list->list[next_free].f.stale_link = stale_link;
595 list->list[next_free].f.dir_size_computed = 0;
596 list->list[next_free].st = st;
597 list->list[next_free].sort_key = NULL;
598 list->list[next_free].second_sort_key = NULL;
599 next_free++;
601 if ((next_free & 31) == 0)
602 rotate_dash ();
605 if (next_free != 0)
606 do_sort (list, sort, next_free - 1, lc_reverse, lc_case_sensitive, exec_ff);
608 ret:
609 mc_closedir (dirp);
610 tree_store_end_check ();
611 return next_free;
615 /* --------------------------------------------------------------------------------------------- */
617 gboolean
618 if_link_is_exe (const vfs_path_t * full_name_vpath, const file_entry * file)
620 struct stat b;
622 if (S_ISLNK (file->st.st_mode) && mc_stat (full_name_vpath, &b) == 0)
623 return is_exe (b.st_mode);
624 return TRUE;
627 /* --------------------------------------------------------------------------------------------- */
628 /** If fltr is null, then it is a match */
631 do_reload_dir (const vfs_path_t * vpath, dir_list * list, sortfn * sort, int count,
632 gboolean lc_reverse, gboolean lc_case_sensitive, gboolean exec_ff, const char *fltr)
634 DIR *dirp;
635 struct dirent *dp;
636 int next_free = 0;
637 int i, status, link_to_dir, stale_link;
638 struct stat st;
639 int marked_cnt;
640 GHashTable *marked_files;
641 const char *tmp_path;
643 dirp = mc_opendir (vpath);
644 if (dirp == NULL)
646 message (D_ERROR, MSG_ERROR, _("Cannot read directory contents"));
647 clean_dir (list, count);
648 return set_zero_dir (list) ? 1 : 0;
651 tree_store_start_check (vpath);
653 marked_files = g_hash_table_new (g_str_hash, g_str_equal);
654 alloc_dir_copy (list->size);
655 for (marked_cnt = i = 0; i < count; i++)
657 dir_copy.list[i].fnamelen = list->list[i].fnamelen;
658 dir_copy.list[i].fname = list->list[i].fname;
659 dir_copy.list[i].f.marked = list->list[i].f.marked;
660 dir_copy.list[i].f.dir_size_computed = list->list[i].f.dir_size_computed;
661 dir_copy.list[i].f.link_to_dir = list->list[i].f.link_to_dir;
662 dir_copy.list[i].f.stale_link = list->list[i].f.stale_link;
663 dir_copy.list[i].sort_key = NULL;
664 dir_copy.list[i].second_sort_key = NULL;
665 if (list->list[i].f.marked)
667 g_hash_table_insert (marked_files, dir_copy.list[i].fname, &dir_copy.list[i]);
668 marked_cnt++;
672 /* Add ".." except to the root directory. The ".." entry
673 (if any) must be the first in the list. */
674 tmp_path = vfs_path_get_by_index (vpath, 0)->path;
675 if (!
676 (vfs_path_elements_count (vpath) == 1 && (tmp_path[0] == PATH_SEP)
677 && (tmp_path[1] == '\0')))
679 if (!set_zero_dir (list))
681 clean_dir (list, count);
682 clean_dir (&dir_copy, count);
683 return next_free;
686 if (get_dotdot_dir_stat (vpath, &st))
687 list->list[next_free].st = st;
689 next_free++;
692 while ((dp = mc_readdir (dirp)))
694 status = handle_dirent (list, fltr, dp, &st, next_free, &link_to_dir, &stale_link);
695 if (status == 0)
696 continue;
697 if (status == -1)
699 mc_closedir (dirp);
700 /* Norbert (Feb 12, 1997):
701 Just in case someone finds this memory leak:
702 -1 means big trouble (at the moment no memory left),
703 I don't bother with further cleanup because if one gets to
704 this point he will have more problems than a few memory
705 leaks and because one 'clean_dir' would not be enough (and
706 because I don't want to spent the time to make it working,
707 IMHO it's not worthwhile).
708 clean_dir (&dir_copy, count);
710 tree_store_end_check ();
711 g_hash_table_destroy (marked_files);
712 return next_free;
715 list->list[next_free].f.marked = 0;
718 * If we have marked files in the copy, scan through the copy
719 * to find matching file. Decrease number of remaining marks if
720 * we copied one.
722 if (marked_cnt > 0)
724 if ((g_hash_table_lookup (marked_files, dp->d_name)))
726 list->list[next_free].f.marked = 1;
727 marked_cnt--;
731 list->list[next_free].fnamelen = NLENGTH (dp);
732 list->list[next_free].fname = g_strndup (dp->d_name, list->list[next_free].fnamelen);
733 list->list[next_free].f.link_to_dir = link_to_dir;
734 list->list[next_free].f.stale_link = stale_link;
735 list->list[next_free].f.dir_size_computed = 0;
736 list->list[next_free].st = st;
737 list->list[next_free].sort_key = NULL;
738 list->list[next_free].second_sort_key = NULL;
739 next_free++;
740 if (!(next_free % 16))
741 rotate_dash ();
743 mc_closedir (dirp);
744 tree_store_end_check ();
745 g_hash_table_destroy (marked_files);
746 if (next_free)
748 do_sort (list, sort, next_free - 1, lc_reverse, lc_case_sensitive, exec_ff);
750 clean_dir (&dir_copy, count);
751 return next_free;
754 /* --------------------------------------------------------------------------------------------- */