added new parameter mcview_eof into ini-file
[free-mc.git] / vfs / vfs.c
blob6ca2c7524b053f42ab07363ad9c9c28f7fd669ac
1 /* Virtual File System switch code
2 Copyright (C) 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3 2007 Free Software Foundation, Inc.
5 Written by: 1995 Miguel de Icaza
6 1995 Jakub Jelinek
7 1998 Pavel Machek
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Library General Public License
11 as published by the Free Software Foundation; either version 2 of
12 the License, or (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU Library General Public License for more details.
19 You should have received a copy of the GNU Library General Public
20 License along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
23 /**
24 * \file
25 * \brief Source: Virtual File System switch code
26 * \author Miguel de Icaza
27 * \author Jakub Jelinek
28 * \author Pavel Machek
29 * \date 1995, 1998
30 * \warning funtions like extfs_lstat() have right to destroy any
31 * strings you pass to them. This is acutally ok as you g_strdup what
32 * you are passing to them, anyway; still, beware.
34 * Namespace: exports *many* functions with vfs_ prefix; exports
35 * parse_ls_lga and friends which do not have that prefix.
38 #include <config.h>
40 #include <stdio.h>
41 #include <stdlib.h> /* For atol() */
42 #include <stdarg.h>
43 #include <string.h>
44 #include <errno.h>
45 #include <sys/types.h>
46 #include <signal.h>
47 #include <ctype.h> /* is_digit() */
48 #include <fcntl.h>
49 #include <sys/stat.h>
50 #include <unistd.h>
51 #include <dirent.h>
53 #include "../src/global.h"
55 #include "../src/wtools.h" /* message() */
56 #include "../src/main.h" /* print_vfs_message */
57 #include "../src/strutil.h"
59 #include "utilvfs.h"
60 #include "gc.h"
61 #include "vfs.h"
62 #ifdef USE_NETCODE
63 # include "netutil.h"
64 #endif
65 #include "ftpfs.h"
66 #include "mcfs.h"
67 #include "smbfs.h"
68 #include "local.h"
70 /** They keep track of the current directory */
71 static struct vfs_class *current_vfs;
72 static char *current_dir;
74 struct vfs_openfile {
75 int handle;
76 struct vfs_class *vclass;
77 void *fsinfo;
80 struct vfs_dirinfo{
81 DIR *info;
82 GIConv converter;
86 static GPtrArray *vfs_openfiles;
87 static long vfs_free_handle_list = -1;
88 #define VFS_FIRST_HANDLE 100
90 static struct vfs_class *localfs_class;
91 static GString *vfs_str_buffer;
93 static const char *supported_encodings[] = {
94 "UTF8",
95 "UTF-8",
96 "BIG5",
97 "ASCII",
98 "ISO8859",
99 "ISO-8859",
100 "ISO_8859",
101 "KOI8",
102 "CP852",
103 "CP866",
104 "CP125",
105 NULL
108 /** Create new VFS handle and put it to the list */
109 static int
110 vfs_new_handle (struct vfs_class *vclass, void *fsinfo)
112 struct vfs_openfile *h;
114 h = g_new (struct vfs_openfile, 1);
115 h->fsinfo = fsinfo;
116 h->vclass = vclass;
118 /* Allocate the first free handle */
119 h->handle = vfs_free_handle_list;
120 if (h->handle == -1) {
121 /* No free allocated handles, allocate one */
122 h->handle = vfs_openfiles->len;
123 g_ptr_array_add (vfs_openfiles, h);
124 } else {
125 vfs_free_handle_list = (long) g_ptr_array_index (vfs_openfiles, vfs_free_handle_list);
126 g_ptr_array_index (vfs_openfiles, h->handle) = h;
129 h->handle += VFS_FIRST_HANDLE;
130 return h->handle;
133 /** Find VFS class by file handle */
134 static struct vfs_class *
135 vfs_op (int handle)
137 struct vfs_openfile *h;
139 if (handle < VFS_FIRST_HANDLE ||
140 (guint)(handle - VFS_FIRST_HANDLE) >= vfs_openfiles->len)
141 return NULL;
143 h = (struct vfs_openfile *) g_ptr_array_index (
144 vfs_openfiles, handle - VFS_FIRST_HANDLE);
145 if (!h)
146 return NULL;
148 g_assert (h->handle == handle);
150 return h->vclass;
153 /** Find private file data by file handle */
154 static void *
155 vfs_info (int handle)
157 struct vfs_openfile *h;
159 if (handle < VFS_FIRST_HANDLE ||
160 (guint)(handle - VFS_FIRST_HANDLE) >= vfs_openfiles->len)
161 return NULL;
163 h = (struct vfs_openfile *) g_ptr_array_index (
164 vfs_openfiles, handle - VFS_FIRST_HANDLE);
165 if (!h)
166 return NULL;
168 g_assert (h->handle == handle);
170 return h->fsinfo;
173 /** Free open file data for given file handle */
174 static void
175 vfs_free_handle (int handle)
177 if (handle < VFS_FIRST_HANDLE ||
178 (guint)(handle - VFS_FIRST_HANDLE) >= vfs_openfiles->len)
179 return;
181 g_ptr_array_index (vfs_openfiles, handle - VFS_FIRST_HANDLE) =
182 (void *) vfs_free_handle_list;
183 vfs_free_handle_list = handle - VFS_FIRST_HANDLE;
186 static struct vfs_class *vfs_list;
189 vfs_register_class (struct vfs_class *vfs)
191 if (vfs->init) /* vfs has own initialization function */
192 if (!(*vfs->init)(vfs)) /* but it failed */
193 return 0;
195 vfs->next = vfs_list;
196 vfs_list = vfs;
198 return 1;
201 /** Return VFS class for the given prefix */
202 static struct vfs_class *
203 vfs_prefix_to_class (char *prefix)
205 struct vfs_class *vfs;
207 /* Avoid last class (localfs) that would accept any prefix */
208 for (vfs = vfs_list; vfs->next; vfs = vfs->next) {
209 if (vfs->which) {
210 if ((*vfs->which) (vfs, prefix) == -1)
211 continue;
212 return vfs;
214 if (vfs->prefix
215 && !strncmp (prefix, vfs->prefix, strlen (vfs->prefix)))
216 return vfs;
218 return NULL;
221 /** Strip known vfs suffixes from a filename (possible improvement: strip
222 * suffix from last path component).
223 * \return a malloced string which has to be freed.
225 char *
226 vfs_strip_suffix_from_filename (const char *filename)
228 struct vfs_class *vfs;
229 char *semi;
230 char *p;
232 if (!filename)
233 vfs_die ("vfs_strip_suffix_from_path got NULL: impossible");
235 p = g_strdup (filename);
236 if (!(semi = strrchr (p, '#')))
237 return p;
239 /* Avoid last class (localfs) that would accept any prefix */
240 for (vfs = vfs_list; vfs->next; vfs = vfs->next) {
241 if (vfs->which) {
242 if ((*vfs->which) (vfs, semi + 1) == -1)
243 continue;
244 *semi = '\0'; /* Found valid suffix */
245 return p;
247 if (vfs->prefix
248 && !strncmp (semi + 1, vfs->prefix, strlen (vfs->prefix))) {
249 *semi = '\0'; /* Found valid suffix */
250 return p;
253 return p;
256 static int
257 path_magic (const char *path)
259 struct stat buf;
261 if (!stat(path, &buf))
262 return 0;
264 return 1;
268 * Splits path extracting vfs part.
270 * Splits path
271 * \verbatim /p1#op/inpath \endverbatim
272 * into
273 * \verbatim inpath,op; \endverbatim
274 * returns which vfs it is.
275 * What is left in path is p1. You still want to g_free(path), you DON'T
276 * want to free neither *inpath nor *op
278 struct vfs_class *
279 vfs_split (char *path, char **inpath, char **op)
281 char *semi;
282 char *slash;
283 struct vfs_class *ret;
285 if (!path)
286 vfs_die("Cannot split NULL");
288 semi = strrchr (path, '#');
289 if (!semi || !path_magic(path))
290 return NULL;
292 slash = strchr (semi, PATH_SEP);
293 *semi = 0;
295 if (op)
296 *op = NULL;
298 if (inpath)
299 *inpath = NULL;
301 if (slash)
302 *slash = 0;
304 if ((ret = vfs_prefix_to_class (semi+1))){
305 if (op)
306 *op = semi + 1;
307 if (inpath)
308 *inpath = slash ? slash + 1 : NULL;
309 return ret;
313 if (slash)
314 *slash = PATH_SEP;
315 ret = vfs_split (path, inpath, op);
316 *semi = '#';
317 return ret;
320 static struct vfs_class *
321 _vfs_get_class (char *path)
323 char *semi;
324 char *slash;
325 struct vfs_class *ret;
327 g_return_val_if_fail(path, NULL);
329 semi = strrchr (path, '#');
330 if (!semi || !path_magic (path))
331 return NULL;
333 slash = strchr (semi, PATH_SEP);
334 *semi = 0;
335 if (slash)
336 *slash = 0;
338 ret = vfs_prefix_to_class (semi+1);
340 if (slash)
341 *slash = PATH_SEP;
342 if (!ret)
343 ret = _vfs_get_class (path);
345 *semi = '#';
346 return ret;
349 struct vfs_class *
350 vfs_get_class (const char *pathname)
352 struct vfs_class *vfs;
353 char *path = g_strdup (pathname);
355 vfs = _vfs_get_class (path);
356 g_free (path);
358 if (!vfs)
359 vfs = localfs_class;
361 return vfs;
364 const char *
365 vfs_get_encoding (const char *path)
367 static char result[16];
368 char *work;
369 char *semi;
370 char *slash;
372 work = g_strdup (path);
373 semi = g_strrstr (work, "#enc:");
375 if (semi != NULL) {
376 semi+= 5 * sizeof (char);
377 slash = strchr (semi, PATH_SEP);
378 if (slash != NULL)
379 slash[0] = '\0';
381 g_strlcpy (result, semi, sizeof(result));
382 g_free (work);
383 return result;
384 } else {
385 g_free (work);
386 return NULL;
390 /* return if encoding can by used in vfs (is ascci full compactible) */
391 /* contains only a few encoding now */
392 static int
393 vfs_supported_enconding (const char *encoding) {
394 int t;
395 int result = 0;
397 for (t = 0; supported_encodings[t] != NULL; t++) {
398 result+= (g_ascii_strncasecmp (encoding, supported_encodings[t],
399 strlen (supported_encodings[t])) == 0);
402 return result;
405 /* now used only by vfs_translate_path, but could be used in other vfs
406 * plugin to automatic detect encoding
407 * path - path to translate
408 * size - how many bytes from path translate
409 * defcnv - convertor, that is used as default, when path does not contain any
410 * #enc: subtring
411 * buffer - used to store result of translation
413 static estr_t
414 _vfs_translate_path (const char *path, int size,
415 GIConv defcnv, GString *buffer)
417 const char *semi;
418 const char *ps;
419 const char *slash;
420 estr_t state = ESTR_SUCCESS;
421 static char encoding[16];
422 GIConv coder;
423 int ms;
425 if (size == 0) return 0;
426 size = ( size > 0) ? size : (signed int)strlen (path);
428 /* try found #end: */
429 semi = g_strrstr_len (path, size, "#enc:");
430 if (semi != NULL) {
431 /* first must be translated part before #enc: */
432 ms = semi - path;
434 /* remove '/' before #enc */
435 ps = str_cget_prev_char (semi);
436 if (ps[0] == PATH_SEP) ms = ps - path;
438 state = _vfs_translate_path (path, ms, defcnv, buffer);
440 if (state != ESTR_SUCCESS)
441 return state;
442 /* now can be translated part after #enc: */
444 semi+= 5;
445 slash = strchr (semi, PATH_SEP);
446 /* ignore slashes after size; */
447 if (slash - path >= size) slash = NULL;
449 ms = (slash != NULL) ? slash - semi : (int) strlen (semi);
450 ms = min ((unsigned int) ms, sizeof (encoding) - 1);
451 /* limit encoding size (ms) to path size (size) */
452 if (semi + ms > path + size) ms = path + size - semi;
453 memcpy (encoding, semi, ms);
454 encoding[ms] = '\0';
456 switch (vfs_supported_enconding (encoding)) {
457 case 1:
458 coder = str_crt_conv_to (encoding);
459 if (coder != INVALID_CONV) {
460 if (slash != NULL) {
461 state = str_vfs_convert_to (coder, slash,
462 path + size - slash, buffer);
463 } else if (buffer->str[0] == '\0') {
464 /* exmaple "/#enc:utf-8" */
465 g_string_append_c(buffer, PATH_SEP);
467 str_close_conv (coder);
468 return state;
469 } else {
470 errno = EINVAL;
471 return ESTR_FAILURE;
473 break;
474 default:
475 errno = EINVAL;
476 return ESTR_FAILURE;
478 } else {
479 /* path can be translated whole at once */
480 state = str_vfs_convert_to (defcnv, path, size, buffer);
481 return state;
484 return ESTR_SUCCESS;
487 char *
488 vfs_translate_path (const char *path)
490 estr_t state;
492 g_string_set_size(vfs_str_buffer,0);
493 state = _vfs_translate_path (path, -1, str_cnv_from_term, vfs_str_buffer);
495 strict version
496 return (state == 0) ? vfs_str_buffer->data : NULL;
498 return (state != ESTR_FAILURE) ? vfs_str_buffer->str : NULL;
501 char *
502 vfs_translate_path_n (const char *path)
504 char *result;
506 result = vfs_translate_path (path);
507 return (result != NULL) ? g_strdup (result) : NULL;
510 char *
511 vfs_canon_and_translate (const char *path)
513 char *canon;
514 char *result;
515 if (path == NULL)
516 canon = g_strdup ("");
517 else
518 canon = vfs_canon (path);
519 result = vfs_translate_path_n (canon);
520 g_free (canon);
521 return result;
524 static int
525 ferrno (struct vfs_class *vfs)
527 return vfs->ferrno ? (*vfs->ferrno)(vfs) : E_UNKNOWN;
528 /* Hope that error message is obscure enough ;-) */
532 mc_open (const char *filename, int flags, ...)
534 int mode;
535 void *info;
536 va_list ap;
538 char *file = vfs_canon_and_translate (filename);
539 if (file != NULL) {
540 struct vfs_class *vfs = vfs_get_class (file);
542 /* Get the mode flag */
543 if (flags & O_CREAT) {
544 va_start (ap, flags);
545 mode = va_arg (ap, int);
546 va_end (ap);
547 } else
548 mode = 0;
550 if (!vfs->open) {
551 g_free (file);
552 errno = -EOPNOTSUPP;
553 return -1;
556 info = (*vfs->open) (vfs, file, flags, mode); /* open must be supported */
557 g_free (file);
558 if (!info){
559 errno = ferrno (vfs);
560 return -1;
563 return vfs_new_handle (vfs, info);
564 } else return -1;
568 #define MC_NAMEOP(name, inarg, callarg) \
569 int mc_##name inarg \
571 struct vfs_class *vfs; \
572 int result; \
573 char *mpath = vfs_canon_and_translate (path); \
574 if (mpath != NULL) { \
575 vfs = vfs_get_class (mpath); \
576 if (vfs == NULL){ \
577 g_free (mpath); \
578 return -1; \
580 result = vfs->name ? (*vfs->name)callarg : -1; \
581 g_free (mpath); \
582 if (result == -1) \
583 errno = vfs->name ? ferrno (vfs) : E_NOTSUPP; \
584 return result; \
585 } else return -1; \
588 MC_NAMEOP (chmod, (const char *path, mode_t mode), (vfs, mpath, mode))
589 MC_NAMEOP (chown, (const char *path, uid_t owner, gid_t group), (vfs, mpath, owner, group))
590 MC_NAMEOP (utime, (const char *path, struct utimbuf *times), (vfs, mpath, times))
591 MC_NAMEOP (readlink, (const char *path, char *buf, int bufsiz), (vfs, mpath, buf, bufsiz))
592 MC_NAMEOP (unlink, (const char *path), (vfs, mpath))
593 MC_NAMEOP (mkdir, (const char *path, mode_t mode), (vfs, mpath, mode))
594 MC_NAMEOP (rmdir, (const char *path), (vfs, mpath))
595 MC_NAMEOP (mknod, (const char *path, mode_t mode, dev_t dev), (vfs, mpath, mode, dev))
597 int
598 mc_symlink (const char *name1, const char *path)
600 struct vfs_class *vfs;
601 int result;
602 char *mpath;
603 char *lpath;
604 char *tmp;
606 mpath = vfs_canon_and_translate (path);
607 if (mpath != NULL) {
608 tmp = g_strdup (name1);
609 lpath = vfs_translate_path_n (tmp);
610 g_free (tmp);
612 if (lpath != NULL) {
613 vfs = vfs_get_class (mpath);
614 result = vfs->symlink ? (*vfs->symlink) (vfs, lpath, mpath) : -1;
615 g_free (lpath);
617 if (result == -1)
618 errno = vfs->symlink ? ferrno (vfs) : E_NOTSUPP;
619 return result;
621 g_free (mpath);
623 return -1;
626 #define MC_HANDLEOP(name, inarg, callarg) \
627 ssize_t mc_##name inarg \
629 struct vfs_class *vfs; \
630 int result; \
631 if (handle == -1) \
632 return -1; \
633 vfs = vfs_op (handle); \
634 if (vfs == NULL) \
635 return -1; \
636 result = vfs->name ? (*vfs->name)callarg : -1; \
637 if (result == -1) \
638 errno = vfs->name ? ferrno (vfs) : E_NOTSUPP; \
639 return result; \
642 MC_HANDLEOP(read, (int handle, void *buffer, int count), (vfs_info (handle), buffer, count))
643 MC_HANDLEOP(write, (int handle, const void *buf, int nbyte), (vfs_info (handle), buf, nbyte))
646 #define MC_RENAMEOP(name) \
647 int mc_##name (const char *fname1, const char *fname2) \
649 struct vfs_class *vfs; \
650 int result; \
651 char *name2, *name1; \
652 name1 = vfs_canon_and_translate (fname1); \
653 if (name1 != NULL) { \
654 name2 = vfs_canon_and_translate (fname2); \
655 if (name2 != NULL) { \
656 vfs = vfs_get_class (name1); \
657 if (vfs != vfs_get_class (name2)){ \
658 errno = EXDEV; \
659 g_free (name1); \
660 g_free (name2); \
661 return -1; \
663 result = vfs->name ? (*vfs->name)(vfs, name1, name2) : -1; \
664 g_free (name1); \
665 g_free (name2); \
666 if (result == -1) \
667 errno = vfs->name ? ferrno (vfs) : E_NOTSUPP; \
668 return result; \
669 } else { \
670 g_free (name1); \
671 return -1; \
673 } else return -1; \
676 MC_RENAMEOP (link)
677 MC_RENAMEOP (rename)
681 mc_ctl (int handle, int ctlop, void *arg)
683 struct vfs_class *vfs = vfs_op (handle);
685 if (vfs == NULL)
686 return 0;
688 return vfs->ctl ? (*vfs->ctl)(vfs_info (handle), ctlop, arg) : 0;
692 mc_setctl (const char *path, int ctlop, void *arg)
694 struct vfs_class *vfs;
695 int result;
696 char *mpath;
698 if (!path)
699 vfs_die("You don't want to pass NULL to mc_setctl.");
701 mpath = vfs_canon_and_translate (path);
702 if (mpath != NULL) {
703 vfs = vfs_get_class (mpath);
704 result = vfs->setctl ? (*vfs->setctl)(vfs, mpath, ctlop, arg) : 0;
705 g_free (mpath);
706 return result;
707 } else return -1;
711 mc_close (int handle)
713 struct vfs_class *vfs;
714 int result;
716 if (handle == -1 || !vfs_info (handle))
717 return -1;
719 vfs = vfs_op (handle);
720 if (vfs == NULL)
721 return -1;
723 if (handle < 3)
724 return close (handle);
726 if (!vfs->close)
727 vfs_die ("VFS must support close.\n");
728 result = (*vfs->close)(vfs_info (handle));
729 vfs_free_handle (handle);
730 if (result == -1)
731 errno = ferrno (vfs);
733 return result;
736 DIR *
737 mc_opendir (const char *dirname)
739 int handle, *handlep;
740 void *info;
741 struct vfs_class *vfs;
742 char *canon;
743 char *dname;
744 struct vfs_dirinfo *dirinfo;
745 const char *encoding;
747 canon = vfs_canon (dirname);
748 dname = vfs_translate_path_n (canon);
750 if (dname != NULL) {
751 vfs = vfs_get_class (dname);
752 info = vfs->opendir ? (*vfs->opendir)(vfs, dname) : NULL;
753 g_free (dname);
755 if (info == NULL) {
756 errno = vfs->opendir ? ferrno (vfs) : E_NOTSUPP;
757 g_free (canon);
758 return NULL;
761 dirinfo = g_new (struct vfs_dirinfo, 1);
762 dirinfo->info = info;
764 encoding = vfs_get_encoding (canon);
765 g_free (canon);
766 dirinfo->converter = (encoding != NULL) ? str_crt_conv_from (encoding) :
767 str_cnv_from_term;
768 if (dirinfo->converter == INVALID_CONV) dirinfo->converter =str_cnv_from_term;
770 handle = vfs_new_handle (vfs, dirinfo);
772 handlep = g_new (int, 1);
773 *handlep = handle;
774 return (DIR *) handlep;
775 } else {
776 g_free (canon);
777 return NULL;
781 static struct dirent * mc_readdir_result = NULL;
783 struct dirent *
784 mc_readdir (DIR *dirp)
786 int handle;
787 struct vfs_class *vfs;
788 struct dirent *entry = NULL;
789 struct vfs_dirinfo *dirinfo;
790 estr_t state;
792 if (!mc_readdir_result)
794 /* We can't just allocate struct dirent as (see man dirent.h)
795 * struct dirent has VERY nonnaive semantics of allocating
796 * d_name in it. Moreover, linux's glibc-2.9 allocates dirents _less_,
797 * than 'sizeof (struct dirent)' making full bitwise (sizeof dirent) copy
798 * heap corrupter. So, allocate longliving dirent with at least
799 * (MAXNAMLEN + 1) for d_name in it.
800 * Strictly saying resulting dirent is unusable as we don't adjust internal
801 * structures, holding dirent size. But we don't use it in libc infrastructure.
802 * TODO: to make simpler homemade dirent-alike structure.
804 mc_readdir_result = (struct dirent *) g_malloc (sizeof(struct dirent) + MAXNAMLEN + 1);
807 if (!dirp) {
808 errno = EFAULT;
809 return NULL;
811 handle = *(int *) dirp;
813 vfs = vfs_op (handle);
814 if (vfs == NULL)
815 return NULL;
817 dirinfo = vfs_info (handle);
818 if (vfs->readdir) {
819 entry = (*vfs->readdir) (dirinfo->info);
820 if (entry == NULL) return NULL;
821 g_string_set_size(vfs_str_buffer,0);
822 state = str_vfs_convert_from (dirinfo->converter,
823 entry->d_name, vfs_str_buffer);
824 mc_readdir_result->d_ino = entry->d_ino;
825 g_strlcpy (mc_readdir_result->d_name, vfs_str_buffer->str, MAXNAMLEN + 1);
827 if (entry == NULL) errno = vfs->readdir ? ferrno (vfs) : E_NOTSUPP;
828 return (entry != NULL) ? mc_readdir_result : NULL;
832 mc_closedir (DIR *dirp)
834 int handle = *(int *) dirp;
835 struct vfs_class *vfs = vfs_op (handle);
836 int result;
837 struct vfs_dirinfo *dirinfo;
839 if (vfs == NULL)
840 return -1;
842 dirinfo = vfs_info (handle);
843 if (dirinfo->converter != str_cnv_from_term) str_close_conv (dirinfo->converter);
845 result = vfs->closedir ? (*vfs->closedir)(dirinfo->info) : -1;
846 vfs_free_handle (handle);
847 g_free (dirinfo);
848 g_free (dirp);
849 return result;
852 int mc_stat (const char *filename, struct stat *buf) {
853 struct vfs_class *vfs;
854 int result;
855 char *path;
857 path = vfs_canon_and_translate (filename);
859 if (path == NULL)
860 return -1;
862 vfs = vfs_get_class (path);
864 if (vfs == NULL) {
865 g_free (path);
866 return -1;
869 result = vfs->stat ? (*vfs->stat) (vfs, path, buf) : -1;
871 g_free (path);
873 if (result == -1)
874 errno = vfs->name ? ferrno (vfs) : E_NOTSUPP;
875 return result;
878 int mc_lstat (const char *filename, struct stat *buf) {
879 struct vfs_class *vfs;
880 int result;
881 char *path;
883 path = vfs_canon_and_translate (filename);
885 if (path == NULL)
886 return -1;
888 vfs = vfs_get_class (path);
889 if (vfs == NULL) {
890 g_free (path);
891 return -1;
894 result = vfs->lstat ? (*vfs->lstat) (vfs, path, buf) : -1;
895 g_free (path);
896 if (result == -1)
897 errno = vfs->name ? ferrno (vfs) : E_NOTSUPP;
898 return result;
901 int mc_fstat (int handle, struct stat *buf) {
902 struct vfs_class *vfs;
903 int result;
905 if (handle == -1)
906 return -1;
908 vfs = vfs_op (handle);
909 if (vfs == NULL)
910 return -1;
912 result = vfs->fstat ? (*vfs->fstat) (vfs_info (handle), buf) : -1;
913 if (result == -1)
914 errno = vfs->name ? ferrno (vfs) : E_NOTSUPP;
915 return result;
919 * Return current directory. If it's local, reread the current directory
920 * from the OS. You must g_strdup() whatever this function returns.
922 static const char *
923 _vfs_get_cwd (void)
925 char *sys_cwd;
926 char *trans;
927 const char *encoding;
928 char *tmp;
929 estr_t state;
930 struct stat my_stat, my_stat2;
932 trans = vfs_translate_path_n (current_dir); /* add check if NULL */
934 if (!_vfs_get_class (trans)) {
935 encoding = vfs_get_encoding (current_dir);
936 if (encoding == NULL) {
937 tmp = g_get_current_dir ();
938 if (tmp != NULL) { /* One of the directories in the path is not readable */
939 g_string_set_size(vfs_str_buffer,0);
940 state = str_vfs_convert_from (str_cnv_from_term, tmp, vfs_str_buffer);
941 g_free (tmp);
942 sys_cwd = (state == ESTR_SUCCESS) ? g_strdup (vfs_str_buffer->str) : NULL;
943 if (!sys_cwd)
944 return current_dir;
946 /* Otherwise check if it is O.K. to use the current_dir */
947 if (!cd_symlinks || mc_stat (sys_cwd, &my_stat)
948 || mc_stat (current_dir, &my_stat2)
949 || my_stat.st_ino != my_stat2.st_ino
950 || my_stat.st_dev != my_stat2.st_dev) {
951 g_free (current_dir);
952 current_dir = sys_cwd;
953 return sys_cwd;
954 }/* Otherwise we return current_dir below */
958 g_free (trans);
959 return current_dir;
962 static void
963 vfs_setup_wd (void)
965 current_dir = g_strdup (PATH_SEP_STR);
966 _vfs_get_cwd ();
968 if (strlen (current_dir) > MC_MAXPATHLEN - 2)
969 vfs_die ("Current dir too long.\n");
971 current_vfs = vfs_get_class (current_dir);
975 * Return current directory. If it's local, reread the current directory
976 * from the OS. Put directory to the provided buffer.
978 char *
979 mc_get_current_wd (char *buffer, int size)
981 const char *cwd = _vfs_get_cwd ();
983 g_strlcpy (buffer, cwd, size);
984 return buffer;
988 * Return current directory without any OS calls.
990 char *
991 vfs_get_current_dir (void)
993 return current_dir;
996 off_t mc_lseek (int fd, off_t offset, int whence)
998 struct vfs_class *vfs;
999 int result;
1001 if (fd == -1)
1002 return -1;
1004 vfs = vfs_op (fd);
1005 if (vfs == NULL)
1006 return -1;
1008 result = vfs->lseek ? (*vfs->lseek)(vfs_info (fd), offset, whence) : -1;
1009 if (result == -1)
1010 errno = vfs->lseek ? ferrno (vfs) : E_NOTSUPP;
1011 return result;
1015 * remove //, /./ and /../
1018 #define ISSLASH(a) (!a || (a == '/'))
1020 char *
1021 vfs_canon (const char *path)
1023 if (!path)
1024 vfs_die("Cannot canonicalize NULL");
1026 /* Relative to current directory */
1027 if (*path != PATH_SEP){
1028 char *local, *result;
1030 local = concat_dir_and_file (current_dir, path);
1032 result = vfs_canon (local);
1033 g_free (local);
1034 return result;
1038 * So we have path of following form:
1039 * /p1/p2#op/.././././p3#op/p4. Good luck.
1042 char *result = g_strdup (path);
1043 canonicalize_pathname (result);
1044 return result;
1049 * VFS chdir.
1050 * Return 0 on success, -1 on failure.
1053 mc_chdir (const char *path)
1055 char *new_dir;
1056 char *trans_dir;
1057 struct vfs_class *old_vfs, *new_vfs;
1058 vfsid old_vfsid;
1059 int result;
1061 new_dir = vfs_canon (path);
1062 trans_dir = vfs_translate_path_n (new_dir);
1063 if (trans_dir != NULL) {
1064 new_vfs = vfs_get_class (trans_dir);
1065 if (!new_vfs->chdir) {
1066 g_free (new_dir);
1067 g_free (trans_dir);
1068 return -1;
1071 result = (*new_vfs->chdir) (new_vfs, trans_dir);
1073 if (result == -1) {
1074 errno = ferrno (new_vfs);
1075 g_free (new_dir);
1076 g_free (trans_dir);
1077 return -1;
1080 old_vfsid = vfs_getid (current_vfs, current_dir);
1081 old_vfs = current_vfs;
1083 /* Actually change directory */
1084 g_free (current_dir);
1085 current_dir = new_dir;
1086 current_vfs = new_vfs;
1088 /* This function uses the new current_dir implicitly */
1089 vfs_stamp_create (old_vfs, old_vfsid);
1091 /* Sometimes we assume no trailing slash on cwd */
1092 if (*current_dir) {
1093 char *p;
1094 p = strchr (current_dir, 0) - 1;
1095 if (*p == PATH_SEP && p > current_dir)
1096 *p = 0;
1099 g_free (trans_dir);
1100 return 0;
1101 } else {
1102 g_free (new_dir);
1103 return -1;
1107 /* Return 1 is the current VFS class is local */
1109 vfs_current_is_local (void)
1111 return (current_vfs->flags & VFSF_LOCAL) != 0;
1114 /* Return flags of the VFS class of the given filename */
1116 vfs_file_class_flags (const char *filename)
1118 struct vfs_class *vfs;
1119 char *fname;
1121 fname = vfs_canon_and_translate (filename);
1122 if (fname != NULL) {
1123 vfs = vfs_get_class (fname);
1124 g_free (fname);
1125 return vfs->flags;
1126 } else return -1;
1129 static char *
1130 mc_def_getlocalcopy (const char *filename)
1132 char *tmp;
1133 int fdin, fdout;
1134 ssize_t i;
1135 char buffer[8192];
1136 struct stat mystat;
1138 fdin = mc_open (filename, O_RDONLY | O_LINEAR);
1139 if (fdin == -1)
1140 return NULL;
1142 fdout = vfs_mkstemps (&tmp, "vfs", filename);
1144 if (fdout == -1)
1145 goto fail;
1146 while ((i = mc_read (fdin, buffer, sizeof (buffer))) > 0) {
1147 if (write (fdout, buffer, i) != i)
1148 goto fail;
1150 if (i == -1)
1151 goto fail;
1152 i = mc_close (fdin);
1153 fdin = -1;
1154 if (i == -1)
1155 goto fail;
1156 if (close (fdout) == -1) {
1157 fdout = -1;
1158 goto fail;
1161 if (mc_stat (filename, &mystat) != -1) {
1162 chmod (tmp, mystat.st_mode);
1164 return tmp;
1166 fail:
1167 if (fdout != -1)
1168 close (fdout);
1169 if (fdin != -1)
1170 mc_close (fdin);
1171 g_free (tmp);
1172 return NULL;
1175 char *
1176 mc_getlocalcopy (const char *pathname)
1178 char *result;
1179 char *path;
1181 path = vfs_canon_and_translate (pathname);
1182 if (path != NULL) {
1183 struct vfs_class *vfs = vfs_get_class (path);
1185 result = vfs->getlocalcopy ? (*vfs->getlocalcopy)(vfs, path) :
1186 mc_def_getlocalcopy (path);
1187 g_free (path);
1188 if (!result)
1189 errno = ferrno (vfs);
1190 return result;
1191 } else return NULL;
1194 static int
1195 mc_def_ungetlocalcopy (struct vfs_class *vfs, const char *filename,
1196 const char *local, int has_changed)
1198 int fdin = -1, fdout = -1, i;
1199 if (has_changed) {
1200 char buffer[8192];
1202 if (!vfs->write)
1203 goto failed;
1205 fdin = open (local, O_RDONLY);
1206 if (fdin == -1)
1207 goto failed;
1208 fdout = mc_open (filename, O_WRONLY | O_TRUNC);
1209 if (fdout == -1)
1210 goto failed;
1211 while ((i = read (fdin, buffer, sizeof (buffer))) > 0) {
1212 if (mc_write (fdout, buffer, i) != i)
1213 goto failed;
1215 if (i == -1)
1216 goto failed;
1218 if (close (fdin) == -1) {
1219 fdin = -1;
1220 goto failed;
1222 fdin = -1;
1223 if (mc_close (fdout) == -1) {
1224 fdout = -1;
1225 goto failed;
1228 unlink (local);
1229 return 0;
1231 failed:
1232 message (D_ERROR, _("Changes to file lost"), "%s", filename);
1233 if (fdout != -1)
1234 mc_close (fdout);
1235 if (fdin != -1)
1236 close (fdin);
1237 unlink (local);
1238 return -1;
1242 mc_ungetlocalcopy (const char *pathname, const char *local, int has_changed)
1244 int return_value = 0;
1245 char *path;
1247 path = vfs_canon_and_translate (pathname);
1248 if (path != NULL) {
1249 struct vfs_class *vfs = vfs_get_class (path);
1251 return_value = vfs->ungetlocalcopy ?
1252 (*vfs->ungetlocalcopy)(vfs, path, local, has_changed) :
1253 mc_def_ungetlocalcopy (vfs, path, local, has_changed);
1254 g_free (path);
1255 return return_value;
1256 } else return -1;
1260 void
1261 vfs_init (void)
1263 /* create the VFS handle array */
1264 vfs_openfiles = g_ptr_array_new ();
1266 vfs_str_buffer = g_string_new("");
1267 /* localfs needs to be the first one */
1268 init_localfs();
1269 /* fallback value for vfs_get_class() */
1270 localfs_class = vfs_list;
1272 init_extfs ();
1273 init_sfs ();
1274 init_tarfs ();
1275 init_cpiofs ();
1277 #ifdef USE_EXT2FSLIB
1278 init_undelfs ();
1279 #endif /* USE_EXT2FSLIB */
1281 #ifdef USE_NETCODE
1282 init_ftpfs ();
1283 init_fish ();
1284 #ifdef ENABLE_VFS_SMB
1285 init_smbfs ();
1286 #endif /* ENABLE_VFS_SMB */
1287 #ifdef ENABLE_VFS_MCFS
1288 init_mcfs ();
1289 #endif /* ENABLE_VFS_MCFS */
1290 #endif /* USE_NETCODE */
1292 vfs_setup_wd ();
1295 void
1296 vfs_shut (void)
1298 struct vfs_class *vfs;
1300 vfs_gc_done ();
1302 g_free (current_dir);
1304 for (vfs = vfs_list; vfs; vfs = vfs->next)
1305 if (vfs->done)
1306 (*vfs->done) (vfs);
1308 g_ptr_array_free (vfs_openfiles, TRUE);
1309 g_string_free (vfs_str_buffer, TRUE);
1310 g_free (mc_readdir_result);
1314 * These ones grab information from the VFS
1315 * and handles them to an upper layer
1317 void
1318 vfs_fill_names (fill_names_f func)
1320 struct vfs_class *vfs;
1322 for (vfs=vfs_list; vfs; vfs=vfs->next)
1323 if (vfs->fill_names)
1324 (*vfs->fill_names) (vfs, func);
1328 * Returns vfs path corresponding to given url. If passed string is
1329 * not recognized as url, g_strdup(url) is returned.
1332 static const struct {
1333 const char *name;
1334 size_t name_len;
1335 const char *substitute;
1336 } url_table[] = { {"ftp://", 6, "/#ftp:"},
1337 {"mc://", 5, "/#mc:"},
1338 {"smb://", 6, "/#smb:"},
1339 {"sh://", 5, "/#sh:"},
1340 {"ssh://", 6, "/#sh:"},
1341 {"a:", 2, "/#a"}
1344 char *
1345 vfs_translate_url (const char *url)
1347 size_t i;
1349 for (i = 0; i < sizeof (url_table)/sizeof (url_table[0]); i++)
1350 if (strncmp (url, url_table[i].name, url_table[i].name_len) == 0)
1351 return g_strconcat (url_table[i].substitute, url + url_table[i].name_len, (char*) NULL);
1353 return g_strdup (url);
1356 int vfs_file_is_local (const char *filename)
1358 return vfs_file_class_flags (filename) & VFSF_LOCAL;