Merge branch '3684_mc_profile_root'
[midnight-commander.git] / lib / vfs / direntry.c
blob46cbcb211e5072e0d01d2174d61ab8bfcdfd9710
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) != 0 ? (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) != 0 ? LINK_FOLLOW : LINK_NO_FOLLOW,
387 flags & ~FL_FOLLOW);
388 if ((!ino) && (!*q))
389 /* We are asking about / directory of ftp server: assume it exists */
390 ino =
391 vfs_s_find_inode (path_element->class, super, q,
392 (flags & FL_FOLLOW) != 0 ? LINK_FOLLOW : LINK_NO_FOLLOW,
393 FL_DIR | (flags & ~FL_FOLLOW));
394 return ino;
397 /* --------------------------------------------------------------------------------------------- */
399 static void *
400 vfs_s_opendir (const vfs_path_t * vpath)
402 struct vfs_s_inode *dir;
403 struct dirhandle *info;
404 const vfs_path_element_t *path_element;
406 path_element = vfs_path_get_by_index (vpath, -1);
408 dir = vfs_s_inode_from_path (vpath, FL_DIR | FL_FOLLOW);
409 if (dir == NULL)
410 return NULL;
411 if (!S_ISDIR (dir->st.st_mode))
413 path_element->class->verrno = ENOTDIR;
414 return NULL;
417 dir->st.st_nlink++;
418 #if 0
419 if (dir->subdir == NULL) /* This can actually happen if we allow empty directories */
421 path_element->class->verrno = EAGAIN;
422 return NULL;
424 #endif
425 info = g_new (struct dirhandle, 1);
426 info->cur = dir->subdir;
427 info->dir = dir;
429 return info;
432 /* --------------------------------------------------------------------------------------------- */
434 static void *
435 vfs_s_readdir (void *data)
437 static union vfs_dirent dir;
438 struct dirhandle *info = (struct dirhandle *) data;
439 const char *name;
441 if (info->cur == NULL || info->cur->data == NULL)
442 return NULL;
444 name = ((struct vfs_s_entry *) info->cur->data)->name;
445 if (name != NULL)
446 g_strlcpy (dir.dent.d_name, name, MC_MAXPATHLEN);
447 else
448 vfs_die ("Null in structure-cannot happen");
450 info->cur = g_list_next (info->cur);
452 return (void *) &dir;
455 /* --------------------------------------------------------------------------------------------- */
457 static int
458 vfs_s_closedir (void *data)
460 struct dirhandle *info = (struct dirhandle *) data;
461 struct vfs_s_inode *dir = info->dir;
463 vfs_s_free_inode (dir->super->me, dir);
464 g_free (data);
465 return 0;
468 /* --------------------------------------------------------------------------------------------- */
470 static int
471 vfs_s_chdir (const vfs_path_t * vpath)
473 void *data;
475 data = vfs_s_opendir (vpath);
476 if (data == NULL)
477 return -1;
478 vfs_s_closedir (data);
479 return 0;
482 /* --------------------------------------------------------------------------------------------- */
483 /* --------------------------- stat and friends ---------------------------- */
485 static int
486 vfs_s_internal_stat (const vfs_path_t * vpath, struct stat *buf, int flag)
488 struct vfs_s_inode *ino;
490 ino = vfs_s_inode_from_path (vpath, flag);
491 if (ino == NULL)
492 return -1;
493 *buf = ino->st;
494 return 0;
497 /* --------------------------------------------------------------------------------------------- */
499 static int
500 vfs_s_stat (const vfs_path_t * vpath, struct stat *buf)
502 return vfs_s_internal_stat (vpath, buf, FL_FOLLOW);
505 /* --------------------------------------------------------------------------------------------- */
507 static int
508 vfs_s_lstat (const vfs_path_t * vpath, struct stat *buf)
510 return vfs_s_internal_stat (vpath, buf, FL_NONE);
513 /* --------------------------------------------------------------------------------------------- */
515 static int
516 vfs_s_fstat (void *fh, struct stat *buf)
518 *buf = FH->ino->st;
519 return 0;
522 /* --------------------------------------------------------------------------------------------- */
524 static int
525 vfs_s_readlink (const vfs_path_t * vpath, char *buf, size_t size)
527 struct vfs_s_inode *ino;
528 size_t len;
529 const vfs_path_element_t *path_element;
531 path_element = vfs_path_get_by_index (vpath, -1);
533 ino = vfs_s_inode_from_path (vpath, 0);
534 if (!ino)
535 return -1;
537 if (!S_ISLNK (ino->st.st_mode))
539 path_element->class->verrno = EINVAL;
540 return -1;
543 if (ino->linkname == NULL)
545 path_element->class->verrno = EFAULT;
546 return -1;
549 len = strlen (ino->linkname);
550 if (size < len)
551 len = size;
552 /* readlink() does not append a NUL character to buf */
553 memcpy (buf, ino->linkname, len);
554 return len;
557 /* --------------------------------------------------------------------------------------------- */
559 static ssize_t
560 vfs_s_read (void *fh, char *buffer, size_t count)
562 struct vfs_class *me = FH_SUPER->me;
564 if (FH->linear == LS_LINEAR_PREOPEN)
566 if (!MEDATA->linear_start (me, FH, FH->pos))
567 return -1;
570 if (FH->linear == LS_LINEAR_CLOSED)
571 vfs_die ("linear_start() did not set linear_state!");
573 if (FH->linear == LS_LINEAR_OPEN)
574 return MEDATA->linear_read (me, FH, buffer, count);
576 if (FH->handle != -1)
578 ssize_t n;
580 n = read (FH->handle, buffer, count);
581 if (n < 0)
582 me->verrno = errno;
583 return n;
585 vfs_die ("vfs_s_read: This should not happen\n");
586 return -1;
589 /* --------------------------------------------------------------------------------------------- */
591 static ssize_t
592 vfs_s_write (void *fh, const char *buffer, size_t count)
594 struct vfs_class *me = FH_SUPER->me;
596 if (FH->linear)
597 vfs_die ("no writing to linear files, please");
599 FH->changed = 1;
600 if (FH->handle != -1)
602 ssize_t n;
604 n = write (FH->handle, buffer, count);
605 if (n < 0)
606 me->verrno = errno;
607 return n;
609 vfs_die ("vfs_s_write: This should not happen\n");
610 return 0;
613 /* --------------------------------------------------------------------------------------------- */
615 static off_t
616 vfs_s_lseek (void *fh, off_t offset, int whence)
618 off_t size = FH->ino->st.st_size;
620 if (FH->linear == LS_LINEAR_OPEN)
621 vfs_die ("cannot lseek() after linear_read!");
623 if (FH->handle != -1)
624 { /* If we have local file opened, we want to work with it */
625 off_t retval = lseek (FH->handle, offset, whence);
626 if (retval == -1)
627 FH->ino->super->me->verrno = errno;
628 return retval;
631 switch (whence)
633 case SEEK_CUR:
634 offset += FH->pos;
635 break;
636 case SEEK_END:
637 offset += size;
638 break;
639 default:
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 if (me == NULL)
660 return (-1);
662 FH_SUPER->fd_usage--;
663 if (!FH_SUPER->fd_usage)
664 vfs_stamp_create (me, FH_SUPER);
666 if (FH->linear == LS_LINEAR_OPEN)
667 MEDATA->linear_close (me, fh);
668 if (MEDATA->fh_close)
669 res = MEDATA->fh_close (me, fh);
670 if ((MEDATA->flags & VFS_S_USETMP) && FH->changed && MEDATA->file_store)
672 char *s = vfs_s_fullpath (me, FH->ino);
673 if (!s)
674 res = -1;
675 else
677 res = MEDATA->file_store (me, fh, s, FH->ino->localname);
678 g_free (s);
680 vfs_s_invalidate (me, FH_SUPER);
682 if (FH->handle != -1)
683 close (FH->handle);
685 vfs_s_free_inode (me, FH->ino);
686 if (MEDATA->fh_free_data != NULL)
687 MEDATA->fh_free_data (fh);
688 g_free (fh);
689 return res;
692 /* --------------------------------------------------------------------------------------------- */
694 static void
695 vfs_s_print_stats (const char *fs_name, const char *action,
696 const char *file_name, off_t have, off_t need)
698 if (need != 0)
699 vfs_print_message (_("%s: %s: %s %3d%% (%lld) bytes transferred"), fs_name, action,
700 file_name, (int) ((double) have * 100 / need), (long long) have);
701 else
702 vfs_print_message (_("%s: %s: %s %lld bytes transferred"), fs_name, action, file_name,
703 (long long) have);
706 /* --------------------------------------------------------------------------------------------- */
707 /* ------------------------------- mc support ---------------------------- */
709 static void
710 vfs_s_fill_names (struct vfs_class *me, fill_names_f func)
712 GList *iter;
714 for (iter = MEDATA->supers; iter != NULL; iter = g_list_next (iter))
716 const struct vfs_s_super *super = (const struct vfs_s_super *) iter->data;
717 char *name;
719 name = g_strconcat (super->name, PATH_SEP_STR, me->prefix, VFS_PATH_URL_DELIMITER,
720 /* super->current_dir->name, */ (char *) NULL);
721 func (name);
722 g_free (name);
726 /* --------------------------------------------------------------------------------------------- */
728 static int
729 vfs_s_ferrno (struct vfs_class *me)
731 return me->verrno;
734 /* --------------------------------------------------------------------------------------------- */
736 * Get local copy of the given file. We reuse the existing file cache
737 * for remote filesystems. Archives use standard VFS facilities.
740 static vfs_path_t *
741 vfs_s_getlocalcopy (const vfs_path_t * vpath)
743 vfs_file_handler_t *fh;
744 vfs_path_t *local = NULL;
746 if (vpath == NULL)
747 return NULL;
749 fh = vfs_s_open (vpath, O_RDONLY, 0);
751 if (fh != NULL)
753 const struct vfs_class *me;
755 me = vfs_path_get_by_index (vpath, -1)->class;
756 if ((MEDATA->flags & VFS_S_USETMP) != 0 && (fh->ino != NULL))
757 local = vfs_path_from_str_flags (fh->ino->localname, VPF_NO_CANON);
759 vfs_s_close (fh);
762 return local;
765 /* --------------------------------------------------------------------------------------------- */
767 * Return the local copy. Since we are using our cache, we do nothing -
768 * the cache will be removed when the archive is closed.
771 static int
772 vfs_s_ungetlocalcopy (const vfs_path_t * vpath, const vfs_path_t * local, gboolean has_changed)
774 (void) vpath;
775 (void) local;
776 (void) has_changed;
777 return 0;
780 /* --------------------------------------------------------------------------------------------- */
782 static int
783 vfs_s_setctl (const vfs_path_t * vpath, int ctlop, void *arg)
785 const vfs_path_element_t *path_element;
787 path_element = vfs_path_get_by_index (vpath, -1);
789 switch (ctlop)
791 case VFS_SETCTL_STALE_DATA:
793 struct vfs_s_inode *ino;
795 ino = vfs_s_inode_from_path (vpath, 0);
796 if (ino == NULL)
797 return 0;
798 if (arg)
799 ino->super->want_stale = 1;
800 else
802 ino->super->want_stale = 0;
803 vfs_s_invalidate (path_element->class, ino->super);
805 return 1;
807 case VFS_SETCTL_LOGFILE:
808 ((struct vfs_s_subclass *) path_element->class->data)->logfile = fopen ((char *) arg, "w");
809 return 1;
810 case VFS_SETCTL_FLUSH:
811 ((struct vfs_s_subclass *) path_element->class->data)->flush = 1;
812 return 1;
813 default:
814 return 0;
818 /* --------------------------------------------------------------------------------------------- */
819 /* ----------------------------- Stamping support -------------------------- */
821 static vfsid
822 vfs_s_getid (const vfs_path_t * vpath)
824 struct vfs_s_super *archive = NULL;
825 const char *p;
827 p = vfs_s_get_path (vpath, &archive, FL_NO_OPEN);
828 if (p == NULL)
829 return NULL;
831 return (vfsid) archive;
834 /* --------------------------------------------------------------------------------------------- */
836 static int
837 vfs_s_nothingisopen (vfsid id)
839 (void) id;
840 /* Our data structures should survive free of superblock at any time */
841 return 1;
844 /* --------------------------------------------------------------------------------------------- */
846 static void
847 vfs_s_free (vfsid id)
849 vfs_s_free_super (((struct vfs_s_super *) id)->me, (struct vfs_s_super *) id);
852 /* --------------------------------------------------------------------------------------------- */
854 static int
855 vfs_s_dir_uptodate (struct vfs_class *me, struct vfs_s_inode *ino)
857 struct timeval tim;
859 if (MEDATA->flush)
861 MEDATA->flush = 0;
862 return 0;
865 gettimeofday (&tim, NULL);
866 if (tim.tv_sec < ino->timestamp.tv_sec)
867 return 1;
868 return 0;
872 /* --------------------------------------------------------------------------------------------- */
873 /*** public functions ****************************************************************************/
874 /* --------------------------------------------------------------------------------------------- */
876 struct vfs_s_inode *
877 vfs_s_new_inode (struct vfs_class *me, struct vfs_s_super *super, struct stat *initstat)
879 struct vfs_s_inode *ino;
881 ino = g_try_new0 (struct vfs_s_inode, 1);
882 if (ino == NULL)
883 return NULL;
885 if (initstat)
886 ino->st = *initstat;
887 ino->super = super;
888 ino->st.st_nlink = 0;
889 ino->st.st_ino = MEDATA->inode_counter++;
890 ino->st.st_dev = MEDATA->rdev;
892 super->ino_usage++;
894 CALL (init_inode) (me, ino);
896 return ino;
899 /* --------------------------------------------------------------------------------------------- */
901 void
902 vfs_s_free_inode (struct vfs_class *me, struct vfs_s_inode *ino)
904 if (ino == NULL)
905 vfs_die ("Don't pass NULL to me");
907 /* ==0 can happen if freshly created entry is deleted */
908 if (ino->st.st_nlink > 1)
910 ino->st.st_nlink--;
911 return;
914 while (ino->subdir != NULL)
915 vfs_s_free_entry (me, (struct vfs_s_entry *) ino->subdir->data);
917 CALL (free_inode) (me, ino);
918 g_free (ino->linkname);
919 if ((MEDATA->flags & VFS_S_USETMP) != 0 && ino->localname != NULL)
921 unlink (ino->localname);
922 g_free (ino->localname);
924 ino->super->ino_usage--;
925 g_free (ino);
928 /* --------------------------------------------------------------------------------------------- */
930 struct vfs_s_entry *
931 vfs_s_new_entry (struct vfs_class *me, const char *name, struct vfs_s_inode *inode)
933 struct vfs_s_entry *entry;
935 entry = g_new0 (struct vfs_s_entry, 1);
937 entry->name = g_strdup (name);
938 entry->ino = inode;
939 entry->ino->ent = entry;
940 CALL (init_entry) (me, entry);
942 return entry;
946 /* --------------------------------------------------------------------------------------------- */
948 void
949 vfs_s_free_entry (struct vfs_class *me, struct vfs_s_entry *ent)
951 if (ent->dir != NULL)
952 ent->dir->subdir = g_list_remove (ent->dir->subdir, ent);
954 MC_PTR_FREE (ent->name);
956 if (ent->ino != NULL)
958 ent->ino->ent = NULL;
959 vfs_s_free_inode (me, ent->ino);
962 g_free (ent);
965 /* --------------------------------------------------------------------------------------------- */
967 void
968 vfs_s_insert_entry (struct vfs_class *me, struct vfs_s_inode *dir, struct vfs_s_entry *ent)
970 (void) me;
972 ent->dir = dir;
974 ent->ino->st.st_nlink++;
975 dir->subdir = g_list_append (dir->subdir, ent);
978 /* --------------------------------------------------------------------------------------------- */
980 struct stat *
981 vfs_s_default_stat (struct vfs_class *me, mode_t mode)
983 static struct stat st;
984 mode_t myumask;
986 (void) me;
988 myumask = umask (022);
989 umask (myumask);
990 mode &= ~myumask;
992 st.st_mode = mode;
993 st.st_ino = 0;
994 st.st_dev = 0;
995 st.st_rdev = 0;
996 st.st_uid = getuid ();
997 st.st_gid = getgid ();
998 st.st_size = 0;
999 st.st_mtime = st.st_atime = st.st_ctime = time (NULL);
1001 return &st;
1004 /* --------------------------------------------------------------------------------------------- */
1006 struct vfs_s_entry *
1007 vfs_s_generate_entry (struct vfs_class *me, const char *name, struct vfs_s_inode *parent,
1008 mode_t mode)
1010 struct vfs_s_inode *inode;
1011 struct stat *st;
1013 st = vfs_s_default_stat (me, mode);
1014 inode = vfs_s_new_inode (me, parent->super, st);
1016 return vfs_s_new_entry (me, name, inode);
1019 /* --------------------------------------------------------------------------------------------- */
1021 struct vfs_s_inode *
1022 vfs_s_find_inode (struct vfs_class *me, const struct vfs_s_super *super,
1023 const char *path, int follow, int flags)
1025 struct vfs_s_entry *ent;
1027 if (((MEDATA->flags & VFS_S_REMOTE) == 0) && (*path == '\0'))
1028 return super->root;
1030 ent = (MEDATA->find_entry) (me, super->root, path, follow, flags);
1031 return (ent != NULL) ? ent->ino : NULL;
1034 /* --------------------------------------------------------------------------------------------- */
1035 /* Ook, these were functions around directory entries / inodes */
1036 /* -------------------------------- superblock games -------------------------- */
1038 * get superlock object by vpath
1040 * @param vpath path
1041 * @return superlock object or NULL if not found
1044 struct vfs_s_super *
1045 vfs_get_super_by_vpath (const vfs_path_t * vpath)
1047 GList *iter;
1048 void *cookie = NULL;
1049 const vfs_path_element_t *path_element;
1050 struct vfs_s_subclass *subclass;
1051 struct vfs_s_super *super = NULL;
1052 vfs_path_t *vpath_archive;
1054 path_element = vfs_path_get_by_index (vpath, -1);
1055 subclass = ((struct vfs_s_subclass *) path_element->class->data);
1056 if (subclass == NULL)
1057 return NULL;
1059 vpath_archive = vfs_path_clone (vpath);
1060 vfs_path_remove_element_by_index (vpath_archive, -1);
1062 if (subclass->archive_check != NULL)
1064 cookie = subclass->archive_check (vpath_archive);
1065 if (cookie == NULL)
1066 goto ret;
1069 for (iter = subclass->supers; iter != NULL; iter = g_list_next (iter))
1071 int i;
1073 super = (struct vfs_s_super *) iter->data;
1075 /* 0 == other, 1 == same, return it, 2 == other but stop scanning */
1076 i = subclass->archive_same (path_element, super, vpath_archive, cookie);
1077 if (i == 1)
1078 goto ret;
1079 if (i != 0)
1080 break;
1082 super = NULL;
1085 ret:
1086 vfs_path_free (vpath_archive);
1087 return super;
1090 /* --------------------------------------------------------------------------------------------- */
1092 * get path from last VFS-element and create corresponding superblock
1094 * @param vpath source path object
1095 * @param archive pointer to object for store newly created superblock
1096 * @param flags flags
1098 * @return path from last VFS-element
1100 const char *
1101 vfs_s_get_path (const vfs_path_t * vpath, struct vfs_s_super **archive, int flags)
1103 const char *retval = "";
1104 int result = -1;
1105 struct vfs_s_super *super;
1106 const vfs_path_element_t *path_element;
1107 struct vfs_s_subclass *subclass;
1109 path_element = vfs_path_get_by_index (vpath, -1);
1111 if (path_element->path != NULL)
1112 retval = path_element->path;
1114 super = vfs_get_super_by_vpath (vpath);
1115 if (super != NULL)
1116 goto return_success;
1118 if (flags & FL_NO_OPEN)
1120 path_element->class->verrno = EIO;
1121 return NULL;
1124 super = vfs_s_new_super (path_element->class);
1126 subclass = ((struct vfs_s_subclass *) path_element->class->data);
1127 if (subclass->open_archive != NULL)
1129 vfs_path_t *vpath_archive;
1131 vpath_archive = vfs_path_clone (vpath);
1132 vfs_path_remove_element_by_index (vpath_archive, -1);
1134 result = subclass->open_archive (super, vpath_archive, path_element);
1135 vfs_path_free (vpath_archive);
1137 if (result == -1)
1139 vfs_s_free_super (path_element->class, super);
1140 path_element->class->verrno = EIO;
1141 return NULL;
1143 if (!super->name)
1144 vfs_die ("You have to fill name\n");
1145 if (!super->root)
1146 vfs_die ("You have to fill root inode\n");
1148 vfs_s_insert_super (path_element->class, super);
1149 vfs_stamp_create (path_element->class, super);
1151 return_success:
1152 *archive = super;
1153 return retval;
1156 /* --------------------------------------------------------------------------------------------- */
1158 void
1159 vfs_s_invalidate (struct vfs_class *me, struct vfs_s_super *super)
1161 if (!super->want_stale)
1163 vfs_s_free_inode (me, super->root);
1164 super->root = vfs_s_new_inode (me, super, vfs_s_default_stat (me, S_IFDIR | 0755));
1168 /* --------------------------------------------------------------------------------------------- */
1170 char *
1171 vfs_s_fullpath (struct vfs_class *me, struct vfs_s_inode *ino)
1173 if (!ino->ent)
1174 ERRNOR (EAGAIN, NULL);
1176 if ((MEDATA->flags & VFS_S_USETMP) == 0)
1178 /* archives */
1179 char *newpath;
1180 char *path = g_strdup (ino->ent->name);
1181 while (1)
1183 ino = ino->ent->dir;
1184 if (ino == ino->super->root)
1185 break;
1186 newpath = g_strconcat (ino->ent->name, PATH_SEP_STR, path, (char *) NULL);
1187 g_free (path);
1188 path = newpath;
1190 return path;
1193 /* remote systems */
1194 if ((!ino->ent->dir) || (!ino->ent->dir->ent))
1195 return g_strdup (ino->ent->name);
1197 return g_strconcat (ino->ent->dir->ent->name, PATH_SEP_STR, ino->ent->name, (char *) NULL);
1200 /* --------------------------------------------------------------------------------------------- */
1201 /* --------------------------- stat and friends ---------------------------- */
1203 void *
1204 vfs_s_open (const vfs_path_t * vpath, int flags, mode_t mode)
1206 int was_changed = 0;
1207 vfs_file_handler_t *fh;
1208 struct vfs_s_super *super;
1209 const char *q;
1210 struct vfs_s_inode *ino;
1211 const vfs_path_element_t *path_element;
1213 path_element = vfs_path_get_by_index (vpath, -1);
1215 q = vfs_s_get_path (vpath, &super, 0);
1216 if (q == NULL)
1217 return NULL;
1218 ino = vfs_s_find_inode (path_element->class, super, q, LINK_FOLLOW, FL_NONE);
1219 if (ino && ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL)))
1221 path_element->class->verrno = EEXIST;
1222 return NULL;
1224 if (!ino)
1226 char *dirname, *name;
1227 struct vfs_s_entry *ent;
1228 struct vfs_s_inode *dir;
1230 /* If the filesystem is read-only, disable file creation */
1231 if (!(flags & O_CREAT) || !(path_element->class->write))
1232 return NULL;
1234 dirname = g_path_get_dirname (q);
1235 name = g_path_get_basename (q);
1236 dir = vfs_s_find_inode (path_element->class, super, dirname, LINK_FOLLOW, FL_DIR);
1237 if (dir == NULL)
1239 g_free (dirname);
1240 g_free (name);
1241 return NULL;
1243 ent = vfs_s_generate_entry (path_element->class, name, dir, 0755);
1244 ino = ent->ino;
1245 vfs_s_insert_entry (path_element->class, dir, ent);
1246 if ((VFSDATA (path_element)->flags & VFS_S_USETMP) != 0)
1248 int tmp_handle;
1249 vfs_path_t *tmp_vpath;
1251 tmp_handle = vfs_mkstemps (&tmp_vpath, path_element->class->name, name);
1252 ino->localname = g_strdup (vfs_path_as_str (tmp_vpath));
1253 vfs_path_free (tmp_vpath);
1254 if (tmp_handle == -1)
1256 g_free (dirname);
1257 g_free (name);
1258 return NULL;
1261 close (tmp_handle);
1263 g_free (dirname);
1264 g_free (name);
1265 was_changed = 1;
1268 if (S_ISDIR (ino->st.st_mode))
1270 path_element->class->verrno = EISDIR;
1271 return NULL;
1274 fh = g_new (vfs_file_handler_t, 1);
1275 fh->pos = 0;
1276 fh->ino = ino;
1277 fh->handle = -1;
1278 fh->changed = was_changed;
1279 fh->linear = 0;
1280 fh->data = NULL;
1282 if (IS_LINEAR (flags))
1284 if (VFSDATA (path_element)->linear_start)
1286 vfs_print_message ("%s", _("Starting linear transfer..."));
1287 fh->linear = LS_LINEAR_PREOPEN;
1290 else
1292 struct vfs_s_subclass *s;
1294 s = VFSDATA (path_element);
1295 if (s->fh_open != NULL && s->fh_open (path_element->class, fh, flags, mode) != 0)
1297 if (s->fh_free_data != NULL)
1298 s->fh_free_data (fh);
1299 g_free (fh);
1300 return NULL;
1304 if ((VFSDATA (path_element)->flags & VFS_S_USETMP) != 0 && fh->ino->localname != NULL)
1306 fh->handle = open (fh->ino->localname, NO_LINEAR (flags), mode);
1307 if (fh->handle == -1)
1309 g_free (fh);
1310 path_element->class->verrno = errno;
1311 return NULL;
1315 /* i.e. we had no open files and now we have one */
1316 vfs_rmstamp (path_element->class, (vfsid) super);
1317 super->fd_usage++;
1318 fh->ino->st.st_nlink++;
1319 return fh;
1322 /* --------------------------------------------------------------------------------------------- */
1325 vfs_s_retrieve_file (struct vfs_class *me, struct vfs_s_inode *ino)
1327 /* If you want reget, you'll have to open file with O_LINEAR */
1328 off_t total = 0;
1329 char buffer[8192];
1330 int handle;
1331 ssize_t n;
1332 off_t stat_size = ino->st.st_size;
1333 vfs_file_handler_t fh;
1334 vfs_path_t *tmp_vpath;
1336 if ((MEDATA->flags & VFS_S_USETMP) == 0)
1337 return -1;
1339 memset (&fh, 0, sizeof (fh));
1341 fh.ino = ino;
1342 fh.handle = -1;
1344 handle = vfs_mkstemps (&tmp_vpath, me->name, ino->ent->name);
1345 ino->localname = g_strdup (vfs_path_as_str (tmp_vpath));
1346 vfs_path_free (tmp_vpath);
1347 if (handle == -1)
1349 me->verrno = errno;
1350 goto error_4;
1353 if (!MEDATA->linear_start (me, &fh, 0))
1354 goto error_3;
1356 /* Clear the interrupt status */
1357 tty_got_interrupt ();
1358 tty_enable_interrupt_key ();
1360 while ((n = MEDATA->linear_read (me, &fh, buffer, sizeof (buffer))))
1362 int t;
1363 if (n < 0)
1364 goto error_1;
1366 total += n;
1367 vfs_s_print_stats (me->name, _("Getting file"), ino->ent->name, total, stat_size);
1369 if (tty_got_interrupt ())
1370 goto error_1;
1372 t = write (handle, buffer, n);
1373 if (t != n)
1375 if (t == -1)
1376 me->verrno = errno;
1377 goto error_1;
1380 MEDATA->linear_close (me, &fh);
1381 close (handle);
1383 tty_disable_interrupt_key ();
1384 g_free (fh.data);
1385 return 0;
1387 error_1:
1388 MEDATA->linear_close (me, &fh);
1389 error_3:
1390 tty_disable_interrupt_key ();
1391 close (handle);
1392 unlink (ino->localname);
1393 error_4:
1394 MC_PTR_FREE (ino->localname);
1395 g_free (fh.data);
1396 return -1;
1399 /* --------------------------------------------------------------------------------------------- */
1400 /* ----------------------------- Stamping support -------------------------- */
1402 /* Initialize one of our subclasses - fill common functions */
1403 void
1404 vfs_s_init_class (struct vfs_class *vclass, struct vfs_s_subclass *sub)
1406 vclass->data = sub;
1407 vclass->fill_names = vfs_s_fill_names;
1408 vclass->open = vfs_s_open;
1409 vclass->close = vfs_s_close;
1410 vclass->read = vfs_s_read;
1411 if (!(sub->flags & VFS_S_READONLY))
1413 vclass->write = vfs_s_write;
1415 vclass->opendir = vfs_s_opendir;
1416 vclass->readdir = vfs_s_readdir;
1417 vclass->closedir = vfs_s_closedir;
1418 vclass->stat = vfs_s_stat;
1419 vclass->lstat = vfs_s_lstat;
1420 vclass->fstat = vfs_s_fstat;
1421 vclass->readlink = vfs_s_readlink;
1422 vclass->chdir = vfs_s_chdir;
1423 vclass->ferrno = vfs_s_ferrno;
1424 vclass->lseek = vfs_s_lseek;
1425 vclass->getid = vfs_s_getid;
1426 vclass->nothingisopen = vfs_s_nothingisopen;
1427 vclass->free = vfs_s_free;
1428 if ((sub->flags & VFS_S_USETMP) != 0)
1430 vclass->getlocalcopy = vfs_s_getlocalcopy;
1431 vclass->ungetlocalcopy = vfs_s_ungetlocalcopy;
1432 sub->find_entry = vfs_s_find_entry_linear;
1434 else if ((sub->flags & VFS_S_REMOTE) != 0)
1435 sub->find_entry = vfs_s_find_entry_linear;
1436 else
1437 sub->find_entry = vfs_s_find_entry_tree;
1438 vclass->setctl = vfs_s_setctl;
1439 sub->dir_uptodate = vfs_s_dir_uptodate;
1442 /* --------------------------------------------------------------------------------------------- */
1443 /** Find VFS id for given directory name */
1445 vfsid
1446 vfs_getid (const vfs_path_t * vpath)
1448 const vfs_path_element_t *path_element;
1450 path_element = vfs_path_get_by_index (vpath, -1);
1451 if (!vfs_path_element_valid (path_element) || path_element->class->getid == NULL)
1452 return NULL;
1454 return (*path_element->class->getid) (vpath);
1457 /* --------------------------------------------------------------------------------------------- */
1458 /* ----------- Utility functions for networked filesystems -------------- */
1460 #ifdef ENABLE_VFS_NET
1462 vfs_s_select_on_two (int fd1, int fd2)
1464 fd_set set;
1465 struct timeval time_out;
1466 int v;
1467 int maxfd = (fd1 > fd2 ? fd1 : fd2) + 1;
1469 time_out.tv_sec = 1;
1470 time_out.tv_usec = 0;
1471 FD_ZERO (&set);
1472 FD_SET (fd1, &set);
1473 FD_SET (fd2, &set);
1474 v = select (maxfd, &set, 0, 0, &time_out);
1475 if (v <= 0)
1476 return v;
1477 if (FD_ISSET (fd1, &set))
1478 return 1;
1479 if (FD_ISSET (fd2, &set))
1480 return 2;
1481 return -1;
1484 /* --------------------------------------------------------------------------------------------- */
1487 vfs_s_get_line (struct vfs_class *me, int sock, char *buf, int buf_len, char term)
1489 FILE *logfile = MEDATA->logfile;
1490 int i;
1491 char c;
1493 for (i = 0; i < buf_len - 1; i++, buf++)
1495 if (read (sock, buf, sizeof (char)) <= 0)
1496 return 0;
1497 if (logfile)
1499 size_t ret1;
1500 int ret2;
1501 ret1 = fwrite (buf, 1, 1, logfile);
1502 ret2 = fflush (logfile);
1503 (void) ret1;
1504 (void) ret2;
1506 if (*buf == term)
1508 *buf = 0;
1509 return 1;
1513 /* Line is too long - terminate buffer and discard the rest of line */
1514 *buf = 0;
1515 while (read (sock, &c, sizeof (c)) > 0)
1517 if (logfile)
1519 size_t ret1;
1520 int ret2;
1521 ret1 = fwrite (&c, 1, 1, logfile);
1522 ret2 = fflush (logfile);
1523 (void) ret1;
1524 (void) ret2;
1526 if (c == '\n')
1527 return 1;
1529 return 0;
1532 /* --------------------------------------------------------------------------------------------- */
1535 vfs_s_get_line_interruptible (struct vfs_class *me, char *buffer, int size, int fd)
1537 int i;
1538 int res = 0;
1540 (void) me;
1542 tty_enable_interrupt_key ();
1544 for (i = 0; i < size - 1; i++)
1546 ssize_t n;
1548 n = read (fd, &buffer[i], 1);
1549 if (n == -1 && errno == EINTR)
1551 buffer[i] = '\0';
1552 res = EINTR;
1553 goto ret;
1555 if (n == 0)
1557 buffer[i] = '\0';
1558 goto ret;
1560 if (buffer[i] == '\n')
1562 buffer[i] = '\0';
1563 res = 1;
1564 goto ret;
1568 buffer[size - 1] = '\0';
1570 ret:
1571 tty_disable_interrupt_key ();
1573 return res;
1575 #endif /* ENABLE_VFS_NET */
1577 /* --------------------------------------------------------------------------------------------- */
1579 * Normalize filenames start position
1582 void
1583 vfs_s_normalize_filename_leading_spaces (struct vfs_s_inode *root_inode, size_t final_num_spaces)
1585 GList *iter;
1587 for (iter = root_inode->subdir; iter != NULL; iter = g_list_next (iter))
1589 struct vfs_s_entry *entry = (struct vfs_s_entry *) iter->data;
1590 if ((size_t) entry->ino->data_offset > final_num_spaces)
1592 char *source_name = entry->name;
1593 char *spacer = g_strnfill (entry->ino->data_offset - final_num_spaces, ' ');
1594 entry->name = g_strdup_printf ("%s%s", spacer, source_name);
1595 g_free (spacer);
1596 g_free (source_name);
1598 entry->ino->data_offset = -1;
1602 /* --------------------------------------------------------------------------------------------- */