Ticket 1551: Update GPL version from 2 to 3
[midnight-commander.git] / lib / vfs / direntry.c
blob9af9084a8590c068fcaca7841f5985fd356a9e30
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" /* concat_dir_and_file */
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 static void
113 vfs_s_free_inode (struct vfs_class *me, struct vfs_s_inode *ino)
115 if (ino == NULL)
116 vfs_die ("Don't pass NULL to me");
118 /* ==0 can happen if freshly created entry is deleted */
119 if (ino->st.st_nlink > 1)
121 ino->st.st_nlink--;
122 return;
125 while (ino->subdir != NULL)
126 vfs_s_free_entry (me, (struct vfs_s_entry *) ino->subdir->data);
128 CALL (free_inode) (me, ino);
129 g_free (ino->linkname);
130 if (ino->localname != NULL)
132 unlink (ino->localname);
133 g_free (ino->localname);
135 total_inodes--;
136 ino->super->ino_usage--;
137 g_free (ino);
140 /* --------------------------------------------------------------------------------------------- */
141 /* We were asked to create entries automagically */
143 static struct vfs_s_entry *
144 vfs_s_automake (struct vfs_class *me, struct vfs_s_inode *dir, char *path, int flags)
146 struct vfs_s_entry *res;
147 char *sep;
149 sep = strchr (path, PATH_SEP);
150 if (sep != NULL)
151 *sep = '\0';
153 res = vfs_s_generate_entry (me, path, dir, flags & FL_MKDIR ? (0777 | S_IFDIR) : 0777);
154 vfs_s_insert_entry (me, dir, res);
156 if (sep != NULL)
157 *sep = PATH_SEP;
159 return res;
162 /* --------------------------------------------------------------------------------------------- */
163 /* If the entry is a symlink, find the entry for its target */
165 static struct vfs_s_entry *
166 vfs_s_resolve_symlink (struct vfs_class *me, struct vfs_s_entry *entry, int follow)
168 char *linkname;
169 char *fullname = NULL;
170 struct vfs_s_entry *target;
172 if (follow == LINK_NO_FOLLOW)
173 return entry;
174 if (follow == 0)
175 ERRNOR (ELOOP, NULL);
176 if (!entry)
177 ERRNOR (ENOENT, NULL);
178 if (!S_ISLNK (entry->ino->st.st_mode))
179 return entry;
181 linkname = entry->ino->linkname;
182 if (linkname == NULL)
183 ERRNOR (EFAULT, NULL);
185 /* make full path from relative */
186 if (*linkname != PATH_SEP)
188 char *fullpath = vfs_s_fullpath (me, entry->dir);
189 if (fullpath)
191 fullname = g_strconcat (fullpath, "/", linkname, (char *) NULL);
192 linkname = fullname;
193 g_free (fullpath);
197 target = (MEDATA->find_entry) (me, entry->dir->super->root, linkname, follow - 1, 0);
198 g_free (fullname);
199 return target;
202 /* --------------------------------------------------------------------------------------------- */
204 * Follow > 0: follow links, serves as loop protect,
205 * == -1: do not follow links
208 static struct vfs_s_entry *
209 vfs_s_find_entry_tree (struct vfs_class *me, struct vfs_s_inode *root,
210 const char *a_path, int follow, int flags)
212 size_t pseg;
213 struct vfs_s_entry *ent = NULL;
214 char *const pathref = g_strdup (a_path);
215 char *path = pathref;
217 /* canonicalize as well, but don't remove '../' from path */
218 custom_canonicalize_pathname (path, CANON_PATH_ALL & (~CANON_PATH_REMDOUBLEDOTS));
220 while (root != NULL)
222 GList *iter;
224 while (*path == PATH_SEP) /* Strip leading '/' */
225 path++;
227 if (path[0] == '\0')
229 g_free (pathref);
230 return ent;
233 for (pseg = 0; path[pseg] != '\0' && path[pseg] != PATH_SEP; pseg++)
236 for (iter = root->subdir; iter != NULL; iter = g_list_next (iter))
238 ent = (struct vfs_s_entry *) iter->data;
239 if (strlen (ent->name) == pseg && strncmp (ent->name, path, pseg) == 0)
240 /* FOUND! */
241 break;
244 ent = iter != NULL ? (struct vfs_s_entry *) iter->data : NULL;
246 if (ent == NULL && (flags & (FL_MKFILE | FL_MKDIR)) != 0)
247 ent = vfs_s_automake (me, root, path, flags);
248 if (ent == NULL)
250 me->verrno = ENOENT;
251 goto cleanup;
254 path += pseg;
255 /* here we must follow leading directories always;
256 only the actual file is optional */
257 ent = vfs_s_resolve_symlink (me, ent,
258 strchr (path, PATH_SEP) != NULL ? LINK_FOLLOW : follow);
259 if (ent == NULL)
260 goto cleanup;
261 root = ent->ino;
263 cleanup:
264 g_free (pathref);
265 return NULL;
268 /* --------------------------------------------------------------------------------------------- */
270 static void
271 split_dir_name (struct vfs_class *me, char *path, char **dir, char **name, char **save)
273 char *s;
275 (void) me;
277 s = strrchr (path, PATH_SEP);
278 if (s == NULL)
280 *save = NULL;
281 *name = path;
282 *dir = path + strlen (path); /* an empty string */
284 else
286 *save = s;
287 *dir = path;
288 *s++ = '\0';
289 *name = s;
293 /* --------------------------------------------------------------------------------------------- */
295 static struct vfs_s_entry *
296 vfs_s_find_entry_linear (struct vfs_class *me, struct vfs_s_inode *root,
297 const char *a_path, int follow, int flags)
299 struct vfs_s_entry *ent = NULL;
300 char *const path = g_strdup (a_path);
301 struct vfs_s_entry *retval = NULL;
302 GList *iter;
304 if (root->super->root != root)
305 vfs_die ("We have to use _real_ root. Always. Sorry.");
307 /* canonicalize as well, but don't remove '../' from path */
308 custom_canonicalize_pathname (path, CANON_PATH_ALL & (~CANON_PATH_REMDOUBLEDOTS));
310 if ((flags & FL_DIR) == 0)
312 char *dirname, *name, *save;
313 struct vfs_s_inode *ino;
314 split_dir_name (me, path, &dirname, &name, &save);
315 ino = vfs_s_find_inode (me, root->super, dirname, follow, flags | FL_DIR);
316 if (save != NULL)
317 *save = PATH_SEP;
318 retval = vfs_s_find_entry_tree (me, ino, name, follow, flags);
319 g_free (path);
320 return retval;
323 iter = g_list_find_custom (root->subdir, path, (GCompareFunc) vfs_s_entry_compare);
324 ent = iter != NULL ? (struct vfs_s_entry *) iter->data : NULL;
326 if (ent != NULL && !MEDATA->dir_uptodate (me, ent->ino))
328 #if 1
329 vfs_print_message (_("Directory cache expired for %s"), path);
330 #endif
331 vfs_s_free_entry (me, ent);
332 ent = NULL;
335 if (ent == NULL)
337 struct vfs_s_inode *ino;
339 ino = vfs_s_new_inode (me, root->super, vfs_s_default_stat (me, S_IFDIR | 0755));
340 ent = vfs_s_new_entry (me, path, ino);
341 if (MEDATA->dir_load (me, ino, path) == -1)
343 vfs_s_free_entry (me, ent);
344 g_free (path);
345 return NULL;
348 vfs_s_insert_entry (me, root, ent);
350 iter = g_list_find_custom (root->subdir, path, (GCompareFunc) vfs_s_entry_compare);
351 ent = iter != NULL ? (struct vfs_s_entry *) iter->data : NULL;
353 if (ent == NULL)
354 vfs_die ("find_linear: success but directory is not there\n");
356 #if 0
357 if (!vfs_s_resolve_symlink (me, ent, follow))
359 g_free (path);
360 return NULL;
362 #endif
363 g_free (path);
364 return ent;
367 /* --------------------------------------------------------------------------------------------- */
368 /* Ook, these were functions around directory entries / inodes */
369 /* -------------------------------- superblock games -------------------------- */
371 static struct vfs_s_super *
372 vfs_s_new_super (struct vfs_class *me)
374 struct vfs_s_super *super;
376 super = g_new0 (struct vfs_s_super, 1);
377 super->me = me;
378 return super;
381 /* --------------------------------------------------------------------------------------------- */
383 static inline void
384 vfs_s_insert_super (struct vfs_class *me, struct vfs_s_super *super)
386 MEDATA->supers = g_list_prepend (MEDATA->supers, super);
389 /* --------------------------------------------------------------------------------------------- */
391 static void
392 vfs_s_free_super (struct vfs_class *me, struct vfs_s_super *super)
394 if (super->root != NULL)
396 vfs_s_free_inode (me, super->root);
397 super->root = NULL;
400 #if 0
401 /* FIXME: We currently leak small ammount of memory, sometimes. Fix it if you can. */
402 if (super->ino_usage)
403 message (D_ERROR, "Direntry warning",
404 "Super ino_usage is %d, memory leak", super->ino_usage);
406 if (super->want_stale)
407 message (D_ERROR, "Direntry warning", "%s", "Super has want_stale set");
408 #endif
410 MEDATA->supers = g_list_remove (MEDATA->supers, super);
412 CALL (free_archive) (me, super);
413 #ifdef ENABLE_VFS_NET
414 vfs_path_element_free (super->path_element);
415 #endif
416 g_free (super->name);
417 g_free (super);
420 /* --------------------------------------------------------------------------------------------- */
421 /* Support of archives */
422 /* ------------------------ readdir & friends ----------------------------- */
424 static struct vfs_s_inode *
425 vfs_s_inode_from_path (const vfs_path_t * vpath, int flags)
427 struct vfs_s_super *super;
428 struct vfs_s_inode *ino;
429 const char *q;
430 vfs_path_element_t *path_element;
432 q = vfs_s_get_path (vpath, &super, 0);
433 if (q == NULL)
434 return NULL;
436 path_element = vfs_path_get_by_index (vpath, -1);
438 ino =
439 vfs_s_find_inode (path_element->class, super, q,
440 flags & FL_FOLLOW ? LINK_FOLLOW : LINK_NO_FOLLOW, flags & ~FL_FOLLOW);
441 if ((!ino) && (!*q))
442 /* We are asking about / directory of ftp server: assume it exists */
443 ino =
444 vfs_s_find_inode (path_element->class, super, q,
445 flags & FL_FOLLOW ? LINK_FOLLOW :
446 LINK_NO_FOLLOW, FL_DIR | (flags & ~FL_FOLLOW));
447 return ino;
450 /* --------------------------------------------------------------------------------------------- */
452 static void *
453 vfs_s_opendir (const vfs_path_t * vpath)
455 struct vfs_s_inode *dir;
456 struct dirhandle *info;
457 vfs_path_element_t *path_element;
459 path_element = vfs_path_get_by_index (vpath, -1);
461 dir = vfs_s_inode_from_path (vpath, FL_DIR | FL_FOLLOW);
462 if (dir == NULL)
463 return NULL;
464 if (!S_ISDIR (dir->st.st_mode))
466 path_element->class->verrno = ENOTDIR;
467 return NULL;
470 dir->st.st_nlink++;
471 #if 0
472 if (dir->subdir == NULL) /* This can actually happen if we allow empty directories */
474 path_element->class->verrno = EAGAIN;
475 return NULL;
477 #endif
478 info = g_new (struct dirhandle, 1);
479 info->cur = dir->subdir;
480 info->dir = dir;
482 return info;
485 /* --------------------------------------------------------------------------------------------- */
487 static void *
488 vfs_s_readdir (void *data)
490 static union vfs_dirent dir;
491 struct dirhandle *info = (struct dirhandle *) data;
492 const char *name;
494 if (info->cur == NULL || info->cur->data == NULL)
495 return NULL;
497 name = ((struct vfs_s_entry *) info->cur->data)->name;
498 if (name != NULL)
499 g_strlcpy (dir.dent.d_name, name, MC_MAXPATHLEN);
500 else
501 vfs_die ("Null in structure-cannot happen");
503 compute_namelen (&dir.dent);
504 info->cur = g_list_next (info->cur);
506 return (void *) &dir;
509 /* --------------------------------------------------------------------------------------------- */
511 static int
512 vfs_s_closedir (void *data)
514 struct dirhandle *info = (struct dirhandle *) data;
515 struct vfs_s_inode *dir = info->dir;
517 vfs_s_free_inode (dir->super->me, dir);
518 g_free (data);
519 return 0;
522 /* --------------------------------------------------------------------------------------------- */
524 static int
525 vfs_s_chdir (const vfs_path_t * vpath)
527 void *data;
529 data = vfs_s_opendir (vpath);
530 if (data == NULL)
531 return -1;
532 vfs_s_closedir (data);
533 return 0;
536 /* --------------------------------------------------------------------------------------------- */
537 /* --------------------------- stat and friends ---------------------------- */
539 static int
540 vfs_s_internal_stat (const vfs_path_t * vpath, struct stat *buf, int flag)
542 struct vfs_s_inode *ino;
544 ino = vfs_s_inode_from_path (vpath, flag);
545 if (ino == NULL)
546 return -1;
547 *buf = ino->st;
548 return 0;
551 /* --------------------------------------------------------------------------------------------- */
553 static int
554 vfs_s_stat (const vfs_path_t * vpath, struct stat *buf)
556 return vfs_s_internal_stat (vpath, buf, FL_FOLLOW);
559 /* --------------------------------------------------------------------------------------------- */
561 static int
562 vfs_s_lstat (const vfs_path_t * vpath, struct stat *buf)
564 return vfs_s_internal_stat (vpath, buf, FL_NONE);
567 /* --------------------------------------------------------------------------------------------- */
569 static int
570 vfs_s_fstat (void *fh, struct stat *buf)
572 *buf = FH->ino->st;
573 return 0;
576 /* --------------------------------------------------------------------------------------------- */
578 static int
579 vfs_s_readlink (const vfs_path_t * vpath, char *buf, size_t size)
581 struct vfs_s_inode *ino;
582 size_t len;
583 vfs_path_element_t *path_element;
585 path_element = vfs_path_get_by_index (vpath, -1);
587 ino = vfs_s_inode_from_path (vpath, 0);
588 if (!ino)
589 return -1;
591 if (!S_ISLNK (ino->st.st_mode))
593 path_element->class->verrno = EINVAL;
594 return -1;
597 if (ino->linkname == NULL)
599 path_element->class->verrno = EFAULT;
600 return -1;
603 len = strlen (ino->linkname);
604 if (size < len)
605 len = size;
606 /* readlink() does not append a NUL character to buf */
607 memcpy (buf, ino->linkname, len);
608 return len;
611 /* --------------------------------------------------------------------------------------------- */
613 static ssize_t
614 vfs_s_read (void *fh, char *buffer, size_t count)
616 int n;
617 struct vfs_class *me = FH_SUPER->me;
619 if (FH->linear == LS_LINEAR_PREOPEN)
621 if (!MEDATA->linear_start (me, FH, FH->pos))
622 return -1;
625 if (FH->linear == LS_LINEAR_CLOSED)
626 vfs_die ("linear_start() did not set linear_state!");
628 if (FH->linear == LS_LINEAR_OPEN)
629 return MEDATA->linear_read (me, FH, buffer, count);
631 if (FH->handle != -1)
633 n = read (FH->handle, buffer, count);
634 if (n < 0)
635 me->verrno = errno;
636 return n;
638 vfs_die ("vfs_s_read: This should not happen\n");
639 return -1;
642 /* --------------------------------------------------------------------------------------------- */
644 static ssize_t
645 vfs_s_write (void *fh, const char *buffer, size_t count)
647 int n;
648 struct vfs_class *me = FH_SUPER->me;
650 if (FH->linear)
651 vfs_die ("no writing to linear files, please");
653 FH->changed = 1;
654 if (FH->handle != -1)
656 n = write (FH->handle, buffer, count);
657 if (n < 0)
658 me->verrno = errno;
659 return n;
661 vfs_die ("vfs_s_write: This should not happen\n");
662 return 0;
665 /* --------------------------------------------------------------------------------------------- */
667 static off_t
668 vfs_s_lseek (void *fh, off_t offset, int whence)
670 off_t size = FH->ino->st.st_size;
672 if (FH->linear == LS_LINEAR_OPEN)
673 vfs_die ("cannot lseek() after linear_read!");
675 if (FH->handle != -1)
676 { /* If we have local file opened, we want to work with it */
677 off_t retval = lseek (FH->handle, offset, whence);
678 if (retval == -1)
679 FH->ino->super->me->verrno = errno;
680 return retval;
683 switch (whence)
685 case SEEK_CUR:
686 offset += FH->pos;
687 break;
688 case SEEK_END:
689 offset += size;
690 break;
692 if (offset < 0)
693 FH->pos = 0;
694 else if (offset < size)
695 FH->pos = offset;
696 else
697 FH->pos = size;
698 return FH->pos;
701 /* --------------------------------------------------------------------------------------------- */
703 static int
704 vfs_s_close (void *fh)
706 int res = 0;
707 struct vfs_class *me = FH_SUPER->me;
709 FH_SUPER->fd_usage--;
710 if (!FH_SUPER->fd_usage)
711 vfs_stamp_create (me, FH_SUPER);
713 if (FH->linear == LS_LINEAR_OPEN)
714 MEDATA->linear_close (me, fh);
715 if (MEDATA->fh_close)
716 res = MEDATA->fh_close (me, fh);
717 if (FH->changed && MEDATA->file_store)
719 char *s = vfs_s_fullpath (me, FH->ino);
720 if (!s)
721 res = -1;
722 else
724 res = MEDATA->file_store (me, fh, s, FH->ino->localname);
725 g_free (s);
727 vfs_s_invalidate (me, FH_SUPER);
729 if (FH->handle != -1)
730 close (FH->handle);
732 vfs_s_free_inode (me, FH->ino);
733 g_free (fh);
734 return res;
737 /* --------------------------------------------------------------------------------------------- */
739 static void
740 vfs_s_print_stats (const char *fs_name, const char *action,
741 const char *file_name, off_t have, off_t need)
743 static const char *i18n_percent_transf_format = NULL;
744 static const char *i18n_transf_format = NULL;
746 if (i18n_percent_transf_format == NULL)
748 i18n_percent_transf_format = "%s: %s: %s %3d%% (%" PRIuMAX " %s";
749 i18n_transf_format = "%s: %s: %s %" PRIuMAX " %s";
752 if (need)
753 vfs_print_message (i18n_percent_transf_format, fs_name, action,
754 file_name, (int) ((double) have * 100 / need), (uintmax_t) have,
755 _("bytes transferred"));
756 else
757 vfs_print_message (i18n_transf_format, fs_name, action, file_name, (uintmax_t) have,
758 _("bytes transferred"));
761 /* --------------------------------------------------------------------------------------------- */
762 /* ------------------------------- mc support ---------------------------- */
764 static void
765 vfs_s_fill_names (struct vfs_class *me, fill_names_f func)
767 GList *iter;
769 for (iter = MEDATA->supers; iter != NULL; iter = g_list_next (iter))
771 const struct vfs_s_super *super = (const struct vfs_s_super *) iter->data;
772 char *name;
774 name = g_strconcat (super->name, "/", me->prefix, VFS_PATH_URL_DELIMITER,
775 /* super->current_dir->name, */ (char *) NULL);
776 func (name);
777 g_free (name);
781 /* --------------------------------------------------------------------------------------------- */
783 static int
784 vfs_s_ferrno (struct vfs_class *me)
786 return me->verrno;
789 /* --------------------------------------------------------------------------------------------- */
791 * Get local copy of the given file. We reuse the existing file cache
792 * for remote filesystems. Archives use standard VFS facilities.
795 static char *
796 vfs_s_getlocalcopy (const vfs_path_t * vpath)
798 vfs_file_handler_t *fh;
799 char *local = NULL;
801 fh = vfs_s_open (vpath, O_RDONLY, 0);
803 if (fh != NULL)
805 if ((fh->ino != NULL) && (fh->ino->localname != NULL))
806 local = g_strdup (fh->ino->localname);
808 vfs_s_close (fh);
811 return local;
814 /* --------------------------------------------------------------------------------------------- */
816 * Return the local copy. Since we are using our cache, we do nothing -
817 * the cache will be removed when the archive is closed.
820 static int
821 vfs_s_ungetlocalcopy (const vfs_path_t * vpath, const char *local, int has_changed)
823 (void) vpath;
824 (void) local;
825 (void) has_changed;
826 return 0;
829 /* --------------------------------------------------------------------------------------------- */
831 static int
832 vfs_s_setctl (const vfs_path_t * vpath, int ctlop, void *arg)
834 vfs_path_element_t *path_element;
836 path_element = vfs_path_get_by_index (vpath, -1);
837 switch (ctlop)
839 case VFS_SETCTL_STALE_DATA:
841 struct vfs_s_inode *ino = vfs_s_inode_from_path (vpath, 0);
843 if (ino == NULL)
844 return 0;
845 if (arg)
846 ino->super->want_stale = 1;
847 else
849 ino->super->want_stale = 0;
850 vfs_s_invalidate (path_element->class, ino->super);
852 return 1;
854 case VFS_SETCTL_LOGFILE:
855 ((struct vfs_s_subclass *) path_element->class->data)->logfile = fopen ((char *) arg, "w");
856 return 1;
857 case VFS_SETCTL_FLUSH:
858 ((struct vfs_s_subclass *) path_element->class->data)->flush = 1;
859 return 1;
861 return 0;
864 /* --------------------------------------------------------------------------------------------- */
865 /* ----------------------------- Stamping support -------------------------- */
867 static vfsid
868 vfs_s_getid (const vfs_path_t * vpath)
870 struct vfs_s_super *archive = NULL;
871 const char *p;
873 p = vfs_s_get_path (vpath, &archive, FL_NO_OPEN);
874 if (p == NULL)
875 return NULL;
877 return (vfsid) archive;
880 /* --------------------------------------------------------------------------------------------- */
882 static int
883 vfs_s_nothingisopen (vfsid id)
885 (void) id;
886 /* Our data structures should survive free of superblock at any time */
887 return 1;
890 /* --------------------------------------------------------------------------------------------- */
892 static void
893 vfs_s_free (vfsid id)
895 vfs_s_free_super (((struct vfs_s_super *) id)->me, (struct vfs_s_super *) id);
898 /* --------------------------------------------------------------------------------------------- */
900 static int
901 vfs_s_dir_uptodate (struct vfs_class *me, struct vfs_s_inode *ino)
903 struct timeval tim;
905 if (MEDATA->flush)
907 MEDATA->flush = 0;
908 return 0;
911 gettimeofday (&tim, NULL);
912 if (tim.tv_sec < ino->timestamp.tv_sec)
913 return 1;
914 return 0;
918 /* --------------------------------------------------------------------------------------------- */
919 /*** public functions ****************************************************************************/
920 /* --------------------------------------------------------------------------------------------- */
922 struct vfs_s_inode *
923 vfs_s_new_inode (struct vfs_class *me, struct vfs_s_super *super, struct stat *initstat)
925 struct vfs_s_inode *ino;
927 ino = g_try_new0 (struct vfs_s_inode, 1);
928 if (ino == NULL)
929 return NULL;
931 if (initstat)
932 ino->st = *initstat;
933 ino->super = super;
934 ino->st.st_nlink = 0;
935 ino->st.st_ino = MEDATA->inode_counter++;
936 ino->st.st_dev = MEDATA->rdev;
938 super->ino_usage++;
939 total_inodes++;
941 CALL (init_inode) (me, ino);
943 return ino;
946 /* --------------------------------------------------------------------------------------------- */
948 struct vfs_s_entry *
949 vfs_s_new_entry (struct vfs_class *me, const char *name, struct vfs_s_inode *inode)
951 struct vfs_s_entry *entry;
953 entry = g_new0 (struct vfs_s_entry, 1);
954 total_entries++;
956 entry->name = g_strdup (name);
957 entry->ino = inode;
958 entry->ino->ent = entry;
959 CALL (init_entry) (me, entry);
961 return entry;
965 /* --------------------------------------------------------------------------------------------- */
967 void
968 vfs_s_free_entry (struct vfs_class *me, struct vfs_s_entry *ent)
970 if (ent->dir != NULL)
971 ent->dir->subdir = g_list_remove (ent->dir->subdir, ent);
973 g_free (ent->name);
974 /* ent->name = NULL; */
976 if (ent->ino != NULL)
978 ent->ino->ent = NULL;
979 vfs_s_free_inode (me, ent->ino);
982 total_entries--;
983 g_free (ent);
986 /* --------------------------------------------------------------------------------------------- */
988 void
989 vfs_s_insert_entry (struct vfs_class *me, struct vfs_s_inode *dir, struct vfs_s_entry *ent)
991 (void) me;
993 ent->dir = dir;
995 ent->ino->st.st_nlink++;
996 dir->subdir = g_list_append (dir->subdir, ent);
999 /* --------------------------------------------------------------------------------------------- */
1001 struct stat *
1002 vfs_s_default_stat (struct vfs_class *me, mode_t mode)
1004 static struct stat st;
1005 int myumask;
1007 (void) me;
1009 myumask = umask (022);
1010 umask (myumask);
1011 mode &= ~myumask;
1013 st.st_mode = mode;
1014 st.st_ino = 0;
1015 st.st_dev = 0;
1016 st.st_rdev = 0;
1017 st.st_uid = getuid ();
1018 st.st_gid = getgid ();
1019 st.st_size = 0;
1020 st.st_mtime = st.st_atime = st.st_ctime = time (NULL);
1022 return &st;
1025 /* --------------------------------------------------------------------------------------------- */
1027 struct vfs_s_entry *
1028 vfs_s_generate_entry (struct vfs_class *me, const char *name, struct vfs_s_inode *parent,
1029 mode_t mode)
1031 struct vfs_s_inode *inode;
1032 struct stat *st;
1034 st = vfs_s_default_stat (me, mode);
1035 inode = vfs_s_new_inode (me, parent->super, st);
1037 return vfs_s_new_entry (me, name, inode);
1040 /* --------------------------------------------------------------------------------------------- */
1042 struct vfs_s_inode *
1043 vfs_s_find_inode (struct vfs_class *me, const struct vfs_s_super *super,
1044 const char *path, int follow, int flags)
1046 struct vfs_s_entry *ent;
1048 if (((MEDATA->flags & VFS_S_REMOTE) == 0) && (*path == '\0'))
1049 return super->root;
1051 ent = (MEDATA->find_entry) (me, super->root, path, follow, flags);
1052 return (ent != NULL) ? ent->ino : NULL;
1055 /* --------------------------------------------------------------------------------------------- */
1056 /* Ook, these were functions around directory entries / inodes */
1057 /* -------------------------------- superblock games -------------------------- */
1059 * get path from last VFS-element and create corresponding superblock
1061 * @param vpath source path object
1062 * @param archive pointer to object for store newly created superblock
1063 * @param flags flags
1065 * @return path from last VFS-element
1067 const char *
1068 vfs_s_get_path (const vfs_path_t * vpath, struct vfs_s_super **archive, int flags)
1070 GList *iter;
1071 const char *retval;
1072 int result = -1;
1073 struct vfs_s_super *super;
1074 void *cookie = NULL;
1075 vfs_path_element_t *path_element;
1076 vfs_path_t *vpath_archive;
1077 struct vfs_s_subclass *subclass;
1079 path_element = vfs_path_get_by_index (vpath, -1);
1080 subclass = ((struct vfs_s_subclass *) path_element->class->data);
1082 vpath_archive = vfs_path_clone (vpath);
1083 vfs_path_remove_element_by_index (vpath_archive, -1);
1085 retval = (path_element->path != NULL) ? path_element->path : "";
1087 if (subclass->archive_check != NULL)
1089 cookie = subclass->archive_check (vpath_archive);
1090 if (cookie == NULL)
1092 vfs_path_free (vpath_archive);
1093 return NULL;
1097 for (iter = subclass->supers; iter != NULL; iter = g_list_next (iter))
1099 int i;
1101 super = (struct vfs_s_super *) iter->data;
1103 /* 0 == other, 1 == same, return it, 2 == other but stop scanning */
1104 i = subclass->archive_same (path_element, super, vpath_archive, cookie);
1105 if (i != 0)
1107 if (i == 1)
1108 goto return_success;
1109 break;
1113 if (flags & FL_NO_OPEN)
1115 path_element->class->verrno = EIO;
1116 vfs_path_free (vpath_archive);
1117 return NULL;
1120 super = vfs_s_new_super (path_element->class);
1121 if (subclass->open_archive != NULL)
1122 result = subclass->open_archive (super, vpath_archive, path_element);
1123 if (result == -1)
1125 vfs_s_free_super (path_element->class, super);
1126 path_element->class->verrno = EIO;
1127 vfs_path_free (vpath_archive);
1128 return NULL;
1130 if (!super->name)
1131 vfs_die ("You have to fill name\n");
1132 if (!super->root)
1133 vfs_die ("You have to fill root inode\n");
1135 vfs_s_insert_super (path_element->class, super);
1136 vfs_stamp_create (path_element->class, super);
1138 return_success:
1139 *archive = super;
1140 vfs_path_free (vpath_archive);
1141 return retval;
1144 /* --------------------------------------------------------------------------------------------- */
1146 void
1147 vfs_s_invalidate (struct vfs_class *me, struct vfs_s_super *super)
1149 if (!super->want_stale)
1151 vfs_s_free_inode (me, super->root);
1152 super->root = vfs_s_new_inode (me, super, vfs_s_default_stat (me, S_IFDIR | 0755));
1156 /* --------------------------------------------------------------------------------------------- */
1158 char *
1159 vfs_s_fullpath (struct vfs_class *me, struct vfs_s_inode *ino)
1161 if (!ino->ent)
1162 ERRNOR (EAGAIN, NULL);
1164 if (!(MEDATA->flags & VFS_S_REMOTE))
1166 /* archives */
1167 char *newpath;
1168 char *path = g_strdup (ino->ent->name);
1169 while (1)
1171 ino = ino->ent->dir;
1172 if (ino == ino->super->root)
1173 break;
1174 newpath = g_strconcat (ino->ent->name, "/", path, (char *) NULL);
1175 g_free (path);
1176 path = newpath;
1178 return path;
1181 /* remote systems */
1182 if ((!ino->ent->dir) || (!ino->ent->dir->ent))
1183 return g_strdup (ino->ent->name);
1185 return g_strconcat (ino->ent->dir->ent->name, PATH_SEP_STR, ino->ent->name, (char *) NULL);
1188 /* --------------------------------------------------------------------------------------------- */
1189 /* --------------------------- stat and friends ---------------------------- */
1191 void *
1192 vfs_s_open (const vfs_path_t * vpath, int flags, mode_t mode)
1194 int was_changed = 0;
1195 vfs_file_handler_t *fh;
1196 struct vfs_s_super *super;
1197 const char *q;
1198 struct vfs_s_inode *ino;
1199 vfs_path_element_t *path_element;
1201 path_element = vfs_path_get_by_index (vpath, -1);
1203 q = vfs_s_get_path (vpath, &super, 0);
1204 if (q == NULL)
1205 return NULL;
1206 ino = vfs_s_find_inode (path_element->class, super, q, LINK_FOLLOW, FL_NONE);
1207 if (ino && ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL)))
1209 path_element->class->verrno = EEXIST;
1210 return NULL;
1212 if (!ino)
1214 char *dirname, *name, *save, *q_mangle;
1215 struct vfs_s_entry *ent;
1216 struct vfs_s_inode *dir;
1217 int tmp_handle;
1219 /* If the filesystem is read-only, disable file creation */
1220 if (!(flags & O_CREAT) || !(path_element->class->write))
1221 return NULL;
1223 q_mangle = g_strdup (q);
1224 split_dir_name (path_element->class, q_mangle, &dirname, &name, &save);
1225 dir = vfs_s_find_inode (path_element->class, super, dirname, LINK_FOLLOW, FL_DIR);
1226 if (dir == NULL)
1228 g_free (q_mangle);
1229 return NULL;
1231 if (save)
1232 *save = PATH_SEP;
1233 ent = vfs_s_generate_entry (path_element->class, name, dir, 0755);
1234 ino = ent->ino;
1235 vfs_s_insert_entry (path_element->class, dir, ent);
1236 tmp_handle = vfs_mkstemps (&ino->localname, path_element->class->name, name);
1237 g_free (q_mangle);
1238 if (tmp_handle == -1)
1239 return NULL;
1241 close (tmp_handle);
1242 was_changed = 1;
1245 if (S_ISDIR (ino->st.st_mode))
1247 path_element->class->verrno = EISDIR;
1248 return NULL;
1251 fh = g_new (vfs_file_handler_t, 1);
1252 fh->pos = 0;
1253 fh->ino = ino;
1254 fh->handle = -1;
1255 fh->changed = was_changed;
1256 fh->linear = 0;
1257 fh->data = NULL;
1259 if (IS_LINEAR (flags))
1261 if (VFSDATA (path_element)->linear_start)
1263 vfs_print_message (_("Starting linear transfer..."));
1264 fh->linear = LS_LINEAR_PREOPEN;
1267 else if ((VFSDATA (path_element)->fh_open != NULL)
1268 && (VFSDATA (path_element)->fh_open (path_element->class, fh, flags, mode) != 0))
1270 g_free (fh);
1271 return NULL;
1274 if (fh->ino->localname)
1276 fh->handle = open (fh->ino->localname, NO_LINEAR (flags), mode);
1277 if (fh->handle == -1)
1279 g_free (fh);
1280 path_element->class->verrno = errno;
1281 return NULL;
1285 /* i.e. we had no open files and now we have one */
1286 vfs_rmstamp (path_element->class, (vfsid) super);
1287 super->fd_usage++;
1288 fh->ino->st.st_nlink++;
1289 return fh;
1292 /* --------------------------------------------------------------------------------------------- */
1295 vfs_s_retrieve_file (struct vfs_class *me, struct vfs_s_inode *ino)
1297 /* If you want reget, you'll have to open file with O_LINEAR */
1298 off_t total = 0;
1299 char buffer[8192];
1300 int handle, n;
1301 off_t stat_size = ino->st.st_size;
1302 vfs_file_handler_t fh;
1304 memset (&fh, 0, sizeof (fh));
1306 fh.ino = ino;
1307 fh.handle = -1;
1309 handle = vfs_mkstemps (&ino->localname, me->name, ino->ent->name);
1310 if (handle == -1)
1312 me->verrno = errno;
1313 goto error_4;
1316 if (!MEDATA->linear_start (me, &fh, 0))
1317 goto error_3;
1319 /* Clear the interrupt status */
1320 tty_got_interrupt ();
1321 tty_enable_interrupt_key ();
1323 while ((n = MEDATA->linear_read (me, &fh, buffer, sizeof (buffer))))
1325 int t;
1326 if (n < 0)
1327 goto error_1;
1329 total += n;
1330 vfs_s_print_stats (me->name, _("Getting file"), ino->ent->name, total, stat_size);
1332 if (tty_got_interrupt ())
1333 goto error_1;
1335 t = write (handle, buffer, n);
1336 if (t != n)
1338 if (t == -1)
1339 me->verrno = errno;
1340 goto error_1;
1343 MEDATA->linear_close (me, &fh);
1344 close (handle);
1346 tty_disable_interrupt_key ();
1347 g_free (fh.data);
1348 return 0;
1350 error_1:
1351 MEDATA->linear_close (me, &fh);
1352 error_3:
1353 tty_disable_interrupt_key ();
1354 close (handle);
1355 unlink (ino->localname);
1356 error_4:
1357 g_free (ino->localname);
1358 ino->localname = NULL;
1359 g_free (fh.data);
1360 return -1;
1363 /* --------------------------------------------------------------------------------------------- */
1364 /* ----------------------------- Stamping support -------------------------- */
1366 /* Initialize one of our subclasses - fill common functions */
1367 void
1368 vfs_s_init_class (struct vfs_class *vclass, struct vfs_s_subclass *sub)
1370 vclass->data = sub;
1371 vclass->fill_names = vfs_s_fill_names;
1372 vclass->open = vfs_s_open;
1373 vclass->close = vfs_s_close;
1374 vclass->read = vfs_s_read;
1375 if (!(sub->flags & VFS_S_READONLY))
1377 vclass->write = vfs_s_write;
1379 vclass->opendir = vfs_s_opendir;
1380 vclass->readdir = vfs_s_readdir;
1381 vclass->closedir = vfs_s_closedir;
1382 vclass->stat = vfs_s_stat;
1383 vclass->lstat = vfs_s_lstat;
1384 vclass->fstat = vfs_s_fstat;
1385 vclass->readlink = vfs_s_readlink;
1386 vclass->chdir = vfs_s_chdir;
1387 vclass->ferrno = vfs_s_ferrno;
1388 vclass->lseek = vfs_s_lseek;
1389 vclass->getid = vfs_s_getid;
1390 vclass->nothingisopen = vfs_s_nothingisopen;
1391 vclass->free = vfs_s_free;
1392 if (sub->flags & VFS_S_REMOTE)
1394 vclass->getlocalcopy = vfs_s_getlocalcopy;
1395 vclass->ungetlocalcopy = vfs_s_ungetlocalcopy;
1396 sub->find_entry = vfs_s_find_entry_linear;
1398 else
1400 sub->find_entry = vfs_s_find_entry_tree;
1402 vclass->setctl = vfs_s_setctl;
1403 sub->dir_uptodate = vfs_s_dir_uptodate;
1406 /* --------------------------------------------------------------------------------------------- */
1407 /** Find VFS id for given directory name */
1409 vfsid
1410 vfs_getid (const vfs_path_t * vpath)
1412 vfs_path_element_t *path_element;
1414 path_element = vfs_path_get_by_index (vpath, -1);
1415 if (!vfs_path_element_valid (path_element) || path_element->class->getid == NULL)
1416 return NULL;
1418 return (*path_element->class->getid) (vpath);
1421 /* --------------------------------------------------------------------------------------------- */
1422 /* ----------- Utility functions for networked filesystems -------------- */
1424 #ifdef ENABLE_VFS_NET
1426 vfs_s_select_on_two (int fd1, int fd2)
1428 fd_set set;
1429 struct timeval time_out;
1430 int v;
1431 int maxfd = (fd1 > fd2 ? fd1 : fd2) + 1;
1433 time_out.tv_sec = 1;
1434 time_out.tv_usec = 0;
1435 FD_ZERO (&set);
1436 FD_SET (fd1, &set);
1437 FD_SET (fd2, &set);
1438 v = select (maxfd, &set, 0, 0, &time_out);
1439 if (v <= 0)
1440 return v;
1441 if (FD_ISSET (fd1, &set))
1442 return 1;
1443 if (FD_ISSET (fd2, &set))
1444 return 2;
1445 return -1;
1448 /* --------------------------------------------------------------------------------------------- */
1451 vfs_s_get_line (struct vfs_class *me, int sock, char *buf, int buf_len, char term)
1453 FILE *logfile = MEDATA->logfile;
1454 int i;
1455 char c;
1457 for (i = 0; i < buf_len - 1; i++, buf++)
1459 if (read (sock, buf, sizeof (char)) <= 0)
1460 return 0;
1461 if (logfile)
1463 size_t ret1;
1464 int ret2;
1465 ret1 = fwrite (buf, 1, 1, logfile);
1466 ret2 = fflush (logfile);
1468 if (*buf == term)
1470 *buf = 0;
1471 return 1;
1475 /* Line is too long - terminate buffer and discard the rest of line */
1476 *buf = 0;
1477 while (read (sock, &c, sizeof (c)) > 0)
1479 if (logfile)
1481 size_t ret1;
1482 int ret2;
1483 ret1 = fwrite (&c, 1, 1, logfile);
1484 ret2 = fflush (logfile);
1486 if (c == '\n')
1487 return 1;
1489 return 0;
1492 /* --------------------------------------------------------------------------------------------- */
1495 vfs_s_get_line_interruptible (struct vfs_class *me, char *buffer, int size, int fd)
1497 int n;
1498 int i;
1500 (void) me;
1502 tty_enable_interrupt_key ();
1503 for (i = 0; i < size - 1; i++)
1505 n = read (fd, buffer + i, 1);
1506 tty_disable_interrupt_key ();
1507 if (n == -1 && errno == EINTR)
1509 buffer[i] = 0;
1510 return EINTR;
1512 if (n == 0)
1514 buffer[i] = 0;
1515 return 0;
1517 if (buffer[i] == '\n')
1519 buffer[i] = 0;
1520 return 1;
1523 buffer[size - 1] = 0;
1524 return 0;
1526 #endif /* ENABLE_VFS_NET */
1528 /* --------------------------------------------------------------------------------------------- */
1530 * Normalize filenames start position
1533 void
1534 vfs_s_normalize_filename_leading_spaces (struct vfs_s_inode *root_inode, size_t final_num_spaces)
1536 GList *iter;
1538 for (iter = root_inode->subdir; iter != NULL; iter = g_list_next (iter))
1540 struct vfs_s_entry *entry = (struct vfs_s_entry *) iter->data;
1541 if ((size_t) entry->ino->data_offset > final_num_spaces)
1543 char *source_name = entry->name;
1544 char *spacer = g_strnfill (entry->ino->data_offset - final_num_spaces, ' ');
1545 entry->name = g_strdup_printf ("%s%s", spacer, source_name);
1546 g_free (spacer);
1547 g_free (source_name);
1549 entry->ino->data_offset = -1;
1553 /* --------------------------------------------------------------------------------------------- */