Changed interface of function mc_open() for handle vfs_path_t object as parameter
[midnight-commander.git] / lib / vfs / interface.c
blobbd50f282d4d78b982bd7070b3bd60d5f19f53d23
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 = NULL;
79 int fdin = -1, fdout = -1;
80 ssize_t i;
81 char buffer[BUF_1K * 8];
82 struct stat mystat;
83 vfs_path_t *vpath;
85 vpath = vfs_path_from_str (filename);
86 fdin = mc_open (vpath, O_RDONLY | O_LINEAR);
87 if (fdin == -1)
88 goto fail;
90 fdout = vfs_mkstemps (&tmp, "vfs", filename);
91 if (fdout == -1)
92 goto fail;
94 while ((i = mc_read (fdin, buffer, sizeof (buffer))) > 0)
96 if (write (fdout, buffer, i) != i)
97 goto fail;
99 if (i == -1)
100 goto fail;
101 i = mc_close (fdin);
102 fdin = -1;
103 if (i == -1)
104 goto fail;
105 if (close (fdout) == -1)
107 fdout = -1;
108 goto fail;
111 if (mc_stat (vpath, &mystat) != -1)
112 chmod (tmp, mystat.st_mode);
113 vfs_path_free (vpath);
115 return tmp;
117 fail:
118 vfs_path_free (vpath);
119 if (fdout != -1)
120 close (fdout);
121 if (fdin != -1)
122 mc_close (fdin);
123 g_free (tmp);
124 return NULL;
127 /* --------------------------------------------------------------------------------------------- */
129 static int
130 mc_def_ungetlocalcopy (struct vfs_class *vfs, const char *filename,
131 const char *local, int has_changed)
133 vfs_path_t *vpath;
134 int fdin = -1, fdout = -1;
136 vpath = vfs_path_from_str (filename);
138 if (has_changed)
140 char buffer[BUF_1K * 8];
141 ssize_t i;
143 if (!vfs->write)
144 goto failed;
146 fdin = open (local, O_RDONLY);
147 if (fdin == -1)
148 goto failed;
149 fdout = mc_open (vpath, O_WRONLY | O_TRUNC);
150 if (fdout == -1)
151 goto failed;
152 while ((i = read (fdin, buffer, sizeof (buffer))) > 0)
153 if (mc_write (fdout, buffer, (size_t) i) != i)
154 goto failed;
155 if (i == -1)
156 goto failed;
158 if (close (fdin) == -1)
160 fdin = -1;
161 goto failed;
163 fdin = -1;
164 if (mc_close (fdout) == -1)
166 fdout = -1;
167 goto failed;
170 unlink (local);
171 vfs_path_free (vpath);
172 return 0;
174 failed:
175 message (D_ERROR, _("Changes to file lost"), "%s", filename);
176 if (fdout != -1)
177 mc_close (fdout);
178 if (fdin != -1)
179 close (fdin);
180 unlink (local);
181 vfs_path_free (vpath);
182 return -1;
185 /* --------------------------------------------------------------------------------------------- */
186 /*** public functions ****************************************************************************/
187 /* --------------------------------------------------------------------------------------------- */
190 mc_open (const vfs_path_t * vpath, int flags, ...)
192 int mode = 0, result = -1;
193 vfs_path_element_t *path_element;
195 if (vpath == NULL)
196 return -1;
198 /* Get the mode flag */
199 if (flags & O_CREAT)
201 va_list ap;
202 va_start (ap, flags);
203 mode = va_arg (ap, int);
204 va_end (ap);
207 path_element = vfs_path_get_by_index (vpath, -1);
208 if (vfs_path_element_valid (path_element) && path_element->class->open != NULL)
210 void *info;
211 /* open must be supported */
212 info = path_element->class->open (vpath, flags, mode);
213 if (info == NULL)
214 errno = vfs_ferrno (path_element->class);
215 else
216 result = vfs_new_handle (path_element->class, info);
218 else
219 errno = -EOPNOTSUPP;
221 return result;
224 /* --------------------------------------------------------------------------------------------- */
226 /* *INDENT-OFF* */
228 #define MC_NAMEOP(name, inarg, callarg) \
229 int mc_##name inarg \
231 int result; \
232 vfs_path_element_t *path_element; \
234 if (vpath == NULL) \
235 return -1; \
237 path_element = vfs_path_get_by_index (vpath, -1); \
238 if (!vfs_path_element_valid (path_element)) \
240 return -1; \
243 result = path_element->class->name != NULL ? path_element->class->name callarg : -1; \
244 if (result == -1) \
245 errno = path_element->class->name != NULL ? vfs_ferrno (path_element->class) : E_NOTSUPP; \
246 return result; \
249 MC_NAMEOP (chmod, (const vfs_path_t *vpath, mode_t mode), (vpath, mode))
250 MC_NAMEOP (chown, (const vfs_path_t *vpath, uid_t owner, gid_t group), (vpath, owner, group))
251 MC_NAMEOP (utime, (const vfs_path_t *vpath, struct utimbuf * times), (vpath, times))
252 MC_NAMEOP (readlink, (const vfs_path_t *vpath, char *buf, size_t bufsiz), (vpath, buf, bufsiz))
253 MC_NAMEOP (unlink, (const vfs_path_t *vpath), (vpath))
254 MC_NAMEOP (mkdir, (const vfs_path_t *vpath, mode_t mode), (vpath, mode))
255 MC_NAMEOP (rmdir, (const vfs_path_t *vpath), (vpath))
256 MC_NAMEOP (mknod, (const vfs_path_t *vpath, mode_t mode, dev_t dev), (vpath, mode, dev))
258 /* *INDENT-ON* */
260 /* --------------------------------------------------------------------------------------------- */
263 mc_symlink (const vfs_path_t * vpath1, const vfs_path_t * vpath2)
265 int result = -1;
267 if (vpath1 == NULL)
268 return -1;
270 if (vpath1 != NULL)
272 vfs_path_element_t *path_element;
274 path_element = vfs_path_get_by_index (vpath2, -1);
275 if (vfs_path_element_valid (path_element))
277 result =
278 path_element->class->symlink != NULL ?
279 path_element->class->symlink (vpath1, vpath2) : -1;
281 if (result == -1)
282 errno =
283 path_element->class->symlink != NULL ?
284 vfs_ferrno (path_element->class) : E_NOTSUPP;
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 vfs_path_t *vpath1, const vfs_path_t *vpath2) \
318 int result; \
319 vfs_path_element_t *path_element1, *path_element2; \
321 if (vpath1 == NULL || vpath2 == NULL) \
322 return -1; \
324 path_element1 = vfs_path_get_by_index (vpath1, - 1); \
325 path_element2 = vfs_path_get_by_index (vpath2, - 1); \
327 if (!vfs_path_element_valid (path_element1) || !vfs_path_element_valid (path_element2) || \
328 path_element1->class != path_element2->class) \
330 errno = EXDEV; \
331 return -1; \
334 result = path_element1->class->name != NULL \
335 ? path_element1->class->name (vpath1, vpath2) \
336 : -1; \
337 if (result == -1) \
338 errno = path_element1->class->name != NULL ? vfs_ferrno (path_element1->class) : E_NOTSUPP; \
339 return result; \
342 MC_RENAMEOP (link)
343 MC_RENAMEOP (rename)
345 /* *INDENT-ON* */
347 /* --------------------------------------------------------------------------------------------- */
350 mc_ctl (int handle, int ctlop, void *arg)
352 struct vfs_class *vfs = vfs_class_find_by_handle (handle);
354 if (vfs == NULL)
355 return 0;
357 return vfs->ctl ? (*vfs->ctl) (vfs_class_data_find_by_handle (handle), ctlop, arg) : 0;
360 /* --------------------------------------------------------------------------------------------- */
363 mc_setctl (const vfs_path_t * vpath, int ctlop, void *arg)
365 int result = -1;
367 vfs_path_element_t *path_element;
369 if (vpath == NULL)
370 vfs_die ("You don't want to pass NULL to mc_setctl.");
372 path_element = vfs_path_get_by_index (vpath, -1);
373 if (vfs_path_element_valid (path_element))
374 result =
375 path_element->class->setctl != NULL ? path_element->class->setctl (vpath,
376 ctlop, arg) : 0;
378 return result;
381 /* --------------------------------------------------------------------------------------------- */
384 mc_close (int handle)
386 struct vfs_class *vfs;
387 int result;
389 if (handle == -1 || !vfs_class_data_find_by_handle (handle))
390 return -1;
392 vfs = vfs_class_find_by_handle (handle);
393 if (vfs == NULL)
394 return -1;
396 if (handle < 3)
397 return close (handle);
399 if (!vfs->close)
400 vfs_die ("VFS must support close.\n");
401 result = (*vfs->close) (vfs_class_data_find_by_handle (handle));
402 vfs_free_handle (handle);
403 if (result == -1)
404 errno = vfs_ferrno (vfs);
406 return result;
409 /* --------------------------------------------------------------------------------------------- */
411 DIR *
412 mc_opendir (const vfs_path_t * vpath)
414 int handle, *handlep;
415 void *info;
416 vfs_path_element_t *path_element;
418 if (vpath == NULL)
419 return NULL;
421 path_element = vfs_path_get_by_index (vpath, -1);
423 if (!vfs_path_element_valid (path_element))
425 errno = E_NOTSUPP;
426 return NULL;
429 info = path_element->class->opendir ? (*path_element->class->opendir) (vpath) : NULL;
431 if (info == NULL)
433 errno = path_element->class->opendir ? vfs_ferrno (path_element->class) : E_NOTSUPP;
434 return NULL;
437 path_element->dir.info = info;
439 path_element->dir.converter =
440 (path_element->encoding !=
441 NULL) ? str_crt_conv_from (path_element->encoding) : str_cnv_from_term;
442 if (path_element->dir.converter == INVALID_CONV)
443 path_element->dir.converter = str_cnv_from_term;
445 handle = vfs_new_handle (path_element->class, vfs_path_element_clone (path_element));
447 handlep = g_new (int, 1);
448 *handlep = handle;
449 return (DIR *) handlep;
452 /* --------------------------------------------------------------------------------------------- */
454 struct dirent *
455 mc_readdir (DIR * dirp)
457 int handle;
458 struct vfs_class *vfs;
459 struct dirent *entry = NULL;
460 vfs_path_element_t *vfs_path_element;
461 estr_t state;
463 if (!mc_readdir_result)
465 /* We can't just allocate struct dirent as (see man dirent.h)
466 * struct dirent has VERY nonnaive semantics of allocating
467 * d_name in it. Moreover, linux's glibc-2.9 allocates dirents _less_,
468 * than 'sizeof (struct dirent)' making full bitwise (sizeof dirent) copy
469 * heap corrupter. So, allocate longliving dirent with at least
470 * (MAXNAMLEN + 1) for d_name in it.
471 * Strictly saying resulting dirent is unusable as we don't adjust internal
472 * structures, holding dirent size. But we don't use it in libc infrastructure.
473 * TODO: to make simpler homemade dirent-alike structure.
475 mc_readdir_result = (struct dirent *) g_malloc (sizeof (struct dirent) + MAXNAMLEN + 1);
478 if (!dirp)
480 errno = EFAULT;
481 return NULL;
483 handle = *(int *) dirp;
485 vfs = vfs_class_find_by_handle (handle);
486 if (vfs == NULL)
487 return NULL;
489 vfs_path_element = vfs_class_data_find_by_handle (handle);
490 if (vfs->readdir)
492 entry = (*vfs->readdir) (vfs_path_element->dir.info);
493 if (entry == NULL)
494 return NULL;
495 g_string_set_size (vfs_str_buffer, 0);
496 state =
497 str_vfs_convert_from (vfs_path_element->dir.converter, entry->d_name, vfs_str_buffer);
498 mc_readdir_result->d_ino = entry->d_ino;
499 g_strlcpy (mc_readdir_result->d_name, vfs_str_buffer->str, MAXNAMLEN + 1);
501 if (entry == NULL)
502 errno = vfs->readdir ? vfs_ferrno (vfs) : E_NOTSUPP;
503 return (entry != NULL) ? mc_readdir_result : NULL;
506 /* --------------------------------------------------------------------------------------------- */
509 mc_closedir (DIR * dirp)
511 int handle = *(int *) dirp;
512 struct vfs_class *vfs;
513 int result = -1;
515 vfs = vfs_class_find_by_handle (handle);
516 if (vfs != NULL)
518 vfs_path_element_t *vfs_path_element;
519 vfs_path_element = vfs_class_data_find_by_handle (handle);
520 if (vfs_path_element->dir.converter != str_cnv_from_term)
522 str_close_conv (vfs_path_element->dir.converter);
523 vfs_path_element->dir.converter = INVALID_CONV;
526 result = vfs->closedir ? (*vfs->closedir) (vfs_path_element->dir.info) : -1;
527 vfs_free_handle (handle);
528 vfs_path_element_free (vfs_path_element);
530 g_free (dirp);
531 return result;
534 /* --------------------------------------------------------------------------------------------- */
537 mc_stat (const vfs_path_t * vpath, struct stat *buf)
539 int result = -1;
540 vfs_path_element_t *path_element;
542 if (vpath == NULL)
543 return -1;
545 path_element = vfs_path_get_by_index (vpath, -1);
547 if (vfs_path_element_valid (path_element))
549 result = path_element->class->stat ? (*path_element->class->stat) (vpath, buf) : -1;
550 if (result == -1)
551 errno = path_element->class->name ? vfs_ferrno (path_element->class) : E_NOTSUPP;
554 return result;
557 /* --------------------------------------------------------------------------------------------- */
560 mc_lstat (const vfs_path_t * vpath, struct stat *buf)
562 int result = -1;
563 vfs_path_element_t *path_element;
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->lstat ? (*path_element->class->lstat) (vpath, buf) : -1;
573 if (result == -1)
574 errno = path_element->class->name ? vfs_ferrno (path_element->class) : E_NOTSUPP;
577 return result;
580 /* --------------------------------------------------------------------------------------------- */
583 mc_fstat (int handle, struct stat *buf)
585 struct vfs_class *vfs;
586 int result;
588 if (handle == -1)
589 return -1;
591 vfs = vfs_class_find_by_handle (handle);
592 if (vfs == NULL)
593 return -1;
595 result = vfs->fstat ? (*vfs->fstat) (vfs_class_data_find_by_handle (handle), buf) : -1;
596 if (result == -1)
597 errno = vfs->name ? vfs_ferrno (vfs) : E_NOTSUPP;
598 return result;
601 /* --------------------------------------------------------------------------------------------- */
603 * Return current directory. If it's local, reread the current directory
604 * from the OS. Put directory to the provided buffer.
607 char *
608 mc_get_current_wd (char *buffer, size_t size)
610 char *cwd = _vfs_get_cwd ();
612 g_strlcpy (buffer, cwd, size);
613 g_free (cwd);
615 return buffer;
618 /* --------------------------------------------------------------------------------------------- */
620 char *
621 mc_getlocalcopy (const char *pathname)
623 char *result = NULL;
624 vfs_path_t *vpath;
625 vfs_path_element_t *path_element;
627 vpath = vfs_path_from_str (pathname);
628 if (vpath == NULL)
629 return NULL;
631 path_element = vfs_path_get_by_index (vpath, -1);
633 if (vfs_path_element_valid (path_element))
635 result = path_element->class->getlocalcopy != NULL ?
636 path_element->class->getlocalcopy (vpath) : mc_def_getlocalcopy (pathname);
637 if (result == NULL)
638 errno = vfs_ferrno (path_element->class);
640 vfs_path_free (vpath);
641 return result;
644 /* --------------------------------------------------------------------------------------------- */
647 mc_ungetlocalcopy (const char *pathname, const char *local, int has_changed)
649 int return_value = -1;
650 vfs_path_t *vpath;
651 vfs_path_element_t *path_element;
653 vpath = vfs_path_from_str (pathname);
654 if (vpath == NULL)
655 return -1;
657 path_element = vfs_path_get_by_index (vpath, -1);
659 if (vfs_path_element_valid (path_element))
661 return_value = path_element->class->ungetlocalcopy != NULL ?
662 path_element->class->ungetlocalcopy (vpath, local,
663 has_changed) :
664 mc_def_ungetlocalcopy (path_element->class, path_element->path, local, has_changed);
666 vfs_path_free (vpath);
667 return return_value;
670 /* --------------------------------------------------------------------------------------------- */
672 * VFS chdir.
674 * @param vpath VFS-path
676 * @return 0 on success, -1 on failure.
680 mc_chdir (const vfs_path_t * vpath)
682 struct vfs_class *old_vfs;
683 vfsid old_vfsid;
684 int result;
685 vfs_path_element_t *path_element;
687 if (vpath == NULL)
688 return -1;
690 path_element = vfs_path_get_by_index (vpath, -1);
692 if (!vfs_path_element_valid (path_element) || path_element->class->chdir == NULL)
694 return -1;
697 result = (*path_element->class->chdir) (vpath);
699 if (result == -1)
701 errno = vfs_ferrno (path_element->class);
702 return -1;
705 old_vfsid = vfs_getid (vfs_get_raw_current_dir ());
706 old_vfs = current_vfs;
708 /* Actually change directory */
709 vfs_set_raw_current_dir (vfs_path_clone (vpath));
710 current_vfs = path_element->class;
712 /* This function uses the new current_dir implicitly */
713 vfs_stamp_create (old_vfs, old_vfsid);
715 /* Sometimes we assume no trailing slash on cwd */
716 path_element = vfs_path_get_by_index (vfs_get_raw_current_dir (), -1);
717 if (vfs_path_element_valid (path_element) && (*path_element->path != '\0'))
719 char *p;
720 p = strchr (path_element->path, 0) - 1;
721 if (*p == PATH_SEP && p > path_element->path)
722 *p = 0;
724 return 0;
727 /* --------------------------------------------------------------------------------------------- */
729 off_t
730 mc_lseek (int fd, off_t offset, int whence)
732 struct vfs_class *vfs;
733 off_t result;
735 if (fd == -1)
736 return -1;
738 vfs = vfs_class_find_by_handle (fd);
739 if (vfs == NULL)
740 return -1;
742 result = vfs->lseek ? (*vfs->lseek) (vfs_class_data_find_by_handle (fd), offset, whence) : -1;
743 if (result == -1)
744 errno = vfs->lseek ? vfs_ferrno (vfs) : E_NOTSUPP;
745 return result;
748 /* --------------------------------------------------------------------------------------------- */