Following prototypes of functions was changed in VFS-module API:
[midnight-commander.git] / lib / vfs / direntry.c
blobe1a9f1e6e426f9285ef7155c5cec888a25bc153f
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 (struct vfs_class *me, const char *dirname)
443 struct vfs_s_inode *dir;
444 struct dirhandle *info;
446 dir = vfs_s_inode_from_path (me, dirname, FL_DIR | FL_FOLLOW);
447 if (dir == NULL)
448 return NULL;
449 if (!S_ISDIR (dir->st.st_mode))
450 ERRNOR (ENOTDIR, NULL);
452 dir->st.st_nlink++;
453 #if 0
454 if (dir->subdir == NULL) /* This can actually happen if we allow empty directories */
455 ERRNOR (EAGAIN, NULL);
456 #endif
457 info = g_new (struct dirhandle, 1);
458 info->cur = dir->subdir;
459 info->dir = dir;
461 return info;
464 /* --------------------------------------------------------------------------------------------- */
466 static void *
467 vfs_s_readdir (void *data)
469 static union vfs_dirent dir;
470 struct dirhandle *info = (struct dirhandle *) data;
471 const char *name;
473 if (info->cur == NULL || info->cur->data == NULL)
474 return NULL;
476 name = ((struct vfs_s_entry *) info->cur->data)->name;
477 if (name != NULL)
478 g_strlcpy (dir.dent.d_name, name, MC_MAXPATHLEN);
479 else
480 vfs_die ("Null in structure-cannot happen");
482 compute_namelen (&dir.dent);
483 info->cur = g_list_next (info->cur);
485 return (void *) &dir;
488 /* --------------------------------------------------------------------------------------------- */
490 static int
491 vfs_s_closedir (void *data)
493 struct dirhandle *info = (struct dirhandle *) data;
494 struct vfs_s_inode *dir = info->dir;
496 vfs_s_free_inode (dir->super->me, dir);
497 g_free (data);
498 return 0;
501 /* --------------------------------------------------------------------------------------------- */
503 static int
504 vfs_s_chdir (struct vfs_class *me, const char *path)
506 void *data;
508 data = vfs_s_opendir (me, path);
509 if (data == NULL)
510 return -1;
511 vfs_s_closedir (data);
512 return 0;
515 /* --------------------------------------------------------------------------------------------- */
516 /* --------------------------- stat and friends ---------------------------- */
518 static int
519 vfs_s_internal_stat (struct vfs_class *me, const char *path, struct stat *buf, int flag)
521 struct vfs_s_inode *ino;
523 ino = vfs_s_inode_from_path (me, path, flag);
524 if (ino == NULL)
525 return -1;
526 *buf = ino->st;
527 return 0;
530 /* --------------------------------------------------------------------------------------------- */
532 static int
533 vfs_s_stat (struct vfs_class *me, const char *path, struct stat *buf)
535 return vfs_s_internal_stat (me, path, buf, FL_FOLLOW);
538 /* --------------------------------------------------------------------------------------------- */
540 static int
541 vfs_s_lstat (struct vfs_class *me, const char *path, struct stat *buf)
543 return vfs_s_internal_stat (me, path, buf, FL_NONE);
546 /* --------------------------------------------------------------------------------------------- */
548 static int
549 vfs_s_fstat (void *fh, struct stat *buf)
551 *buf = FH->ino->st;
552 return 0;
555 /* --------------------------------------------------------------------------------------------- */
557 static int
558 vfs_s_readlink (struct vfs_class *me, const char *path, char *buf, size_t size)
560 struct vfs_s_inode *ino;
561 size_t len;
563 ino = vfs_s_inode_from_path (me, path, 0);
564 if (!ino)
565 return -1;
567 if (!S_ISLNK (ino->st.st_mode))
568 ERRNOR (EINVAL, -1);
570 if (ino->linkname == NULL)
571 ERRNOR (EFAULT, -1);
573 len = strlen (ino->linkname);
574 if (size < len)
575 len = size;
576 /* readlink() does not append a NUL character to buf */
577 memcpy (buf, ino->linkname, len);
578 return len;
581 /* --------------------------------------------------------------------------------------------- */
583 static ssize_t
584 vfs_s_read (void *fh, char *buffer, size_t count)
586 int n;
587 struct vfs_class *me = FH_SUPER->me;
589 if (FH->linear == LS_LINEAR_PREOPEN)
591 if (!MEDATA->linear_start (me, FH, FH->pos))
592 return -1;
595 if (FH->linear == LS_LINEAR_CLOSED)
596 vfs_die ("linear_start() did not set linear_state!");
598 if (FH->linear == LS_LINEAR_OPEN)
599 return MEDATA->linear_read (me, FH, buffer, count);
601 if (FH->handle != -1)
603 n = read (FH->handle, buffer, count);
604 if (n < 0)
605 me->verrno = errno;
606 return n;
608 vfs_die ("vfs_s_read: This should not happen\n");
609 return -1;
612 /* --------------------------------------------------------------------------------------------- */
614 static ssize_t
615 vfs_s_write (void *fh, const char *buffer, size_t count)
617 int n;
618 struct vfs_class *me = FH_SUPER->me;
620 if (FH->linear)
621 vfs_die ("no writing to linear files, please");
623 FH->changed = 1;
624 if (FH->handle != -1)
626 n = write (FH->handle, buffer, count);
627 if (n < 0)
628 me->verrno = errno;
629 return n;
631 vfs_die ("vfs_s_write: This should not happen\n");
632 return 0;
635 /* --------------------------------------------------------------------------------------------- */
637 static off_t
638 vfs_s_lseek (void *fh, off_t offset, int whence)
640 off_t size = FH->ino->st.st_size;
642 if (FH->linear == LS_LINEAR_OPEN)
643 vfs_die ("cannot lseek() after linear_read!");
645 if (FH->handle != -1)
646 { /* If we have local file opened, we want to work with it */
647 off_t retval = lseek (FH->handle, offset, whence);
648 if (retval == -1)
649 FH->ino->super->me->verrno = errno;
650 return retval;
653 switch (whence)
655 case SEEK_CUR:
656 offset += FH->pos;
657 break;
658 case SEEK_END:
659 offset += size;
660 break;
662 if (offset < 0)
663 FH->pos = 0;
664 else if (offset < size)
665 FH->pos = offset;
666 else
667 FH->pos = size;
668 return FH->pos;
671 /* --------------------------------------------------------------------------------------------- */
673 static int
674 vfs_s_close (void *fh)
676 int res = 0;
677 struct vfs_class *me = FH_SUPER->me;
679 FH_SUPER->fd_usage--;
680 if (!FH_SUPER->fd_usage)
681 vfs_stamp_create (me, FH_SUPER);
683 if (FH->linear == LS_LINEAR_OPEN)
684 MEDATA->linear_close (me, fh);
685 if (MEDATA->fh_close)
686 res = MEDATA->fh_close (me, fh);
687 if (FH->changed && MEDATA->file_store)
689 char *s = vfs_s_fullpath (me, FH->ino);
690 if (!s)
691 res = -1;
692 else
694 res = MEDATA->file_store (me, fh, s, FH->ino->localname);
695 g_free (s);
697 vfs_s_invalidate (me, FH_SUPER);
699 if (FH->handle != -1)
700 close (FH->handle);
702 vfs_s_free_inode (me, FH->ino);
703 g_free (fh);
704 return res;
707 /* --------------------------------------------------------------------------------------------- */
709 static void
710 vfs_s_print_stats (const char *fs_name, const char *action,
711 const char *file_name, off_t have, off_t need)
713 static const char *i18n_percent_transf_format = NULL;
714 static const char *i18n_transf_format = NULL;
716 if (i18n_percent_transf_format == NULL)
718 i18n_percent_transf_format = "%s: %s: %s %3d%% (%" PRIuMAX " %s";
719 i18n_transf_format = "%s: %s: %s %" PRIuMAX " %s";
722 if (need)
723 vfs_print_message (i18n_percent_transf_format, fs_name, action,
724 file_name, (int) ((double) have * 100 / need), (uintmax_t) have,
725 _("bytes transferred"));
726 else
727 vfs_print_message (i18n_transf_format, fs_name, action, file_name, (uintmax_t) have,
728 _("bytes transferred"));
731 /* --------------------------------------------------------------------------------------------- */
732 /* ------------------------------- mc support ---------------------------- */
734 static void
735 vfs_s_fill_names (struct vfs_class *me, fill_names_f func)
737 GList *iter;
739 for (iter = MEDATA->supers; iter != NULL; iter = g_list_next (iter))
741 const struct vfs_s_super *super = (const struct vfs_s_super *) iter->data;
742 char *name;
744 name = g_strconcat (super->name, "#", me->prefix, "/",
745 /* super->current_dir->name, */ (char *) NULL);
746 func (name);
747 g_free (name);
751 /* --------------------------------------------------------------------------------------------- */
753 static int
754 vfs_s_ferrno (struct vfs_class *me)
756 return me->verrno;
759 /* --------------------------------------------------------------------------------------------- */
761 * Get local copy of the given file. We reuse the existing file cache
762 * for remote filesystems. Archives use standard VFS facilities.
765 static char *
766 vfs_s_getlocalcopy (const vfs_path_t * vpath)
768 vfs_file_handler_t *fh;
769 char *local = NULL;
771 fh = vfs_s_open (vpath, O_RDONLY, 0);
773 if (fh != NULL)
775 if ((fh->ino != NULL) && (fh->ino->localname != NULL))
776 local = g_strdup (fh->ino->localname);
778 vfs_s_close (fh);
781 return local;
784 /* --------------------------------------------------------------------------------------------- */
786 * Return the local copy. Since we are using our cache, we do nothing -
787 * the cache will be removed when the archive is closed.
790 static int
791 vfs_s_ungetlocalcopy (const vfs_path_t * vpath, const char *local, int has_changed)
793 (void) vpath;
794 (void) local;
795 (void) has_changed;
796 return 0;
799 /* --------------------------------------------------------------------------------------------- */
801 static int
802 vfs_s_setctl (struct vfs_class *me, const char *path, int ctlop, void *arg)
804 switch (ctlop)
806 case VFS_SETCTL_STALE_DATA:
808 struct vfs_s_inode *ino = vfs_s_inode_from_path (me, path, 0);
810 if (ino == NULL)
811 return 0;
812 if (arg)
813 ino->super->want_stale = 1;
814 else
816 ino->super->want_stale = 0;
817 vfs_s_invalidate (me, ino->super);
819 return 1;
821 case VFS_SETCTL_LOGFILE:
822 MEDATA->logfile = fopen ((char *) arg, "w");
823 return 1;
824 case VFS_SETCTL_FLUSH:
825 MEDATA->flush = 1;
826 return 1;
828 return 0;
831 /* --------------------------------------------------------------------------------------------- */
832 /* ----------------------------- Stamping support -------------------------- */
834 static vfsid
835 vfs_s_getid (struct vfs_class *me, const char *path)
837 struct vfs_s_super *archive = NULL;
838 char *p;
840 p = vfs_s_get_path (me, path, &archive, FL_NO_OPEN);
841 if (p == NULL)
842 return NULL;
843 g_free (p);
844 return (vfsid) archive;
847 /* --------------------------------------------------------------------------------------------- */
849 static int
850 vfs_s_nothingisopen (vfsid id)
852 (void) id;
853 /* Our data structures should survive free of superblock at any time */
854 return 1;
857 /* --------------------------------------------------------------------------------------------- */
859 static void
860 vfs_s_free (vfsid id)
862 vfs_s_free_super (((struct vfs_s_super *) id)->me, (struct vfs_s_super *) id);
865 /* --------------------------------------------------------------------------------------------- */
867 static int
868 vfs_s_dir_uptodate (struct vfs_class *me, struct vfs_s_inode *ino)
870 struct timeval tim;
872 if (MEDATA->flush)
874 MEDATA->flush = 0;
875 return 0;
878 gettimeofday (&tim, NULL);
879 if (tim.tv_sec < ino->timestamp.tv_sec)
880 return 1;
881 return 0;
885 /* --------------------------------------------------------------------------------------------- */
886 /*** public functions ****************************************************************************/
887 /* --------------------------------------------------------------------------------------------- */
889 struct vfs_s_inode *
890 vfs_s_new_inode (struct vfs_class *me, struct vfs_s_super *super, struct stat *initstat)
892 struct vfs_s_inode *ino;
894 ino = g_try_new0 (struct vfs_s_inode, 1);
895 if (ino == NULL)
896 return NULL;
898 if (initstat)
899 ino->st = *initstat;
900 ino->super = super;
901 ino->st.st_nlink = 0;
902 ino->st.st_ino = MEDATA->inode_counter++;
903 ino->st.st_dev = MEDATA->rdev;
905 super->ino_usage++;
906 total_inodes++;
908 CALL (init_inode) (me, ino);
910 return ino;
913 /* --------------------------------------------------------------------------------------------- */
915 struct vfs_s_entry *
916 vfs_s_new_entry (struct vfs_class *me, const char *name, struct vfs_s_inode *inode)
918 struct vfs_s_entry *entry;
920 entry = g_new0 (struct vfs_s_entry, 1);
921 total_entries++;
923 entry->name = g_strdup (name);
924 entry->ino = inode;
925 entry->ino->ent = entry;
926 CALL (init_entry) (me, entry);
928 return entry;
932 /* --------------------------------------------------------------------------------------------- */
934 void
935 vfs_s_free_entry (struct vfs_class *me, struct vfs_s_entry *ent)
937 if (ent->dir != NULL)
938 ent->dir->subdir = g_list_remove (ent->dir->subdir, ent);
940 g_free (ent->name);
941 /* ent->name = NULL; */
943 if (ent->ino != NULL)
945 ent->ino->ent = NULL;
946 vfs_s_free_inode (me, ent->ino);
949 total_entries--;
950 g_free (ent);
953 /* --------------------------------------------------------------------------------------------- */
955 void
956 vfs_s_insert_entry (struct vfs_class *me, struct vfs_s_inode *dir, struct vfs_s_entry *ent)
958 (void) me;
960 ent->dir = dir;
962 ent->ino->st.st_nlink++;
963 dir->subdir = g_list_append (dir->subdir, ent);
966 /* --------------------------------------------------------------------------------------------- */
968 struct stat *
969 vfs_s_default_stat (struct vfs_class *me, mode_t mode)
971 static struct stat st;
972 int myumask;
974 (void) me;
976 myumask = umask (022);
977 umask (myumask);
978 mode &= ~myumask;
980 st.st_mode = mode;
981 st.st_ino = 0;
982 st.st_dev = 0;
983 st.st_rdev = 0;
984 st.st_uid = getuid ();
985 st.st_gid = getgid ();
986 st.st_size = 0;
987 st.st_mtime = st.st_atime = st.st_ctime = time (NULL);
989 return &st;
992 /* --------------------------------------------------------------------------------------------- */
994 struct vfs_s_entry *
995 vfs_s_generate_entry (struct vfs_class *me, const char *name, struct vfs_s_inode *parent,
996 mode_t mode)
998 struct vfs_s_inode *inode;
999 struct stat *st;
1001 st = vfs_s_default_stat (me, mode);
1002 inode = vfs_s_new_inode (me, parent->super, st);
1004 return vfs_s_new_entry (me, name, inode);
1007 /* --------------------------------------------------------------------------------------------- */
1009 struct vfs_s_inode *
1010 vfs_s_find_inode (struct vfs_class *me, const struct vfs_s_super *super,
1011 const char *path, int follow, int flags)
1013 struct vfs_s_entry *ent;
1015 if (((MEDATA->flags & VFS_S_REMOTE) == 0) && (*path == '\0'))
1016 return super->root;
1018 ent = (MEDATA->find_entry) (me, super->root, path, follow, flags);
1019 return (ent != NULL) ? ent->ino : NULL;
1022 /* --------------------------------------------------------------------------------------------- */
1023 /* Ook, these were functions around directory entries / inodes */
1024 /* -------------------------------- superblock games -------------------------- */
1027 * Dissect the path and create corresponding superblock. Note that inname
1028 * can be changed and the result may point inside the original string.
1030 const char *
1031 vfs_s_get_path_mangle (struct vfs_class *me, char *inname, struct vfs_s_super **archive, int flags)
1033 GList *iter;
1034 const char *retval;
1035 char *local, *op;
1036 const char *archive_name;
1037 int result = -1;
1038 struct vfs_s_super *super;
1039 void *cookie = NULL;
1041 archive_name = inname;
1042 vfs_split (inname, &local, &op);
1043 retval = (local != NULL) ? local : "";
1045 if (MEDATA->archive_check != NULL)
1047 cookie = MEDATA->archive_check (me, archive_name, op);
1048 if (cookie == NULL)
1049 return NULL;
1052 for (iter = MEDATA->supers; iter != NULL; iter = g_list_next (iter))
1054 int i;
1056 super = (struct vfs_s_super *) iter->data;
1058 /* 0 == other, 1 == same, return it, 2 == other but stop scanning */
1059 i = MEDATA->archive_same (me, super, archive_name, op, cookie);
1060 if (i != 0)
1062 if (i == 1)
1063 goto return_success;
1064 break;
1068 if (flags & FL_NO_OPEN)
1069 ERRNOR (EIO, NULL);
1071 super = vfs_s_new_super (me);
1072 if (MEDATA->open_archive != NULL)
1073 result = MEDATA->open_archive (me, super, archive_name, op);
1074 if (result == -1)
1076 vfs_s_free_super (me, super);
1077 ERRNOR (EIO, NULL);
1079 if (!super->name)
1080 vfs_die ("You have to fill name\n");
1081 if (!super->root)
1082 vfs_die ("You have to fill root inode\n");
1084 vfs_s_insert_super (me, super);
1085 vfs_stamp_create (me, super);
1087 return_success:
1088 *archive = super;
1089 return retval;
1092 /* --------------------------------------------------------------------------------------------- */
1094 void
1095 vfs_s_invalidate (struct vfs_class *me, struct vfs_s_super *super)
1097 if (!super->want_stale)
1099 vfs_s_free_inode (me, super->root);
1100 super->root = vfs_s_new_inode (me, super, vfs_s_default_stat (me, S_IFDIR | 0755));
1104 /* --------------------------------------------------------------------------------------------- */
1106 char *
1107 vfs_s_fullpath (struct vfs_class *me, struct vfs_s_inode *ino)
1109 if (!ino->ent)
1110 ERRNOR (EAGAIN, NULL);
1112 if (!(MEDATA->flags & VFS_S_REMOTE))
1114 /* archives */
1115 char *newpath;
1116 char *path = g_strdup (ino->ent->name);
1117 while (1)
1119 ino = ino->ent->dir;
1120 if (ino == ino->super->root)
1121 break;
1122 newpath = g_strconcat (ino->ent->name, "/", path, (char *) NULL);
1123 g_free (path);
1124 path = newpath;
1126 return path;
1129 /* remote systems */
1130 if ((!ino->ent->dir) || (!ino->ent->dir->ent))
1131 return g_strdup (ino->ent->name);
1133 return g_strconcat (ino->ent->dir->ent->name, PATH_SEP_STR, ino->ent->name, (char *) NULL);
1136 /* --------------------------------------------------------------------------------------------- */
1137 /* --------------------------- stat and friends ---------------------------- */
1139 void *
1140 vfs_s_open (const vfs_path_t * vpath, int flags, mode_t mode)
1142 int was_changed = 0;
1143 vfs_file_handler_t *fh;
1144 struct vfs_s_super *super;
1145 char *q;
1146 struct vfs_s_inode *ino;
1147 vfs_path_element_t *path_element;
1149 path_element = vfs_path_get_by_index (vpath, vfs_path_length (vpath) - 1);
1151 q = vfs_s_get_path (path_element->class, vpath->unparsed, &super, 0);
1152 if (q == NULL)
1153 return NULL;
1154 ino = vfs_s_find_inode (path_element->class, super, q, LINK_FOLLOW, FL_NONE);
1155 if (ino && ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL)))
1157 g_free (q);
1158 path_element->class->verrno = EEXIST;
1159 return NULL;
1161 if (!ino)
1163 char *dirname, *name, *save;
1164 struct vfs_s_entry *ent;
1165 struct vfs_s_inode *dir;
1166 int tmp_handle;
1168 /* If the filesystem is read-only, disable file creation */
1169 if (!(flags & O_CREAT) || !(path_element->class->write))
1171 g_free (q);
1172 return NULL;
1175 split_dir_name (path_element->class, q, &dirname, &name, &save);
1176 dir = vfs_s_find_inode (path_element->class, super, dirname, LINK_FOLLOW, FL_DIR);
1177 if (dir == NULL)
1179 g_free (q);
1180 return NULL;
1182 if (save)
1183 *save = PATH_SEP;
1184 ent = vfs_s_generate_entry (path_element->class, name, dir, 0755);
1185 ino = ent->ino;
1186 vfs_s_insert_entry (path_element->class, dir, ent);
1187 tmp_handle = vfs_mkstemps (&ino->localname, path_element->class->name, name);
1188 if (tmp_handle == -1)
1190 g_free (q);
1191 return NULL;
1193 close (tmp_handle);
1194 was_changed = 1;
1197 g_free (q);
1199 if (S_ISDIR (ino->st.st_mode))
1201 path_element->class->verrno = EISDIR;
1202 return NULL;
1205 fh = g_new (vfs_file_handler_t, 1);
1206 fh->pos = 0;
1207 fh->ino = ino;
1208 fh->handle = -1;
1209 fh->changed = was_changed;
1210 fh->linear = 0;
1211 fh->data = NULL;
1213 if (IS_LINEAR (flags))
1215 if (VFSDATA (path_element)->linear_start)
1217 vfs_print_message (_("Starting linear transfer..."));
1218 fh->linear = LS_LINEAR_PREOPEN;
1221 else if ((VFSDATA (path_element)->fh_open != NULL)
1222 && (VFSDATA (path_element)->fh_open (path_element->class, fh, flags, mode) != 0))
1224 g_free (fh);
1225 return NULL;
1228 if (fh->ino->localname)
1230 fh->handle = open (fh->ino->localname, NO_LINEAR (flags), mode);
1231 if (fh->handle == -1)
1233 g_free (fh);
1234 path_element->class->verrno = errno;
1235 return NULL;
1239 /* i.e. we had no open files and now we have one */
1240 vfs_rmstamp (path_element->class, (vfsid) super);
1241 super->fd_usage++;
1242 fh->ino->st.st_nlink++;
1243 return fh;
1246 /* --------------------------------------------------------------------------------------------- */
1249 vfs_s_retrieve_file (struct vfs_class *me, struct vfs_s_inode *ino)
1251 /* If you want reget, you'll have to open file with O_LINEAR */
1252 off_t total = 0;
1253 char buffer[8192];
1254 int handle, n;
1255 off_t stat_size = ino->st.st_size;
1256 vfs_file_handler_t fh;
1258 memset (&fh, 0, sizeof (fh));
1260 fh.ino = ino;
1261 fh.handle = -1;
1263 handle = vfs_mkstemps (&ino->localname, me->name, ino->ent->name);
1264 if (handle == -1)
1266 me->verrno = errno;
1267 goto error_4;
1270 if (!MEDATA->linear_start (me, &fh, 0))
1271 goto error_3;
1273 /* Clear the interrupt status */
1274 tty_got_interrupt ();
1275 tty_enable_interrupt_key ();
1277 while ((n = MEDATA->linear_read (me, &fh, buffer, sizeof (buffer))))
1279 int t;
1280 if (n < 0)
1281 goto error_1;
1283 total += n;
1284 vfs_s_print_stats (me->name, _("Getting file"), ino->ent->name, total, stat_size);
1286 if (tty_got_interrupt ())
1287 goto error_1;
1289 t = write (handle, buffer, n);
1290 if (t != n)
1292 if (t == -1)
1293 me->verrno = errno;
1294 goto error_1;
1297 MEDATA->linear_close (me, &fh);
1298 close (handle);
1300 tty_disable_interrupt_key ();
1301 g_free (fh.data);
1302 return 0;
1304 error_1:
1305 MEDATA->linear_close (me, &fh);
1306 error_3:
1307 tty_disable_interrupt_key ();
1308 close (handle);
1309 unlink (ino->localname);
1310 error_4:
1311 g_free (ino->localname);
1312 ino->localname = NULL;
1313 g_free (fh.data);
1314 return -1;
1317 /* --------------------------------------------------------------------------------------------- */
1318 /* ----------------------------- Stamping support -------------------------- */
1320 /* Initialize one of our subclasses - fill common functions */
1321 void
1322 vfs_s_init_class (struct vfs_class *vclass, struct vfs_s_subclass *sub)
1324 vclass->data = sub;
1325 vclass->fill_names = vfs_s_fill_names;
1326 vclass->open = vfs_s_open;
1327 vclass->close = vfs_s_close;
1328 vclass->read = vfs_s_read;
1329 if (!(sub->flags & VFS_S_READONLY))
1331 vclass->write = vfs_s_write;
1333 vclass->opendir = vfs_s_opendir;
1334 vclass->readdir = vfs_s_readdir;
1335 vclass->closedir = vfs_s_closedir;
1336 vclass->stat = vfs_s_stat;
1337 vclass->lstat = vfs_s_lstat;
1338 vclass->fstat = vfs_s_fstat;
1339 vclass->readlink = vfs_s_readlink;
1340 vclass->chdir = vfs_s_chdir;
1341 vclass->ferrno = vfs_s_ferrno;
1342 vclass->lseek = vfs_s_lseek;
1343 vclass->getid = vfs_s_getid;
1344 vclass->nothingisopen = vfs_s_nothingisopen;
1345 vclass->free = vfs_s_free;
1346 if (sub->flags & VFS_S_REMOTE)
1348 vclass->getlocalcopy = vfs_s_getlocalcopy;
1349 vclass->ungetlocalcopy = vfs_s_ungetlocalcopy;
1350 sub->find_entry = vfs_s_find_entry_linear;
1352 else
1354 sub->find_entry = vfs_s_find_entry_tree;
1356 vclass->setctl = vfs_s_setctl;
1357 sub->dir_uptodate = vfs_s_dir_uptodate;
1360 /* --------------------------------------------------------------------------------------------- */
1361 /** Find VFS id for given directory name */
1363 vfsid
1364 vfs_getid (struct vfs_class *vclass, const char *dir)
1366 char *dir1;
1367 vfsid id = NULL;
1369 /* append slash if needed */
1370 dir1 = concat_dir_and_file (dir, "");
1371 if (vclass->getid)
1372 id = (*vclass->getid) (vclass, dir1);
1374 g_free (dir1);
1375 return id;
1378 /* --------------------------------------------------------------------------------------------- */
1379 /* ----------- Utility functions for networked filesystems -------------- */
1381 #ifdef ENABLE_VFS_NET
1383 vfs_s_select_on_two (int fd1, int fd2)
1385 fd_set set;
1386 struct timeval time_out;
1387 int v;
1388 int maxfd = (fd1 > fd2 ? fd1 : fd2) + 1;
1390 time_out.tv_sec = 1;
1391 time_out.tv_usec = 0;
1392 FD_ZERO (&set);
1393 FD_SET (fd1, &set);
1394 FD_SET (fd2, &set);
1395 v = select (maxfd, &set, 0, 0, &time_out);
1396 if (v <= 0)
1397 return v;
1398 if (FD_ISSET (fd1, &set))
1399 return 1;
1400 if (FD_ISSET (fd2, &set))
1401 return 2;
1402 return -1;
1405 /* --------------------------------------------------------------------------------------------- */
1408 vfs_s_get_line (struct vfs_class *me, int sock, char *buf, int buf_len, char term)
1410 FILE *logfile = MEDATA->logfile;
1411 int i;
1412 char c;
1414 for (i = 0; i < buf_len - 1; i++, buf++)
1416 if (read (sock, buf, sizeof (char)) <= 0)
1417 return 0;
1418 if (logfile)
1420 size_t ret1;
1421 int ret2;
1422 ret1 = fwrite (buf, 1, 1, logfile);
1423 ret2 = fflush (logfile);
1425 if (*buf == term)
1427 *buf = 0;
1428 return 1;
1432 /* Line is too long - terminate buffer and discard the rest of line */
1433 *buf = 0;
1434 while (read (sock, &c, sizeof (c)) > 0)
1436 if (logfile)
1438 size_t ret1;
1439 int ret2;
1440 ret1 = fwrite (&c, 1, 1, logfile);
1441 ret2 = fflush (logfile);
1443 if (c == '\n')
1444 return 1;
1446 return 0;
1449 /* --------------------------------------------------------------------------------------------- */
1452 vfs_s_get_line_interruptible (struct vfs_class *me, char *buffer, int size, int fd)
1454 int n;
1455 int i;
1457 (void) me;
1459 tty_enable_interrupt_key ();
1460 for (i = 0; i < size - 1; i++)
1462 n = read (fd, buffer + i, 1);
1463 tty_disable_interrupt_key ();
1464 if (n == -1 && errno == EINTR)
1466 buffer[i] = 0;
1467 return EINTR;
1469 if (n == 0)
1471 buffer[i] = 0;
1472 return 0;
1474 if (buffer[i] == '\n')
1476 buffer[i] = 0;
1477 return 1;
1480 buffer[size - 1] = 0;
1481 return 0;
1483 #endif /* ENABLE_VFS_NET */
1485 /* --------------------------------------------------------------------------------------------- */