Revert "misc/mc.ext.in: added 3gp video"
[midnight-commander.git] / vfs / vfs.c
blob7c1de542a53a90025b084cdb12c5e61b3c236bce
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 '/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 if (vfs == NULL)
680 return 0;
682 return vfs->ctl ? (*vfs->ctl)(vfs_info (handle), ctlop, arg) : 0;
686 mc_setctl (const char *path, int ctlop, void *arg)
688 struct vfs_class *vfs;
689 int result;
690 char *mpath;
692 if (!path)
693 vfs_die("You don't want to pass NULL to mc_setctl.");
695 mpath = vfs_canon_and_translate (path);
696 if (mpath != NULL) {
697 vfs = vfs_get_class (mpath);
698 result = vfs->setctl ? (*vfs->setctl)(vfs, mpath, ctlop, arg) : 0;
699 g_free (mpath);
700 return result;
701 } else return -1;
705 mc_close (int handle)
707 struct vfs_class *vfs;
708 int result;
710 if (handle == -1 || !vfs_info (handle))
711 return -1;
713 vfs = vfs_op (handle);
714 if (vfs == NULL)
715 return -1;
717 if (handle < 3)
718 return close (handle);
720 if (!vfs->close)
721 vfs_die ("VFS must support close.\n");
722 result = (*vfs->close)(vfs_info (handle));
723 vfs_free_handle (handle);
724 if (result == -1)
725 errno = ferrno (vfs);
727 return result;
730 DIR *
731 mc_opendir (const char *dirname)
733 int handle, *handlep;
734 void *info;
735 struct vfs_class *vfs;
736 char *canon;
737 char *dname;
738 struct vfs_dirinfo *dirinfo;
739 const char *encoding;
741 canon = vfs_canon (dirname);
742 dname = vfs_translate_path_n (canon);
744 if (dname != NULL) {
745 vfs = vfs_get_class (dname);
746 info = vfs->opendir ? (*vfs->opendir)(vfs, dname) : NULL;
747 g_free (dname);
749 if (!info){
750 errno = vfs->opendir ? ferrno (vfs) : E_NOTSUPP;
751 g_free (canon);
752 return NULL;
755 dirinfo = g_new (struct vfs_dirinfo, 1);
756 dirinfo->info = info;
758 encoding = vfs_get_encoding (canon);
759 g_free (canon);
760 dirinfo->converter = (encoding != NULL) ? str_crt_conv_from (encoding) :
761 str_cnv_from_term;
762 if (dirinfo->converter == INVALID_CONV) dirinfo->converter =str_cnv_from_term;
764 handle = vfs_new_handle (vfs, dirinfo);
766 handlep = g_new (int, 1);
767 *handlep = handle;
768 return (DIR *) handlep;
769 } else {
770 g_free (canon);
771 return NULL;
775 static struct dirent * mc_readdir_result = NULL;
777 struct dirent *
778 mc_readdir (DIR *dirp)
780 int handle;
781 struct vfs_class *vfs;
782 struct dirent *entry = NULL;
783 struct vfs_dirinfo *dirinfo;
784 estr_t state;
786 if (!mc_readdir_result)
788 /* We can't just allocate struct dirent as (see man dirent.h)
789 * struct dirent has VERY nonnaive semantics of allocating
790 * d_name in it. Moreover, linux's glibc-2.9 allocates dirents _less_,
791 * than 'sizeof (struct dirent)' making full bitwise (sizeof dirent) copy
792 * heap corrupter. So, allocate longliving dirent with at least
793 * (NAME_MAX + 1) for d_name in it.
794 * Strictly saying resulting dirent is unusable as we don't adjust internal
795 * structures, holding dirent size. But we don't use it in libc infrastructure.
796 * TODO: to make simpler homemade dirent-alike structure.
798 mc_readdir_result = (struct dirent *)malloc(sizeof(struct dirent) + NAME_MAX + 1);
801 if (!dirp) {
802 errno = EFAULT;
803 return NULL;
805 handle = *(int *) dirp;
807 vfs = vfs_op (handle);
808 if (vfs == NULL)
809 return NULL;
811 dirinfo = vfs_info (handle);
812 if (vfs->readdir) {
813 entry = (*vfs->readdir) (dirinfo->info);
814 if (entry == NULL) return NULL;
815 g_string_set_size(vfs_str_buffer,0);
816 state = str_vfs_convert_from (dirinfo->converter,
817 entry->d_name, vfs_str_buffer);
818 mc_readdir_result->d_ino = entry->d_ino;
819 g_strlcpy (mc_readdir_result->d_name, vfs_str_buffer->str, NAME_MAX + 1);
821 if (entry == NULL) errno = vfs->readdir ? ferrno (vfs) : E_NOTSUPP;
822 return (entry != NULL) ? mc_readdir_result : NULL;
826 mc_closedir (DIR *dirp)
828 int handle = *(int *) dirp;
829 struct vfs_class *vfs = vfs_op (handle);
830 int result;
831 struct vfs_dirinfo *dirinfo;
833 if (vfs == NULL)
834 return -1;
836 dirinfo = vfs_info (handle);
837 if (dirinfo->converter != str_cnv_from_term) str_close_conv (dirinfo->converter);
839 result = vfs->closedir ? (*vfs->closedir)(dirinfo->info) : -1;
840 vfs_free_handle (handle);
841 g_free (dirinfo);
842 g_free (dirp);
843 return result;
846 int mc_stat (const char *filename, struct stat *buf) {
847 struct vfs_class *vfs;
848 int result;
849 char *path;
851 path = vfs_canon_and_translate (filename);
853 if (path == NULL)
854 return -1;
856 vfs = vfs_get_class (path);
858 if (vfs == NULL) {
859 g_free (path);
860 return -1;
863 result = vfs->stat ? (*vfs->stat) (vfs, path, buf) : -1;
865 g_free (path);
867 if (result == -1)
868 errno = vfs->name ? ferrno (vfs) : E_NOTSUPP;
869 return result;
872 int mc_lstat (const char *filename, struct stat *buf) {
873 struct vfs_class *vfs;
874 int result;
875 char *path;
877 path = vfs_canon_and_translate (filename);
879 if (path == NULL)
880 return -1;
882 vfs = vfs_get_class (path);
883 if (vfs == NULL) {
884 g_free (path);
885 return -1;
888 result = vfs->lstat ? (*vfs->lstat) (vfs, path, buf) : -1;
889 g_free (path);
890 if (result == -1)
891 errno = vfs->name ? ferrno (vfs) : E_NOTSUPP;
892 return result;
895 int mc_fstat (int handle, struct stat *buf) {
896 struct vfs_class *vfs;
897 int result;
899 if (handle == -1)
900 return -1;
902 vfs = vfs_op (handle);
903 if (vfs == NULL)
904 return -1;
906 result = vfs->fstat ? (*vfs->fstat) (vfs_info (handle), buf) : -1;
907 if (result == -1)
908 errno = vfs->name ? ferrno (vfs) : E_NOTSUPP;
909 return result;
913 * Return current directory. If it's local, reread the current directory
914 * from the OS. You must g_strdup() whatever this function returns.
916 static const char *
917 _vfs_get_cwd (void)
919 char *sys_cwd;
920 char *trans;
921 const char *encoding;
922 char *tmp;
923 estr_t state;
924 struct stat my_stat, my_stat2;
926 trans = vfs_translate_path_n (current_dir); /* add check if NULL */
928 if (!_vfs_get_class (trans)) {
929 encoding = vfs_get_encoding (current_dir);
930 if (encoding == NULL) {
931 tmp = g_get_current_dir ();
932 if (tmp != NULL) { /* One of the directories in the path is not readable */
933 g_string_set_size(vfs_str_buffer,0);
934 state = str_vfs_convert_from (str_cnv_from_term, tmp, vfs_str_buffer);
935 g_free (tmp);
936 sys_cwd = (state == ESTR_SUCCESS) ? g_strdup (vfs_str_buffer->str) : NULL;
937 if (!sys_cwd)
938 return current_dir;
940 /* Otherwise check if it is O.K. to use the current_dir */
941 if (!cd_symlinks || mc_stat (sys_cwd, &my_stat)
942 || mc_stat (current_dir, &my_stat2)
943 || my_stat.st_ino != my_stat2.st_ino
944 || my_stat.st_dev != my_stat2.st_dev) {
945 g_free (current_dir);
946 current_dir = sys_cwd;
947 return sys_cwd;
948 }/* Otherwise we return current_dir below */
952 g_free (trans);
953 return current_dir;
956 static void
957 vfs_setup_wd (void)
959 current_dir = g_strdup (PATH_SEP_STR);
960 _vfs_get_cwd ();
962 if (strlen (current_dir) > MC_MAXPATHLEN - 2)
963 vfs_die ("Current dir too long.\n");
965 current_vfs = vfs_get_class (current_dir);
969 * Return current directory. If it's local, reread the current directory
970 * from the OS. Put directory to the provided buffer.
972 char *
973 mc_get_current_wd (char *buffer, int size)
975 const char *cwd = _vfs_get_cwd ();
977 g_strlcpy (buffer, cwd, size);
978 return buffer;
982 * Return current directory without any OS calls.
984 char *
985 vfs_get_current_dir (void)
987 return current_dir;
990 off_t mc_lseek (int fd, off_t offset, int whence)
992 struct vfs_class *vfs;
993 int result;
995 if (fd == -1)
996 return -1;
998 vfs = vfs_op (fd);
999 if (vfs == NULL)
1000 return -1;
1002 result = vfs->lseek ? (*vfs->lseek)(vfs_info (fd), offset, whence) : -1;
1003 if (result == -1)
1004 errno = vfs->lseek ? ferrno (vfs) : E_NOTSUPP;
1005 return result;
1009 * remove //, /./ and /../
1012 #define ISSLASH(a) (!a || (a == '/'))
1014 char *
1015 vfs_canon (const char *path)
1017 if (!path)
1018 vfs_die("Cannot canonicalize NULL");
1020 /* Relative to current directory */
1021 if (*path != PATH_SEP){
1022 char *local, *result;
1024 local = concat_dir_and_file (current_dir, path);
1026 result = vfs_canon (local);
1027 g_free (local);
1028 return result;
1032 * So we have path of following form:
1033 * /p1/p2#op/.././././p3#op/p4. Good luck.
1036 char *result = g_strdup (path);
1037 canonicalize_pathname (result);
1038 return result;
1043 * VFS chdir.
1044 * Return 0 on success, -1 on failure.
1047 mc_chdir (const char *path)
1049 char *new_dir;
1050 char *trans_dir;
1051 struct vfs_class *old_vfs, *new_vfs;
1052 vfsid old_vfsid;
1053 int result;
1055 new_dir = vfs_canon (path);
1056 trans_dir = vfs_translate_path_n (new_dir);
1057 if (trans_dir != NULL) {
1058 new_vfs = vfs_get_class (trans_dir);
1059 if (!new_vfs->chdir) {
1060 g_free (new_dir);
1061 g_free (trans_dir);
1062 return -1;
1065 result = (*new_vfs->chdir) (new_vfs, trans_dir);
1067 if (result == -1) {
1068 errno = ferrno (new_vfs);
1069 g_free (new_dir);
1070 g_free (trans_dir);
1071 return -1;
1074 old_vfsid = vfs_getid (current_vfs, current_dir);
1075 old_vfs = current_vfs;
1077 /* Actually change directory */
1078 g_free (current_dir);
1079 current_dir = new_dir;
1080 current_vfs = new_vfs;
1082 /* This function uses the new current_dir implicitly */
1083 vfs_stamp_create (old_vfs, old_vfsid);
1085 /* Sometimes we assume no trailing slash on cwd */
1086 if (*current_dir) {
1087 char *p;
1088 p = strchr (current_dir, 0) - 1;
1089 if (*p == PATH_SEP && p > current_dir)
1090 *p = 0;
1093 g_free (trans_dir);
1094 return 0;
1095 } else {
1096 g_free (new_dir);
1097 return -1;
1101 /* Return 1 is the current VFS class is local */
1103 vfs_current_is_local (void)
1105 return (current_vfs->flags & VFSF_LOCAL) != 0;
1108 /* Return flags of the VFS class of the given filename */
1110 vfs_file_class_flags (const char *filename)
1112 struct vfs_class *vfs;
1113 char *fname;
1115 fname = vfs_canon_and_translate (filename);
1116 if (fname != NULL) {
1117 vfs = vfs_get_class (fname);
1118 g_free (fname);
1119 return vfs->flags;
1120 } else return -1;
1123 static char *
1124 mc_def_getlocalcopy (const char *filename)
1126 char *tmp;
1127 int fdin, fdout, i;
1128 char buffer[8192];
1129 struct stat mystat;
1131 fdin = mc_open (filename, O_RDONLY | O_LINEAR);
1132 if (fdin == -1)
1133 return NULL;
1135 fdout = vfs_mkstemps (&tmp, "vfs", filename);
1137 if (fdout == -1)
1138 goto fail;
1139 while ((i = mc_read (fdin, buffer, sizeof (buffer))) > 0) {
1140 if (write (fdout, buffer, i) != i)
1141 goto fail;
1143 if (i == -1)
1144 goto fail;
1145 i = mc_close (fdin);
1146 fdin = -1;
1147 if (i == -1)
1148 goto fail;
1149 if (close (fdout) == -1) {
1150 fdout = -1;
1151 goto fail;
1154 if (mc_stat (filename, &mystat) != -1) {
1155 chmod (tmp, mystat.st_mode);
1157 return tmp;
1159 fail:
1160 if (fdout != -1)
1161 close (fdout);
1162 if (fdin != -1)
1163 mc_close (fdin);
1164 g_free (tmp);
1165 return NULL;
1168 char *
1169 mc_getlocalcopy (const char *pathname)
1171 char *result;
1172 char *path;
1174 path = vfs_canon_and_translate (pathname);
1175 if (path != NULL) {
1176 struct vfs_class *vfs = vfs_get_class (path);
1178 result = vfs->getlocalcopy ? (*vfs->getlocalcopy)(vfs, path) :
1179 mc_def_getlocalcopy (path);
1180 g_free (path);
1181 if (!result)
1182 errno = ferrno (vfs);
1183 return result;
1184 } else return NULL;
1187 static int
1188 mc_def_ungetlocalcopy (struct vfs_class *vfs, const char *filename,
1189 const char *local, int has_changed)
1191 int fdin = -1, fdout = -1, i;
1192 if (has_changed) {
1193 char buffer[8192];
1195 if (!vfs->write)
1196 goto failed;
1198 fdin = open (local, O_RDONLY);
1199 if (fdin == -1)
1200 goto failed;
1201 fdout = mc_open (filename, O_WRONLY | O_TRUNC);
1202 if (fdout == -1)
1203 goto failed;
1204 while ((i = read (fdin, buffer, sizeof (buffer))) > 0) {
1205 if (mc_write (fdout, buffer, i) != i)
1206 goto failed;
1208 if (i == -1)
1209 goto failed;
1211 if (close (fdin) == -1) {
1212 fdin = -1;
1213 goto failed;
1215 fdin = -1;
1216 if (mc_close (fdout) == -1) {
1217 fdout = -1;
1218 goto failed;
1221 unlink (local);
1222 return 0;
1224 failed:
1225 message (D_ERROR, _("Changes to file lost"), "%s", filename);
1226 if (fdout != -1)
1227 mc_close (fdout);
1228 if (fdin != -1)
1229 close (fdin);
1230 unlink (local);
1231 return -1;
1235 mc_ungetlocalcopy (const char *pathname, const char *local, int has_changed)
1237 int return_value = 0;
1238 char *path;
1240 path = vfs_canon_and_translate (pathname);
1241 if (path != NULL) {
1242 struct vfs_class *vfs = vfs_get_class (path);
1244 return_value = vfs->ungetlocalcopy ?
1245 (*vfs->ungetlocalcopy)(vfs, path, local, has_changed) :
1246 mc_def_ungetlocalcopy (vfs, path, local, has_changed);
1247 g_free (path);
1248 return return_value;
1249 } else return -1;
1253 void
1254 vfs_init (void)
1256 /* create the VFS handle array */
1257 vfs_openfiles = g_ptr_array_new ();
1259 vfs_str_buffer = g_string_new("");
1260 /* localfs needs to be the first one */
1261 init_localfs();
1262 /* fallback value for vfs_get_class() */
1263 localfs_class = vfs_list;
1265 init_extfs ();
1266 init_sfs ();
1267 init_tarfs ();
1268 init_cpiofs ();
1270 #ifdef USE_EXT2FSLIB
1271 init_undelfs ();
1272 #endif /* USE_EXT2FSLIB */
1274 #ifdef USE_NETCODE
1275 tcp_init();
1276 init_ftpfs ();
1277 init_fish ();
1278 #ifdef WITH_SMBFS
1279 init_smbfs ();
1280 #endif /* WITH_SMBFS */
1281 #ifdef ENABLE_VFS_MCFS
1282 init_mcfs ();
1283 #endif /* ENABLE_VFS_MCFS */
1284 #endif /* USE_NETCODE */
1286 vfs_setup_wd ();
1289 void
1290 vfs_shut (void)
1292 struct vfs_class *vfs;
1294 vfs_gc_done ();
1296 g_free (current_dir);
1298 for (vfs = vfs_list; vfs; vfs = vfs->next)
1299 if (vfs->done)
1300 (*vfs->done) (vfs);
1302 g_ptr_array_free (vfs_openfiles, TRUE);
1303 g_string_free (vfs_str_buffer, TRUE);
1304 g_free (mc_readdir_result);
1308 * These ones grab information from the VFS
1309 * and handles them to an upper layer
1311 void
1312 vfs_fill_names (fill_names_f func)
1314 struct vfs_class *vfs;
1316 for (vfs=vfs_list; vfs; vfs=vfs->next)
1317 if (vfs->fill_names)
1318 (*vfs->fill_names) (vfs, func);
1322 * Returns vfs path corresponding to given url. If passed string is
1323 * not recognized as url, g_strdup(url) is returned.
1326 static const struct {
1327 const char *name;
1328 size_t name_len;
1329 const char *substitute;
1330 } url_table[] = { {"ftp://", 6, "/#ftp:"},
1331 {"mc://", 5, "/#mc:"},
1332 {"smb://", 6, "/#smb:"},
1333 {"sh://", 5, "/#sh:"},
1334 {"ssh://", 6, "/#sh:"},
1335 {"a:", 2, "/#a"}
1338 char *
1339 vfs_translate_url (const char *url)
1341 size_t i;
1343 for (i = 0; i < sizeof (url_table)/sizeof (url_table[0]); i++)
1344 if (strncmp (url, url_table[i].name, url_table[i].name_len) == 0)
1345 return g_strconcat (url_table[i].substitute, url + url_table[i].name_len, (char*) NULL);
1347 return g_strdup (url);
1350 int vfs_file_is_local (const char *filename)
1352 return vfs_file_class_flags (filename) & VFSF_LOCAL;