Following prototypes of functions was changed in VFS-module API:
[midnight-commander.git] / lib / vfs / direntry.c
blob45049159e654085062b27152c5dbd43ab1e73011
1 /** \file
2 * \brief Source: directory cache support
4 * So that you do not have copy of this in each and every filesystem.
6 * Very loosely based on tar.c from midnight and archives.[ch] from
7 * avfs by Miklos Szeredi (mszeredi@inf.bme.hu)
9 * Unfortunately, I was unable to keep all filesystems
10 * uniform. tar-like filesystems use tree structure where each
11 * directory has pointers to its subdirectories. We can do this
12 * because we have full information about our archive.
14 * At ftp-like filesystems, situation is a little bit different. When
15 * you cd /usr/src/linux/drivers/char, you do _not_ want /usr,
16 * /usr/src, /usr/src/linux and /usr/src/linux/drivers to be
17 * listed. That means that we do not have complete information, and if
18 * /usr is symlink to /4, we will not know. Also we have to time out
19 * entries and things would get messy with tree-like approach. So we
20 * do different trick: root directory is completely special and
21 * completely fake, it contains entries such as 'usr', 'usr/src', ...,
22 * and we'll try to use custom find_entry function.
24 * \author Pavel Machek <pavel@ucw.cz>, distribute under LGPL.
25 * \date 1998
27 * \warning Paths here do _not_ begin with '/', so root directory of
28 * archive/site is simply "".
31 #include <config.h>
33 #include <errno.h>
34 #include <fcntl.h> /* include fcntl.h -> sys/fcntl.h only */
35 /* includes fcntl.h see IEEE Std 1003.1-2008 */
36 #include <time.h>
37 #include <sys/time.h> /* gettimeofday() */
38 #include <inttypes.h> /* uintmax_t */
39 #include <stdarg.h>
41 #include "lib/global.h"
43 #include "lib/tty/tty.h" /* enable/disable interrupt key */
44 #include "lib/util.h" /* concat_dir_and_file */
45 #if 0
46 #include "lib/widget.h" /* message() */
47 #endif
49 #include "vfs.h"
50 #include "utilvfs.h"
51 #include "xdirentry.h"
52 #include "gc.h" /* vfs_rmstamp */
54 /*** global variables ****************************************************************************/
56 /*** file scope macro definitions ****************************************************************/
58 #define CALL(x) if (MEDATA->x) MEDATA->x
60 /*** file scope type declarations ****************************************************************/
62 struct dirhandle
64 GList *cur;
65 struct vfs_s_inode *dir;
68 /*** file scope variables ************************************************************************/
70 static volatile int total_inodes = 0, total_entries = 0;
72 /*** file scope functions ************************************************************************/
73 /* --------------------------------------------------------------------------------------------- */
75 static int
76 vfs_s_entry_compare (const void *a, const void *b)
78 const struct vfs_s_entry *e = (const struct vfs_s_entry *) a;
79 const char *name = (const char *) b;
81 return strcmp (e->name, name);
84 /* --------------------------------------------------------------------------------------------- */
86 static void
87 vfs_s_free_inode (struct vfs_class *me, struct vfs_s_inode *ino)
89 if (ino == NULL)
90 vfs_die ("Don't pass NULL to me");
92 /* ==0 can happen if freshly created entry is deleted */
93 if (ino->st.st_nlink > 1)
95 ino->st.st_nlink--;
96 return;
99 while (ino->subdir != NULL)
100 vfs_s_free_entry (me, (struct vfs_s_entry *) ino->subdir->data);
102 CALL (free_inode) (me, ino);
103 g_free (ino->linkname);
104 if (ino->localname != NULL)
106 unlink (ino->localname);
107 g_free (ino->localname);
109 total_inodes--;
110 ino->super->ino_usage--;
111 g_free (ino);
114 /* --------------------------------------------------------------------------------------------- */
115 /* We were asked to create entries automagically */
117 static struct vfs_s_entry *
118 vfs_s_automake (struct vfs_class *me, struct vfs_s_inode *dir, char *path, int flags)
120 struct vfs_s_entry *res;
121 char *sep;
123 sep = strchr (path, PATH_SEP);
124 if (sep != NULL)
125 *sep = '\0';
127 res = vfs_s_generate_entry (me, path, dir, flags & FL_MKDIR ? (0777 | S_IFDIR) : 0777);
128 vfs_s_insert_entry (me, dir, res);
130 if (sep != NULL)
131 *sep = PATH_SEP;
133 return res;
136 /* --------------------------------------------------------------------------------------------- */
137 /* If the entry is a symlink, find the entry for its target */
139 static struct vfs_s_entry *
140 vfs_s_resolve_symlink (struct vfs_class *me, struct vfs_s_entry *entry, int follow)
142 char *linkname;
143 char *fullname = NULL;
144 struct vfs_s_entry *target;
146 if (follow == LINK_NO_FOLLOW)
147 return entry;
148 if (follow == 0)
149 ERRNOR (ELOOP, NULL);
150 if (!entry)
151 ERRNOR (ENOENT, NULL);
152 if (!S_ISLNK (entry->ino->st.st_mode))
153 return entry;
155 linkname = entry->ino->linkname;
156 if (linkname == NULL)
157 ERRNOR (EFAULT, NULL);
159 /* make full path from relative */
160 if (*linkname != PATH_SEP)
162 char *fullpath = vfs_s_fullpath (me, entry->dir);
163 if (fullpath)
165 fullname = g_strconcat (fullpath, "/", linkname, (char *) NULL);
166 linkname = fullname;
167 g_free (fullpath);
171 target = (MEDATA->find_entry) (me, entry->dir->super->root, linkname, follow - 1, 0);
172 g_free (fullname);
173 return target;
176 /* --------------------------------------------------------------------------------------------- */
178 * Follow > 0: follow links, serves as loop protect,
179 * == -1: do not follow links
182 static struct vfs_s_entry *
183 vfs_s_find_entry_tree (struct vfs_class *me, struct vfs_s_inode *root,
184 const char *a_path, int follow, int flags)
186 size_t pseg;
187 struct vfs_s_entry *ent = NULL;
188 char *const pathref = g_strdup (a_path);
189 char *path = pathref;
191 /* canonicalize as well, but don't remove '../' from path */
192 custom_canonicalize_pathname (path, CANON_PATH_ALL & (~CANON_PATH_REMDOUBLEDOTS));
194 while (root != NULL)
196 GList *iter;
198 while (*path == PATH_SEP) /* Strip leading '/' */
199 path++;
201 if (path[0] == '\0')
203 g_free (pathref);
204 return ent;
207 for (pseg = 0; path[pseg] != '\0' && path[pseg] != PATH_SEP; pseg++)
210 for (iter = root->subdir; iter != NULL; iter = g_list_next (iter))
212 ent = (struct vfs_s_entry *) iter->data;
213 if (strlen (ent->name) == pseg && strncmp (ent->name, path, pseg) == 0)
214 /* FOUND! */
215 break;
218 ent = iter != NULL ? (struct vfs_s_entry *) iter->data : NULL;
220 if (ent == NULL && (flags & (FL_MKFILE | FL_MKDIR)) != 0)
221 ent = vfs_s_automake (me, root, path, flags);
222 if (ent == NULL)
224 me->verrno = ENOENT;
225 goto cleanup;
228 path += pseg;
229 /* here we must follow leading directories always;
230 only the actual file is optional */
231 ent = vfs_s_resolve_symlink (me, ent,
232 strchr (path, PATH_SEP) != NULL ? LINK_FOLLOW : follow);
233 if (ent == NULL)
234 goto cleanup;
235 root = ent->ino;
237 cleanup:
238 g_free (pathref);
239 return NULL;
242 /* --------------------------------------------------------------------------------------------- */
244 static void
245 split_dir_name (struct vfs_class *me, char *path, char **dir, char **name, char **save)
247 char *s;
249 (void) me;
251 s = strrchr (path, PATH_SEP);
252 if (s == NULL)
254 *save = NULL;
255 *name = path;
256 *dir = path + strlen (path); /* an empty string */
258 else
260 *save = s;
261 *dir = path;
262 *s++ = '\0';
263 *name = s;
267 /* --------------------------------------------------------------------------------------------- */
269 static struct vfs_s_entry *
270 vfs_s_find_entry_linear (struct vfs_class *me, struct vfs_s_inode *root,
271 const char *a_path, int follow, int flags)
273 struct vfs_s_entry *ent = NULL;
274 char *const path = g_strdup (a_path);
275 struct vfs_s_entry *retval = NULL;
276 GList *iter;
278 if (root->super->root != root)
279 vfs_die ("We have to use _real_ root. Always. Sorry.");
281 /* canonicalize as well, but don't remove '../' from path */
282 custom_canonicalize_pathname (path, CANON_PATH_ALL & (~CANON_PATH_REMDOUBLEDOTS));
284 if ((flags & FL_DIR) == 0)
286 char *dirname, *name, *save;
287 struct vfs_s_inode *ino;
288 split_dir_name (me, path, &dirname, &name, &save);
289 ino = vfs_s_find_inode (me, root->super, dirname, follow, flags | FL_DIR);
290 if (save != NULL)
291 *save = PATH_SEP;
292 retval = vfs_s_find_entry_tree (me, ino, name, follow, flags);
293 g_free (path);
294 return retval;
297 iter = g_list_find_custom (root->subdir, path, (GCompareFunc) vfs_s_entry_compare);
298 ent = iter != NULL ? (struct vfs_s_entry *) iter->data : NULL;
300 if (ent != NULL && !MEDATA->dir_uptodate (me, ent->ino))
302 #if 1
303 vfs_print_message (_("Directory cache expired for %s"), path);
304 #endif
305 vfs_s_free_entry (me, ent);
306 ent = NULL;
309 if (ent == NULL)
311 struct vfs_s_inode *ino;
313 ino = vfs_s_new_inode (me, root->super, vfs_s_default_stat (me, S_IFDIR | 0755));
314 ent = vfs_s_new_entry (me, path, ino);
315 if (MEDATA->dir_load (me, ino, path) == -1)
317 vfs_s_free_entry (me, ent);
318 g_free (path);
319 return NULL;
322 vfs_s_insert_entry (me, root, ent);
324 iter = g_list_find_custom (root->subdir, path, (GCompareFunc) vfs_s_entry_compare);
325 ent = iter != NULL ? (struct vfs_s_entry *) iter->data : NULL;
327 if (ent == NULL)
328 vfs_die ("find_linear: success but directory is not there\n");
330 #if 0
331 if (!vfs_s_resolve_symlink (me, ent, follow))
333 g_free (path);
334 return NULL;
336 #endif
337 g_free (path);
338 return ent;
341 /* --------------------------------------------------------------------------------------------- */
342 /* Ook, these were functions around directory entries / inodes */
343 /* -------------------------------- superblock games -------------------------- */
345 static struct vfs_s_super *
346 vfs_s_new_super (struct vfs_class *me)
348 struct vfs_s_super *super;
350 super = g_new0 (struct vfs_s_super, 1);
351 super->me = me;
352 return super;
355 /* --------------------------------------------------------------------------------------------- */
357 static inline void
358 vfs_s_insert_super (struct vfs_class *me, struct vfs_s_super *super)
360 MEDATA->supers = g_list_prepend (MEDATA->supers, super);
363 /* --------------------------------------------------------------------------------------------- */
365 static void
366 vfs_s_free_super (struct vfs_class *me, struct vfs_s_super *super)
368 if (super->root != NULL)
370 vfs_s_free_inode (me, super->root);
371 super->root = NULL;
374 #if 0
375 /* FIXME: We currently leak small ammount of memory, sometimes. Fix it if you can. */
376 if (super->ino_usage)
377 message (D_ERROR, "Direntry warning",
378 "Super ino_usage is %d, memory leak", super->ino_usage);
380 if (super->want_stale)
381 message (D_ERROR, "Direntry warning", "%s", "Super has want_stale set");
382 #endif
384 MEDATA->supers = g_list_remove (MEDATA->supers, super);
386 CALL (free_archive) (me, super);
387 #ifdef ENABLE_VFS_NET
388 vfs_url_free (super->url);
389 #endif
390 g_free (super->name);
391 g_free (super);
394 /* --------------------------------------------------------------------------------------------- */
396 * Dissect the path and create corresponding superblock.
397 * The result should be freed.
400 static char *
401 vfs_s_get_path (struct vfs_class *me, const char *inname, struct vfs_s_super **archive, int flags)
403 char *buf, *retval;
405 buf = g_strdup (inname);
406 retval = g_strdup (vfs_s_get_path_mangle (me, buf, archive, flags));
407 g_free (buf);
408 return retval;
411 /* --------------------------------------------------------------------------------------------- */
412 /* Support of archives */
413 /* ------------------------ readdir & friends ----------------------------- */
415 static struct vfs_s_inode *
416 vfs_s_inode_from_path (struct vfs_class *me, const char *name, int flags)
418 struct vfs_s_super *super;
419 struct vfs_s_inode *ino;
420 char *q;
422 if (!(q = vfs_s_get_path (me, name, &super, 0)))
423 return NULL;
425 ino =
426 vfs_s_find_inode (me, super, q,
427 flags & FL_FOLLOW ? LINK_FOLLOW : LINK_NO_FOLLOW, flags & ~FL_FOLLOW);
428 if ((!ino) && (!*q))
429 /* We are asking about / directory of ftp server: assume it exists */
430 ino =
431 vfs_s_find_inode (me, super, q,
432 flags & FL_FOLLOW ? LINK_FOLLOW :
433 LINK_NO_FOLLOW, FL_DIR | (flags & ~FL_FOLLOW));
434 g_free (q);
435 return ino;
438 /* --------------------------------------------------------------------------------------------- */
440 static void *
441 vfs_s_opendir (const vfs_path_t * vpath)
443 struct vfs_s_inode *dir;
444 struct dirhandle *info;
445 vfs_path_element_t *path_element;
447 path_element = vfs_path_get_by_index (vpath, vfs_path_length (vpath) - 1);
448 dir = vfs_s_inode_from_path (path_element->class, vpath->unparsed, FL_DIR | FL_FOLLOW);
449 if (dir == NULL)
450 return NULL;
451 if (!S_ISDIR (dir->st.st_mode))
453 path_element->class->verrno = ENOTDIR;
454 return NULL;
457 dir->st.st_nlink++;
458 #if 0
459 if (dir->subdir == NULL) /* This can actually happen if we allow empty directories */
461 path_element->class->verrno = EAGAIN;
462 return NULL;
464 #endif
465 info = g_new (struct dirhandle, 1);
466 info->cur = dir->subdir;
467 info->dir = dir;
469 return info;
472 /* --------------------------------------------------------------------------------------------- */
474 static void *
475 vfs_s_readdir (void *data)
477 static union vfs_dirent dir;
478 struct dirhandle *info = (struct dirhandle *) data;
479 const char *name;
481 if (info->cur == NULL || info->cur->data == NULL)
482 return NULL;
484 name = ((struct vfs_s_entry *) info->cur->data)->name;
485 if (name != NULL)
486 g_strlcpy (dir.dent.d_name, name, MC_MAXPATHLEN);
487 else
488 vfs_die ("Null in structure-cannot happen");
490 compute_namelen (&dir.dent);
491 info->cur = g_list_next (info->cur);
493 return (void *) &dir;
496 /* --------------------------------------------------------------------------------------------- */
498 static int
499 vfs_s_closedir (void *data)
501 struct dirhandle *info = (struct dirhandle *) data;
502 struct vfs_s_inode *dir = info->dir;
504 vfs_s_free_inode (dir->super->me, dir);
505 g_free (data);
506 return 0;
509 /* --------------------------------------------------------------------------------------------- */
511 static int
512 vfs_s_chdir (const vfs_path_t * vpath)
514 void *data;
516 data = vfs_s_opendir (vpath);
517 if (data == NULL)
518 return -1;
519 vfs_s_closedir (data);
520 return 0;
523 /* --------------------------------------------------------------------------------------------- */
524 /* --------------------------- stat and friends ---------------------------- */
526 static int
527 vfs_s_internal_stat (const vfs_path_t * vpath, struct stat *buf, int flag)
529 struct vfs_s_inode *ino;
530 vfs_path_element_t *path_element;
532 path_element = vfs_path_get_by_index (vpath, vfs_path_length (vpath) - 1);
533 ino = vfs_s_inode_from_path (path_element->class, vpath->unparsed, flag);
534 if (ino == NULL)
535 return -1;
536 *buf = ino->st;
537 return 0;
540 /* --------------------------------------------------------------------------------------------- */
542 static int
543 vfs_s_stat (const vfs_path_t * vpath, struct stat *buf)
545 return vfs_s_internal_stat (vpath, buf, FL_FOLLOW);
548 /* --------------------------------------------------------------------------------------------- */
550 static int
551 vfs_s_lstat (const vfs_path_t * vpath, struct stat *buf)
553 return vfs_s_internal_stat (vpath, buf, FL_NONE);
556 /* --------------------------------------------------------------------------------------------- */
558 static int
559 vfs_s_fstat (void *fh, struct stat *buf)
561 *buf = FH->ino->st;
562 return 0;
565 /* --------------------------------------------------------------------------------------------- */
567 static int
568 vfs_s_readlink (const vfs_path_t * vpath, char *buf, size_t size)
570 struct vfs_s_inode *ino;
571 size_t len;
572 vfs_path_element_t *path_element;
574 path_element = vfs_path_get_by_index (vpath, vfs_path_length (vpath) - 1);
576 ino = vfs_s_inode_from_path (path_element->class, vpath->unparsed, 0);
577 if (!ino)
578 return -1;
580 if (!S_ISLNK (ino->st.st_mode))
582 path_element->class->verrno = EINVAL;
583 return -1;
586 if (ino->linkname == NULL)
588 path_element->class->verrno = EFAULT;
589 return -1;
592 len = strlen (ino->linkname);
593 if (size < len)
594 len = size;
595 /* readlink() does not append a NUL character to buf */
596 memcpy (buf, ino->linkname, len);
597 return len;
600 /* --------------------------------------------------------------------------------------------- */
602 static ssize_t
603 vfs_s_read (void *fh, char *buffer, size_t count)
605 int n;
606 struct vfs_class *me = FH_SUPER->me;
608 if (FH->linear == LS_LINEAR_PREOPEN)
610 if (!MEDATA->linear_start (me, FH, FH->pos))
611 return -1;
614 if (FH->linear == LS_LINEAR_CLOSED)
615 vfs_die ("linear_start() did not set linear_state!");
617 if (FH->linear == LS_LINEAR_OPEN)
618 return MEDATA->linear_read (me, FH, buffer, count);
620 if (FH->handle != -1)
622 n = read (FH->handle, buffer, count);
623 if (n < 0)
624 me->verrno = errno;
625 return n;
627 vfs_die ("vfs_s_read: This should not happen\n");
628 return -1;
631 /* --------------------------------------------------------------------------------------------- */
633 static ssize_t
634 vfs_s_write (void *fh, const char *buffer, size_t count)
636 int n;
637 struct vfs_class *me = FH_SUPER->me;
639 if (FH->linear)
640 vfs_die ("no writing to linear files, please");
642 FH->changed = 1;
643 if (FH->handle != -1)
645 n = write (FH->handle, buffer, count);
646 if (n < 0)
647 me->verrno = errno;
648 return n;
650 vfs_die ("vfs_s_write: This should not happen\n");
651 return 0;
654 /* --------------------------------------------------------------------------------------------- */
656 static off_t
657 vfs_s_lseek (void *fh, off_t offset, int whence)
659 off_t size = FH->ino->st.st_size;
661 if (FH->linear == LS_LINEAR_OPEN)
662 vfs_die ("cannot lseek() after linear_read!");
664 if (FH->handle != -1)
665 { /* If we have local file opened, we want to work with it */
666 off_t retval = lseek (FH->handle, offset, whence);
667 if (retval == -1)
668 FH->ino->super->me->verrno = errno;
669 return retval;
672 switch (whence)
674 case SEEK_CUR:
675 offset += FH->pos;
676 break;
677 case SEEK_END:
678 offset += size;
679 break;
681 if (offset < 0)
682 FH->pos = 0;
683 else if (offset < size)
684 FH->pos = offset;
685 else
686 FH->pos = size;
687 return FH->pos;
690 /* --------------------------------------------------------------------------------------------- */
692 static int
693 vfs_s_close (void *fh)
695 int res = 0;
696 struct vfs_class *me = FH_SUPER->me;
698 FH_SUPER->fd_usage--;
699 if (!FH_SUPER->fd_usage)
700 vfs_stamp_create (me, FH_SUPER);
702 if (FH->linear == LS_LINEAR_OPEN)
703 MEDATA->linear_close (me, fh);
704 if (MEDATA->fh_close)
705 res = MEDATA->fh_close (me, fh);
706 if (FH->changed && MEDATA->file_store)
708 char *s = vfs_s_fullpath (me, FH->ino);
709 if (!s)
710 res = -1;
711 else
713 res = MEDATA->file_store (me, fh, s, FH->ino->localname);
714 g_free (s);
716 vfs_s_invalidate (me, FH_SUPER);
718 if (FH->handle != -1)
719 close (FH->handle);
721 vfs_s_free_inode (me, FH->ino);
722 g_free (fh);
723 return res;
726 /* --------------------------------------------------------------------------------------------- */
728 static void
729 vfs_s_print_stats (const char *fs_name, const char *action,
730 const char *file_name, off_t have, off_t need)
732 static const char *i18n_percent_transf_format = NULL;
733 static const char *i18n_transf_format = NULL;
735 if (i18n_percent_transf_format == NULL)
737 i18n_percent_transf_format = "%s: %s: %s %3d%% (%" PRIuMAX " %s";
738 i18n_transf_format = "%s: %s: %s %" PRIuMAX " %s";
741 if (need)
742 vfs_print_message (i18n_percent_transf_format, fs_name, action,
743 file_name, (int) ((double) have * 100 / need), (uintmax_t) have,
744 _("bytes transferred"));
745 else
746 vfs_print_message (i18n_transf_format, fs_name, action, file_name, (uintmax_t) have,
747 _("bytes transferred"));
750 /* --------------------------------------------------------------------------------------------- */
751 /* ------------------------------- mc support ---------------------------- */
753 static void
754 vfs_s_fill_names (struct vfs_class *me, fill_names_f func)
756 GList *iter;
758 for (iter = MEDATA->supers; iter != NULL; iter = g_list_next (iter))
760 const struct vfs_s_super *super = (const struct vfs_s_super *) iter->data;
761 char *name;
763 name = g_strconcat (super->name, "#", me->prefix, "/",
764 /* super->current_dir->name, */ (char *) NULL);
765 func (name);
766 g_free (name);
770 /* --------------------------------------------------------------------------------------------- */
772 static int
773 vfs_s_ferrno (struct vfs_class *me)
775 return me->verrno;
778 /* --------------------------------------------------------------------------------------------- */
780 * Get local copy of the given file. We reuse the existing file cache
781 * for remote filesystems. Archives use standard VFS facilities.
784 static char *
785 vfs_s_getlocalcopy (const vfs_path_t * vpath)
787 vfs_file_handler_t *fh;
788 char *local = NULL;
790 fh = vfs_s_open (vpath, O_RDONLY, 0);
792 if (fh != NULL)
794 if ((fh->ino != NULL) && (fh->ino->localname != NULL))
795 local = g_strdup (fh->ino->localname);
797 vfs_s_close (fh);
800 return local;
803 /* --------------------------------------------------------------------------------------------- */
805 * Return the local copy. Since we are using our cache, we do nothing -
806 * the cache will be removed when the archive is closed.
809 static int
810 vfs_s_ungetlocalcopy (const vfs_path_t * vpath, const char *local, int has_changed)
812 (void) vpath;
813 (void) local;
814 (void) has_changed;
815 return 0;
818 /* --------------------------------------------------------------------------------------------- */
820 static int
821 vfs_s_setctl (const vfs_path_t * vpath, int ctlop, void *arg)
823 vfs_path_element_t *path_element;
825 path_element = vfs_path_get_by_index (vpath, vfs_path_length (vpath) - 1);
826 switch (ctlop)
828 case VFS_SETCTL_STALE_DATA:
830 struct vfs_s_inode *ino =
831 vfs_s_inode_from_path (path_element->class, vpath->unparsed, 0);
833 if (ino == NULL)
834 return 0;
835 if (arg)
836 ino->super->want_stale = 1;
837 else
839 ino->super->want_stale = 0;
840 vfs_s_invalidate (path_element->class, ino->super);
842 return 1;
844 case VFS_SETCTL_LOGFILE:
845 ((struct vfs_s_subclass *) path_element->class->data)->logfile = fopen ((char *) arg, "w");
846 return 1;
847 case VFS_SETCTL_FLUSH:
848 ((struct vfs_s_subclass *) path_element->class->data)->flush = 1;
849 return 1;
851 return 0;
854 /* --------------------------------------------------------------------------------------------- */
855 /* ----------------------------- Stamping support -------------------------- */
857 static vfsid
858 vfs_s_getid (const vfs_path_t * vpath)
860 struct vfs_s_super *archive = NULL;
861 char *p;
862 vfs_path_element_t *path_element;
864 path_element = vfs_path_get_by_index (vpath, vfs_path_length (vpath) - 1);
866 p = vfs_s_get_path (path_element->class, vpath->unparsed, &archive, FL_NO_OPEN);
867 if (p == NULL)
868 return NULL;
869 g_free (p);
870 return (vfsid) archive;
873 /* --------------------------------------------------------------------------------------------- */
875 static int
876 vfs_s_nothingisopen (vfsid id)
878 (void) id;
879 /* Our data structures should survive free of superblock at any time */
880 return 1;
883 /* --------------------------------------------------------------------------------------------- */
885 static void
886 vfs_s_free (vfsid id)
888 vfs_s_free_super (((struct vfs_s_super *) id)->me, (struct vfs_s_super *) id);
891 /* --------------------------------------------------------------------------------------------- */
893 static int
894 vfs_s_dir_uptodate (struct vfs_class *me, struct vfs_s_inode *ino)
896 struct timeval tim;
898 if (MEDATA->flush)
900 MEDATA->flush = 0;
901 return 0;
904 gettimeofday (&tim, NULL);
905 if (tim.tv_sec < ino->timestamp.tv_sec)
906 return 1;
907 return 0;
911 /* --------------------------------------------------------------------------------------------- */
912 /*** public functions ****************************************************************************/
913 /* --------------------------------------------------------------------------------------------- */
915 struct vfs_s_inode *
916 vfs_s_new_inode (struct vfs_class *me, struct vfs_s_super *super, struct stat *initstat)
918 struct vfs_s_inode *ino;
920 ino = g_try_new0 (struct vfs_s_inode, 1);
921 if (ino == NULL)
922 return NULL;
924 if (initstat)
925 ino->st = *initstat;
926 ino->super = super;
927 ino->st.st_nlink = 0;
928 ino->st.st_ino = MEDATA->inode_counter++;
929 ino->st.st_dev = MEDATA->rdev;
931 super->ino_usage++;
932 total_inodes++;
934 CALL (init_inode) (me, ino);
936 return ino;
939 /* --------------------------------------------------------------------------------------------- */
941 struct vfs_s_entry *
942 vfs_s_new_entry (struct vfs_class *me, const char *name, struct vfs_s_inode *inode)
944 struct vfs_s_entry *entry;
946 entry = g_new0 (struct vfs_s_entry, 1);
947 total_entries++;
949 entry->name = g_strdup (name);
950 entry->ino = inode;
951 entry->ino->ent = entry;
952 CALL (init_entry) (me, entry);
954 return entry;
958 /* --------------------------------------------------------------------------------------------- */
960 void
961 vfs_s_free_entry (struct vfs_class *me, struct vfs_s_entry *ent)
963 if (ent->dir != NULL)
964 ent->dir->subdir = g_list_remove (ent->dir->subdir, ent);
966 g_free (ent->name);
967 /* ent->name = NULL; */
969 if (ent->ino != NULL)
971 ent->ino->ent = NULL;
972 vfs_s_free_inode (me, ent->ino);
975 total_entries--;
976 g_free (ent);
979 /* --------------------------------------------------------------------------------------------- */
981 void
982 vfs_s_insert_entry (struct vfs_class *me, struct vfs_s_inode *dir, struct vfs_s_entry *ent)
984 (void) me;
986 ent->dir = dir;
988 ent->ino->st.st_nlink++;
989 dir->subdir = g_list_append (dir->subdir, ent);
992 /* --------------------------------------------------------------------------------------------- */
994 struct stat *
995 vfs_s_default_stat (struct vfs_class *me, mode_t mode)
997 static struct stat st;
998 int myumask;
1000 (void) me;
1002 myumask = umask (022);
1003 umask (myumask);
1004 mode &= ~myumask;
1006 st.st_mode = mode;
1007 st.st_ino = 0;
1008 st.st_dev = 0;
1009 st.st_rdev = 0;
1010 st.st_uid = getuid ();
1011 st.st_gid = getgid ();
1012 st.st_size = 0;
1013 st.st_mtime = st.st_atime = st.st_ctime = time (NULL);
1015 return &st;
1018 /* --------------------------------------------------------------------------------------------- */
1020 struct vfs_s_entry *
1021 vfs_s_generate_entry (struct vfs_class *me, const char *name, struct vfs_s_inode *parent,
1022 mode_t mode)
1024 struct vfs_s_inode *inode;
1025 struct stat *st;
1027 st = vfs_s_default_stat (me, mode);
1028 inode = vfs_s_new_inode (me, parent->super, st);
1030 return vfs_s_new_entry (me, name, inode);
1033 /* --------------------------------------------------------------------------------------------- */
1035 struct vfs_s_inode *
1036 vfs_s_find_inode (struct vfs_class *me, const struct vfs_s_super *super,
1037 const char *path, int follow, int flags)
1039 struct vfs_s_entry *ent;
1041 if (((MEDATA->flags & VFS_S_REMOTE) == 0) && (*path == '\0'))
1042 return super->root;
1044 ent = (MEDATA->find_entry) (me, super->root, path, follow, flags);
1045 return (ent != NULL) ? ent->ino : NULL;
1048 /* --------------------------------------------------------------------------------------------- */
1049 /* Ook, these were functions around directory entries / inodes */
1050 /* -------------------------------- superblock games -------------------------- */
1053 * Dissect the path and create corresponding superblock. Note that inname
1054 * can be changed and the result may point inside the original string.
1056 const char *
1057 vfs_s_get_path_mangle (struct vfs_class *me, char *inname, struct vfs_s_super **archive, int flags)
1059 GList *iter;
1060 const char *retval;
1061 char *local, *op;
1062 const char *archive_name;
1063 int result = -1;
1064 struct vfs_s_super *super;
1065 void *cookie = NULL;
1067 archive_name = inname;
1068 vfs_split (inname, &local, &op);
1069 retval = (local != NULL) ? local : "";
1071 if (MEDATA->archive_check != NULL)
1073 cookie = MEDATA->archive_check (me, archive_name, op);
1074 if (cookie == NULL)
1075 return NULL;
1078 for (iter = MEDATA->supers; iter != NULL; iter = g_list_next (iter))
1080 int i;
1082 super = (struct vfs_s_super *) iter->data;
1084 /* 0 == other, 1 == same, return it, 2 == other but stop scanning */
1085 i = MEDATA->archive_same (me, super, archive_name, op, cookie);
1086 if (i != 0)
1088 if (i == 1)
1089 goto return_success;
1090 break;
1094 if (flags & FL_NO_OPEN)
1095 ERRNOR (EIO, NULL);
1097 super = vfs_s_new_super (me);
1098 if (MEDATA->open_archive != NULL)
1099 result = MEDATA->open_archive (me, super, archive_name, op);
1100 if (result == -1)
1102 vfs_s_free_super (me, super);
1103 ERRNOR (EIO, NULL);
1105 if (!super->name)
1106 vfs_die ("You have to fill name\n");
1107 if (!super->root)
1108 vfs_die ("You have to fill root inode\n");
1110 vfs_s_insert_super (me, super);
1111 vfs_stamp_create (me, super);
1113 return_success:
1114 *archive = super;
1115 return retval;
1118 /* --------------------------------------------------------------------------------------------- */
1120 void
1121 vfs_s_invalidate (struct vfs_class *me, struct vfs_s_super *super)
1123 if (!super->want_stale)
1125 vfs_s_free_inode (me, super->root);
1126 super->root = vfs_s_new_inode (me, super, vfs_s_default_stat (me, S_IFDIR | 0755));
1130 /* --------------------------------------------------------------------------------------------- */
1132 char *
1133 vfs_s_fullpath (struct vfs_class *me, struct vfs_s_inode *ino)
1135 if (!ino->ent)
1136 ERRNOR (EAGAIN, NULL);
1138 if (!(MEDATA->flags & VFS_S_REMOTE))
1140 /* archives */
1141 char *newpath;
1142 char *path = g_strdup (ino->ent->name);
1143 while (1)
1145 ino = ino->ent->dir;
1146 if (ino == ino->super->root)
1147 break;
1148 newpath = g_strconcat (ino->ent->name, "/", path, (char *) NULL);
1149 g_free (path);
1150 path = newpath;
1152 return path;
1155 /* remote systems */
1156 if ((!ino->ent->dir) || (!ino->ent->dir->ent))
1157 return g_strdup (ino->ent->name);
1159 return g_strconcat (ino->ent->dir->ent->name, PATH_SEP_STR, ino->ent->name, (char *) NULL);
1162 /* --------------------------------------------------------------------------------------------- */
1163 /* --------------------------- stat and friends ---------------------------- */
1165 void *
1166 vfs_s_open (const vfs_path_t * vpath, int flags, mode_t mode)
1168 int was_changed = 0;
1169 vfs_file_handler_t *fh;
1170 struct vfs_s_super *super;
1171 char *q;
1172 struct vfs_s_inode *ino;
1173 vfs_path_element_t *path_element;
1175 path_element = vfs_path_get_by_index (vpath, vfs_path_length (vpath) - 1);
1177 q = vfs_s_get_path (path_element->class, vpath->unparsed, &super, 0);
1178 if (q == NULL)
1179 return NULL;
1180 ino = vfs_s_find_inode (path_element->class, super, q, LINK_FOLLOW, FL_NONE);
1181 if (ino && ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL)))
1183 g_free (q);
1184 path_element->class->verrno = EEXIST;
1185 return NULL;
1187 if (!ino)
1189 char *dirname, *name, *save;
1190 struct vfs_s_entry *ent;
1191 struct vfs_s_inode *dir;
1192 int tmp_handle;
1194 /* If the filesystem is read-only, disable file creation */
1195 if (!(flags & O_CREAT) || !(path_element->class->write))
1197 g_free (q);
1198 return NULL;
1201 split_dir_name (path_element->class, q, &dirname, &name, &save);
1202 dir = vfs_s_find_inode (path_element->class, super, dirname, LINK_FOLLOW, FL_DIR);
1203 if (dir == NULL)
1205 g_free (q);
1206 return NULL;
1208 if (save)
1209 *save = PATH_SEP;
1210 ent = vfs_s_generate_entry (path_element->class, name, dir, 0755);
1211 ino = ent->ino;
1212 vfs_s_insert_entry (path_element->class, dir, ent);
1213 tmp_handle = vfs_mkstemps (&ino->localname, path_element->class->name, name);
1214 if (tmp_handle == -1)
1216 g_free (q);
1217 return NULL;
1219 close (tmp_handle);
1220 was_changed = 1;
1223 g_free (q);
1225 if (S_ISDIR (ino->st.st_mode))
1227 path_element->class->verrno = EISDIR;
1228 return NULL;
1231 fh = g_new (vfs_file_handler_t, 1);
1232 fh->pos = 0;
1233 fh->ino = ino;
1234 fh->handle = -1;
1235 fh->changed = was_changed;
1236 fh->linear = 0;
1237 fh->data = NULL;
1239 if (IS_LINEAR (flags))
1241 if (VFSDATA (path_element)->linear_start)
1243 vfs_print_message (_("Starting linear transfer..."));
1244 fh->linear = LS_LINEAR_PREOPEN;
1247 else if ((VFSDATA (path_element)->fh_open != NULL)
1248 && (VFSDATA (path_element)->fh_open (path_element->class, fh, flags, mode) != 0))
1250 g_free (fh);
1251 return NULL;
1254 if (fh->ino->localname)
1256 fh->handle = open (fh->ino->localname, NO_LINEAR (flags), mode);
1257 if (fh->handle == -1)
1259 g_free (fh);
1260 path_element->class->verrno = errno;
1261 return NULL;
1265 /* i.e. we had no open files and now we have one */
1266 vfs_rmstamp (path_element->class, (vfsid) super);
1267 super->fd_usage++;
1268 fh->ino->st.st_nlink++;
1269 return fh;
1272 /* --------------------------------------------------------------------------------------------- */
1275 vfs_s_retrieve_file (struct vfs_class *me, struct vfs_s_inode *ino)
1277 /* If you want reget, you'll have to open file with O_LINEAR */
1278 off_t total = 0;
1279 char buffer[8192];
1280 int handle, n;
1281 off_t stat_size = ino->st.st_size;
1282 vfs_file_handler_t fh;
1284 memset (&fh, 0, sizeof (fh));
1286 fh.ino = ino;
1287 fh.handle = -1;
1289 handle = vfs_mkstemps (&ino->localname, me->name, ino->ent->name);
1290 if (handle == -1)
1292 me->verrno = errno;
1293 goto error_4;
1296 if (!MEDATA->linear_start (me, &fh, 0))
1297 goto error_3;
1299 /* Clear the interrupt status */
1300 tty_got_interrupt ();
1301 tty_enable_interrupt_key ();
1303 while ((n = MEDATA->linear_read (me, &fh, buffer, sizeof (buffer))))
1305 int t;
1306 if (n < 0)
1307 goto error_1;
1309 total += n;
1310 vfs_s_print_stats (me->name, _("Getting file"), ino->ent->name, total, stat_size);
1312 if (tty_got_interrupt ())
1313 goto error_1;
1315 t = write (handle, buffer, n);
1316 if (t != n)
1318 if (t == -1)
1319 me->verrno = errno;
1320 goto error_1;
1323 MEDATA->linear_close (me, &fh);
1324 close (handle);
1326 tty_disable_interrupt_key ();
1327 g_free (fh.data);
1328 return 0;
1330 error_1:
1331 MEDATA->linear_close (me, &fh);
1332 error_3:
1333 tty_disable_interrupt_key ();
1334 close (handle);
1335 unlink (ino->localname);
1336 error_4:
1337 g_free (ino->localname);
1338 ino->localname = NULL;
1339 g_free (fh.data);
1340 return -1;
1343 /* --------------------------------------------------------------------------------------------- */
1344 /* ----------------------------- Stamping support -------------------------- */
1346 /* Initialize one of our subclasses - fill common functions */
1347 void
1348 vfs_s_init_class (struct vfs_class *vclass, struct vfs_s_subclass *sub)
1350 vclass->data = sub;
1351 vclass->fill_names = vfs_s_fill_names;
1352 vclass->open = vfs_s_open;
1353 vclass->close = vfs_s_close;
1354 vclass->read = vfs_s_read;
1355 if (!(sub->flags & VFS_S_READONLY))
1357 vclass->write = vfs_s_write;
1359 vclass->opendir = vfs_s_opendir;
1360 vclass->readdir = vfs_s_readdir;
1361 vclass->closedir = vfs_s_closedir;
1362 vclass->stat = vfs_s_stat;
1363 vclass->lstat = vfs_s_lstat;
1364 vclass->fstat = vfs_s_fstat;
1365 vclass->readlink = vfs_s_readlink;
1366 vclass->chdir = vfs_s_chdir;
1367 vclass->ferrno = vfs_s_ferrno;
1368 vclass->lseek = vfs_s_lseek;
1369 vclass->getid = vfs_s_getid;
1370 vclass->nothingisopen = vfs_s_nothingisopen;
1371 vclass->free = vfs_s_free;
1372 if (sub->flags & VFS_S_REMOTE)
1374 vclass->getlocalcopy = vfs_s_getlocalcopy;
1375 vclass->ungetlocalcopy = vfs_s_ungetlocalcopy;
1376 sub->find_entry = vfs_s_find_entry_linear;
1378 else
1380 sub->find_entry = vfs_s_find_entry_tree;
1382 vclass->setctl = vfs_s_setctl;
1383 sub->dir_uptodate = vfs_s_dir_uptodate;
1386 /* --------------------------------------------------------------------------------------------- */
1387 /** Find VFS id for given directory name */
1389 vfsid
1390 vfs_getid (const vfs_path_t * vpath)
1392 vfs_path_element_t *path_element;
1394 path_element = vfs_path_get_by_index (vpath, vfs_path_length (vpath) - 1);
1395 if (path_element == NULL || path_element->class->getid == NULL)
1396 return NULL;
1398 return (*path_element->class->getid) (vpath);
1401 /* --------------------------------------------------------------------------------------------- */
1402 /* ----------- Utility functions for networked filesystems -------------- */
1404 #ifdef ENABLE_VFS_NET
1406 vfs_s_select_on_two (int fd1, int fd2)
1408 fd_set set;
1409 struct timeval time_out;
1410 int v;
1411 int maxfd = (fd1 > fd2 ? fd1 : fd2) + 1;
1413 time_out.tv_sec = 1;
1414 time_out.tv_usec = 0;
1415 FD_ZERO (&set);
1416 FD_SET (fd1, &set);
1417 FD_SET (fd2, &set);
1418 v = select (maxfd, &set, 0, 0, &time_out);
1419 if (v <= 0)
1420 return v;
1421 if (FD_ISSET (fd1, &set))
1422 return 1;
1423 if (FD_ISSET (fd2, &set))
1424 return 2;
1425 return -1;
1428 /* --------------------------------------------------------------------------------------------- */
1431 vfs_s_get_line (struct vfs_class *me, int sock, char *buf, int buf_len, char term)
1433 FILE *logfile = MEDATA->logfile;
1434 int i;
1435 char c;
1437 for (i = 0; i < buf_len - 1; i++, buf++)
1439 if (read (sock, buf, sizeof (char)) <= 0)
1440 return 0;
1441 if (logfile)
1443 size_t ret1;
1444 int ret2;
1445 ret1 = fwrite (buf, 1, 1, logfile);
1446 ret2 = fflush (logfile);
1448 if (*buf == term)
1450 *buf = 0;
1451 return 1;
1455 /* Line is too long - terminate buffer and discard the rest of line */
1456 *buf = 0;
1457 while (read (sock, &c, sizeof (c)) > 0)
1459 if (logfile)
1461 size_t ret1;
1462 int ret2;
1463 ret1 = fwrite (&c, 1, 1, logfile);
1464 ret2 = fflush (logfile);
1466 if (c == '\n')
1467 return 1;
1469 return 0;
1472 /* --------------------------------------------------------------------------------------------- */
1475 vfs_s_get_line_interruptible (struct vfs_class *me, char *buffer, int size, int fd)
1477 int n;
1478 int i;
1480 (void) me;
1482 tty_enable_interrupt_key ();
1483 for (i = 0; i < size - 1; i++)
1485 n = read (fd, buffer + i, 1);
1486 tty_disable_interrupt_key ();
1487 if (n == -1 && errno == EINTR)
1489 buffer[i] = 0;
1490 return EINTR;
1492 if (n == 0)
1494 buffer[i] = 0;
1495 return 0;
1497 if (buffer[i] == '\n')
1499 buffer[i] = 0;
1500 return 1;
1503 buffer[size - 1] = 0;
1504 return 0;
1506 #endif /* ENABLE_VFS_NET */
1508 /* --------------------------------------------------------------------------------------------- */