Ticket #1545: Don't escape tilde on cmdline, this is not necessary.
[midnight-commander.git] / vfs / vfs.c
blobcbe24afa1a5481768a561d1c8be1fb3aa89e0b5a
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 inline 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 inline 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 inline 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 inline 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 '/p1#op/inpath' into inpath,op; returns which vfs it is.
269 * What is left in path is p1. You still want to g_free(path), you DON'T
270 * want to free neither *inpath nor *op
272 struct vfs_class *
273 vfs_split (char *path, char **inpath, char **op)
275 char *semi;
276 char *slash;
277 struct vfs_class *ret;
279 if (!path)
280 vfs_die("Cannot split NULL");
282 semi = strrchr (path, '#');
283 if (!semi || !path_magic(path))
284 return NULL;
286 slash = strchr (semi, PATH_SEP);
287 *semi = 0;
289 if (op)
290 *op = NULL;
292 if (inpath)
293 *inpath = NULL;
295 if (slash)
296 *slash = 0;
298 if ((ret = vfs_prefix_to_class (semi+1))){
299 if (op)
300 *op = semi + 1;
301 if (inpath)
302 *inpath = slash ? slash + 1 : NULL;
303 return ret;
307 if (slash)
308 *slash = PATH_SEP;
309 ret = vfs_split (path, inpath, op);
310 *semi = '#';
311 return ret;
314 static struct vfs_class *
315 _vfs_get_class (char *path)
317 char *semi;
318 char *slash;
319 struct vfs_class *ret;
321 g_return_val_if_fail(path, NULL);
323 semi = strrchr (path, '#');
324 if (!semi || !path_magic (path))
325 return NULL;
327 slash = strchr (semi, PATH_SEP);
328 *semi = 0;
329 if (slash)
330 *slash = 0;
332 ret = vfs_prefix_to_class (semi+1);
334 if (slash)
335 *slash = PATH_SEP;
336 if (!ret)
337 ret = _vfs_get_class (path);
339 *semi = '#';
340 return ret;
343 struct vfs_class *
344 vfs_get_class (const char *pathname)
346 struct vfs_class *vfs;
347 char *path = g_strdup (pathname);
349 vfs = _vfs_get_class (path);
350 g_free (path);
352 if (!vfs)
353 vfs = localfs_class;
355 return vfs;
358 const char *
359 vfs_get_encoding (const char *path)
361 static char result[16];
362 char *work;
363 char *semi;
364 char *slash;
366 work = g_strdup (path);
367 semi = g_strrstr (work, "#enc:");
369 if (semi != NULL) {
370 semi+= 5 * sizeof (char);
371 slash = strchr (semi, PATH_SEP);
372 if (slash != NULL)
373 slash[0] = '\0';
375 g_strlcpy (result, semi, sizeof(result));
376 g_free (work);
377 return result;
378 } else {
379 g_free (work);
380 return NULL;
384 /* return if encoding can by used in vfs (is ascci full compactible) */
385 /* contains only a few encoding now */
386 static int
387 vfs_supported_enconding (const char *encoding) {
388 int t;
389 int result = 0;
391 for (t = 0; supported_encodings[t] != NULL; t++) {
392 result+= (g_ascii_strncasecmp (encoding, supported_encodings[t],
393 strlen (supported_encodings[t])) == 0);
396 return result;
399 /* now used only by vfs_translate_path, but could be used in other vfs
400 * plugin to automatic detect encoding
401 * path - path to translate
402 * size - how many bytes from path translate
403 * defcnv - convertor, that is used as default, when path does not contain any
404 * #enc: subtring
405 * buffer - used to store result of translation
407 static estr_t
408 _vfs_translate_path (const char *path, int size,
409 GIConv defcnv, GString *buffer)
411 const char *semi;
412 const char *ps;
413 const char *slash;
414 estr_t state = ESTR_SUCCESS;
415 static char encoding[16];
416 GIConv coder;
417 int ms;
419 if (size == 0) return 0;
420 size = ( size > 0) ? size : (signed int)strlen (path);
422 /* try found #end: */
423 semi = g_strrstr_len (path, size, "#enc:");
424 if (semi != NULL) {
425 /* first must be translated part before #enc: */
426 ms = semi - path;
428 /* remove '/' before #enc */
429 ps = str_cget_prev_char (semi);
430 if (ps[0] == PATH_SEP) ms = ps - path;
432 state = _vfs_translate_path (path, ms, defcnv, buffer);
434 if (state != ESTR_SUCCESS)
435 return state;
436 /* now can be translated part after #enc: */
438 semi+= 5;
439 slash = strchr (semi, PATH_SEP);
440 /* ignore slashes after size; */
441 if (slash - path >= size) slash = NULL;
443 ms = (slash != NULL) ? slash - semi : (int) strlen (semi);
444 ms = min ((unsigned int) ms, sizeof (encoding) - 1);
445 /* limit encoding size (ms) to path size (size) */
446 if (semi + ms > path + size) ms = path + size - semi;
447 memcpy (encoding, semi, ms);
448 encoding[ms] = '\0';
450 switch (vfs_supported_enconding (encoding)) {
451 case 1:
452 coder = str_crt_conv_to (encoding);
453 if (coder != INVALID_CONV) {
454 if (slash != NULL) {
455 state = str_vfs_convert_to (coder, slash,
456 path + size - slash, buffer);
457 } else if (buffer->str[0] == '\0') {
458 /* exmaple "/#enc:utf-8" */
459 g_string_append_c(buffer, PATH_SEP);
461 str_close_conv (coder);
462 return state;
463 } else {
464 errno = EINVAL;
465 return ESTR_FAILURE;
467 break;
468 default:
469 errno = EINVAL;
470 return ESTR_FAILURE;
472 } else {
473 /* path can be translated whole at once */
474 state = str_vfs_convert_to (defcnv, path, size, buffer);
475 return state;
478 return ESTR_SUCCESS;
481 char *
482 vfs_translate_path (const char *path)
484 estr_t state;
486 g_string_set_size(vfs_str_buffer,0);
487 state = _vfs_translate_path (path, -1, str_cnv_from_term, vfs_str_buffer);
489 strict version
490 return (state == 0) ? vfs_str_buffer->data : NULL;
492 return (state != ESTR_FAILURE) ? vfs_str_buffer->str : NULL;
495 char *
496 vfs_translate_path_n (const char *path)
498 char *result;
500 result = vfs_translate_path (path);
501 return (result != NULL) ? g_strdup (result) : NULL;
504 char *
505 vfs_canon_and_translate (const char *path)
507 char *canon;
508 char *result;
509 if (path == NULL)
510 canon = g_strdup ("");
511 else
512 canon = vfs_canon (path);
513 result = vfs_translate_path_n (canon);
514 g_free (canon);
515 return result;
518 static int
519 ferrno (struct vfs_class *vfs)
521 return vfs->ferrno ? (*vfs->ferrno)(vfs) : E_UNKNOWN;
522 /* Hope that error message is obscure enough ;-) */
526 mc_open (const char *filename, int flags, ...)
528 int mode;
529 void *info;
530 va_list ap;
532 char *file = vfs_canon_and_translate (filename);
533 if (file != NULL) {
534 struct vfs_class *vfs = vfs_get_class (file);
536 /* Get the mode flag */
537 if (flags & O_CREAT) {
538 va_start (ap, flags);
539 mode = va_arg (ap, int);
540 va_end (ap);
541 } else
542 mode = 0;
544 if (!vfs->open) {
545 g_free (file);
546 errno = -EOPNOTSUPP;
547 return -1;
550 info = (*vfs->open) (vfs, file, flags, mode); /* open must be supported */
551 g_free (file);
552 if (!info){
553 errno = ferrno (vfs);
554 return -1;
557 return vfs_new_handle (vfs, info);
558 } else return -1;
562 #define MC_NAMEOP(name, inarg, callarg) \
563 int mc_##name inarg \
565 struct vfs_class *vfs; \
566 int result; \
567 char *mpath = vfs_canon_and_translate (path); \
568 if (mpath != NULL) { \
569 vfs = vfs_get_class (mpath); \
570 if (vfs == NULL){ \
571 g_free (mpath); \
572 return -1; \
574 result = vfs->name ? (*vfs->name)callarg : -1; \
575 g_free (mpath); \
576 if (result == -1) \
577 errno = vfs->name ? ferrno (vfs) : E_NOTSUPP; \
578 return result; \
579 } else return -1; \
582 MC_NAMEOP (chmod, (const char *path, mode_t mode), (vfs, mpath, mode))
583 MC_NAMEOP (chown, (const char *path, uid_t owner, gid_t group), (vfs, mpath, owner, group))
584 MC_NAMEOP (utime, (const char *path, struct utimbuf *times), (vfs, mpath, times))
585 MC_NAMEOP (readlink, (const char *path, char *buf, int bufsiz), (vfs, mpath, buf, bufsiz))
586 MC_NAMEOP (unlink, (const char *path), (vfs, mpath))
587 MC_NAMEOP (mkdir, (const char *path, mode_t mode), (vfs, mpath, mode))
588 MC_NAMEOP (rmdir, (const char *path), (vfs, mpath))
589 MC_NAMEOP (mknod, (const char *path, mode_t mode, dev_t dev), (vfs, mpath, mode, dev))
591 int
592 mc_symlink (const char *name1, const char *path)
594 struct vfs_class *vfs;
595 int result;
596 char *mpath;
597 char *lpath;
598 char *tmp;
600 mpath = vfs_canon_and_translate (path);
601 if (mpath != NULL) {
602 tmp = g_strdup (name1);
603 lpath = vfs_translate_path_n (tmp);
604 g_free (tmp);
606 if (lpath != NULL) {
607 vfs = vfs_get_class (mpath);
608 result = vfs->symlink ? (*vfs->symlink) (vfs, lpath, mpath) : -1;
609 g_free (lpath);
611 if (result == -1)
612 errno = vfs->symlink ? ferrno (vfs) : E_NOTSUPP;
613 return result;
615 g_free (mpath);
617 return -1;
620 #define MC_HANDLEOP(name, inarg, callarg) \
621 ssize_t mc_##name inarg \
623 struct vfs_class *vfs; \
624 int result; \
625 if (handle == -1) \
626 return -1; \
627 vfs = vfs_op (handle); \
628 if (vfs == NULL) \
629 return -1; \
630 result = vfs->name ? (*vfs->name)callarg : -1; \
631 if (result == -1) \
632 errno = vfs->name ? ferrno (vfs) : E_NOTSUPP; \
633 return result; \
636 MC_HANDLEOP(read, (int handle, void *buffer, int count), (vfs_info (handle), buffer, count))
637 MC_HANDLEOP(write, (int handle, const void *buf, int nbyte), (vfs_info (handle), buf, nbyte))
640 #define MC_RENAMEOP(name) \
641 int mc_##name (const char *fname1, const char *fname2) \
643 struct vfs_class *vfs; \
644 int result; \
645 char *name2, *name1; \
646 name1 = vfs_canon_and_translate (fname1); \
647 if (name1 != NULL) { \
648 name2 = vfs_canon_and_translate (fname2); \
649 if (name2 != NULL) { \
650 vfs = vfs_get_class (name1); \
651 if (vfs != vfs_get_class (name2)){ \
652 errno = EXDEV; \
653 g_free (name1); \
654 g_free (name2); \
655 return -1; \
657 result = vfs->name ? (*vfs->name)(vfs, name1, name2) : -1; \
658 g_free (name1); \
659 g_free (name2); \
660 if (result == -1) \
661 errno = vfs->name ? ferrno (vfs) : E_NOTSUPP; \
662 return result; \
663 } else { \
664 g_free (name1); \
665 return -1; \
667 } else return -1; \
670 MC_RENAMEOP (link)
671 MC_RENAMEOP (rename)
675 mc_ctl (int handle, int ctlop, void *arg)
677 struct vfs_class *vfs = vfs_op (handle);
679 return vfs->ctl ? (*vfs->ctl)(vfs_info (handle), ctlop, arg) : 0;
683 mc_setctl (const char *path, int ctlop, void *arg)
685 struct vfs_class *vfs;
686 int result;
687 char *mpath;
689 if (!path)
690 vfs_die("You don't want to pass NULL to mc_setctl.");
692 mpath = vfs_canon_and_translate (path);
693 if (mpath != NULL) {
694 vfs = vfs_get_class (mpath);
695 result = vfs->setctl ? (*vfs->setctl)(vfs, mpath, ctlop, arg) : 0;
696 g_free (mpath);
697 return result;
698 } else return -1;
702 mc_close (int handle)
704 struct vfs_class *vfs;
705 int result;
707 if (handle == -1 || !vfs_info (handle))
708 return -1;
710 vfs = vfs_op (handle);
711 if (handle < 3)
712 return close (handle);
714 if (!vfs->close)
715 vfs_die ("VFS must support close.\n");
716 result = (*vfs->close)(vfs_info (handle));
717 vfs_free_handle (handle);
718 if (result == -1)
719 errno = ferrno (vfs);
721 return result;
724 DIR *
725 mc_opendir (const char *dirname)
727 int handle, *handlep;
728 void *info;
729 struct vfs_class *vfs;
730 char *canon;
731 char *dname;
732 struct vfs_dirinfo *dirinfo;
733 const char *encoding;
735 canon = vfs_canon (dirname);
736 dname = vfs_translate_path_n (canon);
738 if (dname != NULL) {
739 vfs = vfs_get_class (dname);
740 info = vfs->opendir ? (*vfs->opendir)(vfs, dname) : NULL;
741 g_free (dname);
743 if (!info){
744 errno = vfs->opendir ? ferrno (vfs) : E_NOTSUPP;
745 g_free (canon);
746 return NULL;
749 dirinfo = g_new (struct vfs_dirinfo, 1);
750 dirinfo->info = info;
752 encoding = vfs_get_encoding (canon);
753 g_free (canon);
754 dirinfo->converter = (encoding != NULL) ? str_crt_conv_from (encoding) :
755 str_cnv_from_term;
756 if (dirinfo->converter == INVALID_CONV) dirinfo->converter =str_cnv_from_term;
758 handle = vfs_new_handle (vfs, dirinfo);
760 handlep = g_new (int, 1);
761 *handlep = handle;
762 return (DIR *) handlep;
763 } else {
764 g_free (canon);
765 return NULL;
769 static struct dirent * mc_readdir_result = NULL;
771 struct dirent *
772 mc_readdir (DIR *dirp)
774 int handle;
775 struct vfs_class *vfs;
776 struct dirent *entry = NULL;
777 struct vfs_dirinfo *dirinfo;
778 estr_t state;
780 if (!mc_readdir_result)
782 /* We can't just allocate struct dirent as (see man dirent.h)
783 * struct dirent has VERY nonnaive semantics of allocating
784 * d_name in it. Moreover, linux's glibc-2.9 allocates dirents _less_,
785 * than 'sizeof (struct dirent)' making full bitwise (sizeof dirent) copy
786 * heap corrupter. So, allocate longliving dirent with at least
787 * (NAME_MAX + 1) for d_name in it.
788 * Strictly saying resulting dirent is unusable as we don't adjust internal
789 * structures, holding dirent size. But we don't use it in libc infrastructure.
790 * TODO: to make simpler homemade dirent-alike structure.
792 mc_readdir_result = (struct dirent *)malloc(sizeof(struct dirent) + NAME_MAX + 1);
795 if (!dirp) {
796 errno = EFAULT;
797 return NULL;
799 handle = *(int *) dirp;
800 vfs = vfs_op (handle);
801 dirinfo = vfs_info (handle);
802 if (vfs->readdir) {
803 entry = (*vfs->readdir) (dirinfo->info);
804 if (entry == NULL) return NULL;
805 g_string_set_size(vfs_str_buffer,0);
806 state = str_vfs_convert_from (dirinfo->converter,
807 entry->d_name, vfs_str_buffer);
808 mc_readdir_result->d_ino = entry->d_ino;
809 g_strlcpy (mc_readdir_result->d_name, vfs_str_buffer->str, NAME_MAX + 1);
811 if (entry == NULL) errno = vfs->readdir ? ferrno (vfs) : E_NOTSUPP;
812 return (entry != NULL) ? mc_readdir_result : NULL;
816 mc_closedir (DIR *dirp)
818 int handle = *(int *) dirp;
819 struct vfs_class *vfs = vfs_op (handle);
820 int result;
821 struct vfs_dirinfo *dirinfo;
823 dirinfo = vfs_info (handle);
824 if (dirinfo->converter != str_cnv_from_term) str_close_conv (dirinfo->converter);
826 result = vfs->closedir ? (*vfs->closedir)(dirinfo->info) : -1;
827 vfs_free_handle (handle);
828 g_free (dirinfo);
829 g_free (dirp);
830 return result;
833 int mc_stat (const char *filename, struct stat *buf) {
834 struct vfs_class *vfs;
835 int result;
836 char *path;
838 path = vfs_canon_and_translate (filename);
840 if (path != NULL) {
841 vfs = vfs_get_class (path);
843 result = vfs->stat ? (*vfs->stat) (vfs, path, buf) : -1;
845 g_free (path);
847 if (result == -1)
848 errno = vfs->name ? ferrno (vfs) : E_NOTSUPP;
849 return result;
850 } else return -1;
853 int mc_lstat (const char *filename, struct stat *buf) {
854 struct vfs_class *vfs;
855 int result;
856 char *path;
858 path = vfs_canon_and_translate (filename);
860 if (path != NULL) {
861 vfs = vfs_get_class (path);
862 result = vfs->lstat ? (*vfs->lstat) (vfs, path, buf) : -1;
863 g_free (path);
864 if (result == -1)
865 errno = vfs->name ? ferrno (vfs) : E_NOTSUPP;
866 return result;
867 } else return -1;
870 int mc_fstat (int handle, struct stat *buf) {
871 struct vfs_class *vfs;
872 int result;
874 if (handle == -1)
875 return -1;
876 vfs = vfs_op (handle);
877 result = vfs->fstat ? (*vfs->fstat) (vfs_info (handle), buf) : -1;
878 if (result == -1)
879 errno = vfs->name ? ferrno (vfs) : E_NOTSUPP;
880 return result;
884 * Return current directory. If it's local, reread the current directory
885 * from the OS. You must g_strdup() whatever this function returns.
887 static const char *
888 _vfs_get_cwd (void)
890 char *sys_cwd;
891 char *trans;
892 const char *encoding;
893 char *tmp;
894 estr_t state;
895 struct stat my_stat, my_stat2;
897 trans = vfs_translate_path_n (current_dir); /* add check if NULL */
899 if (!_vfs_get_class (trans)) {
900 encoding = vfs_get_encoding (current_dir);
901 if (encoding == NULL) {
902 tmp = g_get_current_dir ();
903 if (tmp != NULL) { /* One of the directories in the path is not readable */
904 g_string_set_size(vfs_str_buffer,0);
905 state = str_vfs_convert_from (str_cnv_from_term, tmp, vfs_str_buffer);
906 g_free (tmp);
907 sys_cwd = (state == ESTR_SUCCESS) ? g_strdup (vfs_str_buffer->str) : NULL;
908 if (!sys_cwd)
909 return current_dir;
911 /* Otherwise check if it is O.K. to use the current_dir */
912 if (!cd_symlinks || mc_stat (sys_cwd, &my_stat)
913 || mc_stat (current_dir, &my_stat2)
914 || my_stat.st_ino != my_stat2.st_ino
915 || my_stat.st_dev != my_stat2.st_dev) {
916 g_free (current_dir);
917 current_dir = sys_cwd;
918 return sys_cwd;
919 }/* Otherwise we return current_dir below */
923 g_free (trans);
924 return current_dir;
927 static void
928 vfs_setup_wd (void)
930 current_dir = g_strdup (PATH_SEP_STR);
931 _vfs_get_cwd ();
933 if (strlen (current_dir) > MC_MAXPATHLEN - 2)
934 vfs_die ("Current dir too long.\n");
936 current_vfs = vfs_get_class (current_dir);
940 * Return current directory. If it's local, reread the current directory
941 * from the OS. Put directory to the provided buffer.
943 char *
944 mc_get_current_wd (char *buffer, int size)
946 const char *cwd = _vfs_get_cwd ();
948 g_strlcpy (buffer, cwd, size);
949 return buffer;
953 * Return current directory without any OS calls.
955 char *
956 vfs_get_current_dir (void)
958 return current_dir;
961 off_t mc_lseek (int fd, off_t offset, int whence)
963 struct vfs_class *vfs;
964 int result;
966 if (fd == -1)
967 return -1;
969 vfs = vfs_op (fd);
970 result = vfs->lseek ? (*vfs->lseek)(vfs_info (fd), offset, whence) : -1;
971 if (result == -1)
972 errno = vfs->lseek ? ferrno (vfs) : E_NOTSUPP;
973 return result;
977 * remove //, /./ and /../
980 #define ISSLASH(a) (!a || (a == '/'))
982 char *
983 vfs_canon (const char *path)
985 if (!path)
986 vfs_die("Cannot canonicalize NULL");
988 /* Relative to current directory */
989 if (*path != PATH_SEP){
990 char *local, *result;
992 local = concat_dir_and_file (current_dir, path);
994 result = vfs_canon (local);
995 g_free (local);
996 return result;
1000 * So we have path of following form:
1001 * /p1/p2#op/.././././p3#op/p4. Good luck.
1004 char *result = g_strdup (path);
1005 canonicalize_pathname (result);
1006 return result;
1011 * VFS chdir.
1012 * Return 0 on success, -1 on failure.
1015 mc_chdir (const char *path)
1017 char *new_dir;
1018 char *trans_dir;
1019 struct vfs_class *old_vfs, *new_vfs;
1020 vfsid old_vfsid;
1021 int result;
1023 new_dir = vfs_canon (path);
1024 trans_dir = vfs_translate_path_n (new_dir);
1025 if (trans_dir != NULL) {
1026 new_vfs = vfs_get_class (trans_dir);
1027 if (!new_vfs->chdir) {
1028 g_free (new_dir);
1029 g_free (trans_dir);
1030 return -1;
1033 result = (*new_vfs->chdir) (new_vfs, trans_dir);
1035 if (result == -1) {
1036 errno = ferrno (new_vfs);
1037 g_free (new_dir);
1038 g_free (trans_dir);
1039 return -1;
1042 old_vfsid = vfs_getid (current_vfs, current_dir);
1043 old_vfs = current_vfs;
1045 /* Actually change directory */
1046 g_free (current_dir);
1047 current_dir = new_dir;
1048 current_vfs = new_vfs;
1050 /* This function uses the new current_dir implicitly */
1051 vfs_stamp_create (old_vfs, old_vfsid);
1053 /* Sometimes we assume no trailing slash on cwd */
1054 if (*current_dir) {
1055 char *p;
1056 p = strchr (current_dir, 0) - 1;
1057 if (*p == PATH_SEP && p > current_dir)
1058 *p = 0;
1061 g_free (trans_dir);
1062 return 0;
1063 } else {
1064 g_free (new_dir);
1065 return -1;
1069 /* Return 1 is the current VFS class is local */
1071 vfs_current_is_local (void)
1073 return (current_vfs->flags & VFSF_LOCAL) != 0;
1076 /* Return flags of the VFS class of the given filename */
1078 vfs_file_class_flags (const char *filename)
1080 struct vfs_class *vfs;
1081 char *fname;
1083 fname = vfs_canon_and_translate (filename);
1084 if (fname != NULL) {
1085 vfs = vfs_get_class (fname);
1086 g_free (fname);
1087 return vfs->flags;
1088 } else return -1;
1091 static char *
1092 mc_def_getlocalcopy (const char *filename)
1094 char *tmp;
1095 int fdin, fdout, i;
1096 char buffer[8192];
1097 struct stat mystat;
1099 fdin = mc_open (filename, O_RDONLY | O_LINEAR);
1100 if (fdin == -1)
1101 return NULL;
1103 fdout = vfs_mkstemps (&tmp, "vfs", filename);
1105 if (fdout == -1)
1106 goto fail;
1107 while ((i = mc_read (fdin, buffer, sizeof (buffer))) > 0) {
1108 if (write (fdout, buffer, i) != i)
1109 goto fail;
1111 if (i == -1)
1112 goto fail;
1113 i = mc_close (fdin);
1114 fdin = -1;
1115 if (i == -1)
1116 goto fail;
1117 if (close (fdout) == -1) {
1118 fdout = -1;
1119 goto fail;
1122 if (mc_stat (filename, &mystat) != -1) {
1123 chmod (tmp, mystat.st_mode);
1125 return tmp;
1127 fail:
1128 if (fdout != -1)
1129 close (fdout);
1130 if (fdin != -1)
1131 mc_close (fdin);
1132 g_free (tmp);
1133 return NULL;
1136 char *
1137 mc_getlocalcopy (const char *pathname)
1139 char *result;
1140 char *path;
1142 path = vfs_canon_and_translate (pathname);
1143 if (path != NULL) {
1144 struct vfs_class *vfs = vfs_get_class (path);
1146 result = vfs->getlocalcopy ? (*vfs->getlocalcopy)(vfs, path) :
1147 mc_def_getlocalcopy (path);
1148 g_free (path);
1149 if (!result)
1150 errno = ferrno (vfs);
1151 return result;
1152 } else return NULL;
1155 static int
1156 mc_def_ungetlocalcopy (struct vfs_class *vfs, const char *filename,
1157 const char *local, int has_changed)
1159 int fdin = -1, fdout = -1, i;
1160 if (has_changed) {
1161 char buffer[8192];
1163 if (!vfs->write)
1164 goto failed;
1166 fdin = open (local, O_RDONLY);
1167 if (fdin == -1)
1168 goto failed;
1169 fdout = mc_open (filename, O_WRONLY | O_TRUNC);
1170 if (fdout == -1)
1171 goto failed;
1172 while ((i = read (fdin, buffer, sizeof (buffer))) > 0) {
1173 if (mc_write (fdout, buffer, i) != i)
1174 goto failed;
1176 if (i == -1)
1177 goto failed;
1179 if (close (fdin) == -1) {
1180 fdin = -1;
1181 goto failed;
1183 fdin = -1;
1184 if (mc_close (fdout) == -1) {
1185 fdout = -1;
1186 goto failed;
1189 unlink (local);
1190 return 0;
1192 failed:
1193 message (D_ERROR, _("Changes to file lost"), "%s", filename);
1194 if (fdout != -1)
1195 mc_close (fdout);
1196 if (fdin != -1)
1197 close (fdin);
1198 unlink (local);
1199 return -1;
1203 mc_ungetlocalcopy (const char *pathname, const char *local, int has_changed)
1205 int return_value = 0;
1206 char *path;
1208 path = vfs_canon_and_translate (pathname);
1209 if (path != NULL) {
1210 struct vfs_class *vfs = vfs_get_class (path);
1212 return_value = vfs->ungetlocalcopy ?
1213 (*vfs->ungetlocalcopy)(vfs, path, local, has_changed) :
1214 mc_def_ungetlocalcopy (vfs, path, local, has_changed);
1215 g_free (path);
1216 return return_value;
1217 } else return -1;
1221 void
1222 vfs_init (void)
1224 /* create the VFS handle array */
1225 vfs_openfiles = g_ptr_array_new ();
1227 vfs_str_buffer = g_string_new("");
1228 /* localfs needs to be the first one */
1229 init_localfs();
1230 /* fallback value for vfs_get_class() */
1231 localfs_class = vfs_list;
1233 init_extfs ();
1234 init_sfs ();
1235 init_tarfs ();
1236 init_cpiofs ();
1238 #ifdef USE_EXT2FSLIB
1239 init_undelfs ();
1240 #endif /* USE_EXT2FSLIB */
1242 #ifdef USE_NETCODE
1243 tcp_init();
1244 init_ftpfs ();
1245 init_fish ();
1246 #ifdef WITH_SMBFS
1247 init_smbfs ();
1248 #endif /* WITH_SMBFS */
1249 #ifdef ENABLE_VFS_MCFS
1250 init_mcfs ();
1251 #endif /* ENABLE_VFS_MCFS */
1252 #endif /* USE_NETCODE */
1254 vfs_setup_wd ();
1257 void
1258 vfs_shut (void)
1260 struct vfs_class *vfs;
1262 vfs_gc_done ();
1264 g_free (current_dir);
1266 for (vfs = vfs_list; vfs; vfs = vfs->next)
1267 if (vfs->done)
1268 (*vfs->done) (vfs);
1270 g_ptr_array_free (vfs_openfiles, TRUE);
1272 g_string_free (vfs_str_buffer, TRUE);
1276 * These ones grab information from the VFS
1277 * and handles them to an upper layer
1279 void
1280 vfs_fill_names (fill_names_f func)
1282 struct vfs_class *vfs;
1284 for (vfs=vfs_list; vfs; vfs=vfs->next)
1285 if (vfs->fill_names)
1286 (*vfs->fill_names) (vfs, func);
1290 * Returns vfs path corresponding to given url. If passed string is
1291 * not recognized as url, g_strdup(url) is returned.
1294 static const struct {
1295 const char *name;
1296 size_t name_len;
1297 const char *substitute;
1298 } url_table[] = { {"ftp://", 6, "/#ftp:"},
1299 {"mc://", 5, "/#mc:"},
1300 {"smb://", 6, "/#smb:"},
1301 {"sh://", 5, "/#sh:"},
1302 {"ssh://", 6, "/#sh:"},
1303 {"a:", 2, "/#a"}
1306 char *
1307 vfs_translate_url (const char *url)
1309 size_t i;
1311 for (i = 0; i < sizeof (url_table)/sizeof (url_table[0]); i++)
1312 if (strncmp (url, url_table[i].name, url_table[i].name_len) == 0)
1313 return g_strconcat (url_table[i].substitute, url + url_table[i].name_len, (char*) NULL);
1315 return g_strdup (url);
1318 int vfs_file_is_local (const char *filename)
1320 return vfs_file_class_flags (filename) & VFSF_LOCAL;