2 Virtual File System: interface functions
4 Copyright (C) 2011-2017
5 Free Software Foundation, Inc.
8 Slava Zanko <slavazanko@gmail.com>, 2011, 2013
10 This file is part of the Midnight Commander.
12 The Midnight Commander is free software: you can redistribute it
13 and/or modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation, either version 3 of the License,
15 or (at your option) any later version.
17 The Midnight Commander is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>.
28 * \brief Source: Virtual File System: path handlers
37 #include <stdlib.h> /* For atol() */
41 #include <sys/types.h>
43 #include <ctype.h> /* is_digit() */
50 #include "lib/global.h"
52 #include "lib/widget.h" /* message() */
53 #include "lib/strutil.h" /* str_crt_conv_from() */
60 #include "xdirentry.h"
62 /* TODO: move it to separate private .h */
63 extern GString
*vfs_str_buffer
;
64 extern vfs_class
*current_vfs
;
65 extern struct dirent
*mc_readdir_result
;
67 /*** global variables ****************************************************************************/
69 struct dirent
*mc_readdir_result
= NULL
;
71 /*** file scope macro definitions ****************************************************************/
73 /*** file scope type declarations ****************************************************************/
75 /*** file scope variables ************************************************************************/
77 /*** file scope functions ************************************************************************/
78 /* --------------------------------------------------------------------------------------------- */
81 mc_def_getlocalcopy (const vfs_path_t
* filename_vpath
)
83 vfs_path_t
*tmp_vpath
= NULL
;
86 char buffer
[BUF_1K
* 8];
89 fdin
= mc_open (filename_vpath
, O_RDONLY
| O_LINEAR
);
93 fdout
= vfs_mkstemps (&tmp_vpath
, "vfs", vfs_path_get_last_path_str (filename_vpath
));
97 while ((i
= mc_read (fdin
, buffer
, sizeof (buffer
))) > 0)
99 if (write (fdout
, buffer
, i
) != i
)
114 if (mc_stat (filename_vpath
, &mystat
) != -1)
115 mc_chmod (tmp_vpath
, mystat
.st_mode
);
120 vfs_path_free (tmp_vpath
);
128 /* --------------------------------------------------------------------------------------------- */
131 mc_def_ungetlocalcopy (const vfs_path_t
* filename_vpath
,
132 const vfs_path_t
* local_vpath
, gboolean has_changed
)
134 int fdin
= -1, fdout
= -1;
137 local
= vfs_path_get_last_path_str (local_vpath
);
141 char buffer
[BUF_1K
* 8];
144 if (vfs_path_get_last_path_vfs (filename_vpath
)->write
== NULL
)
147 fdin
= open (local
, O_RDONLY
);
150 fdout
= mc_open (filename_vpath
, O_WRONLY
| O_TRUNC
);
153 while ((i
= read (fdin
, buffer
, sizeof (buffer
))) > 0)
154 if (mc_write (fdout
, buffer
, (size_t) i
) != i
)
159 if (close (fdin
) == -1)
165 if (mc_close (fdout
) == -1)
175 message (D_ERROR
, _("Changes to file lost"), "%s", vfs_path_get_last_path_str (filename_vpath
));
184 /* --------------------------------------------------------------------------------------------- */
185 /*** public functions ****************************************************************************/
186 /* --------------------------------------------------------------------------------------------- */
189 mc_open (const vfs_path_t
* vpath
, int flags
, ...)
193 const vfs_path_element_t
*path_element
;
198 /* Get the mode flag */
202 va_start (ap
, flags
);
203 /* We have to use PROMOTED_MODE_T instead of mode_t. Doing 'va_arg (ap, mode_t)'
204 * fails on systems where 'mode_t' is smaller than 'int' because of C's "default
205 * argument promotions". */
206 mode
= va_arg (ap
, PROMOTED_MODE_T
);
210 path_element
= vfs_path_get_by_index (vpath
, -1);
211 if (vfs_path_element_valid (path_element
) && path_element
->class->open
!= NULL
)
214 /* open must be supported */
215 info
= path_element
->class->open (vpath
, flags
, mode
);
217 errno
= vfs_ferrno (path_element
->class);
219 result
= vfs_new_handle (path_element
->class, info
);
227 /* --------------------------------------------------------------------------------------------- */
231 #define MC_NAMEOP(name, inarg, callarg) \
232 int mc_##name inarg \
235 const vfs_path_element_t *path_element; \
240 path_element = vfs_path_get_by_index (vpath, -1); \
241 if (!vfs_path_element_valid (path_element)) \
246 result = path_element->class->name != NULL ? path_element->class->name callarg : -1; \
248 errno = path_element->class->name != NULL ? vfs_ferrno (path_element->class) : E_NOTSUPP; \
252 MC_NAMEOP (chmod
, (const vfs_path_t
*vpath
, mode_t mode
), (vpath
, mode
))
253 MC_NAMEOP (chown
, (const vfs_path_t
*vpath
, uid_t owner
, gid_t group
), (vpath
, owner
, group
))
254 MC_NAMEOP (utime
, (const vfs_path_t
*vpath
, mc_timesbuf_t
* times
), (vpath
, times
))
255 MC_NAMEOP (readlink
, (const vfs_path_t
*vpath
, char *buf
, size_t bufsiz
), (vpath
, buf
, bufsiz
))
256 MC_NAMEOP (unlink
, (const vfs_path_t
*vpath
), (vpath
))
257 MC_NAMEOP (mkdir
, (const vfs_path_t
*vpath
, mode_t mode
), (vpath
, mode
))
258 MC_NAMEOP (rmdir
, (const vfs_path_t
*vpath
), (vpath
))
259 MC_NAMEOP (mknod
, (const vfs_path_t
*vpath
, mode_t mode
, dev_t dev
), (vpath
, mode
, dev
))
263 /* --------------------------------------------------------------------------------------------- */
266 mc_symlink (const vfs_path_t
* vpath1
, const vfs_path_t
* vpath2
)
275 const vfs_path_element_t
*path_element
;
277 path_element
= vfs_path_get_by_index (vpath2
, -1);
278 if (vfs_path_element_valid (path_element
))
281 path_element
->class->symlink
!= NULL
?
282 path_element
->class->symlink (vpath1
, vpath2
) : -1;
286 path_element
->class->symlink
!= NULL
?
287 vfs_ferrno (path_element
->class) : E_NOTSUPP
;
293 /* --------------------------------------------------------------------------------------------- */
297 #define MC_HANDLEOP(name) \
298 ssize_t mc_##name (int handle, C void *buf, size_t count) \
300 struct vfs_class *vfs; \
301 void *fsinfo = NULL; \
305 vfs = vfs_class_find_by_handle (handle, &fsinfo); \
308 result = vfs->name != NULL ? vfs->name (fsinfo, buf, count) : -1; \
310 errno = vfs->name != NULL ? vfs_ferrno (vfs) : E_NOTSUPP; \
321 /* --------------------------------------------------------------------------------------------- */
323 #define MC_RENAMEOP(name) \
324 int mc_##name (const vfs_path_t *vpath1, const vfs_path_t *vpath2) \
327 const vfs_path_element_t *path_element1; \
328 const vfs_path_element_t *path_element2; \
330 if (vpath1 == NULL || vpath2 == NULL) \
333 path_element1 = vfs_path_get_by_index (vpath1, (-1)); \
334 path_element2 = vfs_path_get_by_index (vpath2, (-1)); \
336 if (!vfs_path_element_valid (path_element1) || !vfs_path_element_valid (path_element2) || \
337 path_element1->class != path_element2->class) \
343 result = path_element1->class->name != NULL \
344 ? path_element1->class->name (vpath1, vpath2) \
347 errno = path_element1->class->name != NULL ? vfs_ferrno (path_element1->class) : E_NOTSUPP; \
356 /* --------------------------------------------------------------------------------------------- */
359 mc_ctl (int handle
, int ctlop
, void *arg
)
361 struct vfs_class
*vfs
;
364 vfs
= vfs_class_find_by_handle (handle
, &fsinfo
);
366 return (vfs
== NULL
|| vfs
->ctl
== NULL
) ? 0 : vfs
->ctl (fsinfo
, ctlop
, arg
);
369 /* --------------------------------------------------------------------------------------------- */
372 mc_setctl (const vfs_path_t
* vpath
, int ctlop
, void *arg
)
375 const vfs_path_element_t
*path_element
;
378 vfs_die ("You don't want to pass NULL to mc_setctl.");
380 path_element
= vfs_path_get_by_index (vpath
, -1);
381 if (vfs_path_element_valid (path_element
))
383 path_element
->class->setctl
!= NULL
? path_element
->class->setctl (vpath
,
389 /* --------------------------------------------------------------------------------------------- */
392 mc_close (int handle
)
394 struct vfs_class
*vfs
;
401 vfs
= vfs_class_find_by_handle (handle
, &fsinfo
);
402 if (vfs
== NULL
|| fsinfo
== NULL
)
406 return close (handle
);
409 vfs_die ("VFS must support close.\n");
410 result
= (*vfs
->close
) (fsinfo
);
411 vfs_free_handle (handle
);
413 errno
= vfs_ferrno (vfs
);
418 /* --------------------------------------------------------------------------------------------- */
421 mc_opendir (const vfs_path_t
* vpath
)
423 int handle
, *handlep
;
425 vfs_path_element_t
*path_element
;
430 path_element
= (vfs_path_element_t
*) vfs_path_get_by_index (vpath
, -1);
432 if (!vfs_path_element_valid (path_element
))
438 info
= path_element
->class->opendir
? (*path_element
->class->opendir
) (vpath
) : NULL
;
442 errno
= path_element
->class->opendir
? vfs_ferrno (path_element
->class) : E_NOTSUPP
;
446 path_element
->dir
.info
= info
;
449 path_element
->dir
.converter
= (path_element
->encoding
!= NULL
) ?
450 str_crt_conv_from (path_element
->encoding
) : str_cnv_from_term
;
451 if (path_element
->dir
.converter
== INVALID_CONV
)
452 path_element
->dir
.converter
= str_cnv_from_term
;
455 handle
= vfs_new_handle (path_element
->class, vfs_path_element_clone (path_element
));
457 handlep
= g_new (int, 1);
459 return (DIR *) handlep
;
462 /* --------------------------------------------------------------------------------------------- */
465 mc_readdir (DIR * dirp
)
468 struct vfs_class
*vfs
;
470 struct dirent
*entry
= NULL
;
471 vfs_path_element_t
*vfs_path_element
;
473 if (!mc_readdir_result
)
475 /* We can't just allocate struct dirent as (see man dirent.h)
476 * struct dirent has VERY nonnaive semantics of allocating
477 * d_name in it. Moreover, linux's glibc-2.9 allocates dirents _less_,
478 * than 'sizeof (struct dirent)' making full bitwise (sizeof dirent) copy
479 * heap corrupter. So, allocate longliving dirent with at least
480 * (MAXNAMLEN + 1) for d_name in it.
481 * Strictly saying resulting dirent is unusable as we don't adjust internal
482 * structures, holding dirent size. But we don't use it in libc infrastructure.
483 * TODO: to make simpler homemade dirent-alike structure.
485 mc_readdir_result
= (struct dirent
*) g_malloc (sizeof (struct dirent
) + MAXNAMLEN
+ 1);
493 handle
= *(int *) dirp
;
495 vfs
= vfs_class_find_by_handle (handle
, &fsinfo
);
496 if (vfs
== NULL
|| fsinfo
== NULL
)
499 vfs_path_element
= (vfs_path_element_t
*) fsinfo
;
502 entry
= (*vfs
->readdir
) (vfs_path_element
->dir
.info
);
506 g_string_set_size (vfs_str_buffer
, 0);
508 str_vfs_convert_from (vfs_path_element
->dir
.converter
, entry
->d_name
, vfs_str_buffer
);
510 g_string_assign (vfs_str_buffer
, entry
->d_name
);
512 mc_readdir_result
->d_ino
= entry
->d_ino
;
513 g_strlcpy (mc_readdir_result
->d_name
, vfs_str_buffer
->str
, MAXNAMLEN
+ 1);
516 errno
= vfs
->readdir
? vfs_ferrno (vfs
) : E_NOTSUPP
;
517 return (entry
!= NULL
) ? mc_readdir_result
: NULL
;
520 /* --------------------------------------------------------------------------------------------- */
523 mc_closedir (DIR * dirp
)
526 struct vfs_class
*vfs
;
533 handle
= *(int *) dirp
;
535 vfs
= vfs_class_find_by_handle (handle
, &fsinfo
);
536 if (vfs
!= NULL
&& fsinfo
!= NULL
)
538 vfs_path_element_t
*vfs_path_element
= (vfs_path_element_t
*) fsinfo
;
541 if (vfs_path_element
->dir
.converter
!= str_cnv_from_term
)
543 str_close_conv (vfs_path_element
->dir
.converter
);
544 vfs_path_element
->dir
.converter
= INVALID_CONV
;
548 result
= vfs
->closedir
? (*vfs
->closedir
) (vfs_path_element
->dir
.info
) : -1;
549 vfs_free_handle (handle
);
550 vfs_path_element_free (vfs_path_element
);
556 /* --------------------------------------------------------------------------------------------- */
559 mc_stat (const vfs_path_t
* vpath
, struct stat
*buf
)
562 const vfs_path_element_t
*path_element
;
567 path_element
= vfs_path_get_by_index (vpath
, -1);
569 if (vfs_path_element_valid (path_element
))
571 result
= path_element
->class->stat
? (*path_element
->class->stat
) (vpath
, buf
) : -1;
573 errno
= path_element
->class->name
? vfs_ferrno (path_element
->class) : E_NOTSUPP
;
579 /* --------------------------------------------------------------------------------------------- */
582 mc_lstat (const vfs_path_t
* vpath
, struct stat
*buf
)
585 const vfs_path_element_t
*path_element
;
590 path_element
= vfs_path_get_by_index (vpath
, -1);
592 if (vfs_path_element_valid (path_element
))
594 result
= path_element
->class->lstat
? (*path_element
->class->lstat
) (vpath
, buf
) : -1;
596 errno
= path_element
->class->name
? vfs_ferrno (path_element
->class) : E_NOTSUPP
;
602 /* --------------------------------------------------------------------------------------------- */
605 mc_fstat (int handle
, struct stat
*buf
)
607 struct vfs_class
*vfs
;
614 vfs
= vfs_class_find_by_handle (handle
, &fsinfo
);
618 result
= vfs
->fstat
? (*vfs
->fstat
) (fsinfo
, buf
) : -1;
620 errno
= vfs
->fstat
? vfs_ferrno (vfs
) : E_NOTSUPP
;
624 /* --------------------------------------------------------------------------------------------- */
627 mc_getlocalcopy (const vfs_path_t
* pathname_vpath
)
629 vfs_path_t
*result
= NULL
;
630 const vfs_path_element_t
*path_element
;
632 if (pathname_vpath
== NULL
)
635 path_element
= vfs_path_get_by_index (pathname_vpath
, -1);
637 if (vfs_path_element_valid (path_element
))
639 result
= path_element
->class->getlocalcopy
!= NULL
?
640 path_element
->class->getlocalcopy (pathname_vpath
) :
641 mc_def_getlocalcopy (pathname_vpath
);
643 errno
= vfs_ferrno (path_element
->class);
648 /* --------------------------------------------------------------------------------------------- */
651 mc_ungetlocalcopy (const vfs_path_t
* pathname_vpath
, const vfs_path_t
* local_vpath
,
652 gboolean has_changed
)
654 int return_value
= -1;
655 const vfs_path_element_t
*path_element
;
657 if (pathname_vpath
== NULL
)
660 path_element
= vfs_path_get_by_index (pathname_vpath
, -1);
662 if (vfs_path_element_valid (path_element
))
663 return_value
= path_element
->class->ungetlocalcopy
!= NULL
?
664 path_element
->class->ungetlocalcopy (pathname_vpath
, local_vpath
, has_changed
) :
665 mc_def_ungetlocalcopy (pathname_vpath
, local_vpath
, has_changed
);
670 /* --------------------------------------------------------------------------------------------- */
674 * @param vpath VFS-path
676 * @return 0 on success, -1 on failure.
680 mc_chdir (const vfs_path_t
* vpath
)
682 struct vfs_class
*old_vfs
;
685 const vfs_path_element_t
*path_element
;
686 vfs_path_t
*cd_vpath
;
692 cd_vpath
= vfs_path_to_absolute (vpath
);
694 cd_vpath
= vfs_path_clone (vpath
);
696 path_element
= vfs_path_get_by_index (cd_vpath
, -1);
698 if (!vfs_path_element_valid (path_element
) || path_element
->class->chdir
== NULL
)
703 result
= (*path_element
->class->chdir
) (cd_vpath
);
707 errno
= vfs_ferrno (path_element
->class);
711 old_vfsid
= vfs_getid (vfs_get_raw_current_dir ());
712 old_vfs
= current_vfs
;
714 /* Actually change directory */
715 vfs_set_raw_current_dir (cd_vpath
);
717 current_vfs
= path_element
->class;
719 /* This function uses the new current_dir implicitly */
720 vfs_stamp_create (old_vfs
, old_vfsid
);
722 /* Sometimes we assume no trailing slash on cwd */
723 path_element
= vfs_path_get_by_index (vfs_get_raw_current_dir (), -1);
724 if (vfs_path_element_valid (path_element
))
726 if (*path_element
->path
!= '\0')
730 p
= strchr (path_element
->path
, 0) - 1;
731 if (IS_PATH_SEP (*p
) && p
> path_element
->path
)
735 #ifdef ENABLE_VFS_NET
737 struct vfs_s_super
*super
;
739 super
= vfs_get_super_by_vpath (vpath
);
740 if (super
!= NULL
&& super
->path_element
!= NULL
)
742 g_free (super
->path_element
->path
);
743 super
->path_element
->path
= g_strdup (path_element
->path
);
746 #endif /* ENABLE_VFS_NET */
752 vfs_path_free (cd_vpath
);
756 /* --------------------------------------------------------------------------------------------- */
759 mc_lseek (int fd
, off_t offset
, int whence
)
761 struct vfs_class
*vfs
;
768 vfs
= vfs_class_find_by_handle (fd
, &fsinfo
);
772 result
= vfs
->lseek
? (*vfs
->lseek
) (fsinfo
, offset
, whence
) : -1;
774 errno
= vfs
->lseek
? vfs_ferrno (vfs
) : E_NOTSUPP
;
778 /* --------------------------------------------------------------------------------------------- */
779 /* Following code heavily borrows from libiberty, mkstemps.c */
782 * pname (output) - pointer to the name of the temp file (needs g_free).
783 * NULL if the function fails.
784 * prefix - part of the filename before the random part.
785 * Prepend $TMPDIR or /tmp if there are no path separators.
786 * suffix - if not NULL, part of the filename after the random part.
789 * handle of the open file or -1 if couldn't open any.
793 mc_mkstemps (vfs_path_t
** pname_vpath
, const char *prefix
, const char *suffix
)
798 if (strchr (prefix
, PATH_SEP
) != NULL
)
799 p1
= g_strdup (prefix
);
802 /* Add prefix first to find the position of XXXXXX */
803 p1
= g_build_filename (mc_tmpdir (), prefix
, (char *) NULL
);
806 p2
= g_strconcat (p1
, "XXXXXX", suffix
, (char *) NULL
);
811 *pname_vpath
= vfs_path_from_str (p2
);
823 /* --------------------------------------------------------------------------------------------- */
825 * Return the directory where mc should keep its temporary files.
826 * This directory is (in Bourne shell terms) "${TMPDIR=/tmp}/mc-$USER"
827 * When called the first time, the directory is created if needed.
828 * The first call should be done early, since we are using fprintf()
829 * and not message() to report possible problems.
835 static char buffer
[PATH_MAX
];
836 static const char *tmpdir
= NULL
;
840 const char *error
= NULL
;
842 /* Check if already correctly initialized */
843 if (tmpdir
&& lstat (tmpdir
, &st
) == 0 && S_ISDIR (st
.st_mode
) &&
844 st
.st_uid
== getuid () && (st
.st_mode
& 0777) == 0700)
847 sys_tmp
= getenv ("TMPDIR");
848 if (sys_tmp
== NULL
|| !IS_PATH_SEP (sys_tmp
[0]))
849 sys_tmp
= TMPDIR_DEFAULT
;
851 pwd
= getpwuid (getuid ());
854 g_snprintf (buffer
, sizeof (buffer
), "%s/mc-%s", sys_tmp
, pwd
->pw_name
);
856 g_snprintf (buffer
, sizeof (buffer
), "%s/mc-%lu", sys_tmp
, (unsigned long) getuid ());
858 canonicalize_pathname (buffer
);
860 /* Try to create directory */
861 if (mkdir (buffer
, S_IRWXU
) != 0)
863 if (errno
== EEXIST
&& lstat (buffer
, &st
) == 0)
865 /* Sanity check for existing directory */
866 if (!S_ISDIR (st
.st_mode
))
867 error
= _("%s is not a directory\n");
868 else if (st
.st_uid
!= getuid ())
869 error
= _("Directory %s is not owned by you\n");
870 else if (((st
.st_mode
& 0777) != 0700) && (chmod (buffer
, 0700) != 0))
871 error
= _("Cannot set correct permissions for directory %s\n");
876 _("Cannot create temporary directory %s: %s\n"),
877 buffer
, unix_error_string (errno
));
885 char *fallback_prefix
;
886 gboolean fallback_ok
= FALSE
;
887 vfs_path_t
*test_vpath
;
890 fprintf (stderr
, error
, buffer
);
892 /* Test if sys_tmp is suitable for temporary files */
893 fallback_prefix
= g_strdup_printf ("%s/mctest", sys_tmp
);
894 test_fd
= mc_mkstemps (&test_vpath
, fallback_prefix
, NULL
);
895 g_free (fallback_prefix
);
899 test_fd
= open (vfs_path_as_str (test_vpath
), O_RDONLY
);
903 unlink (vfs_path_as_str (test_vpath
));
910 fprintf (stderr
, _("Temporary files will be created in %s\n"), sys_tmp
);
911 g_snprintf (buffer
, sizeof (buffer
), "%s", sys_tmp
);
916 fprintf (stderr
, _("Temporary files will not be created\n"));
917 g_snprintf (buffer
, sizeof (buffer
), "%s", "/dev/null/");
920 vfs_path_free (test_vpath
);
921 fprintf (stderr
, "%s\n", _("Press any key to continue..."));
928 g_setenv ("MC_TMPDIR", tmpdir
, TRUE
);
933 /* --------------------------------------------------------------------------------------------- */