Ticket #2361: VFS URI reimplementation
[midnight-commander.git] / lib / vfs / interface.c
blob31b5702d7e9bea51f3b645b1e8fe0a5f43b5b0e7
1 /* Virtual File System: interface functions
2 Copyright (C) 2011 Free Software Foundation, Inc.
4 Written by:
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,
22 MA 02110-1301, USA.
25 /**
26 * \file
27 * \brief Source: Virtual File System: path handlers
28 * \author Slava Zanko
29 * \date 2011
33 #include <config.h>
35 #include <stdio.h>
36 #include <stdlib.h> /* For atol() */
37 #include <stdarg.h>
38 #include <string.h>
39 #include <errno.h>
40 #include <sys/types.h>
41 #include <signal.h>
42 #include <ctype.h> /* is_digit() */
43 #include <fcntl.h>
44 #include <sys/stat.h>
45 #include <unistd.h>
46 #include <dirent.h>
48 #include "lib/global.h"
50 #include "lib/widget.h" /* message() */
51 #include "lib/strutil.h" /* str_crt_conv_from() */
53 #include "vfs.h"
54 #include "utilvfs.h"
55 #include "path.h"
56 #include "gc.h"
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 /* --------------------------------------------------------------------------------------------- */
74 static char *
75 mc_def_getlocalcopy (const char *filename)
77 char *tmp;
78 int fdin, fdout;
79 ssize_t i;
80 char buffer[8192];
81 struct stat mystat;
83 fdin = mc_open (filename, O_RDONLY | O_LINEAR);
84 if (fdin == -1)
85 return NULL;
87 fdout = vfs_mkstemps (&tmp, "vfs", filename);
89 if (fdout == -1)
90 goto fail;
91 while ((i = mc_read (fdin, buffer, sizeof (buffer))) > 0)
93 if (write (fdout, buffer, i) != i)
94 goto fail;
96 if (i == -1)
97 goto fail;
98 i = mc_close (fdin);
99 fdin = -1;
100 if (i == -1)
101 goto fail;
102 if (close (fdout) == -1)
104 fdout = -1;
105 goto fail;
108 if (mc_stat (filename, &mystat) != -1)
109 chmod (tmp, mystat.st_mode);
111 return tmp;
113 fail:
114 if (fdout != -1)
115 close (fdout);
116 if (fdin != -1)
117 mc_close (fdin);
118 g_free (tmp);
119 return NULL;
122 /* --------------------------------------------------------------------------------------------- */
124 static int
125 mc_def_ungetlocalcopy (struct vfs_class *vfs, const char *filename,
126 const char *local, int has_changed)
128 int fdin = -1, fdout = -1;
129 if (has_changed)
131 char buffer[8192];
132 ssize_t i;
134 if (!vfs->write)
135 goto failed;
137 fdin = open (local, O_RDONLY);
138 if (fdin == -1)
139 goto failed;
140 fdout = mc_open (filename, O_WRONLY | O_TRUNC);
141 if (fdout == -1)
142 goto failed;
143 while ((i = read (fdin, buffer, sizeof (buffer))) > 0)
144 if (mc_write (fdout, buffer, (size_t) i) != i)
145 goto failed;
146 if (i == -1)
147 goto failed;
149 if (close (fdin) == -1)
151 fdin = -1;
152 goto failed;
154 fdin = -1;
155 if (mc_close (fdout) == -1)
157 fdout = -1;
158 goto failed;
161 unlink (local);
162 return 0;
164 failed:
165 message (D_ERROR, _("Changes to file lost"), "%s", filename);
166 if (fdout != -1)
167 mc_close (fdout);
168 if (fdin != -1)
169 close (fdin);
170 unlink (local);
171 return -1;
174 /* --------------------------------------------------------------------------------------------- */
175 /*** public functions ****************************************************************************/
176 /* --------------------------------------------------------------------------------------------- */
179 mc_open (const char *filename, int flags, ...)
181 int mode = 0, result = -1;
182 vfs_path_t *vpath;
183 vfs_path_element_t *path_element;
185 vpath = vfs_path_from_str (filename);
186 if (vpath == NULL)
187 return -1;
189 /* Get the mode flag */
190 if (flags & O_CREAT)
192 va_list ap;
193 va_start (ap, flags);
194 mode = va_arg (ap, int);
195 va_end (ap);
198 path_element = vfs_path_get_by_index (vpath, -1);
199 if (vfs_path_element_valid (path_element) && path_element->class->open != NULL)
201 void *info;
202 /* open must be supported */
203 info = path_element->class->open (vpath, flags, mode);
204 if (info == NULL)
205 errno = vfs_ferrno (path_element->class);
206 else
207 result = vfs_new_handle (path_element->class, info);
209 else
210 errno = -EOPNOTSUPP;
212 vfs_path_free (vpath);
213 return result;
216 /* --------------------------------------------------------------------------------------------- */
218 /* *INDENT-OFF* */
220 #define MC_NAMEOP(name, inarg, callarg) \
221 int mc_##name inarg \
223 int result; \
224 vfs_path_t *vpath; \
225 vfs_path_element_t *path_element; \
227 vpath = vfs_path_from_str (path); \
228 if (vpath == NULL) \
229 return -1; \
231 path_element = vfs_path_get_by_index (vpath, -1); \
232 if (!vfs_path_element_valid (path_element)) \
234 vfs_path_free(vpath); \
235 return -1; \
238 result = path_element->class->name != NULL ? path_element->class->name callarg : -1; \
239 if (result == -1) \
240 errno = path_element->class->name != NULL ? vfs_ferrno (path_element->class) : E_NOTSUPP; \
241 vfs_path_free(vpath); \
242 return result; \
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))
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 (name1);
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 char *path, int ctlop, void *arg)
377 int result = -1;
379 vfs_path_t *vpath;
380 vfs_path_element_t *path_element;
382 vpath = vfs_path_from_str (path);
383 if (vpath == NULL)
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))
388 result =
389 path_element->class->setctl != NULL ? path_element->class->setctl (vpath,
390 ctlop, arg) : 0;
392 vfs_path_free (vpath);
393 return result;
396 /* --------------------------------------------------------------------------------------------- */
399 mc_close (int handle)
401 struct vfs_class *vfs;
402 int result;
404 if (handle == -1 || !vfs_class_data_find_by_handle (handle))
405 return -1;
407 vfs = vfs_class_find_by_handle (handle);
408 if (vfs == NULL)
409 return -1;
411 if (handle < 3)
412 return close (handle);
414 if (!vfs->close)
415 vfs_die ("VFS must support close.\n");
416 result = (*vfs->close) (vfs_class_data_find_by_handle (handle));
417 vfs_free_handle (handle);
418 if (result == -1)
419 errno = vfs_ferrno (vfs);
421 return result;
424 /* --------------------------------------------------------------------------------------------- */
426 DIR *
427 mc_opendir (const char *dirname)
429 int handle, *handlep;
430 void *info;
431 vfs_path_t *vpath;
432 vfs_path_element_t *path_element;
434 vpath = vfs_path_from_str (dirname);
436 if (vpath == NULL)
437 return NULL;
439 path_element = vfs_path_get_by_index (vpath, -1);
441 if (!vfs_path_element_valid (path_element))
443 errno = E_NOTSUPP;
444 vfs_path_free (vpath);
445 return NULL;
448 info = path_element->class->opendir ? (*path_element->class->opendir) (vpath) : NULL;
450 if (info == NULL)
452 errno = path_element->class->opendir ? vfs_ferrno (path_element->class) : E_NOTSUPP;
453 vfs_path_free (vpath);
454 return NULL;
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);
468 *handlep = handle;
469 vfs_path_free (vpath);
470 return (DIR *) handlep;
473 /* --------------------------------------------------------------------------------------------- */
475 struct dirent *
476 mc_readdir (DIR * dirp)
478 int handle;
479 struct vfs_class *vfs;
480 struct dirent *entry = NULL;
481 vfs_path_element_t *vfs_path_element;
482 estr_t state;
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);
499 if (!dirp)
501 errno = EFAULT;
502 return NULL;
504 handle = *(int *) dirp;
506 vfs = vfs_class_find_by_handle (handle);
507 if (vfs == NULL)
508 return NULL;
510 vfs_path_element = vfs_class_data_find_by_handle (handle);
511 if (vfs->readdir)
513 entry = (*vfs->readdir) (vfs_path_element->dir.info);
514 if (entry == NULL)
515 return NULL;
516 g_string_set_size (vfs_str_buffer, 0);
517 state =
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);
522 if (entry == NULL)
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;
534 int result = -1;
536 vfs = vfs_class_find_by_handle (handle);
537 if (vfs != NULL)
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);
551 g_free (dirp);
552 return result;
555 /* --------------------------------------------------------------------------------------------- */
558 mc_stat (const char *filename, struct stat *buf)
560 int result = -1;
561 vfs_path_t *vpath;
562 vfs_path_element_t *path_element;
564 vpath = vfs_path_from_str (filename);
565 if (vpath == NULL)
566 return -1;
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;
573 if (result == -1)
574 errno = path_element->class->name ? vfs_ferrno (path_element->class) : E_NOTSUPP;
577 vfs_path_free (vpath);
578 return result;
581 /* --------------------------------------------------------------------------------------------- */
584 mc_lstat (const char *filename, struct stat *buf)
586 int result = -1;
587 vfs_path_t *vpath;
588 vfs_path_element_t *path_element;
590 vpath = vfs_path_from_str (filename);
591 if (vpath == NULL)
592 return -1;
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;
599 if (result == -1)
600 errno = path_element->class->name ? vfs_ferrno (path_element->class) : E_NOTSUPP;
603 vfs_path_free (vpath);
604 return result;
607 /* --------------------------------------------------------------------------------------------- */
610 mc_fstat (int handle, struct stat *buf)
612 struct vfs_class *vfs;
613 int result;
615 if (handle == -1)
616 return -1;
618 vfs = vfs_class_find_by_handle (handle);
619 if (vfs == NULL)
620 return -1;
622 result = vfs->fstat ? (*vfs->fstat) (vfs_class_data_find_by_handle (handle), buf) : -1;
623 if (result == -1)
624 errno = vfs->name ? vfs_ferrno (vfs) : E_NOTSUPP;
625 return result;
628 /* --------------------------------------------------------------------------------------------- */
630 * Return current directory. If it's local, reread the current directory
631 * from the OS. Put directory to the provided buffer.
634 char *
635 mc_get_current_wd (char *buffer, size_t size)
637 char *cwd = _vfs_get_cwd ();
639 g_strlcpy (buffer, cwd, size);
640 g_free (cwd);
642 return buffer;
645 /* --------------------------------------------------------------------------------------------- */
647 char *
648 mc_getlocalcopy (const char *pathname)
650 char *result = NULL;
651 vfs_path_t *vpath;
652 vfs_path_element_t *path_element;
654 vpath = vfs_path_from_str (pathname);
655 if (vpath == NULL)
656 return NULL;
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);
664 if (result == NULL)
665 errno = vfs_ferrno (path_element->class);
667 vfs_path_free (vpath);
668 return result;
671 /* --------------------------------------------------------------------------------------------- */
674 mc_ungetlocalcopy (const char *pathname, const char *local, int has_changed)
676 int return_value = -1;
677 vfs_path_t *vpath;
678 vfs_path_element_t *path_element;
680 vpath = vfs_path_from_str (pathname);
681 if (vpath == NULL)
682 return -1;
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,
690 has_changed) :
691 mc_def_ungetlocalcopy (path_element->class, path_element->path, local, has_changed);
693 vfs_path_free (vpath);
694 return return_value;
697 /* --------------------------------------------------------------------------------------------- */
699 * VFS chdir.
700 * Return 0 on success, -1 on failure.
704 mc_chdir (const char *path)
706 struct vfs_class *old_vfs;
707 vfsid old_vfsid;
708 int result;
709 vfs_path_t *vpath;
710 vfs_path_element_t *path_element;
712 vpath = vfs_path_from_str (path);
714 if (vpath == NULL)
715 return -1;
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);
722 return -1;
725 result = (*path_element->class->chdir) (vpath);
727 if (result == -1)
729 errno = vfs_ferrno (path_element->class);
730 vfs_path_free (vpath);
731 return -1;
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'))
748 char *p;
749 p = strchr (path_element->path, 0) - 1;
750 if (*p == PATH_SEP && p > path_element->path)
751 *p = 0;
753 return 0;
756 /* --------------------------------------------------------------------------------------------- */
758 off_t
759 mc_lseek (int fd, off_t offset, int whence)
761 struct vfs_class *vfs;
762 off_t result;
764 if (fd == -1)
765 return -1;
767 vfs = vfs_class_find_by_handle (fd);
768 if (vfs == NULL)
769 return -1;
771 result = vfs->lseek ? (*vfs->lseek) (vfs_class_data_find_by_handle (fd), offset, whence) : -1;
772 if (result == -1)
773 errno = vfs->lseek ? vfs_ferrno (vfs) : E_NOTSUPP;
774 return result;
777 /* --------------------------------------------------------------------------------------------- */