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 char *filename
)
82 int fdin
= -1, fdout
= -1;
84 char buffer
[BUF_1K
* 8];
88 vpath
= vfs_path_from_str (filename
);
89 fdin
= mc_open (vpath
, O_RDONLY
| O_LINEAR
);
93 fdout
= vfs_mkstemps (&tmp
, "vfs", filename
);
97 while ((i
= mc_read (fdin
, buffer
, sizeof (buffer
))) > 0)
99 if (write (fdout
, buffer
, i
) != i
)
108 if (close (fdout
) == -1)
114 if (mc_stat (vpath
, &mystat
) != -1)
115 chmod (tmp
, mystat
.st_mode
);
116 vfs_path_free (vpath
);
121 vfs_path_free (vpath
);
130 /* --------------------------------------------------------------------------------------------- */
133 mc_def_ungetlocalcopy (struct vfs_class
*vfs
, const char *filename
,
134 const char *local
, int has_changed
)
137 int fdin
= -1, fdout
= -1;
139 vpath
= vfs_path_from_str (filename
);
143 char buffer
[BUF_1K
* 8];
149 fdin
= open (local
, O_RDONLY
);
152 fdout
= mc_open (vpath
, O_WRONLY
| O_TRUNC
);
155 while ((i
= read (fdin
, buffer
, sizeof (buffer
))) > 0)
156 if (mc_write (fdout
, buffer
, (size_t) i
) != i
)
161 if (close (fdin
) == -1)
167 if (mc_close (fdout
) == -1)
174 vfs_path_free (vpath
);
178 message (D_ERROR
, _("Changes to file lost"), "%s", filename
);
184 vfs_path_free (vpath
);
188 /* --------------------------------------------------------------------------------------------- */
189 /*** public functions ****************************************************************************/
190 /* --------------------------------------------------------------------------------------------- */
193 mc_open (const vfs_path_t
* vpath
, int flags
, ...)
195 int mode
= 0, result
= -1;
196 vfs_path_element_t
*path_element
;
201 /* Get the mode flag */
205 va_start (ap
, flags
);
206 mode
= va_arg (ap
, int);
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 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
, struct utimbuf
* 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 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, inarg, callarg) \
298 ssize_t mc_##name inarg \
300 struct vfs_class *vfs; \
304 vfs = vfs_class_find_by_handle (handle); \
307 result = vfs->name != NULL ? vfs->name callarg : -1; \
309 errno = vfs->name != NULL ? vfs_ferrno (vfs) : E_NOTSUPP; \
313 MC_HANDLEOP (read
, (int handle
, void *buffer
, size_t count
), (vfs_class_data_find_by_handle (handle
), buffer
, count
))
314 MC_HANDLEOP (write
, (int handle
, const void *buf
, size_t nbyte
), (vfs_class_data_find_by_handle (handle
), buf
, nbyte
))
316 /* --------------------------------------------------------------------------------------------- */
318 #define MC_RENAMEOP(name) \
319 int mc_##name (const vfs_path_t *vpath1, const vfs_path_t *vpath2) \
322 vfs_path_element_t *path_element1, *path_element2; \
324 if (vpath1 == NULL || vpath2 == NULL) \
327 path_element1 = vfs_path_get_by_index (vpath1, - 1); \
328 path_element2 = vfs_path_get_by_index (vpath2, - 1); \
330 if (!vfs_path_element_valid (path_element1) || !vfs_path_element_valid (path_element2) || \
331 path_element1->class != path_element2->class) \
337 result = path_element1->class->name != NULL \
338 ? path_element1->class->name (vpath1, vpath2) \
341 errno = path_element1->class->name != NULL ? vfs_ferrno (path_element1->class) : E_NOTSUPP; \
350 /* --------------------------------------------------------------------------------------------- */
353 mc_ctl (int handle
, int ctlop
, void *arg
)
355 struct vfs_class
*vfs
= vfs_class_find_by_handle (handle
);
360 return vfs
->ctl
? (*vfs
->ctl
) (vfs_class_data_find_by_handle (handle
), ctlop
, arg
) : 0;
363 /* --------------------------------------------------------------------------------------------- */
366 mc_setctl (const vfs_path_t
* vpath
, int ctlop
, void *arg
)
370 vfs_path_element_t
*path_element
;
373 vfs_die ("You don't want to pass NULL to mc_setctl.");
375 path_element
= vfs_path_get_by_index (vpath
, -1);
376 if (vfs_path_element_valid (path_element
))
378 path_element
->class->setctl
!= NULL
? path_element
->class->setctl (vpath
,
384 /* --------------------------------------------------------------------------------------------- */
387 mc_close (int handle
)
389 struct vfs_class
*vfs
;
392 if (handle
== -1 || !vfs_class_data_find_by_handle (handle
))
395 vfs
= vfs_class_find_by_handle (handle
);
400 return close (handle
);
403 vfs_die ("VFS must support close.\n");
404 result
= (*vfs
->close
) (vfs_class_data_find_by_handle (handle
));
405 vfs_free_handle (handle
);
407 errno
= vfs_ferrno (vfs
);
412 /* --------------------------------------------------------------------------------------------- */
415 mc_opendir (const vfs_path_t
* vpath
)
417 int handle
, *handlep
;
419 vfs_path_element_t
*path_element
;
424 path_element
= vfs_path_get_by_index (vpath
, -1);
426 if (!vfs_path_element_valid (path_element
))
432 info
= path_element
->class->opendir
? (*path_element
->class->opendir
) (vpath
) : NULL
;
436 errno
= path_element
->class->opendir
? vfs_ferrno (path_element
->class) : E_NOTSUPP
;
440 path_element
->dir
.info
= info
;
442 path_element
->dir
.converter
=
443 (path_element
->encoding
!=
444 NULL
) ? str_crt_conv_from (path_element
->encoding
) : str_cnv_from_term
;
445 if (path_element
->dir
.converter
== INVALID_CONV
)
446 path_element
->dir
.converter
= str_cnv_from_term
;
448 handle
= vfs_new_handle (path_element
->class, vfs_path_element_clone (path_element
));
450 handlep
= g_new (int, 1);
452 return (DIR *) handlep
;
455 /* --------------------------------------------------------------------------------------------- */
458 mc_readdir (DIR * dirp
)
461 struct vfs_class
*vfs
;
462 struct dirent
*entry
= NULL
;
463 vfs_path_element_t
*vfs_path_element
;
466 if (!mc_readdir_result
)
468 /* We can't just allocate struct dirent as (see man dirent.h)
469 * struct dirent has VERY nonnaive semantics of allocating
470 * d_name in it. Moreover, linux's glibc-2.9 allocates dirents _less_,
471 * than 'sizeof (struct dirent)' making full bitwise (sizeof dirent) copy
472 * heap corrupter. So, allocate longliving dirent with at least
473 * (MAXNAMLEN + 1) for d_name in it.
474 * Strictly saying resulting dirent is unusable as we don't adjust internal
475 * structures, holding dirent size. But we don't use it in libc infrastructure.
476 * TODO: to make simpler homemade dirent-alike structure.
478 mc_readdir_result
= (struct dirent
*) g_malloc (sizeof (struct dirent
) + MAXNAMLEN
+ 1);
486 handle
= *(int *) dirp
;
488 vfs
= vfs_class_find_by_handle (handle
);
492 vfs_path_element
= vfs_class_data_find_by_handle (handle
);
495 entry
= (*vfs
->readdir
) (vfs_path_element
->dir
.info
);
498 g_string_set_size (vfs_str_buffer
, 0);
500 str_vfs_convert_from (vfs_path_element
->dir
.converter
, entry
->d_name
, vfs_str_buffer
);
501 mc_readdir_result
->d_ino
= entry
->d_ino
;
502 g_strlcpy (mc_readdir_result
->d_name
, vfs_str_buffer
->str
, MAXNAMLEN
+ 1);
505 errno
= vfs
->readdir
? vfs_ferrno (vfs
) : E_NOTSUPP
;
506 return (entry
!= NULL
) ? mc_readdir_result
: NULL
;
509 /* --------------------------------------------------------------------------------------------- */
512 mc_closedir (DIR * dirp
)
514 int handle
= *(int *) dirp
;
515 struct vfs_class
*vfs
;
518 vfs
= vfs_class_find_by_handle (handle
);
521 vfs_path_element_t
*vfs_path_element
;
522 vfs_path_element
= vfs_class_data_find_by_handle (handle
);
523 if (vfs_path_element
->dir
.converter
!= str_cnv_from_term
)
525 str_close_conv (vfs_path_element
->dir
.converter
);
526 vfs_path_element
->dir
.converter
= INVALID_CONV
;
529 result
= vfs
->closedir
? (*vfs
->closedir
) (vfs_path_element
->dir
.info
) : -1;
530 vfs_free_handle (handle
);
531 vfs_path_element_free (vfs_path_element
);
537 /* --------------------------------------------------------------------------------------------- */
540 mc_stat (const vfs_path_t
* vpath
, struct stat
*buf
)
543 vfs_path_element_t
*path_element
;
548 path_element
= vfs_path_get_by_index (vpath
, -1);
550 if (vfs_path_element_valid (path_element
))
552 result
= path_element
->class->stat
? (*path_element
->class->stat
) (vpath
, buf
) : -1;
554 errno
= path_element
->class->name
? vfs_ferrno (path_element
->class) : E_NOTSUPP
;
560 /* --------------------------------------------------------------------------------------------- */
563 mc_lstat (const vfs_path_t
* vpath
, struct stat
*buf
)
566 vfs_path_element_t
*path_element
;
571 path_element
= vfs_path_get_by_index (vpath
, -1);
573 if (vfs_path_element_valid (path_element
))
575 result
= path_element
->class->lstat
? (*path_element
->class->lstat
) (vpath
, buf
) : -1;
577 errno
= path_element
->class->name
? vfs_ferrno (path_element
->class) : E_NOTSUPP
;
583 /* --------------------------------------------------------------------------------------------- */
586 mc_fstat (int handle
, struct stat
*buf
)
588 struct vfs_class
*vfs
;
594 vfs
= vfs_class_find_by_handle (handle
);
598 result
= vfs
->fstat
? (*vfs
->fstat
) (vfs_class_data_find_by_handle (handle
), buf
) : -1;
600 errno
= vfs
->name
? vfs_ferrno (vfs
) : E_NOTSUPP
;
604 /* --------------------------------------------------------------------------------------------- */
606 * Return current directory. If it's local, reread the current directory
607 * from the OS. Put directory to the provided buffer.
611 mc_get_current_wd (char *buffer
, size_t size
)
613 char *cwd
= _vfs_get_cwd ();
615 g_strlcpy (buffer
, cwd
, size
);
621 /* --------------------------------------------------------------------------------------------- */
624 mc_getlocalcopy (const char *pathname
)
628 vfs_path_element_t
*path_element
;
630 vpath
= vfs_path_from_str (pathname
);
634 path_element
= vfs_path_get_by_index (vpath
, -1);
636 if (vfs_path_element_valid (path_element
))
638 result
= path_element
->class->getlocalcopy
!= NULL
?
639 path_element
->class->getlocalcopy (vpath
) : mc_def_getlocalcopy (pathname
);
641 errno
= vfs_ferrno (path_element
->class);
643 vfs_path_free (vpath
);
647 /* --------------------------------------------------------------------------------------------- */
650 mc_ungetlocalcopy (const char *pathname
, const char *local
, int has_changed
)
652 int return_value
= -1;
654 vfs_path_element_t
*path_element
;
656 vpath
= vfs_path_from_str (pathname
);
660 path_element
= vfs_path_get_by_index (vpath
, -1);
662 if (vfs_path_element_valid (path_element
))
664 return_value
= path_element
->class->ungetlocalcopy
!= NULL
?
665 path_element
->class->ungetlocalcopy (vpath
, local
,
667 mc_def_ungetlocalcopy (path_element
->class, path_element
->path
, local
, has_changed
);
669 vfs_path_free (vpath
);
673 /* --------------------------------------------------------------------------------------------- */
677 * @param vpath VFS-path
679 * @return 0 on success, -1 on failure.
683 mc_chdir (const vfs_path_t
* vpath
)
685 struct vfs_class
*old_vfs
;
688 vfs_path_element_t
*path_element
;
693 path_element
= vfs_path_get_by_index (vpath
, -1);
695 if (!vfs_path_element_valid (path_element
) || path_element
->class->chdir
== NULL
)
700 result
= (*path_element
->class->chdir
) (vpath
);
704 errno
= vfs_ferrno (path_element
->class);
708 old_vfsid
= vfs_getid (vfs_get_raw_current_dir ());
709 old_vfs
= current_vfs
;
711 /* Actually change directory */
712 vfs_set_raw_current_dir (vfs_path_clone (vpath
));
713 current_vfs
= path_element
->class;
715 /* This function uses the new current_dir implicitly */
716 vfs_stamp_create (old_vfs
, old_vfsid
);
718 /* Sometimes we assume no trailing slash on cwd */
719 path_element
= vfs_path_get_by_index (vfs_get_raw_current_dir (), -1);
720 if (vfs_path_element_valid (path_element
) && (*path_element
->path
!= '\0'))
723 p
= strchr (path_element
->path
, 0) - 1;
724 if (*p
== PATH_SEP
&& p
> path_element
->path
)
730 /* --------------------------------------------------------------------------------------------- */
733 mc_lseek (int fd
, off_t offset
, int whence
)
735 struct vfs_class
*vfs
;
741 vfs
= vfs_class_find_by_handle (fd
);
745 result
= vfs
->lseek
? (*vfs
->lseek
) (vfs_class_data_find_by_handle (fd
), offset
, whence
) : -1;
747 errno
= vfs
->lseek
? vfs_ferrno (vfs
) : E_NOTSUPP
;
751 /* --------------------------------------------------------------------------------------------- */
752 /* Following code heavily borrows from libiberty, mkstemps.c */
755 * pname (output) - pointer to the name of the temp file (needs g_free).
756 * NULL if the function fails.
757 * prefix - part of the filename before the random part.
758 * Prepend $TMPDIR or /tmp if there are no path separators.
759 * suffix - if not NULL, part of the filename after the random part.
762 * handle of the open file or -1 if couldn't open any.
766 mc_mkstemps (char **pname
, const char *prefix
, const char *suffix
)
768 static const char letters
[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
769 static unsigned long value
;
776 if (strchr (prefix
, PATH_SEP
) == NULL
)
778 /* Add prefix first to find the position of XXXXXX */
779 tmpbase
= g_build_filename (mc_tmpdir (), prefix
, NULL
);
783 tmpbase
= g_strdup (prefix
);
786 tmpname
= g_strconcat (tmpbase
, "XXXXXX", suffix
, (char *) NULL
);
788 XXXXXX
= &tmpname
[strlen (tmpbase
)];
791 /* Get some more or less random data. */
792 gettimeofday (&tv
, NULL
);
793 value
+= (tv
.tv_usec
<< 16) ^ tv
.tv_sec
^ getpid ();
795 for (count
= 0; count
< TMP_MAX
; ++count
)
797 unsigned long v
= value
;
800 /* Fill in the random bits. */
801 XXXXXX
[0] = letters
[v
% 62];
803 XXXXXX
[1] = letters
[v
% 62];
805 XXXXXX
[2] = letters
[v
% 62];
807 XXXXXX
[3] = letters
[v
% 62];
809 XXXXXX
[4] = letters
[v
% 62];
811 XXXXXX
[5] = letters
[v
% 62];
813 fd
= open (tmpname
, O_RDWR
| O_CREAT
| O_TRUNC
| O_EXCL
, S_IRUSR
| S_IWUSR
);
816 /* Successfully created. */
820 /* This is a random value. It is only necessary that the next
821 TMP_MAX values generated by adding 7777 to VALUE are different
822 with (module 2^32). */
826 /* Unsuccessful. Free the filename. */
833 /* --------------------------------------------------------------------------------------------- */
835 * Return the directory where mc should keep its temporary files.
836 * This directory is (in Bourne shell terms) "${TMPDIR=/tmp}/mc-$USER"
837 * When called the first time, the directory is created if needed.
838 * The first call should be done early, since we are using fprintf()
839 * and not message() to report possible problems.
845 static char buffer
[64];
846 static const char *tmpdir
= NULL
;
850 const char *error
= NULL
;
852 /* Check if already correctly initialized */
853 if (tmpdir
&& lstat (tmpdir
, &st
) == 0 && S_ISDIR (st
.st_mode
) &&
854 st
.st_uid
== getuid () && (st
.st_mode
& 0777) == 0700)
857 sys_tmp
= getenv ("TMPDIR");
858 if (!sys_tmp
|| sys_tmp
[0] != '/')
860 sys_tmp
= TMPDIR_DEFAULT
;
863 pwd
= getpwuid (getuid ());
866 g_snprintf (buffer
, sizeof (buffer
), "%s/mc-%s", sys_tmp
, pwd
->pw_name
);
868 g_snprintf (buffer
, sizeof (buffer
), "%s/mc-%lu", sys_tmp
, (unsigned long) getuid ());
870 canonicalize_pathname (buffer
);
872 if (lstat (buffer
, &st
) == 0)
874 /* Sanity check for existing directory */
875 if (!S_ISDIR (st
.st_mode
))
876 error
= _("%s is not a directory\n");
877 else if (st
.st_uid
!= getuid ())
878 error
= _("Directory %s is not owned by you\n");
879 else if (((st
.st_mode
& 0777) != 0700) && (chmod (buffer
, 0700) != 0))
880 error
= _("Cannot set correct permissions for directory %s\n");
884 /* Need to create directory */
885 if (mkdir (buffer
, S_IRWXU
) != 0)
888 _("Cannot create temporary directory %s: %s\n"),
889 buffer
, unix_error_string (errno
));
897 char *test_fn
, *fallback_prefix
;
901 fprintf (stderr
, error
, buffer
);
903 /* Test if sys_tmp is suitable for temporary files */
904 fallback_prefix
= g_strdup_printf ("%s/mctest", sys_tmp
);
905 test_fd
= mc_mkstemps (&test_fn
, fallback_prefix
, NULL
);
906 g_free (fallback_prefix
);
910 test_fd
= open (test_fn
, O_RDONLY
);
921 fprintf (stderr
, _("Temporary files will be created in %s\n"), sys_tmp
);
922 g_snprintf (buffer
, sizeof (buffer
), "%s", sys_tmp
);
927 fprintf (stderr
, _("Temporary files will not be created\n"));
928 g_snprintf (buffer
, sizeof (buffer
), "%s", "/dev/null/");
931 fprintf (stderr
, "%s\n", _("Press any key to continue..."));
938 g_setenv ("MC_TMPDIR", tmpdir
, TRUE
);
943 /* --------------------------------------------------------------------------------------------- */