2 Directory cache support
4 Copyright (C) 1998, 2011
5 The Free Software Foundation, Inc.
8 Pavel Machek <pavel@ucw.cz>, 1998
10 This file is part of the Midnight Commander.
12 The Midnight Commander is free software: you can redistribute it
13 and/or modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation, either version 3 of the License,
15 or (at your option) any later version.
17 The Midnight Commander is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 \warning Paths here do _not_ begin with '/', so root directory of
26 archive/site is simply "".
30 * \brief Source: directory cache support
32 * So that you do not have copy of this in each and every filesystem.
34 * Very loosely based on tar.c from midnight and archives.[ch] from
35 * avfs by Miklos Szeredi (mszeredi@inf.bme.hu)
37 * Unfortunately, I was unable to keep all filesystems
38 * uniform. tar-like filesystems use tree structure where each
39 * directory has pointers to its subdirectories. We can do this
40 * because we have full information about our archive.
42 * At ftp-like filesystems, situation is a little bit different. When
43 * you cd /usr/src/linux/drivers/char, you do _not_ want /usr,
44 * /usr/src, /usr/src/linux and /usr/src/linux/drivers to be
45 * listed. That means that we do not have complete information, and if
46 * /usr is symlink to /4, we will not know. Also we have to time out
47 * entries and things would get messy with tree-like approach. So we
48 * do different trick: root directory is completely special and
49 * completely fake, it contains entries such as 'usr', 'usr/src', ...,
50 * and we'll try to use custom find_entry function.
52 * \author Pavel Machek <pavel@ucw.cz>
60 #include <fcntl.h> /* include fcntl.h -> sys/fcntl.h only */
61 /* includes fcntl.h see IEEE Std 1003.1-2008 */
63 #include <sys/time.h> /* gettimeofday() */
64 #include <inttypes.h> /* uintmax_t */
67 #include "lib/global.h"
69 #include "lib/tty/tty.h" /* enable/disable interrupt key */
70 #include "lib/util.h" /* custom_canonicalize_pathname() */
72 #include "lib/widget.h" /* message() */
77 #include "xdirentry.h"
78 #include "gc.h" /* vfs_rmstamp */
80 /*** global variables ****************************************************************************/
82 /*** file scope macro definitions ****************************************************************/
84 #define CALL(x) if (MEDATA->x) MEDATA->x
86 /*** file scope type declarations ****************************************************************/
91 struct vfs_s_inode
*dir
;
94 /*** file scope variables ************************************************************************/
96 static volatile int total_inodes
= 0, total_entries
= 0;
98 /*** file scope functions ************************************************************************/
99 /* --------------------------------------------------------------------------------------------- */
102 vfs_s_entry_compare (const void *a
, const void *b
)
104 const struct vfs_s_entry
*e
= (const struct vfs_s_entry
*) a
;
105 const char *name
= (const char *) b
;
107 return strcmp (e
->name
, name
);
110 /* --------------------------------------------------------------------------------------------- */
112 /* We were asked to create entries automagically */
114 static struct vfs_s_entry
*
115 vfs_s_automake (struct vfs_class
*me
, struct vfs_s_inode
*dir
, char *path
, int flags
)
117 struct vfs_s_entry
*res
;
120 sep
= strchr (path
, PATH_SEP
);
124 res
= vfs_s_generate_entry (me
, path
, dir
, flags
& FL_MKDIR
? (0777 | S_IFDIR
) : 0777);
125 vfs_s_insert_entry (me
, dir
, res
);
133 /* --------------------------------------------------------------------------------------------- */
134 /* If the entry is a symlink, find the entry for its target */
136 static struct vfs_s_entry
*
137 vfs_s_resolve_symlink (struct vfs_class
*me
, struct vfs_s_entry
*entry
, int follow
)
140 char *fullname
= NULL
;
141 struct vfs_s_entry
*target
;
143 if (follow
== LINK_NO_FOLLOW
)
146 ERRNOR (ELOOP
, NULL
);
148 ERRNOR (ENOENT
, NULL
);
149 if (!S_ISLNK (entry
->ino
->st
.st_mode
))
152 linkname
= entry
->ino
->linkname
;
153 if (linkname
== NULL
)
154 ERRNOR (EFAULT
, NULL
);
156 /* make full path from relative */
157 if (*linkname
!= PATH_SEP
)
159 char *fullpath
= vfs_s_fullpath (me
, entry
->dir
);
162 fullname
= g_strconcat (fullpath
, "/", linkname
, (char *) NULL
);
168 target
= (MEDATA
->find_entry
) (me
, entry
->dir
->super
->root
, linkname
, follow
- 1, 0);
173 /* --------------------------------------------------------------------------------------------- */
175 * Follow > 0: follow links, serves as loop protect,
176 * == -1: do not follow links
179 static struct vfs_s_entry
*
180 vfs_s_find_entry_tree (struct vfs_class
*me
, struct vfs_s_inode
*root
,
181 const char *a_path
, int follow
, int flags
)
184 struct vfs_s_entry
*ent
= NULL
;
185 char *const pathref
= g_strdup (a_path
);
186 char *path
= pathref
;
188 /* canonicalize as well, but don't remove '../' from path */
189 custom_canonicalize_pathname (path
, CANON_PATH_ALL
& (~CANON_PATH_REMDOUBLEDOTS
));
195 while (*path
== PATH_SEP
) /* Strip leading '/' */
204 for (pseg
= 0; path
[pseg
] != '\0' && path
[pseg
] != PATH_SEP
; pseg
++)
207 for (iter
= root
->subdir
; iter
!= NULL
; iter
= g_list_next (iter
))
209 ent
= (struct vfs_s_entry
*) iter
->data
;
210 if (strlen (ent
->name
) == pseg
&& strncmp (ent
->name
, path
, pseg
) == 0)
215 ent
= iter
!= NULL
? (struct vfs_s_entry
*) iter
->data
: NULL
;
217 if (ent
== NULL
&& (flags
& (FL_MKFILE
| FL_MKDIR
)) != 0)
218 ent
= vfs_s_automake (me
, root
, path
, flags
);
226 /* here we must follow leading directories always;
227 only the actual file is optional */
228 ent
= vfs_s_resolve_symlink (me
, ent
,
229 strchr (path
, PATH_SEP
) != NULL
? LINK_FOLLOW
: follow
);
239 /* --------------------------------------------------------------------------------------------- */
241 static struct vfs_s_entry
*
242 vfs_s_find_entry_linear (struct vfs_class
*me
, struct vfs_s_inode
*root
,
243 const char *a_path
, int follow
, int flags
)
245 struct vfs_s_entry
*ent
= NULL
;
246 char *const path
= g_strdup (a_path
);
247 struct vfs_s_entry
*retval
= NULL
;
250 if (root
->super
->root
!= root
)
251 vfs_die ("We have to use _real_ root. Always. Sorry.");
253 /* canonicalize as well, but don't remove '../' from path */
254 custom_canonicalize_pathname (path
, CANON_PATH_ALL
& (~CANON_PATH_REMDOUBLEDOTS
));
256 if ((flags
& FL_DIR
) == 0)
258 char *dirname
, *name
;
259 struct vfs_s_inode
*ino
;
261 dirname
= g_path_get_dirname (path
);
262 name
= g_path_get_basename (path
);
263 ino
= vfs_s_find_inode (me
, root
->super
, dirname
, follow
, flags
| FL_DIR
);
264 retval
= vfs_s_find_entry_tree (me
, ino
, name
, follow
, flags
);
271 iter
= g_list_find_custom (root
->subdir
, path
, (GCompareFunc
) vfs_s_entry_compare
);
272 ent
= iter
!= NULL
? (struct vfs_s_entry
*) iter
->data
: NULL
;
274 if (ent
!= NULL
&& !MEDATA
->dir_uptodate (me
, ent
->ino
))
277 vfs_print_message (_("Directory cache expired for %s"), path
);
279 vfs_s_free_entry (me
, ent
);
285 struct vfs_s_inode
*ino
;
287 ino
= vfs_s_new_inode (me
, root
->super
, vfs_s_default_stat (me
, S_IFDIR
| 0755));
288 ent
= vfs_s_new_entry (me
, path
, ino
);
289 if (MEDATA
->dir_load (me
, ino
, path
) == -1)
291 vfs_s_free_entry (me
, ent
);
296 vfs_s_insert_entry (me
, root
, ent
);
298 iter
= g_list_find_custom (root
->subdir
, path
, (GCompareFunc
) vfs_s_entry_compare
);
299 ent
= iter
!= NULL
? (struct vfs_s_entry
*) iter
->data
: NULL
;
302 vfs_die ("find_linear: success but directory is not there\n");
305 if (!vfs_s_resolve_symlink (me
, ent
, follow
))
315 /* --------------------------------------------------------------------------------------------- */
316 /* Ook, these were functions around directory entries / inodes */
317 /* -------------------------------- superblock games -------------------------- */
319 static struct vfs_s_super
*
320 vfs_s_new_super (struct vfs_class
*me
)
322 struct vfs_s_super
*super
;
324 super
= g_new0 (struct vfs_s_super
, 1);
329 /* --------------------------------------------------------------------------------------------- */
332 vfs_s_insert_super (struct vfs_class
*me
, struct vfs_s_super
*super
)
334 MEDATA
->supers
= g_list_prepend (MEDATA
->supers
, super
);
337 /* --------------------------------------------------------------------------------------------- */
340 vfs_s_free_super (struct vfs_class
*me
, struct vfs_s_super
*super
)
342 if (super
->root
!= NULL
)
344 vfs_s_free_inode (me
, super
->root
);
349 /* FIXME: We currently leak small ammount of memory, sometimes. Fix it if you can. */
350 if (super
->ino_usage
)
351 message (D_ERROR
, "Direntry warning",
352 "Super ino_usage is %d, memory leak", super
->ino_usage
);
354 if (super
->want_stale
)
355 message (D_ERROR
, "Direntry warning", "%s", "Super has want_stale set");
358 MEDATA
->supers
= g_list_remove (MEDATA
->supers
, super
);
360 CALL (free_archive
) (me
, super
);
361 #ifdef ENABLE_VFS_NET
362 vfs_path_element_free (super
->path_element
);
364 g_free (super
->name
);
368 /* --------------------------------------------------------------------------------------------- */
369 /* Support of archives */
370 /* ------------------------ readdir & friends ----------------------------- */
372 static struct vfs_s_inode
*
373 vfs_s_inode_from_path (const vfs_path_t
* vpath
, int flags
)
375 struct vfs_s_super
*super
;
376 struct vfs_s_inode
*ino
;
378 const vfs_path_element_t
*path_element
;
380 q
= vfs_s_get_path (vpath
, &super
, 0);
384 path_element
= vfs_path_get_by_index (vpath
, -1);
387 vfs_s_find_inode (path_element
->class, super
, q
,
388 flags
& FL_FOLLOW
? LINK_FOLLOW
: LINK_NO_FOLLOW
, flags
& ~FL_FOLLOW
);
390 /* We are asking about / directory of ftp server: assume it exists */
392 vfs_s_find_inode (path_element
->class, super
, q
,
393 flags
& FL_FOLLOW
? LINK_FOLLOW
:
394 LINK_NO_FOLLOW
, FL_DIR
| (flags
& ~FL_FOLLOW
));
398 /* --------------------------------------------------------------------------------------------- */
401 vfs_s_opendir (const vfs_path_t
* vpath
)
403 struct vfs_s_inode
*dir
;
404 struct dirhandle
*info
;
405 const vfs_path_element_t
*path_element
;
407 path_element
= vfs_path_get_by_index (vpath
, -1);
409 dir
= vfs_s_inode_from_path (vpath
, FL_DIR
| FL_FOLLOW
);
412 if (!S_ISDIR (dir
->st
.st_mode
))
414 path_element
->class->verrno
= ENOTDIR
;
420 if (dir
->subdir
== NULL
) /* This can actually happen if we allow empty directories */
422 path_element
->class->verrno
= EAGAIN
;
426 info
= g_new (struct dirhandle
, 1);
427 info
->cur
= dir
->subdir
;
433 /* --------------------------------------------------------------------------------------------- */
436 vfs_s_readdir (void *data
)
438 static union vfs_dirent dir
;
439 struct dirhandle
*info
= (struct dirhandle
*) data
;
442 if (info
->cur
== NULL
|| info
->cur
->data
== NULL
)
445 name
= ((struct vfs_s_entry
*) info
->cur
->data
)->name
;
447 g_strlcpy (dir
.dent
.d_name
, name
, MC_MAXPATHLEN
);
449 vfs_die ("Null in structure-cannot happen");
451 compute_namelen (&dir
.dent
);
452 info
->cur
= g_list_next (info
->cur
);
454 return (void *) &dir
;
457 /* --------------------------------------------------------------------------------------------- */
460 vfs_s_closedir (void *data
)
462 struct dirhandle
*info
= (struct dirhandle
*) data
;
463 struct vfs_s_inode
*dir
= info
->dir
;
465 vfs_s_free_inode (dir
->super
->me
, dir
);
470 /* --------------------------------------------------------------------------------------------- */
473 vfs_s_chdir (const vfs_path_t
* vpath
)
477 data
= vfs_s_opendir (vpath
);
480 vfs_s_closedir (data
);
484 /* --------------------------------------------------------------------------------------------- */
485 /* --------------------------- stat and friends ---------------------------- */
488 vfs_s_internal_stat (const vfs_path_t
* vpath
, struct stat
*buf
, int flag
)
490 struct vfs_s_inode
*ino
;
492 ino
= vfs_s_inode_from_path (vpath
, flag
);
499 /* --------------------------------------------------------------------------------------------- */
502 vfs_s_stat (const vfs_path_t
* vpath
, struct stat
*buf
)
504 return vfs_s_internal_stat (vpath
, buf
, FL_FOLLOW
);
507 /* --------------------------------------------------------------------------------------------- */
510 vfs_s_lstat (const vfs_path_t
* vpath
, struct stat
*buf
)
512 return vfs_s_internal_stat (vpath
, buf
, FL_NONE
);
515 /* --------------------------------------------------------------------------------------------- */
518 vfs_s_fstat (void *fh
, struct stat
*buf
)
524 /* --------------------------------------------------------------------------------------------- */
527 vfs_s_readlink (const vfs_path_t
* vpath
, char *buf
, size_t size
)
529 struct vfs_s_inode
*ino
;
531 const vfs_path_element_t
*path_element
;
533 path_element
= vfs_path_get_by_index (vpath
, -1);
535 ino
= vfs_s_inode_from_path (vpath
, 0);
539 if (!S_ISLNK (ino
->st
.st_mode
))
541 path_element
->class->verrno
= EINVAL
;
545 if (ino
->linkname
== NULL
)
547 path_element
->class->verrno
= EFAULT
;
551 len
= strlen (ino
->linkname
);
554 /* readlink() does not append a NUL character to buf */
555 memcpy (buf
, ino
->linkname
, len
);
559 /* --------------------------------------------------------------------------------------------- */
562 vfs_s_read (void *fh
, char *buffer
, size_t count
)
565 struct vfs_class
*me
= FH_SUPER
->me
;
567 if (FH
->linear
== LS_LINEAR_PREOPEN
)
569 if (!MEDATA
->linear_start (me
, FH
, FH
->pos
))
573 if (FH
->linear
== LS_LINEAR_CLOSED
)
574 vfs_die ("linear_start() did not set linear_state!");
576 if (FH
->linear
== LS_LINEAR_OPEN
)
577 return MEDATA
->linear_read (me
, FH
, buffer
, count
);
579 if (FH
->handle
!= -1)
581 n
= read (FH
->handle
, buffer
, count
);
586 vfs_die ("vfs_s_read: This should not happen\n");
590 /* --------------------------------------------------------------------------------------------- */
593 vfs_s_write (void *fh
, const char *buffer
, size_t count
)
596 struct vfs_class
*me
= FH_SUPER
->me
;
599 vfs_die ("no writing to linear files, please");
602 if (FH
->handle
!= -1)
604 n
= write (FH
->handle
, buffer
, count
);
609 vfs_die ("vfs_s_write: This should not happen\n");
613 /* --------------------------------------------------------------------------------------------- */
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
);
627 FH
->ino
->super
->me
->verrno
= errno
;
642 else if (offset
< size
)
649 /* --------------------------------------------------------------------------------------------- */
652 vfs_s_close (void *fh
)
655 struct vfs_class
*me
= FH_SUPER
->me
;
657 FH_SUPER
->fd_usage
--;
658 if (!FH_SUPER
->fd_usage
)
659 vfs_stamp_create (me
, FH_SUPER
);
661 if (FH
->linear
== LS_LINEAR_OPEN
)
662 MEDATA
->linear_close (me
, fh
);
663 if (MEDATA
->fh_close
)
664 res
= MEDATA
->fh_close (me
, fh
);
665 if ((MEDATA
->flags
& VFS_S_USETMP
) && FH
->changed
&& MEDATA
->file_store
)
667 char *s
= vfs_s_fullpath (me
, FH
->ino
);
672 res
= MEDATA
->file_store (me
, fh
, s
, FH
->ino
->localname
);
675 vfs_s_invalidate (me
, FH_SUPER
);
677 if (FH
->handle
!= -1)
680 vfs_s_free_inode (me
, FH
->ino
);
681 if (MEDATA
->fh_free_data
!= NULL
)
682 MEDATA
->fh_free_data (fh
);
687 /* --------------------------------------------------------------------------------------------- */
690 vfs_s_print_stats (const char *fs_name
, const char *action
,
691 const char *file_name
, off_t have
, off_t need
)
693 static const char *i18n_percent_transf_format
= NULL
;
694 static const char *i18n_transf_format
= NULL
;
696 if (i18n_percent_transf_format
== NULL
)
698 i18n_percent_transf_format
= "%s: %s: %s %3d%% (%" PRIuMAX
" %s";
699 i18n_transf_format
= "%s: %s: %s %" PRIuMAX
" %s";
703 vfs_print_message (i18n_percent_transf_format
, fs_name
, action
,
704 file_name
, (int) ((double) have
* 100 / need
), (uintmax_t) have
,
705 _("bytes transferred"));
707 vfs_print_message (i18n_transf_format
, fs_name
, action
, file_name
, (uintmax_t) have
,
708 _("bytes transferred"));
711 /* --------------------------------------------------------------------------------------------- */
712 /* ------------------------------- mc support ---------------------------- */
715 vfs_s_fill_names (struct vfs_class
*me
, fill_names_f func
)
719 for (iter
= MEDATA
->supers
; iter
!= NULL
; iter
= g_list_next (iter
))
721 const struct vfs_s_super
*super
= (const struct vfs_s_super
*) iter
->data
;
724 name
= g_strconcat (super
->name
, "/", me
->prefix
, VFS_PATH_URL_DELIMITER
,
725 /* super->current_dir->name, */ (char *) NULL
);
731 /* --------------------------------------------------------------------------------------------- */
734 vfs_s_ferrno (struct vfs_class
*me
)
739 /* --------------------------------------------------------------------------------------------- */
741 * Get local copy of the given file. We reuse the existing file cache
742 * for remote filesystems. Archives use standard VFS facilities.
746 vfs_s_getlocalcopy (const vfs_path_t
* vpath
)
748 vfs_file_handler_t
*fh
;
749 vfs_path_t
*local
= NULL
;
754 fh
= vfs_s_open (vpath
, O_RDONLY
, 0);
758 const struct vfs_class
*me
;
760 me
= vfs_path_get_by_index (vpath
, -1)->class;
761 if ((MEDATA
->flags
& VFS_S_USETMP
) != 0 && (fh
->ino
!= NULL
))
762 local
= vfs_path_from_str_flags (fh
->ino
->localname
, VPF_NO_CANON
);
770 /* --------------------------------------------------------------------------------------------- */
772 * Return the local copy. Since we are using our cache, we do nothing -
773 * the cache will be removed when the archive is closed.
777 vfs_s_ungetlocalcopy (const vfs_path_t
* vpath
, const vfs_path_t
* local
, gboolean has_changed
)
785 /* --------------------------------------------------------------------------------------------- */
788 vfs_s_setctl (const vfs_path_t
* vpath
, int ctlop
, void *arg
)
790 const vfs_path_element_t
*path_element
;
792 path_element
= vfs_path_get_by_index (vpath
, -1);
796 case VFS_SETCTL_STALE_DATA
:
798 struct vfs_s_inode
*ino
;
800 ino
= vfs_s_inode_from_path (vpath
, 0);
804 ino
->super
->want_stale
= 1;
807 ino
->super
->want_stale
= 0;
808 vfs_s_invalidate (path_element
->class, ino
->super
);
812 case VFS_SETCTL_LOGFILE
:
813 ((struct vfs_s_subclass
*) path_element
->class->data
)->logfile
= fopen ((char *) arg
, "w");
815 case VFS_SETCTL_FLUSH
:
816 ((struct vfs_s_subclass
*) path_element
->class->data
)->flush
= 1;
822 /* --------------------------------------------------------------------------------------------- */
823 /* ----------------------------- Stamping support -------------------------- */
826 vfs_s_getid (const vfs_path_t
* vpath
)
828 struct vfs_s_super
*archive
= NULL
;
831 p
= vfs_s_get_path (vpath
, &archive
, FL_NO_OPEN
);
835 return (vfsid
) archive
;
838 /* --------------------------------------------------------------------------------------------- */
841 vfs_s_nothingisopen (vfsid id
)
844 /* Our data structures should survive free of superblock at any time */
848 /* --------------------------------------------------------------------------------------------- */
851 vfs_s_free (vfsid id
)
853 vfs_s_free_super (((struct vfs_s_super
*) id
)->me
, (struct vfs_s_super
*) id
);
856 /* --------------------------------------------------------------------------------------------- */
859 vfs_s_dir_uptodate (struct vfs_class
*me
, struct vfs_s_inode
*ino
)
869 gettimeofday (&tim
, NULL
);
870 if (tim
.tv_sec
< ino
->timestamp
.tv_sec
)
876 /* --------------------------------------------------------------------------------------------- */
877 /*** public functions ****************************************************************************/
878 /* --------------------------------------------------------------------------------------------- */
881 vfs_s_new_inode (struct vfs_class
*me
, struct vfs_s_super
*super
, struct stat
*initstat
)
883 struct vfs_s_inode
*ino
;
885 ino
= g_try_new0 (struct vfs_s_inode
, 1);
892 ino
->st
.st_nlink
= 0;
893 ino
->st
.st_ino
= MEDATA
->inode_counter
++;
894 ino
->st
.st_dev
= MEDATA
->rdev
;
899 CALL (init_inode
) (me
, ino
);
904 /* --------------------------------------------------------------------------------------------- */
907 vfs_s_free_inode (struct vfs_class
*me
, struct vfs_s_inode
*ino
)
910 vfs_die ("Don't pass NULL to me");
912 /* ==0 can happen if freshly created entry is deleted */
913 if (ino
->st
.st_nlink
> 1)
919 while (ino
->subdir
!= NULL
)
920 vfs_s_free_entry (me
, (struct vfs_s_entry
*) ino
->subdir
->data
);
922 CALL (free_inode
) (me
, ino
);
923 g_free (ino
->linkname
);
924 if ((MEDATA
->flags
& VFS_S_USETMP
) != 0 && ino
->localname
!= NULL
)
926 unlink (ino
->localname
);
927 g_free (ino
->localname
);
930 ino
->super
->ino_usage
--;
934 /* --------------------------------------------------------------------------------------------- */
937 vfs_s_new_entry (struct vfs_class
*me
, const char *name
, struct vfs_s_inode
*inode
)
939 struct vfs_s_entry
*entry
;
941 entry
= g_new0 (struct vfs_s_entry
, 1);
944 entry
->name
= g_strdup (name
);
946 entry
->ino
->ent
= entry
;
947 CALL (init_entry
) (me
, entry
);
953 /* --------------------------------------------------------------------------------------------- */
956 vfs_s_free_entry (struct vfs_class
*me
, struct vfs_s_entry
*ent
)
958 if (ent
->dir
!= NULL
)
959 ent
->dir
->subdir
= g_list_remove (ent
->dir
->subdir
, ent
);
962 /* ent->name = NULL; */
964 if (ent
->ino
!= NULL
)
966 ent
->ino
->ent
= NULL
;
967 vfs_s_free_inode (me
, ent
->ino
);
974 /* --------------------------------------------------------------------------------------------- */
977 vfs_s_insert_entry (struct vfs_class
*me
, struct vfs_s_inode
*dir
, struct vfs_s_entry
*ent
)
983 ent
->ino
->st
.st_nlink
++;
984 dir
->subdir
= g_list_append (dir
->subdir
, ent
);
987 /* --------------------------------------------------------------------------------------------- */
990 vfs_s_default_stat (struct vfs_class
*me
, mode_t mode
)
992 static struct stat st
;
997 myumask
= umask (022);
1005 st
.st_uid
= getuid ();
1006 st
.st_gid
= getgid ();
1008 st
.st_mtime
= st
.st_atime
= st
.st_ctime
= time (NULL
);
1013 /* --------------------------------------------------------------------------------------------- */
1015 struct vfs_s_entry
*
1016 vfs_s_generate_entry (struct vfs_class
*me
, const char *name
, struct vfs_s_inode
*parent
,
1019 struct vfs_s_inode
*inode
;
1022 st
= vfs_s_default_stat (me
, mode
);
1023 inode
= vfs_s_new_inode (me
, parent
->super
, st
);
1025 return vfs_s_new_entry (me
, name
, inode
);
1028 /* --------------------------------------------------------------------------------------------- */
1030 struct vfs_s_inode
*
1031 vfs_s_find_inode (struct vfs_class
*me
, const struct vfs_s_super
*super
,
1032 const char *path
, int follow
, int flags
)
1034 struct vfs_s_entry
*ent
;
1036 if (((MEDATA
->flags
& VFS_S_REMOTE
) == 0) && (*path
== '\0'))
1039 ent
= (MEDATA
->find_entry
) (me
, super
->root
, path
, follow
, flags
);
1040 return (ent
!= NULL
) ? ent
->ino
: NULL
;
1043 /* --------------------------------------------------------------------------------------------- */
1044 /* Ook, these were functions around directory entries / inodes */
1045 /* -------------------------------- superblock games -------------------------- */
1047 * get superlock object by vpath
1050 * @return superlock object or NULL if not found
1053 struct vfs_s_super
*
1054 vfs_get_super_by_vpath (const vfs_path_t
* vpath
)
1057 void *cookie
= NULL
;
1058 const vfs_path_element_t
*path_element
;
1059 struct vfs_s_subclass
*subclass
;
1060 struct vfs_s_super
*super
= NULL
;
1061 vfs_path_t
*vpath_archive
;
1063 path_element
= vfs_path_get_by_index (vpath
, -1);
1064 subclass
= ((struct vfs_s_subclass
*) path_element
->class->data
);
1065 if (subclass
== NULL
)
1068 vpath_archive
= vfs_path_clone (vpath
);
1069 vfs_path_remove_element_by_index (vpath_archive
, -1);
1071 if (subclass
->archive_check
!= NULL
)
1073 cookie
= subclass
->archive_check (vpath_archive
);
1078 for (iter
= subclass
->supers
; iter
!= NULL
; iter
= g_list_next (iter
))
1082 super
= (struct vfs_s_super
*) iter
->data
;
1084 /* 0 == other, 1 == same, return it, 2 == other but stop scanning */
1085 i
= subclass
->archive_same (path_element
, super
, vpath_archive
, cookie
);
1095 vfs_path_free (vpath_archive
);
1099 /* --------------------------------------------------------------------------------------------- */
1101 * get path from last VFS-element and create corresponding superblock
1103 * @param vpath source path object
1104 * @param archive pointer to object for store newly created superblock
1105 * @param flags flags
1107 * @return path from last VFS-element
1110 vfs_s_get_path (const vfs_path_t
* vpath
, struct vfs_s_super
**archive
, int flags
)
1112 const char *retval
= "";
1114 struct vfs_s_super
*super
;
1115 const vfs_path_element_t
*path_element
;
1116 struct vfs_s_subclass
*subclass
;
1118 path_element
= vfs_path_get_by_index (vpath
, -1);
1120 if (path_element
->path
!= NULL
)
1121 retval
= path_element
->path
;
1123 super
= vfs_get_super_by_vpath (vpath
);
1125 goto return_success
;
1127 if (flags
& FL_NO_OPEN
)
1129 path_element
->class->verrno
= EIO
;
1133 super
= vfs_s_new_super (path_element
->class);
1135 subclass
= ((struct vfs_s_subclass
*) path_element
->class->data
);
1136 if (subclass
->open_archive
!= NULL
)
1138 vfs_path_t
*vpath_archive
;
1140 vpath_archive
= vfs_path_clone (vpath
);
1141 vfs_path_remove_element_by_index (vpath_archive
, -1);
1143 result
= subclass
->open_archive (super
, vpath_archive
, path_element
);
1144 vfs_path_free (vpath_archive
);
1148 vfs_s_free_super (path_element
->class, super
);
1149 path_element
->class->verrno
= EIO
;
1153 vfs_die ("You have to fill name\n");
1155 vfs_die ("You have to fill root inode\n");
1157 vfs_s_insert_super (path_element
->class, super
);
1158 vfs_stamp_create (path_element
->class, super
);
1165 /* --------------------------------------------------------------------------------------------- */
1168 vfs_s_invalidate (struct vfs_class
*me
, struct vfs_s_super
*super
)
1170 if (!super
->want_stale
)
1172 vfs_s_free_inode (me
, super
->root
);
1173 super
->root
= vfs_s_new_inode (me
, super
, vfs_s_default_stat (me
, S_IFDIR
| 0755));
1177 /* --------------------------------------------------------------------------------------------- */
1180 vfs_s_fullpath (struct vfs_class
*me
, struct vfs_s_inode
*ino
)
1183 ERRNOR (EAGAIN
, NULL
);
1185 if ((MEDATA
->flags
& VFS_S_USETMP
) == 0)
1189 char *path
= g_strdup (ino
->ent
->name
);
1192 ino
= ino
->ent
->dir
;
1193 if (ino
== ino
->super
->root
)
1195 newpath
= g_strconcat (ino
->ent
->name
, "/", path
, (char *) NULL
);
1202 /* remote systems */
1203 if ((!ino
->ent
->dir
) || (!ino
->ent
->dir
->ent
))
1204 return g_strdup (ino
->ent
->name
);
1206 return g_strconcat (ino
->ent
->dir
->ent
->name
, PATH_SEP_STR
, ino
->ent
->name
, (char *) NULL
);
1209 /* --------------------------------------------------------------------------------------------- */
1210 /* --------------------------- stat and friends ---------------------------- */
1213 vfs_s_open (const vfs_path_t
* vpath
, int flags
, mode_t mode
)
1215 int was_changed
= 0;
1216 vfs_file_handler_t
*fh
;
1217 struct vfs_s_super
*super
;
1219 struct vfs_s_inode
*ino
;
1220 const vfs_path_element_t
*path_element
;
1222 path_element
= vfs_path_get_by_index (vpath
, -1);
1224 q
= vfs_s_get_path (vpath
, &super
, 0);
1227 ino
= vfs_s_find_inode (path_element
->class, super
, q
, LINK_FOLLOW
, FL_NONE
);
1228 if (ino
&& ((flags
& (O_CREAT
| O_EXCL
)) == (O_CREAT
| O_EXCL
)))
1230 path_element
->class->verrno
= EEXIST
;
1235 char *dirname
, *name
;
1236 struct vfs_s_entry
*ent
;
1237 struct vfs_s_inode
*dir
;
1240 /* If the filesystem is read-only, disable file creation */
1241 if (!(flags
& O_CREAT
) || !(path_element
->class->write
))
1244 dirname
= g_path_get_dirname (q
);
1245 name
= g_path_get_basename (q
);
1246 dir
= vfs_s_find_inode (path_element
->class, super
, dirname
, LINK_FOLLOW
, FL_DIR
);
1253 ent
= vfs_s_generate_entry (path_element
->class, name
, dir
, 0755);
1255 vfs_s_insert_entry (path_element
->class, dir
, ent
);
1256 if ((VFSDATA (path_element
)->flags
& VFS_S_USETMP
) != 0)
1258 vfs_path_t
*tmp_vpath
;
1260 tmp_handle
= vfs_mkstemps (&tmp_vpath
, path_element
->class->name
, name
);
1261 ino
->localname
= vfs_path_to_str (tmp_vpath
);
1262 vfs_path_free (tmp_vpath
);
1263 if (tmp_handle
== -1)
1277 if (S_ISDIR (ino
->st
.st_mode
))
1279 path_element
->class->verrno
= EISDIR
;
1283 fh
= g_new (vfs_file_handler_t
, 1);
1287 fh
->changed
= was_changed
;
1291 if (IS_LINEAR (flags
))
1293 if (VFSDATA (path_element
)->linear_start
)
1295 vfs_print_message (_("Starting linear transfer..."));
1296 fh
->linear
= LS_LINEAR_PREOPEN
;
1301 struct vfs_s_subclass
*s
;
1303 s
= VFSDATA (path_element
);
1304 if (s
->fh_open
!= NULL
&& s
->fh_open (path_element
->class, fh
, flags
, mode
) != 0)
1306 if (s
->fh_free_data
!= NULL
)
1307 s
->fh_free_data (fh
);
1313 if ((VFSDATA (path_element
)->flags
& VFS_S_USETMP
) != 0 && fh
->ino
->localname
!= NULL
)
1315 fh
->handle
= open (fh
->ino
->localname
, NO_LINEAR (flags
), mode
);
1316 if (fh
->handle
== -1)
1319 path_element
->class->verrno
= errno
;
1324 /* i.e. we had no open files and now we have one */
1325 vfs_rmstamp (path_element
->class, (vfsid
) super
);
1327 fh
->ino
->st
.st_nlink
++;
1331 /* --------------------------------------------------------------------------------------------- */
1334 vfs_s_retrieve_file (struct vfs_class
*me
, struct vfs_s_inode
*ino
)
1336 /* If you want reget, you'll have to open file with O_LINEAR */
1340 off_t stat_size
= ino
->st
.st_size
;
1341 vfs_file_handler_t fh
;
1342 vfs_path_t
*tmp_vpath
;
1344 if ((MEDATA
->flags
& VFS_S_USETMP
) == 0)
1347 memset (&fh
, 0, sizeof (fh
));
1352 handle
= vfs_mkstemps (&tmp_vpath
, me
->name
, ino
->ent
->name
);
1353 ino
->localname
= vfs_path_to_str (tmp_vpath
);
1354 vfs_path_free (tmp_vpath
);
1361 if (!MEDATA
->linear_start (me
, &fh
, 0))
1364 /* Clear the interrupt status */
1365 tty_got_interrupt ();
1366 tty_enable_interrupt_key ();
1368 while ((n
= MEDATA
->linear_read (me
, &fh
, buffer
, sizeof (buffer
))))
1375 vfs_s_print_stats (me
->name
, _("Getting file"), ino
->ent
->name
, total
, stat_size
);
1377 if (tty_got_interrupt ())
1380 t
= write (handle
, buffer
, n
);
1388 MEDATA
->linear_close (me
, &fh
);
1391 tty_disable_interrupt_key ();
1396 MEDATA
->linear_close (me
, &fh
);
1398 tty_disable_interrupt_key ();
1400 unlink (ino
->localname
);
1402 g_free (ino
->localname
);
1403 ino
->localname
= NULL
;
1408 /* --------------------------------------------------------------------------------------------- */
1409 /* ----------------------------- Stamping support -------------------------- */
1411 /* Initialize one of our subclasses - fill common functions */
1413 vfs_s_init_class (struct vfs_class
*vclass
, struct vfs_s_subclass
*sub
)
1416 vclass
->fill_names
= vfs_s_fill_names
;
1417 vclass
->open
= vfs_s_open
;
1418 vclass
->close
= vfs_s_close
;
1419 vclass
->read
= vfs_s_read
;
1420 if (!(sub
->flags
& VFS_S_READONLY
))
1422 vclass
->write
= vfs_s_write
;
1424 vclass
->opendir
= vfs_s_opendir
;
1425 vclass
->readdir
= vfs_s_readdir
;
1426 vclass
->closedir
= vfs_s_closedir
;
1427 vclass
->stat
= vfs_s_stat
;
1428 vclass
->lstat
= vfs_s_lstat
;
1429 vclass
->fstat
= vfs_s_fstat
;
1430 vclass
->readlink
= vfs_s_readlink
;
1431 vclass
->chdir
= vfs_s_chdir
;
1432 vclass
->ferrno
= vfs_s_ferrno
;
1433 vclass
->lseek
= vfs_s_lseek
;
1434 vclass
->getid
= vfs_s_getid
;
1435 vclass
->nothingisopen
= vfs_s_nothingisopen
;
1436 vclass
->free
= vfs_s_free
;
1437 if ((sub
->flags
& VFS_S_USETMP
) != 0)
1439 vclass
->getlocalcopy
= vfs_s_getlocalcopy
;
1440 vclass
->ungetlocalcopy
= vfs_s_ungetlocalcopy
;
1441 sub
->find_entry
= vfs_s_find_entry_linear
;
1443 else if ((sub
->flags
& VFS_S_REMOTE
) != 0)
1444 sub
->find_entry
= vfs_s_find_entry_linear
;
1446 sub
->find_entry
= vfs_s_find_entry_tree
;
1447 vclass
->setctl
= vfs_s_setctl
;
1448 sub
->dir_uptodate
= vfs_s_dir_uptodate
;
1451 /* --------------------------------------------------------------------------------------------- */
1452 /** Find VFS id for given directory name */
1455 vfs_getid (const vfs_path_t
* vpath
)
1457 const vfs_path_element_t
*path_element
;
1459 path_element
= vfs_path_get_by_index (vpath
, -1);
1460 if (!vfs_path_element_valid (path_element
) || path_element
->class->getid
== NULL
)
1463 return (*path_element
->class->getid
) (vpath
);
1466 /* --------------------------------------------------------------------------------------------- */
1467 /* ----------- Utility functions for networked filesystems -------------- */
1469 #ifdef ENABLE_VFS_NET
1471 vfs_s_select_on_two (int fd1
, int fd2
)
1474 struct timeval time_out
;
1476 int maxfd
= (fd1
> fd2
? fd1
: fd2
) + 1;
1478 time_out
.tv_sec
= 1;
1479 time_out
.tv_usec
= 0;
1483 v
= select (maxfd
, &set
, 0, 0, &time_out
);
1486 if (FD_ISSET (fd1
, &set
))
1488 if (FD_ISSET (fd2
, &set
))
1493 /* --------------------------------------------------------------------------------------------- */
1496 vfs_s_get_line (struct vfs_class
*me
, int sock
, char *buf
, int buf_len
, char term
)
1498 FILE *logfile
= MEDATA
->logfile
;
1502 for (i
= 0; i
< buf_len
- 1; i
++, buf
++)
1504 if (read (sock
, buf
, sizeof (char)) <= 0)
1510 ret1
= fwrite (buf
, 1, 1, logfile
);
1511 ret2
= fflush (logfile
);
1520 /* Line is too long - terminate buffer and discard the rest of line */
1522 while (read (sock
, &c
, sizeof (c
)) > 0)
1528 ret1
= fwrite (&c
, 1, 1, logfile
);
1529 ret2
= fflush (logfile
);
1537 /* --------------------------------------------------------------------------------------------- */
1540 vfs_s_get_line_interruptible (struct vfs_class
*me
, char *buffer
, int size
, int fd
)
1547 tty_enable_interrupt_key ();
1548 for (i
= 0; i
< size
- 1; i
++)
1550 n
= read (fd
, buffer
+ i
, 1);
1551 tty_disable_interrupt_key ();
1552 if (n
== -1 && errno
== EINTR
)
1562 if (buffer
[i
] == '\n')
1568 buffer
[size
- 1] = 0;
1571 #endif /* ENABLE_VFS_NET */
1573 /* --------------------------------------------------------------------------------------------- */
1575 * Normalize filenames start position
1579 vfs_s_normalize_filename_leading_spaces (struct vfs_s_inode
*root_inode
, size_t final_num_spaces
)
1583 for (iter
= root_inode
->subdir
; iter
!= NULL
; iter
= g_list_next (iter
))
1585 struct vfs_s_entry
*entry
= (struct vfs_s_entry
*) iter
->data
;
1586 if ((size_t) entry
->ino
->data_offset
> final_num_spaces
)
1588 char *source_name
= entry
->name
;
1589 char *spacer
= g_strnfill (entry
->ino
->data_offset
- final_num_spaces
, ' ');
1590 entry
->name
= g_strdup_printf ("%s%s", spacer
, source_name
);
1592 g_free (source_name
);
1594 entry
->ino
->data_offset
= -1;
1598 /* --------------------------------------------------------------------------------------------- */