1 /* Virtual File System: interface functions
2 Copyright (C) 2011 Free Software Foundation, Inc.
5 Slava Zanko <slavazanko@gmail.com>, 2011
7 This file is part of the Midnight Commander.
9 The Midnight Commander is free software; you can redistribute it
10 and/or modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation; either version 2 of the
12 License, or (at your option) any later version.
14 The Midnight Commander is distributed in the hope that it will be
15 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
16 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
27 * \brief Source: Virtual File System: path handlers
36 #include <stdlib.h> /* For atol() */
40 #include <sys/types.h>
42 #include <ctype.h> /* is_digit() */
48 #include "lib/global.h"
50 #include "lib/widget.h" /* message() */
51 #include "lib/strutil.h" /* str_crt_conv_from() */
58 extern GString
*vfs_str_buffer
;
59 extern struct vfs_class
*current_vfs
;
61 /*** global variables ****************************************************************************/
63 struct dirent
*mc_readdir_result
= NULL
;
65 /*** file scope macro definitions ****************************************************************/
67 /*** file scope type declarations ****************************************************************/
69 /*** file scope variables ************************************************************************/
71 /*** file scope functions ************************************************************************/
72 /* --------------------------------------------------------------------------------------------- */
75 mc_def_getlocalcopy (const char *filename
)
83 fdin
= mc_open (filename
, O_RDONLY
| O_LINEAR
);
87 fdout
= vfs_mkstemps (&tmp
, "vfs", filename
);
91 while ((i
= mc_read (fdin
, buffer
, sizeof (buffer
))) > 0)
93 if (write (fdout
, buffer
, i
) != i
)
102 if (close (fdout
) == -1)
108 if (mc_stat (filename
, &mystat
) != -1)
109 chmod (tmp
, mystat
.st_mode
);
122 /* --------------------------------------------------------------------------------------------- */
125 mc_def_ungetlocalcopy (struct vfs_class
*vfs
, const char *filename
,
126 const char *local
, int has_changed
)
128 int fdin
= -1, fdout
= -1;
137 fdin
= open (local
, O_RDONLY
);
140 fdout
= mc_open (filename
, O_WRONLY
| O_TRUNC
);
143 while ((i
= read (fdin
, buffer
, sizeof (buffer
))) > 0)
144 if (mc_write (fdout
, buffer
, (size_t) i
) != i
)
149 if (close (fdin
) == -1)
155 if (mc_close (fdout
) == -1)
165 message (D_ERROR
, _("Changes to file lost"), "%s", filename
);
174 /* --------------------------------------------------------------------------------------------- */
175 /*** public functions ****************************************************************************/
176 /* --------------------------------------------------------------------------------------------- */
179 mc_open (const char *filename
, int flags
, ...)
181 int mode
= 0, result
= -1;
183 vfs_path_element_t
*path_element
;
185 vpath
= vfs_path_from_str (filename
);
189 /* Get the mode flag */
193 va_start (ap
, flags
);
194 mode
= va_arg (ap
, int);
198 path_element
= vfs_path_get_by_index (vpath
, -1);
199 if (vfs_path_element_valid (path_element
) && path_element
->class->open
!= NULL
)
202 /* open must be supported */
203 info
= path_element
->class->open (vpath
, flags
, mode
);
205 errno
= vfs_ferrno (path_element
->class);
207 result
= vfs_new_handle (path_element
->class, info
);
212 vfs_path_free (vpath
);
216 /* --------------------------------------------------------------------------------------------- */
220 #define MC_NAMEOP(name, inarg, callarg) \
221 int mc_##name inarg \
225 vfs_path_element_t *path_element; \
227 vpath = vfs_path_from_str (path); \
231 path_element = vfs_path_get_by_index (vpath, -1); \
232 if (!vfs_path_element_valid (path_element)) \
234 vfs_path_free(vpath); \
238 result = path_element->class->name != NULL ? path_element->class->name callarg : -1; \
240 errno = path_element->class->name != NULL ? vfs_ferrno (path_element->class) : E_NOTSUPP; \
241 vfs_path_free(vpath); \
245 MC_NAMEOP (chmod
, (const char *path
, mode_t mode
), (vpath
, mode
))
246 MC_NAMEOP (chown
, (const char *path
, uid_t owner
, gid_t group
), (vpath
, owner
, group
))
247 MC_NAMEOP (utime
, (const char *path
, struct utimbuf
* times
), (vpath
, times
))
248 MC_NAMEOP (readlink
, (const char *path
, char *buf
, size_t bufsiz
), (vpath
, buf
, bufsiz
))
249 MC_NAMEOP (unlink
, (const char *path
), (vpath
))
250 MC_NAMEOP (mkdir
, (const char *path
, mode_t mode
), (vpath
, mode
))
251 MC_NAMEOP (rmdir
, (const char *path
), (vpath
))
252 MC_NAMEOP (mknod
, (const char *path
, mode_t mode
, dev_t dev
), (vpath
, mode
, dev
))
256 /* --------------------------------------------------------------------------------------------- */
259 mc_symlink (const char *name1
, const char *path
)
262 vfs_path_t
*vpath1
, *vpath2
;
264 vpath1
= vfs_path_from_str (path
);
268 vpath2
= vfs_path_from_str (name1
);
272 vfs_path_element_t
*path_element
= vfs_path_get_by_index (vpath1
, -1);
273 if (vfs_path_element_valid (path_element
))
276 path_element
->class->symlink
!=
277 NULL
? path_element
->class->symlink (vpath2
, vpath1
) : -1;
281 path_element
->class->symlink
!=
282 NULL
? vfs_ferrno (path_element
->class) : E_NOTSUPP
;
285 vfs_path_free (vpath1
);
286 vfs_path_free (vpath2
);
290 /* --------------------------------------------------------------------------------------------- */
294 #define MC_HANDLEOP(name, inarg, callarg) \
295 ssize_t mc_##name inarg \
297 struct vfs_class *vfs; \
301 vfs = vfs_class_find_by_handle (handle); \
304 result = vfs->name != NULL ? vfs->name callarg : -1; \
306 errno = vfs->name != NULL ? vfs_ferrno (vfs) : E_NOTSUPP; \
310 MC_HANDLEOP (read
, (int handle
, void *buffer
, size_t count
), (vfs_class_data_find_by_handle (handle
), buffer
, count
))
311 MC_HANDLEOP (write
, (int handle
, const void *buf
, size_t nbyte
), (vfs_class_data_find_by_handle (handle
), buf
, nbyte
))
313 /* --------------------------------------------------------------------------------------------- */
315 #define MC_RENAMEOP(name) \
316 int mc_##name (const char *fname1, const char *fname2) \
319 vfs_path_t *vpath1, *vpath2; \
320 vfs_path_element_t *path_element1, *path_element2; \
322 vpath1 = vfs_path_from_str (fname1); \
323 if (vpath1 == NULL) \
326 vpath2 = vfs_path_from_str (fname2); \
327 if (vpath2 == NULL) \
329 vfs_path_free(vpath1); \
332 path_element1 = vfs_path_get_by_index (vpath1, - 1); \
333 path_element2 = vfs_path_get_by_index (vpath2, - 1); \
335 if (!vfs_path_element_valid (path_element1) || !vfs_path_element_valid (path_element2) || \
336 path_element1->class != path_element2->class) \
339 vfs_path_free(vpath1); \
340 vfs_path_free(vpath2); \
344 result = path_element1->class->name != NULL \
345 ? path_element1->class->name (vpath1, vpath2) \
348 errno = path_element1->class->name != NULL ? vfs_ferrno (path_element1->class) : E_NOTSUPP; \
349 vfs_path_free(vpath1); \
350 vfs_path_free(vpath2); \
359 /* --------------------------------------------------------------------------------------------- */
362 mc_ctl (int handle
, int ctlop
, void *arg
)
364 struct vfs_class
*vfs
= vfs_class_find_by_handle (handle
);
369 return vfs
->ctl
? (*vfs
->ctl
) (vfs_class_data_find_by_handle (handle
), ctlop
, arg
) : 0;
372 /* --------------------------------------------------------------------------------------------- */
375 mc_setctl (const char *path
, int ctlop
, void *arg
)
380 vfs_path_element_t
*path_element
;
382 vpath
= vfs_path_from_str (path
);
384 vfs_die ("You don't want to pass NULL to mc_setctl.");
386 path_element
= vfs_path_get_by_index (vpath
, -1);
387 if (vfs_path_element_valid (path_element
))
389 path_element
->class->setctl
!= NULL
? path_element
->class->setctl (vpath
,
392 vfs_path_free (vpath
);
396 /* --------------------------------------------------------------------------------------------- */
399 mc_close (int handle
)
401 struct vfs_class
*vfs
;
404 if (handle
== -1 || !vfs_class_data_find_by_handle (handle
))
407 vfs
= vfs_class_find_by_handle (handle
);
412 return close (handle
);
415 vfs_die ("VFS must support close.\n");
416 result
= (*vfs
->close
) (vfs_class_data_find_by_handle (handle
));
417 vfs_free_handle (handle
);
419 errno
= vfs_ferrno (vfs
);
424 /* --------------------------------------------------------------------------------------------- */
427 mc_opendir (const char *dirname
)
429 int handle
, *handlep
;
432 vfs_path_element_t
*path_element
;
434 vpath
= vfs_path_from_str (dirname
);
439 path_element
= vfs_path_get_by_index (vpath
, -1);
441 if (!vfs_path_element_valid (path_element
))
444 vfs_path_free (vpath
);
448 info
= path_element
->class->opendir
? (*path_element
->class->opendir
) (vpath
) : NULL
;
452 errno
= path_element
->class->opendir
? vfs_ferrno (path_element
->class) : E_NOTSUPP
;
453 vfs_path_free (vpath
);
457 path_element
->dir
.info
= info
;
459 path_element
->dir
.converter
=
460 (path_element
->encoding
!=
461 NULL
) ? str_crt_conv_from (path_element
->encoding
) : str_cnv_from_term
;
462 if (path_element
->dir
.converter
== INVALID_CONV
)
463 path_element
->dir
.converter
= str_cnv_from_term
;
465 handle
= vfs_new_handle (path_element
->class, vfs_path_element_clone (path_element
));
467 handlep
= g_new (int, 1);
469 vfs_path_free (vpath
);
470 return (DIR *) handlep
;
473 /* --------------------------------------------------------------------------------------------- */
476 mc_readdir (DIR * dirp
)
479 struct vfs_class
*vfs
;
480 struct dirent
*entry
= NULL
;
481 vfs_path_element_t
*vfs_path_element
;
484 if (!mc_readdir_result
)
486 /* We can't just allocate struct dirent as (see man dirent.h)
487 * struct dirent has VERY nonnaive semantics of allocating
488 * d_name in it. Moreover, linux's glibc-2.9 allocates dirents _less_,
489 * than 'sizeof (struct dirent)' making full bitwise (sizeof dirent) copy
490 * heap corrupter. So, allocate longliving dirent with at least
491 * (MAXNAMLEN + 1) for d_name in it.
492 * Strictly saying resulting dirent is unusable as we don't adjust internal
493 * structures, holding dirent size. But we don't use it in libc infrastructure.
494 * TODO: to make simpler homemade dirent-alike structure.
496 mc_readdir_result
= (struct dirent
*) g_malloc (sizeof (struct dirent
) + MAXNAMLEN
+ 1);
504 handle
= *(int *) dirp
;
506 vfs
= vfs_class_find_by_handle (handle
);
510 vfs_path_element
= vfs_class_data_find_by_handle (handle
);
513 entry
= (*vfs
->readdir
) (vfs_path_element
->dir
.info
);
516 g_string_set_size (vfs_str_buffer
, 0);
518 str_vfs_convert_from (vfs_path_element
->dir
.converter
, entry
->d_name
, vfs_str_buffer
);
519 mc_readdir_result
->d_ino
= entry
->d_ino
;
520 g_strlcpy (mc_readdir_result
->d_name
, vfs_str_buffer
->str
, MAXNAMLEN
+ 1);
523 errno
= vfs
->readdir
? vfs_ferrno (vfs
) : E_NOTSUPP
;
524 return (entry
!= NULL
) ? mc_readdir_result
: NULL
;
527 /* --------------------------------------------------------------------------------------------- */
530 mc_closedir (DIR * dirp
)
532 int handle
= *(int *) dirp
;
533 struct vfs_class
*vfs
;
536 vfs
= vfs_class_find_by_handle (handle
);
539 vfs_path_element_t
*vfs_path_element
;
540 vfs_path_element
= vfs_class_data_find_by_handle (handle
);
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
;
547 result
= vfs
->closedir
? (*vfs
->closedir
) (vfs_path_element
->dir
.info
) : -1;
548 vfs_free_handle (handle
);
549 vfs_path_element_free (vfs_path_element
);
555 /* --------------------------------------------------------------------------------------------- */
558 mc_stat (const char *filename
, struct stat
*buf
)
562 vfs_path_element_t
*path_element
;
564 vpath
= vfs_path_from_str (filename
);
568 path_element
= vfs_path_get_by_index (vpath
, -1);
570 if (vfs_path_element_valid (path_element
))
572 result
= path_element
->class->stat
? (*path_element
->class->stat
) (vpath
, buf
) : -1;
574 errno
= path_element
->class->name
? vfs_ferrno (path_element
->class) : E_NOTSUPP
;
577 vfs_path_free (vpath
);
581 /* --------------------------------------------------------------------------------------------- */
584 mc_lstat (const char *filename
, struct stat
*buf
)
588 vfs_path_element_t
*path_element
;
590 vpath
= vfs_path_from_str (filename
);
594 path_element
= vfs_path_get_by_index (vpath
, -1);
596 if (vfs_path_element_valid (path_element
))
598 result
= path_element
->class->lstat
? (*path_element
->class->lstat
) (vpath
, buf
) : -1;
600 errno
= path_element
->class->name
? vfs_ferrno (path_element
->class) : E_NOTSUPP
;
603 vfs_path_free (vpath
);
607 /* --------------------------------------------------------------------------------------------- */
610 mc_fstat (int handle
, struct stat
*buf
)
612 struct vfs_class
*vfs
;
618 vfs
= vfs_class_find_by_handle (handle
);
622 result
= vfs
->fstat
? (*vfs
->fstat
) (vfs_class_data_find_by_handle (handle
), buf
) : -1;
624 errno
= vfs
->name
? vfs_ferrno (vfs
) : E_NOTSUPP
;
628 /* --------------------------------------------------------------------------------------------- */
630 * Return current directory. If it's local, reread the current directory
631 * from the OS. Put directory to the provided buffer.
635 mc_get_current_wd (char *buffer
, size_t size
)
637 char *cwd
= _vfs_get_cwd ();
639 g_strlcpy (buffer
, cwd
, size
);
645 /* --------------------------------------------------------------------------------------------- */
648 mc_getlocalcopy (const char *pathname
)
652 vfs_path_element_t
*path_element
;
654 vpath
= vfs_path_from_str (pathname
);
658 path_element
= vfs_path_get_by_index (vpath
, -1);
660 if (vfs_path_element_valid (path_element
))
662 result
= path_element
->class->getlocalcopy
!= NULL
?
663 path_element
->class->getlocalcopy (vpath
) : mc_def_getlocalcopy (pathname
);
665 errno
= vfs_ferrno (path_element
->class);
667 vfs_path_free (vpath
);
671 /* --------------------------------------------------------------------------------------------- */
674 mc_ungetlocalcopy (const char *pathname
, const char *local
, int has_changed
)
676 int return_value
= -1;
678 vfs_path_element_t
*path_element
;
680 vpath
= vfs_path_from_str (pathname
);
684 path_element
= vfs_path_get_by_index (vpath
, -1);
686 if (vfs_path_element_valid (path_element
))
688 return_value
= path_element
->class->ungetlocalcopy
!= NULL
?
689 path_element
->class->ungetlocalcopy (vpath
, local
,
691 mc_def_ungetlocalcopy (path_element
->class, path_element
->path
, local
, has_changed
);
693 vfs_path_free (vpath
);
697 /* --------------------------------------------------------------------------------------------- */
700 * Return 0 on success, -1 on failure.
704 mc_chdir (const char *path
)
706 struct vfs_class
*old_vfs
;
710 vfs_path_element_t
*path_element
;
712 vpath
= vfs_path_from_str (path
);
717 path_element
= vfs_path_get_by_index (vpath
, -1);
719 if (!vfs_path_element_valid (path_element
) || path_element
->class->chdir
== NULL
)
721 vfs_path_free (vpath
);
725 result
= (*path_element
->class->chdir
) (vpath
);
729 errno
= vfs_ferrno (path_element
->class);
730 vfs_path_free (vpath
);
734 old_vfsid
= vfs_getid (vfs_get_raw_current_dir ());
735 old_vfs
= current_vfs
;
737 /* Actually change directory */
738 vfs_set_raw_current_dir (vpath
);
739 current_vfs
= path_element
->class;
741 /* This function uses the new current_dir implicitly */
742 vfs_stamp_create (old_vfs
, old_vfsid
);
744 /* Sometimes we assume no trailing slash on cwd */
745 path_element
= vfs_path_get_by_index (vfs_get_raw_current_dir (), -1);
746 if (vfs_path_element_valid (path_element
) && (*path_element
->path
!= '\0'))
749 p
= strchr (path_element
->path
, 0) - 1;
750 if (*p
== PATH_SEP
&& p
> path_element
->path
)
756 /* --------------------------------------------------------------------------------------------- */
759 mc_lseek (int fd
, off_t offset
, int whence
)
761 struct vfs_class
*vfs
;
767 vfs
= vfs_class_find_by_handle (fd
);
771 result
= vfs
->lseek
? (*vfs
->lseek
) (vfs_class_data_find_by_handle (fd
), offset
, whence
) : -1;
773 errno
= vfs
->lseek
? vfs_ferrno (vfs
) : E_NOTSUPP
;
777 /* --------------------------------------------------------------------------------------------- */