Ticket #2447: code cleanup before 4.7.5 release.
[pantumic.git] / lib / vfs / mc-vfs / vfs.c
bloba539a4343eaecebd8f3918be7563609854ecfd63
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"
55 #include "lib/util.h"
56 #include "lib/widget.h" /* message() */
58 #ifdef HAVE_CHARSET
59 #include "lib/charsets.h"
60 #endif
61 #include "src/setup.h" /* cd_symlinks */
63 #include "vfs-impl.h"
64 #include "utilvfs.h"
65 #include "gc.h"
67 #ifdef ENABLE_VFS_NET
68 #include "netutil.h"
69 #endif
70 #ifdef ENABLE_VFS_FTP
71 #include "ftpfs.h"
72 #endif
73 #ifdef ENABLE_VFS_SMB
74 #include "smbfs.h"
75 #endif
77 #include "local.h"
79 /*** global variables ****************************************************************************/
81 /*** file scope macro definitions ****************************************************************/
83 #if defined(_AIX) && !defined(NAME_MAX)
84 #define NAME_MAX FILENAME_MAX
85 #endif
87 #define VFS_FIRST_HANDLE 100
89 #define ISSLASH(a) (!a || (a == '/'))
91 /*** file scope type declarations ****************************************************************/
93 struct vfs_openfile
95 int handle;
96 struct vfs_class *vclass;
97 void *fsinfo;
100 struct vfs_dirinfo
102 DIR *info;
103 GIConv converter;
106 /*** file scope variables ************************************************************************/
108 /** They keep track of the current directory */
109 static struct vfs_class *current_vfs;
110 static char *current_dir;
112 static GPtrArray *vfs_openfiles;
113 static long vfs_free_handle_list = -1;
115 static struct vfs_class *localfs_class;
116 static GString *vfs_str_buffer;
118 static struct vfs_class *vfs_list;
120 static struct dirent *mc_readdir_result = NULL;
122 static const struct
124 const char *name;
125 size_t name_len;
126 const char *substitute;
127 } url_table[] =
129 /* *INDENT-OFF* */
130 #ifdef ENABLE_VFS_FTP
131 { "ftp://", 6, "/#ftp:" },
132 #endif
133 #ifdef ENABLE_VFS_FISH
134 { "sh://", 5, "/#sh:" },
135 { "ssh://", 6, "/#sh:" },
136 #endif
137 #ifdef ENABLE_VFS_SMB
138 { "smb://", 6, "/#smb:" },
139 #endif
140 { "a:", 2, "/#a" }
141 /* *INDENT-ON* */
144 /*** file scope functions ************************************************************************/
145 /* --------------------------------------------------------------------------------------------- */
147 * Create new VFS handle and put it to the list
150 static int
151 vfs_new_handle (struct vfs_class *vclass, void *fsinfo)
153 struct vfs_openfile *h;
155 h = g_new (struct vfs_openfile, 1);
156 h->fsinfo = fsinfo;
157 h->vclass = vclass;
159 /* Allocate the first free handle */
160 h->handle = vfs_free_handle_list;
161 if (h->handle == -1)
163 /* No free allocated handles, allocate one */
164 h->handle = vfs_openfiles->len;
165 g_ptr_array_add (vfs_openfiles, h);
167 else
169 vfs_free_handle_list = (long) g_ptr_array_index (vfs_openfiles, vfs_free_handle_list);
170 g_ptr_array_index (vfs_openfiles, h->handle) = h;
173 h->handle += VFS_FIRST_HANDLE;
174 return h->handle;
177 /* --------------------------------------------------------------------------------------------- */
178 /** Find VFS class by file handle */
180 static struct vfs_class *
181 vfs_op (int handle)
183 struct vfs_openfile *h;
185 if (handle < VFS_FIRST_HANDLE || (guint) (handle - VFS_FIRST_HANDLE) >= vfs_openfiles->len)
186 return NULL;
188 h = (struct vfs_openfile *) g_ptr_array_index (vfs_openfiles, handle - VFS_FIRST_HANDLE);
189 if (!h)
190 return NULL;
192 g_assert (h->handle == handle);
194 return h->vclass;
197 /* --------------------------------------------------------------------------------------------- */
198 /** Find private file data by file handle */
200 static void *
201 vfs_info (int handle)
203 struct vfs_openfile *h;
205 if (handle < VFS_FIRST_HANDLE || (guint) (handle - VFS_FIRST_HANDLE) >= vfs_openfiles->len)
206 return NULL;
208 h = (struct vfs_openfile *) g_ptr_array_index (vfs_openfiles, handle - VFS_FIRST_HANDLE);
209 if (!h)
210 return NULL;
212 g_assert (h->handle == handle);
214 return h->fsinfo;
217 /* --------------------------------------------------------------------------------------------- */
218 /** Free open file data for given file handle */
220 static void
221 vfs_free_handle (int handle)
223 const int idx = handle - VFS_FIRST_HANDLE;
225 if (handle >= VFS_FIRST_HANDLE && (guint) idx < vfs_openfiles->len)
227 struct vfs_openfile *h;
229 h = (struct vfs_openfile *) g_ptr_array_index (vfs_openfiles, idx);
230 g_free (h);
231 g_ptr_array_index (vfs_openfiles, idx) = (void *) vfs_free_handle_list;
232 vfs_free_handle_list = idx;
237 /* --------------------------------------------------------------------------------------------- */
239 /** Return VFS class for the given prefix */
240 static struct vfs_class *
241 vfs_prefix_to_class (char *prefix)
243 struct vfs_class *vfs;
245 /* Avoid last class (localfs) that would accept any prefix */
246 for (vfs = vfs_list; vfs->next != NULL; vfs = vfs->next)
248 if (vfs->which != NULL)
250 if ((*vfs->which) (vfs, prefix) == -1)
251 continue;
252 return vfs;
254 if (vfs->prefix != NULL && strncmp (prefix, vfs->prefix, strlen (vfs->prefix)) == 0)
255 return vfs;
257 return NULL;
260 /* --------------------------------------------------------------------------------------------- */
262 static gboolean
263 path_magic (const char *path)
265 struct stat buf;
267 return (stat (path, &buf) != 0);
270 /* --------------------------------------------------------------------------------------------- */
272 static struct vfs_class *
273 _vfs_get_class (char *path)
275 char *semi;
276 char *slash;
277 struct vfs_class *ret;
279 g_return_val_if_fail (path, NULL);
281 semi = strrchr (path, '#');
282 if (semi == NULL || !path_magic (path))
283 return NULL;
285 slash = strchr (semi, PATH_SEP);
286 *semi = '\0';
287 if (slash != NULL)
288 *slash = '\0';
290 ret = vfs_prefix_to_class (semi + 1);
292 if (slash != NULL)
293 *slash = PATH_SEP;
294 if (ret == NULL)
295 ret = _vfs_get_class (path);
297 *semi = '#';
298 return ret;
301 /* --------------------------------------------------------------------------------------------- */
302 /* now used only by vfs_translate_path, but could be used in other vfs
303 * plugin to automatic detect encoding
304 * path - path to translate
305 * size - how many bytes from path translate
306 * defcnv - convertor, that is used as default, when path does not contain any
307 * #enc: subtring
308 * buffer - used to store result of translation
311 static estr_t
312 _vfs_translate_path (const char *path, int size, GIConv defcnv, GString * buffer)
314 const char *semi;
315 const char *slash;
316 estr_t state = ESTR_SUCCESS;
318 if (size == 0)
319 return ESTR_SUCCESS;
321 size = (size > 0) ? size : (signed int) strlen (path);
323 /* try found /#enc: */
324 semi = g_strrstr_len (path, size, PATH_SEP_STR VFS_ENCODING_PREFIX);
325 if (semi != NULL)
327 char encoding[16];
328 GIConv coder = INVALID_CONV;
329 int ms;
331 /* first must be translated part before /#enc: */
332 ms = semi - path;
334 state = _vfs_translate_path (path, ms, defcnv, buffer);
336 if (state != ESTR_SUCCESS)
337 return state;
339 /* now can be translated part after #enc: */
340 semi += strlen (VFS_ENCODING_PREFIX) + 1; /* skip "/#enc:" */
341 slash = strchr (semi, PATH_SEP);
342 /* ignore slashes after size; */
343 if (slash - path >= size)
344 slash = NULL;
346 ms = (slash != NULL) ? slash - semi : (int) strlen (semi);
347 ms = min ((unsigned int) ms, sizeof (encoding) - 1);
348 /* limit encoding size (ms) to path size (size) */
349 if (semi + ms > path + size)
350 ms = path + size - semi;
351 memcpy (encoding, semi, ms);
352 encoding[ms] = '\0';
354 #if HAVE_CHARSET
355 if (is_supported_encoding (encoding))
356 coder = str_crt_conv_to (encoding);
357 #endif
359 if (coder != INVALID_CONV)
361 if (slash != NULL)
362 state = str_vfs_convert_to (coder, slash, path + size - slash, buffer);
363 else if (buffer->len == 0)
364 g_string_append_c (buffer, PATH_SEP);
365 str_close_conv (coder);
366 return state;
369 errno = EINVAL;
370 state = ESTR_FAILURE;
372 else
374 /* path can be translated whole at once */
375 state = str_vfs_convert_to (defcnv, path, size, buffer);
378 return state;
381 /* --------------------------------------------------------------------------------------------- */
383 static int
384 ferrno (struct vfs_class *vfs)
386 return vfs->ferrno ? (*vfs->ferrno) (vfs) : E_UNKNOWN;
387 /* Hope that error message is obscure enough ;-) */
390 /* --------------------------------------------------------------------------------------------- */
392 * Return current directory. If it's local, reread the current directory
393 * from the OS. You must g_strdup() whatever this function returns.
396 static const char *
397 _vfs_get_cwd (void)
399 char *trans;
401 trans = vfs_translate_path_n (current_dir);
403 if (_vfs_get_class (trans) == NULL)
405 const char *encoding = vfs_get_encoding (current_dir);
407 if (encoding == NULL)
409 char *tmp;
411 tmp = g_get_current_dir ();
412 if (tmp != NULL)
413 { /* One of the directories in the path is not readable */
414 estr_t state;
415 char *sys_cwd;
417 g_string_set_size (vfs_str_buffer, 0);
418 state = str_vfs_convert_from (str_cnv_from_term, tmp, vfs_str_buffer);
419 g_free (tmp);
421 sys_cwd = (state == ESTR_SUCCESS) ? g_strdup (vfs_str_buffer->str) : NULL;
422 if (sys_cwd != NULL)
424 struct stat my_stat, my_stat2;
425 /* Check if it is O.K. to use the current_dir */
426 if (cd_symlinks
427 && mc_stat (sys_cwd, &my_stat) == 0
428 && mc_stat (current_dir, &my_stat2) == 0
429 && my_stat.st_ino == my_stat2.st_ino && my_stat.st_dev == my_stat2.st_dev)
430 g_free (sys_cwd);
431 else
433 g_free (current_dir);
434 current_dir = sys_cwd;
441 g_free (trans);
442 return current_dir;
445 /* --------------------------------------------------------------------------------------------- */
447 static void
448 vfs_setup_wd (void)
450 current_dir = g_strdup (PATH_SEP_STR);
451 _vfs_get_cwd ();
453 if (strlen (current_dir) > MC_MAXPATHLEN - 2)
454 vfs_die ("Current dir too long.\n");
456 current_vfs = vfs_get_class (current_dir);
459 /* --------------------------------------------------------------------------------------------- */
461 static char *
462 mc_def_getlocalcopy (const char *filename)
464 char *tmp;
465 int fdin, fdout;
466 ssize_t i;
467 char buffer[8192];
468 struct stat mystat;
470 fdin = mc_open (filename, O_RDONLY | O_LINEAR);
471 if (fdin == -1)
472 return NULL;
474 fdout = vfs_mkstemps (&tmp, "vfs", filename);
476 if (fdout == -1)
477 goto fail;
478 while ((i = mc_read (fdin, buffer, sizeof (buffer))) > 0)
480 if (write (fdout, buffer, i) != i)
481 goto fail;
483 if (i == -1)
484 goto fail;
485 i = mc_close (fdin);
486 fdin = -1;
487 if (i == -1)
488 goto fail;
489 if (close (fdout) == -1)
491 fdout = -1;
492 goto fail;
495 if (mc_stat (filename, &mystat) != -1)
496 chmod (tmp, mystat.st_mode);
498 return tmp;
500 fail:
501 if (fdout != -1)
502 close (fdout);
503 if (fdin != -1)
504 mc_close (fdin);
505 g_free (tmp);
506 return NULL;
509 /* --------------------------------------------------------------------------------------------- */
511 static int
512 mc_def_ungetlocalcopy (struct vfs_class *vfs, const char *filename,
513 const char *local, int has_changed)
515 int fdin = -1, fdout = -1;
516 if (has_changed)
518 char buffer[8192];
519 ssize_t i;
521 if (!vfs->write)
522 goto failed;
524 fdin = open (local, O_RDONLY);
525 if (fdin == -1)
526 goto failed;
527 fdout = mc_open (filename, O_WRONLY | O_TRUNC);
528 if (fdout == -1)
529 goto failed;
530 while ((i = read (fdin, buffer, sizeof (buffer))) > 0)
531 if (mc_write (fdout, buffer, (size_t) i) != i)
532 goto failed;
533 if (i == -1)
534 goto failed;
536 if (close (fdin) == -1)
538 fdin = -1;
539 goto failed;
541 fdin = -1;
542 if (mc_close (fdout) == -1)
544 fdout = -1;
545 goto failed;
548 unlink (local);
549 return 0;
551 failed:
552 message (D_ERROR, _("Changes to file lost"), "%s", filename);
553 if (fdout != -1)
554 mc_close (fdout);
555 if (fdin != -1)
556 close (fdin);
557 unlink (local);
558 return -1;
561 /* --------------------------------------------------------------------------------------------- */
562 /*** public functions ****************************************************************************/
563 /* --------------------------------------------------------------------------------------------- */
566 vfs_register_class (struct vfs_class *vfs)
568 if (vfs->init != NULL) /* vfs has own initialization function */
569 if (!(*vfs->init) (vfs)) /* but it failed */
570 return 0;
572 vfs->next = vfs_list;
573 vfs_list = vfs;
575 return 1;
578 /* --------------------------------------------------------------------------------------------- */
579 /** Strip known vfs suffixes from a filename (possible improvement: strip
580 * suffix from last path component).
581 * \return a malloced string which has to be freed.
584 char *
585 vfs_strip_suffix_from_filename (const char *filename)
587 struct vfs_class *vfs;
588 char *semi;
589 char *p;
591 if (filename == NULL)
592 vfs_die ("vfs_strip_suffix_from_path got NULL: impossible");
594 p = g_strdup (filename);
595 semi = strrchr (p, '#');
596 if (semi == NULL)
597 return p;
599 /* Avoid last class (localfs) that would accept any prefix */
600 for (vfs = vfs_list; vfs->next != NULL; vfs = vfs->next)
602 if (vfs->which != NULL)
604 if ((*vfs->which) (vfs, semi + 1) == -1)
605 continue;
606 *semi = '\0'; /* Found valid suffix */
607 return p;
609 if (vfs->prefix != NULL && strncmp (semi + 1, vfs->prefix, strlen (vfs->prefix)) == 0)
611 *semi = '\0'; /* Found valid suffix */
612 return p;
615 return p;
618 /* --------------------------------------------------------------------------------------------- */
621 * Splits path extracting vfs part.
623 * Splits path
624 * \verbatim /p1#op/inpath \endverbatim
625 * into
626 * \verbatim inpath,op; \endverbatim
627 * returns which vfs it is.
628 * What is left in path is p1. You still want to g_free(path), you DON'T
629 * want to free neither *inpath nor *op
632 struct vfs_class *
633 vfs_split (char *path, char **inpath, char **op)
635 char *semi;
636 char *slash;
637 struct vfs_class *ret;
639 if (path == NULL)
640 vfs_die ("Cannot split NULL");
642 semi = strrchr (path, '#');
643 if (semi == NULL || !path_magic (path))
644 return NULL;
646 slash = strchr (semi, PATH_SEP);
647 *semi = '\0';
649 if (op != NULL)
650 *op = NULL;
652 if (inpath != NULL)
653 *inpath = NULL;
655 if (slash != NULL)
656 *slash = '\0';
658 ret = vfs_prefix_to_class (semi + 1);
659 if (ret != NULL)
661 if (op != NULL)
662 *op = semi + 1;
663 if (inpath != NULL)
664 *inpath = slash != NULL ? slash + 1 : NULL;
665 return ret;
668 if (slash != NULL)
669 *slash = PATH_SEP;
671 ret = vfs_split (path, inpath, op);
672 *semi = '#';
673 return ret;
676 /* --------------------------------------------------------------------------------------------- */
678 struct vfs_class *
679 vfs_get_class (const char *pathname)
681 struct vfs_class *vfs;
682 char *path = g_strdup (pathname);
684 vfs = _vfs_get_class (path);
685 g_free (path);
687 if (!vfs)
688 vfs = localfs_class;
690 return vfs;
693 /* --------------------------------------------------------------------------------------------- */
695 const char *
696 vfs_get_encoding (const char *path)
698 static char result[16];
699 char *work;
700 char *semi;
701 char *slash;
703 work = g_strdup (path);
704 /* try found /#enc: */
705 semi = g_strrstr (work, PATH_SEP_STR VFS_ENCODING_PREFIX);
707 if (semi != NULL)
709 semi += strlen (VFS_ENCODING_PREFIX) + 1; /* skip "/#enc:" */
710 slash = strchr (semi, PATH_SEP);
711 if (slash != NULL)
712 slash[0] = '\0';
714 g_strlcpy (result, semi, sizeof (result));
715 g_free (work);
716 return result;
718 else
720 g_free (work);
721 return NULL;
725 /* --------------------------------------------------------------------------------------------- */
727 char *
728 vfs_translate_path (const char *path)
730 estr_t state;
732 g_string_set_size (vfs_str_buffer, 0);
733 state = _vfs_translate_path (path, -1, str_cnv_from_term, vfs_str_buffer);
735 strict version
736 return (state == 0) ? vfs_str_buffer->data : NULL;
738 return (state != ESTR_FAILURE) ? vfs_str_buffer->str : NULL;
741 /* --------------------------------------------------------------------------------------------- */
743 char *
744 vfs_translate_path_n (const char *path)
746 char *result;
748 result = vfs_translate_path (path);
749 return (result != NULL) ? g_strdup (result) : NULL;
752 /* --------------------------------------------------------------------------------------------- */
754 char *
755 vfs_canon_and_translate (const char *path)
757 char *canon;
758 char *result;
759 if (path == NULL)
760 canon = g_strdup ("");
761 else
762 canon = vfs_canon (path);
763 result = vfs_translate_path_n (canon);
764 g_free (canon);
765 return result;
768 /* --------------------------------------------------------------------------------------------- */
771 mc_open (const char *filename, int flags, ...)
773 int mode = 0;
774 void *info;
775 va_list ap;
776 char *file;
777 struct vfs_class *vfs;
779 file = vfs_canon_and_translate (filename);
780 if (file == NULL)
781 return -1;
783 vfs = vfs_get_class (file);
785 /* Get the mode flag */
786 if (flags & O_CREAT)
788 va_start (ap, flags);
789 mode = va_arg (ap, int);
790 va_end (ap);
793 if (vfs->open == NULL)
795 g_free (file);
796 errno = -EOPNOTSUPP;
797 return -1;
800 info = vfs->open (vfs, file, flags, mode); /* open must be supported */
801 g_free (file);
802 if (info == NULL)
804 errno = ferrno (vfs);
805 return -1;
808 return vfs_new_handle (vfs, info);
811 /* --------------------------------------------------------------------------------------------- */
813 /* *INDENT-OFF* */
815 #define MC_NAMEOP(name, inarg, callarg) \
816 int mc_##name inarg \
818 struct vfs_class *vfs; \
819 int result; \
820 char *mpath; \
821 mpath = vfs_canon_and_translate (path); \
822 if (mpath == NULL) \
823 return -1; \
824 vfs = vfs_get_class (mpath); \
825 if (vfs == NULL) \
827 g_free (mpath); \
828 return -1; \
830 result = vfs->name != NULL ? vfs->name callarg : -1; \
831 g_free (mpath); \
832 if (result == -1) \
833 errno = vfs->name != NULL ? ferrno (vfs) : E_NOTSUPP; \
834 return result; \
837 MC_NAMEOP (chmod, (const char *path, mode_t mode), (vfs, mpath, mode))
838 MC_NAMEOP (chown, (const char *path, uid_t owner, gid_t group), (vfs, mpath, owner, group))
839 MC_NAMEOP (utime, (const char *path, struct utimbuf * times), (vfs, mpath, times))
840 MC_NAMEOP (readlink, (const char *path, char *buf, size_t bufsiz), (vfs, mpath, buf, bufsiz))
841 MC_NAMEOP (unlink, (const char *path), (vfs, mpath))
842 MC_NAMEOP (mkdir, (const char *path, mode_t mode), (vfs, mpath, mode))
843 MC_NAMEOP (rmdir, (const char *path), (vfs, mpath))
844 MC_NAMEOP (mknod, (const char *path, mode_t mode, dev_t dev), (vfs, mpath, mode, dev))
846 /* *INDENT-ON* */
848 /* --------------------------------------------------------------------------------------------- */
851 mc_symlink (const char *name1, const char *path)
853 struct vfs_class *vfs;
854 int result;
855 char *mpath;
856 char *lpath;
857 char *tmp;
859 mpath = vfs_canon_and_translate (path);
860 if (mpath == NULL)
861 return -1;
863 tmp = g_strdup (name1);
864 lpath = vfs_translate_path_n (tmp);
865 g_free (tmp);
867 if (lpath != NULL)
869 vfs = vfs_get_class (mpath);
870 result = vfs->symlink != NULL ? vfs->symlink (vfs, lpath, mpath) : -1;
871 g_free (lpath);
872 g_free (mpath);
874 if (result == -1)
875 errno = vfs->symlink != NULL ? ferrno (vfs) : E_NOTSUPP;
876 return result;
879 g_free (mpath);
881 return -1;
884 /* --------------------------------------------------------------------------------------------- */
886 /* *INDENT-OFF* */
888 #define MC_HANDLEOP(name, inarg, callarg) \
889 ssize_t mc_##name inarg \
891 struct vfs_class *vfs; \
892 int result; \
893 if (handle == -1) \
894 return -1; \
895 vfs = vfs_op (handle); \
896 if (vfs == NULL) \
897 return -1; \
898 result = vfs->name != NULL ? vfs->name callarg : -1; \
899 if (result == -1) \
900 errno = vfs->name != NULL ? ferrno (vfs) : E_NOTSUPP; \
901 return result; \
904 MC_HANDLEOP (read, (int handle, void *buffer, size_t count), (vfs_info (handle), buffer, count))
905 MC_HANDLEOP (write, (int handle, const void *buf, size_t nbyte), (vfs_info (handle), buf, nbyte))
907 /* --------------------------------------------------------------------------------------------- */
909 #define MC_RENAMEOP(name) \
910 int mc_##name (const char *fname1, const char *fname2) \
912 struct vfs_class *vfs; \
913 int result; \
914 char *name2, *name1; \
915 name1 = vfs_canon_and_translate (fname1); \
916 if (name1 == NULL) \
917 return -1; \
918 name2 = vfs_canon_and_translate (fname2); \
919 if (name2 == NULL) { \
920 g_free (name1); \
921 return -1; \
923 vfs = vfs_get_class (name1); \
924 if (vfs != vfs_get_class (name2)) \
926 errno = EXDEV; \
927 g_free (name1); \
928 g_free (name2); \
929 return -1; \
931 result = vfs->name != NULL ? vfs->name (vfs, name1, name2) : -1; \
932 g_free (name1); \
933 g_free (name2); \
934 if (result == -1) \
935 errno = vfs->name != NULL ? ferrno (vfs) : E_NOTSUPP; \
936 return result; \
939 MC_RENAMEOP (link)
940 MC_RENAMEOP (rename)
942 /* *INDENT-ON* */
944 /* --------------------------------------------------------------------------------------------- */
947 mc_ctl (int handle, int ctlop, void *arg)
949 struct vfs_class *vfs = vfs_op (handle);
951 if (vfs == NULL)
952 return 0;
954 return vfs->ctl ? (*vfs->ctl) (vfs_info (handle), ctlop, arg) : 0;
957 /* --------------------------------------------------------------------------------------------- */
960 mc_setctl (const char *path, int ctlop, void *arg)
962 struct vfs_class *vfs;
963 int result = -1;
964 char *mpath;
966 if (path == NULL)
967 vfs_die ("You don't want to pass NULL to mc_setctl.");
969 mpath = vfs_canon_and_translate (path);
970 if (mpath != NULL)
972 vfs = vfs_get_class (mpath);
973 result = vfs->setctl != NULL ? vfs->setctl (vfs, mpath, ctlop, arg) : 0;
974 g_free (mpath);
977 return result;
980 /* --------------------------------------------------------------------------------------------- */
983 mc_close (int handle)
985 struct vfs_class *vfs;
986 int result;
988 if (handle == -1 || !vfs_info (handle))
989 return -1;
991 vfs = vfs_op (handle);
992 if (vfs == NULL)
993 return -1;
995 if (handle < 3)
996 return close (handle);
998 if (!vfs->close)
999 vfs_die ("VFS must support close.\n");
1000 result = (*vfs->close) (vfs_info (handle));
1001 vfs_free_handle (handle);
1002 if (result == -1)
1003 errno = ferrno (vfs);
1005 return result;
1008 /* --------------------------------------------------------------------------------------------- */
1010 DIR *
1011 mc_opendir (const char *dirname)
1013 int handle, *handlep;
1014 void *info;
1015 struct vfs_class *vfs;
1016 char *canon;
1017 char *dname;
1018 struct vfs_dirinfo *dirinfo;
1019 const char *encoding;
1021 canon = vfs_canon (dirname);
1022 dname = vfs_translate_path_n (canon);
1024 if (dname != NULL)
1026 vfs = vfs_get_class (dname);
1027 info = vfs->opendir ? (*vfs->opendir) (vfs, dname) : NULL;
1028 g_free (dname);
1030 if (info == NULL)
1032 errno = vfs->opendir ? ferrno (vfs) : E_NOTSUPP;
1033 g_free (canon);
1034 return NULL;
1037 dirinfo = g_new (struct vfs_dirinfo, 1);
1038 dirinfo->info = info;
1040 encoding = vfs_get_encoding (canon);
1041 g_free (canon);
1042 dirinfo->converter = (encoding != NULL) ? str_crt_conv_from (encoding) : str_cnv_from_term;
1043 if (dirinfo->converter == INVALID_CONV)
1044 dirinfo->converter = str_cnv_from_term;
1046 handle = vfs_new_handle (vfs, dirinfo);
1048 handlep = g_new (int, 1);
1049 *handlep = handle;
1050 return (DIR *) handlep;
1052 else
1054 g_free (canon);
1055 return NULL;
1059 /* --------------------------------------------------------------------------------------------- */
1061 struct dirent *
1062 mc_readdir (DIR * dirp)
1064 int handle;
1065 struct vfs_class *vfs;
1066 struct dirent *entry = NULL;
1067 struct vfs_dirinfo *dirinfo;
1068 estr_t state;
1070 if (!mc_readdir_result)
1072 /* We can't just allocate struct dirent as (see man dirent.h)
1073 * struct dirent has VERY nonnaive semantics of allocating
1074 * d_name in it. Moreover, linux's glibc-2.9 allocates dirents _less_,
1075 * than 'sizeof (struct dirent)' making full bitwise (sizeof dirent) copy
1076 * heap corrupter. So, allocate longliving dirent with at least
1077 * (MAXNAMLEN + 1) for d_name in it.
1078 * Strictly saying resulting dirent is unusable as we don't adjust internal
1079 * structures, holding dirent size. But we don't use it in libc infrastructure.
1080 * TODO: to make simpler homemade dirent-alike structure.
1082 mc_readdir_result = (struct dirent *) g_malloc (sizeof (struct dirent) + MAXNAMLEN + 1);
1085 if (!dirp)
1087 errno = EFAULT;
1088 return NULL;
1090 handle = *(int *) dirp;
1092 vfs = vfs_op (handle);
1093 if (vfs == NULL)
1094 return NULL;
1096 dirinfo = vfs_info (handle);
1097 if (vfs->readdir)
1099 entry = (*vfs->readdir) (dirinfo->info);
1100 if (entry == NULL)
1101 return NULL;
1102 g_string_set_size (vfs_str_buffer, 0);
1103 state = str_vfs_convert_from (dirinfo->converter, entry->d_name, vfs_str_buffer);
1104 mc_readdir_result->d_ino = entry->d_ino;
1105 g_strlcpy (mc_readdir_result->d_name, vfs_str_buffer->str, MAXNAMLEN + 1);
1107 if (entry == NULL)
1108 errno = vfs->readdir ? ferrno (vfs) : E_NOTSUPP;
1109 return (entry != NULL) ? mc_readdir_result : NULL;
1112 /* --------------------------------------------------------------------------------------------- */
1115 mc_closedir (DIR * dirp)
1117 int handle = *(int *) dirp;
1118 struct vfs_class *vfs;
1119 int result = -1;
1121 vfs = vfs_op (handle);
1122 if (vfs != NULL)
1124 struct vfs_dirinfo *dirinfo;
1126 dirinfo = vfs_info (handle);
1127 if (dirinfo->converter != str_cnv_from_term)
1128 str_close_conv (dirinfo->converter);
1130 result = vfs->closedir ? (*vfs->closedir) (dirinfo->info) : -1;
1131 vfs_free_handle (handle);
1132 g_free (dirinfo);
1134 g_free (dirp);
1135 return result;
1138 /* --------------------------------------------------------------------------------------------- */
1141 mc_stat (const char *filename, struct stat *buf)
1143 struct vfs_class *vfs;
1144 int result;
1145 char *path;
1147 path = vfs_canon_and_translate (filename);
1149 if (path == NULL)
1150 return -1;
1152 vfs = vfs_get_class (path);
1154 if (vfs == NULL)
1156 g_free (path);
1157 return -1;
1160 result = vfs->stat ? (*vfs->stat) (vfs, path, buf) : -1;
1162 g_free (path);
1164 if (result == -1)
1165 errno = vfs->name ? ferrno (vfs) : E_NOTSUPP;
1166 return result;
1169 /* --------------------------------------------------------------------------------------------- */
1172 mc_lstat (const char *filename, struct stat *buf)
1174 struct vfs_class *vfs;
1175 int result;
1176 char *path;
1178 path = vfs_canon_and_translate (filename);
1180 if (path == NULL)
1181 return -1;
1183 vfs = vfs_get_class (path);
1184 if (vfs == NULL)
1186 g_free (path);
1187 return -1;
1190 result = vfs->lstat ? (*vfs->lstat) (vfs, path, buf) : -1;
1191 g_free (path);
1192 if (result == -1)
1193 errno = vfs->name ? ferrno (vfs) : E_NOTSUPP;
1194 return result;
1197 /* --------------------------------------------------------------------------------------------- */
1200 mc_fstat (int handle, struct stat *buf)
1202 struct vfs_class *vfs;
1203 int result;
1205 if (handle == -1)
1206 return -1;
1208 vfs = vfs_op (handle);
1209 if (vfs == NULL)
1210 return -1;
1212 result = vfs->fstat ? (*vfs->fstat) (vfs_info (handle), buf) : -1;
1213 if (result == -1)
1214 errno = vfs->name ? ferrno (vfs) : E_NOTSUPP;
1215 return result;
1218 /* --------------------------------------------------------------------------------------------- */
1220 * Return current directory. If it's local, reread the current directory
1221 * from the OS. Put directory to the provided buffer.
1224 char *
1225 mc_get_current_wd (char *buffer, size_t size)
1227 const char *cwd = _vfs_get_cwd ();
1229 g_strlcpy (buffer, cwd, size);
1230 return buffer;
1233 /* --------------------------------------------------------------------------------------------- */
1235 * Return current directory without any OS calls.
1238 char *
1239 vfs_get_current_dir (void)
1241 return current_dir;
1244 /* --------------------------------------------------------------------------------------------- */
1246 off_t
1247 mc_lseek (int fd, off_t offset, int whence)
1249 struct vfs_class *vfs;
1250 off_t result;
1252 if (fd == -1)
1253 return -1;
1255 vfs = vfs_op (fd);
1256 if (vfs == NULL)
1257 return -1;
1259 result = vfs->lseek ? (*vfs->lseek) (vfs_info (fd), offset, whence) : -1;
1260 if (result == -1)
1261 errno = vfs->lseek ? ferrno (vfs) : E_NOTSUPP;
1262 return result;
1265 /* --------------------------------------------------------------------------------------------- */
1267 * remove //, /./ and /../
1270 char *
1271 vfs_canon (const char *path)
1273 if (!path)
1274 vfs_die ("Cannot canonicalize NULL");
1276 /* Relative to current directory */
1277 if (*path != PATH_SEP)
1279 char *local, *result;
1281 local = concat_dir_and_file (current_dir, path);
1283 result = vfs_canon (local);
1284 g_free (local);
1285 return result;
1289 * So we have path of following form:
1290 * /p1/p2#op/.././././p3#op/p4. Good luck.
1293 char *result = g_strdup (path);
1294 canonicalize_pathname (result);
1295 return result;
1299 /* --------------------------------------------------------------------------------------------- */
1301 * VFS chdir.
1302 * Return 0 on success, -1 on failure.
1306 mc_chdir (const char *path)
1308 char *new_dir;
1309 char *trans_dir;
1310 struct vfs_class *old_vfs, *new_vfs;
1311 vfsid old_vfsid;
1312 int result;
1314 new_dir = vfs_canon (path);
1315 trans_dir = vfs_translate_path_n (new_dir);
1316 if (trans_dir != NULL)
1318 new_vfs = vfs_get_class (trans_dir);
1319 if (!new_vfs->chdir)
1321 g_free (new_dir);
1322 g_free (trans_dir);
1323 return -1;
1326 result = (*new_vfs->chdir) (new_vfs, trans_dir);
1328 if (result == -1)
1330 errno = ferrno (new_vfs);
1331 g_free (new_dir);
1332 g_free (trans_dir);
1333 return -1;
1336 old_vfsid = vfs_getid (current_vfs, current_dir);
1337 old_vfs = current_vfs;
1339 /* Actually change directory */
1340 g_free (current_dir);
1341 current_dir = new_dir;
1342 current_vfs = new_vfs;
1344 /* This function uses the new current_dir implicitly */
1345 vfs_stamp_create (old_vfs, old_vfsid);
1347 /* Sometimes we assume no trailing slash on cwd */
1348 if (*current_dir)
1350 char *p;
1351 p = strchr (current_dir, 0) - 1;
1352 if (*p == PATH_SEP && p > current_dir)
1353 *p = 0;
1356 g_free (trans_dir);
1357 return 0;
1359 else
1361 g_free (new_dir);
1362 return -1;
1366 /* --------------------------------------------------------------------------------------------- */
1367 /* Return TRUE is the current VFS class is local */
1369 gboolean
1370 vfs_current_is_local (void)
1372 return (current_vfs->flags & VFSF_LOCAL) != 0;
1375 /* --------------------------------------------------------------------------------------------- */
1376 /* Return flags of the VFS class of the given filename */
1378 vfs_class_flags_t
1379 vfs_file_class_flags (const char *filename)
1381 struct vfs_class *vfs;
1382 char *fname;
1384 fname = vfs_canon_and_translate (filename);
1385 if (fname == NULL)
1386 return VFSF_UNKNOWN;
1388 vfs = vfs_get_class (fname);
1389 g_free (fname);
1390 return vfs->flags;
1393 /* --------------------------------------------------------------------------------------------- */
1395 char *
1396 mc_getlocalcopy (const char *pathname)
1398 char *result = NULL;
1399 char *path;
1401 path = vfs_canon_and_translate (pathname);
1402 if (path != NULL)
1404 struct vfs_class *vfs = vfs_get_class (path);
1406 result = vfs->getlocalcopy != NULL ?
1407 vfs->getlocalcopy (vfs, path) : mc_def_getlocalcopy (path);
1408 g_free (path);
1409 if (result == NULL)
1410 errno = ferrno (vfs);
1413 return result;
1416 /* --------------------------------------------------------------------------------------------- */
1419 mc_ungetlocalcopy (const char *pathname, const char *local, int has_changed)
1421 int return_value = -1;
1422 char *path;
1424 path = vfs_canon_and_translate (pathname);
1425 if (path != NULL)
1427 struct vfs_class *vfs = vfs_get_class (path);
1429 return_value = vfs->ungetlocalcopy != NULL ?
1430 vfs->ungetlocalcopy (vfs, path, local, has_changed) :
1431 mc_def_ungetlocalcopy (vfs, path, local, has_changed);
1432 g_free (path);
1435 return return_value;
1438 /* --------------------------------------------------------------------------------------------- */
1440 void
1441 vfs_init (void)
1443 /* create the VFS handle array */
1444 vfs_openfiles = g_ptr_array_new ();
1446 vfs_str_buffer = g_string_new ("");
1447 /* localfs needs to be the first one */
1448 init_localfs ();
1449 /* fallback value for vfs_get_class() */
1450 localfs_class = vfs_list;
1452 #ifdef ENABLE_VFS_CPIO
1453 init_cpiofs ();
1454 #endif /* ENABLE_VFS_CPIO */
1455 #ifdef ENABLE_VFS_TAR
1456 init_tarfs ();
1457 #endif /* ENABLE_VFS_TAR */
1458 #ifdef ENABLE_VFS_SFS
1459 init_sfs ();
1460 #endif /* ENABLE_VFS_SFS */
1461 #ifdef ENABLE_VFS_EXTFS
1462 init_extfs ();
1463 #endif /* ENABLE_VFS_EXTFS */
1464 #ifdef ENABLE_VFS_UNDELFS
1465 init_undelfs ();
1466 #endif /* ENABLE_VFS_UNDELFS */
1468 #ifdef ENABLE_VFS_FTP
1469 init_ftpfs ();
1470 #endif /* ENABLE_VFS_FTP */
1471 #ifdef ENABLE_VFS_FISH
1472 init_fish ();
1473 #endif /* ENABLE_VFS_FISH */
1474 #ifdef ENABLE_VFS_SMB
1475 init_smbfs ();
1476 #endif /* ENABLE_VFS_SMB */
1478 vfs_setup_wd ();
1481 /* --------------------------------------------------------------------------------------------- */
1483 void
1484 vfs_shut (void)
1486 struct vfs_class *vfs;
1488 vfs_gc_done ();
1490 g_free (current_dir);
1492 for (vfs = vfs_list; vfs; vfs = vfs->next)
1493 if (vfs->done)
1494 (*vfs->done) (vfs);
1496 g_ptr_array_free (vfs_openfiles, TRUE);
1497 g_string_free (vfs_str_buffer, TRUE);
1498 g_free (mc_readdir_result);
1501 /* --------------------------------------------------------------------------------------------- */
1503 * These ones grab information from the VFS
1504 * and handles them to an upper layer
1507 void
1508 vfs_fill_names (fill_names_f func)
1510 struct vfs_class *vfs;
1512 for (vfs = vfs_list; vfs; vfs = vfs->next)
1513 if (vfs->fill_names)
1514 (*vfs->fill_names) (vfs, func);
1517 /* --------------------------------------------------------------------------------------------- */
1519 * Returns vfs path corresponding to given url. If passed string is
1520 * not recognized as url, g_strdup(url) is returned.
1523 char *
1524 vfs_translate_url (const char *url)
1526 size_t i;
1528 for (i = 0; i < sizeof (url_table) / sizeof (url_table[0]); i++)
1529 if (strncmp (url, url_table[i].name, url_table[i].name_len) == 0)
1530 return g_strconcat (url_table[i].substitute, url + url_table[i].name_len,
1531 (char *) NULL);
1533 return g_strdup (url);
1536 /* --------------------------------------------------------------------------------------------- */
1538 gboolean
1539 vfs_file_is_local (const char *filename)
1541 return (vfs_file_class_flags (filename) & VFSF_LOCAL) != 0;
1544 /* --------------------------------------------------------------------------------------------- */