Ticket #1990: code cleanup before 4.7.0.2 release.
[midnight-commander/osp/fridri1.git] / src / dir.c
blob034467b886fc29080f22ad1f20eeb1fab89de3ca
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_ext (file_entry *a, file_entry *b)
103 int r;
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);
114 if (r)
115 return r * reverse;
116 else
117 return sort_name (a, b);
118 } else
119 return bd-ad;
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;
131 if (result != 0)
132 return result * reverse;
133 else
134 return sort_name (a, b);
136 else
137 return bd-ad;
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;
149 if (result != 0)
150 return result * reverse;
151 else
152 return sort_name (a, b);
154 else
155 return bd-ad;
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;
167 if (result != 0)
168 return result * reverse;
169 else
170 return sort_name (a, b);
172 else
173 return bd-ad;
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;
184 else
185 return bd-ad;
189 sort_size (file_entry *a, file_entry *b)
191 int ad = MY_ISDIR (a);
192 int bd = MY_ISDIR (b);
193 int result = 0;
195 if (ad != bd && !mix_all_files)
196 return bd - ad;
198 result = a->st.st_size < b->st.st_size ? -1 :
199 a->st.st_size > b->st.st_size;
200 if (result != 0)
201 return result * reverse;
202 else
203 return sort_name (a, b);
206 /* clear keys, should be call after sorting is finished */
207 static void
208 clean_sort_keys (dir_list *list, int start, int count)
210 int i;
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;
221 void
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;
226 if (top == 0)
227 return;
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, ".."))
232 dot_dot_found = 1;
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);
243 void
244 clean_dir (dir_list *list, int count)
246 int i;
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 */
255 gboolean
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)
263 return FALSE;
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;
276 return TRUE;
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 */
281 static int
282 handle_dirent (dir_list *list, const char *fltr, struct dirent *dp,
283 struct stat *buf1, int next_free, int *link_to_dir,
284 int *stale_link)
286 if (dp->d_name[0] == '.' && dp->d_name[1] == 0)
287 return 0;
288 if (dp->d_name[0] == '.' && dp->d_name[1] == '.' && dp->d_name[2] == 0)
289 return 0;
290 if (!show_dot_files && (dp->d_name[0] == '.'))
291 return 0;
292 if (!show_backups && dp->d_name[NLENGTH (dp) - 1] == '~')
293 return 0;
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? */
307 *link_to_dir = 0;
308 *stale_link = 0;
309 if (S_ISLNK (buf1->st_mode)) {
310 struct stat buf2;
311 if (!mc_stat (dp->d_name, &buf2))
312 *link_to_dir = S_ISDIR (buf2.st_mode) != 0;
313 else
314 *stale_link = 1;
316 if (!(S_ISDIR (buf1->st_mode) || *link_to_dir) && (fltr != NULL)
317 && !mc_search (fltr, dp->d_name, MC_SEARCH_T_GLOB))
318 return 0;
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)
325 return -1;
326 list->size += RESIZE_STEPS;
328 return 1;
331 /* get info about ".." */
332 static gboolean
333 get_dotdot_dir_stat (const char *path, struct stat *st)
335 gboolean ret = FALSE;
337 if ((path != NULL) && (path[0] != '\0') && (st != NULL)) {
338 char *dotdot_dir;
339 struct stat s;
341 dotdot_dir = g_strdup_printf ("%s/../", path);
342 canonicalize_pathname (dotdot_dir);
343 ret = mc_stat (dotdot_dir, &s) == 0;
344 g_free (dotdot_dir);
345 *st = s;
348 return ret;
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,
359 int *stale_link)
361 if (path [0] == '.' && path [1] == 0)
362 return 0;
363 if (path [0] == '.' && path [1] == '.' && path [2] == 0)
364 return 0;
365 if (mc_lstat (path, buf1) == -1)
366 return 0;
368 if (S_ISDIR (buf1->st_mode))
369 tree_store_mark_checked (path);
371 /* A link to a file or a directory? */
372 *link_to_dir = 0;
373 *stale_link = 0;
374 if (S_ISLNK(buf1->st_mode)){
375 struct stat buf2;
376 if (!mc_stat (path, &buf2))
377 *link_to_dir = S_ISDIR(buf2.st_mode) != 0;
378 else
379 *stale_link = 1;
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)
387 return -1;
388 list->size += RESIZE_STEPS;
390 return 1;
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)
397 DIR *dirp;
398 struct dirent *dp;
399 int status, link_to_dir, stale_link;
400 int next_free = 0;
401 struct stat st;
403 /* ".." (if any) must be the first entry in the list */
404 if (!set_zero_dir (list))
405 return next_free;
407 if (get_dotdot_dir_stat (path, &st))
408 list->list[next_free].st = st;
409 next_free++;
411 dirp = mc_opendir (path);
412 if (!dirp) {
413 message (D_ERROR, MSG_ERROR, _("Cannot read directory contents"));
414 return next_free;
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'))
421 next_free--;
423 while ((dp = mc_readdir (dirp))) {
424 status =
425 handle_dirent (list, fltr, dp, &st, next_free, &link_to_dir,
426 &stale_link);
427 if (status == 0)
428 continue;
429 if (status == -1) {
430 tree_store_end_check ();
431 mc_closedir (dirp);
432 return next_free;
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;
443 next_free++;
445 if ((next_free & 31) == 0)
446 rotate_dash ();
449 if (next_free != 0)
450 do_sort (list, sort, next_free - 1, lc_reverse, lc_case_sensitive, exec_ff);
452 mc_closedir (dirp);
453 tree_store_end_check ();
454 return next_free;
458 link_isdir (const file_entry *file)
460 if (file->f.link_to_dir)
461 return 1;
462 else
463 return 0;
467 if_link_is_exe (const char *full_name, const file_entry *file)
469 struct stat b;
471 if (S_ISLNK (file->st.st_mode) && !mc_stat (full_name, &b)) {
472 return is_exe (b.st_mode);
473 } else
474 return 1;
477 static dir_list dir_copy = { 0, 0 };
479 static void
480 alloc_dir_copy (int size)
482 if (dir_copy.size < size) {
483 if (dir_copy.list) {
484 int i;
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)
500 DIR *dirp;
501 struct dirent *dp;
502 int next_free = 0;
503 int i, status, link_to_dir, stale_link;
504 struct stat st;
505 int marked_cnt;
506 GHashTable *marked_files;
508 dirp = mc_opendir (path);
509 if (!dirp) {
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,
530 &dir_copy.list[i]);
531 marked_cnt++;
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);
541 return next_free;
544 if (get_dotdot_dir_stat (path, &st))
545 list->list[next_free].st = st;
547 next_free++;
550 while ((dp = mc_readdir (dirp))) {
551 status =
552 handle_dirent (list, fltr, dp, &st, next_free, &link_to_dir,
553 &stale_link);
554 if (status == 0)
555 continue;
556 if (status == -1) {
557 mc_closedir (dirp);
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);
570 return next_free;
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
578 * we copied one.
580 if (marked_cnt > 0) {
581 if ((g_hash_table_lookup (marked_files, dp->d_name))) {
582 list->list[next_free].f.marked = 1;
583 marked_cnt--;
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;
595 next_free++;
596 if (!(next_free % 16))
597 rotate_dash ();
599 mc_closedir (dirp);
600 tree_store_end_check ();
601 g_hash_table_destroy (marked_files);
602 if (next_free) {
603 do_sort (list, sort, next_free - 1, rev, lc_case_sensitive, exec_ff);
605 clean_dir (&dir_copy, count);
606 return next_free;