Changed interface of following functions to handle vfs_path_t object as parameter:
[midnight-commander.git] / lib / vfs / interface.c
blobf4e47aec92a8bb17dc03b72c3e0fb2ccffd13e37
1 /*
2 Virtual File System: interface functions
4 Copyright (C) 2011
5 The Free Software Foundation, Inc.
7 Written by:
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/>.
26 /**
27 * \file
28 * \brief Source: Virtual File System: path handlers
29 * \author Slava Zanko
30 * \date 2011
34 #include <config.h>
36 #include <stdio.h>
37 #include <stdlib.h> /* For atol() */
38 #include <stdarg.h>
39 #include <string.h>
40 #include <errno.h>
41 #include <sys/types.h>
42 #include <signal.h>
43 #include <ctype.h> /* is_digit() */
44 #include <fcntl.h>
45 #include <sys/stat.h>
46 #include <unistd.h>
47 #include <dirent.h>
49 #include "lib/global.h"
51 #include "lib/widget.h" /* message() */
52 #include "lib/strutil.h" /* str_crt_conv_from() */
54 #include "vfs.h"
55 #include "utilvfs.h"
56 #include "path.h"
57 #include "gc.h"
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 /* --------------------------------------------------------------------------------------------- */
75 static char *
76 mc_def_getlocalcopy (const char *filename)
78 char *tmp;
79 int fdin, fdout;
80 ssize_t i;
81 char buffer[8192];
82 struct stat mystat;
83 vfs_path_t *vpath = vfs_path_from_str (filename);
85 fdin = mc_open (filename, O_RDONLY | O_LINEAR);
86 if (fdin == -1)
87 return NULL;
89 fdout = vfs_mkstemps (&tmp, "vfs", filename);
91 if (fdout == -1)
92 goto fail;
93 while ((i = mc_read (fdin, buffer, sizeof (buffer))) > 0)
95 if (write (fdout, buffer, i) != i)
96 goto fail;
98 if (i == -1)
99 goto fail;
100 i = mc_close (fdin);
101 fdin = -1;
102 if (i == -1)
103 goto fail;
104 if (close (fdout) == -1)
106 fdout = -1;
107 goto fail;
110 if (mc_stat (vpath, &mystat) != -1)
111 chmod (tmp, mystat.st_mode);
112 vfs_path_free (vpath);
114 return tmp;
116 fail:
117 vfs_path_free (vpath);
118 if (fdout != -1)
119 close (fdout);
120 if (fdin != -1)
121 mc_close (fdin);
122 g_free (tmp);
123 return NULL;
126 /* --------------------------------------------------------------------------------------------- */
128 static int
129 mc_def_ungetlocalcopy (struct vfs_class *vfs, const char *filename,
130 const char *local, int has_changed)
132 int fdin = -1, fdout = -1;
133 if (has_changed)
135 char buffer[8192];
136 ssize_t i;
138 if (!vfs->write)
139 goto failed;
141 fdin = open (local, O_RDONLY);
142 if (fdin == -1)
143 goto failed;
144 fdout = mc_open (filename, O_WRONLY | O_TRUNC);
145 if (fdout == -1)
146 goto failed;
147 while ((i = read (fdin, buffer, sizeof (buffer))) > 0)
148 if (mc_write (fdout, buffer, (size_t) i) != i)
149 goto failed;
150 if (i == -1)
151 goto failed;
153 if (close (fdin) == -1)
155 fdin = -1;
156 goto failed;
158 fdin = -1;
159 if (mc_close (fdout) == -1)
161 fdout = -1;
162 goto failed;
165 unlink (local);
166 return 0;
168 failed:
169 message (D_ERROR, _("Changes to file lost"), "%s", filename);
170 if (fdout != -1)
171 mc_close (fdout);
172 if (fdin != -1)
173 close (fdin);
174 unlink (local);
175 return -1;
178 /* --------------------------------------------------------------------------------------------- */
179 /*** public functions ****************************************************************************/
180 /* --------------------------------------------------------------------------------------------- */
183 mc_open (const char *filename, int flags, ...)
185 int mode = 0, result = -1;
186 vfs_path_t *vpath;
187 vfs_path_element_t *path_element;
189 vpath = vfs_path_from_str (filename);
190 if (vpath == NULL)
191 return -1;
193 /* Get the mode flag */
194 if (flags & O_CREAT)
196 va_list ap;
197 va_start (ap, flags);
198 mode = va_arg (ap, int);
199 va_end (ap);
202 path_element = vfs_path_get_by_index (vpath, -1);
203 if (vfs_path_element_valid (path_element) && path_element->class->open != NULL)
205 void *info;
206 /* open must be supported */
207 info = path_element->class->open (vpath, flags, mode);
208 if (info == NULL)
209 errno = vfs_ferrno (path_element->class);
210 else
211 result = vfs_new_handle (path_element->class, info);
213 else
214 errno = -EOPNOTSUPP;
216 vfs_path_free (vpath);
217 return result;
220 /* --------------------------------------------------------------------------------------------- */
222 /* *INDENT-OFF* */
224 #define MC_NAMEOP(name, inarg, callarg) \
225 int mc_##name inarg \
227 int result; \
228 vfs_path_element_t *path_element; \
230 if (vpath == NULL) \
231 return -1; \
233 path_element = vfs_path_get_by_index (vpath, -1); \
234 if (!vfs_path_element_valid (path_element)) \
236 return -1; \
239 result = path_element->class->name != NULL ? path_element->class->name callarg : -1; \
240 if (result == -1) \
241 errno = path_element->class->name != NULL ? vfs_ferrno (path_element->class) : E_NOTSUPP; \
242 return result; \
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))
254 /* *INDENT-ON* */
256 /* --------------------------------------------------------------------------------------------- */
259 mc_symlink (const char *name1, const char *path)
261 int result = -1;
262 vfs_path_t *vpath1, *vpath2;
264 vpath1 = vfs_path_from_str (path);
265 if (vpath1 == NULL)
266 return -1;
268 vpath2 = vfs_path_from_str_flags (name1, VPF_NO_CANON);
270 if (vpath2 != NULL)
272 vfs_path_element_t *path_element = vfs_path_get_by_index (vpath1, -1);
273 if (vfs_path_element_valid (path_element))
275 result =
276 path_element->class->symlink !=
277 NULL ? path_element->class->symlink (vpath2, vpath1) : -1;
279 if (result == -1)
280 errno =
281 path_element->class->symlink !=
282 NULL ? vfs_ferrno (path_element->class) : E_NOTSUPP;
285 vfs_path_free (vpath1);
286 vfs_path_free (vpath2);
287 return result;
290 /* --------------------------------------------------------------------------------------------- */
292 /* *INDENT-OFF* */
294 #define MC_HANDLEOP(name, inarg, callarg) \
295 ssize_t mc_##name inarg \
297 struct vfs_class *vfs; \
298 int result; \
299 if (handle == -1) \
300 return -1; \
301 vfs = vfs_class_find_by_handle (handle); \
302 if (vfs == NULL) \
303 return -1; \
304 result = vfs->name != NULL ? vfs->name callarg : -1; \
305 if (result == -1) \
306 errno = vfs->name != NULL ? vfs_ferrno (vfs) : E_NOTSUPP; \
307 return result; \
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) \
318 int result; \
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) \
324 return -1; \
326 vpath2 = vfs_path_from_str (fname2); \
327 if (vpath2 == NULL) \
329 vfs_path_free(vpath1); \
330 return -1; \
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) \
338 errno = EXDEV; \
339 vfs_path_free(vpath1); \
340 vfs_path_free(vpath2); \
341 return -1; \
344 result = path_element1->class->name != NULL \
345 ? path_element1->class->name (vpath1, vpath2) \
346 : -1; \
347 if (result == -1) \
348 errno = path_element1->class->name != NULL ? vfs_ferrno (path_element1->class) : E_NOTSUPP; \
349 vfs_path_free(vpath1); \
350 vfs_path_free(vpath2); \
351 return result; \
354 MC_RENAMEOP (link)
355 MC_RENAMEOP (rename)
357 /* *INDENT-ON* */
359 /* --------------------------------------------------------------------------------------------- */
362 mc_ctl (int handle, int ctlop, void *arg)
364 struct vfs_class *vfs = vfs_class_find_by_handle (handle);
366 if (vfs == NULL)
367 return 0;
369 return vfs->ctl ? (*vfs->ctl) (vfs_class_data_find_by_handle (handle), ctlop, arg) : 0;
372 /* --------------------------------------------------------------------------------------------- */
375 mc_setctl (const vfs_path_t * vpath, int ctlop, void *arg)
377 int result = -1;
379 vfs_path_element_t *path_element;
381 if (vpath == NULL)
382 vfs_die ("You don't want to pass NULL to mc_setctl.");
384 path_element = vfs_path_get_by_index (vpath, -1);
385 if (vfs_path_element_valid (path_element))
386 result =
387 path_element->class->setctl != NULL ? path_element->class->setctl (vpath,
388 ctlop, arg) : 0;
390 return result;
393 /* --------------------------------------------------------------------------------------------- */
396 mc_close (int handle)
398 struct vfs_class *vfs;
399 int result;
401 if (handle == -1 || !vfs_class_data_find_by_handle (handle))
402 return -1;
404 vfs = vfs_class_find_by_handle (handle);
405 if (vfs == NULL)
406 return -1;
408 if (handle < 3)
409 return close (handle);
411 if (!vfs->close)
412 vfs_die ("VFS must support close.\n");
413 result = (*vfs->close) (vfs_class_data_find_by_handle (handle));
414 vfs_free_handle (handle);
415 if (result == -1)
416 errno = vfs_ferrno (vfs);
418 return result;
421 /* --------------------------------------------------------------------------------------------- */
423 DIR *
424 mc_opendir (const vfs_path_t * vpath)
426 int handle, *handlep;
427 void *info;
428 vfs_path_element_t *path_element;
430 if (vpath == NULL)
431 return NULL;
433 path_element = vfs_path_get_by_index (vpath, -1);
435 if (!vfs_path_element_valid (path_element))
437 errno = E_NOTSUPP;
438 return NULL;
441 info = path_element->class->opendir ? (*path_element->class->opendir) (vpath) : NULL;
443 if (info == NULL)
445 errno = path_element->class->opendir ? vfs_ferrno (path_element->class) : E_NOTSUPP;
446 return NULL;
449 path_element->dir.info = info;
451 path_element->dir.converter =
452 (path_element->encoding !=
453 NULL) ? str_crt_conv_from (path_element->encoding) : str_cnv_from_term;
454 if (path_element->dir.converter == INVALID_CONV)
455 path_element->dir.converter = str_cnv_from_term;
457 handle = vfs_new_handle (path_element->class, vfs_path_element_clone (path_element));
459 handlep = g_new (int, 1);
460 *handlep = handle;
461 return (DIR *) handlep;
464 /* --------------------------------------------------------------------------------------------- */
466 struct dirent *
467 mc_readdir (DIR * dirp)
469 int handle;
470 struct vfs_class *vfs;
471 struct dirent *entry = NULL;
472 vfs_path_element_t *vfs_path_element;
473 estr_t state;
475 if (!mc_readdir_result)
477 /* We can't just allocate struct dirent as (see man dirent.h)
478 * struct dirent has VERY nonnaive semantics of allocating
479 * d_name in it. Moreover, linux's glibc-2.9 allocates dirents _less_,
480 * than 'sizeof (struct dirent)' making full bitwise (sizeof dirent) copy
481 * heap corrupter. So, allocate longliving dirent with at least
482 * (MAXNAMLEN + 1) for d_name in it.
483 * Strictly saying resulting dirent is unusable as we don't adjust internal
484 * structures, holding dirent size. But we don't use it in libc infrastructure.
485 * TODO: to make simpler homemade dirent-alike structure.
487 mc_readdir_result = (struct dirent *) g_malloc (sizeof (struct dirent) + MAXNAMLEN + 1);
490 if (!dirp)
492 errno = EFAULT;
493 return NULL;
495 handle = *(int *) dirp;
497 vfs = vfs_class_find_by_handle (handle);
498 if (vfs == NULL)
499 return NULL;
501 vfs_path_element = vfs_class_data_find_by_handle (handle);
502 if (vfs->readdir)
504 entry = (*vfs->readdir) (vfs_path_element->dir.info);
505 if (entry == NULL)
506 return NULL;
507 g_string_set_size (vfs_str_buffer, 0);
508 state =
509 str_vfs_convert_from (vfs_path_element->dir.converter, entry->d_name, vfs_str_buffer);
510 mc_readdir_result->d_ino = entry->d_ino;
511 g_strlcpy (mc_readdir_result->d_name, vfs_str_buffer->str, MAXNAMLEN + 1);
513 if (entry == NULL)
514 errno = vfs->readdir ? vfs_ferrno (vfs) : E_NOTSUPP;
515 return (entry != NULL) ? mc_readdir_result : NULL;
518 /* --------------------------------------------------------------------------------------------- */
521 mc_closedir (DIR * dirp)
523 int handle = *(int *) dirp;
524 struct vfs_class *vfs;
525 int result = -1;
527 vfs = vfs_class_find_by_handle (handle);
528 if (vfs != NULL)
530 vfs_path_element_t *vfs_path_element;
531 vfs_path_element = vfs_class_data_find_by_handle (handle);
532 if (vfs_path_element->dir.converter != str_cnv_from_term)
534 str_close_conv (vfs_path_element->dir.converter);
535 vfs_path_element->dir.converter = INVALID_CONV;
538 result = vfs->closedir ? (*vfs->closedir) (vfs_path_element->dir.info) : -1;
539 vfs_free_handle (handle);
540 vfs_path_element_free (vfs_path_element);
542 g_free (dirp);
543 return result;
546 /* --------------------------------------------------------------------------------------------- */
549 mc_stat (const vfs_path_t * vpath, struct stat *buf)
551 int result = -1;
552 vfs_path_element_t *path_element;
554 if (vpath == NULL)
555 return -1;
557 path_element = vfs_path_get_by_index (vpath, -1);
559 if (vfs_path_element_valid (path_element))
561 result = path_element->class->stat ? (*path_element->class->stat) (vpath, buf) : -1;
562 if (result == -1)
563 errno = path_element->class->name ? vfs_ferrno (path_element->class) : E_NOTSUPP;
566 return result;
569 /* --------------------------------------------------------------------------------------------- */
572 mc_lstat (const vfs_path_t * vpath, struct stat *buf)
574 int result = -1;
575 vfs_path_element_t *path_element;
577 if (vpath == NULL)
578 return -1;
580 path_element = vfs_path_get_by_index (vpath, -1);
582 if (vfs_path_element_valid (path_element))
584 result = path_element->class->lstat ? (*path_element->class->lstat) (vpath, buf) : -1;
585 if (result == -1)
586 errno = path_element->class->name ? vfs_ferrno (path_element->class) : E_NOTSUPP;
589 return result;
592 /* --------------------------------------------------------------------------------------------- */
595 mc_fstat (int handle, struct stat *buf)
597 struct vfs_class *vfs;
598 int result;
600 if (handle == -1)
601 return -1;
603 vfs = vfs_class_find_by_handle (handle);
604 if (vfs == NULL)
605 return -1;
607 result = vfs->fstat ? (*vfs->fstat) (vfs_class_data_find_by_handle (handle), buf) : -1;
608 if (result == -1)
609 errno = vfs->name ? vfs_ferrno (vfs) : E_NOTSUPP;
610 return result;
613 /* --------------------------------------------------------------------------------------------- */
615 * Return current directory. If it's local, reread the current directory
616 * from the OS. Put directory to the provided buffer.
619 char *
620 mc_get_current_wd (char *buffer, size_t size)
622 char *cwd = _vfs_get_cwd ();
624 g_strlcpy (buffer, cwd, size);
625 g_free (cwd);
627 return buffer;
630 /* --------------------------------------------------------------------------------------------- */
632 char *
633 mc_getlocalcopy (const char *pathname)
635 char *result = NULL;
636 vfs_path_t *vpath;
637 vfs_path_element_t *path_element;
639 vpath = vfs_path_from_str (pathname);
640 if (vpath == NULL)
641 return NULL;
643 path_element = vfs_path_get_by_index (vpath, -1);
645 if (vfs_path_element_valid (path_element))
647 result = path_element->class->getlocalcopy != NULL ?
648 path_element->class->getlocalcopy (vpath) : mc_def_getlocalcopy (pathname);
649 if (result == NULL)
650 errno = vfs_ferrno (path_element->class);
652 vfs_path_free (vpath);
653 return result;
656 /* --------------------------------------------------------------------------------------------- */
659 mc_ungetlocalcopy (const char *pathname, const char *local, int has_changed)
661 int return_value = -1;
662 vfs_path_t *vpath;
663 vfs_path_element_t *path_element;
665 vpath = vfs_path_from_str (pathname);
666 if (vpath == NULL)
667 return -1;
669 path_element = vfs_path_get_by_index (vpath, -1);
671 if (vfs_path_element_valid (path_element))
673 return_value = path_element->class->ungetlocalcopy != NULL ?
674 path_element->class->ungetlocalcopy (vpath, local,
675 has_changed) :
676 mc_def_ungetlocalcopy (path_element->class, path_element->path, local, has_changed);
678 vfs_path_free (vpath);
679 return return_value;
682 /* --------------------------------------------------------------------------------------------- */
684 * VFS chdir.
686 * @param vpath VFS-path
688 * @return 0 on success, -1 on failure.
692 mc_chdir (const vfs_path_t * vpath)
694 struct vfs_class *old_vfs;
695 vfsid old_vfsid;
696 int result;
697 vfs_path_element_t *path_element;
699 if (vpath == NULL)
700 return -1;
702 path_element = vfs_path_get_by_index (vpath, -1);
704 if (!vfs_path_element_valid (path_element) || path_element->class->chdir == NULL)
706 return -1;
709 result = (*path_element->class->chdir) (vpath);
711 if (result == -1)
713 errno = vfs_ferrno (path_element->class);
714 return -1;
717 old_vfsid = vfs_getid (vfs_get_raw_current_dir ());
718 old_vfs = current_vfs;
720 /* Actually change directory */
721 vfs_set_raw_current_dir (vfs_path_clone (vpath));
722 current_vfs = path_element->class;
724 /* This function uses the new current_dir implicitly */
725 vfs_stamp_create (old_vfs, old_vfsid);
727 /* Sometimes we assume no trailing slash on cwd */
728 path_element = vfs_path_get_by_index (vfs_get_raw_current_dir (), -1);
729 if (vfs_path_element_valid (path_element) && (*path_element->path != '\0'))
731 char *p;
732 p = strchr (path_element->path, 0) - 1;
733 if (*p == PATH_SEP && p > path_element->path)
734 *p = 0;
736 return 0;
739 /* --------------------------------------------------------------------------------------------- */
741 off_t
742 mc_lseek (int fd, off_t offset, int whence)
744 struct vfs_class *vfs;
745 off_t result;
747 if (fd == -1)
748 return -1;
750 vfs = vfs_class_find_by_handle (fd);
751 if (vfs == NULL)
752 return -1;
754 result = vfs->lseek ? (*vfs->lseek) (vfs_class_data_find_by_handle (fd), offset, whence) : -1;
755 if (result == -1)
756 errno = vfs->lseek ? vfs_ferrno (vfs) : E_NOTSUPP;
757 return result;
760 /* --------------------------------------------------------------------------------------------- */