summaryrefslogtreecommitdiffstats
path: root/lib/vfs/vfs.c
blob: 18d15ed3581a982a330f94434610313c1be45fee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
/*
   Virtual File System switch code

   Copyright (C) 1995-2024
   Free Software Foundation, Inc.

   Written by: 1995 Miguel de Icaza
   Jakub Jelinek, 1995
   Pavel Machek, 1998
   Slava Zanko <slavazanko@gmail.com>, 2011-2013
   Andrew Borodin <aborodin@vmail.ru>, 2011-2022

   This file is part of the Midnight Commander.

   The Midnight Commander is free software: you can redistribute it
   and/or modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation, either version 3 of the License,
   or (at your option) any later version.

   The Midnight Commander is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * \file
 * \brief Source: Virtual File System switch code
 * \author Miguel de Icaza
 * \author Jakub Jelinek
 * \author Pavel Machek
 * \date 1995, 1998
 * \warning functions like extfs_lstat() have right to destroy any
 * strings you pass to them. This is actually ok as you g_strdup what
 * you are passing to them, anyway; still, beware.
 *
 * Namespace: exports *many* functions with vfs_ prefix; exports
 * parse_ls_lga and friends which do not have that prefix.
 */

#include <config.h>

#include <errno.h>
#include <stdlib.h>

#ifdef __linux__
#ifdef HAVE_LINUX_FS_H
#include <linux/fs.h>
#endif /* HAVE_LINUX_FS_H */
#ifdef HAVE_SYS_IOCTL_H
#include <sys/ioctl.h>
#endif /* HAVE_SYS_IOCTL_H */
#endif /* __linux__ */

#include "lib/global.h"
#include "lib/strutil.h"
#include "lib/util.h"
#include "lib/widget.h"         /* message() */
#include "lib/event.h"

#ifdef HAVE_CHARSET
#include "lib/charsets.h"
#endif

#include "vfs.h"
#include "utilvfs.h"
#include "gc.h"

/* TODO: move it to the separate .h */
extern struct vfs_dirent *mc_readdir_result;
extern GPtrArray *vfs__classes_list;
extern GString *vfs_str_buffer;
extern vfs_class *current_vfs;

/*** global variables ****************************************************************************/

struct vfs_dirent *mc_readdir_result = NULL;
GPtrArray *vfs__classes_list = NULL;
GString *vfs_str_buffer = NULL;
vfs_class *current_vfs = NULL;

/*** file scope macro definitions ****************************************************************/

#define VFS_FIRST_HANDLE 100

/*** file scope type declarations ****************************************************************/

struct vfs_openfile
{
    int handle;
    vfs_class *vclass;
    void *fsinfo;
};

/*** forward declarations (file scope functions) *************************************************/

/*** file scope variables ************************************************************************/

/** They keep track of the current directory */
static vfs_path_t *current_path = NULL;

static GPtrArray *vfs_openfiles = NULL;
static long vfs_free_handle_list = -1;

/* --------------------------------------------------------------------------------------------- */
/*** file scope functions ************************************************************************/
/* --------------------------------------------------------------------------------------------- */
/* now used only by vfs_translate_path, but could be used in other vfs 
 * plugin to automatic detect encoding
 * path - path to translate
 * size - how many bytes from path translate
 * defcnv - converter, that is used as default, when path does not contain any
 *          #enc: substring
 * buffer - used to store result of translation
 */

static estr_t
_vfs_translate_path (const char *path, int size, GIConv defcnv, GString * buffer)
{
    estr_t state = ESTR_SUCCESS;
#ifdef HAVE_CHARSET
    const char *semi;

    if (size == 0)
        return ESTR_SUCCESS;

    size = (size > 0) ? size : (signed int) strlen (path);

    /* try found /#enc: */
    semi = g_strrstr_len (path, size, VFS_ENCODING_PREFIX);
    if (semi != NULL && (semi == path || IS_PATH_SEP (semi[-1])))
    {
        char encoding[16];
        const char *slash;
        GIConv coder = INVALID_CONV;
        int ms;

        /* first must be translated part before #enc: */
        ms = semi - path;

        state = _vfs_translate_path (path, ms, defcnv, buffer);

        if (state != ESTR_SUCCESS)
            return state;

        /* now can be translated part after #enc: */
        semi += strlen (VFS_ENCODING_PREFIX);   /* skip "#enc:" */
        slash = strchr (semi, PATH_SEP);
        /* ignore slashes after size; */
        if (slash - path >= size)
            slash = NULL;

        ms = (slash != NULL) ? slash - semi : (int) strlen (semi);
        ms = MIN ((unsigned int) ms, sizeof (encoding) - 1);
        /* limit encoding size (ms) to path size (size) */
        if (semi + ms > path + size)
            ms = path + size - semi;
        memcpy (encoding, semi, ms);
        encoding[ms] = '\0';

        if (is_supported_encoding (encoding))
            coder = str_crt_conv_to (encoding);

        if (coder != INVALID_CONV)
        {
            if (slash != NULL)
                state = str_vfs_convert_to (coder, slash + 1, path + size - slash - 1, buffer);
            str_close_conv (coder);
            return state;
        }

        errno = EINVAL;
        state = ESTR_FAILURE;
    }
    else
    {
        /* path can be translated whole at once */
        state = str_vfs_convert_to (defcnv, path, size, buffer);
    }
#else
    (void) size;
    (void) defcnv;

    g_string_assign (buffer, path);
#endif /* HAVE_CHARSET */

    return state;
}

/* --------------------------------------------------------------------------------------------- */

static struct vfs_openfile *
vfs_get_openfile (int handle)
{
    struct vfs_openfile *h;

    if (handle < VFS_FIRST_HANDLE || (guint) (handle - VFS_FIRST_HANDLE) >= vfs_openfiles->len)
        return NULL;

    h = (struct vfs_openfile *) g_ptr_array_index (vfs_openfiles, handle - VFS_FIRST_HANDLE);
    if (h == NULL)
        return NULL;

    g_assert (h->handle == handle);

    return h;
}

/* --------------------------------------------------------------------------------------------- */

static gboolean
vfs_test_current_dir (const vfs_path_t * vpath)
{
    struct stat my_stat, my_stat2;

    return (mc_global.vfs.cd_symlinks && mc_stat (vpath, &my_stat) == 0
            && mc_stat (vfs_get_raw_current_dir (), &my_stat2) == 0
            && my_stat.st_ino == my_stat2.st_ino && my_stat.st_dev == my_stat2.st_dev);
}


/* --------------------------------------------------------------------------------------------- */
/*** public functions ****************************************************************************/
/* --------------------------------------------------------------------------------------------- */
/** Free open file data for given file handle */

void
vfs_free_handle (int handle)
{
    const int idx = handle - VFS_FIRST_HANDLE;

    if (handle >= VFS_FIRST_HANDLE && (guint) idx < vfs_openfiles->len)
    {
        struct vfs_openfile *h;

        h = (struct vfs_openfile *) g_ptr_array_index (vfs_openfiles, idx);
        g_free (h);
        g_ptr_array_index (vfs_openfiles, idx) = (void *) vfs_free_handle_list;
        vfs_free_handle_list = idx;
    }
}


/* --------------------------------------------------------------------------------------------- */
/** Find VFS class by file handle */

struct vfs_class *
vfs_class_find_by_handle (int handle, void **fsinfo)
{
    struct vfs_openfile *h;

    h = vfs_get_openfile (handle);

    if (h == NULL)
        return NULL;

    if (fsinfo != NULL)
        *fsinfo = h->fsinfo;

    return h->vclass;
}

/* --------------------------------------------------------------------------------------------- */

/**
 * Create new VFS handle and put it to the list
 */

int
vfs_new_handle (struct vfs_class *vclass, void *fsinfo)
{
    struct vfs_openfile *h;

    h = g_new (struct vfs_openfile, 1);
    h->fsinfo = fsinfo;
    h->vclass = vclass;

    /* Allocate the first free handle */
    h->handle = vfs_free_handle_list;
    if (h->handle == -1)
    {
        /* No free allocated handles, allocate one */
        h->handle = vfs_openfiles->len;
        g_ptr_array_add (vfs_openfiles, h);
    }
    else
    {
        vfs_free_handle_list = (long) g_ptr_array_index (vfs_openfiles, vfs_free_handle_list);
        g_ptr_array_index (vfs_openfiles, h->handle) = h;
    }

    h->handle += VFS_FIRST_HANDLE;
    return h->handle;
}

/* --------------------------------------------------------------------------------------------- */

int
vfs_ferrno (struct vfs_class *vfs)
{
    return vfs->ferrno ? (*vfs->ferrno) (vfs) : E_UNKNOWN;
    /* Hope that error message is obscure enough ;-) */
}

/* --------------------------------------------------------------------------------------------- */

gboolean
vfs_register_class (struct vfs_class * vfs)
{
    if (vfs->init != NULL)      /* vfs has own initialization function */
        if (!vfs->init (vfs))   /* but it failed */
            return FALSE;

    g_ptr_array_add (vfs__classes_list, vfs);

    return TRUE;
}

/* --------------------------------------------------------------------------------------------- */

void
vfs_unregister_class (struct vfs_class *vfs)
{
    if (vfs->done != NULL)
        vfs->done (vfs);

    g_ptr_array_remove (vfs__classes_list, vfs);
}

/* --------------------------------------------------------------------------------------------- */
/** Strip known vfs suffixes from a filename (possible improvement: strip
 *  suffix from last path component).
 *  \return a malloced string which has to be freed.
 */

char *
vfs_strip_suffix_from_filename (const char *filename)
{
    char *semi, *p;

    if (filename == NULL)
        vfs_die ("vfs_strip_suffix_from_path got NULL: impossible");

    p = g_strdup (filename);
    semi = g_strrstr (p, VFS_PATH_URL_DELIMITER);
    if (semi != NULL)
    {
        char *vfs_prefix;

        *semi = '\0';
        vfs_prefix = strrchr (p, PATH_SEP);
        if (vfs_prefix == NULL)
            *semi = *VFS_PATH_URL_DELIMITER;
        else
            *vfs_prefix = '\0';
    }

    return p;
}

/* --------------------------------------------------------------------------------------------- */

const char *
vfs_translate_path (const char *path)
{
    estr_t state;

    g_string_set_size (vfs_str_buffer, 0);
    state = _vfs_translate_path (path, -1, str_cnv_from_term, vfs_str_buffer);
    return (state != ESTR_FAILURE) ? vfs_str_buffer->str : NULL;
}

/* --------------------------------------------------------------------------------------------- */

char *
vfs_translate_path_n (const char *path)
{
    const char *result;

    result = vfs_translate_path (path);
    return g_strdup (result);
}

/* --------------------------------------------------------------------------------------------- */
/**
 * Get current directory without any OS calls.
 *
 * @return string contains current path
 */

const char *
vfs_get_current_dir (void)
{
    return current_path->str;
}

/* --------------------------------------------------------------------------------------------- */
/**
 * Get current directory without any OS calls.
 *
 * @return newly allocated string contains current path
 */

char *
vfs_get_current_dir_n (void)
{
    return g_strdup (current_path->str);
}

/* --------------------------------------------------------------------------------------------- */
/**
 * Get raw current directory object without any OS calls.
 *
 * @return object contain current path
 */

const vfs_path_t *
vfs_get_raw_current_dir (void)
{
    return current_path;
}

/* --------------------------------------------------------------------------------------------- */
/**
 * Set current directory object.
 *
 * @param vpath new path
 */
void
vfs_set_raw_current_dir (const vfs_path_t * vpath)
{
    vfs_path_free (current_path, TRUE);
    current_path = (vfs_path_t *) vpath;
}

/* --------------------------------------------------------------------------------------------- */
/* Return TRUE is the current VFS class is local */

gboolean
vfs_current_is_local (void)
{
    return (current_vfs->flags & VFSF_LOCAL) != 0;
}

/* --------------------------------------------------------------------------------------------- */
/* Return flags of the VFS class of the given filename */

vfs_flags_t
vfs_file_class_flags (const vfs_path_t * vpath)
{
    const vfs_path_element_t *path_element;

    path_element = vfs_path_get_by_index (vpath, -1);
    if (!vfs_path_element_valid (path_element))
        return VFSF_UNKNOWN;

    return path_element->class->flags;
}

/* --------------------------------------------------------------------------------------------- */

void
vfs_init (void)
{
    /* create the VFS handle arrays */
    vfs__classes_list = g_ptr_array_new ();

    /* create the VFS handle array */
    vfs_openfiles = g_ptr_array_new ();

    vfs_str_buffer = g_string_new ("");

    mc_readdir_result = vfs_dirent_init (NULL, "", -1);
}

/* --------------------------------------------------------------------------------------------- */

void
vfs_setup_work_dir (void)
{
    vfs_setup_cwd ();

    /* FIXME: is we really need for this check? */
    /*
       if (strlen (current_dir) > MC_MAXPATHLEN - 2)
       vfs_die ("Current dir too long.\n");
     */

    current_vfs = VFS_CLASS (vfs_path_get_last_path_vfs (current_path));
}

/* --------------------------------------------------------------------------------------------- */

void
vfs_shut (void)
{
    guint i;

    vfs_gc_done ();

    vfs_set_raw_current_dir (NULL);

    for (i = 0; i < vfs__classes_list->len; i++)
    {
        struct vfs_class *vfs = VFS_CLASS (g_ptr_array_index (vfs__classes_list, i));

        if (vfs->done != NULL)
            vfs->done (vfs);
    }

    /* NULL-ize pointers to make unit tests happy */
    g_ptr_array_free (vfs_openfiles, TRUE);
    vfs_openfiles = NULL;
    g_ptr_array_free (vfs__classes_list, TRUE);
    vfs__classes_list = NULL;
    g_string_free (vfs_str_buffer, TRUE);
    vfs_str_buffer = NULL;
    current_vfs = NULL;
    vfs_free_handle_list = -1;
    vfs_dirent_free (mc_readdir_result);
    mc_readdir_result = NULL;
}

/* --------------------------------------------------------------------------------------------- */
/**
  * Init or create vfs_dirent structure
  *
  * @d vfs_dirent structure to init. If NULL, new structure is created.
  * @fname file name
  * @ino file inode number
  *
  * @return pointer to d if d isn't NULL, or pointer to newly created structure.
  */

struct vfs_dirent *
vfs_dirent_init (struct vfs_dirent *d, const char *fname, ino_t ino)
{
    struct vfs_dirent *ret = d;

    if (ret == NULL)
        ret = g_new0 (struct vfs_dirent, 1);

    if (ret->d_name_str == NULL)
        ret->d_name_str = g_string_sized_new (MC_MAXFILENAMELEN);

    vfs_dirent_assign (ret, fname, ino);

    return ret;
}

/* --------------------------------------------------------------------------------------------- */
/**
  * Assign members of vfs_dirent structure
  *
  * @d vfs_dirent structure for assignment
  * @fname file name
  * @ino file inode number
  */

void
vfs_dirent_assign (struct vfs_dirent *d, const char *fname, ino_t ino)
{
    g_string_assign (d->d_name_str, fname);
    d->d_name = d->d_name_str->str;
    d->d_ino = ino;
}

/* --------------------------------------------------------------------------------------------- */
/**
  * Destroy vfs_dirent structure
  *
  * @d vfs_dirent structure to destroy.
  */

void
vfs_dirent_free (struct vfs_dirent *d)
{
    g_string_free (d->d_name_str, TRUE);
    g_free (d);
}

/* --------------------------------------------------------------------------------------------- */

/**
 * These ones grab information from the VFS
 *  and handles them to an upper layer
 */

void
vfs_fill_names (fill_names_f func)
{
    guint i;

    for (i = 0; i < vfs__classes_list->len; i++)
    {
        struct vfs_class *vfs = VFS_CLASS (g_ptr_array_index (vfs__classes_list, i));

        if (vfs->fill_names != NULL)
            vfs->fill_names (vfs, func);
    }
}

/* --------------------------------------------------------------------------------------------- */

gboolean
vfs_file_is_local (const vfs_path_t * vpath)
{
    return (vfs_file_class_flags (vpath) & VFSF_LOCAL) != 0;
}

/* --------------------------------------------------------------------------------------------- */

void
vfs_print_message (const char *msg, ...)
{
    ev_vfs_print_message_t event_data;
    va_list ap;

    va_start (ap, msg);
    event_data.msg = g_strdup_vprintf (msg, ap);
    va_end (ap);

    mc_event_raise (MCEVENT_GROUP_CORE, "vfs_print_message", (gpointer) & event_data);
}

/* --------------------------------------------------------------------------------------------- */
/**
 * If it's local, reread the current directory
 * from the OS.
 */

void
vfs_setup_cwd (void)
{
    char *current_dir;
    vfs_path_t *tmp_vpath;
    const struct vfs_class *me;

    if (vfs_get_raw_current_dir () == NULL)
    {
        current_dir = g_get_current_dir ();
        vfs_set_raw_current_dir (vfs_path_from_str (current_dir));
        g_free (current_dir);

        current_dir = getenv ("PWD");
        tmp_vpath = vfs_path_from_str (current_dir);

        if (tmp_vpath != NULL)
        {
            if (vfs_test_current_dir (tmp_vpath))
                vfs_set_raw_current_dir (tmp_vpath);
            else
                vfs_path_free (tmp_vpath, TRUE);
        }
    }

    me = vfs_path_get_last_path_vfs (vfs_get_raw_current_dir ());
    if ((me->flags & VFSF_LOCAL) != 0)
    {
        current_dir = g_get_current_dir ();
        tmp_vpath = vfs_path_from_str (current_dir);
        g_free (current_dir);

        if (tmp_vpath != NULL)
        {
            /* One of directories in the path is not readable */

            /* Check if it is O.K. to use the current_dir */
            if (!vfs_test_current_dir (tmp_vpath))
                vfs_set_raw_current_dir (tmp_vpath);
            else
                vfs_path_free (tmp_vpath, TRUE);
        }
    }
}

/* --------------------------------------------------------------------------------------------- */
/**
 * Return current directory.  If it's local, reread the current directory
 * from the OS.
 */

char *
vfs_get_cwd (void)
{
    vfs_setup_cwd ();
    return vfs_get_current_dir_n ();
}

/* --------------------------------------------------------------------------------------------- */
/**
 * Preallocate space for file in new place for ensure that file
 * will be fully copied with less fragmentation.
 *
 * @param dest_vfs_fd mc VFS file handler
 * @param src_fsize source file size
 * @param dest_fsize destination file size (if destination exists, otherwise should be 0)
 *
 * @return 0 if success and non-zero otherwise.
 * Note: function doesn't touch errno global variable.
 */

int
vfs_preallocate (int dest_vfs_fd, off_t src_fsize, off_t dest_fsize)
{
#ifndef HAVE_POSIX_FALLOCATE
    (void) dest_vfs_fd;
    (void) src_fsize;
    (void) dest_fsize;
    return 0;

#else /* HAVE_POSIX_FALLOCATE */
    void *dest_fd = NULL;
    struct vfs_class *dest_class;

    if (src_fsize == 0)
        return 0;

    dest_class = vfs_class_find_by_handle (dest_vfs_fd, &dest_fd);
    if ((dest_class->flags & VFSF_LOCAL) == 0 || dest_fd == NULL)
        return 0;

    return posix_fallocate (*(int *) dest_fd, dest_fsize, src_fsize - dest_fsize);

#endif /* HAVE_POSIX_FALLOCATE */
}

 /* --------------------------------------------------------------------------------------------- */

int
vfs_clone_file (int dest_vfs_fd, int src_vfs_fd)
{
#ifdef FICLONE
    void *dest_fd = NULL;
    void *src_fd = NULL;
    struct vfs_class *dest_class;
    struct vfs_class *src_class;

    dest_class = vfs_class_find_by_handle (dest_vfs_fd, &dest_fd);
    if ((dest_class->flags & VFSF_LOCAL) == 0)
    {
        errno = ENOTSUP;
        return (-1);
    }
    if (dest_fd == NULL)
    {
        errno = EBADF;
        return (-1);
    }

    src_class = vfs_class_find_by_handle (src_vfs_fd, &src_fd);
    if ((src_class->flags & VFSF_LOCAL) == 0)
    {
        errno = ENOTSUP;
        return (-1);
    }
    if (src_fd == NULL)
    {
        errno = EBADF;
        return (-1);
    }

    return ioctl (*(int *) dest_fd, FICLONE, *(int *) src_fd);
#else
    (void) dest_vfs_fd;
    (void) src_vfs_fd;
    errno = ENOTSUP;
    return (-1);
#endif
}

/* --------------------------------------------------------------------------------------------- */