lib/vfs/direntry.c: types accuracy.
[midnight-commander.git] / lib / vfs / direntry.c
blob5607987dcef949b206ba993663484c1ad6c0dc7d
1 /*
2 Directory cache support
4 Copyright (C) 1998, 2011
5 The Free Software Foundation, Inc.
7 Written by:
8 Pavel Machek <pavel@ucw.cz>, 1998
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/>.
25 \warning Paths here do _not_ begin with '/', so root directory of
26 archive/site is simply "".
29 /** \file
30 * \brief Source: directory cache support
32 * So that you do not have copy of this in each and every filesystem.
34 * Very loosely based on tar.c from midnight and archives.[ch] from
35 * avfs by Miklos Szeredi (mszeredi@inf.bme.hu)
37 * Unfortunately, I was unable to keep all filesystems
38 * uniform. tar-like filesystems use tree structure where each
39 * directory has pointers to its subdirectories. We can do this
40 * because we have full information about our archive.
42 * At ftp-like filesystems, situation is a little bit different. When
43 * you cd /usr/src/linux/drivers/char, you do _not_ want /usr,
44 * /usr/src, /usr/src/linux and /usr/src/linux/drivers to be
45 * listed. That means that we do not have complete information, and if
46 * /usr is symlink to /4, we will not know. Also we have to time out
47 * entries and things would get messy with tree-like approach. So we
48 * do different trick: root directory is completely special and
49 * completely fake, it contains entries such as 'usr', 'usr/src', ...,
50 * and we'll try to use custom find_entry function.
52 * \author Pavel Machek <pavel@ucw.cz>
53 * \date 1998
57 #include <config.h>
59 #include <errno.h>
60 #include <fcntl.h> /* include fcntl.h -> sys/fcntl.h only */
61 /* includes fcntl.h see IEEE Std 1003.1-2008 */
62 #include <time.h>
63 #include <sys/time.h> /* gettimeofday() */
64 #include <inttypes.h> /* uintmax_t */
65 #include <stdarg.h>
67 #include "lib/global.h"
69 #include "lib/tty/tty.h" /* enable/disable interrupt key */
70 #include "lib/util.h" /* custom_canonicalize_pathname() */
71 #if 0
72 #include "lib/widget.h" /* message() */
73 #endif
75 #include "vfs.h"
76 #include "utilvfs.h"
77 #include "xdirentry.h"
78 #include "gc.h" /* vfs_rmstamp */
80 /*** global variables ****************************************************************************/
82 /*** file scope macro definitions ****************************************************************/
84 #define CALL(x) if (MEDATA->x) MEDATA->x
86 /*** file scope type declarations ****************************************************************/
88 struct dirhandle
90 GList *cur;
91 struct vfs_s_inode *dir;
94 /*** file scope variables ************************************************************************/
96 static volatile int total_inodes = 0, total_entries = 0;
98 /*** file scope functions ************************************************************************/
99 /* --------------------------------------------------------------------------------------------- */
101 static int
102 vfs_s_entry_compare (const void *a, const void *b)
104 const struct vfs_s_entry *e = (const struct vfs_s_entry *) a;
105 const char *name = (const char *) b;
107 return strcmp (e->name, name);
110 /* --------------------------------------------------------------------------------------------- */
112 /* We were asked to create entries automagically */
114 static struct vfs_s_entry *
115 vfs_s_automake (struct vfs_class *me, struct vfs_s_inode *dir, char *path, int flags)
117 struct vfs_s_entry *res;
118 char *sep;
120 sep = strchr (path, PATH_SEP);
121 if (sep != NULL)
122 *sep = '\0';
124 res = vfs_s_generate_entry (me, path, dir, flags & FL_MKDIR ? (0777 | S_IFDIR) : 0777);
125 vfs_s_insert_entry (me, dir, res);
127 if (sep != NULL)
128 *sep = PATH_SEP;
130 return res;
133 /* --------------------------------------------------------------------------------------------- */
134 /* If the entry is a symlink, find the entry for its target */
136 static struct vfs_s_entry *
137 vfs_s_resolve_symlink (struct vfs_class *me, struct vfs_s_entry *entry, int follow)
139 char *linkname;
140 char *fullname = NULL;
141 struct vfs_s_entry *target;
143 if (follow == LINK_NO_FOLLOW)
144 return entry;
145 if (follow == 0)
146 ERRNOR (ELOOP, NULL);
147 if (!entry)
148 ERRNOR (ENOENT, NULL);
149 if (!S_ISLNK (entry->ino->st.st_mode))
150 return entry;
152 linkname = entry->ino->linkname;
153 if (linkname == NULL)
154 ERRNOR (EFAULT, NULL);
156 /* make full path from relative */
157 if (*linkname != PATH_SEP)
159 char *fullpath = vfs_s_fullpath (me, entry->dir);
160 if (fullpath)
162 fullname = g_strconcat (fullpath, "/", linkname, (char *) NULL);
163 linkname = fullname;
164 g_free (fullpath);
168 target = (MEDATA->find_entry) (me, entry->dir->super->root, linkname, follow - 1, 0);
169 g_free (fullname);
170 return target;
173 /* --------------------------------------------------------------------------------------------- */
175 * Follow > 0: follow links, serves as loop protect,
176 * == -1: do not follow links
179 static struct vfs_s_entry *
180 vfs_s_find_entry_tree (struct vfs_class *me, struct vfs_s_inode *root,
181 const char *a_path, int follow, int flags)
183 size_t pseg;
184 struct vfs_s_entry *ent = NULL;
185 char *const pathref = g_strdup (a_path);
186 char *path = pathref;
188 /* canonicalize as well, but don't remove '../' from path */
189 custom_canonicalize_pathname (path, CANON_PATH_ALL & (~CANON_PATH_REMDOUBLEDOTS));
191 while (root != NULL)
193 GList *iter;
195 while (*path == PATH_SEP) /* Strip leading '/' */
196 path++;
198 if (path[0] == '\0')
200 g_free (pathref);
201 return ent;
204 for (pseg = 0; path[pseg] != '\0' && path[pseg] != PATH_SEP; pseg++)
207 for (iter = root->subdir; iter != NULL; iter = g_list_next (iter))
209 ent = (struct vfs_s_entry *) iter->data;
210 if (strlen (ent->name) == pseg && strncmp (ent->name, path, pseg) == 0)
211 /* FOUND! */
212 break;
215 ent = iter != NULL ? (struct vfs_s_entry *) iter->data : NULL;
217 if (ent == NULL && (flags & (FL_MKFILE | FL_MKDIR)) != 0)
218 ent = vfs_s_automake (me, root, path, flags);
219 if (ent == NULL)
221 me->verrno = ENOENT;
222 goto cleanup;
225 path += pseg;
226 /* here we must follow leading directories always;
227 only the actual file is optional */
228 ent = vfs_s_resolve_symlink (me, ent,
229 strchr (path, PATH_SEP) != NULL ? LINK_FOLLOW : follow);
230 if (ent == NULL)
231 goto cleanup;
232 root = ent->ino;
234 cleanup:
235 g_free (pathref);
236 return NULL;
239 /* --------------------------------------------------------------------------------------------- */
241 static struct vfs_s_entry *
242 vfs_s_find_entry_linear (struct vfs_class *me, struct vfs_s_inode *root,
243 const char *a_path, int follow, int flags)
245 struct vfs_s_entry *ent = NULL;
246 char *const path = g_strdup (a_path);
247 struct vfs_s_entry *retval = NULL;
248 GList *iter;
250 if (root->super->root != root)
251 vfs_die ("We have to use _real_ root. Always. Sorry.");
253 /* canonicalize as well, but don't remove '../' from path */
254 custom_canonicalize_pathname (path, CANON_PATH_ALL & (~CANON_PATH_REMDOUBLEDOTS));
256 if ((flags & FL_DIR) == 0)
258 char *dirname, *name;
259 struct vfs_s_inode *ino;
261 dirname = g_path_get_dirname (path);
262 name = g_path_get_basename (path);
263 ino = vfs_s_find_inode (me, root->super, dirname, follow, flags | FL_DIR);
264 retval = vfs_s_find_entry_tree (me, ino, name, follow, flags);
265 g_free (dirname);
266 g_free (name);
267 g_free (path);
268 return retval;
271 iter = g_list_find_custom (root->subdir, path, (GCompareFunc) vfs_s_entry_compare);
272 ent = iter != NULL ? (struct vfs_s_entry *) iter->data : NULL;
274 if (ent != NULL && !MEDATA->dir_uptodate (me, ent->ino))
276 #if 1
277 vfs_print_message (_("Directory cache expired for %s"), path);
278 #endif
279 vfs_s_free_entry (me, ent);
280 ent = NULL;
283 if (ent == NULL)
285 struct vfs_s_inode *ino;
287 ino = vfs_s_new_inode (me, root->super, vfs_s_default_stat (me, S_IFDIR | 0755));
288 ent = vfs_s_new_entry (me, path, ino);
289 if (MEDATA->dir_load (me, ino, path) == -1)
291 vfs_s_free_entry (me, ent);
292 g_free (path);
293 return NULL;
296 vfs_s_insert_entry (me, root, ent);
298 iter = g_list_find_custom (root->subdir, path, (GCompareFunc) vfs_s_entry_compare);
299 ent = iter != NULL ? (struct vfs_s_entry *) iter->data : NULL;
301 if (ent == NULL)
302 vfs_die ("find_linear: success but directory is not there\n");
304 #if 0
305 if (!vfs_s_resolve_symlink (me, ent, follow))
307 g_free (path);
308 return NULL;
310 #endif
311 g_free (path);
312 return ent;
315 /* --------------------------------------------------------------------------------------------- */
316 /* Ook, these were functions around directory entries / inodes */
317 /* -------------------------------- superblock games -------------------------- */
319 static struct vfs_s_super *
320 vfs_s_new_super (struct vfs_class *me)
322 struct vfs_s_super *super;
324 super = g_new0 (struct vfs_s_super, 1);
325 super->me = me;
326 return super;
329 /* --------------------------------------------------------------------------------------------- */
331 static inline void
332 vfs_s_insert_super (struct vfs_class *me, struct vfs_s_super *super)
334 MEDATA->supers = g_list_prepend (MEDATA->supers, super);
337 /* --------------------------------------------------------------------------------------------- */
339 static void
340 vfs_s_free_super (struct vfs_class *me, struct vfs_s_super *super)
342 if (super->root != NULL)
344 vfs_s_free_inode (me, super->root);
345 super->root = NULL;
348 #if 0
349 /* FIXME: We currently leak small ammount of memory, sometimes. Fix it if you can. */
350 if (super->ino_usage)
351 message (D_ERROR, "Direntry warning",
352 "Super ino_usage is %d, memory leak", super->ino_usage);
354 if (super->want_stale)
355 message (D_ERROR, "Direntry warning", "%s", "Super has want_stale set");
356 #endif
358 MEDATA->supers = g_list_remove (MEDATA->supers, super);
360 CALL (free_archive) (me, super);
361 #ifdef ENABLE_VFS_NET
362 vfs_path_element_free (super->path_element);
363 #endif
364 g_free (super->name);
365 g_free (super);
368 /* --------------------------------------------------------------------------------------------- */
369 /* Support of archives */
370 /* ------------------------ readdir & friends ----------------------------- */
372 static struct vfs_s_inode *
373 vfs_s_inode_from_path (const vfs_path_t * vpath, int flags)
375 struct vfs_s_super *super;
376 struct vfs_s_inode *ino;
377 const char *q;
378 const vfs_path_element_t *path_element;
380 q = vfs_s_get_path (vpath, &super, 0);
381 if (q == NULL)
382 return NULL;
384 path_element = vfs_path_get_by_index (vpath, -1);
386 ino =
387 vfs_s_find_inode (path_element->class, super, q,
388 flags & FL_FOLLOW ? LINK_FOLLOW : LINK_NO_FOLLOW, flags & ~FL_FOLLOW);
389 if ((!ino) && (!*q))
390 /* We are asking about / directory of ftp server: assume it exists */
391 ino =
392 vfs_s_find_inode (path_element->class, super, q,
393 flags & FL_FOLLOW ? LINK_FOLLOW :
394 LINK_NO_FOLLOW, FL_DIR | (flags & ~FL_FOLLOW));
395 return ino;
398 /* --------------------------------------------------------------------------------------------- */
400 static void *
401 vfs_s_opendir (const vfs_path_t * vpath)
403 struct vfs_s_inode *dir;
404 struct dirhandle *info;
405 const vfs_path_element_t *path_element;
407 path_element = vfs_path_get_by_index (vpath, -1);
409 dir = vfs_s_inode_from_path (vpath, FL_DIR | FL_FOLLOW);
410 if (dir == NULL)
411 return NULL;
412 if (!S_ISDIR (dir->st.st_mode))
414 path_element->class->verrno = ENOTDIR;
415 return NULL;
418 dir->st.st_nlink++;
419 #if 0
420 if (dir->subdir == NULL) /* This can actually happen if we allow empty directories */
422 path_element->class->verrno = EAGAIN;
423 return NULL;
425 #endif
426 info = g_new (struct dirhandle, 1);
427 info->cur = dir->subdir;
428 info->dir = dir;
430 return info;
433 /* --------------------------------------------------------------------------------------------- */
435 static void *
436 vfs_s_readdir (void *data)
438 static union vfs_dirent dir;
439 struct dirhandle *info = (struct dirhandle *) data;
440 const char *name;
442 if (info->cur == NULL || info->cur->data == NULL)
443 return NULL;
445 name = ((struct vfs_s_entry *) info->cur->data)->name;
446 if (name != NULL)
447 g_strlcpy (dir.dent.d_name, name, MC_MAXPATHLEN);
448 else
449 vfs_die ("Null in structure-cannot happen");
451 compute_namelen (&dir.dent);
452 info->cur = g_list_next (info->cur);
454 return (void *) &dir;
457 /* --------------------------------------------------------------------------------------------- */
459 static int
460 vfs_s_closedir (void *data)
462 struct dirhandle *info = (struct dirhandle *) data;
463 struct vfs_s_inode *dir = info->dir;
465 vfs_s_free_inode (dir->super->me, dir);
466 g_free (data);
467 return 0;
470 /* --------------------------------------------------------------------------------------------- */
472 static int
473 vfs_s_chdir (const vfs_path_t * vpath)
475 void *data;
477 data = vfs_s_opendir (vpath);
478 if (data == NULL)
479 return -1;
480 vfs_s_closedir (data);
481 return 0;
484 /* --------------------------------------------------------------------------------------------- */
485 /* --------------------------- stat and friends ---------------------------- */
487 static int
488 vfs_s_internal_stat (const vfs_path_t * vpath, struct stat *buf, int flag)
490 struct vfs_s_inode *ino;
492 ino = vfs_s_inode_from_path (vpath, flag);
493 if (ino == NULL)
494 return -1;
495 *buf = ino->st;
496 return 0;
499 /* --------------------------------------------------------------------------------------------- */
501 static int
502 vfs_s_stat (const vfs_path_t * vpath, struct stat *buf)
504 return vfs_s_internal_stat (vpath, buf, FL_FOLLOW);
507 /* --------------------------------------------------------------------------------------------- */
509 static int
510 vfs_s_lstat (const vfs_path_t * vpath, struct stat *buf)
512 return vfs_s_internal_stat (vpath, buf, FL_NONE);
515 /* --------------------------------------------------------------------------------------------- */
517 static int
518 vfs_s_fstat (void *fh, struct stat *buf)
520 *buf = FH->ino->st;
521 return 0;
524 /* --------------------------------------------------------------------------------------------- */
526 static int
527 vfs_s_readlink (const vfs_path_t * vpath, char *buf, size_t size)
529 struct vfs_s_inode *ino;
530 size_t len;
531 const vfs_path_element_t *path_element;
533 path_element = vfs_path_get_by_index (vpath, -1);
535 ino = vfs_s_inode_from_path (vpath, 0);
536 if (!ino)
537 return -1;
539 if (!S_ISLNK (ino->st.st_mode))
541 path_element->class->verrno = EINVAL;
542 return -1;
545 if (ino->linkname == NULL)
547 path_element->class->verrno = EFAULT;
548 return -1;
551 len = strlen (ino->linkname);
552 if (size < len)
553 len = size;
554 /* readlink() does not append a NUL character to buf */
555 memcpy (buf, ino->linkname, len);
556 return len;
559 /* --------------------------------------------------------------------------------------------- */
561 static ssize_t
562 vfs_s_read (void *fh, char *buffer, size_t count)
564 struct vfs_class *me = FH_SUPER->me;
566 if (FH->linear == LS_LINEAR_PREOPEN)
568 if (!MEDATA->linear_start (me, FH, FH->pos))
569 return -1;
572 if (FH->linear == LS_LINEAR_CLOSED)
573 vfs_die ("linear_start() did not set linear_state!");
575 if (FH->linear == LS_LINEAR_OPEN)
576 return MEDATA->linear_read (me, FH, buffer, count);
578 if (FH->handle != -1)
580 ssize_t n;
582 n = read (FH->handle, buffer, count);
583 if (n < 0)
584 me->verrno = errno;
585 return n;
587 vfs_die ("vfs_s_read: This should not happen\n");
588 return -1;
591 /* --------------------------------------------------------------------------------------------- */
593 static ssize_t
594 vfs_s_write (void *fh, const char *buffer, size_t count)
596 struct vfs_class *me = FH_SUPER->me;
598 if (FH->linear)
599 vfs_die ("no writing to linear files, please");
601 FH->changed = 1;
602 if (FH->handle != -1)
604 ssize_t n;
606 n = write (FH->handle, buffer, count);
607 if (n < 0)
608 me->verrno = errno;
609 return n;
611 vfs_die ("vfs_s_write: This should not happen\n");
612 return 0;
615 /* --------------------------------------------------------------------------------------------- */
617 static off_t
618 vfs_s_lseek (void *fh, off_t offset, int whence)
620 off_t size = FH->ino->st.st_size;
622 if (FH->linear == LS_LINEAR_OPEN)
623 vfs_die ("cannot lseek() after linear_read!");
625 if (FH->handle != -1)
626 { /* If we have local file opened, we want to work with it */
627 off_t retval = lseek (FH->handle, offset, whence);
628 if (retval == -1)
629 FH->ino->super->me->verrno = errno;
630 return retval;
633 switch (whence)
635 case SEEK_CUR:
636 offset += FH->pos;
637 break;
638 case SEEK_END:
639 offset += size;
640 break;
642 if (offset < 0)
643 FH->pos = 0;
644 else if (offset < size)
645 FH->pos = offset;
646 else
647 FH->pos = size;
648 return FH->pos;
651 /* --------------------------------------------------------------------------------------------- */
653 static int
654 vfs_s_close (void *fh)
656 int res = 0;
657 struct vfs_class *me = FH_SUPER->me;
659 FH_SUPER->fd_usage--;
660 if (!FH_SUPER->fd_usage)
661 vfs_stamp_create (me, FH_SUPER);
663 if (FH->linear == LS_LINEAR_OPEN)
664 MEDATA->linear_close (me, fh);
665 if (MEDATA->fh_close)
666 res = MEDATA->fh_close (me, fh);
667 if ((MEDATA->flags & VFS_S_USETMP) && FH->changed && MEDATA->file_store)
669 char *s = vfs_s_fullpath (me, FH->ino);
670 if (!s)
671 res = -1;
672 else
674 res = MEDATA->file_store (me, fh, s, FH->ino->localname);
675 g_free (s);
677 vfs_s_invalidate (me, FH_SUPER);
679 if (FH->handle != -1)
680 close (FH->handle);
682 vfs_s_free_inode (me, FH->ino);
683 if (MEDATA->fh_free_data != NULL)
684 MEDATA->fh_free_data (fh);
685 g_free (fh);
686 return res;
689 /* --------------------------------------------------------------------------------------------- */
691 static void
692 vfs_s_print_stats (const char *fs_name, const char *action,
693 const char *file_name, off_t have, off_t need)
695 static const char *i18n_percent_transf_format = NULL;
696 static const char *i18n_transf_format = NULL;
698 if (i18n_percent_transf_format == NULL)
700 i18n_percent_transf_format = "%s: %s: %s %3d%% (%" PRIuMAX " %s";
701 i18n_transf_format = "%s: %s: %s %" PRIuMAX " %s";
704 if (need)
705 vfs_print_message (i18n_percent_transf_format, fs_name, action,
706 file_name, (int) ((double) have * 100 / need), (uintmax_t) have,
707 _("bytes transferred"));
708 else
709 vfs_print_message (i18n_transf_format, fs_name, action, file_name, (uintmax_t) have,
710 _("bytes transferred"));
713 /* --------------------------------------------------------------------------------------------- */
714 /* ------------------------------- mc support ---------------------------- */
716 static void
717 vfs_s_fill_names (struct vfs_class *me, fill_names_f func)
719 GList *iter;
721 for (iter = MEDATA->supers; iter != NULL; iter = g_list_next (iter))
723 const struct vfs_s_super *super = (const struct vfs_s_super *) iter->data;
724 char *name;
726 name = g_strconcat (super->name, "/", me->prefix, VFS_PATH_URL_DELIMITER,
727 /* super->current_dir->name, */ (char *) NULL);
728 func (name);
729 g_free (name);
733 /* --------------------------------------------------------------------------------------------- */
735 static int
736 vfs_s_ferrno (struct vfs_class *me)
738 return me->verrno;
741 /* --------------------------------------------------------------------------------------------- */
743 * Get local copy of the given file. We reuse the existing file cache
744 * for remote filesystems. Archives use standard VFS facilities.
747 static vfs_path_t *
748 vfs_s_getlocalcopy (const vfs_path_t * vpath)
750 vfs_file_handler_t *fh;
751 vfs_path_t *local = NULL;
753 if (vpath == NULL)
754 return NULL;
756 fh = vfs_s_open (vpath, O_RDONLY, 0);
758 if (fh != NULL)
760 const struct vfs_class *me;
762 me = vfs_path_get_by_index (vpath, -1)->class;
763 if ((MEDATA->flags & VFS_S_USETMP) != 0 && (fh->ino != NULL))
764 local = vfs_path_from_str_flags (fh->ino->localname, VPF_NO_CANON);
766 vfs_s_close (fh);
769 return local;
772 /* --------------------------------------------------------------------------------------------- */
774 * Return the local copy. Since we are using our cache, we do nothing -
775 * the cache will be removed when the archive is closed.
778 static int
779 vfs_s_ungetlocalcopy (const vfs_path_t * vpath, const vfs_path_t * local, gboolean has_changed)
781 (void) vpath;
782 (void) local;
783 (void) has_changed;
784 return 0;
787 /* --------------------------------------------------------------------------------------------- */
789 static int
790 vfs_s_setctl (const vfs_path_t * vpath, int ctlop, void *arg)
792 const vfs_path_element_t *path_element;
794 path_element = vfs_path_get_by_index (vpath, -1);
796 switch (ctlop)
798 case VFS_SETCTL_STALE_DATA:
800 struct vfs_s_inode *ino;
802 ino = vfs_s_inode_from_path (vpath, 0);
803 if (ino == NULL)
804 return 0;
805 if (arg)
806 ino->super->want_stale = 1;
807 else
809 ino->super->want_stale = 0;
810 vfs_s_invalidate (path_element->class, ino->super);
812 return 1;
814 case VFS_SETCTL_LOGFILE:
815 ((struct vfs_s_subclass *) path_element->class->data)->logfile = fopen ((char *) arg, "w");
816 return 1;
817 case VFS_SETCTL_FLUSH:
818 ((struct vfs_s_subclass *) path_element->class->data)->flush = 1;
819 return 1;
821 return 0;
824 /* --------------------------------------------------------------------------------------------- */
825 /* ----------------------------- Stamping support -------------------------- */
827 static vfsid
828 vfs_s_getid (const vfs_path_t * vpath)
830 struct vfs_s_super *archive = NULL;
831 const char *p;
833 p = vfs_s_get_path (vpath, &archive, FL_NO_OPEN);
834 if (p == NULL)
835 return NULL;
837 return (vfsid) archive;
840 /* --------------------------------------------------------------------------------------------- */
842 static int
843 vfs_s_nothingisopen (vfsid id)
845 (void) id;
846 /* Our data structures should survive free of superblock at any time */
847 return 1;
850 /* --------------------------------------------------------------------------------------------- */
852 static void
853 vfs_s_free (vfsid id)
855 vfs_s_free_super (((struct vfs_s_super *) id)->me, (struct vfs_s_super *) id);
858 /* --------------------------------------------------------------------------------------------- */
860 static int
861 vfs_s_dir_uptodate (struct vfs_class *me, struct vfs_s_inode *ino)
863 struct timeval tim;
865 if (MEDATA->flush)
867 MEDATA->flush = 0;
868 return 0;
871 gettimeofday (&tim, NULL);
872 if (tim.tv_sec < ino->timestamp.tv_sec)
873 return 1;
874 return 0;
878 /* --------------------------------------------------------------------------------------------- */
879 /*** public functions ****************************************************************************/
880 /* --------------------------------------------------------------------------------------------- */
882 struct vfs_s_inode *
883 vfs_s_new_inode (struct vfs_class *me, struct vfs_s_super *super, struct stat *initstat)
885 struct vfs_s_inode *ino;
887 ino = g_try_new0 (struct vfs_s_inode, 1);
888 if (ino == NULL)
889 return NULL;
891 if (initstat)
892 ino->st = *initstat;
893 ino->super = super;
894 ino->st.st_nlink = 0;
895 ino->st.st_ino = MEDATA->inode_counter++;
896 ino->st.st_dev = MEDATA->rdev;
898 super->ino_usage++;
899 total_inodes++;
901 CALL (init_inode) (me, ino);
903 return ino;
906 /* --------------------------------------------------------------------------------------------- */
908 void
909 vfs_s_free_inode (struct vfs_class *me, struct vfs_s_inode *ino)
911 if (ino == NULL)
912 vfs_die ("Don't pass NULL to me");
914 /* ==0 can happen if freshly created entry is deleted */
915 if (ino->st.st_nlink > 1)
917 ino->st.st_nlink--;
918 return;
921 while (ino->subdir != NULL)
922 vfs_s_free_entry (me, (struct vfs_s_entry *) ino->subdir->data);
924 CALL (free_inode) (me, ino);
925 g_free (ino->linkname);
926 if ((MEDATA->flags & VFS_S_USETMP) != 0 && ino->localname != NULL)
928 unlink (ino->localname);
929 g_free (ino->localname);
931 total_inodes--;
932 ino->super->ino_usage--;
933 g_free (ino);
936 /* --------------------------------------------------------------------------------------------- */
938 struct vfs_s_entry *
939 vfs_s_new_entry (struct vfs_class *me, const char *name, struct vfs_s_inode *inode)
941 struct vfs_s_entry *entry;
943 entry = g_new0 (struct vfs_s_entry, 1);
944 total_entries++;
946 entry->name = g_strdup (name);
947 entry->ino = inode;
948 entry->ino->ent = entry;
949 CALL (init_entry) (me, entry);
951 return entry;
955 /* --------------------------------------------------------------------------------------------- */
957 void
958 vfs_s_free_entry (struct vfs_class *me, struct vfs_s_entry *ent)
960 if (ent->dir != NULL)
961 ent->dir->subdir = g_list_remove (ent->dir->subdir, ent);
963 g_free (ent->name);
964 /* ent->name = NULL; */
966 if (ent->ino != NULL)
968 ent->ino->ent = NULL;
969 vfs_s_free_inode (me, ent->ino);
972 total_entries--;
973 g_free (ent);
976 /* --------------------------------------------------------------------------------------------- */
978 void
979 vfs_s_insert_entry (struct vfs_class *me, struct vfs_s_inode *dir, struct vfs_s_entry *ent)
981 (void) me;
983 ent->dir = dir;
985 ent->ino->st.st_nlink++;
986 dir->subdir = g_list_append (dir->subdir, ent);
989 /* --------------------------------------------------------------------------------------------- */
991 struct stat *
992 vfs_s_default_stat (struct vfs_class *me, mode_t mode)
994 static struct stat st;
995 int myumask;
997 (void) me;
999 myumask = umask (022);
1000 umask (myumask);
1001 mode &= ~myumask;
1003 st.st_mode = mode;
1004 st.st_ino = 0;
1005 st.st_dev = 0;
1006 st.st_rdev = 0;
1007 st.st_uid = getuid ();
1008 st.st_gid = getgid ();
1009 st.st_size = 0;
1010 st.st_mtime = st.st_atime = st.st_ctime = time (NULL);
1012 return &st;
1015 /* --------------------------------------------------------------------------------------------- */
1017 struct vfs_s_entry *
1018 vfs_s_generate_entry (struct vfs_class *me, const char *name, struct vfs_s_inode *parent,
1019 mode_t mode)
1021 struct vfs_s_inode *inode;
1022 struct stat *st;
1024 st = vfs_s_default_stat (me, mode);
1025 inode = vfs_s_new_inode (me, parent->super, st);
1027 return vfs_s_new_entry (me, name, inode);
1030 /* --------------------------------------------------------------------------------------------- */
1032 struct vfs_s_inode *
1033 vfs_s_find_inode (struct vfs_class *me, const struct vfs_s_super *super,
1034 const char *path, int follow, int flags)
1036 struct vfs_s_entry *ent;
1038 if (((MEDATA->flags & VFS_S_REMOTE) == 0) && (*path == '\0'))
1039 return super->root;
1041 ent = (MEDATA->find_entry) (me, super->root, path, follow, flags);
1042 return (ent != NULL) ? ent->ino : NULL;
1045 /* --------------------------------------------------------------------------------------------- */
1046 /* Ook, these were functions around directory entries / inodes */
1047 /* -------------------------------- superblock games -------------------------- */
1049 * get superlock object by vpath
1051 * @param vpath path
1052 * @return superlock object or NULL if not found
1055 struct vfs_s_super *
1056 vfs_get_super_by_vpath (const vfs_path_t * vpath)
1058 GList *iter;
1059 void *cookie = NULL;
1060 const vfs_path_element_t *path_element;
1061 struct vfs_s_subclass *subclass;
1062 struct vfs_s_super *super = NULL;
1063 vfs_path_t *vpath_archive;
1065 path_element = vfs_path_get_by_index (vpath, -1);
1066 subclass = ((struct vfs_s_subclass *) path_element->class->data);
1067 if (subclass == NULL)
1068 return NULL;
1070 vpath_archive = vfs_path_clone (vpath);
1071 vfs_path_remove_element_by_index (vpath_archive, -1);
1073 if (subclass->archive_check != NULL)
1075 cookie = subclass->archive_check (vpath_archive);
1076 if (cookie == NULL)
1077 goto ret;
1080 for (iter = subclass->supers; iter != NULL; iter = g_list_next (iter))
1082 int i;
1084 super = (struct vfs_s_super *) iter->data;
1086 /* 0 == other, 1 == same, return it, 2 == other but stop scanning */
1087 i = subclass->archive_same (path_element, super, vpath_archive, cookie);
1088 if (i == 1)
1089 goto ret;
1090 if (i != 0)
1091 break;
1093 super = NULL;
1096 ret:
1097 vfs_path_free (vpath_archive);
1098 return super;
1101 /* --------------------------------------------------------------------------------------------- */
1103 * get path from last VFS-element and create corresponding superblock
1105 * @param vpath source path object
1106 * @param archive pointer to object for store newly created superblock
1107 * @param flags flags
1109 * @return path from last VFS-element
1111 const char *
1112 vfs_s_get_path (const vfs_path_t * vpath, struct vfs_s_super **archive, int flags)
1114 const char *retval = "";
1115 int result = -1;
1116 struct vfs_s_super *super;
1117 const vfs_path_element_t *path_element;
1118 struct vfs_s_subclass *subclass;
1120 path_element = vfs_path_get_by_index (vpath, -1);
1122 if (path_element->path != NULL)
1123 retval = path_element->path;
1125 super = vfs_get_super_by_vpath (vpath);
1126 if (super != NULL)
1127 goto return_success;
1129 if (flags & FL_NO_OPEN)
1131 path_element->class->verrno = EIO;
1132 return NULL;
1135 super = vfs_s_new_super (path_element->class);
1137 subclass = ((struct vfs_s_subclass *) path_element->class->data);
1138 if (subclass->open_archive != NULL)
1140 vfs_path_t *vpath_archive;
1142 vpath_archive = vfs_path_clone (vpath);
1143 vfs_path_remove_element_by_index (vpath_archive, -1);
1145 result = subclass->open_archive (super, vpath_archive, path_element);
1146 vfs_path_free (vpath_archive);
1148 if (result == -1)
1150 vfs_s_free_super (path_element->class, super);
1151 path_element->class->verrno = EIO;
1152 return NULL;
1154 if (!super->name)
1155 vfs_die ("You have to fill name\n");
1156 if (!super->root)
1157 vfs_die ("You have to fill root inode\n");
1159 vfs_s_insert_super (path_element->class, super);
1160 vfs_stamp_create (path_element->class, super);
1162 return_success:
1163 *archive = super;
1164 return retval;
1167 /* --------------------------------------------------------------------------------------------- */
1169 void
1170 vfs_s_invalidate (struct vfs_class *me, struct vfs_s_super *super)
1172 if (!super->want_stale)
1174 vfs_s_free_inode (me, super->root);
1175 super->root = vfs_s_new_inode (me, super, vfs_s_default_stat (me, S_IFDIR | 0755));
1179 /* --------------------------------------------------------------------------------------------- */
1181 char *
1182 vfs_s_fullpath (struct vfs_class *me, struct vfs_s_inode *ino)
1184 if (!ino->ent)
1185 ERRNOR (EAGAIN, NULL);
1187 if ((MEDATA->flags & VFS_S_USETMP) == 0)
1189 /* archives */
1190 char *newpath;
1191 char *path = g_strdup (ino->ent->name);
1192 while (1)
1194 ino = ino->ent->dir;
1195 if (ino == ino->super->root)
1196 break;
1197 newpath = g_strconcat (ino->ent->name, "/", path, (char *) NULL);
1198 g_free (path);
1199 path = newpath;
1201 return path;
1204 /* remote systems */
1205 if ((!ino->ent->dir) || (!ino->ent->dir->ent))
1206 return g_strdup (ino->ent->name);
1208 return g_strconcat (ino->ent->dir->ent->name, PATH_SEP_STR, ino->ent->name, (char *) NULL);
1211 /* --------------------------------------------------------------------------------------------- */
1212 /* --------------------------- stat and friends ---------------------------- */
1214 void *
1215 vfs_s_open (const vfs_path_t * vpath, int flags, mode_t mode)
1217 int was_changed = 0;
1218 vfs_file_handler_t *fh;
1219 struct vfs_s_super *super;
1220 const char *q;
1221 struct vfs_s_inode *ino;
1222 const vfs_path_element_t *path_element;
1224 path_element = vfs_path_get_by_index (vpath, -1);
1226 q = vfs_s_get_path (vpath, &super, 0);
1227 if (q == NULL)
1228 return NULL;
1229 ino = vfs_s_find_inode (path_element->class, super, q, LINK_FOLLOW, FL_NONE);
1230 if (ino && ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL)))
1232 path_element->class->verrno = EEXIST;
1233 return NULL;
1235 if (!ino)
1237 char *dirname, *name;
1238 struct vfs_s_entry *ent;
1239 struct vfs_s_inode *dir;
1240 int tmp_handle;
1242 /* If the filesystem is read-only, disable file creation */
1243 if (!(flags & O_CREAT) || !(path_element->class->write))
1244 return NULL;
1246 dirname = g_path_get_dirname (q);
1247 name = g_path_get_basename (q);
1248 dir = vfs_s_find_inode (path_element->class, super, dirname, LINK_FOLLOW, FL_DIR);
1249 if (dir == NULL)
1251 g_free (dirname);
1252 g_free (name);
1253 return NULL;
1255 ent = vfs_s_generate_entry (path_element->class, name, dir, 0755);
1256 ino = ent->ino;
1257 vfs_s_insert_entry (path_element->class, dir, ent);
1258 if ((VFSDATA (path_element)->flags & VFS_S_USETMP) != 0)
1260 vfs_path_t *tmp_vpath;
1262 tmp_handle = vfs_mkstemps (&tmp_vpath, path_element->class->name, name);
1263 ino->localname = vfs_path_to_str (tmp_vpath);
1264 vfs_path_free (tmp_vpath);
1265 if (tmp_handle == -1)
1267 g_free (dirname);
1268 g_free (name);
1269 return NULL;
1272 close (tmp_handle);
1274 g_free (dirname);
1275 g_free (name);
1276 was_changed = 1;
1279 if (S_ISDIR (ino->st.st_mode))
1281 path_element->class->verrno = EISDIR;
1282 return NULL;
1285 fh = g_new (vfs_file_handler_t, 1);
1286 fh->pos = 0;
1287 fh->ino = ino;
1288 fh->handle = -1;
1289 fh->changed = was_changed;
1290 fh->linear = 0;
1291 fh->data = NULL;
1293 if (IS_LINEAR (flags))
1295 if (VFSDATA (path_element)->linear_start)
1297 vfs_print_message (_("Starting linear transfer..."));
1298 fh->linear = LS_LINEAR_PREOPEN;
1301 else
1303 struct vfs_s_subclass *s;
1305 s = VFSDATA (path_element);
1306 if (s->fh_open != NULL && s->fh_open (path_element->class, fh, flags, mode) != 0)
1308 if (s->fh_free_data != NULL)
1309 s->fh_free_data (fh);
1310 g_free (fh);
1311 return NULL;
1315 if ((VFSDATA (path_element)->flags & VFS_S_USETMP) != 0 && fh->ino->localname != NULL)
1317 fh->handle = open (fh->ino->localname, NO_LINEAR (flags), mode);
1318 if (fh->handle == -1)
1320 g_free (fh);
1321 path_element->class->verrno = errno;
1322 return NULL;
1326 /* i.e. we had no open files and now we have one */
1327 vfs_rmstamp (path_element->class, (vfsid) super);
1328 super->fd_usage++;
1329 fh->ino->st.st_nlink++;
1330 return fh;
1333 /* --------------------------------------------------------------------------------------------- */
1336 vfs_s_retrieve_file (struct vfs_class *me, struct vfs_s_inode *ino)
1338 /* If you want reget, you'll have to open file with O_LINEAR */
1339 off_t total = 0;
1340 char buffer[8192];
1341 int handle, n;
1342 off_t stat_size = ino->st.st_size;
1343 vfs_file_handler_t fh;
1344 vfs_path_t *tmp_vpath;
1346 if ((MEDATA->flags & VFS_S_USETMP) == 0)
1347 return -1;
1349 memset (&fh, 0, sizeof (fh));
1351 fh.ino = ino;
1352 fh.handle = -1;
1354 handle = vfs_mkstemps (&tmp_vpath, me->name, ino->ent->name);
1355 ino->localname = vfs_path_to_str (tmp_vpath);
1356 vfs_path_free (tmp_vpath);
1357 if (handle == -1)
1359 me->verrno = errno;
1360 goto error_4;
1363 if (!MEDATA->linear_start (me, &fh, 0))
1364 goto error_3;
1366 /* Clear the interrupt status */
1367 tty_got_interrupt ();
1368 tty_enable_interrupt_key ();
1370 while ((n = MEDATA->linear_read (me, &fh, buffer, sizeof (buffer))))
1372 int t;
1373 if (n < 0)
1374 goto error_1;
1376 total += n;
1377 vfs_s_print_stats (me->name, _("Getting file"), ino->ent->name, total, stat_size);
1379 if (tty_got_interrupt ())
1380 goto error_1;
1382 t = write (handle, buffer, n);
1383 if (t != n)
1385 if (t == -1)
1386 me->verrno = errno;
1387 goto error_1;
1390 MEDATA->linear_close (me, &fh);
1391 close (handle);
1393 tty_disable_interrupt_key ();
1394 g_free (fh.data);
1395 return 0;
1397 error_1:
1398 MEDATA->linear_close (me, &fh);
1399 error_3:
1400 tty_disable_interrupt_key ();
1401 close (handle);
1402 unlink (ino->localname);
1403 error_4:
1404 g_free (ino->localname);
1405 ino->localname = NULL;
1406 g_free (fh.data);
1407 return -1;
1410 /* --------------------------------------------------------------------------------------------- */
1411 /* ----------------------------- Stamping support -------------------------- */
1413 /* Initialize one of our subclasses - fill common functions */
1414 void
1415 vfs_s_init_class (struct vfs_class *vclass, struct vfs_s_subclass *sub)
1417 vclass->data = sub;
1418 vclass->fill_names = vfs_s_fill_names;
1419 vclass->open = vfs_s_open;
1420 vclass->close = vfs_s_close;
1421 vclass->read = vfs_s_read;
1422 if (!(sub->flags & VFS_S_READONLY))
1424 vclass->write = vfs_s_write;
1426 vclass->opendir = vfs_s_opendir;
1427 vclass->readdir = vfs_s_readdir;
1428 vclass->closedir = vfs_s_closedir;
1429 vclass->stat = vfs_s_stat;
1430 vclass->lstat = vfs_s_lstat;
1431 vclass->fstat = vfs_s_fstat;
1432 vclass->readlink = vfs_s_readlink;
1433 vclass->chdir = vfs_s_chdir;
1434 vclass->ferrno = vfs_s_ferrno;
1435 vclass->lseek = vfs_s_lseek;
1436 vclass->getid = vfs_s_getid;
1437 vclass->nothingisopen = vfs_s_nothingisopen;
1438 vclass->free = vfs_s_free;
1439 if ((sub->flags & VFS_S_USETMP) != 0)
1441 vclass->getlocalcopy = vfs_s_getlocalcopy;
1442 vclass->ungetlocalcopy = vfs_s_ungetlocalcopy;
1443 sub->find_entry = vfs_s_find_entry_linear;
1445 else if ((sub->flags & VFS_S_REMOTE) != 0)
1446 sub->find_entry = vfs_s_find_entry_linear;
1447 else
1448 sub->find_entry = vfs_s_find_entry_tree;
1449 vclass->setctl = vfs_s_setctl;
1450 sub->dir_uptodate = vfs_s_dir_uptodate;
1453 /* --------------------------------------------------------------------------------------------- */
1454 /** Find VFS id for given directory name */
1456 vfsid
1457 vfs_getid (const vfs_path_t * vpath)
1459 const vfs_path_element_t *path_element;
1461 path_element = vfs_path_get_by_index (vpath, -1);
1462 if (!vfs_path_element_valid (path_element) || path_element->class->getid == NULL)
1463 return NULL;
1465 return (*path_element->class->getid) (vpath);
1468 /* --------------------------------------------------------------------------------------------- */
1469 /* ----------- Utility functions for networked filesystems -------------- */
1471 #ifdef ENABLE_VFS_NET
1473 vfs_s_select_on_two (int fd1, int fd2)
1475 fd_set set;
1476 struct timeval time_out;
1477 int v;
1478 int maxfd = (fd1 > fd2 ? fd1 : fd2) + 1;
1480 time_out.tv_sec = 1;
1481 time_out.tv_usec = 0;
1482 FD_ZERO (&set);
1483 FD_SET (fd1, &set);
1484 FD_SET (fd2, &set);
1485 v = select (maxfd, &set, 0, 0, &time_out);
1486 if (v <= 0)
1487 return v;
1488 if (FD_ISSET (fd1, &set))
1489 return 1;
1490 if (FD_ISSET (fd2, &set))
1491 return 2;
1492 return -1;
1495 /* --------------------------------------------------------------------------------------------- */
1498 vfs_s_get_line (struct vfs_class *me, int sock, char *buf, int buf_len, char term)
1500 FILE *logfile = MEDATA->logfile;
1501 int i;
1502 char c;
1504 for (i = 0; i < buf_len - 1; i++, buf++)
1506 if (read (sock, buf, sizeof (char)) <= 0)
1507 return 0;
1508 if (logfile)
1510 size_t ret1;
1511 int ret2;
1512 ret1 = fwrite (buf, 1, 1, logfile);
1513 ret2 = fflush (logfile);
1515 if (*buf == term)
1517 *buf = 0;
1518 return 1;
1522 /* Line is too long - terminate buffer and discard the rest of line */
1523 *buf = 0;
1524 while (read (sock, &c, sizeof (c)) > 0)
1526 if (logfile)
1528 size_t ret1;
1529 int ret2;
1530 ret1 = fwrite (&c, 1, 1, logfile);
1531 ret2 = fflush (logfile);
1533 if (c == '\n')
1534 return 1;
1536 return 0;
1539 /* --------------------------------------------------------------------------------------------- */
1542 vfs_s_get_line_interruptible (struct vfs_class *me, char *buffer, int size, int fd)
1544 int n;
1545 int i;
1547 (void) me;
1549 tty_enable_interrupt_key ();
1550 for (i = 0; i < size - 1; i++)
1552 n = read (fd, buffer + i, 1);
1553 tty_disable_interrupt_key ();
1554 if (n == -1 && errno == EINTR)
1556 buffer[i] = 0;
1557 return EINTR;
1559 if (n == 0)
1561 buffer[i] = 0;
1562 return 0;
1564 if (buffer[i] == '\n')
1566 buffer[i] = 0;
1567 return 1;
1570 buffer[size - 1] = 0;
1571 return 0;
1573 #endif /* ENABLE_VFS_NET */
1575 /* --------------------------------------------------------------------------------------------- */
1577 * Normalize filenames start position
1580 void
1581 vfs_s_normalize_filename_leading_spaces (struct vfs_s_inode *root_inode, size_t final_num_spaces)
1583 GList *iter;
1585 for (iter = root_inode->subdir; iter != NULL; iter = g_list_next (iter))
1587 struct vfs_s_entry *entry = (struct vfs_s_entry *) iter->data;
1588 if ((size_t) entry->ino->data_offset > final_num_spaces)
1590 char *source_name = entry->name;
1591 char *spacer = g_strnfill (entry->ino->data_offset - final_num_spaces, ' ');
1592 entry->name = g_strdup_printf ("%s%s", spacer, source_name);
1593 g_free (spacer);
1594 g_free (source_name);
1596 entry->ino->data_offset = -1;
1600 /* --------------------------------------------------------------------------------------------- */