patches for AIX with ncurses support
[midnight-commander.git] / lib / vfs / mc-vfs / vfs.c
blob3f9226969d52c85ad4387117b6f71edf43ec0e4f
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 "lib/global.h"
54 #include "lib/strutil.h"
56 #include "src/wtools.h" /* message() */
57 #include "src/main.h" /* print_vfs_message */
59 #include "utilvfs.h"
60 #include "gc.h"
61 #include "vfs.h"
62 #ifdef USE_NETCODE
63 # include "netutil.h"
64 #endif
65 #include "ftpfs.h"
66 #include "mcfs.h"
67 #include "smbfs.h"
68 #include "local.h"
70 #if defined(_AIX) && !defined(NAME_MAX)
71 # define NAME_MAX FILENAME_MAX
72 #endif
74 /** They keep track of the current directory */
75 static struct vfs_class *current_vfs;
76 static char *current_dir;
78 struct vfs_openfile {
79 int handle;
80 struct vfs_class *vclass;
81 void *fsinfo;
84 struct vfs_dirinfo{
85 DIR *info;
86 GIConv converter;
90 static GPtrArray *vfs_openfiles;
91 static long vfs_free_handle_list = -1;
92 #define VFS_FIRST_HANDLE 100
94 static struct vfs_class *localfs_class;
95 static GString *vfs_str_buffer;
97 static const char *supported_encodings[] = {
98 "UTF8",
99 "UTF-8",
100 "BIG5",
101 "ASCII",
102 "ISO8859",
103 "ISO-8859",
104 "ISO_8859",
105 "KOI8",
106 "CP852",
107 "CP866",
108 "CP125",
109 NULL
112 /** Create new VFS handle and put it to the list */
113 static int
114 vfs_new_handle (struct vfs_class *vclass, void *fsinfo)
116 struct vfs_openfile *h;
118 h = g_new (struct vfs_openfile, 1);
119 h->fsinfo = fsinfo;
120 h->vclass = vclass;
122 /* Allocate the first free handle */
123 h->handle = vfs_free_handle_list;
124 if (h->handle == -1) {
125 /* No free allocated handles, allocate one */
126 h->handle = vfs_openfiles->len;
127 g_ptr_array_add (vfs_openfiles, h);
128 } else {
129 vfs_free_handle_list = (long) g_ptr_array_index (vfs_openfiles, vfs_free_handle_list);
130 g_ptr_array_index (vfs_openfiles, h->handle) = h;
133 h->handle += VFS_FIRST_HANDLE;
134 return h->handle;
137 /** Find VFS class by file handle */
138 static struct vfs_class *
139 vfs_op (int handle)
141 struct vfs_openfile *h;
143 if (handle < VFS_FIRST_HANDLE ||
144 (guint)(handle - VFS_FIRST_HANDLE) >= vfs_openfiles->len)
145 return NULL;
147 h = (struct vfs_openfile *) g_ptr_array_index (
148 vfs_openfiles, handle - VFS_FIRST_HANDLE);
149 if (!h)
150 return NULL;
152 g_assert (h->handle == handle);
154 return h->vclass;
157 /** Find private file data by file handle */
158 static void *
159 vfs_info (int handle)
161 struct vfs_openfile *h;
163 if (handle < VFS_FIRST_HANDLE ||
164 (guint)(handle - VFS_FIRST_HANDLE) >= vfs_openfiles->len)
165 return NULL;
167 h = (struct vfs_openfile *) g_ptr_array_index (
168 vfs_openfiles, handle - VFS_FIRST_HANDLE);
169 if (!h)
170 return NULL;
172 g_assert (h->handle == handle);
174 return h->fsinfo;
177 /** Free open file data for given file handle */
178 static void
179 vfs_free_handle (int handle)
181 if (handle < VFS_FIRST_HANDLE ||
182 (guint)(handle - VFS_FIRST_HANDLE) >= vfs_openfiles->len)
183 return;
185 g_ptr_array_index (vfs_openfiles, handle - VFS_FIRST_HANDLE) =
186 (void *) vfs_free_handle_list;
187 vfs_free_handle_list = handle - VFS_FIRST_HANDLE;
190 static struct vfs_class *vfs_list;
193 vfs_register_class (struct vfs_class *vfs)
195 if (vfs->init) /* vfs has own initialization function */
196 if (!(*vfs->init)(vfs)) /* but it failed */
197 return 0;
199 vfs->next = vfs_list;
200 vfs_list = vfs;
202 return 1;
205 /** Return VFS class for the given prefix */
206 static struct vfs_class *
207 vfs_prefix_to_class (char *prefix)
209 struct vfs_class *vfs;
211 /* Avoid last class (localfs) that would accept any prefix */
212 for (vfs = vfs_list; vfs->next; vfs = vfs->next) {
213 if (vfs->which) {
214 if ((*vfs->which) (vfs, prefix) == -1)
215 continue;
216 return vfs;
218 if (vfs->prefix
219 && !strncmp (prefix, vfs->prefix, strlen (vfs->prefix)))
220 return vfs;
222 return NULL;
225 /** Strip known vfs suffixes from a filename (possible improvement: strip
226 * suffix from last path component).
227 * \return a malloced string which has to be freed.
229 char *
230 vfs_strip_suffix_from_filename (const char *filename)
232 struct vfs_class *vfs;
233 char *semi;
234 char *p;
236 if (!filename)
237 vfs_die ("vfs_strip_suffix_from_path got NULL: impossible");
239 p = g_strdup (filename);
240 if (!(semi = strrchr (p, '#')))
241 return p;
243 /* Avoid last class (localfs) that would accept any prefix */
244 for (vfs = vfs_list; vfs->next; vfs = vfs->next) {
245 if (vfs->which) {
246 if ((*vfs->which) (vfs, semi + 1) == -1)
247 continue;
248 *semi = '\0'; /* Found valid suffix */
249 return p;
251 if (vfs->prefix
252 && !strncmp (semi + 1, vfs->prefix, strlen (vfs->prefix))) {
253 *semi = '\0'; /* Found valid suffix */
254 return p;
257 return p;
260 static int
261 path_magic (const char *path)
263 struct stat buf;
265 if (!stat(path, &buf))
266 return 0;
268 return 1;
272 * Splits path extracting vfs part.
274 * Splits path
275 * \verbatim /p1#op/inpath \endverbatim
276 * into
277 * \verbatim inpath,op; \endverbatim
278 * returns which vfs it is.
279 * What is left in path is p1. You still want to g_free(path), you DON'T
280 * want to free neither *inpath nor *op
282 struct vfs_class *
283 vfs_split (char *path, char **inpath, char **op)
285 char *semi;
286 char *slash;
287 struct vfs_class *ret;
289 if (!path)
290 vfs_die("Cannot split NULL");
292 semi = strrchr (path, '#');
293 if (!semi || !path_magic(path))
294 return NULL;
296 slash = strchr (semi, PATH_SEP);
297 *semi = 0;
299 if (op)
300 *op = NULL;
302 if (inpath)
303 *inpath = NULL;
305 if (slash)
306 *slash = 0;
308 if ((ret = vfs_prefix_to_class (semi+1))){
309 if (op)
310 *op = semi + 1;
311 if (inpath)
312 *inpath = slash ? slash + 1 : NULL;
313 return ret;
317 if (slash)
318 *slash = PATH_SEP;
319 ret = vfs_split (path, inpath, op);
320 *semi = '#';
321 return ret;
324 static struct vfs_class *
325 _vfs_get_class (char *path)
327 char *semi;
328 char *slash;
329 struct vfs_class *ret;
331 g_return_val_if_fail(path, NULL);
333 semi = strrchr (path, '#');
334 if (!semi || !path_magic (path))
335 return NULL;
337 slash = strchr (semi, PATH_SEP);
338 *semi = 0;
339 if (slash)
340 *slash = 0;
342 ret = vfs_prefix_to_class (semi+1);
344 if (slash)
345 *slash = PATH_SEP;
346 if (!ret)
347 ret = _vfs_get_class (path);
349 *semi = '#';
350 return ret;
353 struct vfs_class *
354 vfs_get_class (const char *pathname)
356 struct vfs_class *vfs;
357 char *path = g_strdup (pathname);
359 vfs = _vfs_get_class (path);
360 g_free (path);
362 if (!vfs)
363 vfs = localfs_class;
365 return vfs;
368 const char *
369 vfs_get_encoding (const char *path)
371 static char result[16];
372 char *work;
373 char *semi;
374 char *slash;
376 work = g_strdup (path);
377 semi = g_strrstr (work, "#enc:");
379 if (semi != NULL) {
380 semi+= 5 * sizeof (char);
381 slash = strchr (semi, PATH_SEP);
382 if (slash != NULL)
383 slash[0] = '\0';
385 g_strlcpy (result, semi, sizeof(result));
386 g_free (work);
387 return result;
388 } else {
389 g_free (work);
390 return NULL;
394 /* return if encoding can by used in vfs (is ascci full compactible) */
395 /* contains only a few encoding now */
396 static int
397 vfs_supported_enconding (const char *encoding) {
398 int t;
399 int result = 0;
401 for (t = 0; supported_encodings[t] != NULL; t++) {
402 result+= (g_ascii_strncasecmp (encoding, supported_encodings[t],
403 strlen (supported_encodings[t])) == 0);
406 return result;
409 /* now used only by vfs_translate_path, but could be used in other vfs
410 * plugin to automatic detect encoding
411 * path - path to translate
412 * size - how many bytes from path translate
413 * defcnv - convertor, that is used as default, when path does not contain any
414 * #enc: subtring
415 * buffer - used to store result of translation
417 static estr_t
418 _vfs_translate_path (const char *path, int size,
419 GIConv defcnv, GString *buffer)
421 const char *semi;
422 const char *ps;
423 const char *slash;
424 estr_t state = ESTR_SUCCESS;
425 static char encoding[16];
426 GIConv coder;
427 int ms;
429 if (size == 0) return 0;
430 size = ( size > 0) ? size : (signed int)strlen (path);
432 /* try found #end: */
433 semi = g_strrstr_len (path, size, "#enc:");
434 if (semi != NULL) {
435 /* first must be translated part before #enc: */
436 ms = semi - path;
438 /* remove '/' before #enc */
439 ps = str_cget_prev_char (semi);
440 if (ps[0] == PATH_SEP) ms = ps - path;
442 state = _vfs_translate_path (path, ms, defcnv, buffer);
444 if (state != ESTR_SUCCESS)
445 return state;
446 /* now can be translated part after #enc: */
448 semi+= 5;
449 slash = strchr (semi, PATH_SEP);
450 /* ignore slashes after size; */
451 if (slash - path >= size) slash = NULL;
453 ms = (slash != NULL) ? slash - semi : (int) strlen (semi);
454 ms = min ((unsigned int) ms, sizeof (encoding) - 1);
455 /* limit encoding size (ms) to path size (size) */
456 if (semi + ms > path + size) ms = path + size - semi;
457 memcpy (encoding, semi, ms);
458 encoding[ms] = '\0';
460 switch (vfs_supported_enconding (encoding)) {
461 case 1:
462 coder = str_crt_conv_to (encoding);
463 if (coder != INVALID_CONV) {
464 if (slash != NULL) {
465 state = str_vfs_convert_to (coder, slash,
466 path + size - slash, buffer);
467 } else if (buffer->str[0] == '\0') {
468 /* exmaple "/#enc:utf-8" */
469 g_string_append_c(buffer, PATH_SEP);
471 str_close_conv (coder);
472 return state;
473 } else {
474 errno = EINVAL;
475 return ESTR_FAILURE;
477 break;
478 default:
479 errno = EINVAL;
480 return ESTR_FAILURE;
482 } else {
483 /* path can be translated whole at once */
484 state = str_vfs_convert_to (defcnv, path, size, buffer);
485 return state;
488 return ESTR_SUCCESS;
491 char *
492 vfs_translate_path (const char *path)
494 estr_t state;
496 g_string_set_size(vfs_str_buffer,0);
497 state = _vfs_translate_path (path, -1, str_cnv_from_term, vfs_str_buffer);
499 strict version
500 return (state == 0) ? vfs_str_buffer->data : NULL;
502 return (state != ESTR_FAILURE) ? vfs_str_buffer->str : NULL;
505 char *
506 vfs_translate_path_n (const char *path)
508 char *result;
510 result = vfs_translate_path (path);
511 return (result != NULL) ? g_strdup (result) : NULL;
514 char *
515 vfs_canon_and_translate (const char *path)
517 char *canon;
518 char *result;
519 if (path == NULL)
520 canon = g_strdup ("");
521 else
522 canon = vfs_canon (path);
523 result = vfs_translate_path_n (canon);
524 g_free (canon);
525 return result;
528 static int
529 ferrno (struct vfs_class *vfs)
531 return vfs->ferrno ? (*vfs->ferrno)(vfs) : E_UNKNOWN;
532 /* Hope that error message is obscure enough ;-) */
536 mc_open (const char *filename, int flags, ...)
538 int mode;
539 void *info;
540 va_list ap;
542 char *file = vfs_canon_and_translate (filename);
543 if (file != NULL) {
544 struct vfs_class *vfs = vfs_get_class (file);
546 /* Get the mode flag */
547 if (flags & O_CREAT) {
548 va_start (ap, flags);
549 mode = va_arg (ap, int);
550 va_end (ap);
551 } else
552 mode = 0;
554 if (!vfs->open) {
555 g_free (file);
556 errno = -EOPNOTSUPP;
557 return -1;
560 info = (*vfs->open) (vfs, file, flags, mode); /* open must be supported */
561 g_free (file);
562 if (!info){
563 errno = ferrno (vfs);
564 return -1;
567 return vfs_new_handle (vfs, info);
568 } else return -1;
572 #define MC_NAMEOP(name, inarg, callarg) \
573 int mc_##name inarg \
575 struct vfs_class *vfs; \
576 int result; \
577 char *mpath = vfs_canon_and_translate (path); \
578 if (mpath != NULL) { \
579 vfs = vfs_get_class (mpath); \
580 if (vfs == NULL){ \
581 g_free (mpath); \
582 return -1; \
584 result = vfs->name ? (*vfs->name)callarg : -1; \
585 g_free (mpath); \
586 if (result == -1) \
587 errno = vfs->name ? ferrno (vfs) : E_NOTSUPP; \
588 return result; \
589 } else return -1; \
592 MC_NAMEOP (chmod, (const char *path, mode_t mode), (vfs, mpath, mode))
593 MC_NAMEOP (chown, (const char *path, uid_t owner, gid_t group), (vfs, mpath, owner, group))
594 MC_NAMEOP (utime, (const char *path, struct utimbuf *times), (vfs, mpath, times))
595 MC_NAMEOP (readlink, (const char *path, char *buf, int bufsiz), (vfs, mpath, buf, bufsiz))
596 MC_NAMEOP (unlink, (const char *path), (vfs, mpath))
597 MC_NAMEOP (mkdir, (const char *path, mode_t mode), (vfs, mpath, mode))
598 MC_NAMEOP (rmdir, (const char *path), (vfs, mpath))
599 MC_NAMEOP (mknod, (const char *path, mode_t mode, dev_t dev), (vfs, mpath, mode, dev))
601 int
602 mc_symlink (const char *name1, const char *path)
604 struct vfs_class *vfs;
605 int result;
606 char *mpath;
607 char *lpath;
608 char *tmp;
610 mpath = vfs_canon_and_translate (path);
611 if (mpath != NULL) {
612 tmp = g_strdup (name1);
613 lpath = vfs_translate_path_n (tmp);
614 g_free (tmp);
616 if (lpath != NULL) {
617 vfs = vfs_get_class (mpath);
618 result = vfs->symlink ? (*vfs->symlink) (vfs, lpath, mpath) : -1;
619 g_free (lpath);
621 if (result == -1)
622 errno = vfs->symlink ? ferrno (vfs) : E_NOTSUPP;
623 return result;
625 g_free (mpath);
627 return -1;
630 #define MC_HANDLEOP(name, inarg, callarg) \
631 ssize_t mc_##name inarg \
633 struct vfs_class *vfs; \
634 int result; \
635 if (handle == -1) \
636 return -1; \
637 vfs = vfs_op (handle); \
638 if (vfs == NULL) \
639 return -1; \
640 result = vfs->name ? (*vfs->name)callarg : -1; \
641 if (result == -1) \
642 errno = vfs->name ? ferrno (vfs) : E_NOTSUPP; \
643 return result; \
646 MC_HANDLEOP(read, (int handle, void *buffer, int count), (vfs_info (handle), buffer, count))
647 MC_HANDLEOP(write, (int handle, const void *buf, int nbyte), (vfs_info (handle), buf, nbyte))
650 #define MC_RENAMEOP(name) \
651 int mc_##name (const char *fname1, const char *fname2) \
653 struct vfs_class *vfs; \
654 int result; \
655 char *name2, *name1; \
656 name1 = vfs_canon_and_translate (fname1); \
657 if (name1 != NULL) { \
658 name2 = vfs_canon_and_translate (fname2); \
659 if (name2 != NULL) { \
660 vfs = vfs_get_class (name1); \
661 if (vfs != vfs_get_class (name2)){ \
662 errno = EXDEV; \
663 g_free (name1); \
664 g_free (name2); \
665 return -1; \
667 result = vfs->name ? (*vfs->name)(vfs, name1, name2) : -1; \
668 g_free (name1); \
669 g_free (name2); \
670 if (result == -1) \
671 errno = vfs->name ? ferrno (vfs) : E_NOTSUPP; \
672 return result; \
673 } else { \
674 g_free (name1); \
675 return -1; \
677 } else return -1; \
680 MC_RENAMEOP (link)
681 MC_RENAMEOP (rename)
685 mc_ctl (int handle, int ctlop, void *arg)
687 struct vfs_class *vfs = vfs_op (handle);
689 if (vfs == NULL)
690 return 0;
692 return vfs->ctl ? (*vfs->ctl)(vfs_info (handle), ctlop, arg) : 0;
696 mc_setctl (const char *path, int ctlop, void *arg)
698 struct vfs_class *vfs;
699 int result;
700 char *mpath;
702 if (!path)
703 vfs_die("You don't want to pass NULL to mc_setctl.");
705 mpath = vfs_canon_and_translate (path);
706 if (mpath != NULL) {
707 vfs = vfs_get_class (mpath);
708 result = vfs->setctl ? (*vfs->setctl)(vfs, mpath, ctlop, arg) : 0;
709 g_free (mpath);
710 return result;
711 } else return -1;
715 mc_close (int handle)
717 struct vfs_class *vfs;
718 int result;
720 if (handle == -1 || !vfs_info (handle))
721 return -1;
723 vfs = vfs_op (handle);
724 if (vfs == NULL)
725 return -1;
727 if (handle < 3)
728 return close (handle);
730 if (!vfs->close)
731 vfs_die ("VFS must support close.\n");
732 result = (*vfs->close)(vfs_info (handle));
733 vfs_free_handle (handle);
734 if (result == -1)
735 errno = ferrno (vfs);
737 return result;
740 DIR *
741 mc_opendir (const char *dirname)
743 int handle, *handlep;
744 void *info;
745 struct vfs_class *vfs;
746 char *canon;
747 char *dname;
748 struct vfs_dirinfo *dirinfo;
749 const char *encoding;
751 canon = vfs_canon (dirname);
752 dname = vfs_translate_path_n (canon);
754 if (dname != NULL) {
755 vfs = vfs_get_class (dname);
756 info = vfs->opendir ? (*vfs->opendir)(vfs, dname) : NULL;
757 g_free (dname);
759 if (info == NULL) {
760 errno = vfs->opendir ? ferrno (vfs) : E_NOTSUPP;
761 g_free (canon);
762 return NULL;
765 dirinfo = g_new (struct vfs_dirinfo, 1);
766 dirinfo->info = info;
768 encoding = vfs_get_encoding (canon);
769 g_free (canon);
770 dirinfo->converter = (encoding != NULL) ? str_crt_conv_from (encoding) :
771 str_cnv_from_term;
772 if (dirinfo->converter == INVALID_CONV) dirinfo->converter =str_cnv_from_term;
774 handle = vfs_new_handle (vfs, dirinfo);
776 handlep = g_new (int, 1);
777 *handlep = handle;
778 return (DIR *) handlep;
779 } else {
780 g_free (canon);
781 return NULL;
785 static struct dirent * mc_readdir_result = NULL;
787 struct dirent *
788 mc_readdir (DIR *dirp)
790 int handle;
791 struct vfs_class *vfs;
792 struct dirent *entry = NULL;
793 struct vfs_dirinfo *dirinfo;
794 estr_t state;
796 if (!mc_readdir_result)
798 /* We can't just allocate struct dirent as (see man dirent.h)
799 * struct dirent has VERY nonnaive semantics of allocating
800 * d_name in it. Moreover, linux's glibc-2.9 allocates dirents _less_,
801 * than 'sizeof (struct dirent)' making full bitwise (sizeof dirent) copy
802 * heap corrupter. So, allocate longliving dirent with at least
803 * (MAXNAMLEN + 1) for d_name in it.
804 * Strictly saying resulting dirent is unusable as we don't adjust internal
805 * structures, holding dirent size. But we don't use it in libc infrastructure.
806 * TODO: to make simpler homemade dirent-alike structure.
808 mc_readdir_result = (struct dirent *) g_malloc (sizeof(struct dirent) + MAXNAMLEN + 1);
811 if (!dirp) {
812 errno = EFAULT;
813 return NULL;
815 handle = *(int *) dirp;
817 vfs = vfs_op (handle);
818 if (vfs == NULL)
819 return NULL;
821 dirinfo = vfs_info (handle);
822 if (vfs->readdir) {
823 entry = (*vfs->readdir) (dirinfo->info);
824 if (entry == NULL) return NULL;
825 g_string_set_size(vfs_str_buffer,0);
826 state = str_vfs_convert_from (dirinfo->converter,
827 entry->d_name, vfs_str_buffer);
828 mc_readdir_result->d_ino = entry->d_ino;
829 g_strlcpy (mc_readdir_result->d_name, vfs_str_buffer->str, MAXNAMLEN + 1);
831 if (entry == NULL) errno = vfs->readdir ? ferrno (vfs) : E_NOTSUPP;
832 return (entry != NULL) ? mc_readdir_result : NULL;
836 mc_closedir (DIR *dirp)
838 int handle = *(int *) dirp;
839 struct vfs_class *vfs = vfs_op (handle);
840 int result;
841 struct vfs_dirinfo *dirinfo;
843 if (vfs == NULL)
844 return -1;
846 dirinfo = vfs_info (handle);
847 if (dirinfo->converter != str_cnv_from_term) str_close_conv (dirinfo->converter);
849 result = vfs->closedir ? (*vfs->closedir)(dirinfo->info) : -1;
850 vfs_free_handle (handle);
851 g_free (dirinfo);
852 g_free (dirp);
853 return result;
856 int mc_stat (const char *filename, struct stat *buf) {
857 struct vfs_class *vfs;
858 int result;
859 char *path;
861 path = vfs_canon_and_translate (filename);
863 if (path == NULL)
864 return -1;
866 vfs = vfs_get_class (path);
868 if (vfs == NULL) {
869 g_free (path);
870 return -1;
873 result = vfs->stat ? (*vfs->stat) (vfs, path, buf) : -1;
875 g_free (path);
877 if (result == -1)
878 errno = vfs->name ? ferrno (vfs) : E_NOTSUPP;
879 return result;
882 int mc_lstat (const char *filename, struct stat *buf) {
883 struct vfs_class *vfs;
884 int result;
885 char *path;
887 path = vfs_canon_and_translate (filename);
889 if (path == NULL)
890 return -1;
892 vfs = vfs_get_class (path);
893 if (vfs == NULL) {
894 g_free (path);
895 return -1;
898 result = vfs->lstat ? (*vfs->lstat) (vfs, path, buf) : -1;
899 g_free (path);
900 if (result == -1)
901 errno = vfs->name ? ferrno (vfs) : E_NOTSUPP;
902 return result;
905 int mc_fstat (int handle, struct stat *buf) {
906 struct vfs_class *vfs;
907 int result;
909 if (handle == -1)
910 return -1;
912 vfs = vfs_op (handle);
913 if (vfs == NULL)
914 return -1;
916 result = vfs->fstat ? (*vfs->fstat) (vfs_info (handle), buf) : -1;
917 if (result == -1)
918 errno = vfs->name ? ferrno (vfs) : E_NOTSUPP;
919 return result;
923 * Return current directory. If it's local, reread the current directory
924 * from the OS. You must g_strdup() whatever this function returns.
926 static const char *
927 _vfs_get_cwd (void)
929 char *sys_cwd;
930 char *trans;
931 const char *encoding;
932 char *tmp;
933 estr_t state;
934 struct stat my_stat, my_stat2;
936 trans = vfs_translate_path_n (current_dir); /* add check if NULL */
938 if (!_vfs_get_class (trans)) {
939 encoding = vfs_get_encoding (current_dir);
940 if (encoding == NULL) {
941 tmp = g_get_current_dir ();
942 if (tmp != NULL) { /* One of the directories in the path is not readable */
943 g_string_set_size(vfs_str_buffer,0);
944 state = str_vfs_convert_from (str_cnv_from_term, tmp, vfs_str_buffer);
945 g_free (tmp);
946 sys_cwd = (state == ESTR_SUCCESS) ? g_strdup (vfs_str_buffer->str) : NULL;
947 if (!sys_cwd)
948 return current_dir;
950 /* Otherwise check if it is O.K. to use the current_dir */
951 if (!cd_symlinks || mc_stat (sys_cwd, &my_stat)
952 || mc_stat (current_dir, &my_stat2)
953 || my_stat.st_ino != my_stat2.st_ino
954 || my_stat.st_dev != my_stat2.st_dev) {
955 g_free (current_dir);
956 current_dir = sys_cwd;
957 return sys_cwd;
958 }/* Otherwise we return current_dir below */
962 g_free (trans);
963 return current_dir;
966 static void
967 vfs_setup_wd (void)
969 current_dir = g_strdup (PATH_SEP_STR);
970 _vfs_get_cwd ();
972 if (strlen (current_dir) > MC_MAXPATHLEN - 2)
973 vfs_die ("Current dir too long.\n");
975 current_vfs = vfs_get_class (current_dir);
979 * Return current directory. If it's local, reread the current directory
980 * from the OS. Put directory to the provided buffer.
982 char *
983 mc_get_current_wd (char *buffer, int size)
985 const char *cwd = _vfs_get_cwd ();
987 g_strlcpy (buffer, cwd, size);
988 return buffer;
992 * Return current directory without any OS calls.
994 char *
995 vfs_get_current_dir (void)
997 return current_dir;
1000 off_t mc_lseek (int fd, off_t offset, int whence)
1002 struct vfs_class *vfs;
1003 int result;
1005 if (fd == -1)
1006 return -1;
1008 vfs = vfs_op (fd);
1009 if (vfs == NULL)
1010 return -1;
1012 result = vfs->lseek ? (*vfs->lseek)(vfs_info (fd), offset, whence) : -1;
1013 if (result == -1)
1014 errno = vfs->lseek ? ferrno (vfs) : E_NOTSUPP;
1015 return result;
1019 * remove //, /./ and /../
1022 #define ISSLASH(a) (!a || (a == '/'))
1024 char *
1025 vfs_canon (const char *path)
1027 if (!path)
1028 vfs_die("Cannot canonicalize NULL");
1030 /* Relative to current directory */
1031 if (*path != PATH_SEP){
1032 char *local, *result;
1034 local = concat_dir_and_file (current_dir, path);
1036 result = vfs_canon (local);
1037 g_free (local);
1038 return result;
1042 * So we have path of following form:
1043 * /p1/p2#op/.././././p3#op/p4. Good luck.
1046 char *result = g_strdup (path);
1047 canonicalize_pathname (result);
1048 return result;
1053 * VFS chdir.
1054 * Return 0 on success, -1 on failure.
1057 mc_chdir (const char *path)
1059 char *new_dir;
1060 char *trans_dir;
1061 struct vfs_class *old_vfs, *new_vfs;
1062 vfsid old_vfsid;
1063 int result;
1065 new_dir = vfs_canon (path);
1066 trans_dir = vfs_translate_path_n (new_dir);
1067 if (trans_dir != NULL) {
1068 new_vfs = vfs_get_class (trans_dir);
1069 if (!new_vfs->chdir) {
1070 g_free (new_dir);
1071 g_free (trans_dir);
1072 return -1;
1075 result = (*new_vfs->chdir) (new_vfs, trans_dir);
1077 if (result == -1) {
1078 errno = ferrno (new_vfs);
1079 g_free (new_dir);
1080 g_free (trans_dir);
1081 return -1;
1084 old_vfsid = vfs_getid (current_vfs, current_dir);
1085 old_vfs = current_vfs;
1087 /* Actually change directory */
1088 g_free (current_dir);
1089 current_dir = new_dir;
1090 current_vfs = new_vfs;
1092 /* This function uses the new current_dir implicitly */
1093 vfs_stamp_create (old_vfs, old_vfsid);
1095 /* Sometimes we assume no trailing slash on cwd */
1096 if (*current_dir) {
1097 char *p;
1098 p = strchr (current_dir, 0) - 1;
1099 if (*p == PATH_SEP && p > current_dir)
1100 *p = 0;
1103 g_free (trans_dir);
1104 return 0;
1105 } else {
1106 g_free (new_dir);
1107 return -1;
1111 /* Return 1 is the current VFS class is local */
1113 vfs_current_is_local (void)
1115 return (current_vfs->flags & VFSF_LOCAL) != 0;
1118 /* Return flags of the VFS class of the given filename */
1120 vfs_file_class_flags (const char *filename)
1122 struct vfs_class *vfs;
1123 char *fname;
1125 fname = vfs_canon_and_translate (filename);
1126 if (fname != NULL) {
1127 vfs = vfs_get_class (fname);
1128 g_free (fname);
1129 return vfs->flags;
1130 } else return -1;
1133 static char *
1134 mc_def_getlocalcopy (const char *filename)
1136 char *tmp;
1137 int fdin, fdout;
1138 ssize_t i;
1139 char buffer[8192];
1140 struct stat mystat;
1142 fdin = mc_open (filename, O_RDONLY | O_LINEAR);
1143 if (fdin == -1)
1144 return NULL;
1146 fdout = vfs_mkstemps (&tmp, "vfs", filename);
1148 if (fdout == -1)
1149 goto fail;
1150 while ((i = mc_read (fdin, buffer, sizeof (buffer))) > 0) {
1151 if (write (fdout, buffer, i) != i)
1152 goto fail;
1154 if (i == -1)
1155 goto fail;
1156 i = mc_close (fdin);
1157 fdin = -1;
1158 if (i == -1)
1159 goto fail;
1160 if (close (fdout) == -1) {
1161 fdout = -1;
1162 goto fail;
1165 if (mc_stat (filename, &mystat) != -1) {
1166 chmod (tmp, mystat.st_mode);
1168 return tmp;
1170 fail:
1171 if (fdout != -1)
1172 close (fdout);
1173 if (fdin != -1)
1174 mc_close (fdin);
1175 g_free (tmp);
1176 return NULL;
1179 char *
1180 mc_getlocalcopy (const char *pathname)
1182 char *result;
1183 char *path;
1185 path = vfs_canon_and_translate (pathname);
1186 if (path != NULL) {
1187 struct vfs_class *vfs = vfs_get_class (path);
1189 result = vfs->getlocalcopy ? (*vfs->getlocalcopy)(vfs, path) :
1190 mc_def_getlocalcopy (path);
1191 g_free (path);
1192 if (!result)
1193 errno = ferrno (vfs);
1194 return result;
1195 } else return NULL;
1198 static int
1199 mc_def_ungetlocalcopy (struct vfs_class *vfs, const char *filename,
1200 const char *local, int has_changed)
1202 int fdin = -1, fdout = -1, i;
1203 if (has_changed) {
1204 char buffer[8192];
1206 if (!vfs->write)
1207 goto failed;
1209 fdin = open (local, O_RDONLY);
1210 if (fdin == -1)
1211 goto failed;
1212 fdout = mc_open (filename, O_WRONLY | O_TRUNC);
1213 if (fdout == -1)
1214 goto failed;
1215 while ((i = read (fdin, buffer, sizeof (buffer))) > 0) {
1216 if (mc_write (fdout, buffer, i) != i)
1217 goto failed;
1219 if (i == -1)
1220 goto failed;
1222 if (close (fdin) == -1) {
1223 fdin = -1;
1224 goto failed;
1226 fdin = -1;
1227 if (mc_close (fdout) == -1) {
1228 fdout = -1;
1229 goto failed;
1232 unlink (local);
1233 return 0;
1235 failed:
1236 message (D_ERROR, _("Changes to file lost"), "%s", filename);
1237 if (fdout != -1)
1238 mc_close (fdout);
1239 if (fdin != -1)
1240 close (fdin);
1241 unlink (local);
1242 return -1;
1246 mc_ungetlocalcopy (const char *pathname, const char *local, int has_changed)
1248 int return_value = 0;
1249 char *path;
1251 path = vfs_canon_and_translate (pathname);
1252 if (path != NULL) {
1253 struct vfs_class *vfs = vfs_get_class (path);
1255 return_value = vfs->ungetlocalcopy ?
1256 (*vfs->ungetlocalcopy)(vfs, path, local, has_changed) :
1257 mc_def_ungetlocalcopy (vfs, path, local, has_changed);
1258 g_free (path);
1259 return return_value;
1260 } else return -1;
1264 void
1265 vfs_init (void)
1267 /* create the VFS handle array */
1268 vfs_openfiles = g_ptr_array_new ();
1270 vfs_str_buffer = g_string_new("");
1271 /* localfs needs to be the first one */
1272 init_localfs();
1273 /* fallback value for vfs_get_class() */
1274 localfs_class = vfs_list;
1276 init_extfs ();
1277 init_sfs ();
1278 init_tarfs ();
1279 init_cpiofs ();
1281 #ifdef USE_EXT2FSLIB
1282 init_undelfs ();
1283 #endif /* USE_EXT2FSLIB */
1285 #ifdef USE_NETCODE
1286 init_ftpfs ();
1287 init_fish ();
1288 #ifdef ENABLE_VFS_SMB
1289 init_smbfs ();
1290 #endif /* ENABLE_VFS_SMB */
1291 #ifdef ENABLE_VFS_MCFS
1292 init_mcfs ();
1293 #endif /* ENABLE_VFS_MCFS */
1294 #endif /* USE_NETCODE */
1296 vfs_setup_wd ();
1299 void
1300 vfs_shut (void)
1302 struct vfs_class *vfs;
1304 vfs_gc_done ();
1306 g_free (current_dir);
1308 for (vfs = vfs_list; vfs; vfs = vfs->next)
1309 if (vfs->done)
1310 (*vfs->done) (vfs);
1312 g_ptr_array_free (vfs_openfiles, TRUE);
1313 g_string_free (vfs_str_buffer, TRUE);
1314 g_free (mc_readdir_result);
1318 * These ones grab information from the VFS
1319 * and handles them to an upper layer
1321 void
1322 vfs_fill_names (fill_names_f func)
1324 struct vfs_class *vfs;
1326 for (vfs=vfs_list; vfs; vfs=vfs->next)
1327 if (vfs->fill_names)
1328 (*vfs->fill_names) (vfs, func);
1332 * Returns vfs path corresponding to given url. If passed string is
1333 * not recognized as url, g_strdup(url) is returned.
1336 static const struct {
1337 const char *name;
1338 size_t name_len;
1339 const char *substitute;
1340 } url_table[] = { {"ftp://", 6, "/#ftp:"},
1341 {"mc://", 5, "/#mc:"},
1342 {"smb://", 6, "/#smb:"},
1343 {"sh://", 5, "/#sh:"},
1344 {"ssh://", 6, "/#sh:"},
1345 {"a:", 2, "/#a"}
1348 char *
1349 vfs_translate_url (const char *url)
1351 size_t i;
1353 for (i = 0; i < sizeof (url_table)/sizeof (url_table[0]); i++)
1354 if (strncmp (url, url_table[i].name, url_table[i].name_len) == 0)
1355 return g_strconcat (url_table[i].substitute, url + url_table[i].name_len, (char*) NULL);
1357 return g_strdup (url);
1360 int vfs_file_is_local (const char *filename)
1362 return vfs_file_class_flags (filename) & VFSF_LOCAL;