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() */
49 #include "lib/global.h"
51 #include "lib/widget.h" /* message() */
52 #include "lib/strutil.h" /* str_crt_conv_from() */
59 extern GString
*vfs_str_buffer
;
60 extern struct vfs_class
*current_vfs
;
62 /*** global variables ****************************************************************************/
64 struct dirent
*mc_readdir_result
= NULL
;
66 /*** file scope macro definitions ****************************************************************/
68 /*** file scope type declarations ****************************************************************/
70 /*** file scope variables ************************************************************************/
72 /*** file scope functions ************************************************************************/
73 /* --------------------------------------------------------------------------------------------- */
76 mc_def_getlocalcopy (const char *filename
)
83 vfs_path_t
*vpath
= vfs_path_from_str (filename
);
85 fdin
= mc_open (filename
, O_RDONLY
| O_LINEAR
);
89 fdout
= vfs_mkstemps (&tmp
, "vfs", filename
);
93 while ((i
= mc_read (fdin
, buffer
, sizeof (buffer
))) > 0)
95 if (write (fdout
, buffer
, i
) != i
)
104 if (close (fdout
) == -1)
110 if (mc_stat (vpath
, &mystat
) != -1)
111 chmod (tmp
, mystat
.st_mode
);
112 vfs_path_free (vpath
);
117 vfs_path_free (vpath
);
126 /* --------------------------------------------------------------------------------------------- */
129 mc_def_ungetlocalcopy (struct vfs_class
*vfs
, const char *filename
,
130 const char *local
, int has_changed
)
132 int fdin
= -1, fdout
= -1;
141 fdin
= open (local
, O_RDONLY
);
144 fdout
= mc_open (filename
, O_WRONLY
| O_TRUNC
);
147 while ((i
= read (fdin
, buffer
, sizeof (buffer
))) > 0)
148 if (mc_write (fdout
, buffer
, (size_t) i
) != i
)
153 if (close (fdin
) == -1)
159 if (mc_close (fdout
) == -1)
169 message (D_ERROR
, _("Changes to file lost"), "%s", filename
);
178 /* --------------------------------------------------------------------------------------------- */
179 /*** public functions ****************************************************************************/
180 /* --------------------------------------------------------------------------------------------- */
183 mc_open (const char *filename
, int flags
, ...)
185 int mode
= 0, result
= -1;
187 vfs_path_element_t
*path_element
;
189 vpath
= vfs_path_from_str (filename
);
193 /* Get the mode flag */
197 va_start (ap
, flags
);
198 mode
= va_arg (ap
, int);
202 path_element
= vfs_path_get_by_index (vpath
, -1);
203 if (vfs_path_element_valid (path_element
) && path_element
->class->open
!= NULL
)
206 /* open must be supported */
207 info
= path_element
->class->open (vpath
, flags
, mode
);
209 errno
= vfs_ferrno (path_element
->class);
211 result
= vfs_new_handle (path_element
->class, info
);
216 vfs_path_free (vpath
);
220 /* --------------------------------------------------------------------------------------------- */
224 #define MC_NAMEOP(name, inarg, callarg) \
225 int mc_##name inarg \
228 vfs_path_element_t *path_element; \
233 path_element = vfs_path_get_by_index (vpath, -1); \
234 if (!vfs_path_element_valid (path_element)) \
239 result = path_element->class->name != NULL ? path_element->class->name callarg : -1; \
241 errno = path_element->class->name != NULL ? vfs_ferrno (path_element->class) : E_NOTSUPP; \
245 MC_NAMEOP (chmod
, (const vfs_path_t
*vpath
, mode_t mode
), (vpath
, mode
))
246 MC_NAMEOP (chown
, (const vfs_path_t
*vpath
, uid_t owner
, gid_t group
), (vpath
, owner
, group
))
247 MC_NAMEOP (utime
, (const vfs_path_t
*vpath
, struct utimbuf
* times
), (vpath
, times
))
248 MC_NAMEOP (readlink
, (const vfs_path_t
*vpath
, char *buf
, size_t bufsiz
), (vpath
, buf
, bufsiz
))
249 MC_NAMEOP (unlink
, (const vfs_path_t
*vpath
), (vpath
))
250 MC_NAMEOP (mkdir
, (const vfs_path_t
*vpath
, mode_t mode
), (vpath
, mode
))
251 MC_NAMEOP (rmdir
, (const vfs_path_t
*vpath
), (vpath
))
252 MC_NAMEOP (mknod
, (const vfs_path_t
*vpath
, mode_t mode
, dev_t dev
), (vpath
, mode
, dev
))
256 /* --------------------------------------------------------------------------------------------- */
259 mc_symlink (const vfs_path_t
*vpath1
, const vfs_path_t
*vpath2
)
268 vfs_path_element_t
*path_element
;
270 path_element
= vfs_path_get_by_index (vpath2
, -1);
271 if (vfs_path_element_valid (path_element
))
274 path_element
->class->symlink
!= NULL
?
275 path_element
->class->symlink (vpath1
, vpath2
) : -1;
279 path_element
->class->symlink
!= NULL
?
280 vfs_ferrno (path_element
->class) : E_NOTSUPP
;
286 /* --------------------------------------------------------------------------------------------- */
290 #define MC_HANDLEOP(name, inarg, callarg) \
291 ssize_t mc_##name inarg \
293 struct vfs_class *vfs; \
297 vfs = vfs_class_find_by_handle (handle); \
300 result = vfs->name != NULL ? vfs->name callarg : -1; \
302 errno = vfs->name != NULL ? vfs_ferrno (vfs) : E_NOTSUPP; \
306 MC_HANDLEOP (read
, (int handle
, void *buffer
, size_t count
), (vfs_class_data_find_by_handle (handle
), buffer
, count
))
307 MC_HANDLEOP (write
, (int handle
, const void *buf
, size_t nbyte
), (vfs_class_data_find_by_handle (handle
), buf
, nbyte
))
309 /* --------------------------------------------------------------------------------------------- */
311 #define MC_RENAMEOP(name) \
312 int mc_##name (const vfs_path_t *vpath1, const vfs_path_t *vpath2) \
315 vfs_path_element_t *path_element1, *path_element2; \
317 if (vpath1 == NULL || vpath2 == NULL) \
320 path_element1 = vfs_path_get_by_index (vpath1, - 1); \
321 path_element2 = vfs_path_get_by_index (vpath2, - 1); \
323 if (!vfs_path_element_valid (path_element1) || !vfs_path_element_valid (path_element2) || \
324 path_element1->class != path_element2->class) \
330 result = path_element1->class->name != NULL \
331 ? path_element1->class->name (vpath1, vpath2) \
334 errno = path_element1->class->name != NULL ? vfs_ferrno (path_element1->class) : E_NOTSUPP; \
343 /* --------------------------------------------------------------------------------------------- */
346 mc_ctl (int handle
, int ctlop
, void *arg
)
348 struct vfs_class
*vfs
= vfs_class_find_by_handle (handle
);
353 return vfs
->ctl
? (*vfs
->ctl
) (vfs_class_data_find_by_handle (handle
), ctlop
, arg
) : 0;
356 /* --------------------------------------------------------------------------------------------- */
359 mc_setctl (const vfs_path_t
* vpath
, int ctlop
, void *arg
)
363 vfs_path_element_t
*path_element
;
366 vfs_die ("You don't want to pass NULL to mc_setctl.");
368 path_element
= vfs_path_get_by_index (vpath
, -1);
369 if (vfs_path_element_valid (path_element
))
371 path_element
->class->setctl
!= NULL
? path_element
->class->setctl (vpath
,
377 /* --------------------------------------------------------------------------------------------- */
380 mc_close (int handle
)
382 struct vfs_class
*vfs
;
385 if (handle
== -1 || !vfs_class_data_find_by_handle (handle
))
388 vfs
= vfs_class_find_by_handle (handle
);
393 return close (handle
);
396 vfs_die ("VFS must support close.\n");
397 result
= (*vfs
->close
) (vfs_class_data_find_by_handle (handle
));
398 vfs_free_handle (handle
);
400 errno
= vfs_ferrno (vfs
);
405 /* --------------------------------------------------------------------------------------------- */
408 mc_opendir (const vfs_path_t
* vpath
)
410 int handle
, *handlep
;
412 vfs_path_element_t
*path_element
;
417 path_element
= vfs_path_get_by_index (vpath
, -1);
419 if (!vfs_path_element_valid (path_element
))
425 info
= path_element
->class->opendir
? (*path_element
->class->opendir
) (vpath
) : NULL
;
429 errno
= path_element
->class->opendir
? vfs_ferrno (path_element
->class) : E_NOTSUPP
;
433 path_element
->dir
.info
= info
;
435 path_element
->dir
.converter
=
436 (path_element
->encoding
!=
437 NULL
) ? str_crt_conv_from (path_element
->encoding
) : str_cnv_from_term
;
438 if (path_element
->dir
.converter
== INVALID_CONV
)
439 path_element
->dir
.converter
= str_cnv_from_term
;
441 handle
= vfs_new_handle (path_element
->class, vfs_path_element_clone (path_element
));
443 handlep
= g_new (int, 1);
445 return (DIR *) handlep
;
448 /* --------------------------------------------------------------------------------------------- */
451 mc_readdir (DIR * dirp
)
454 struct vfs_class
*vfs
;
455 struct dirent
*entry
= NULL
;
456 vfs_path_element_t
*vfs_path_element
;
459 if (!mc_readdir_result
)
461 /* We can't just allocate struct dirent as (see man dirent.h)
462 * struct dirent has VERY nonnaive semantics of allocating
463 * d_name in it. Moreover, linux's glibc-2.9 allocates dirents _less_,
464 * than 'sizeof (struct dirent)' making full bitwise (sizeof dirent) copy
465 * heap corrupter. So, allocate longliving dirent with at least
466 * (MAXNAMLEN + 1) for d_name in it.
467 * Strictly saying resulting dirent is unusable as we don't adjust internal
468 * structures, holding dirent size. But we don't use it in libc infrastructure.
469 * TODO: to make simpler homemade dirent-alike structure.
471 mc_readdir_result
= (struct dirent
*) g_malloc (sizeof (struct dirent
) + MAXNAMLEN
+ 1);
479 handle
= *(int *) dirp
;
481 vfs
= vfs_class_find_by_handle (handle
);
485 vfs_path_element
= vfs_class_data_find_by_handle (handle
);
488 entry
= (*vfs
->readdir
) (vfs_path_element
->dir
.info
);
491 g_string_set_size (vfs_str_buffer
, 0);
493 str_vfs_convert_from (vfs_path_element
->dir
.converter
, entry
->d_name
, vfs_str_buffer
);
494 mc_readdir_result
->d_ino
= entry
->d_ino
;
495 g_strlcpy (mc_readdir_result
->d_name
, vfs_str_buffer
->str
, MAXNAMLEN
+ 1);
498 errno
= vfs
->readdir
? vfs_ferrno (vfs
) : E_NOTSUPP
;
499 return (entry
!= NULL
) ? mc_readdir_result
: NULL
;
502 /* --------------------------------------------------------------------------------------------- */
505 mc_closedir (DIR * dirp
)
507 int handle
= *(int *) dirp
;
508 struct vfs_class
*vfs
;
511 vfs
= vfs_class_find_by_handle (handle
);
514 vfs_path_element_t
*vfs_path_element
;
515 vfs_path_element
= vfs_class_data_find_by_handle (handle
);
516 if (vfs_path_element
->dir
.converter
!= str_cnv_from_term
)
518 str_close_conv (vfs_path_element
->dir
.converter
);
519 vfs_path_element
->dir
.converter
= INVALID_CONV
;
522 result
= vfs
->closedir
? (*vfs
->closedir
) (vfs_path_element
->dir
.info
) : -1;
523 vfs_free_handle (handle
);
524 vfs_path_element_free (vfs_path_element
);
530 /* --------------------------------------------------------------------------------------------- */
533 mc_stat (const vfs_path_t
* vpath
, struct stat
*buf
)
536 vfs_path_element_t
*path_element
;
541 path_element
= vfs_path_get_by_index (vpath
, -1);
543 if (vfs_path_element_valid (path_element
))
545 result
= path_element
->class->stat
? (*path_element
->class->stat
) (vpath
, buf
) : -1;
547 errno
= path_element
->class->name
? vfs_ferrno (path_element
->class) : E_NOTSUPP
;
553 /* --------------------------------------------------------------------------------------------- */
556 mc_lstat (const vfs_path_t
* vpath
, struct stat
*buf
)
559 vfs_path_element_t
*path_element
;
564 path_element
= vfs_path_get_by_index (vpath
, -1);
566 if (vfs_path_element_valid (path_element
))
568 result
= path_element
->class->lstat
? (*path_element
->class->lstat
) (vpath
, buf
) : -1;
570 errno
= path_element
->class->name
? vfs_ferrno (path_element
->class) : E_NOTSUPP
;
576 /* --------------------------------------------------------------------------------------------- */
579 mc_fstat (int handle
, struct stat
*buf
)
581 struct vfs_class
*vfs
;
587 vfs
= vfs_class_find_by_handle (handle
);
591 result
= vfs
->fstat
? (*vfs
->fstat
) (vfs_class_data_find_by_handle (handle
), buf
) : -1;
593 errno
= vfs
->name
? vfs_ferrno (vfs
) : E_NOTSUPP
;
597 /* --------------------------------------------------------------------------------------------- */
599 * Return current directory. If it's local, reread the current directory
600 * from the OS. Put directory to the provided buffer.
604 mc_get_current_wd (char *buffer
, size_t size
)
606 char *cwd
= _vfs_get_cwd ();
608 g_strlcpy (buffer
, cwd
, size
);
614 /* --------------------------------------------------------------------------------------------- */
617 mc_getlocalcopy (const char *pathname
)
621 vfs_path_element_t
*path_element
;
623 vpath
= vfs_path_from_str (pathname
);
627 path_element
= vfs_path_get_by_index (vpath
, -1);
629 if (vfs_path_element_valid (path_element
))
631 result
= path_element
->class->getlocalcopy
!= NULL
?
632 path_element
->class->getlocalcopy (vpath
) : mc_def_getlocalcopy (pathname
);
634 errno
= vfs_ferrno (path_element
->class);
636 vfs_path_free (vpath
);
640 /* --------------------------------------------------------------------------------------------- */
643 mc_ungetlocalcopy (const char *pathname
, const char *local
, int has_changed
)
645 int return_value
= -1;
647 vfs_path_element_t
*path_element
;
649 vpath
= vfs_path_from_str (pathname
);
653 path_element
= vfs_path_get_by_index (vpath
, -1);
655 if (vfs_path_element_valid (path_element
))
657 return_value
= path_element
->class->ungetlocalcopy
!= NULL
?
658 path_element
->class->ungetlocalcopy (vpath
, local
,
660 mc_def_ungetlocalcopy (path_element
->class, path_element
->path
, local
, has_changed
);
662 vfs_path_free (vpath
);
666 /* --------------------------------------------------------------------------------------------- */
670 * @param vpath VFS-path
672 * @return 0 on success, -1 on failure.
676 mc_chdir (const vfs_path_t
* vpath
)
678 struct vfs_class
*old_vfs
;
681 vfs_path_element_t
*path_element
;
686 path_element
= vfs_path_get_by_index (vpath
, -1);
688 if (!vfs_path_element_valid (path_element
) || path_element
->class->chdir
== NULL
)
693 result
= (*path_element
->class->chdir
) (vpath
);
697 errno
= vfs_ferrno (path_element
->class);
701 old_vfsid
= vfs_getid (vfs_get_raw_current_dir ());
702 old_vfs
= current_vfs
;
704 /* Actually change directory */
705 vfs_set_raw_current_dir (vfs_path_clone (vpath
));
706 current_vfs
= path_element
->class;
708 /* This function uses the new current_dir implicitly */
709 vfs_stamp_create (old_vfs
, old_vfsid
);
711 /* Sometimes we assume no trailing slash on cwd */
712 path_element
= vfs_path_get_by_index (vfs_get_raw_current_dir (), -1);
713 if (vfs_path_element_valid (path_element
) && (*path_element
->path
!= '\0'))
716 p
= strchr (path_element
->path
, 0) - 1;
717 if (*p
== PATH_SEP
&& p
> path_element
->path
)
723 /* --------------------------------------------------------------------------------------------- */
726 mc_lseek (int fd
, off_t offset
, int whence
)
728 struct vfs_class
*vfs
;
734 vfs
= vfs_class_find_by_handle (fd
);
738 result
= vfs
->lseek
? (*vfs
->lseek
) (vfs_class_data_find_by_handle (fd
), offset
, whence
) : -1;
740 errno
= vfs
->lseek
? vfs_ferrno (vfs
) : E_NOTSUPP
;
744 /* --------------------------------------------------------------------------------------------- */