2 * \brief Source: directory cache support
4 * So that you do not have copy of this in each and every filesystem.
6 * Very loosely based on tar.c from midnight and archives.[ch] from
7 * avfs by Miklos Szeredi (mszeredi@inf.bme.hu)
9 * Unfortunately, I was unable to keep all filesystems
10 * uniform. tar-like filesystems use tree structure where each
11 * directory has pointers to its subdirectories. We can do this
12 * because we have full information about our archive.
14 * At ftp-like filesystems, situation is a little bit different. When
15 * you cd /usr/src/linux/drivers/char, you do _not_ want /usr,
16 * /usr/src, /usr/src/linux and /usr/src/linux/drivers to be
17 * listed. That means that we do not have complete information, and if
18 * /usr is symlink to /4, we will not know. Also we have to time out
19 * entries and things would get messy with tree-like approach. So we
20 * do different trick: root directory is completely special and
21 * completely fake, it contains entries such as 'usr', 'usr/src', ...,
22 * and we'll try to use custom find_entry function.
24 * \author Pavel Machek <pavel@ucw.cz>, distribute under LGPL.
27 * \warning Paths here do _not_ begin with '/', so root directory of
28 * archive/site is simply "".
34 #include <fcntl.h> /* include fcntl.h -> sys/fcntl.h only */
35 /* includes fcntl.h see IEEE Std 1003.1-2008 */
37 #include <sys/time.h> /* gettimeofday() */
38 #include <inttypes.h> /* uintmax_t */
41 #include "lib/global.h"
43 #include "lib/tty/tty.h" /* enable/disable interrupt key */
44 #include "lib/util.h" /* concat_dir_and_file */
46 #include "lib/widget.h" /* message() */
51 #include "xdirentry.h"
52 #include "gc.h" /* vfs_rmstamp */
54 /*** global variables ****************************************************************************/
56 /*** file scope macro definitions ****************************************************************/
58 #define CALL(x) if (MEDATA->x) MEDATA->x
60 /*** file scope type declarations ****************************************************************/
65 struct vfs_s_inode
*dir
;
68 /*** file scope variables ************************************************************************/
70 static volatile int total_inodes
= 0, total_entries
= 0;
72 /*** file scope functions ************************************************************************/
73 /* --------------------------------------------------------------------------------------------- */
76 vfs_s_entry_compare (const void *a
, const void *b
)
78 const struct vfs_s_entry
*e
= (const struct vfs_s_entry
*) a
;
79 const char *name
= (const char *) b
;
81 return strcmp (e
->name
, name
);
84 /* --------------------------------------------------------------------------------------------- */
87 vfs_s_free_inode (struct vfs_class
*me
, struct vfs_s_inode
*ino
)
90 vfs_die ("Don't pass NULL to me");
92 /* ==0 can happen if freshly created entry is deleted */
93 if (ino
->st
.st_nlink
> 1)
99 while (ino
->subdir
!= NULL
)
100 vfs_s_free_entry (me
, (struct vfs_s_entry
*) ino
->subdir
->data
);
102 CALL (free_inode
) (me
, ino
);
103 g_free (ino
->linkname
);
104 if (ino
->localname
!= NULL
)
106 unlink (ino
->localname
);
107 g_free (ino
->localname
);
110 ino
->super
->ino_usage
--;
114 /* --------------------------------------------------------------------------------------------- */
115 /* We were asked to create entries automagically */
117 static struct vfs_s_entry
*
118 vfs_s_automake (struct vfs_class
*me
, struct vfs_s_inode
*dir
, char *path
, int flags
)
120 struct vfs_s_entry
*res
;
123 sep
= strchr (path
, PATH_SEP
);
127 res
= vfs_s_generate_entry (me
, path
, dir
, flags
& FL_MKDIR
? (0777 | S_IFDIR
) : 0777);
128 vfs_s_insert_entry (me
, dir
, res
);
136 /* --------------------------------------------------------------------------------------------- */
137 /* If the entry is a symlink, find the entry for its target */
139 static struct vfs_s_entry
*
140 vfs_s_resolve_symlink (struct vfs_class
*me
, struct vfs_s_entry
*entry
, int follow
)
143 char *fullname
= NULL
;
144 struct vfs_s_entry
*target
;
146 if (follow
== LINK_NO_FOLLOW
)
149 ERRNOR (ELOOP
, NULL
);
151 ERRNOR (ENOENT
, NULL
);
152 if (!S_ISLNK (entry
->ino
->st
.st_mode
))
155 linkname
= entry
->ino
->linkname
;
156 if (linkname
== NULL
)
157 ERRNOR (EFAULT
, NULL
);
159 /* make full path from relative */
160 if (*linkname
!= PATH_SEP
)
162 char *fullpath
= vfs_s_fullpath (me
, entry
->dir
);
165 fullname
= g_strconcat (fullpath
, "/", linkname
, (char *) NULL
);
171 target
= (MEDATA
->find_entry
) (me
, entry
->dir
->super
->root
, linkname
, follow
- 1, 0);
176 /* --------------------------------------------------------------------------------------------- */
178 * Follow > 0: follow links, serves as loop protect,
179 * == -1: do not follow links
182 static struct vfs_s_entry
*
183 vfs_s_find_entry_tree (struct vfs_class
*me
, struct vfs_s_inode
*root
,
184 const char *a_path
, int follow
, int flags
)
187 struct vfs_s_entry
*ent
= NULL
;
188 char *const pathref
= g_strdup (a_path
);
189 char *path
= pathref
;
191 /* canonicalize as well, but don't remove '../' from path */
192 custom_canonicalize_pathname (path
, CANON_PATH_ALL
& (~CANON_PATH_REMDOUBLEDOTS
));
198 while (*path
== PATH_SEP
) /* Strip leading '/' */
207 for (pseg
= 0; path
[pseg
] != '\0' && path
[pseg
] != PATH_SEP
; pseg
++)
210 for (iter
= root
->subdir
; iter
!= NULL
; iter
= g_list_next (iter
))
212 ent
= (struct vfs_s_entry
*) iter
->data
;
213 if (strlen (ent
->name
) == pseg
&& strncmp (ent
->name
, path
, pseg
) == 0)
218 ent
= iter
!= NULL
? (struct vfs_s_entry
*) iter
->data
: NULL
;
220 if (ent
== NULL
&& (flags
& (FL_MKFILE
| FL_MKDIR
)) != 0)
221 ent
= vfs_s_automake (me
, root
, path
, flags
);
229 /* here we must follow leading directories always;
230 only the actual file is optional */
231 ent
= vfs_s_resolve_symlink (me
, ent
,
232 strchr (path
, PATH_SEP
) != NULL
? LINK_FOLLOW
: follow
);
242 /* --------------------------------------------------------------------------------------------- */
245 split_dir_name (struct vfs_class
*me
, char *path
, char **dir
, char **name
, char **save
)
251 s
= strrchr (path
, PATH_SEP
);
256 *dir
= path
+ strlen (path
); /* an empty string */
267 /* --------------------------------------------------------------------------------------------- */
269 static struct vfs_s_entry
*
270 vfs_s_find_entry_linear (struct vfs_class
*me
, struct vfs_s_inode
*root
,
271 const char *a_path
, int follow
, int flags
)
273 struct vfs_s_entry
*ent
= NULL
;
274 char *const path
= g_strdup (a_path
);
275 struct vfs_s_entry
*retval
= NULL
;
278 if (root
->super
->root
!= root
)
279 vfs_die ("We have to use _real_ root. Always. Sorry.");
281 /* canonicalize as well, but don't remove '../' from path */
282 custom_canonicalize_pathname (path
, CANON_PATH_ALL
& (~CANON_PATH_REMDOUBLEDOTS
));
284 if ((flags
& FL_DIR
) == 0)
286 char *dirname
, *name
, *save
;
287 struct vfs_s_inode
*ino
;
288 split_dir_name (me
, path
, &dirname
, &name
, &save
);
289 ino
= vfs_s_find_inode (me
, root
->super
, dirname
, follow
, flags
| FL_DIR
);
292 retval
= vfs_s_find_entry_tree (me
, ino
, name
, follow
, flags
);
297 iter
= g_list_find_custom (root
->subdir
, path
, (GCompareFunc
) vfs_s_entry_compare
);
298 ent
= iter
!= NULL
? (struct vfs_s_entry
*) iter
->data
: NULL
;
300 if (ent
!= NULL
&& !MEDATA
->dir_uptodate (me
, ent
->ino
))
303 vfs_print_message (_("Directory cache expired for %s"), path
);
305 vfs_s_free_entry (me
, ent
);
311 struct vfs_s_inode
*ino
;
313 ino
= vfs_s_new_inode (me
, root
->super
, vfs_s_default_stat (me
, S_IFDIR
| 0755));
314 ent
= vfs_s_new_entry (me
, path
, ino
);
315 if (MEDATA
->dir_load (me
, ino
, path
) == -1)
317 vfs_s_free_entry (me
, ent
);
322 vfs_s_insert_entry (me
, root
, ent
);
324 iter
= g_list_find_custom (root
->subdir
, path
, (GCompareFunc
) vfs_s_entry_compare
);
325 ent
= iter
!= NULL
? (struct vfs_s_entry
*) iter
->data
: NULL
;
328 vfs_die ("find_linear: success but directory is not there\n");
331 if (!vfs_s_resolve_symlink (me
, ent
, follow
))
341 /* --------------------------------------------------------------------------------------------- */
342 /* Ook, these were functions around directory entries / inodes */
343 /* -------------------------------- superblock games -------------------------- */
345 static struct vfs_s_super
*
346 vfs_s_new_super (struct vfs_class
*me
)
348 struct vfs_s_super
*super
;
350 super
= g_new0 (struct vfs_s_super
, 1);
355 /* --------------------------------------------------------------------------------------------- */
358 vfs_s_insert_super (struct vfs_class
*me
, struct vfs_s_super
*super
)
360 MEDATA
->supers
= g_list_prepend (MEDATA
->supers
, super
);
363 /* --------------------------------------------------------------------------------------------- */
366 vfs_s_free_super (struct vfs_class
*me
, struct vfs_s_super
*super
)
368 if (super
->root
!= NULL
)
370 vfs_s_free_inode (me
, super
->root
);
375 /* FIXME: We currently leak small ammount of memory, sometimes. Fix it if you can. */
376 if (super
->ino_usage
)
377 message (D_ERROR
, "Direntry warning",
378 "Super ino_usage is %d, memory leak", super
->ino_usage
);
380 if (super
->want_stale
)
381 message (D_ERROR
, "Direntry warning", "%s", "Super has want_stale set");
384 MEDATA
->supers
= g_list_remove (MEDATA
->supers
, super
);
386 CALL (free_archive
) (me
, super
);
387 #ifdef ENABLE_VFS_NET
388 vfs_url_free (super
->url
);
390 g_free (super
->name
);
394 /* --------------------------------------------------------------------------------------------- */
396 * Dissect the path and create corresponding superblock.
397 * The result should be freed.
401 vfs_s_get_path (struct vfs_class
*me
, const char *inname
, struct vfs_s_super
**archive
, int flags
)
405 buf
= g_strdup (inname
);
406 retval
= g_strdup (vfs_s_get_path_mangle (me
, buf
, archive
, flags
));
411 /* --------------------------------------------------------------------------------------------- */
412 /* Support of archives */
413 /* ------------------------ readdir & friends ----------------------------- */
415 static struct vfs_s_inode
*
416 vfs_s_inode_from_path (struct vfs_class
*me
, const char *name
, int flags
)
418 struct vfs_s_super
*super
;
419 struct vfs_s_inode
*ino
;
422 if (!(q
= vfs_s_get_path (me
, name
, &super
, 0)))
426 vfs_s_find_inode (me
, super
, q
,
427 flags
& FL_FOLLOW
? LINK_FOLLOW
: LINK_NO_FOLLOW
, flags
& ~FL_FOLLOW
);
429 /* We are asking about / directory of ftp server: assume it exists */
431 vfs_s_find_inode (me
, super
, q
,
432 flags
& FL_FOLLOW
? LINK_FOLLOW
:
433 LINK_NO_FOLLOW
, FL_DIR
| (flags
& ~FL_FOLLOW
));
438 /* --------------------------------------------------------------------------------------------- */
441 vfs_s_opendir (struct vfs_class
*me
, const char *dirname
)
443 struct vfs_s_inode
*dir
;
444 struct dirhandle
*info
;
446 dir
= vfs_s_inode_from_path (me
, dirname
, FL_DIR
| FL_FOLLOW
);
449 if (!S_ISDIR (dir
->st
.st_mode
))
450 ERRNOR (ENOTDIR
, NULL
);
454 if (dir
->subdir
== NULL
) /* This can actually happen if we allow empty directories */
455 ERRNOR (EAGAIN
, NULL
);
457 info
= g_new (struct dirhandle
, 1);
458 info
->cur
= dir
->subdir
;
464 /* --------------------------------------------------------------------------------------------- */
467 vfs_s_readdir (void *data
)
469 static union vfs_dirent dir
;
470 struct dirhandle
*info
= (struct dirhandle
*) data
;
473 if (info
->cur
== NULL
|| info
->cur
->data
== NULL
)
476 name
= ((struct vfs_s_entry
*) info
->cur
->data
)->name
;
478 g_strlcpy (dir
.dent
.d_name
, name
, MC_MAXPATHLEN
);
480 vfs_die ("Null in structure-cannot happen");
482 compute_namelen (&dir
.dent
);
483 info
->cur
= g_list_next (info
->cur
);
485 return (void *) &dir
;
488 /* --------------------------------------------------------------------------------------------- */
491 vfs_s_closedir (void *data
)
493 struct dirhandle
*info
= (struct dirhandle
*) data
;
494 struct vfs_s_inode
*dir
= info
->dir
;
496 vfs_s_free_inode (dir
->super
->me
, dir
);
501 /* --------------------------------------------------------------------------------------------- */
504 vfs_s_chdir (struct vfs_class
*me
, const char *path
)
508 data
= vfs_s_opendir (me
, path
);
511 vfs_s_closedir (data
);
515 /* --------------------------------------------------------------------------------------------- */
516 /* --------------------------- stat and friends ---------------------------- */
519 vfs_s_internal_stat (struct vfs_class
*me
, const char *path
, struct stat
*buf
, int flag
)
521 struct vfs_s_inode
*ino
;
523 ino
= vfs_s_inode_from_path (me
, path
, flag
);
530 /* --------------------------------------------------------------------------------------------- */
533 vfs_s_stat (struct vfs_class
*me
, const char *path
, struct stat
*buf
)
535 return vfs_s_internal_stat (me
, path
, buf
, FL_FOLLOW
);
538 /* --------------------------------------------------------------------------------------------- */
541 vfs_s_lstat (struct vfs_class
*me
, const char *path
, struct stat
*buf
)
543 return vfs_s_internal_stat (me
, path
, buf
, FL_NONE
);
546 /* --------------------------------------------------------------------------------------------- */
549 vfs_s_fstat (void *fh
, struct stat
*buf
)
555 /* --------------------------------------------------------------------------------------------- */
558 vfs_s_readlink (struct vfs_class
*me
, const char *path
, char *buf
, size_t size
)
560 struct vfs_s_inode
*ino
;
563 ino
= vfs_s_inode_from_path (me
, path
, 0);
567 if (!S_ISLNK (ino
->st
.st_mode
))
570 if (ino
->linkname
== NULL
)
573 len
= strlen (ino
->linkname
);
576 /* readlink() does not append a NUL character to buf */
577 memcpy (buf
, ino
->linkname
, len
);
581 /* --------------------------------------------------------------------------------------------- */
584 vfs_s_read (void *fh
, char *buffer
, size_t count
)
587 struct vfs_class
*me
= FH_SUPER
->me
;
589 if (FH
->linear
== LS_LINEAR_PREOPEN
)
591 if (!MEDATA
->linear_start (me
, FH
, FH
->pos
))
595 if (FH
->linear
== LS_LINEAR_CLOSED
)
596 vfs_die ("linear_start() did not set linear_state!");
598 if (FH
->linear
== LS_LINEAR_OPEN
)
599 return MEDATA
->linear_read (me
, FH
, buffer
, count
);
601 if (FH
->handle
!= -1)
603 n
= read (FH
->handle
, buffer
, count
);
608 vfs_die ("vfs_s_read: This should not happen\n");
612 /* --------------------------------------------------------------------------------------------- */
615 vfs_s_write (void *fh
, const char *buffer
, size_t count
)
618 struct vfs_class
*me
= FH_SUPER
->me
;
621 vfs_die ("no writing to linear files, please");
624 if (FH
->handle
!= -1)
626 n
= write (FH
->handle
, buffer
, count
);
631 vfs_die ("vfs_s_write: This should not happen\n");
635 /* --------------------------------------------------------------------------------------------- */
638 vfs_s_lseek (void *fh
, off_t offset
, int whence
)
640 off_t size
= FH
->ino
->st
.st_size
;
642 if (FH
->linear
== LS_LINEAR_OPEN
)
643 vfs_die ("cannot lseek() after linear_read!");
645 if (FH
->handle
!= -1)
646 { /* If we have local file opened, we want to work with it */
647 off_t retval
= lseek (FH
->handle
, offset
, whence
);
649 FH
->ino
->super
->me
->verrno
= errno
;
664 else if (offset
< size
)
671 /* --------------------------------------------------------------------------------------------- */
674 vfs_s_close (void *fh
)
677 struct vfs_class
*me
= FH_SUPER
->me
;
679 FH_SUPER
->fd_usage
--;
680 if (!FH_SUPER
->fd_usage
)
681 vfs_stamp_create (me
, FH_SUPER
);
683 if (FH
->linear
== LS_LINEAR_OPEN
)
684 MEDATA
->linear_close (me
, fh
);
685 if (MEDATA
->fh_close
)
686 res
= MEDATA
->fh_close (me
, fh
);
687 if (FH
->changed
&& MEDATA
->file_store
)
689 char *s
= vfs_s_fullpath (me
, FH
->ino
);
694 res
= MEDATA
->file_store (me
, fh
, s
, FH
->ino
->localname
);
697 vfs_s_invalidate (me
, FH_SUPER
);
699 if (FH
->handle
!= -1)
702 vfs_s_free_inode (me
, FH
->ino
);
707 /* --------------------------------------------------------------------------------------------- */
710 vfs_s_print_stats (const char *fs_name
, const char *action
,
711 const char *file_name
, off_t have
, off_t need
)
713 static const char *i18n_percent_transf_format
= NULL
;
714 static const char *i18n_transf_format
= NULL
;
716 if (i18n_percent_transf_format
== NULL
)
718 i18n_percent_transf_format
= "%s: %s: %s %3d%% (%" PRIuMAX
" %s";
719 i18n_transf_format
= "%s: %s: %s %" PRIuMAX
" %s";
723 vfs_print_message (i18n_percent_transf_format
, fs_name
, action
,
724 file_name
, (int) ((double) have
* 100 / need
), (uintmax_t) have
,
725 _("bytes transferred"));
727 vfs_print_message (i18n_transf_format
, fs_name
, action
, file_name
, (uintmax_t) have
,
728 _("bytes transferred"));
731 /* --------------------------------------------------------------------------------------------- */
732 /* ------------------------------- mc support ---------------------------- */
735 vfs_s_fill_names (struct vfs_class
*me
, fill_names_f func
)
739 for (iter
= MEDATA
->supers
; iter
!= NULL
; iter
= g_list_next (iter
))
741 const struct vfs_s_super
*super
= (const struct vfs_s_super
*) iter
->data
;
744 name
= g_strconcat (super
->name
, "#", me
->prefix
, "/",
745 /* super->current_dir->name, */ (char *) NULL
);
751 /* --------------------------------------------------------------------------------------------- */
754 vfs_s_ferrno (struct vfs_class
*me
)
759 /* --------------------------------------------------------------------------------------------- */
761 * Get local copy of the given file. We reuse the existing file cache
762 * for remote filesystems. Archives use standard VFS facilities.
766 vfs_s_getlocalcopy (const vfs_path_t
* vpath
)
768 vfs_file_handler_t
*fh
;
771 fh
= vfs_s_open (vpath
, O_RDONLY
, 0);
775 if ((fh
->ino
!= NULL
) && (fh
->ino
->localname
!= NULL
))
776 local
= g_strdup (fh
->ino
->localname
);
784 /* --------------------------------------------------------------------------------------------- */
786 * Return the local copy. Since we are using our cache, we do nothing -
787 * the cache will be removed when the archive is closed.
791 vfs_s_ungetlocalcopy (const vfs_path_t
* vpath
, const char *local
, int has_changed
)
799 /* --------------------------------------------------------------------------------------------- */
802 vfs_s_setctl (struct vfs_class
*me
, const char *path
, int ctlop
, void *arg
)
806 case VFS_SETCTL_STALE_DATA
:
808 struct vfs_s_inode
*ino
= vfs_s_inode_from_path (me
, path
, 0);
813 ino
->super
->want_stale
= 1;
816 ino
->super
->want_stale
= 0;
817 vfs_s_invalidate (me
, ino
->super
);
821 case VFS_SETCTL_LOGFILE
:
822 MEDATA
->logfile
= fopen ((char *) arg
, "w");
824 case VFS_SETCTL_FLUSH
:
831 /* --------------------------------------------------------------------------------------------- */
832 /* ----------------------------- Stamping support -------------------------- */
835 vfs_s_getid (struct vfs_class
*me
, const char *path
)
837 struct vfs_s_super
*archive
= NULL
;
840 p
= vfs_s_get_path (me
, path
, &archive
, FL_NO_OPEN
);
844 return (vfsid
) archive
;
847 /* --------------------------------------------------------------------------------------------- */
850 vfs_s_nothingisopen (vfsid id
)
853 /* Our data structures should survive free of superblock at any time */
857 /* --------------------------------------------------------------------------------------------- */
860 vfs_s_free (vfsid id
)
862 vfs_s_free_super (((struct vfs_s_super
*) id
)->me
, (struct vfs_s_super
*) id
);
865 /* --------------------------------------------------------------------------------------------- */
868 vfs_s_dir_uptodate (struct vfs_class
*me
, struct vfs_s_inode
*ino
)
878 gettimeofday (&tim
, NULL
);
879 if (tim
.tv_sec
< ino
->timestamp
.tv_sec
)
885 /* --------------------------------------------------------------------------------------------- */
886 /*** public functions ****************************************************************************/
887 /* --------------------------------------------------------------------------------------------- */
890 vfs_s_new_inode (struct vfs_class
*me
, struct vfs_s_super
*super
, struct stat
*initstat
)
892 struct vfs_s_inode
*ino
;
894 ino
= g_try_new0 (struct vfs_s_inode
, 1);
901 ino
->st
.st_nlink
= 0;
902 ino
->st
.st_ino
= MEDATA
->inode_counter
++;
903 ino
->st
.st_dev
= MEDATA
->rdev
;
908 CALL (init_inode
) (me
, ino
);
913 /* --------------------------------------------------------------------------------------------- */
916 vfs_s_new_entry (struct vfs_class
*me
, const char *name
, struct vfs_s_inode
*inode
)
918 struct vfs_s_entry
*entry
;
920 entry
= g_new0 (struct vfs_s_entry
, 1);
923 entry
->name
= g_strdup (name
);
925 entry
->ino
->ent
= entry
;
926 CALL (init_entry
) (me
, entry
);
932 /* --------------------------------------------------------------------------------------------- */
935 vfs_s_free_entry (struct vfs_class
*me
, struct vfs_s_entry
*ent
)
937 if (ent
->dir
!= NULL
)
938 ent
->dir
->subdir
= g_list_remove (ent
->dir
->subdir
, ent
);
941 /* ent->name = NULL; */
943 if (ent
->ino
!= NULL
)
945 ent
->ino
->ent
= NULL
;
946 vfs_s_free_inode (me
, ent
->ino
);
953 /* --------------------------------------------------------------------------------------------- */
956 vfs_s_insert_entry (struct vfs_class
*me
, struct vfs_s_inode
*dir
, struct vfs_s_entry
*ent
)
962 ent
->ino
->st
.st_nlink
++;
963 dir
->subdir
= g_list_append (dir
->subdir
, ent
);
966 /* --------------------------------------------------------------------------------------------- */
969 vfs_s_default_stat (struct vfs_class
*me
, mode_t mode
)
971 static struct stat st
;
976 myumask
= umask (022);
984 st
.st_uid
= getuid ();
985 st
.st_gid
= getgid ();
987 st
.st_mtime
= st
.st_atime
= st
.st_ctime
= time (NULL
);
992 /* --------------------------------------------------------------------------------------------- */
995 vfs_s_generate_entry (struct vfs_class
*me
, const char *name
, struct vfs_s_inode
*parent
,
998 struct vfs_s_inode
*inode
;
1001 st
= vfs_s_default_stat (me
, mode
);
1002 inode
= vfs_s_new_inode (me
, parent
->super
, st
);
1004 return vfs_s_new_entry (me
, name
, inode
);
1007 /* --------------------------------------------------------------------------------------------- */
1009 struct vfs_s_inode
*
1010 vfs_s_find_inode (struct vfs_class
*me
, const struct vfs_s_super
*super
,
1011 const char *path
, int follow
, int flags
)
1013 struct vfs_s_entry
*ent
;
1015 if (((MEDATA
->flags
& VFS_S_REMOTE
) == 0) && (*path
== '\0'))
1018 ent
= (MEDATA
->find_entry
) (me
, super
->root
, path
, follow
, flags
);
1019 return (ent
!= NULL
) ? ent
->ino
: NULL
;
1022 /* --------------------------------------------------------------------------------------------- */
1023 /* Ook, these were functions around directory entries / inodes */
1024 /* -------------------------------- superblock games -------------------------- */
1027 * Dissect the path and create corresponding superblock. Note that inname
1028 * can be changed and the result may point inside the original string.
1031 vfs_s_get_path_mangle (struct vfs_class
*me
, char *inname
, struct vfs_s_super
**archive
, int flags
)
1036 const char *archive_name
;
1038 struct vfs_s_super
*super
;
1039 void *cookie
= NULL
;
1041 archive_name
= inname
;
1042 vfs_split (inname
, &local
, &op
);
1043 retval
= (local
!= NULL
) ? local
: "";
1045 if (MEDATA
->archive_check
!= NULL
)
1047 cookie
= MEDATA
->archive_check (me
, archive_name
, op
);
1052 for (iter
= MEDATA
->supers
; iter
!= NULL
; iter
= g_list_next (iter
))
1056 super
= (struct vfs_s_super
*) iter
->data
;
1058 /* 0 == other, 1 == same, return it, 2 == other but stop scanning */
1059 i
= MEDATA
->archive_same (me
, super
, archive_name
, op
, cookie
);
1063 goto return_success
;
1068 if (flags
& FL_NO_OPEN
)
1071 super
= vfs_s_new_super (me
);
1072 if (MEDATA
->open_archive
!= NULL
)
1073 result
= MEDATA
->open_archive (me
, super
, archive_name
, op
);
1076 vfs_s_free_super (me
, super
);
1080 vfs_die ("You have to fill name\n");
1082 vfs_die ("You have to fill root inode\n");
1084 vfs_s_insert_super (me
, super
);
1085 vfs_stamp_create (me
, super
);
1092 /* --------------------------------------------------------------------------------------------- */
1095 vfs_s_invalidate (struct vfs_class
*me
, struct vfs_s_super
*super
)
1097 if (!super
->want_stale
)
1099 vfs_s_free_inode (me
, super
->root
);
1100 super
->root
= vfs_s_new_inode (me
, super
, vfs_s_default_stat (me
, S_IFDIR
| 0755));
1104 /* --------------------------------------------------------------------------------------------- */
1107 vfs_s_fullpath (struct vfs_class
*me
, struct vfs_s_inode
*ino
)
1110 ERRNOR (EAGAIN
, NULL
);
1112 if (!(MEDATA
->flags
& VFS_S_REMOTE
))
1116 char *path
= g_strdup (ino
->ent
->name
);
1119 ino
= ino
->ent
->dir
;
1120 if (ino
== ino
->super
->root
)
1122 newpath
= g_strconcat (ino
->ent
->name
, "/", path
, (char *) NULL
);
1129 /* remote systems */
1130 if ((!ino
->ent
->dir
) || (!ino
->ent
->dir
->ent
))
1131 return g_strdup (ino
->ent
->name
);
1133 return g_strconcat (ino
->ent
->dir
->ent
->name
, PATH_SEP_STR
, ino
->ent
->name
, (char *) NULL
);
1136 /* --------------------------------------------------------------------------------------------- */
1137 /* --------------------------- stat and friends ---------------------------- */
1140 vfs_s_open (const vfs_path_t
* vpath
, int flags
, mode_t mode
)
1142 int was_changed
= 0;
1143 vfs_file_handler_t
*fh
;
1144 struct vfs_s_super
*super
;
1146 struct vfs_s_inode
*ino
;
1147 vfs_path_element_t
*path_element
;
1149 path_element
= vfs_path_get_by_index (vpath
, vfs_path_length (vpath
) - 1);
1151 q
= vfs_s_get_path (path_element
->class, vpath
->unparsed
, &super
, 0);
1154 ino
= vfs_s_find_inode (path_element
->class, super
, q
, LINK_FOLLOW
, FL_NONE
);
1155 if (ino
&& ((flags
& (O_CREAT
| O_EXCL
)) == (O_CREAT
| O_EXCL
)))
1158 path_element
->class->verrno
= EEXIST
;
1163 char *dirname
, *name
, *save
;
1164 struct vfs_s_entry
*ent
;
1165 struct vfs_s_inode
*dir
;
1168 /* If the filesystem is read-only, disable file creation */
1169 if (!(flags
& O_CREAT
) || !(path_element
->class->write
))
1175 split_dir_name (path_element
->class, q
, &dirname
, &name
, &save
);
1176 dir
= vfs_s_find_inode (path_element
->class, super
, dirname
, LINK_FOLLOW
, FL_DIR
);
1184 ent
= vfs_s_generate_entry (path_element
->class, name
, dir
, 0755);
1186 vfs_s_insert_entry (path_element
->class, dir
, ent
);
1187 tmp_handle
= vfs_mkstemps (&ino
->localname
, path_element
->class->name
, name
);
1188 if (tmp_handle
== -1)
1199 if (S_ISDIR (ino
->st
.st_mode
))
1201 path_element
->class->verrno
= EISDIR
;
1205 fh
= g_new (vfs_file_handler_t
, 1);
1209 fh
->changed
= was_changed
;
1213 if (IS_LINEAR (flags
))
1215 if (VFSDATA (path_element
)->linear_start
)
1217 vfs_print_message (_("Starting linear transfer..."));
1218 fh
->linear
= LS_LINEAR_PREOPEN
;
1221 else if ((VFSDATA (path_element
)->fh_open
!= NULL
)
1222 && (VFSDATA (path_element
)->fh_open (path_element
->class, fh
, flags
, mode
) != 0))
1228 if (fh
->ino
->localname
)
1230 fh
->handle
= open (fh
->ino
->localname
, NO_LINEAR (flags
), mode
);
1231 if (fh
->handle
== -1)
1234 path_element
->class->verrno
= errno
;
1239 /* i.e. we had no open files and now we have one */
1240 vfs_rmstamp (path_element
->class, (vfsid
) super
);
1242 fh
->ino
->st
.st_nlink
++;
1246 /* --------------------------------------------------------------------------------------------- */
1249 vfs_s_retrieve_file (struct vfs_class
*me
, struct vfs_s_inode
*ino
)
1251 /* If you want reget, you'll have to open file with O_LINEAR */
1255 off_t stat_size
= ino
->st
.st_size
;
1256 vfs_file_handler_t fh
;
1258 memset (&fh
, 0, sizeof (fh
));
1263 handle
= vfs_mkstemps (&ino
->localname
, me
->name
, ino
->ent
->name
);
1270 if (!MEDATA
->linear_start (me
, &fh
, 0))
1273 /* Clear the interrupt status */
1274 tty_got_interrupt ();
1275 tty_enable_interrupt_key ();
1277 while ((n
= MEDATA
->linear_read (me
, &fh
, buffer
, sizeof (buffer
))))
1284 vfs_s_print_stats (me
->name
, _("Getting file"), ino
->ent
->name
, total
, stat_size
);
1286 if (tty_got_interrupt ())
1289 t
= write (handle
, buffer
, n
);
1297 MEDATA
->linear_close (me
, &fh
);
1300 tty_disable_interrupt_key ();
1305 MEDATA
->linear_close (me
, &fh
);
1307 tty_disable_interrupt_key ();
1309 unlink (ino
->localname
);
1311 g_free (ino
->localname
);
1312 ino
->localname
= NULL
;
1317 /* --------------------------------------------------------------------------------------------- */
1318 /* ----------------------------- Stamping support -------------------------- */
1320 /* Initialize one of our subclasses - fill common functions */
1322 vfs_s_init_class (struct vfs_class
*vclass
, struct vfs_s_subclass
*sub
)
1325 vclass
->fill_names
= vfs_s_fill_names
;
1326 vclass
->open
= vfs_s_open
;
1327 vclass
->close
= vfs_s_close
;
1328 vclass
->read
= vfs_s_read
;
1329 if (!(sub
->flags
& VFS_S_READONLY
))
1331 vclass
->write
= vfs_s_write
;
1333 vclass
->opendir
= vfs_s_opendir
;
1334 vclass
->readdir
= vfs_s_readdir
;
1335 vclass
->closedir
= vfs_s_closedir
;
1336 vclass
->stat
= vfs_s_stat
;
1337 vclass
->lstat
= vfs_s_lstat
;
1338 vclass
->fstat
= vfs_s_fstat
;
1339 vclass
->readlink
= vfs_s_readlink
;
1340 vclass
->chdir
= vfs_s_chdir
;
1341 vclass
->ferrno
= vfs_s_ferrno
;
1342 vclass
->lseek
= vfs_s_lseek
;
1343 vclass
->getid
= vfs_s_getid
;
1344 vclass
->nothingisopen
= vfs_s_nothingisopen
;
1345 vclass
->free
= vfs_s_free
;
1346 if (sub
->flags
& VFS_S_REMOTE
)
1348 vclass
->getlocalcopy
= vfs_s_getlocalcopy
;
1349 vclass
->ungetlocalcopy
= vfs_s_ungetlocalcopy
;
1350 sub
->find_entry
= vfs_s_find_entry_linear
;
1354 sub
->find_entry
= vfs_s_find_entry_tree
;
1356 vclass
->setctl
= vfs_s_setctl
;
1357 sub
->dir_uptodate
= vfs_s_dir_uptodate
;
1360 /* --------------------------------------------------------------------------------------------- */
1361 /** Find VFS id for given directory name */
1364 vfs_getid (struct vfs_class
*vclass
, const char *dir
)
1369 /* append slash if needed */
1370 dir1
= concat_dir_and_file (dir
, "");
1372 id
= (*vclass
->getid
) (vclass
, dir1
);
1378 /* --------------------------------------------------------------------------------------------- */
1379 /* ----------- Utility functions for networked filesystems -------------- */
1381 #ifdef ENABLE_VFS_NET
1383 vfs_s_select_on_two (int fd1
, int fd2
)
1386 struct timeval time_out
;
1388 int maxfd
= (fd1
> fd2
? fd1
: fd2
) + 1;
1390 time_out
.tv_sec
= 1;
1391 time_out
.tv_usec
= 0;
1395 v
= select (maxfd
, &set
, 0, 0, &time_out
);
1398 if (FD_ISSET (fd1
, &set
))
1400 if (FD_ISSET (fd2
, &set
))
1405 /* --------------------------------------------------------------------------------------------- */
1408 vfs_s_get_line (struct vfs_class
*me
, int sock
, char *buf
, int buf_len
, char term
)
1410 FILE *logfile
= MEDATA
->logfile
;
1414 for (i
= 0; i
< buf_len
- 1; i
++, buf
++)
1416 if (read (sock
, buf
, sizeof (char)) <= 0)
1422 ret1
= fwrite (buf
, 1, 1, logfile
);
1423 ret2
= fflush (logfile
);
1432 /* Line is too long - terminate buffer and discard the rest of line */
1434 while (read (sock
, &c
, sizeof (c
)) > 0)
1440 ret1
= fwrite (&c
, 1, 1, logfile
);
1441 ret2
= fflush (logfile
);
1449 /* --------------------------------------------------------------------------------------------- */
1452 vfs_s_get_line_interruptible (struct vfs_class
*me
, char *buffer
, int size
, int fd
)
1459 tty_enable_interrupt_key ();
1460 for (i
= 0; i
< size
- 1; i
++)
1462 n
= read (fd
, buffer
+ i
, 1);
1463 tty_disable_interrupt_key ();
1464 if (n
== -1 && errno
== EINTR
)
1474 if (buffer
[i
] == '\n')
1480 buffer
[size
- 1] = 0;
1483 #endif /* ENABLE_VFS_NET */
1485 /* --------------------------------------------------------------------------------------------- */