Ticket #212
[midnight-commander.git] / vfs / direntry.c
blobf7a8bfb25a400ff056db76601f2568c62e63cea9
2 /** \file
3 * \brief Source: directory cache support
5 * So that you do not have copy of this in each and every filesystem.
7 * Very loosely based on tar.c from midnight and archives.[ch] from
8 * avfs by Miklos Szeredi (mszeredi@inf.bme.hu)
10 * Unfortunately, I was unable to keep all filesystems
11 * uniform. tar-like filesystems use tree structure where each
12 * directory has pointers to its subdirectories. We can do this
13 * because we have full information about our archive.
15 * At ftp-like filesystems, situation is a little bit different. When
16 * you cd /usr/src/linux/drivers/char, you do _not_ want /usr,
17 * /usr/src, /usr/src/linux and /usr/src/linux/drivers to be
18 * listed. That means that we do not have complete information, and if
19 * /usr is symlink to /4, we will not know. Also we have to time out
20 * entries and things would get messy with tree-like approach. So we
21 * do different trick: root directory is completely special and
22 * completely fake, it contains entries such as 'usr', 'usr/src', ...,
23 * and we'll try to use custom find_entry function.
25 * \author Pavel Machek <pavel@ucw.cz>, distribute under LGPL.
26 * \date 1998
28 * \warning Paths here do _not_ begin with '/', so root directory of
29 * archive/site is simply "".
32 #include <config.h>
34 #include <errno.h>
35 #include <sys/fcntl.h>
36 #include <time.h>
37 #include <sys/time.h> /* gettimeofday() */
39 #include "../src/global.h"
41 #include "../src/tty/tty.h" /* enable/disable interrupt key */
43 #include "../src/wtools.h" /* message() */
44 #include "../src/main.h" /* print_vfs_message */
46 #include "utilvfs.h"
47 #include "vfs-impl.h"
48 #include "gc.h" /* vfs_rmstamp */
49 #include "xdirentry.h"
51 #define CALL(x) if (MEDATA->x) MEDATA->x
53 static volatile int total_inodes = 0, total_entries = 0;
55 struct vfs_s_inode *
56 vfs_s_new_inode (struct vfs_class *me, struct vfs_s_super *super, struct stat *initstat)
58 struct vfs_s_inode *ino;
60 ino = g_new0 (struct vfs_s_inode, 1);
61 if (!ino)
62 return NULL;
64 if (initstat)
65 ino->st = *initstat;
66 ino->super = super;
67 ino->st.st_nlink = 0;
68 ino->st.st_ino = MEDATA->inode_counter++;
69 ino->st.st_dev = MEDATA->rdev;
71 super->ino_usage++;
72 total_inodes++;
74 CALL (init_inode) (me, ino);
76 return ino;
79 struct vfs_s_entry *
80 vfs_s_new_entry (struct vfs_class *me, const char *name, struct vfs_s_inode *inode)
82 struct vfs_s_entry *entry;
84 entry = g_new0 (struct vfs_s_entry, 1);
85 total_entries++;
87 if (name)
88 entry->name = g_strdup (name);
90 entry->ino = inode;
91 entry->ino->ent = entry;
92 CALL (init_entry) (me, entry);
94 return entry;
97 static void
98 vfs_s_free_inode (struct vfs_class *me, struct vfs_s_inode *ino)
100 if (!ino)
101 vfs_die ("Don't pass NULL to me");
103 /* ==0 can happen if freshly created entry is deleted */
104 if (ino->st.st_nlink <= 1){
105 while (ino->subdir){
106 vfs_s_free_entry (me, ino->subdir);
109 CALL (free_inode) (me, ino);
110 g_free (ino->linkname);
111 if (ino->localname){
112 unlink (ino->localname);
113 g_free(ino->localname);
115 total_inodes--;
116 ino->super->ino_usage--;
117 g_free(ino);
118 } else ino->st.st_nlink--;
121 void
122 vfs_s_free_entry (struct vfs_class *me, struct vfs_s_entry *ent)
124 if (ent->prevp){ /* It is possible that we are deleting freshly created entry */
125 *ent->prevp = ent->next;
126 if (ent->next)
127 ent->next->prevp = ent->prevp;
130 g_free (ent->name);
131 ent->name = NULL;
133 if (ent->ino){
134 ent->ino->ent = NULL;
135 vfs_s_free_inode (me, ent->ino);
136 ent->ino = NULL;
139 total_entries--;
140 g_free(ent);
143 void
144 vfs_s_insert_entry (struct vfs_class *me, struct vfs_s_inode *dir, struct vfs_s_entry *ent)
146 struct vfs_s_entry **ep;
148 (void) me;
150 for (ep = &dir->subdir; *ep != NULL; ep = &((*ep)->next))
152 ent->prevp = ep;
153 ent->next = NULL;
154 ent->dir = dir;
155 *ep = ent;
157 ent->ino->st.st_nlink++;
160 struct stat *
161 vfs_s_default_stat (struct vfs_class *me, mode_t mode)
163 static struct stat st;
164 int myumask;
166 (void) me;
168 myumask = umask (022);
169 umask (myumask);
170 mode &= ~myumask;
172 st.st_mode = mode;
173 st.st_ino = 0;
174 st.st_dev = 0;
175 st.st_rdev = 0;
176 st.st_uid = getuid ();
177 st.st_gid = getgid ();
178 st.st_size = 0;
179 st.st_mtime = st.st_atime = st.st_ctime = time (NULL);
181 return &st;
184 struct vfs_s_entry *
185 vfs_s_generate_entry (struct vfs_class *me, const char *name, struct vfs_s_inode *parent, mode_t mode)
187 struct vfs_s_inode *inode;
188 struct stat *st;
190 st = vfs_s_default_stat (me, mode);
191 inode = vfs_s_new_inode (me, parent->super, st);
193 return vfs_s_new_entry (me, name, inode);
196 /* We were asked to create entries automagically */
197 static struct vfs_s_entry *
198 vfs_s_automake (struct vfs_class *me, struct vfs_s_inode *dir, char *path, int flags)
200 struct vfs_s_entry *res;
201 char *sep = strchr (path, PATH_SEP);
203 if (sep)
204 *sep = 0;
205 res = vfs_s_generate_entry (me, path, dir, flags & FL_MKDIR ? (0777 | S_IFDIR) : 0777);
206 vfs_s_insert_entry (me, dir, res);
208 if (sep)
209 *sep = PATH_SEP;
211 return res;
214 /* If the entry is a symlink, find the entry for its target */
215 static struct vfs_s_entry *
216 vfs_s_resolve_symlink (struct vfs_class *me, struct vfs_s_entry *entry,
217 int follow)
219 char *linkname;
220 char *fullname = NULL;
221 struct vfs_s_entry *target;
223 if (follow == LINK_NO_FOLLOW)
224 return entry;
225 if (follow == 0)
226 ERRNOR (ELOOP, NULL);
227 if (!entry)
228 ERRNOR (ENOENT, NULL);
229 if (!S_ISLNK (entry->ino->st.st_mode))
230 return entry;
232 linkname = entry->ino->linkname;
233 if (linkname == NULL)
234 ERRNOR (EFAULT, NULL);
236 /* make full path from relative */
237 if (*linkname != PATH_SEP) {
238 char *fullpath = vfs_s_fullpath (me, entry->dir);
239 if (fullpath) {
240 fullname = g_strconcat (fullpath, "/", linkname, NULL);
241 linkname = fullname;
242 g_free (fullpath);
246 target =
247 (MEDATA->find_entry) (me, entry->dir->super->root, linkname,
248 follow - 1, 0);
249 g_free (fullname);
250 return target;
254 * Follow > 0: follow links, serves as loop protect,
255 * == -1: do not follow links
257 static struct vfs_s_entry *
258 vfs_s_find_entry_tree (struct vfs_class *me, struct vfs_s_inode *root,
259 const char *a_path, int follow, int flags)
261 size_t pseg;
262 struct vfs_s_entry *ent = NULL;
263 char * const pathref = g_strdup (a_path);
264 char *path = pathref;
266 canonicalize_pathname (path);
268 while (root) {
269 while (*path == PATH_SEP) /* Strip leading '/' */
270 path++;
272 if (!path[0]) {
273 g_free (pathref);
274 return ent;
277 for (pseg = 0; path[pseg] && path[pseg] != PATH_SEP; pseg++);
279 for (ent = root->subdir; ent != NULL; ent = ent->next)
280 if (strlen (ent->name) == pseg
281 && (!strncmp (ent->name, path, pseg)))
282 /* FOUND! */
283 break;
285 if (!ent && (flags & (FL_MKFILE | FL_MKDIR)))
286 ent = vfs_s_automake (me, root, path, flags);
287 if (!ent) {
288 me->verrno = ENOENT;
289 goto cleanup;
291 path += pseg;
292 /* here we must follow leading directories always;
293 only the actual file is optional */
294 ent =
295 vfs_s_resolve_symlink (me, ent,
296 strchr (path,
297 PATH_SEP) ? LINK_FOLLOW :
298 follow);
299 if (!ent)
300 goto cleanup;
301 root = ent->ino;
303 cleanup:
304 g_free (pathref);
305 return NULL;
308 static void
309 split_dir_name (struct vfs_class *me, char *path, char **dir, char **name, char **save)
311 char *s;
313 (void) me;
315 s = strrchr (path, PATH_SEP);
316 if (s == NULL) {
317 *save = NULL;
318 *name = path;
319 *dir = path + strlen(path); /* an empty string */
320 } else {
321 *save = s;
322 *dir = path;
323 *s++ = '\0';
324 *name = s;
328 static struct vfs_s_entry *
329 vfs_s_find_entry_linear (struct vfs_class *me, struct vfs_s_inode *root,
330 const char *a_path, int follow, int flags)
332 struct vfs_s_entry *ent = NULL;
333 char * const path = g_strdup (a_path);
334 struct vfs_s_entry *retval = NULL;
336 if (root->super->root != root)
337 vfs_die ("We have to use _real_ root. Always. Sorry.");
339 canonicalize_pathname (path);
341 if (!(flags & FL_DIR)) {
342 char *dirname, *name, *save;
343 struct vfs_s_inode *ino;
344 split_dir_name (me, path, &dirname, &name, &save);
345 ino =
346 vfs_s_find_inode (me, root->super, dirname, follow,
347 flags | FL_DIR);
348 if (save)
349 *save = PATH_SEP;
350 retval = vfs_s_find_entry_tree (me, ino, name, follow, flags);
351 g_free (path);
352 return retval;
355 for (ent = root->subdir; ent != NULL; ent = ent->next)
356 if (!strcmp (ent->name, path))
357 break;
359 if (ent && (!(MEDATA->dir_uptodate) (me, ent->ino))) {
360 #if 1
361 print_vfs_message (_("Directory cache expired for %s"), path);
362 #endif
363 vfs_s_free_entry (me, ent);
364 ent = NULL;
367 if (!ent) {
368 struct vfs_s_inode *ino;
370 ino =
371 vfs_s_new_inode (me, root->super,
372 vfs_s_default_stat (me, S_IFDIR | 0755));
373 ent = vfs_s_new_entry (me, path, ino);
374 if ((MEDATA->dir_load) (me, ino, path) == -1) {
375 vfs_s_free_entry (me, ent);
376 g_free (path);
377 return NULL;
379 vfs_s_insert_entry (me, root, ent);
381 for (ent = root->subdir; ent != NULL; ent = ent->next)
382 if (!strcmp (ent->name, path))
383 break;
385 if (!ent)
386 vfs_die ("find_linear: success but directory is not there\n");
388 #if 0
389 if (!vfs_s_resolve_symlink (me, ent, follow)) {
390 g_free (path);
391 return NULL;
393 #endif
394 g_free (path);
395 return ent;
398 struct vfs_s_inode *
399 vfs_s_find_inode (struct vfs_class *me, const struct vfs_s_super *super,
400 const char *path, int follow, int flags)
402 struct vfs_s_entry *ent;
403 if (!(MEDATA->flags & VFS_S_REMOTE) && (!*path))
404 return super->root;
405 ent = (MEDATA->find_entry) (me, super->root, path, follow, flags);
406 if (!ent)
407 return NULL;
408 return ent->ino;
411 /* Ook, these were functions around directory entries / inodes */
412 /* -------------------------------- superblock games -------------------------- */
414 static struct vfs_s_super *
415 vfs_s_new_super (struct vfs_class *me)
417 struct vfs_s_super *super;
419 super = g_new0 (struct vfs_s_super, 1);
420 super->me = me;
421 return super;
424 static void
425 vfs_s_insert_super (struct vfs_class *me, struct vfs_s_super *super)
427 super->next = MEDATA->supers;
428 super->prevp = &MEDATA->supers;
430 if (MEDATA->supers != NULL)
431 MEDATA->supers->prevp = &super->next;
432 MEDATA->supers = super;
435 static void
436 vfs_s_free_super (struct vfs_class *me, struct vfs_s_super *super)
438 if (super->root){
439 vfs_s_free_inode (me, super->root);
440 super->root = NULL;
443 #if 0
444 /* FIXME: We currently leak small ammount of memory, sometimes. Fix it if you can. */
445 if (super->ino_usage)
446 message (D_ERROR, " Direntry warning ",
447 "Super ino_usage is %d, memory leak",
448 super->ino_usage);
450 if (super->want_stale)
451 message (D_ERROR, " Direntry warning ", "Super has want_stale set");
452 #endif
454 if (super->prevp){
455 *super->prevp = super->next;
456 if (super->next)
457 super->next->prevp = super->prevp;
460 CALL (free_archive) (me, super);
461 g_free (super->name);
462 g_free(super);
467 * Dissect the path and create corresponding superblock. Note that inname
468 * can be changed and the result may point inside the original string.
470 const char *
471 vfs_s_get_path_mangle (struct vfs_class *me, char *inname,
472 struct vfs_s_super **archive, int flags)
474 const char *retval;
475 char *local, *op;
476 const char *archive_name;
477 int result = -1;
478 struct vfs_s_super *super;
479 void *cookie = NULL;
481 archive_name = inname;
482 vfs_split (inname, &local, &op);
483 retval = (local) ? local : "";
485 if (MEDATA->archive_check)
486 if (!(cookie = MEDATA->archive_check (me, archive_name, op)))
487 return NULL;
489 for (super = MEDATA->supers; super != NULL; super = super->next) {
490 /* 0 == other, 1 == same, return it, 2 == other but stop scanning */
491 int i;
492 if ((i =
493 MEDATA->archive_same (me, super, archive_name, op, cookie))) {
494 if (i == 1)
495 goto return_success;
496 else
497 break;
501 if (flags & FL_NO_OPEN)
502 ERRNOR (EIO, NULL);
504 super = vfs_s_new_super (me);
505 result = MEDATA->open_archive (me, super, archive_name, op);
506 if (result == -1) {
507 vfs_s_free_super (me, super);
508 ERRNOR (EIO, NULL);
510 if (!super->name)
511 vfs_die ("You have to fill name\n");
512 if (!super->root)
513 vfs_die ("You have to fill root inode\n");
515 vfs_s_insert_super (me, super);
516 vfs_stamp_create (me, super);
518 return_success:
519 *archive = super;
520 return retval;
525 * Dissect the path and create corresponding superblock.
526 * The result should be freed.
528 static char *
529 vfs_s_get_path (struct vfs_class *me, const char *inname,
530 struct vfs_s_super **archive, int flags)
532 char *buf, *retval;
534 buf = g_strdup (inname);
535 retval = g_strdup (vfs_s_get_path_mangle (me, buf, archive, flags));
536 g_free (buf);
537 return retval;
540 void
541 vfs_s_invalidate (struct vfs_class *me, struct vfs_s_super *super)
543 if (!super->want_stale){
544 vfs_s_free_inode (me, super->root);
545 super->root = vfs_s_new_inode (me, super, vfs_s_default_stat (me, S_IFDIR | 0755));
549 char *
550 vfs_s_fullpath (struct vfs_class *me, struct vfs_s_inode *ino)
552 if (!ino->ent)
553 ERRNOR (EAGAIN, NULL);
555 if (!(MEDATA->flags & VFS_S_REMOTE)) {
556 /* archives */
557 char *newpath;
558 char *path = g_strdup (ino->ent->name);
559 while (1) {
560 ino = ino->ent->dir;
561 if (ino == ino->super->root)
562 break;
563 newpath = g_strconcat (ino->ent->name, "/", path, (char *) NULL);
564 g_free (path);
565 path = newpath;
567 return path;
570 /* remote systems */
571 if ((!ino->ent->dir) || (!ino->ent->dir->ent))
572 return g_strdup (ino->ent->name);
574 return g_strconcat (ino->ent->dir->ent->name, PATH_SEP_STR,
575 ino->ent->name, (char *) NULL);
578 /* Support of archives */
579 /* ------------------------ readdir & friends ----------------------------- */
581 static struct vfs_s_inode *
582 vfs_s_inode_from_path (struct vfs_class *me, const char *name, int flags)
584 struct vfs_s_super *super;
585 struct vfs_s_inode *ino;
586 char *q;
588 if (!(q = vfs_s_get_path (me, name, &super, 0)))
589 return NULL;
591 ino =
592 vfs_s_find_inode (me, super, q,
593 flags & FL_FOLLOW ? LINK_FOLLOW : LINK_NO_FOLLOW,
594 flags & ~FL_FOLLOW);
595 if ((!ino) && (!*q))
596 /* We are asking about / directory of ftp server: assume it exists */
597 ino =
598 vfs_s_find_inode (me, super, q,
599 flags & FL_FOLLOW ? LINK_FOLLOW :
600 LINK_NO_FOLLOW,
601 FL_DIR | (flags & ~FL_FOLLOW));
602 g_free (q);
603 return ino;
606 struct dirhandle {
607 struct vfs_s_entry *cur;
608 struct vfs_s_inode *dir;
611 static void *
612 vfs_s_opendir (struct vfs_class *me, const char *dirname)
614 struct vfs_s_inode *dir;
615 struct dirhandle *info;
617 dir = vfs_s_inode_from_path (me, dirname, FL_DIR | FL_FOLLOW);
618 if (!dir)
619 return NULL;
620 if (!S_ISDIR (dir->st.st_mode))
621 ERRNOR (ENOTDIR, NULL);
623 dir->st.st_nlink++;
624 #if 0
625 if (!dir->subdir) /* This can actually happen if we allow empty directories */
626 ERRNOR (EAGAIN, NULL);
627 #endif
628 info = g_new (struct dirhandle, 1);
629 info->cur = dir->subdir;
630 info->dir = dir;
632 return info;
635 static void *
636 vfs_s_readdir(void *data)
638 static union vfs_dirent dir;
639 struct dirhandle *info = (struct dirhandle *) data;
641 if (!(info->cur))
642 return NULL;
644 if (info->cur->name) {
645 g_strlcpy (dir.dent.d_name, info->cur->name, MC_MAXPATHLEN);
646 } else {
647 vfs_die("Null in structure-cannot happen");
650 compute_namelen(&dir.dent);
651 info->cur = info->cur->next;
653 return (void *) &dir;
656 static int
657 vfs_s_closedir (void *data)
659 struct dirhandle *info = (struct dirhandle *) data;
660 struct vfs_s_inode *dir = info->dir;
662 vfs_s_free_inode (dir->super->me, dir);
663 g_free (data);
664 return 0;
667 static int
668 vfs_s_chdir (struct vfs_class *me, const char *path)
670 void *data;
671 if (!(data = vfs_s_opendir (me, path)))
672 return -1;
673 vfs_s_closedir (data);
674 return 0;
677 /* --------------------------- stat and friends ---------------------------- */
679 static int
680 vfs_s_internal_stat (struct vfs_class *me, const char *path, struct stat *buf, int flag)
682 struct vfs_s_inode *ino;
684 if (!(ino = vfs_s_inode_from_path (me, path, flag)))
685 return -1;
686 *buf = ino->st;
687 return 0;
690 static int
691 vfs_s_stat (struct vfs_class *me, const char *path, struct stat *buf)
693 return vfs_s_internal_stat (me, path, buf, FL_FOLLOW);
696 static int
697 vfs_s_lstat (struct vfs_class *me, const char *path, struct stat *buf)
699 return vfs_s_internal_stat (me, path, buf, FL_NONE);
702 static int
703 vfs_s_fstat (void *fh, struct stat *buf)
705 *buf = FH->ino->st;
706 return 0;
709 static int
710 vfs_s_readlink (struct vfs_class *me, const char *path, char *buf, size_t size)
712 struct vfs_s_inode *ino;
713 size_t len;
715 ino = vfs_s_inode_from_path (me, path, 0);
716 if (!ino)
717 return -1;
719 if (!S_ISLNK (ino->st.st_mode))
720 ERRNOR (EINVAL, -1);
722 if (ino->linkname == NULL)
723 ERRNOR (EFAULT, -1);
725 len = strlen (ino->linkname);
726 if (size < len)
727 len = size;
728 /* readlink() does not append a NUL character to buf */
729 memcpy (buf, ino->linkname, len);
730 return len;
733 void *
734 vfs_s_open (struct vfs_class *me, const char *file, int flags, int mode)
736 int was_changed = 0;
737 struct vfs_s_fh *fh;
738 struct vfs_s_super *super;
739 char *q;
740 struct vfs_s_inode *ino;
742 if ((q = vfs_s_get_path (me, file, &super, 0)) == NULL)
743 return NULL;
744 ino = vfs_s_find_inode (me, super, q, LINK_FOLLOW, FL_NONE);
745 if (ino && ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))) {
746 g_free (q);
747 ERRNOR (EEXIST, NULL);
749 if (!ino) {
750 char *dirname, *name, *save;
751 struct vfs_s_entry *ent;
752 struct vfs_s_inode *dir;
753 int tmp_handle;
755 /* If the filesystem is read-only, disable file creation */
756 if (!(flags & O_CREAT) || !(me->write)) {
757 g_free (q);
758 return NULL;
761 split_dir_name (me, q, &dirname, &name, &save);
762 /* FIXME: check if vfs_s_find_inode returns NULL */
763 dir = vfs_s_find_inode (me, super, dirname, LINK_FOLLOW, FL_DIR);
764 if (save)
765 *save = PATH_SEP;
766 ent = vfs_s_generate_entry (me, name, dir, 0755);
767 ino = ent->ino;
768 vfs_s_insert_entry (me, dir, ent);
769 tmp_handle = vfs_mkstemps (&ino->localname, me->name, name);
770 if (tmp_handle == -1) {
771 g_free (q);
772 return NULL;
774 close (tmp_handle);
775 was_changed = 1;
778 g_free (q);
780 if (S_ISDIR (ino->st.st_mode))
781 ERRNOR (EISDIR, NULL);
783 fh = g_new (struct vfs_s_fh, 1);
784 fh->pos = 0;
785 fh->ino = ino;
786 fh->handle = -1;
787 fh->changed = was_changed;
788 fh->linear = 0;
790 if (IS_LINEAR (flags)) {
791 if (MEDATA->linear_start) {
792 print_vfs_message (_("Starting linear transfer..."));
793 fh->linear = LS_LINEAR_PREOPEN;
795 } else if ((MEDATA->fh_open)
796 && (MEDATA->fh_open (me, fh, flags, mode))) {
797 g_free (fh);
798 return NULL;
801 if (fh->ino->localname) {
802 fh->handle = open (fh->ino->localname, NO_LINEAR (flags), mode);
803 if (fh->handle == -1) {
804 g_free (fh);
805 ERRNOR (errno, NULL);
809 /* i.e. we had no open files and now we have one */
810 vfs_rmstamp (me, (vfsid) super);
811 super->fd_usage++;
812 fh->ino->st.st_nlink++;
813 return fh;
816 static ssize_t
817 vfs_s_read (void *fh, char *buffer, int count)
819 int n;
820 struct vfs_class *me = FH_SUPER->me;
822 if (FH->linear == LS_LINEAR_PREOPEN) {
823 if (!MEDATA->linear_start (me, FH, FH->pos))
824 return -1;
827 if (FH->linear == LS_LINEAR_CLOSED)
828 vfs_die ("linear_start() did not set linear_state!");
830 if (FH->linear == LS_LINEAR_OPEN)
831 return MEDATA->linear_read (me, FH, buffer, count);
833 if (FH->handle != -1){
834 n = read (FH->handle, buffer, count);
835 if (n < 0)
836 me->verrno = errno;
837 return n;
839 vfs_die ("vfs_s_read: This should not happen\n");
840 return -1;
843 static ssize_t
844 vfs_s_write (void *fh, const char *buffer, int count)
846 int n;
847 struct vfs_class *me = FH_SUPER->me;
849 if (FH->linear)
850 vfs_die ("no writing to linear files, please");
852 FH->changed = 1;
853 if (FH->handle != -1){
854 n = write (FH->handle, buffer, count);
855 if (n < 0)
856 me->verrno = errno;
857 return n;
859 vfs_die ("vfs_s_write: This should not happen\n");
860 return 0;
863 static off_t
864 vfs_s_lseek (void *fh, off_t offset, int whence)
866 off_t size = FH->ino->st.st_size;
868 if (FH->linear == LS_LINEAR_OPEN)
869 vfs_die ("cannot lseek() after linear_read!");
871 if (FH->handle != -1){ /* If we have local file opened, we want to work with it */
872 int retval = lseek (FH->handle, offset, whence);
873 if (retval == -1)
874 FH->ino->super->me->verrno = errno;
875 return retval;
878 switch (whence){
879 case SEEK_CUR:
880 offset += FH->pos; break;
881 case SEEK_END:
882 offset += size; break;
884 if (offset < 0)
885 FH->pos = 0;
886 else if (offset < size)
887 FH->pos = offset;
888 else
889 FH->pos = size;
890 return FH->pos;
893 static int
894 vfs_s_close (void *fh)
896 int res = 0;
897 struct vfs_class *me = FH_SUPER->me;
899 FH_SUPER->fd_usage--;
900 if (!FH_SUPER->fd_usage)
901 vfs_stamp_create (me, FH_SUPER);
903 if (FH->linear == LS_LINEAR_OPEN)
904 MEDATA->linear_close (me, fh);
905 if (MEDATA->fh_close)
906 res = MEDATA->fh_close (me, fh);
907 if (FH->changed && MEDATA->file_store){
908 char *s = vfs_s_fullpath (me, FH->ino);
909 if (!s)
910 res = -1;
911 else {
912 res = MEDATA->file_store (me, fh, s, FH->ino->localname);
913 g_free (s);
915 vfs_s_invalidate (me, FH_SUPER);
917 if (FH->handle != -1)
918 close (FH->handle);
920 vfs_s_free_inode (me, FH->ino);
921 g_free (fh);
922 return res;
925 static void
926 vfs_s_print_stats (const char *fs_name, const char *action,
927 const char *file_name, off_t have, off_t need)
929 static const char *i18n_percent_transf_format = NULL;
930 static const char *i18n_transf_format = NULL;
932 if (i18n_percent_transf_format == NULL) {
933 i18n_percent_transf_format =
934 _("%s: %s: %s %3d%% (%lu bytes transferred)");
935 i18n_transf_format = _("%s: %s: %s %lu bytes transferred");
938 if (need)
939 print_vfs_message (i18n_percent_transf_format, fs_name, action,
940 file_name, (int) ((double) have * 100 / need),
941 (unsigned long) have);
942 else
943 print_vfs_message (i18n_transf_format, fs_name, action, file_name,
944 (unsigned long) have);
948 vfs_s_retrieve_file (struct vfs_class *me, struct vfs_s_inode *ino)
950 /* If you want reget, you'll have to open file with O_LINEAR */
951 off_t total = 0;
952 char buffer[8192];
953 int handle, n;
954 off_t stat_size = ino->st.st_size;
955 struct vfs_s_fh fh;
957 memset (&fh, 0, sizeof (fh));
959 fh.ino = ino;
960 fh.handle = -1;
962 handle = vfs_mkstemps (&ino->localname, me->name, ino->ent->name);
963 if (handle == -1) {
964 me->verrno = errno;
965 goto error_4;
968 if (!MEDATA->linear_start (me, &fh, 0))
969 goto error_3;
971 /* Clear the interrupt status */
972 tty_got_interrupt ();
973 tty_enable_interrupt_key ();
975 while ((n = MEDATA->linear_read (me, &fh, buffer, sizeof (buffer)))) {
976 int t;
977 if (n < 0)
978 goto error_1;
980 total += n;
981 vfs_s_print_stats (me->name, _("Getting file"), ino->ent->name,
982 total, stat_size);
984 if (tty_got_interrupt ())
985 goto error_1;
987 t = write (handle, buffer, n);
988 if (t != n) {
989 if (t == -1)
990 me->verrno = errno;
991 goto error_1;
994 MEDATA->linear_close (me, &fh);
995 close (handle);
997 tty_disable_interrupt_key ();
998 return 0;
1000 error_1:
1001 MEDATA->linear_close (me, &fh);
1002 error_3:
1003 tty_disable_interrupt_key ();
1004 close (handle);
1005 unlink (ino->localname);
1006 error_4:
1007 g_free (ino->localname);
1008 ino->localname = NULL;
1009 return -1;
1012 /* ------------------------------- mc support ---------------------------- */
1014 static void
1015 vfs_s_fill_names (struct vfs_class *me, fill_names_f func)
1017 struct vfs_s_super *a = MEDATA->supers;
1018 char *name;
1020 while (a){
1021 name = g_strconcat ( a->name, "#", me->prefix, "/", /* a->current_dir->name, */ NULL);
1022 (*func)(name);
1023 g_free (name);
1024 a = a->next;
1028 static int
1029 vfs_s_ferrno (struct vfs_class *me)
1031 return me->verrno;
1035 * Get local copy of the given file. We reuse the existing file cache
1036 * for remote filesystems. Archives use standard VFS facilities.
1038 static char *
1039 vfs_s_getlocalcopy (struct vfs_class *me, const char *path)
1041 struct vfs_s_fh *fh;
1042 char *local;
1044 fh = vfs_s_open (me, path, O_RDONLY, 0);
1045 if (!fh || !fh->ino || !fh->ino->localname)
1046 return NULL;
1048 local = g_strdup (fh->ino->localname);
1049 vfs_s_close (fh);
1050 return local;
1054 * Return the local copy. Since we are using our cache, we do nothing -
1055 * the cache will be removed when the archive is closed.
1057 static int
1058 vfs_s_ungetlocalcopy (struct vfs_class *me, const char *path,
1059 const char *local, int has_changed)
1061 (void) me;
1062 (void) path;
1063 (void) local;
1064 (void) has_changed;
1065 return 0;
1068 static int
1069 vfs_s_setctl (struct vfs_class *me, const char *path, int ctlop, void *arg)
1071 switch (ctlop) {
1072 case VFS_SETCTL_STALE_DATA:
1074 struct vfs_s_inode *ino = vfs_s_inode_from_path (me, path, 0);
1076 if (!ino)
1077 return 0;
1078 if (arg)
1079 ino->super->want_stale = 1;
1080 else {
1081 ino->super->want_stale = 0;
1082 vfs_s_invalidate (me, ino->super);
1084 return 1;
1086 case VFS_SETCTL_LOGFILE:
1087 MEDATA->logfile = fopen ((char *) arg, "w");
1088 return 1;
1089 case VFS_SETCTL_FLUSH:
1090 MEDATA->flush = 1;
1091 return 1;
1093 return 0;
1097 /* ----------------------------- Stamping support -------------------------- */
1099 static vfsid
1100 vfs_s_getid (struct vfs_class *me, const char *path)
1102 struct vfs_s_super *archive;
1103 char *p;
1105 if (!(p = vfs_s_get_path (me, path, &archive, FL_NO_OPEN)))
1106 return NULL;
1107 g_free(p);
1108 return (vfsid) archive;
1111 static int
1112 vfs_s_nothingisopen (vfsid id)
1114 (void) id;
1115 /* Our data structures should survive free of superblock at any time */
1116 return 1;
1119 static void
1120 vfs_s_free (vfsid id)
1122 vfs_s_free_super (((struct vfs_s_super *)id)->me, (struct vfs_s_super *)id);
1125 static int
1126 vfs_s_dir_uptodate (struct vfs_class *me, struct vfs_s_inode *ino)
1128 struct timeval tim;
1130 if (MEDATA->flush) {
1131 MEDATA->flush = 0;
1132 return 0;
1135 gettimeofday(&tim, NULL);
1136 if (tim.tv_sec < ino->timestamp.tv_sec)
1137 return 1;
1138 return 0;
1141 /* Initialize one of our subclasses - fill common functions */
1142 void
1143 vfs_s_init_class (struct vfs_class *vclass, struct vfs_s_subclass *sub)
1145 vclass->data = sub;
1146 vclass->fill_names = vfs_s_fill_names;
1147 vclass->open = vfs_s_open;
1148 vclass->close = vfs_s_close;
1149 vclass->read = vfs_s_read;
1150 if (!(sub->flags & VFS_S_READONLY)) {
1151 vclass->write = vfs_s_write;
1153 vclass->opendir = vfs_s_opendir;
1154 vclass->readdir = vfs_s_readdir;
1155 vclass->closedir = vfs_s_closedir;
1156 vclass->stat = vfs_s_stat;
1157 vclass->lstat = vfs_s_lstat;
1158 vclass->fstat = vfs_s_fstat;
1159 vclass->readlink = vfs_s_readlink;
1160 vclass->chdir = vfs_s_chdir;
1161 vclass->ferrno = vfs_s_ferrno;
1162 vclass->lseek = vfs_s_lseek;
1163 vclass->getid = vfs_s_getid;
1164 vclass->nothingisopen = vfs_s_nothingisopen;
1165 vclass->free = vfs_s_free;
1166 if (sub->flags & VFS_S_REMOTE) {
1167 vclass->getlocalcopy = vfs_s_getlocalcopy;
1168 vclass->ungetlocalcopy = vfs_s_ungetlocalcopy;
1169 sub->find_entry = vfs_s_find_entry_linear;
1170 } else {
1171 sub->find_entry = vfs_s_find_entry_tree;
1173 vclass->setctl = vfs_s_setctl;
1174 sub->dir_uptodate = vfs_s_dir_uptodate;
1177 /* ----------- Utility functions for networked filesystems -------------- */
1179 #ifdef USE_NETCODE
1181 vfs_s_select_on_two (int fd1, int fd2)
1183 fd_set set;
1184 struct timeval timeout;
1185 int v;
1186 int maxfd = (fd1 > fd2 ? fd1 : fd2) + 1;
1188 timeout.tv_sec = 1;
1189 timeout.tv_usec = 0;
1190 FD_ZERO (&set);
1191 FD_SET (fd1, &set);
1192 FD_SET (fd2, &set);
1193 v = select (maxfd, &set, 0, 0, &timeout);
1194 if (v <= 0)
1195 return v;
1196 if (FD_ISSET (fd1, &set))
1197 return 1;
1198 if (FD_ISSET (fd2, &set))
1199 return 2;
1200 return -1;
1204 vfs_s_get_line (struct vfs_class *me, int sock, char *buf, int buf_len, char term)
1206 FILE *logfile = MEDATA->logfile;
1207 int i;
1208 char c;
1210 for (i = 0; i < buf_len - 1; i++, buf++){
1211 if (read (sock, buf, sizeof(char)) <= 0)
1212 return 0;
1213 if (logfile){
1214 fwrite (buf, 1, 1, logfile);
1215 fflush (logfile);
1217 if (*buf == term){
1218 *buf = 0;
1219 return 1;
1223 /* Line is too long - terminate buffer and discard the rest of line */
1224 *buf = 0;
1225 while (read (sock, &c, sizeof (c)) > 0) {
1226 if (logfile){
1227 fwrite (&c, 1, 1, logfile);
1228 fflush (logfile);
1230 if (c == '\n')
1231 return 1;
1233 return 0;
1237 vfs_s_get_line_interruptible (struct vfs_class *me, char *buffer, int size, int fd)
1239 int n;
1240 int i;
1242 (void) me;
1244 tty_enable_interrupt_key ();
1245 for (i = 0; i < size-1; i++){
1246 n = read (fd, buffer+i, 1);
1247 tty_disable_interrupt_key ();
1248 if (n == -1 && errno == EINTR){
1249 buffer [i] = 0;
1250 return EINTR;
1252 if (n == 0){
1253 buffer [i] = 0;
1254 return 0;
1256 if (buffer [i] == '\n'){
1257 buffer [i] = 0;
1258 return 1;
1261 buffer [size-1] = 0;
1262 return 0;
1264 #endif /* USE_NETCODE */