2 Virtual File System: interface functions
5 The Free Software Foundation, Inc.
8 Slava Zanko <slavazanko@gmail.com>, 2011
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() */
51 #include "lib/global.h"
53 #include "lib/widget.h" /* message() */
54 #include "lib/strutil.h" /* str_crt_conv_from() */
62 extern GString
*vfs_str_buffer
;
63 extern struct vfs_class
*current_vfs
;
65 /*** global variables ****************************************************************************/
67 struct dirent
*mc_readdir_result
= NULL
;
69 /*** file scope macro definitions ****************************************************************/
71 /*** file scope type declarations ****************************************************************/
73 /*** file scope variables ************************************************************************/
75 /*** file scope functions ************************************************************************/
76 /* --------------------------------------------------------------------------------------------- */
79 mc_def_getlocalcopy (const vfs_path_t
* filename_vpath
)
81 vfs_path_t
*tmp_vpath
= NULL
;
82 int fdin
= -1, fdout
= -1;
84 char buffer
[BUF_1K
* 8];
87 fdin
= mc_open (filename_vpath
, O_RDONLY
| O_LINEAR
);
91 fdout
= vfs_mkstemps (&tmp_vpath
, "vfs", vfs_path_get_last_path_str (filename_vpath
));
95 while ((i
= mc_read (fdin
, buffer
, sizeof (buffer
))) > 0)
97 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
, ...)
191 int mode
= 0, result
= -1;
192 const vfs_path_element_t
*path_element
;
197 /* Get the mode flag */
201 va_start (ap
, flags
);
202 mode
= va_arg (ap
, int);
206 path_element
= vfs_path_get_by_index (vpath
, -1);
207 if (vfs_path_element_valid (path_element
) && path_element
->class->open
!= NULL
)
210 /* open must be supported */
211 info
= path_element
->class->open (vpath
, flags
, mode
);
213 errno
= vfs_ferrno (path_element
->class);
215 result
= vfs_new_handle (path_element
->class, info
);
223 /* --------------------------------------------------------------------------------------------- */
227 #define MC_NAMEOP(name, inarg, callarg) \
228 int mc_##name inarg \
231 const vfs_path_element_t *path_element; \
236 path_element = vfs_path_get_by_index (vpath, -1); \
237 if (!vfs_path_element_valid (path_element)) \
242 result = path_element->class->name != NULL ? path_element->class->name callarg : -1; \
244 errno = path_element->class->name != NULL ? vfs_ferrno (path_element->class) : E_NOTSUPP; \
248 MC_NAMEOP (chmod
, (const vfs_path_t
*vpath
, mode_t mode
), (vpath
, mode
))
249 MC_NAMEOP (chown
, (const vfs_path_t
*vpath
, uid_t owner
, gid_t group
), (vpath
, owner
, group
))
250 MC_NAMEOP (utime
, (const vfs_path_t
*vpath
, struct utimbuf
* times
), (vpath
, times
))
251 MC_NAMEOP (readlink
, (const vfs_path_t
*vpath
, char *buf
, size_t bufsiz
), (vpath
, buf
, bufsiz
))
252 MC_NAMEOP (unlink
, (const vfs_path_t
*vpath
), (vpath
))
253 MC_NAMEOP (mkdir
, (const vfs_path_t
*vpath
, mode_t mode
), (vpath
, mode
))
254 MC_NAMEOP (rmdir
, (const vfs_path_t
*vpath
), (vpath
))
255 MC_NAMEOP (mknod
, (const vfs_path_t
*vpath
, mode_t mode
, dev_t dev
), (vpath
, mode
, dev
))
259 /* --------------------------------------------------------------------------------------------- */
262 mc_symlink (const vfs_path_t
* vpath1
, const vfs_path_t
* vpath2
)
271 const vfs_path_element_t
*path_element
;
273 path_element
= vfs_path_get_by_index (vpath2
, -1);
274 if (vfs_path_element_valid (path_element
))
277 path_element
->class->symlink
!= NULL
?
278 path_element
->class->symlink (vpath1
, vpath2
) : -1;
282 path_element
->class->symlink
!= NULL
?
283 vfs_ferrno (path_element
->class) : E_NOTSUPP
;
289 /* --------------------------------------------------------------------------------------------- */
293 #define MC_HANDLEOP(name, inarg, callarg) \
294 ssize_t mc_##name inarg \
296 struct vfs_class *vfs; \
300 vfs = vfs_class_find_by_handle (handle); \
303 result = vfs->name != NULL ? vfs->name callarg : -1; \
305 errno = vfs->name != NULL ? vfs_ferrno (vfs) : E_NOTSUPP; \
309 MC_HANDLEOP (read
, (int handle
, void *buffer
, size_t count
), (vfs_class_data_find_by_handle (handle
), buffer
, count
))
310 MC_HANDLEOP (write
, (int handle
, const void *buf
, size_t nbyte
), (vfs_class_data_find_by_handle (handle
), buf
, nbyte
))
312 /* --------------------------------------------------------------------------------------------- */
314 #define MC_RENAMEOP(name) \
315 int mc_##name (const vfs_path_t *vpath1, const vfs_path_t *vpath2) \
318 const vfs_path_element_t *path_element1; \
319 const vfs_path_element_t *path_element2; \
321 if (vpath1 == NULL || vpath2 == NULL) \
324 path_element1 = vfs_path_get_by_index (vpath1, - 1); \
325 path_element2 = vfs_path_get_by_index (vpath2, - 1); \
327 if (!vfs_path_element_valid (path_element1) || !vfs_path_element_valid (path_element2) || \
328 path_element1->class != path_element2->class) \
334 result = path_element1->class->name != NULL \
335 ? path_element1->class->name (vpath1, vpath2) \
338 errno = path_element1->class->name != NULL ? vfs_ferrno (path_element1->class) : E_NOTSUPP; \
347 /* --------------------------------------------------------------------------------------------- */
350 mc_ctl (int handle
, int ctlop
, void *arg
)
352 struct vfs_class
*vfs
= vfs_class_find_by_handle (handle
);
357 return vfs
->ctl
? (*vfs
->ctl
) (vfs_class_data_find_by_handle (handle
), ctlop
, arg
) : 0;
360 /* --------------------------------------------------------------------------------------------- */
363 mc_setctl (const vfs_path_t
* vpath
, int ctlop
, void *arg
)
366 const vfs_path_element_t
*path_element
;
369 vfs_die ("You don't want to pass NULL to mc_setctl.");
371 path_element
= vfs_path_get_by_index (vpath
, -1);
372 if (vfs_path_element_valid (path_element
))
374 path_element
->class->setctl
!= NULL
? path_element
->class->setctl (vpath
,
380 /* --------------------------------------------------------------------------------------------- */
383 mc_close (int handle
)
385 struct vfs_class
*vfs
;
388 if (handle
== -1 || !vfs_class_data_find_by_handle (handle
))
391 vfs
= vfs_class_find_by_handle (handle
);
396 return close (handle
);
399 vfs_die ("VFS must support close.\n");
400 result
= (*vfs
->close
) (vfs_class_data_find_by_handle (handle
));
401 vfs_free_handle (handle
);
403 errno
= vfs_ferrno (vfs
);
408 /* --------------------------------------------------------------------------------------------- */
411 mc_opendir (const vfs_path_t
* vpath
)
413 int handle
, *handlep
;
415 vfs_path_element_t
*path_element
;
420 path_element
= (vfs_path_element_t
*) vfs_path_get_by_index (vpath
, -1);
422 if (!vfs_path_element_valid (path_element
))
428 info
= path_element
->class->opendir
? (*path_element
->class->opendir
) (vpath
) : NULL
;
432 errno
= path_element
->class->opendir
? vfs_ferrno (path_element
->class) : E_NOTSUPP
;
436 path_element
->dir
.info
= info
;
438 path_element
->dir
.converter
= (path_element
->encoding
!= NULL
) ?
439 str_crt_conv_from (path_element
->encoding
) : str_cnv_from_term
;
440 if (path_element
->dir
.converter
== INVALID_CONV
)
441 path_element
->dir
.converter
= str_cnv_from_term
;
443 handle
= vfs_new_handle (path_element
->class, vfs_path_element_clone (path_element
));
445 handlep
= g_new (int, 1);
447 return (DIR *) handlep
;
450 /* --------------------------------------------------------------------------------------------- */
453 mc_readdir (DIR * dirp
)
456 struct vfs_class
*vfs
;
457 struct dirent
*entry
= NULL
;
458 vfs_path_element_t
*vfs_path_element
;
461 if (!mc_readdir_result
)
463 /* We can't just allocate struct dirent as (see man dirent.h)
464 * struct dirent has VERY nonnaive semantics of allocating
465 * d_name in it. Moreover, linux's glibc-2.9 allocates dirents _less_,
466 * than 'sizeof (struct dirent)' making full bitwise (sizeof dirent) copy
467 * heap corrupter. So, allocate longliving dirent with at least
468 * (MAXNAMLEN + 1) for d_name in it.
469 * Strictly saying resulting dirent is unusable as we don't adjust internal
470 * structures, holding dirent size. But we don't use it in libc infrastructure.
471 * TODO: to make simpler homemade dirent-alike structure.
473 mc_readdir_result
= (struct dirent
*) g_malloc (sizeof (struct dirent
) + MAXNAMLEN
+ 1);
481 handle
= *(int *) dirp
;
483 vfs
= vfs_class_find_by_handle (handle
);
487 vfs_path_element
= vfs_class_data_find_by_handle (handle
);
490 entry
= (*vfs
->readdir
) (vfs_path_element
->dir
.info
);
493 g_string_set_size (vfs_str_buffer
, 0);
495 str_vfs_convert_from (vfs_path_element
->dir
.converter
, entry
->d_name
, vfs_str_buffer
);
496 mc_readdir_result
->d_ino
= entry
->d_ino
;
497 g_strlcpy (mc_readdir_result
->d_name
, vfs_str_buffer
->str
, MAXNAMLEN
+ 1);
500 errno
= vfs
->readdir
? vfs_ferrno (vfs
) : E_NOTSUPP
;
501 return (entry
!= NULL
) ? mc_readdir_result
: NULL
;
504 /* --------------------------------------------------------------------------------------------- */
507 mc_closedir (DIR * dirp
)
509 int handle
= *(int *) dirp
;
510 struct vfs_class
*vfs
;
513 vfs
= vfs_class_find_by_handle (handle
);
516 vfs_path_element_t
*vfs_path_element
;
517 vfs_path_element
= vfs_class_data_find_by_handle (handle
);
518 if (vfs_path_element
->dir
.converter
!= str_cnv_from_term
)
520 str_close_conv (vfs_path_element
->dir
.converter
);
521 vfs_path_element
->dir
.converter
= INVALID_CONV
;
524 result
= vfs
->closedir
? (*vfs
->closedir
) (vfs_path_element
->dir
.info
) : -1;
525 vfs_free_handle (handle
);
526 vfs_path_element_free (vfs_path_element
);
532 /* --------------------------------------------------------------------------------------------- */
535 mc_stat (const vfs_path_t
* vpath
, struct stat
*buf
)
538 const vfs_path_element_t
*path_element
;
543 path_element
= vfs_path_get_by_index (vpath
, -1);
545 if (vfs_path_element_valid (path_element
))
547 result
= path_element
->class->stat
? (*path_element
->class->stat
) (vpath
, buf
) : -1;
549 errno
= path_element
->class->name
? vfs_ferrno (path_element
->class) : E_NOTSUPP
;
555 /* --------------------------------------------------------------------------------------------- */
558 mc_lstat (const vfs_path_t
* vpath
, struct stat
*buf
)
561 const vfs_path_element_t
*path_element
;
566 path_element
= vfs_path_get_by_index (vpath
, -1);
568 if (vfs_path_element_valid (path_element
))
570 result
= path_element
->class->lstat
? (*path_element
->class->lstat
) (vpath
, buf
) : -1;
572 errno
= path_element
->class->name
? vfs_ferrno (path_element
->class) : E_NOTSUPP
;
578 /* --------------------------------------------------------------------------------------------- */
581 mc_fstat (int handle
, struct stat
*buf
)
583 struct vfs_class
*vfs
;
589 vfs
= vfs_class_find_by_handle (handle
);
593 result
= vfs
->fstat
? (*vfs
->fstat
) (vfs_class_data_find_by_handle (handle
), buf
) : -1;
595 errno
= vfs
->name
? vfs_ferrno (vfs
) : E_NOTSUPP
;
599 /* --------------------------------------------------------------------------------------------- */
602 mc_getlocalcopy (const vfs_path_t
* pathname_vpath
)
604 vfs_path_t
*result
= NULL
;
605 const vfs_path_element_t
*path_element
;
607 if (pathname_vpath
== NULL
)
610 path_element
= vfs_path_get_by_index (pathname_vpath
, -1);
612 if (vfs_path_element_valid (path_element
))
614 result
= path_element
->class->getlocalcopy
!= NULL
?
615 path_element
->class->getlocalcopy (pathname_vpath
) :
616 mc_def_getlocalcopy (pathname_vpath
);
618 errno
= vfs_ferrno (path_element
->class);
623 /* --------------------------------------------------------------------------------------------- */
626 mc_ungetlocalcopy (const vfs_path_t
* pathname_vpath
, const vfs_path_t
* local_vpath
,
627 gboolean has_changed
)
629 int return_value
= -1;
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
))
638 return_value
= path_element
->class->ungetlocalcopy
!= NULL
?
639 path_element
->class->ungetlocalcopy (pathname_vpath
, local_vpath
, has_changed
) :
640 mc_def_ungetlocalcopy (pathname_vpath
, local_vpath
, has_changed
);
645 /* --------------------------------------------------------------------------------------------- */
649 * @param vpath VFS-path
651 * @return 0 on success, -1 on failure.
655 mc_chdir (const vfs_path_t
* vpath
)
657 struct vfs_class
*old_vfs
;
660 const vfs_path_element_t
*path_element
;
661 vfs_path_t
*abcolute_vpath
;
666 path_element
= vfs_path_get_by_index (vpath
, -1);
668 if (!vfs_path_element_valid (path_element
) || path_element
->class->chdir
== NULL
)
673 abcolute_vpath
= vfs_path_to_absolute (vpath
);
675 result
= (*path_element
->class->chdir
) (abcolute_vpath
);
679 vfs_path_free (abcolute_vpath
);
680 errno
= vfs_ferrno (path_element
->class);
684 old_vfsid
= vfs_getid (vfs_get_raw_current_dir ());
685 old_vfs
= current_vfs
;
687 /* Actually change directory */
688 vfs_set_raw_current_dir (abcolute_vpath
);
690 current_vfs
= path_element
->class;
692 /* This function uses the new current_dir implicitly */
693 vfs_stamp_create (old_vfs
, old_vfsid
);
695 /* Sometimes we assume no trailing slash on cwd */
696 path_element
= vfs_path_get_by_index (vfs_get_raw_current_dir (), -1);
697 if (vfs_path_element_valid (path_element
) && (*path_element
->path
!= '\0'))
700 p
= strchr (path_element
->path
, 0) - 1;
701 if (*p
== PATH_SEP
&& p
> path_element
->path
)
707 /* --------------------------------------------------------------------------------------------- */
710 mc_lseek (int fd
, off_t offset
, int whence
)
712 struct vfs_class
*vfs
;
718 vfs
= vfs_class_find_by_handle (fd
);
722 result
= vfs
->lseek
? (*vfs
->lseek
) (vfs_class_data_find_by_handle (fd
), offset
, whence
) : -1;
724 errno
= vfs
->lseek
? vfs_ferrno (vfs
) : E_NOTSUPP
;
728 /* --------------------------------------------------------------------------------------------- */
729 /* Following code heavily borrows from libiberty, mkstemps.c */
732 * pname (output) - pointer to the name of the temp file (needs g_free).
733 * NULL if the function fails.
734 * prefix - part of the filename before the random part.
735 * Prepend $TMPDIR or /tmp if there are no path separators.
736 * suffix - if not NULL, part of the filename after the random part.
739 * handle of the open file or -1 if couldn't open any.
743 mc_mkstemps (vfs_path_t
** pname_vpath
, const char *prefix
, const char *suffix
)
745 static const char letters
[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
746 static unsigned long value
;
754 if (strchr (prefix
, PATH_SEP
) == NULL
)
756 /* Add prefix first to find the position of XXXXXX */
757 tmpbase
= g_build_filename (mc_tmpdir (), prefix
, NULL
);
761 tmpbase
= g_strdup (prefix
);
764 tmpname
= g_strconcat (tmpbase
, "XXXXXX", suffix
, (char *) NULL
);
766 XXXXXX
= &tmpname
[strlen (tmpbase
)];
769 /* Get some more or less random data. */
770 gettimeofday (&tv
, NULL
);
771 value
+= (tv
.tv_usec
<< 16) ^ tv
.tv_sec
^ getpid ();
773 for (count
= 0; count
< TMP_MAX
; ++count
)
775 unsigned long v
= value
;
778 /* Fill in the random bits. */
779 XXXXXX
[0] = letters
[v
% 62];
781 XXXXXX
[1] = letters
[v
% 62];
783 XXXXXX
[2] = letters
[v
% 62];
785 XXXXXX
[3] = letters
[v
% 62];
787 XXXXXX
[4] = letters
[v
% 62];
789 XXXXXX
[5] = letters
[v
% 62];
791 fd
= open (tmpname
, O_RDWR
| O_CREAT
| O_TRUNC
| O_EXCL
, S_IRUSR
| S_IWUSR
);
794 /* Successfully created. */
795 *pname_vpath
= vfs_path_from_str (ret_path
);
800 /* This is a random value. It is only necessary that the next
801 TMP_MAX values generated by adding 7777 to VALUE are different
802 with (module 2^32). */
806 /* Unsuccessful. Free the filename. */
813 /* --------------------------------------------------------------------------------------------- */
815 * Return the directory where mc should keep its temporary files.
816 * This directory is (in Bourne shell terms) "${TMPDIR=/tmp}/mc-$USER"
817 * When called the first time, the directory is created if needed.
818 * The first call should be done early, since we are using fprintf()
819 * and not message() to report possible problems.
825 static char buffer
[64];
826 static const char *tmpdir
;
830 const char *error
= NULL
;
832 /* Check if already correctly initialized */
833 if (tmpdir
&& lstat (tmpdir
, &st
) == 0 && S_ISDIR (st
.st_mode
) &&
834 st
.st_uid
== getuid () && (st
.st_mode
& 0777) == 0700)
837 sys_tmp
= getenv ("TMPDIR");
838 if (!sys_tmp
|| sys_tmp
[0] != '/')
840 sys_tmp
= TMPDIR_DEFAULT
;
843 pwd
= getpwuid (getuid ());
846 g_snprintf (buffer
, sizeof (buffer
), "%s/mc-%s", sys_tmp
, pwd
->pw_name
);
848 g_snprintf (buffer
, sizeof (buffer
), "%s/mc-%lu", sys_tmp
, (unsigned long) getuid ());
850 canonicalize_pathname (buffer
);
852 if (lstat (buffer
, &st
) == 0)
854 /* Sanity check for existing directory */
855 if (!S_ISDIR (st
.st_mode
))
856 error
= _("%s is not a directory\n");
857 else if (st
.st_uid
!= getuid ())
858 error
= _("Directory %s is not owned by you\n");
859 else if (((st
.st_mode
& 0777) != 0700) && (chmod (buffer
, 0700) != 0))
860 error
= _("Cannot set correct permissions for directory %s\n");
864 /* Need to create directory */
865 if (mkdir (buffer
, S_IRWXU
) != 0)
868 _("Cannot create temporary directory %s: %s\n"),
869 buffer
, unix_error_string (errno
));
877 char *fallback_prefix
;
878 gboolean fallback_ok
= FALSE
;
879 vfs_path_t
*test_vpath
;
882 fprintf (stderr
, error
, buffer
);
884 /* Test if sys_tmp is suitable for temporary files */
885 fallback_prefix
= g_strdup_printf ("%s/mctest", sys_tmp
);
886 test_fd
= mc_mkstemps (&test_vpath
, fallback_prefix
, NULL
);
887 g_free (fallback_prefix
);
892 test_fn
= vfs_path_to_str (test_vpath
);
894 test_fd
= open (test_fn
, O_RDONLY
);
906 fprintf (stderr
, _("Temporary files will be created in %s\n"), sys_tmp
);
907 g_snprintf (buffer
, sizeof (buffer
), "%s", sys_tmp
);
912 fprintf (stderr
, _("Temporary files will not be created\n"));
913 g_snprintf (buffer
, sizeof (buffer
), "%s", "/dev/null/");
916 vfs_path_free (test_vpath
);
917 fprintf (stderr
, "%s\n", _("Press any key to continue..."));
924 g_setenv ("MC_TMPDIR", tmpdir
, TRUE
);
929 /* --------------------------------------------------------------------------------------------- */