Ticket #1605: Incorrect parsing FTP-string
[midnight-commander.git] / vfs / vfs.c
blob57ff3d7c3eee9da77e0fc67d5515366581fa36f5
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 "tcputil.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, *at_chr;
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 at_chr = strrchr (semi, '@');
293 if (at_chr)
294 slash = strchr (at_chr, PATH_SEP);
295 else
296 slash = strchr (semi, PATH_SEP);
298 *semi = 0;
300 if (op)
301 *op = NULL;
303 if (inpath)
304 *inpath = NULL;
306 if (slash)
307 *slash = 0;
309 if ((ret = vfs_prefix_to_class (semi+1))){
310 if (op)
311 *op = semi + 1;
312 if (inpath)
313 *inpath = slash ? slash + 1 : NULL;
314 return ret;
318 if (slash)
319 *slash = PATH_SEP;
320 ret = vfs_split (path, inpath, op);
321 *semi = '#';
322 return ret;
325 static struct vfs_class *
326 _vfs_get_class (char *path)
328 char *semi;
329 char *slash;
330 struct vfs_class *ret;
332 g_return_val_if_fail(path, NULL);
334 semi = strrchr (path, '#');
335 if (!semi || !path_magic (path))
336 return NULL;
338 slash = strchr (semi, PATH_SEP);
339 *semi = 0;
340 if (slash)
341 *slash = 0;
343 ret = vfs_prefix_to_class (semi+1);
345 if (slash)
346 *slash = PATH_SEP;
347 if (!ret)
348 ret = _vfs_get_class (path);
350 *semi = '#';
351 return ret;
354 struct vfs_class *
355 vfs_get_class (const char *pathname)
357 struct vfs_class *vfs;
358 char *path = g_strdup (pathname);
360 vfs = _vfs_get_class (path);
361 g_free (path);
363 if (!vfs)
364 vfs = localfs_class;
366 return vfs;
369 const char *
370 vfs_get_encoding (const char *path)
372 static char result[16];
373 char *work;
374 char *semi;
375 char *slash;
377 work = g_strdup (path);
378 semi = g_strrstr (work, "#enc:");
380 if (semi != NULL) {
381 semi+= 5 * sizeof (char);
382 slash = strchr (semi, PATH_SEP);
383 if (slash != NULL)
384 slash[0] = '\0';
386 g_strlcpy (result, semi, sizeof(result));
387 g_free (work);
388 return result;
389 } else {
390 g_free (work);
391 return NULL;
395 /* return if encoding can by used in vfs (is ascci full compactible) */
396 /* contains only a few encoding now */
397 static int
398 vfs_supported_enconding (const char *encoding) {
399 int t;
400 int result = 0;
402 for (t = 0; supported_encodings[t] != NULL; t++) {
403 result+= (g_ascii_strncasecmp (encoding, supported_encodings[t],
404 strlen (supported_encodings[t])) == 0);
407 return result;
410 /* now used only by vfs_translate_path, but could be used in other vfs
411 * plugin to automatic detect encoding
412 * path - path to translate
413 * size - how many bytes from path translate
414 * defcnv - convertor, that is used as default, when path does not contain any
415 * #enc: subtring
416 * buffer - used to store result of translation
418 static estr_t
419 _vfs_translate_path (const char *path, int size,
420 GIConv defcnv, GString *buffer)
422 const char *semi;
423 const char *ps;
424 const char *slash;
425 estr_t state = ESTR_SUCCESS;
426 static char encoding[16];
427 GIConv coder;
428 int ms;
430 if (size == 0) return 0;
431 size = ( size > 0) ? size : (signed int)strlen (path);
433 /* try found #end: */
434 semi = g_strrstr_len (path, size, "#enc:");
435 if (semi != NULL) {
436 /* first must be translated part before #enc: */
437 ms = semi - path;
439 /* remove '/' before #enc */
440 ps = str_cget_prev_char (semi);
441 if (ps[0] == PATH_SEP) ms = ps - path;
443 state = _vfs_translate_path (path, ms, defcnv, buffer);
445 if (state != ESTR_SUCCESS)
446 return state;
447 /* now can be translated part after #enc: */
449 semi+= 5;
450 slash = strchr (semi, PATH_SEP);
451 /* ignore slashes after size; */
452 if (slash - path >= size) slash = NULL;
454 ms = (slash != NULL) ? slash - semi : (int) strlen (semi);
455 ms = min ((unsigned int) ms, sizeof (encoding) - 1);
456 /* limit encoding size (ms) to path size (size) */
457 if (semi + ms > path + size) ms = path + size - semi;
458 memcpy (encoding, semi, ms);
459 encoding[ms] = '\0';
461 switch (vfs_supported_enconding (encoding)) {
462 case 1:
463 coder = str_crt_conv_to (encoding);
464 if (coder != INVALID_CONV) {
465 if (slash != NULL) {
466 state = str_vfs_convert_to (coder, slash,
467 path + size - slash, buffer);
468 } else if (buffer->str[0] == '\0') {
469 /* exmaple "/#enc:utf-8" */
470 g_string_append_c(buffer, PATH_SEP);
472 str_close_conv (coder);
473 return state;
474 } else {
475 errno = EINVAL;
476 return ESTR_FAILURE;
478 break;
479 default:
480 errno = EINVAL;
481 return ESTR_FAILURE;
483 } else {
484 /* path can be translated whole at once */
485 state = str_vfs_convert_to (defcnv, path, size, buffer);
486 return state;
489 return ESTR_SUCCESS;
492 char *
493 vfs_translate_path (const char *path)
495 estr_t state;
497 g_string_set_size(vfs_str_buffer,0);
498 state = _vfs_translate_path (path, -1, str_cnv_from_term, vfs_str_buffer);
500 strict version
501 return (state == 0) ? vfs_str_buffer->data : NULL;
503 return (state != ESTR_FAILURE) ? vfs_str_buffer->str : NULL;
506 char *
507 vfs_translate_path_n (const char *path)
509 char *result;
511 result = vfs_translate_path (path);
512 return (result != NULL) ? g_strdup (result) : NULL;
515 char *
516 vfs_canon_and_translate (const char *path)
518 char *canon;
519 char *result;
520 if (path == NULL)
521 canon = g_strdup ("");
522 else
523 canon = vfs_canon (path);
524 result = vfs_translate_path_n (canon);
525 g_free (canon);
526 return result;
529 static int
530 ferrno (struct vfs_class *vfs)
532 return vfs->ferrno ? (*vfs->ferrno)(vfs) : E_UNKNOWN;
533 /* Hope that error message is obscure enough ;-) */
537 mc_open (const char *filename, int flags, ...)
539 int mode;
540 void *info;
541 va_list ap;
543 char *file = vfs_canon_and_translate (filename);
544 if (file != NULL) {
545 struct vfs_class *vfs = vfs_get_class (file);
547 /* Get the mode flag */
548 if (flags & O_CREAT) {
549 va_start (ap, flags);
550 mode = va_arg (ap, int);
551 va_end (ap);
552 } else
553 mode = 0;
555 if (!vfs->open) {
556 g_free (file);
557 errno = -EOPNOTSUPP;
558 return -1;
561 info = (*vfs->open) (vfs, file, flags, mode); /* open must be supported */
562 g_free (file);
563 if (!info){
564 errno = ferrno (vfs);
565 return -1;
568 return vfs_new_handle (vfs, info);
569 } else return -1;
573 #define MC_NAMEOP(name, inarg, callarg) \
574 int mc_##name inarg \
576 struct vfs_class *vfs; \
577 int result; \
578 char *mpath = vfs_canon_and_translate (path); \
579 if (mpath != NULL) { \
580 vfs = vfs_get_class (mpath); \
581 if (vfs == NULL){ \
582 g_free (mpath); \
583 return -1; \
585 result = vfs->name ? (*vfs->name)callarg : -1; \
586 g_free (mpath); \
587 if (result == -1) \
588 errno = vfs->name ? ferrno (vfs) : E_NOTSUPP; \
589 return result; \
590 } else return -1; \
593 MC_NAMEOP (chmod, (const char *path, mode_t mode), (vfs, mpath, mode))
594 MC_NAMEOP (chown, (const char *path, uid_t owner, gid_t group), (vfs, mpath, owner, group))
595 MC_NAMEOP (utime, (const char *path, struct utimbuf *times), (vfs, mpath, times))
596 MC_NAMEOP (readlink, (const char *path, char *buf, int bufsiz), (vfs, mpath, buf, bufsiz))
597 MC_NAMEOP (unlink, (const char *path), (vfs, mpath))
598 MC_NAMEOP (mkdir, (const char *path, mode_t mode), (vfs, mpath, mode))
599 MC_NAMEOP (rmdir, (const char *path), (vfs, mpath))
600 MC_NAMEOP (mknod, (const char *path, mode_t mode, dev_t dev), (vfs, mpath, mode, dev))
602 int
603 mc_symlink (const char *name1, const char *path)
605 struct vfs_class *vfs;
606 int result;
607 char *mpath;
608 char *lpath;
609 char *tmp;
611 mpath = vfs_canon_and_translate (path);
612 if (mpath != NULL) {
613 tmp = g_strdup (name1);
614 lpath = vfs_translate_path_n (tmp);
615 g_free (tmp);
617 if (lpath != NULL) {
618 vfs = vfs_get_class (mpath);
619 result = vfs->symlink ? (*vfs->symlink) (vfs, lpath, mpath) : -1;
620 g_free (lpath);
622 if (result == -1)
623 errno = vfs->symlink ? ferrno (vfs) : E_NOTSUPP;
624 return result;
626 g_free (mpath);
628 return -1;
631 #define MC_HANDLEOP(name, inarg, callarg) \
632 ssize_t mc_##name inarg \
634 struct vfs_class *vfs; \
635 int result; \
636 if (handle == -1) \
637 return -1; \
638 vfs = vfs_op (handle); \
639 if (vfs == NULL) \
640 return -1; \
641 result = vfs->name ? (*vfs->name)callarg : -1; \
642 if (result == -1) \
643 errno = vfs->name ? ferrno (vfs) : E_NOTSUPP; \
644 return result; \
647 MC_HANDLEOP(read, (int handle, void *buffer, int count), (vfs_info (handle), buffer, count))
648 MC_HANDLEOP(write, (int handle, const void *buf, int nbyte), (vfs_info (handle), buf, nbyte))
651 #define MC_RENAMEOP(name) \
652 int mc_##name (const char *fname1, const char *fname2) \
654 struct vfs_class *vfs; \
655 int result; \
656 char *name2, *name1; \
657 name1 = vfs_canon_and_translate (fname1); \
658 if (name1 != NULL) { \
659 name2 = vfs_canon_and_translate (fname2); \
660 if (name2 != NULL) { \
661 vfs = vfs_get_class (name1); \
662 if (vfs != vfs_get_class (name2)){ \
663 errno = EXDEV; \
664 g_free (name1); \
665 g_free (name2); \
666 return -1; \
668 result = vfs->name ? (*vfs->name)(vfs, name1, name2) : -1; \
669 g_free (name1); \
670 g_free (name2); \
671 if (result == -1) \
672 errno = vfs->name ? ferrno (vfs) : E_NOTSUPP; \
673 return result; \
674 } else { \
675 g_free (name1); \
676 return -1; \
678 } else return -1; \
681 MC_RENAMEOP (link)
682 MC_RENAMEOP (rename)
686 mc_ctl (int handle, int ctlop, void *arg)
688 struct vfs_class *vfs = vfs_op (handle);
690 if (vfs == NULL)
691 return 0;
693 return vfs->ctl ? (*vfs->ctl)(vfs_info (handle), ctlop, arg) : 0;
697 mc_setctl (const char *path, int ctlop, void *arg)
699 struct vfs_class *vfs;
700 int result;
701 char *mpath;
703 if (!path)
704 vfs_die("You don't want to pass NULL to mc_setctl.");
706 mpath = vfs_canon_and_translate (path);
707 if (mpath != NULL) {
708 vfs = vfs_get_class (mpath);
709 result = vfs->setctl ? (*vfs->setctl)(vfs, mpath, ctlop, arg) : 0;
710 g_free (mpath);
711 return result;
712 } else return -1;
716 mc_close (int handle)
718 struct vfs_class *vfs;
719 int result;
721 if (handle == -1 || !vfs_info (handle))
722 return -1;
724 vfs = vfs_op (handle);
725 if (vfs == NULL)
726 return -1;
728 if (handle < 3)
729 return close (handle);
731 if (!vfs->close)
732 vfs_die ("VFS must support close.\n");
733 result = (*vfs->close)(vfs_info (handle));
734 vfs_free_handle (handle);
735 if (result == -1)
736 errno = ferrno (vfs);
738 return result;
741 DIR *
742 mc_opendir (const char *dirname)
744 int handle, *handlep;
745 void *info;
746 struct vfs_class *vfs;
747 char *canon;
748 char *dname;
749 struct vfs_dirinfo *dirinfo;
750 const char *encoding;
752 canon = vfs_canon (dirname);
753 dname = vfs_translate_path_n (canon);
755 if (dname != NULL) {
756 vfs = vfs_get_class (dname);
757 info = vfs->opendir ? (*vfs->opendir)(vfs, dname) : NULL;
758 g_free (dname);
760 if (info == NULL) {
761 errno = vfs->opendir ? ferrno (vfs) : E_NOTSUPP;
762 g_free (canon);
763 return NULL;
766 dirinfo = g_new (struct vfs_dirinfo, 1);
767 dirinfo->info = info;
769 encoding = vfs_get_encoding (canon);
770 g_free (canon);
771 dirinfo->converter = (encoding != NULL) ? str_crt_conv_from (encoding) :
772 str_cnv_from_term;
773 if (dirinfo->converter == INVALID_CONV) dirinfo->converter =str_cnv_from_term;
775 handle = vfs_new_handle (vfs, dirinfo);
777 handlep = g_new (int, 1);
778 *handlep = handle;
779 return (DIR *) handlep;
780 } else {
781 g_free (canon);
782 return NULL;
786 static struct dirent * mc_readdir_result = NULL;
788 struct dirent *
789 mc_readdir (DIR *dirp)
791 int handle;
792 struct vfs_class *vfs;
793 struct dirent *entry = NULL;
794 struct vfs_dirinfo *dirinfo;
795 estr_t state;
797 if (!mc_readdir_result)
799 /* We can't just allocate struct dirent as (see man dirent.h)
800 * struct dirent has VERY nonnaive semantics of allocating
801 * d_name in it. Moreover, linux's glibc-2.9 allocates dirents _less_,
802 * than 'sizeof (struct dirent)' making full bitwise (sizeof dirent) copy
803 * heap corrupter. So, allocate longliving dirent with at least
804 * (NAME_MAX + 1) for d_name in it.
805 * Strictly saying resulting dirent is unusable as we don't adjust internal
806 * structures, holding dirent size. But we don't use it in libc infrastructure.
807 * TODO: to make simpler homemade dirent-alike structure.
809 mc_readdir_result = (struct dirent *) g_malloc (sizeof(struct dirent) + NAME_MAX + 1);
812 if (!dirp) {
813 errno = EFAULT;
814 return NULL;
816 handle = *(int *) dirp;
818 vfs = vfs_op (handle);
819 if (vfs == NULL)
820 return NULL;
822 dirinfo = vfs_info (handle);
823 if (vfs->readdir) {
824 entry = (*vfs->readdir) (dirinfo->info);
825 if (entry == NULL) return NULL;
826 g_string_set_size(vfs_str_buffer,0);
827 state = str_vfs_convert_from (dirinfo->converter,
828 entry->d_name, vfs_str_buffer);
829 mc_readdir_result->d_ino = entry->d_ino;
830 g_strlcpy (mc_readdir_result->d_name, vfs_str_buffer->str, NAME_MAX + 1);
832 if (entry == NULL) errno = vfs->readdir ? ferrno (vfs) : E_NOTSUPP;
833 return (entry != NULL) ? mc_readdir_result : NULL;
837 mc_closedir (DIR *dirp)
839 int handle = *(int *) dirp;
840 struct vfs_class *vfs = vfs_op (handle);
841 int result;
842 struct vfs_dirinfo *dirinfo;
844 if (vfs == NULL)
845 return -1;
847 dirinfo = vfs_info (handle);
848 if (dirinfo->converter != str_cnv_from_term) str_close_conv (dirinfo->converter);
850 result = vfs->closedir ? (*vfs->closedir)(dirinfo->info) : -1;
851 vfs_free_handle (handle);
852 g_free (dirinfo);
853 g_free (dirp);
854 return result;
857 int mc_stat (const char *filename, struct stat *buf) {
858 struct vfs_class *vfs;
859 int result;
860 char *path;
862 path = vfs_canon_and_translate (filename);
864 if (path == NULL)
865 return -1;
867 vfs = vfs_get_class (path);
869 if (vfs == NULL) {
870 g_free (path);
871 return -1;
874 result = vfs->stat ? (*vfs->stat) (vfs, path, buf) : -1;
876 g_free (path);
878 if (result == -1)
879 errno = vfs->name ? ferrno (vfs) : E_NOTSUPP;
880 return result;
883 int mc_lstat (const char *filename, struct stat *buf) {
884 struct vfs_class *vfs;
885 int result;
886 char *path;
888 path = vfs_canon_and_translate (filename);
890 if (path == NULL)
891 return -1;
893 vfs = vfs_get_class (path);
894 if (vfs == NULL) {
895 g_free (path);
896 return -1;
899 result = vfs->lstat ? (*vfs->lstat) (vfs, path, buf) : -1;
900 g_free (path);
901 if (result == -1)
902 errno = vfs->name ? ferrno (vfs) : E_NOTSUPP;
903 return result;
906 int mc_fstat (int handle, struct stat *buf) {
907 struct vfs_class *vfs;
908 int result;
910 if (handle == -1)
911 return -1;
913 vfs = vfs_op (handle);
914 if (vfs == NULL)
915 return -1;
917 result = vfs->fstat ? (*vfs->fstat) (vfs_info (handle), buf) : -1;
918 if (result == -1)
919 errno = vfs->name ? ferrno (vfs) : E_NOTSUPP;
920 return result;
924 * Return current directory. If it's local, reread the current directory
925 * from the OS. You must g_strdup() whatever this function returns.
927 static const char *
928 _vfs_get_cwd (void)
930 char *sys_cwd;
931 char *trans;
932 const char *encoding;
933 char *tmp;
934 estr_t state;
935 struct stat my_stat, my_stat2;
937 trans = vfs_translate_path_n (current_dir); /* add check if NULL */
939 if (!_vfs_get_class (trans)) {
940 encoding = vfs_get_encoding (current_dir);
941 if (encoding == NULL) {
942 tmp = g_get_current_dir ();
943 if (tmp != NULL) { /* One of the directories in the path is not readable */
944 g_string_set_size(vfs_str_buffer,0);
945 state = str_vfs_convert_from (str_cnv_from_term, tmp, vfs_str_buffer);
946 g_free (tmp);
947 sys_cwd = (state == ESTR_SUCCESS) ? g_strdup (vfs_str_buffer->str) : NULL;
948 if (!sys_cwd)
949 return current_dir;
951 /* Otherwise check if it is O.K. to use the current_dir */
952 if (!cd_symlinks || mc_stat (sys_cwd, &my_stat)
953 || mc_stat (current_dir, &my_stat2)
954 || my_stat.st_ino != my_stat2.st_ino
955 || my_stat.st_dev != my_stat2.st_dev) {
956 g_free (current_dir);
957 current_dir = sys_cwd;
958 return sys_cwd;
959 }/* Otherwise we return current_dir below */
963 g_free (trans);
964 return current_dir;
967 static void
968 vfs_setup_wd (void)
970 current_dir = g_strdup (PATH_SEP_STR);
971 _vfs_get_cwd ();
973 if (strlen (current_dir) > MC_MAXPATHLEN - 2)
974 vfs_die ("Current dir too long.\n");
976 current_vfs = vfs_get_class (current_dir);
980 * Return current directory. If it's local, reread the current directory
981 * from the OS. Put directory to the provided buffer.
983 char *
984 mc_get_current_wd (char *buffer, int size)
986 const char *cwd = _vfs_get_cwd ();
988 g_strlcpy (buffer, cwd, size);
989 return buffer;
993 * Return current directory without any OS calls.
995 char *
996 vfs_get_current_dir (void)
998 return current_dir;
1001 off_t mc_lseek (int fd, off_t offset, int whence)
1003 struct vfs_class *vfs;
1004 int result;
1006 if (fd == -1)
1007 return -1;
1009 vfs = vfs_op (fd);
1010 if (vfs == NULL)
1011 return -1;
1013 result = vfs->lseek ? (*vfs->lseek)(vfs_info (fd), offset, whence) : -1;
1014 if (result == -1)
1015 errno = vfs->lseek ? ferrno (vfs) : E_NOTSUPP;
1016 return result;
1020 * remove //, /./ and /../
1023 #define ISSLASH(a) (!a || (a == '/'))
1025 char *
1026 vfs_canon (const char *path)
1028 if (!path)
1029 vfs_die("Cannot canonicalize NULL");
1031 /* Relative to current directory */
1032 if (*path != PATH_SEP){
1033 char *local, *result;
1035 local = concat_dir_and_file (current_dir, path);
1037 result = vfs_canon (local);
1038 g_free (local);
1039 return result;
1043 * So we have path of following form:
1044 * /p1/p2#op/.././././p3#op/p4. Good luck.
1047 char *result = g_strdup (path);
1048 canonicalize_pathname (result);
1049 return result;
1054 * VFS chdir.
1055 * Return 0 on success, -1 on failure.
1058 mc_chdir (const char *path)
1060 char *new_dir;
1061 char *trans_dir;
1062 struct vfs_class *old_vfs, *new_vfs;
1063 vfsid old_vfsid;
1064 int result;
1066 new_dir = vfs_canon (path);
1067 trans_dir = vfs_translate_path_n (new_dir);
1068 if (trans_dir != NULL) {
1069 new_vfs = vfs_get_class (trans_dir);
1070 if (!new_vfs->chdir) {
1071 g_free (new_dir);
1072 g_free (trans_dir);
1073 return -1;
1076 result = (*new_vfs->chdir) (new_vfs, trans_dir);
1078 if (result == -1) {
1079 errno = ferrno (new_vfs);
1080 g_free (new_dir);
1081 g_free (trans_dir);
1082 return -1;
1085 old_vfsid = vfs_getid (current_vfs, current_dir);
1086 old_vfs = current_vfs;
1088 /* Actually change directory */
1089 g_free (current_dir);
1090 current_dir = new_dir;
1091 current_vfs = new_vfs;
1093 /* This function uses the new current_dir implicitly */
1094 vfs_stamp_create (old_vfs, old_vfsid);
1096 /* Sometimes we assume no trailing slash on cwd */
1097 if (*current_dir) {
1098 char *p;
1099 p = strchr (current_dir, 0) - 1;
1100 if (*p == PATH_SEP && p > current_dir)
1101 *p = 0;
1104 g_free (trans_dir);
1105 return 0;
1106 } else {
1107 g_free (new_dir);
1108 return -1;
1112 /* Return 1 is the current VFS class is local */
1114 vfs_current_is_local (void)
1116 return (current_vfs->flags & VFSF_LOCAL) != 0;
1119 /* Return flags of the VFS class of the given filename */
1121 vfs_file_class_flags (const char *filename)
1123 struct vfs_class *vfs;
1124 char *fname;
1126 fname = vfs_canon_and_translate (filename);
1127 if (fname != NULL) {
1128 vfs = vfs_get_class (fname);
1129 g_free (fname);
1130 return vfs->flags;
1131 } else return -1;
1134 static char *
1135 mc_def_getlocalcopy (const char *filename)
1137 char *tmp;
1138 int fdin, fdout;
1139 ssize_t i;
1140 char buffer[8192];
1141 struct stat mystat;
1143 fdin = mc_open (filename, O_RDONLY | O_LINEAR);
1144 if (fdin == -1)
1145 return NULL;
1147 fdout = vfs_mkstemps (&tmp, "vfs", filename);
1149 if (fdout == -1)
1150 goto fail;
1151 while ((i = mc_read (fdin, buffer, sizeof (buffer))) > 0) {
1152 if (write (fdout, buffer, i) != i)
1153 goto fail;
1155 if (i == -1)
1156 goto fail;
1157 i = mc_close (fdin);
1158 fdin = -1;
1159 if (i == -1)
1160 goto fail;
1161 if (close (fdout) == -1) {
1162 fdout = -1;
1163 goto fail;
1166 if (mc_stat (filename, &mystat) != -1) {
1167 chmod (tmp, mystat.st_mode);
1169 return tmp;
1171 fail:
1172 if (fdout != -1)
1173 close (fdout);
1174 if (fdin != -1)
1175 mc_close (fdin);
1176 g_free (tmp);
1177 return NULL;
1180 char *
1181 mc_getlocalcopy (const char *pathname)
1183 char *result;
1184 char *path;
1186 path = vfs_canon_and_translate (pathname);
1187 if (path != NULL) {
1188 struct vfs_class *vfs = vfs_get_class (path);
1190 result = vfs->getlocalcopy ? (*vfs->getlocalcopy)(vfs, path) :
1191 mc_def_getlocalcopy (path);
1192 g_free (path);
1193 if (!result)
1194 errno = ferrno (vfs);
1195 return result;
1196 } else return NULL;
1199 static int
1200 mc_def_ungetlocalcopy (struct vfs_class *vfs, const char *filename,
1201 const char *local, int has_changed)
1203 int fdin = -1, fdout = -1, i;
1204 if (has_changed) {
1205 char buffer[8192];
1207 if (!vfs->write)
1208 goto failed;
1210 fdin = open (local, O_RDONLY);
1211 if (fdin == -1)
1212 goto failed;
1213 fdout = mc_open (filename, O_WRONLY | O_TRUNC);
1214 if (fdout == -1)
1215 goto failed;
1216 while ((i = read (fdin, buffer, sizeof (buffer))) > 0) {
1217 if (mc_write (fdout, buffer, i) != i)
1218 goto failed;
1220 if (i == -1)
1221 goto failed;
1223 if (close (fdin) == -1) {
1224 fdin = -1;
1225 goto failed;
1227 fdin = -1;
1228 if (mc_close (fdout) == -1) {
1229 fdout = -1;
1230 goto failed;
1233 unlink (local);
1234 return 0;
1236 failed:
1237 message (D_ERROR, _("Changes to file lost"), "%s", filename);
1238 if (fdout != -1)
1239 mc_close (fdout);
1240 if (fdin != -1)
1241 close (fdin);
1242 unlink (local);
1243 return -1;
1247 mc_ungetlocalcopy (const char *pathname, const char *local, int has_changed)
1249 int return_value = 0;
1250 char *path;
1252 path = vfs_canon_and_translate (pathname);
1253 if (path != NULL) {
1254 struct vfs_class *vfs = vfs_get_class (path);
1256 return_value = vfs->ungetlocalcopy ?
1257 (*vfs->ungetlocalcopy)(vfs, path, local, has_changed) :
1258 mc_def_ungetlocalcopy (vfs, path, local, has_changed);
1259 g_free (path);
1260 return return_value;
1261 } else return -1;
1265 void
1266 vfs_init (void)
1268 /* create the VFS handle array */
1269 vfs_openfiles = g_ptr_array_new ();
1271 vfs_str_buffer = g_string_new("");
1272 /* localfs needs to be the first one */
1273 init_localfs();
1274 /* fallback value for vfs_get_class() */
1275 localfs_class = vfs_list;
1277 init_extfs ();
1278 init_sfs ();
1279 init_tarfs ();
1280 init_cpiofs ();
1282 #ifdef USE_EXT2FSLIB
1283 init_undelfs ();
1284 #endif /* USE_EXT2FSLIB */
1286 #ifdef USE_NETCODE
1287 tcp_init();
1288 init_ftpfs ();
1289 init_fish ();
1290 #ifdef WITH_SMBFS
1291 init_smbfs ();
1292 #endif /* WITH_SMBFS */
1293 #ifdef ENABLE_VFS_MCFS
1294 init_mcfs ();
1295 #endif /* ENABLE_VFS_MCFS */
1296 #endif /* USE_NETCODE */
1298 vfs_setup_wd ();
1301 void
1302 vfs_shut (void)
1304 struct vfs_class *vfs;
1306 vfs_gc_done ();
1308 g_free (current_dir);
1310 for (vfs = vfs_list; vfs; vfs = vfs->next)
1311 if (vfs->done)
1312 (*vfs->done) (vfs);
1314 g_ptr_array_free (vfs_openfiles, TRUE);
1315 g_string_free (vfs_str_buffer, TRUE);
1316 g_free (mc_readdir_result);
1320 * These ones grab information from the VFS
1321 * and handles them to an upper layer
1323 void
1324 vfs_fill_names (fill_names_f func)
1326 struct vfs_class *vfs;
1328 for (vfs=vfs_list; vfs; vfs=vfs->next)
1329 if (vfs->fill_names)
1330 (*vfs->fill_names) (vfs, func);
1334 * Returns vfs path corresponding to given url. If passed string is
1335 * not recognized as url, g_strdup(url) is returned.
1338 static const struct {
1339 const char *name;
1340 size_t name_len;
1341 const char *substitute;
1342 } url_table[] = { {"ftp://", 6, "/#ftp:"},
1343 {"mc://", 5, "/#mc:"},
1344 {"smb://", 6, "/#smb:"},
1345 {"sh://", 5, "/#sh:"},
1346 {"ssh://", 6, "/#sh:"},
1347 {"a:", 2, "/#a"}
1350 char *
1351 vfs_translate_url (const char *url)
1353 size_t i;
1355 for (i = 0; i < sizeof (url_table)/sizeof (url_table[0]); i++)
1356 if (strncmp (url, url_table[i].name, url_table[i].name_len) == 0)
1357 return g_strconcat (url_table[i].substitute, url + url_table[i].name_len, (char*) NULL);
1359 return g_strdup (url);
1362 int vfs_file_is_local (const char *filename)
1364 return vfs_file_class_flags (filename) & VFSF_LOCAL;