Remove unneeded `struct` keyword for typedef'd structs
[midnight-commander.git] / lib / vfs / direntry.c
blob66742b5628c6559dffa0fab6120c7bffc800599b
1 /*
2 Directory cache support
4 Copyright (C) 1998-2016
5 Free Software Foundation, Inc.
7 Written by:
8 Pavel Machek <pavel@ucw.cz>, 1998
9 Slava Zanko <slavazanko@gmail.com>, 2013
11 This file is part of the Midnight Commander.
13 The Midnight Commander is free software: you can redistribute it
14 and/or modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation, either version 3 of the License,
16 or (at your option) any later version.
18 The Midnight Commander is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 \warning Paths here do _not_ begin with '/', so root directory of
27 archive/site is simply "".
30 /** \file
31 * \brief Source: directory cache support
33 * So that you do not have copy of this in each and every filesystem.
35 * Very loosely based on tar.c from midnight and archives.[ch] from
36 * avfs by Miklos Szeredi (mszeredi@inf.bme.hu)
38 * Unfortunately, I was unable to keep all filesystems
39 * uniform. tar-like filesystems use tree structure where each
40 * directory has pointers to its subdirectories. We can do this
41 * because we have full information about our archive.
43 * At ftp-like filesystems, situation is a little bit different. When
44 * you cd /usr/src/linux/drivers/char, you do _not_ want /usr,
45 * /usr/src, /usr/src/linux and /usr/src/linux/drivers to be
46 * listed. That means that we do not have complete information, and if
47 * /usr is symlink to /4, we will not know. Also we have to time out
48 * entries and things would get messy with tree-like approach. So we
49 * do different trick: root directory is completely special and
50 * completely fake, it contains entries such as 'usr', 'usr/src', ...,
51 * and we'll try to use custom find_entry function.
53 * \author Pavel Machek <pavel@ucw.cz>
54 * \date 1998
58 #include <config.h>
60 #include <errno.h>
61 #include <time.h>
62 #include <sys/time.h> /* gettimeofday() */
63 #include <inttypes.h> /* uintmax_t */
64 #include <stdarg.h>
66 #include "lib/global.h"
68 #include "lib/tty/tty.h" /* enable/disable interrupt key */
69 #include "lib/util.h" /* custom_canonicalize_pathname() */
70 #if 0
71 #include "lib/widget.h" /* message() */
72 #endif
74 #include "vfs.h"
75 #include "utilvfs.h"
76 #include "xdirentry.h"
77 #include "gc.h" /* vfs_rmstamp */
79 /*** global variables ****************************************************************************/
81 /*** file scope macro definitions ****************************************************************/
83 #define CALL(x) if (MEDATA->x) MEDATA->x
85 /*** file scope type declarations ****************************************************************/
87 struct dirhandle
89 GList *cur;
90 struct vfs_s_inode *dir;
93 /*** file scope variables ************************************************************************/
95 /*** file scope functions ************************************************************************/
96 /* --------------------------------------------------------------------------------------------- */
98 static int
99 vfs_s_entry_compare (const void *a, const void *b)
101 const struct vfs_s_entry *e = (const struct vfs_s_entry *) a;
102 const char *name = (const char *) b;
104 return strcmp (e->name, name);
107 /* --------------------------------------------------------------------------------------------- */
109 /* We were asked to create entries automagically */
111 static struct vfs_s_entry *
112 vfs_s_automake (struct vfs_class *me, struct vfs_s_inode *dir, char *path, int flags)
114 struct vfs_s_entry *res;
115 char *sep;
117 sep = strchr (path, PATH_SEP);
118 if (sep != NULL)
119 *sep = '\0';
121 res = vfs_s_generate_entry (me, path, dir, flags & FL_MKDIR ? (0777 | S_IFDIR) : 0777);
122 vfs_s_insert_entry (me, dir, res);
124 if (sep != NULL)
125 *sep = PATH_SEP;
127 return res;
130 /* --------------------------------------------------------------------------------------------- */
131 /* If the entry is a symlink, find the entry for its target */
133 static struct vfs_s_entry *
134 vfs_s_resolve_symlink (struct vfs_class *me, struct vfs_s_entry *entry, int follow)
136 char *linkname;
137 char *fullname = NULL;
138 struct vfs_s_entry *target;
140 if (follow == LINK_NO_FOLLOW)
141 return entry;
142 if (follow == 0)
143 ERRNOR (ELOOP, NULL);
144 if (!entry)
145 ERRNOR (ENOENT, NULL);
146 if (!S_ISLNK (entry->ino->st.st_mode))
147 return entry;
149 linkname = entry->ino->linkname;
150 if (linkname == NULL)
151 ERRNOR (EFAULT, NULL);
153 /* make full path from relative */
154 if (!IS_PATH_SEP (*linkname))
156 char *fullpath;
158 fullpath = vfs_s_fullpath (me, entry->dir);
159 if (fullpath != NULL)
161 fullname = g_strconcat (fullpath, PATH_SEP_STR, linkname, (char *) NULL);
162 linkname = fullname;
163 g_free (fullpath);
167 target = (MEDATA->find_entry) (me, entry->dir->super->root, linkname, follow - 1, 0);
168 g_free (fullname);
169 return target;
172 /* --------------------------------------------------------------------------------------------- */
174 * Follow > 0: follow links, serves as loop protect,
175 * == -1: do not follow links
178 static struct vfs_s_entry *
179 vfs_s_find_entry_tree (struct vfs_class *me, struct vfs_s_inode *root,
180 const char *a_path, int follow, int flags)
182 size_t pseg;
183 struct vfs_s_entry *ent = NULL;
184 char *const pathref = g_strdup (a_path);
185 char *path = pathref;
187 /* canonicalize as well, but don't remove '../' from path */
188 custom_canonicalize_pathname (path, CANON_PATH_ALL & (~CANON_PATH_REMDOUBLEDOTS));
190 while (root != NULL)
192 GList *iter;
194 while (IS_PATH_SEP (*path)) /* Strip leading '/' */
195 path++;
197 if (path[0] == '\0')
199 g_free (pathref);
200 return ent;
203 for (pseg = 0; path[pseg] != '\0' && !IS_PATH_SEP (path[pseg]); pseg++)
206 for (iter = root->subdir; iter != NULL; iter = g_list_next (iter))
208 ent = (struct vfs_s_entry *) iter->data;
209 if (strlen (ent->name) == pseg && strncmp (ent->name, path, pseg) == 0)
210 /* FOUND! */
211 break;
214 ent = iter != NULL ? (struct vfs_s_entry *) iter->data : NULL;
216 if (ent == NULL && (flags & (FL_MKFILE | FL_MKDIR)) != 0)
217 ent = vfs_s_automake (me, root, path, flags);
218 if (ent == NULL)
220 me->verrno = ENOENT;
221 goto cleanup;
224 path += pseg;
225 /* here we must follow leading directories always;
226 only the actual file is optional */
227 ent = vfs_s_resolve_symlink (me, ent,
228 strchr (path, PATH_SEP) != NULL ? LINK_FOLLOW : follow);
229 if (ent == NULL)
230 goto cleanup;
231 root = ent->ino;
233 cleanup:
234 g_free (pathref);
235 return NULL;
238 /* --------------------------------------------------------------------------------------------- */
240 static struct vfs_s_entry *
241 vfs_s_find_entry_linear (struct vfs_class *me, struct vfs_s_inode *root,
242 const char *a_path, int follow, int flags)
244 struct vfs_s_entry *ent = NULL;
245 char *const path = g_strdup (a_path);
246 GList *iter;
248 if (root->super->root != root)
249 vfs_die ("We have to use _real_ root. Always. Sorry.");
251 /* canonicalize as well, but don't remove '../' from path */
252 custom_canonicalize_pathname (path, CANON_PATH_ALL & (~CANON_PATH_REMDOUBLEDOTS));
254 if ((flags & FL_DIR) == 0)
256 char *dirname, *name;
257 struct vfs_s_inode *ino;
259 dirname = g_path_get_dirname (path);
260 name = g_path_get_basename (path);
261 ino = vfs_s_find_inode (me, root->super, dirname, follow, flags | FL_DIR);
262 ent = vfs_s_find_entry_tree (me, ino, name, follow, flags);
263 g_free (dirname);
264 g_free (name);
265 g_free (path);
266 return ent;
269 iter = g_list_find_custom (root->subdir, path, (GCompareFunc) vfs_s_entry_compare);
270 ent = iter != NULL ? (struct vfs_s_entry *) iter->data : NULL;
272 if (ent != NULL && !MEDATA->dir_uptodate (me, ent->ino))
274 #if 1
275 vfs_print_message (_("Directory cache expired for %s"), path);
276 #endif
277 vfs_s_free_entry (me, ent);
278 ent = NULL;
281 if (ent == NULL)
283 struct vfs_s_inode *ino;
285 ino = vfs_s_new_inode (me, root->super, vfs_s_default_stat (me, S_IFDIR | 0755));
286 ent = vfs_s_new_entry (me, path, ino);
287 if (MEDATA->dir_load (me, ino, path) == -1)
289 vfs_s_free_entry (me, ent);
290 g_free (path);
291 return NULL;
294 vfs_s_insert_entry (me, root, ent);
296 iter = g_list_find_custom (root->subdir, path, (GCompareFunc) vfs_s_entry_compare);
297 ent = iter != NULL ? (struct vfs_s_entry *) iter->data : NULL;
299 if (ent == NULL)
300 vfs_die ("find_linear: success but directory is not there\n");
302 #if 0
303 if (!vfs_s_resolve_symlink (me, ent, follow))
305 g_free (path);
306 return NULL;
308 #endif
309 g_free (path);
310 return ent;
313 /* --------------------------------------------------------------------------------------------- */
314 /* Ook, these were functions around directory entries / inodes */
315 /* -------------------------------- superblock games -------------------------- */
317 static struct vfs_s_super *
318 vfs_s_new_super (struct vfs_class *me)
320 struct vfs_s_super *super;
322 super = g_new0 (struct vfs_s_super, 1);
323 super->me = me;
324 return super;
327 /* --------------------------------------------------------------------------------------------- */
329 static inline void
330 vfs_s_insert_super (struct vfs_class *me, struct vfs_s_super *super)
332 MEDATA->supers = g_list_prepend (MEDATA->supers, super);
335 /* --------------------------------------------------------------------------------------------- */
337 static void
338 vfs_s_free_super (struct vfs_class *me, struct vfs_s_super *super)
340 if (super->root != NULL)
342 vfs_s_free_inode (me, super->root);
343 super->root = NULL;
346 #if 0
347 /* FIXME: We currently leak small ammount of memory, sometimes. Fix it if you can. */
348 if (super->ino_usage)
349 message (D_ERROR, "Direntry warning",
350 "Super ino_usage is %d, memory leak", super->ino_usage);
352 if (super->want_stale)
353 message (D_ERROR, "Direntry warning", "%s", "Super has want_stale set");
354 #endif
356 MEDATA->supers = g_list_remove (MEDATA->supers, super);
358 CALL (free_archive) (me, super);
359 #ifdef ENABLE_VFS_NET
360 vfs_path_element_free (super->path_element);
361 #endif
362 g_free (super->name);
363 g_free (super);
366 /* --------------------------------------------------------------------------------------------- */
367 /* Support of archives */
368 /* ------------------------ readdir & friends ----------------------------- */
370 static struct vfs_s_inode *
371 vfs_s_inode_from_path (const vfs_path_t * vpath, int flags)
373 struct vfs_s_super *super;
374 struct vfs_s_inode *ino;
375 const char *q;
376 const vfs_path_element_t *path_element;
378 q = vfs_s_get_path (vpath, &super, 0);
379 if (q == NULL)
380 return NULL;
382 path_element = vfs_path_get_by_index (vpath, -1);
384 ino =
385 vfs_s_find_inode (path_element->class, super, q,
386 flags & FL_FOLLOW ? LINK_FOLLOW : LINK_NO_FOLLOW, flags & ~FL_FOLLOW);
387 if ((!ino) && (!*q))
388 /* We are asking about / directory of ftp server: assume it exists */
389 ino =
390 vfs_s_find_inode (path_element->class, super, q,
391 flags & FL_FOLLOW ? LINK_FOLLOW :
392 LINK_NO_FOLLOW, FL_DIR | (flags & ~FL_FOLLOW));
393 return ino;
396 /* --------------------------------------------------------------------------------------------- */
398 static void *
399 vfs_s_opendir (const vfs_path_t * vpath)
401 struct vfs_s_inode *dir;
402 struct dirhandle *info;
403 const vfs_path_element_t *path_element;
405 path_element = vfs_path_get_by_index (vpath, -1);
407 dir = vfs_s_inode_from_path (vpath, FL_DIR | FL_FOLLOW);
408 if (dir == NULL)
409 return NULL;
410 if (!S_ISDIR (dir->st.st_mode))
412 path_element->class->verrno = ENOTDIR;
413 return NULL;
416 dir->st.st_nlink++;
417 #if 0
418 if (dir->subdir == NULL) /* This can actually happen if we allow empty directories */
420 path_element->class->verrno = EAGAIN;
421 return NULL;
423 #endif
424 info = g_new (struct dirhandle, 1);
425 info->cur = dir->subdir;
426 info->dir = dir;
428 return info;
431 /* --------------------------------------------------------------------------------------------- */
433 static void *
434 vfs_s_readdir (void *data)
436 static union vfs_dirent dir;
437 struct dirhandle *info = (struct dirhandle *) data;
438 const char *name;
440 if (info->cur == NULL || info->cur->data == NULL)
441 return NULL;
443 name = ((struct vfs_s_entry *) info->cur->data)->name;
444 if (name != NULL)
445 g_strlcpy (dir.dent.d_name, name, MC_MAXPATHLEN);
446 else
447 vfs_die ("Null in structure-cannot happen");
449 info->cur = g_list_next (info->cur);
451 return (void *) &dir;
454 /* --------------------------------------------------------------------------------------------- */
456 static int
457 vfs_s_closedir (void *data)
459 struct dirhandle *info = (struct dirhandle *) data;
460 struct vfs_s_inode *dir = info->dir;
462 vfs_s_free_inode (dir->super->me, dir);
463 g_free (data);
464 return 0;
467 /* --------------------------------------------------------------------------------------------- */
469 static int
470 vfs_s_chdir (const vfs_path_t * vpath)
472 void *data;
474 data = vfs_s_opendir (vpath);
475 if (data == NULL)
476 return -1;
477 vfs_s_closedir (data);
478 return 0;
481 /* --------------------------------------------------------------------------------------------- */
482 /* --------------------------- stat and friends ---------------------------- */
484 static int
485 vfs_s_internal_stat (const vfs_path_t * vpath, struct stat *buf, int flag)
487 struct vfs_s_inode *ino;
489 ino = vfs_s_inode_from_path (vpath, flag);
490 if (ino == NULL)
491 return -1;
492 *buf = ino->st;
493 return 0;
496 /* --------------------------------------------------------------------------------------------- */
498 static int
499 vfs_s_stat (const vfs_path_t * vpath, struct stat *buf)
501 return vfs_s_internal_stat (vpath, buf, FL_FOLLOW);
504 /* --------------------------------------------------------------------------------------------- */
506 static int
507 vfs_s_lstat (const vfs_path_t * vpath, struct stat *buf)
509 return vfs_s_internal_stat (vpath, buf, FL_NONE);
512 /* --------------------------------------------------------------------------------------------- */
514 static int
515 vfs_s_fstat (void *fh, struct stat *buf)
517 *buf = FH->ino->st;
518 return 0;
521 /* --------------------------------------------------------------------------------------------- */
523 static int
524 vfs_s_readlink (const vfs_path_t * vpath, char *buf, size_t size)
526 struct vfs_s_inode *ino;
527 size_t len;
528 const vfs_path_element_t *path_element;
530 path_element = vfs_path_get_by_index (vpath, -1);
532 ino = vfs_s_inode_from_path (vpath, 0);
533 if (!ino)
534 return -1;
536 if (!S_ISLNK (ino->st.st_mode))
538 path_element->class->verrno = EINVAL;
539 return -1;
542 if (ino->linkname == NULL)
544 path_element->class->verrno = EFAULT;
545 return -1;
548 len = strlen (ino->linkname);
549 if (size < len)
550 len = size;
551 /* readlink() does not append a NUL character to buf */
552 memcpy (buf, ino->linkname, len);
553 return len;
556 /* --------------------------------------------------------------------------------------------- */
558 static ssize_t
559 vfs_s_read (void *fh, char *buffer, size_t count)
561 struct vfs_class *me = FH_SUPER->me;
563 if (FH->linear == LS_LINEAR_PREOPEN)
565 if (!MEDATA->linear_start (me, FH, FH->pos))
566 return -1;
569 if (FH->linear == LS_LINEAR_CLOSED)
570 vfs_die ("linear_start() did not set linear_state!");
572 if (FH->linear == LS_LINEAR_OPEN)
573 return MEDATA->linear_read (me, FH, buffer, count);
575 if (FH->handle != -1)
577 ssize_t n;
579 n = read (FH->handle, buffer, count);
580 if (n < 0)
581 me->verrno = errno;
582 return n;
584 vfs_die ("vfs_s_read: This should not happen\n");
585 return -1;
588 /* --------------------------------------------------------------------------------------------- */
590 static ssize_t
591 vfs_s_write (void *fh, const char *buffer, size_t count)
593 struct vfs_class *me = FH_SUPER->me;
595 if (FH->linear)
596 vfs_die ("no writing to linear files, please");
598 FH->changed = 1;
599 if (FH->handle != -1)
601 ssize_t n;
603 n = write (FH->handle, buffer, count);
604 if (n < 0)
605 me->verrno = errno;
606 return n;
608 vfs_die ("vfs_s_write: This should not happen\n");
609 return 0;
612 /* --------------------------------------------------------------------------------------------- */
614 static off_t
615 vfs_s_lseek (void *fh, off_t offset, int whence)
617 off_t size = FH->ino->st.st_size;
619 if (FH->linear == LS_LINEAR_OPEN)
620 vfs_die ("cannot lseek() after linear_read!");
622 if (FH->handle != -1)
623 { /* If we have local file opened, we want to work with it */
624 off_t retval = lseek (FH->handle, offset, whence);
625 if (retval == -1)
626 FH->ino->super->me->verrno = errno;
627 return retval;
630 switch (whence)
632 case SEEK_CUR:
633 offset += FH->pos;
634 break;
635 case SEEK_END:
636 offset += size;
637 break;
638 default:
639 break;
641 if (offset < 0)
642 FH->pos = 0;
643 else if (offset < size)
644 FH->pos = offset;
645 else
646 FH->pos = size;
647 return FH->pos;
650 /* --------------------------------------------------------------------------------------------- */
652 static int
653 vfs_s_close (void *fh)
655 int res = 0;
656 struct vfs_class *me = FH_SUPER->me;
658 if (me == NULL)
659 return (-1);
661 FH_SUPER->fd_usage--;
662 if (!FH_SUPER->fd_usage)
663 vfs_stamp_create (me, FH_SUPER);
665 if (FH->linear == LS_LINEAR_OPEN)
666 MEDATA->linear_close (me, fh);
667 if (MEDATA->fh_close)
668 res = MEDATA->fh_close (me, fh);
669 if ((MEDATA->flags & VFS_S_USETMP) && FH->changed && MEDATA->file_store)
671 char *s = vfs_s_fullpath (me, FH->ino);
672 if (!s)
673 res = -1;
674 else
676 res = MEDATA->file_store (me, fh, s, FH->ino->localname);
677 g_free (s);
679 vfs_s_invalidate (me, FH_SUPER);
681 if (FH->handle != -1)
682 close (FH->handle);
684 vfs_s_free_inode (me, FH->ino);
685 if (MEDATA->fh_free_data != NULL)
686 MEDATA->fh_free_data (fh);
687 g_free (fh);
688 return res;
691 /* --------------------------------------------------------------------------------------------- */
693 static void
694 vfs_s_print_stats (const char *fs_name, const char *action,
695 const char *file_name, off_t have, off_t need)
697 if (need != 0)
698 vfs_print_message (_("%s: %s: %s %3d%% (%lld) bytes transferred"), fs_name, action,
699 file_name, (int) ((double) have * 100 / need), (long long) have);
700 else
701 vfs_print_message (_("%s: %s: %s %lld bytes transferred"), fs_name, action, file_name,
702 (long long) have);
705 /* --------------------------------------------------------------------------------------------- */
706 /* ------------------------------- mc support ---------------------------- */
708 static void
709 vfs_s_fill_names (struct vfs_class *me, fill_names_f func)
711 GList *iter;
713 for (iter = MEDATA->supers; iter != NULL; iter = g_list_next (iter))
715 const struct vfs_s_super *super = (const struct vfs_s_super *) iter->data;
716 char *name;
718 name = g_strconcat (super->name, PATH_SEP_STR, me->prefix, VFS_PATH_URL_DELIMITER,
719 /* super->current_dir->name, */ (char *) NULL);
720 func (name);
721 g_free (name);
725 /* --------------------------------------------------------------------------------------------- */
727 static int
728 vfs_s_ferrno (struct vfs_class *me)
730 return me->verrno;
733 /* --------------------------------------------------------------------------------------------- */
735 * Get local copy of the given file. We reuse the existing file cache
736 * for remote filesystems. Archives use standard VFS facilities.
739 static vfs_path_t *
740 vfs_s_getlocalcopy (const vfs_path_t * vpath)
742 vfs_file_handler_t *fh;
743 vfs_path_t *local = NULL;
745 if (vpath == NULL)
746 return NULL;
748 fh = vfs_s_open (vpath, O_RDONLY, 0);
750 if (fh != NULL)
752 const struct vfs_class *me;
754 me = vfs_path_get_by_index (vpath, -1)->class;
755 if ((MEDATA->flags & VFS_S_USETMP) != 0 && (fh->ino != NULL))
756 local = vfs_path_from_str_flags (fh->ino->localname, VPF_NO_CANON);
758 vfs_s_close (fh);
761 return local;
764 /* --------------------------------------------------------------------------------------------- */
766 * Return the local copy. Since we are using our cache, we do nothing -
767 * the cache will be removed when the archive is closed.
770 static int
771 vfs_s_ungetlocalcopy (const vfs_path_t * vpath, const vfs_path_t * local, gboolean has_changed)
773 (void) vpath;
774 (void) local;
775 (void) has_changed;
776 return 0;
779 /* --------------------------------------------------------------------------------------------- */
781 static int
782 vfs_s_setctl (const vfs_path_t * vpath, int ctlop, void *arg)
784 const vfs_path_element_t *path_element;
786 path_element = vfs_path_get_by_index (vpath, -1);
788 switch (ctlop)
790 case VFS_SETCTL_STALE_DATA:
792 struct vfs_s_inode *ino;
794 ino = vfs_s_inode_from_path (vpath, 0);
795 if (ino == NULL)
796 return 0;
797 if (arg)
798 ino->super->want_stale = 1;
799 else
801 ino->super->want_stale = 0;
802 vfs_s_invalidate (path_element->class, ino->super);
804 return 1;
806 case VFS_SETCTL_LOGFILE:
807 ((struct vfs_s_subclass *) path_element->class->data)->logfile = fopen ((char *) arg, "w");
808 return 1;
809 case VFS_SETCTL_FLUSH:
810 ((struct vfs_s_subclass *) path_element->class->data)->flush = 1;
811 return 1;
812 default:
813 return 0;
817 /* --------------------------------------------------------------------------------------------- */
818 /* ----------------------------- Stamping support -------------------------- */
820 static vfsid
821 vfs_s_getid (const vfs_path_t * vpath)
823 struct vfs_s_super *archive = NULL;
824 const char *p;
826 p = vfs_s_get_path (vpath, &archive, FL_NO_OPEN);
827 if (p == NULL)
828 return NULL;
830 return (vfsid) archive;
833 /* --------------------------------------------------------------------------------------------- */
835 static int
836 vfs_s_nothingisopen (vfsid id)
838 (void) id;
839 /* Our data structures should survive free of superblock at any time */
840 return 1;
843 /* --------------------------------------------------------------------------------------------- */
845 static void
846 vfs_s_free (vfsid id)
848 vfs_s_free_super (((struct vfs_s_super *) id)->me, (struct vfs_s_super *) id);
851 /* --------------------------------------------------------------------------------------------- */
853 static int
854 vfs_s_dir_uptodate (struct vfs_class *me, struct vfs_s_inode *ino)
856 struct timeval tim;
858 if (MEDATA->flush)
860 MEDATA->flush = 0;
861 return 0;
864 gettimeofday (&tim, NULL);
865 if (tim.tv_sec < ino->timestamp.tv_sec)
866 return 1;
867 return 0;
871 /* --------------------------------------------------------------------------------------------- */
872 /*** public functions ****************************************************************************/
873 /* --------------------------------------------------------------------------------------------- */
875 struct vfs_s_inode *
876 vfs_s_new_inode (struct vfs_class *me, struct vfs_s_super *super, struct stat *initstat)
878 struct vfs_s_inode *ino;
880 ino = g_try_new0 (struct vfs_s_inode, 1);
881 if (ino == NULL)
882 return NULL;
884 if (initstat)
885 ino->st = *initstat;
886 ino->super = super;
887 ino->st.st_nlink = 0;
888 ino->st.st_ino = MEDATA->inode_counter++;
889 ino->st.st_dev = MEDATA->rdev;
891 super->ino_usage++;
893 CALL (init_inode) (me, ino);
895 return ino;
898 /* --------------------------------------------------------------------------------------------- */
900 void
901 vfs_s_free_inode (struct vfs_class *me, struct vfs_s_inode *ino)
903 if (ino == NULL)
904 vfs_die ("Don't pass NULL to me");
906 /* ==0 can happen if freshly created entry is deleted */
907 if (ino->st.st_nlink > 1)
909 ino->st.st_nlink--;
910 return;
913 while (ino->subdir != NULL)
914 vfs_s_free_entry (me, (struct vfs_s_entry *) ino->subdir->data);
916 CALL (free_inode) (me, ino);
917 g_free (ino->linkname);
918 if ((MEDATA->flags & VFS_S_USETMP) != 0 && ino->localname != NULL)
920 unlink (ino->localname);
921 g_free (ino->localname);
923 ino->super->ino_usage--;
924 g_free (ino);
927 /* --------------------------------------------------------------------------------------------- */
929 struct vfs_s_entry *
930 vfs_s_new_entry (struct vfs_class *me, const char *name, struct vfs_s_inode *inode)
932 struct vfs_s_entry *entry;
934 entry = g_new0 (struct vfs_s_entry, 1);
936 entry->name = g_strdup (name);
937 entry->ino = inode;
938 entry->ino->ent = entry;
939 CALL (init_entry) (me, entry);
941 return entry;
945 /* --------------------------------------------------------------------------------------------- */
947 void
948 vfs_s_free_entry (struct vfs_class *me, struct vfs_s_entry *ent)
950 if (ent->dir != NULL)
951 ent->dir->subdir = g_list_remove (ent->dir->subdir, ent);
953 MC_PTR_FREE (ent->name);
955 if (ent->ino != NULL)
957 ent->ino->ent = NULL;
958 vfs_s_free_inode (me, ent->ino);
961 g_free (ent);
964 /* --------------------------------------------------------------------------------------------- */
966 void
967 vfs_s_insert_entry (struct vfs_class *me, struct vfs_s_inode *dir, struct vfs_s_entry *ent)
969 (void) me;
971 ent->dir = dir;
973 ent->ino->st.st_nlink++;
974 dir->subdir = g_list_append (dir->subdir, ent);
977 /* --------------------------------------------------------------------------------------------- */
979 struct stat *
980 vfs_s_default_stat (struct vfs_class *me, mode_t mode)
982 static struct stat st;
983 mode_t myumask;
985 (void) me;
987 myumask = umask (022);
988 umask (myumask);
989 mode &= ~myumask;
991 st.st_mode = mode;
992 st.st_ino = 0;
993 st.st_dev = 0;
994 st.st_rdev = 0;
995 st.st_uid = getuid ();
996 st.st_gid = getgid ();
997 st.st_size = 0;
998 st.st_mtime = st.st_atime = st.st_ctime = time (NULL);
1000 return &st;
1003 /* --------------------------------------------------------------------------------------------- */
1005 struct vfs_s_entry *
1006 vfs_s_generate_entry (struct vfs_class *me, const char *name, struct vfs_s_inode *parent,
1007 mode_t mode)
1009 struct vfs_s_inode *inode;
1010 struct stat *st;
1012 st = vfs_s_default_stat (me, mode);
1013 inode = vfs_s_new_inode (me, parent->super, st);
1015 return vfs_s_new_entry (me, name, inode);
1018 /* --------------------------------------------------------------------------------------------- */
1020 struct vfs_s_inode *
1021 vfs_s_find_inode (struct vfs_class *me, const struct vfs_s_super *super,
1022 const char *path, int follow, int flags)
1024 struct vfs_s_entry *ent;
1026 if (((MEDATA->flags & VFS_S_REMOTE) == 0) && (*path == '\0'))
1027 return super->root;
1029 ent = (MEDATA->find_entry) (me, super->root, path, follow, flags);
1030 return (ent != NULL) ? ent->ino : NULL;
1033 /* --------------------------------------------------------------------------------------------- */
1034 /* Ook, these were functions around directory entries / inodes */
1035 /* -------------------------------- superblock games -------------------------- */
1037 * get superlock object by vpath
1039 * @param vpath path
1040 * @return superlock object or NULL if not found
1043 struct vfs_s_super *
1044 vfs_get_super_by_vpath (const vfs_path_t * vpath)
1046 GList *iter;
1047 void *cookie = NULL;
1048 const vfs_path_element_t *path_element;
1049 struct vfs_s_subclass *subclass;
1050 struct vfs_s_super *super = NULL;
1051 vfs_path_t *vpath_archive;
1053 path_element = vfs_path_get_by_index (vpath, -1);
1054 subclass = ((struct vfs_s_subclass *) path_element->class->data);
1055 if (subclass == NULL)
1056 return NULL;
1058 vpath_archive = vfs_path_clone (vpath);
1059 vfs_path_remove_element_by_index (vpath_archive, -1);
1061 if (subclass->archive_check != NULL)
1063 cookie = subclass->archive_check (vpath_archive);
1064 if (cookie == NULL)
1065 goto ret;
1068 for (iter = subclass->supers; iter != NULL; iter = g_list_next (iter))
1070 int i;
1072 super = (struct vfs_s_super *) iter->data;
1074 /* 0 == other, 1 == same, return it, 2 == other but stop scanning */
1075 i = subclass->archive_same (path_element, super, vpath_archive, cookie);
1076 if (i == 1)
1077 goto ret;
1078 if (i != 0)
1079 break;
1081 super = NULL;
1084 ret:
1085 vfs_path_free (vpath_archive);
1086 return super;
1089 /* --------------------------------------------------------------------------------------------- */
1091 * get path from last VFS-element and create corresponding superblock
1093 * @param vpath source path object
1094 * @param archive pointer to object for store newly created superblock
1095 * @param flags flags
1097 * @return path from last VFS-element
1099 const char *
1100 vfs_s_get_path (const vfs_path_t * vpath, struct vfs_s_super **archive, int flags)
1102 const char *retval = "";
1103 int result = -1;
1104 struct vfs_s_super *super;
1105 const vfs_path_element_t *path_element;
1106 struct vfs_s_subclass *subclass;
1108 path_element = vfs_path_get_by_index (vpath, -1);
1110 if (path_element->path != NULL)
1111 retval = path_element->path;
1113 super = vfs_get_super_by_vpath (vpath);
1114 if (super != NULL)
1115 goto return_success;
1117 if (flags & FL_NO_OPEN)
1119 path_element->class->verrno = EIO;
1120 return NULL;
1123 super = vfs_s_new_super (path_element->class);
1125 subclass = ((struct vfs_s_subclass *) path_element->class->data);
1126 if (subclass->open_archive != NULL)
1128 vfs_path_t *vpath_archive;
1130 vpath_archive = vfs_path_clone (vpath);
1131 vfs_path_remove_element_by_index (vpath_archive, -1);
1133 result = subclass->open_archive (super, vpath_archive, path_element);
1134 vfs_path_free (vpath_archive);
1136 if (result == -1)
1138 vfs_s_free_super (path_element->class, super);
1139 path_element->class->verrno = EIO;
1140 return NULL;
1142 if (!super->name)
1143 vfs_die ("You have to fill name\n");
1144 if (!super->root)
1145 vfs_die ("You have to fill root inode\n");
1147 vfs_s_insert_super (path_element->class, super);
1148 vfs_stamp_create (path_element->class, super);
1150 return_success:
1151 *archive = super;
1152 return retval;
1155 /* --------------------------------------------------------------------------------------------- */
1157 void
1158 vfs_s_invalidate (struct vfs_class *me, struct vfs_s_super *super)
1160 if (!super->want_stale)
1162 vfs_s_free_inode (me, super->root);
1163 super->root = vfs_s_new_inode (me, super, vfs_s_default_stat (me, S_IFDIR | 0755));
1167 /* --------------------------------------------------------------------------------------------- */
1169 char *
1170 vfs_s_fullpath (struct vfs_class *me, struct vfs_s_inode *ino)
1172 if (!ino->ent)
1173 ERRNOR (EAGAIN, NULL);
1175 if ((MEDATA->flags & VFS_S_USETMP) == 0)
1177 /* archives */
1178 char *newpath;
1179 char *path = g_strdup (ino->ent->name);
1180 while (1)
1182 ino = ino->ent->dir;
1183 if (ino == ino->super->root)
1184 break;
1185 newpath = g_strconcat (ino->ent->name, PATH_SEP_STR, path, (char *) NULL);
1186 g_free (path);
1187 path = newpath;
1189 return path;
1192 /* remote systems */
1193 if ((!ino->ent->dir) || (!ino->ent->dir->ent))
1194 return g_strdup (ino->ent->name);
1196 return g_strconcat (ino->ent->dir->ent->name, PATH_SEP_STR, ino->ent->name, (char *) NULL);
1199 /* --------------------------------------------------------------------------------------------- */
1200 /* --------------------------- stat and friends ---------------------------- */
1202 void *
1203 vfs_s_open (const vfs_path_t * vpath, int flags, mode_t mode)
1205 int was_changed = 0;
1206 vfs_file_handler_t *fh;
1207 struct vfs_s_super *super;
1208 const char *q;
1209 struct vfs_s_inode *ino;
1210 const vfs_path_element_t *path_element;
1212 path_element = vfs_path_get_by_index (vpath, -1);
1214 q = vfs_s_get_path (vpath, &super, 0);
1215 if (q == NULL)
1216 return NULL;
1217 ino = vfs_s_find_inode (path_element->class, super, q, LINK_FOLLOW, FL_NONE);
1218 if (ino && ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL)))
1220 path_element->class->verrno = EEXIST;
1221 return NULL;
1223 if (!ino)
1225 char *dirname, *name;
1226 struct vfs_s_entry *ent;
1227 struct vfs_s_inode *dir;
1229 /* If the filesystem is read-only, disable file creation */
1230 if (!(flags & O_CREAT) || !(path_element->class->write))
1231 return NULL;
1233 dirname = g_path_get_dirname (q);
1234 name = g_path_get_basename (q);
1235 dir = vfs_s_find_inode (path_element->class, super, dirname, LINK_FOLLOW, FL_DIR);
1236 if (dir == NULL)
1238 g_free (dirname);
1239 g_free (name);
1240 return NULL;
1242 ent = vfs_s_generate_entry (path_element->class, name, dir, 0755);
1243 ino = ent->ino;
1244 vfs_s_insert_entry (path_element->class, dir, ent);
1245 if ((VFSDATA (path_element)->flags & VFS_S_USETMP) != 0)
1247 int tmp_handle;
1248 vfs_path_t *tmp_vpath;
1250 tmp_handle = vfs_mkstemps (&tmp_vpath, path_element->class->name, name);
1251 ino->localname = g_strdup (vfs_path_as_str (tmp_vpath));
1252 vfs_path_free (tmp_vpath);
1253 if (tmp_handle == -1)
1255 g_free (dirname);
1256 g_free (name);
1257 return NULL;
1260 close (tmp_handle);
1262 g_free (dirname);
1263 g_free (name);
1264 was_changed = 1;
1267 if (S_ISDIR (ino->st.st_mode))
1269 path_element->class->verrno = EISDIR;
1270 return NULL;
1273 fh = g_new (vfs_file_handler_t, 1);
1274 fh->pos = 0;
1275 fh->ino = ino;
1276 fh->handle = -1;
1277 fh->changed = was_changed;
1278 fh->linear = 0;
1279 fh->data = NULL;
1281 if (IS_LINEAR (flags))
1283 if (VFSDATA (path_element)->linear_start)
1285 vfs_print_message ("%s", _("Starting linear transfer..."));
1286 fh->linear = LS_LINEAR_PREOPEN;
1289 else
1291 struct vfs_s_subclass *s;
1293 s = VFSDATA (path_element);
1294 if (s->fh_open != NULL && s->fh_open (path_element->class, fh, flags, mode) != 0)
1296 if (s->fh_free_data != NULL)
1297 s->fh_free_data (fh);
1298 g_free (fh);
1299 return NULL;
1303 if ((VFSDATA (path_element)->flags & VFS_S_USETMP) != 0 && fh->ino->localname != NULL)
1305 fh->handle = open (fh->ino->localname, NO_LINEAR (flags), mode);
1306 if (fh->handle == -1)
1308 g_free (fh);
1309 path_element->class->verrno = errno;
1310 return NULL;
1314 /* i.e. we had no open files and now we have one */
1315 vfs_rmstamp (path_element->class, (vfsid) super);
1316 super->fd_usage++;
1317 fh->ino->st.st_nlink++;
1318 return fh;
1321 /* --------------------------------------------------------------------------------------------- */
1324 vfs_s_retrieve_file (struct vfs_class *me, struct vfs_s_inode *ino)
1326 /* If you want reget, you'll have to open file with O_LINEAR */
1327 off_t total = 0;
1328 char buffer[8192];
1329 int handle;
1330 ssize_t n;
1331 off_t stat_size = ino->st.st_size;
1332 vfs_file_handler_t fh;
1333 vfs_path_t *tmp_vpath;
1335 if ((MEDATA->flags & VFS_S_USETMP) == 0)
1336 return -1;
1338 memset (&fh, 0, sizeof (fh));
1340 fh.ino = ino;
1341 fh.handle = -1;
1343 handle = vfs_mkstemps (&tmp_vpath, me->name, ino->ent->name);
1344 ino->localname = g_strdup (vfs_path_as_str (tmp_vpath));
1345 vfs_path_free (tmp_vpath);
1346 if (handle == -1)
1348 me->verrno = errno;
1349 goto error_4;
1352 if (!MEDATA->linear_start (me, &fh, 0))
1353 goto error_3;
1355 /* Clear the interrupt status */
1356 tty_got_interrupt ();
1357 tty_enable_interrupt_key ();
1359 while ((n = MEDATA->linear_read (me, &fh, buffer, sizeof (buffer))))
1361 int t;
1362 if (n < 0)
1363 goto error_1;
1365 total += n;
1366 vfs_s_print_stats (me->name, _("Getting file"), ino->ent->name, total, stat_size);
1368 if (tty_got_interrupt ())
1369 goto error_1;
1371 t = write (handle, buffer, n);
1372 if (t != n)
1374 if (t == -1)
1375 me->verrno = errno;
1376 goto error_1;
1379 MEDATA->linear_close (me, &fh);
1380 close (handle);
1382 tty_disable_interrupt_key ();
1383 g_free (fh.data);
1384 return 0;
1386 error_1:
1387 MEDATA->linear_close (me, &fh);
1388 error_3:
1389 tty_disable_interrupt_key ();
1390 close (handle);
1391 unlink (ino->localname);
1392 error_4:
1393 MC_PTR_FREE (ino->localname);
1394 g_free (fh.data);
1395 return -1;
1398 /* --------------------------------------------------------------------------------------------- */
1399 /* ----------------------------- Stamping support -------------------------- */
1401 /* Initialize one of our subclasses - fill common functions */
1402 void
1403 vfs_s_init_class (struct vfs_class *vclass, struct vfs_s_subclass *sub)
1405 vclass->data = sub;
1406 vclass->fill_names = vfs_s_fill_names;
1407 vclass->open = vfs_s_open;
1408 vclass->close = vfs_s_close;
1409 vclass->read = vfs_s_read;
1410 if (!(sub->flags & VFS_S_READONLY))
1412 vclass->write = vfs_s_write;
1414 vclass->opendir = vfs_s_opendir;
1415 vclass->readdir = vfs_s_readdir;
1416 vclass->closedir = vfs_s_closedir;
1417 vclass->stat = vfs_s_stat;
1418 vclass->lstat = vfs_s_lstat;
1419 vclass->fstat = vfs_s_fstat;
1420 vclass->readlink = vfs_s_readlink;
1421 vclass->chdir = vfs_s_chdir;
1422 vclass->ferrno = vfs_s_ferrno;
1423 vclass->lseek = vfs_s_lseek;
1424 vclass->getid = vfs_s_getid;
1425 vclass->nothingisopen = vfs_s_nothingisopen;
1426 vclass->free = vfs_s_free;
1427 if ((sub->flags & VFS_S_USETMP) != 0)
1429 vclass->getlocalcopy = vfs_s_getlocalcopy;
1430 vclass->ungetlocalcopy = vfs_s_ungetlocalcopy;
1431 sub->find_entry = vfs_s_find_entry_linear;
1433 else if ((sub->flags & VFS_S_REMOTE) != 0)
1434 sub->find_entry = vfs_s_find_entry_linear;
1435 else
1436 sub->find_entry = vfs_s_find_entry_tree;
1437 vclass->setctl = vfs_s_setctl;
1438 sub->dir_uptodate = vfs_s_dir_uptodate;
1441 /* --------------------------------------------------------------------------------------------- */
1442 /** Find VFS id for given directory name */
1444 vfsid
1445 vfs_getid (const vfs_path_t * vpath)
1447 const vfs_path_element_t *path_element;
1449 path_element = vfs_path_get_by_index (vpath, -1);
1450 if (!vfs_path_element_valid (path_element) || path_element->class->getid == NULL)
1451 return NULL;
1453 return (*path_element->class->getid) (vpath);
1456 /* --------------------------------------------------------------------------------------------- */
1457 /* ----------- Utility functions for networked filesystems -------------- */
1459 #ifdef ENABLE_VFS_NET
1461 vfs_s_select_on_two (int fd1, int fd2)
1463 fd_set set;
1464 struct timeval time_out;
1465 int v;
1466 int maxfd = (fd1 > fd2 ? fd1 : fd2) + 1;
1468 time_out.tv_sec = 1;
1469 time_out.tv_usec = 0;
1470 FD_ZERO (&set);
1471 FD_SET (fd1, &set);
1472 FD_SET (fd2, &set);
1473 v = select (maxfd, &set, 0, 0, &time_out);
1474 if (v <= 0)
1475 return v;
1476 if (FD_ISSET (fd1, &set))
1477 return 1;
1478 if (FD_ISSET (fd2, &set))
1479 return 2;
1480 return -1;
1483 /* --------------------------------------------------------------------------------------------- */
1486 vfs_s_get_line (struct vfs_class *me, int sock, char *buf, int buf_len, char term)
1488 FILE *logfile = MEDATA->logfile;
1489 int i;
1490 char c;
1492 for (i = 0; i < buf_len - 1; i++, buf++)
1494 if (read (sock, buf, sizeof (char)) <= 0)
1495 return 0;
1496 if (logfile)
1498 size_t ret1;
1499 int ret2;
1500 ret1 = fwrite (buf, 1, 1, logfile);
1501 ret2 = fflush (logfile);
1502 (void) ret1;
1503 (void) ret2;
1505 if (*buf == term)
1507 *buf = 0;
1508 return 1;
1512 /* Line is too long - terminate buffer and discard the rest of line */
1513 *buf = 0;
1514 while (read (sock, &c, sizeof (c)) > 0)
1516 if (logfile)
1518 size_t ret1;
1519 int ret2;
1520 ret1 = fwrite (&c, 1, 1, logfile);
1521 ret2 = fflush (logfile);
1522 (void) ret1;
1523 (void) ret2;
1525 if (c == '\n')
1526 return 1;
1528 return 0;
1531 /* --------------------------------------------------------------------------------------------- */
1534 vfs_s_get_line_interruptible (struct vfs_class *me, char *buffer, int size, int fd)
1536 int i;
1537 int res = 0;
1539 (void) me;
1541 tty_enable_interrupt_key ();
1543 for (i = 0; i < size - 1; i++)
1545 ssize_t n;
1547 n = read (fd, &buffer[i], 1);
1548 if (n == -1 && errno == EINTR)
1550 buffer[i] = '\0';
1551 res = EINTR;
1552 goto ret;
1554 if (n == 0)
1556 buffer[i] = '\0';
1557 goto ret;
1559 if (buffer[i] == '\n')
1561 buffer[i] = '\0';
1562 res = 1;
1563 goto ret;
1567 buffer[size - 1] = '\0';
1569 ret:
1570 tty_disable_interrupt_key ();
1572 return res;
1574 #endif /* ENABLE_VFS_NET */
1576 /* --------------------------------------------------------------------------------------------- */
1578 * Normalize filenames start position
1581 void
1582 vfs_s_normalize_filename_leading_spaces (struct vfs_s_inode *root_inode, size_t final_num_spaces)
1584 GList *iter;
1586 for (iter = root_inode->subdir; iter != NULL; iter = g_list_next (iter))
1588 struct vfs_s_entry *entry = (struct vfs_s_entry *) iter->data;
1589 if ((size_t) entry->ino->data_offset > final_num_spaces)
1591 char *source_name = entry->name;
1592 char *spacer = g_strnfill (entry->ino->data_offset - final_num_spaces, ' ');
1593 entry->name = g_strdup_printf ("%s%s", spacer, source_name);
1594 g_free (spacer);
1595 g_free (source_name);
1597 entry->ino->data_offset = -1;
1601 /* --------------------------------------------------------------------------------------------- */