Return values of following functions are constants now:
[midnight-commander.git] / src / filemanager / dir.c
blob34ac157557699168e3a81fed211f5da837b9e876
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 vfs_path_t * vpath, struct stat *st)
202 gboolean ret = FALSE;
204 if ((vpath != NULL) && (st != NULL))
206 const char *path;
208 path = vfs_path_get_by_index (vpath, 0)->path;
209 if (path != NULL && *path != '\0')
211 vfs_path_t *tmp_vpath;
212 struct stat s;
214 tmp_vpath = vfs_path_append_new (vpath, "..", NULL);
215 ret = mc_stat (tmp_vpath, &s) == 0;
216 vfs_path_free (tmp_vpath);
217 *st = s;
221 return ret;
224 /* --------------------------------------------------------------------------------------------- */
226 static void
227 alloc_dir_copy (int size)
229 if (dir_copy.size < size)
231 if (dir_copy.list)
233 int i;
234 for (i = 0; i < dir_copy.size; i++)
235 g_free (dir_copy.list[i].fname);
236 g_free (dir_copy.list);
239 dir_copy.list = g_new0 (file_entry, size);
240 dir_copy.size = size;
244 /* --------------------------------------------------------------------------------------------- */
245 /*** public functions ****************************************************************************/
246 /* --------------------------------------------------------------------------------------------- */
249 unsorted (file_entry * a, file_entry * b)
251 (void) a;
252 (void) b;
253 return 0;
256 /* --------------------------------------------------------------------------------------------- */
259 sort_name (file_entry * a, file_entry * b)
261 int ad = MY_ISDIR (a);
262 int bd = MY_ISDIR (b);
264 if (ad == bd || panels_options.mix_all_files)
266 /* create key if does not exist, key will be freed after sorting */
267 if (a->sort_key == NULL)
268 a->sort_key = str_create_key_for_filename (a->fname, case_sensitive);
269 if (b->sort_key == NULL)
270 b->sort_key = str_create_key_for_filename (b->fname, case_sensitive);
272 return key_collate (a->sort_key, b->sort_key);
274 return bd - ad;
277 /* --------------------------------------------------------------------------------------------- */
280 sort_vers (file_entry * a, file_entry * b)
282 int ad = MY_ISDIR (a);
283 int bd = MY_ISDIR (b);
285 if (ad == bd || panels_options.mix_all_files)
287 return str_verscmp (a->fname, b->fname) * reverse;
289 else
291 return bd - ad;
295 /* --------------------------------------------------------------------------------------------- */
298 sort_ext (file_entry * a, file_entry * b)
300 int r;
301 int ad = MY_ISDIR (a);
302 int bd = MY_ISDIR (b);
304 if (ad == bd || panels_options.mix_all_files)
306 if (a->second_sort_key == NULL)
307 a->second_sort_key = str_create_key (extension (a->fname), case_sensitive);
308 if (b->second_sort_key == NULL)
309 b->second_sort_key = str_create_key (extension (b->fname), case_sensitive);
311 r = str_key_collate (a->second_sort_key, b->second_sort_key, case_sensitive);
312 if (r)
313 return r * reverse;
314 else
315 return sort_name (a, b);
317 else
318 return bd - ad;
321 /* --------------------------------------------------------------------------------------------- */
324 sort_time (file_entry * a, file_entry * b)
326 int ad = MY_ISDIR (a);
327 int bd = MY_ISDIR (b);
329 if (ad == bd || panels_options.mix_all_files)
331 int result = a->st.st_mtime < b->st.st_mtime ? -1 : a->st.st_mtime > b->st.st_mtime;
332 if (result != 0)
333 return result * reverse;
334 else
335 return sort_name (a, b);
337 else
338 return bd - ad;
341 /* --------------------------------------------------------------------------------------------- */
344 sort_ctime (file_entry * a, file_entry * b)
346 int ad = MY_ISDIR (a);
347 int bd = MY_ISDIR (b);
349 if (ad == bd || panels_options.mix_all_files)
351 int result = a->st.st_ctime < b->st.st_ctime ? -1 : a->st.st_ctime > b->st.st_ctime;
352 if (result != 0)
353 return result * reverse;
354 else
355 return sort_name (a, b);
357 else
358 return bd - ad;
361 /* --------------------------------------------------------------------------------------------- */
364 sort_atime (file_entry * a, file_entry * b)
366 int ad = MY_ISDIR (a);
367 int bd = MY_ISDIR (b);
369 if (ad == bd || panels_options.mix_all_files)
371 int result = a->st.st_atime < b->st.st_atime ? -1 : a->st.st_atime > b->st.st_atime;
372 if (result != 0)
373 return result * reverse;
374 else
375 return sort_name (a, b);
377 else
378 return bd - ad;
381 /* --------------------------------------------------------------------------------------------- */
384 sort_inode (file_entry * a, file_entry * b)
386 int ad = MY_ISDIR (a);
387 int bd = MY_ISDIR (b);
389 if (ad == bd || panels_options.mix_all_files)
390 return (a->st.st_ino - b->st.st_ino) * reverse;
391 else
392 return bd - ad;
395 /* --------------------------------------------------------------------------------------------- */
398 sort_size (file_entry * a, file_entry * b)
400 int ad = MY_ISDIR (a);
401 int bd = MY_ISDIR (b);
402 int result = 0;
404 if (ad != bd && !panels_options.mix_all_files)
405 return bd - ad;
407 result = a->st.st_size < b->st.st_size ? -1 : a->st.st_size > b->st.st_size;
408 if (result != 0)
409 return result * reverse;
410 else
411 return sort_name (a, b);
414 /* --------------------------------------------------------------------------------------------- */
416 void
417 do_sort (dir_list * list, sortfn * sort, int top, gboolean reverse_f, gboolean case_sensitive_f,
418 gboolean exec_first_f)
420 int dot_dot_found = 0;
422 if (top == 0)
423 return;
425 /* If there is an ".." entry the caller must take care to
426 ensure that it occupies the first list element. */
427 if (strcmp (list->list[0].fname, "..") == 0)
428 dot_dot_found = 1;
430 reverse = reverse_f ? -1 : 1;
431 case_sensitive = case_sensitive_f ? 1 : 0;
432 exec_first = exec_first_f;
433 qsort (&(list->list)[dot_dot_found], top + 1 - dot_dot_found, sizeof (file_entry), sort);
435 clean_sort_keys (list, dot_dot_found, top + 1 - dot_dot_found);
438 /* --------------------------------------------------------------------------------------------- */
440 void
441 clean_dir (dir_list * list, int count)
443 int i;
445 for (i = 0; i < count; i++)
447 g_free (list->list[i].fname);
448 list->list[i].fname = NULL;
452 /* --------------------------------------------------------------------------------------------- */
453 /** Used to set up a directory list when there is no access to a directory */
455 gboolean
456 set_zero_dir (dir_list * list)
458 /* Need to grow the *list? */
459 if (list->size == 0)
461 list->list = g_try_realloc (list->list, sizeof (file_entry) * (list->size + RESIZE_STEPS));
462 if (list->list == NULL)
463 return FALSE;
465 list->size += RESIZE_STEPS;
468 memset (&(list->list)[0], 0, sizeof (file_entry));
469 list->list[0].fnamelen = 2;
470 list->list[0].fname = g_strndup ("..", list->list[0].fnamelen);
471 list->list[0].f.link_to_dir = 0;
472 list->list[0].f.stale_link = 0;
473 list->list[0].f.dir_size_computed = 0;
474 list->list[0].f.marked = 0;
475 list->list[0].st.st_mode = 040755;
476 return TRUE;
479 /* --------------------------------------------------------------------------------------------- */
481 handle_path is a simplified handle_dirent. The difference is that
482 handle_path doesn't pay attention to panels_options.show_dot_files
483 and panels_options.show_backups.
484 Moreover handle_path can't be used with a filemask.
485 If you change handle_path then check also handle_dirent. */
486 /* Return values: -1 = failure, 0 = don't add, 1 = add to the list */
489 handle_path (dir_list * list, const char *path,
490 struct stat *buf1, int next_free, int *link_to_dir, int *stale_link)
492 vfs_path_t *vpath;
494 if (path[0] == '.' && path[1] == 0)
495 return 0;
496 if (path[0] == '.' && path[1] == '.' && path[2] == 0)
497 return 0;
499 vpath = vfs_path_from_str (path);
500 if (mc_lstat (vpath, buf1) == -1)
502 vfs_path_free (vpath);
503 return 0;
506 if (S_ISDIR (buf1->st_mode))
507 tree_store_mark_checked (path);
509 /* A link to a file or a directory? */
510 *link_to_dir = 0;
511 *stale_link = 0;
512 if (S_ISLNK (buf1->st_mode))
514 struct stat buf2;
515 if (mc_stat (vpath, &buf2) == 0)
516 *link_to_dir = S_ISDIR (buf2.st_mode) != 0;
517 else
518 *stale_link = 1;
521 vfs_path_free (vpath);
523 /* Need to grow the *list? */
524 if (next_free == list->size)
526 list->list = g_try_realloc (list->list, sizeof (file_entry) * (list->size + RESIZE_STEPS));
527 return -1;
528 list->size += RESIZE_STEPS;
530 return 1;
533 /* --------------------------------------------------------------------------------------------- */
536 do_load_dir (const char *path, dir_list * list, sortfn * sort, gboolean lc_reverse,
537 gboolean lc_case_sensitive, gboolean exec_ff, const char *fltr)
539 DIR *dirp;
540 struct dirent *dp;
541 int status, link_to_dir, stale_link;
542 int next_free = 0;
543 struct stat st;
544 vfs_path_t *vpath;
546 /* ".." (if any) must be the first entry in the list */
547 if (!set_zero_dir (list))
548 return next_free;
550 vpath = vfs_path_from_str (path);
551 if (get_dotdot_dir_stat (vpath, &st))
552 list->list[next_free].st = st;
553 next_free++;
555 dirp = mc_opendir (vpath);
556 if (dirp == NULL)
558 message (D_ERROR, MSG_ERROR, _("Cannot read directory contents"));
559 goto ret;
562 tree_store_start_check (vpath);
564 /* Do not add a ".." entry to the root directory */
565 if ((path[0] == PATH_SEP) && (path[1] == '\0'))
566 next_free--;
568 while ((dp = mc_readdir (dirp)))
570 status = handle_dirent (list, fltr, dp, &st, next_free, &link_to_dir, &stale_link);
571 if (status == 0)
572 continue;
573 if (status == -1)
575 tree_store_end_check ();
576 mc_closedir (dirp);
577 goto ret;
579 list->list[next_free].fnamelen = NLENGTH (dp);
580 list->list[next_free].fname = g_strndup (dp->d_name, list->list[next_free].fnamelen);
581 list->list[next_free].f.marked = 0;
582 list->list[next_free].f.link_to_dir = link_to_dir;
583 list->list[next_free].f.stale_link = stale_link;
584 list->list[next_free].f.dir_size_computed = 0;
585 list->list[next_free].st = st;
586 list->list[next_free].sort_key = NULL;
587 list->list[next_free].second_sort_key = NULL;
588 next_free++;
590 if ((next_free & 31) == 0)
591 rotate_dash ();
594 if (next_free != 0)
595 do_sort (list, sort, next_free - 1, lc_reverse, lc_case_sensitive, exec_ff);
597 mc_closedir (dirp);
598 tree_store_end_check ();
599 ret:
600 vfs_path_free (vpath);
601 return next_free;
605 /* --------------------------------------------------------------------------------------------- */
607 gboolean
608 if_link_is_exe (const vfs_path_t * full_name_vpath, const file_entry * file)
610 struct stat b;
612 if (S_ISLNK (file->st.st_mode) && mc_stat (full_name_vpath, &b) == 0)
613 return is_exe (b.st_mode);
614 return TRUE;
617 /* --------------------------------------------------------------------------------------------- */
618 /** If fltr is null, then it is a match */
621 do_reload_dir (const vfs_path_t * vpath, dir_list * list, sortfn * sort, int count,
622 gboolean lc_reverse, gboolean lc_case_sensitive, gboolean exec_ff, const char *fltr)
624 DIR *dirp;
625 struct dirent *dp;
626 int next_free = 0;
627 int i, status, link_to_dir, stale_link;
628 struct stat st;
629 int marked_cnt;
630 GHashTable *marked_files;
631 const char *tmp_path;
633 dirp = mc_opendir (vpath);
634 if (dirp == NULL)
636 message (D_ERROR, MSG_ERROR, _("Cannot read directory contents"));
637 clean_dir (list, count);
638 return set_zero_dir (list) ? 1 : 0;
641 tree_store_start_check (vpath);
643 marked_files = g_hash_table_new (g_str_hash, g_str_equal);
644 alloc_dir_copy (list->size);
645 for (marked_cnt = i = 0; i < count; i++)
647 dir_copy.list[i].fnamelen = list->list[i].fnamelen;
648 dir_copy.list[i].fname = list->list[i].fname;
649 dir_copy.list[i].f.marked = list->list[i].f.marked;
650 dir_copy.list[i].f.dir_size_computed = list->list[i].f.dir_size_computed;
651 dir_copy.list[i].f.link_to_dir = list->list[i].f.link_to_dir;
652 dir_copy.list[i].f.stale_link = list->list[i].f.stale_link;
653 dir_copy.list[i].sort_key = NULL;
654 dir_copy.list[i].second_sort_key = NULL;
655 if (list->list[i].f.marked)
657 g_hash_table_insert (marked_files, dir_copy.list[i].fname, &dir_copy.list[i]);
658 marked_cnt++;
662 /* Add ".." except to the root directory. The ".." entry
663 (if any) must be the first in the list. */
664 tmp_path = vfs_path_get_by_index (vpath, 0)->path;
665 if (!
666 (vfs_path_elements_count (vpath) == 1 && (tmp_path[0] == PATH_SEP)
667 && (tmp_path[1] == '\0')))
669 if (!set_zero_dir (list))
671 clean_dir (list, count);
672 clean_dir (&dir_copy, count);
673 return next_free;
676 if (get_dotdot_dir_stat (vpath, &st))
677 list->list[next_free].st = st;
679 next_free++;
682 while ((dp = mc_readdir (dirp)))
684 status = handle_dirent (list, fltr, dp, &st, next_free, &link_to_dir, &stale_link);
685 if (status == 0)
686 continue;
687 if (status == -1)
689 mc_closedir (dirp);
690 /* Norbert (Feb 12, 1997):
691 Just in case someone finds this memory leak:
692 -1 means big trouble (at the moment no memory left),
693 I don't bother with further cleanup because if one gets to
694 this point he will have more problems than a few memory
695 leaks and because one 'clean_dir' would not be enough (and
696 because I don't want to spent the time to make it working,
697 IMHO it's not worthwhile).
698 clean_dir (&dir_copy, count);
700 tree_store_end_check ();
701 g_hash_table_destroy (marked_files);
702 return next_free;
705 list->list[next_free].f.marked = 0;
708 * If we have marked files in the copy, scan through the copy
709 * to find matching file. Decrease number of remaining marks if
710 * we copied one.
712 if (marked_cnt > 0)
714 if ((g_hash_table_lookup (marked_files, dp->d_name)))
716 list->list[next_free].f.marked = 1;
717 marked_cnt--;
721 list->list[next_free].fnamelen = NLENGTH (dp);
722 list->list[next_free].fname = g_strndup (dp->d_name, list->list[next_free].fnamelen);
723 list->list[next_free].f.link_to_dir = link_to_dir;
724 list->list[next_free].f.stale_link = stale_link;
725 list->list[next_free].f.dir_size_computed = 0;
726 list->list[next_free].st = st;
727 list->list[next_free].sort_key = NULL;
728 list->list[next_free].second_sort_key = NULL;
729 next_free++;
730 if (!(next_free % 16))
731 rotate_dash ();
733 mc_closedir (dirp);
734 tree_store_end_check ();
735 g_hash_table_destroy (marked_files);
736 if (next_free)
738 do_sort (list, sort, next_free - 1, lc_reverse, lc_case_sensitive, exec_ff);
740 clean_dir (&dir_copy, count);
741 return next_free;
744 /* --------------------------------------------------------------------------------------------- */