Merge branch '2121_dir_symlink'
[kaloumi3.git] / src / dir.c
blob1ace0b738342da9f5b74b3c6d06f7590a76a2a94
1 /* Directory routines
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. */
19 /** \file dir.c
20 * \brief Source: directory routines
23 #include <config.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <sys/stat.h>
30 #include "lib/global.h"
31 #include "lib/tty/tty.h"
32 #include "lib/search.h"
33 #include "lib/vfs/mc-vfs/vfs.h"
34 #include "lib/fs.h"
35 #include "lib/strutil.h"
37 #include "wtools.h"
38 #include "treestore.h"
39 #include "dir.h"
41 /* If true show files starting with a dot */
42 int show_dot_files = 1;
44 /* If true show files ending in ~ */
45 int show_backups = 1;
47 /* If false then directories are shown separately from files */
48 int mix_all_files = 0;
50 /* Reverse flag */
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 },
73 int
74 unsorted (file_entry *a, file_entry *b)
76 (void) a;
77 (void) b;
78 return 0;
81 int
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)
95 * reverse;
97 return bd - ad;
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;
108 } else {
109 return bd - ad;
114 sort_ext (file_entry *a, file_entry *b)
116 int r;
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);
127 if (r)
128 return r * reverse;
129 else
130 return sort_name (a, b);
131 } else
132 return bd-ad;
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;
144 if (result != 0)
145 return result * reverse;
146 else
147 return sort_name (a, b);
149 else
150 return bd-ad;
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;
162 if (result != 0)
163 return result * reverse;
164 else
165 return sort_name (a, b);
167 else
168 return bd-ad;
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;
180 if (result != 0)
181 return result * reverse;
182 else
183 return sort_name (a, b);
185 else
186 return bd-ad;
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;
197 else
198 return bd-ad;
202 sort_size (file_entry *a, file_entry *b)
204 int ad = MY_ISDIR (a);
205 int bd = MY_ISDIR (b);
206 int result = 0;
208 if (ad != bd && !mix_all_files)
209 return bd - ad;
211 result = a->st.st_size < b->st.st_size ? -1 :
212 a->st.st_size > b->st.st_size;
213 if (result != 0)
214 return result * reverse;
215 else
216 return sort_name (a, b);
219 /* clear keys, should be call after sorting is finished */
220 static void
221 clean_sort_keys (dir_list *list, int start, int count)
223 int i;
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;
234 void
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;
239 if (top == 0)
240 return;
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, ".."))
245 dot_dot_found = 1;
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);
256 void
257 clean_dir (dir_list *list, int count)
259 int i;
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 */
268 gboolean
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)
276 return FALSE;
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;
289 return TRUE;
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 */
294 static int
295 handle_dirent (dir_list *list, const char *fltr, struct dirent *dp,
296 struct stat *buf1, int next_free, int *link_to_dir,
297 int *stale_link)
299 if (dp->d_name[0] == '.' && dp->d_name[1] == 0)
300 return 0;
301 if (dp->d_name[0] == '.' && dp->d_name[1] == '.' && dp->d_name[2] == 0)
302 return 0;
303 if (!show_dot_files && (dp->d_name[0] == '.'))
304 return 0;
305 if (!show_backups && dp->d_name[NLENGTH (dp) - 1] == '~')
306 return 0;
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? */
320 *link_to_dir = 0;
321 *stale_link = 0;
322 if (S_ISLNK (buf1->st_mode)) {
323 struct stat buf2;
324 if (!mc_stat (dp->d_name, &buf2))
325 *link_to_dir = S_ISDIR (buf2.st_mode) != 0;
326 else
327 *stale_link = 1;
329 if (!(S_ISDIR (buf1->st_mode) || *link_to_dir) && (fltr != NULL)
330 && !mc_search (fltr, dp->d_name, MC_SEARCH_T_GLOB))
331 return 0;
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)
338 return -1;
339 list->size += RESIZE_STEPS;
341 return 1;
344 /* get info about ".." */
345 static gboolean
346 get_dotdot_dir_stat (const char *path, struct stat *st)
348 gboolean ret = FALSE;
350 if ((path != NULL) && (path[0] != '\0') && (st != NULL)) {
351 char *dotdot_dir;
352 struct stat s;
354 dotdot_dir = g_strdup_printf ("%s/../", path);
355 canonicalize_pathname (dotdot_dir);
356 ret = mc_stat (dotdot_dir, &s) == 0;
357 g_free (dotdot_dir);
358 *st = s;
361 return ret;
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,
372 int *stale_link)
374 if (path [0] == '.' && path [1] == 0)
375 return 0;
376 if (path [0] == '.' && path [1] == '.' && path [2] == 0)
377 return 0;
378 if (mc_lstat (path, buf1) == -1)
379 return 0;
381 if (S_ISDIR (buf1->st_mode))
382 tree_store_mark_checked (path);
384 /* A link to a file or a directory? */
385 *link_to_dir = 0;
386 *stale_link = 0;
387 if (S_ISLNK(buf1->st_mode)){
388 struct stat buf2;
389 if (!mc_stat (path, &buf2))
390 *link_to_dir = S_ISDIR(buf2.st_mode) != 0;
391 else
392 *stale_link = 1;
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)
400 return -1;
401 list->size += RESIZE_STEPS;
403 return 1;
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)
410 DIR *dirp;
411 struct dirent *dp;
412 int status, link_to_dir, stale_link;
413 int next_free = 0;
414 struct stat st;
416 /* ".." (if any) must be the first entry in the list */
417 if (!set_zero_dir (list))
418 return next_free;
420 if (get_dotdot_dir_stat (path, &st))
421 list->list[next_free].st = st;
422 next_free++;
424 dirp = mc_opendir (path);
425 if (!dirp) {
426 message (D_ERROR, MSG_ERROR, _("Cannot read directory contents"));
427 return next_free;
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'))
434 next_free--;
436 while ((dp = mc_readdir (dirp))) {
437 status =
438 handle_dirent (list, fltr, dp, &st, next_free, &link_to_dir,
439 &stale_link);
440 if (status == 0)
441 continue;
442 if (status == -1) {
443 tree_store_end_check ();
444 mc_closedir (dirp);
445 return next_free;
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;
456 next_free++;
458 if ((next_free & 31) == 0)
459 rotate_dash ();
462 if (next_free != 0)
463 do_sort (list, sort, next_free - 1, lc_reverse, lc_case_sensitive, exec_ff);
465 mc_closedir (dirp);
466 tree_store_end_check ();
467 return next_free;
471 link_isdir (const file_entry *file)
473 if (file->f.link_to_dir)
474 return 1;
475 else
476 return 0;
480 if_link_is_exe (const char *full_name, const file_entry *file)
482 struct stat b;
484 if (S_ISLNK (file->st.st_mode) && !mc_stat (full_name, &b)) {
485 return is_exe (b.st_mode);
486 } else
487 return 1;
490 static dir_list dir_copy = { 0, 0 };
492 static void
493 alloc_dir_copy (int size)
495 if (dir_copy.size < size) {
496 if (dir_copy.list) {
497 int i;
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)
513 DIR *dirp;
514 struct dirent *dp;
515 int next_free = 0;
516 int i, status, link_to_dir, stale_link;
517 struct stat st;
518 int marked_cnt;
519 GHashTable *marked_files;
521 dirp = mc_opendir (path);
522 if (!dirp) {
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,
543 &dir_copy.list[i]);
544 marked_cnt++;
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);
554 return next_free;
557 if (get_dotdot_dir_stat (path, &st))
558 list->list[next_free].st = st;
560 next_free++;
563 while ((dp = mc_readdir (dirp))) {
564 status =
565 handle_dirent (list, fltr, dp, &st, next_free, &link_to_dir,
566 &stale_link);
567 if (status == 0)
568 continue;
569 if (status == -1) {
570 mc_closedir (dirp);
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);
583 return next_free;
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
591 * we copied one.
593 if (marked_cnt > 0) {
594 if ((g_hash_table_lookup (marked_files, dp->d_name))) {
595 list->list[next_free].f.marked = 1;
596 marked_cnt--;
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;
608 next_free++;
609 if (!(next_free % 16))
610 rotate_dash ();
612 mc_closedir (dirp);
613 tree_store_end_check ();
614 g_hash_table_destroy (marked_files);
615 if (next_free) {
616 do_sort (list, sort, next_free - 1, rev, lc_case_sensitive, exec_ff);
618 clean_dir (&dir_copy, count);
619 return next_free;