* POTFILES.in: add gnome/gcustom-layout.c file
[midnight-commander.git] / vfs / direntry.c
blob8ac044e5812049404d78704c499a6dd5995f24ef
1 /* Directory cache support -- so that you do not have copy of this in
2 * each and every filesystem.
4 * Written at 1998 by Pavel Machek <pavel@ucw.cz>, distribute under LGPL.
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 * Paths here do _not_ begin with '/', so root directory of
25 * archive/site is simply "". Beware. */
27 static volatile int total_inodes = 0, total_entries = 0;
29 #include "utilvfs.h"
30 #include "xdirentry.h"
31 #include "../src/tty.h"
33 #define CALL(x) if (MEDATA->x) MEDATA->x
35 vfs_s_inode *
36 vfs_s_new_inode (vfs *me, vfs_s_super *super, struct stat *initstat)
38 vfs_s_inode *ino;
40 ino = g_new (vfs_s_inode, 1);
41 if (!ino)
42 return NULL;
45 ino->linkname = ino->localname = NULL;
46 ino->subdir = NULL;
47 if (initstat)
48 ino->st = *initstat;
49 ino->super = super;
50 ino->ent = NULL;
51 ino->flags = 0;
52 ino->st.st_nlink = 0;
53 ino->st.st_ino = MEDATA->inode_counter++;
54 ino->st.st_dev = MEDATA->rdev;
56 super->ino_usage++;
57 total_inodes++;
59 CALL (init_inode) (me, ino);
61 return ino;
64 vfs_s_entry *
65 vfs_s_new_entry (vfs *me, char *name, vfs_s_inode *inode)
67 vfs_s_entry *entry;
69 entry = g_new (struct vfs_s_entry, 1);
70 total_entries++;
72 if (name)
73 entry->name = g_strdup (name);
74 else
75 entry->name = NULL;
76 entry->dir = NULL;
77 entry->next = NULL;
78 entry->prevp = NULL;
79 entry->ino = inode;
80 entry->ino->ent = entry;
81 CALL (init_entry) (me, entry);
83 return entry;
86 void
87 vfs_s_free_inode (vfs *me, vfs_s_inode *ino)
89 if (!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){
94 while (ino->subdir){
95 vfs_s_entry *ent;
96 ent = ino->subdir;
97 vfs_s_free_entry (me, ent);
100 CALL (free_inode) (me, ino);
101 ifree (ino->linkname);
102 if (ino->localname){
103 unlink (ino->localname);
104 g_free(ino->localname);
106 total_inodes--;
107 ino->super->ino_usage--;
108 g_free(ino);
109 } else ino->st.st_nlink--;
112 void
113 vfs_s_free_entry (vfs *me, vfs_s_entry *ent)
115 int is_dot = 0;
116 if (ent->prevp){ /* It is possible that we are deleting freshly created entry */
117 *ent->prevp = ent->next;
118 if (ent->next)
119 ent->next->prevp = ent->prevp;
122 if (ent->name){
123 is_dot = (!strcmp (ent->name, ".")) || (!strcmp (ent->name, ".."));
124 g_free (ent->name);
125 ent->name = NULL;
128 if (!is_dot && ent->ino){
129 ent->ino->ent = NULL;
130 vfs_s_free_inode (me, ent->ino);
131 ent->ino = NULL;
134 total_entries--;
135 g_free(ent);
138 void vfs_s_insert_entry (vfs *me, vfs_s_inode *dir, vfs_s_entry *ent)
140 vfs_s_entry **ep;
142 for (ep = &dir->subdir; *ep != NULL; ep = &((*ep)->next))
144 ent->prevp = ep;
145 ent->next = NULL;
146 ent->dir = dir;
147 *ep = ent;
149 ent->ino->st.st_nlink++;
152 struct stat *vfs_s_default_stat (vfs *me, mode_t mode)
154 static struct stat st;
155 int myumask;
157 myumask = umask (022);
158 umask (myumask);
159 mode &= ~myumask;
161 st.st_mode = mode;
162 st.st_ino = 0;
163 st.st_dev = 0;
164 st.st_rdev = 0;
165 st.st_uid = getuid ();
166 st.st_gid = getgid ();
167 st.st_size = 0;
168 st.st_mtime = st.st_atime = st.st_ctime = time (NULL);
170 return &st;
173 void
174 vfs_s_add_dots (vfs *me, vfs_s_inode *dir, vfs_s_inode *parent)
176 struct vfs_s_entry *dot, *dotdot;
178 if (!parent)
179 parent = dir;
180 dot = vfs_s_new_entry (me, ".", dir);
181 dotdot = vfs_s_new_entry (me, "..", parent);
182 vfs_s_insert_entry (me, dir, dot);
183 vfs_s_insert_entry (me, dir, dotdot);
184 dir->st.st_nlink--;
185 parent->st.st_nlink--; /* We do not count "." and ".." into nlinks */
188 struct vfs_s_entry *
189 vfs_s_generate_entry (vfs *me, char *name, struct vfs_s_inode *parent, mode_t mode)
191 struct vfs_s_inode *inode;
192 struct vfs_s_entry *entry;
193 struct stat *st;
195 st = vfs_s_default_stat (me, mode);
196 inode = vfs_s_new_inode (me, parent->super, st);
197 if (S_ISDIR (mode))
198 vfs_s_add_dots (me, inode, parent);
200 entry = vfs_s_new_entry (me, name, inode);
202 return entry;
205 /* We were asked to create entries automagically */
206 vfs_s_entry *
207 vfs_s_automake (vfs *me, vfs_s_inode *dir, char *path, int flags)
209 struct vfs_s_entry *res;
210 char *sep = strchr (path, PATH_SEP);
212 if (sep)
213 *sep = 0;
214 res = vfs_s_generate_entry (me, path, dir, flags & FL_MKDIR ? (0777 | S_IFDIR) : 0777);
215 vfs_s_insert_entry (me, dir, res);
217 if (sep)
218 *sep = PATH_SEP;
220 return res;
224 * Follow > 0: follow links, serves as loop protect,
225 * == -1: do not follow links
227 vfs_s_entry *
228 vfs_s_find_entry_tree (vfs *me, vfs_s_inode *root, char *path, int follow, int flags)
230 unsigned int pseg;
231 vfs_s_entry *ent = NULL;
232 int found;
233 char p[MC_MAXPATHLEN] = "";
235 while (1){
236 int t;
238 for (pseg = 0; path [pseg] == PATH_SEP; pseg++)
240 if (!path [pseg])
241 return ent;
242 path += pseg;
244 for (pseg = 0; path[pseg] && path[pseg] != PATH_SEP; pseg++)
247 strcat (p, PATH_SEP_STR);
248 strncpy (p + (t = strlen (p)), path, pseg);
249 p[t + pseg] = '\0';
251 found = 0;
252 for (ent = root->subdir; ent != NULL; ent = ent->next)
253 if (strlen (ent->name) == pseg && (!strncmp (ent->name, path, pseg)))
254 /* FOUND! */
255 break;
257 if (!ent && (flags & (FL_MKFILE | FL_MKDIR)))
258 ent = vfs_s_automake (me, root, path, flags);
259 if (!ent) ERRNOR (ENOENT, NULL);
260 path += pseg;
261 /* here we must follow leading directories always; only the actual file is optional */
262 if (!(ent = vfs_s_resolve_symlink (me, ent, p, strchr (path, PATH_SEP) ? LINK_FOLLOW : follow)))
263 return NULL;
264 root = ent->ino;
268 static void
269 split_dir_name (vfs *me, char *path, char **dir, char **name, char **save)
271 char *s;
272 s = strrchr (path, PATH_SEP);
273 if (!s){
274 *save = NULL;
275 *name = path;
276 *dir = "";
277 } else {
278 *save = s;
279 *dir = path;
280 *s++ = 0;
281 if (!*s) /* This can happen if someone does stat("/"); */
282 *name = "";
283 else
284 *name = s;
288 vfs_s_entry *
289 vfs_s_find_entry_linear (vfs *me, vfs_s_inode *root, char *path, int follow, int flags)
291 vfs_s_entry* ent = NULL;
293 if (!(flags & FL_DIR)){
294 char *dirname, *name, *save;
295 vfs_s_inode *ino;
296 vfs_s_entry *ent;
297 split_dir_name (me, path, &dirname, &name, &save);
298 ino = vfs_s_find_inode (me, root, dirname, follow, flags | FL_DIR);
299 if (save)
300 *save = PATH_SEP;
301 ent = vfs_s_find_entry_tree (me, ino, name, follow, flags);
302 return ent;
305 for (ent = root->subdir; ent != NULL; ent = ent->next)
306 if (!strcmp (ent->name, path))
307 break;
309 if (ent && (! (MEDATA->dir_uptodate) (me, ent->ino))){
310 #if 1
311 message_1s (1, "Dir cache expired for", path);
312 #endif
313 vfs_s_free_entry (me, ent);
316 if (!ent){
317 vfs_s_inode *ino;
319 ino = vfs_s_new_inode (me, root->super, vfs_s_default_stat (me, S_IFDIR | 0755));
320 ent = vfs_s_new_entry (me, path, ino);
321 if ((MEDATA->dir_load) (me, ino, path) == -1){
322 vfs_s_free_entry (me, ent);
323 return NULL;
325 vfs_s_insert_entry (me, root, ent);
327 for (ent = root->subdir; ent != NULL; ent = ent->next)
328 if (!strcmp (ent->name, path))
329 break;
331 if (!ent)
332 vfs_die ("find_linear: success but directory is not there\n");
334 #if 0
335 if (!vfs_s_resolve_symlink (me, ent, follow)) return NULL;
336 #endif
337 return ent;
340 vfs_s_inode *
341 vfs_s_find_inode (vfs *me, vfs_s_inode *root, char *path, int follow, int flags)
343 vfs_s_entry *ent;
344 if ((MEDATA->find_entry == vfs_s_find_entry_tree) && (!*path))
345 return root;
346 ent = (MEDATA->find_entry)(me, root, path, follow, flags);
347 if (!ent)
348 return NULL;
349 return ent->ino;
352 vfs_s_entry *
353 vfs_s_resolve_symlink (vfs *me, vfs_s_entry *entry, char *path, int follow)
355 if (follow == LINK_NO_FOLLOW)
356 return entry;
357 if (follow == 0)
358 ERRNOR (ELOOP, NULL);
359 if (!entry)
360 ERRNOR (ENOENT, NULL);
361 if (!S_ISLNK (entry->ino->st.st_mode))
362 return entry;
364 if (*entry->ino->linkname != PATH_SEP)
365 return (MEDATA->find_entry) (me, entry->dir, entry->ino->linkname, follow - 1, 0);
366 else {
367 /* convert the linkname to relative linkname with some leading ../ */
368 char linkname[MC_MAXPATHLEN] = "", *p, *q;
369 for (p = path, q = entry->ino->linkname; *p == *q; p++, q++);
370 while (*(--q) != PATH_SEP);
371 q++;
372 for (;; p++){
373 p = strchr (p, PATH_SEP);
374 if (!p){
375 strcat (linkname, q);
376 break;
378 strcat (linkname, "..");
379 strcat (linkname, PATH_SEP_STR);
381 return (MEDATA->find_entry) (me, entry->dir, linkname, follow - 1, 0);
385 /* Ook, these were functions around direcory entries / inodes */
386 /* -------------------------------- superblock games -------------------------- */
388 vfs_s_super *
389 vfs_s_new_super (vfs *me)
391 vfs_s_super *super;
393 super = g_new0 (struct vfs_s_super, 1);
394 super->root = NULL;
395 super->name = NULL;
396 super->fd_usage = 0;
397 super->ino_usage = 0;
398 super->me = me;
399 return super;
402 void
403 vfs_s_insert_super (vfs *me, vfs_s_super *super)
405 super->next = MEDATA->supers;
406 super->prevp = &MEDATA->supers;
408 if (MEDATA->supers != NULL)
409 MEDATA->supers->prevp = &super->next;
410 MEDATA->supers = super;
413 void
414 vfs_s_free_super (vfs *me, vfs_s_super *super)
416 if (super->root){
417 vfs_s_free_inode (me, super->root);
418 super->root = NULL;
421 #if 1
422 /* We currently leak small ammount of memory, sometimes. Fix it if you can. */
423 if (super->ino_usage)
424 message_1s1d (1, " Direntry warning ", "Super ino_usage is %d, memory leak", super->ino_usage);
426 if (super->want_stale)
427 message_1s (1, " Direntry warning ", "Super has want_stale set");
428 #endif
430 if (super->prevp){
431 *super->prevp = super->next;
432 if (super->next)
433 super->next->prevp = super->prevp;
436 CALL (free_archive) (me, super);
437 ifree (super->name);
438 super->name = NULL;
439 g_free(super);
442 /* ------------------------------------------------------------------------= */
444 static void
445 vfs_s_stamp_me (vfs *me, struct vfs_s_super *psup, char *fs_name)
447 struct vfs_stamping *parent;
448 vfs *v;
450 v = vfs_type (fs_name);
451 if (v == &vfs_local_ops){
452 parent = NULL;
453 } else {
454 parent = g_new (struct vfs_stamping, 1);
455 parent->v = v;
456 parent->next = 0;
457 parent->id = (*v->getid) (v, fs_name, &(parent->parent));
459 vfs_add_noncurrent_stamps (&vfs_tarfs_ops, (vfsid) psup, parent);
460 vfs_rm_parents (parent);
463 char *
464 vfs_s_get_path_mangle (vfs *me, char *inname, struct vfs_s_super **archive, int flags)
466 char *local, *op, *archive_name;
467 int result = -1;
468 struct vfs_s_super *super;
469 void *cookie;
471 archive_name = inname;
472 vfs_split (inname, &local, &op);
473 if (!local)
474 local = "";
476 if (MEDATA->archive_check)
477 if (! (cookie = MEDATA->archive_check (me, archive_name, op)))
478 return NULL;
480 for (super = MEDATA->supers; super != NULL; super = super->next){
481 int i; /* 0 == other, 1 == same, return it, 2 == other but stop scanning */
482 if ((i = MEDATA->archive_same (me, super, archive_name, op, cookie))){
483 if (i==1) goto return_success;
484 else break;
488 if (flags & FL_NO_OPEN)
489 ERRNOR (EIO, NULL);
491 super = vfs_s_new_super (me);
492 result = MEDATA->open_archive (me, super, archive_name, op);
493 if (result == -1){
494 vfs_s_free_super (me, super);
495 ERRNOR (EIO, NULL);
497 if (!super->name)
498 vfs_die ("You have to fill name\n");
499 if (!super->root)
500 vfs_die ("You have to fill root inode\n");
502 vfs_s_insert_super (me, super);
503 vfs_s_stamp_me (me, super, archive_name);
505 return_success:
506 *archive = super;
507 return local;
510 char *
511 vfs_s_get_path (vfs *me, char *inname, struct vfs_s_super **archive, int flags)
513 char *buf = g_strdup( inname );
514 char *res = vfs_s_get_path_mangle (me, buf, archive, flags);
515 char *res2 = NULL;
516 if (res)
517 res2 = g_strdup(res);
518 g_free(buf);
519 return res2;
522 void
523 vfs_s_invalidate (vfs *me, vfs_s_super *super)
525 if (!super->want_stale){
526 vfs_s_free_inode (me, super->root);
527 super->root = vfs_s_new_inode (me, super, vfs_s_default_stat (me, S_IFDIR | 0755));
531 char *
532 vfs_s_fullpath (vfs *me, vfs_s_inode *ino)
534 /* For now, usable only on filesystems with _linear structure */
535 if (MEDATA->find_entry != vfs_s_find_entry_linear)
536 vfs_die ("Implement me!");
537 if ((!ino->ent) || (!ino->ent->dir) || (!ino->ent->dir->ent))
538 ERRNOR (EAGAIN, NULL);
539 return g_strconcat (ino->ent->dir->ent->name, PATH_SEP_STR,
540 ino->ent->name, NULL);
543 /* Support of archives */
544 /* ------------------------ readdir & friends ----------------------------- */
546 vfs_s_super *vfs_s_super_from_path (vfs *me, char *name)
548 struct vfs_s_super *super;
550 if (!vfs_s_get_path_mangle (me, name, &super, 0))
551 return NULL;
552 return super;
555 vfs_s_inode *
556 vfs_s_inode_from_path (vfs *me, char *name, int flags)
558 struct vfs_s_super *super;
559 struct vfs_s_inode *ino;
560 char *q;
562 if (!(q = vfs_s_get_path_mangle (me, name, &super, 0)))
563 return NULL;
565 ino = vfs_s_find_inode (me, super->root, q, flags & FL_FOLLOW ? LINK_FOLLOW : LINK_NO_FOLLOW, flags & ~FL_FOLLOW);
566 if (ino) return ino;
568 return ino;
572 struct dirhandle {
573 vfs_s_entry *cur;
574 vfs_s_inode *dir;
577 void *
578 vfs_s_opendir (vfs *me, char *dirname)
580 struct vfs_s_inode *dir;
581 struct dirhandle *info;
583 dir = vfs_s_inode_from_path (me, dirname, FL_DIR | FL_FOLLOW);
584 if (!dir)
585 return NULL;
586 if (!S_ISDIR (dir->st.st_mode))
587 ERRNOR (ENOTDIR, NULL);
589 dir->st.st_nlink++;
590 #if 0
591 if (!dir->subdir) /* This can actually happen if we allow empty directories */
592 ERRNOR (EAGAIN, NULL);
593 #endif
594 info = g_new (struct dirhandle, 1);
595 info->cur = dir->subdir;
596 info->dir = dir;
598 return info;
601 void *
602 vfs_s_readdir (void *data)
604 static struct {
605 struct dirent dir;
606 #ifdef NEED_EXTRA_DIRENT_BUFFER
607 char extra_buffer [MC_MAXPATHLEN];
608 #endif
609 } dir;
611 struct dirhandle *info = (struct dirhandle *) data;
613 if (!(info->cur))
614 return NULL;
616 if (info->cur->name)
617 strcpy (&(dir.dir.d_name [0]), info->cur->name);
618 else
619 vfs_die ("Null in structure-can not happen");
621 #ifndef DIRENT_LENGTH_COMPUTED
622 dir.d_namlen = strlen (dir.dir.d_name);
623 #endif
624 info->cur = info->cur->next;
626 return (void *)&dir;
630 vfs_s_telldir (void *data)
632 struct dirhandle *info = (struct dirhandle *) data;
633 struct vfs_s_entry *cur;
634 int num = 0;
636 cur = info->dir->subdir;
637 while (cur!=NULL){
638 if (cur == info->cur)
639 return num;
640 num++;
641 cur = cur->next;
643 return -1;
646 void
647 vfs_s_seekdir (void *data, int offset)
649 struct dirhandle *info = (struct dirhandle *) data;
650 int i;
651 info->cur = info->dir->subdir;
652 for (i=0; i<offset; i++)
653 vfs_s_readdir (data);
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;
668 vfs_s_chdir (vfs *me, 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 (vfs *me, 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;
691 vfs_s_stat (vfs *me, char *path, struct stat *buf)
693 return vfs_s_internal_stat (me, path, buf, FL_FOLLOW);
697 vfs_s_lstat (vfs *me, char *path, struct stat *buf)
699 return vfs_s_internal_stat (me, path, buf, FL_NONE);
703 vfs_s_fstat (void *fh, struct stat *buf)
705 *buf = FH->ino->st;
706 return 0;
710 vfs_s_readlink (vfs *me, char *path, char *buf, int size)
712 struct vfs_s_inode *ino;
714 ino = vfs_s_inode_from_path (me, path, 0);
715 if (!ino)
716 return -1;
718 if (!S_ISLNK (ino->st.st_mode))
719 ERRNOR (EINVAL, -1);
720 strncpy (buf, ino->linkname, size);
721 *(buf+size-1) = 0;
722 return strlen (buf);
725 void *
726 vfs_s_open (vfs *me, char *file, int flags, int mode)
728 int was_changed = 0;
729 struct vfs_s_fh *fh;
730 vfs_s_super *super;
731 char *q;
732 struct vfs_s_inode *ino;
734 if ((q = vfs_s_get_path_mangle (me, file, &super, 0)) == NULL)
735 return NULL;
736 ino = vfs_s_find_inode (me, super->root, q, LINK_FOLLOW, FL_NONE);
737 if (ino && ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL)))
738 ERRNOR (EEXIST, NULL);
739 if (!ino){
740 char *dirname, *name, *save;
741 vfs_s_entry *ent;
742 vfs_s_inode *dir;
743 if (!(flags & O_CREAT))
744 return NULL;
746 split_dir_name (me, q, &dirname, &name, &save);
747 /* FIXME: if vfs_s_find_inode returns NULL, this will do rather bad
748 things. */
749 dir = vfs_s_find_inode (me, super->root, dirname, LINK_FOLLOW, FL_DIR);
750 if (save)
751 *save = PATH_SEP;
752 ent = vfs_s_generate_entry (me, name, dir, 0755);
753 ino = ent->ino;
754 vfs_s_insert_entry (me, dir, ent);
755 ino->localname = tempnam (NULL, me->name);
756 was_changed = 1;
759 if (S_ISDIR (ino->st.st_mode))
760 ERRNOR (EISDIR, NULL);
762 fh = g_new (struct vfs_s_fh, 1);
763 fh->pos = 0;
764 fh->ino = ino;
765 fh->handle = -1;
766 fh->changed = was_changed;
767 fh->linear = 0;
768 if (MEDATA->fh_open)
769 if (MEDATA->fh_open (me, fh, flags, mode)){
770 g_free(fh);
771 return NULL;
774 if (fh->ino->localname){
775 fh->handle = open (fh->ino->localname, flags, mode);
776 if (fh->handle == -1){
777 g_free(fh);
778 ERRNOR (errno, NULL);
782 /* i.e. we had no open files and now we have one */
783 vfs_rmstamp (&vfs_tarfs_ops, (vfsid) super, 1);
784 super->fd_usage++;
785 fh->ino->st.st_nlink++;
786 return fh;
790 vfs_s_read (void *fh, char *buffer, int count)
792 int n;
793 vfs *me = FH_SUPER->me;
795 if (FH->linear == LS_LINEAR_CLOSED){
796 print_vfs_message ("Starting linear transfer...");
797 if (!MEDATA->linear_start (me, FH, 0))
798 return -1;
801 if (FH->linear == LS_LINEAR_CLOSED)
802 vfs_die ("linear_start() did not set linear_state!");
804 if (FH->linear == LS_LINEAR_OPEN)
805 return MEDATA->linear_read (me, FH, buffer, count);
807 if (FH->handle){
808 n = read (FH->handle, buffer, count);
809 if (n < 0)
810 me->verrno = errno;
811 return n;
813 vfs_die ("vfs_s_read: This should not happen\n");
814 return -1;
818 vfs_s_write (void *fh, char *buffer, int count)
820 int n;
821 vfs *me = FH_SUPER->me;
823 if (FH->linear)
824 vfs_die ("no writing to linear files, please");
826 FH->changed = 1;
827 if (FH->handle){
828 n = write (FH->handle, buffer, count);
829 if (n < 0)
830 me->verrno = errno;
831 return n;
833 vfs_die ("vfs_s_write: This should not happen\n");
834 return 0;
838 vfs_s_lseek (void *fh, off_t offset, int whence)
840 off_t size = FH->ino->st.st_size;
842 if (FH->handle != -1){ /* If we have local file opened, we want to work with it */
843 int retval = lseek (FH->handle, offset, whence);
844 if (retval == -1)
845 FH->ino->super->me->verrno = errno;
846 return retval;
849 switch (whence){
850 case SEEK_CUR:
851 offset += FH->pos; break;
852 case SEEK_END:
853 offset += size; break;
855 if (offset < 0)
856 FH->pos = 0;
857 else if (offset < size)
858 FH->pos = offset;
859 else
860 FH->pos = size;
861 return FH->pos;
865 vfs_s_close (void *fh)
867 int res = 0;
868 vfs *me = FH_SUPER->me;
870 FH_SUPER->fd_usage--;
871 if (!FH_SUPER->fd_usage){
872 struct vfs_stamping *parent;
873 vfs *v;
875 v = vfs_type (FH_SUPER->name);
876 if (v == &vfs_local_ops){
877 parent = NULL;
878 } else {
879 parent = g_new (struct vfs_stamping, 1);
880 parent->v = v;
881 parent->next = 0;
882 parent->id = (*v->getid) (v, FH_SUPER->name, &(parent->parent));
884 vfs_add_noncurrent_stamps (&vfs_tarfs_ops, (vfsid) (FH_SUPER), parent);
885 vfs_rm_parents (parent);
887 if (FH->linear == LS_LINEAR_OPEN)
888 MEDATA->linear_close (me, fh);
889 if (MEDATA->fh_close)
890 res = MEDATA->fh_close (me, fh);
891 if (FH->changed && MEDATA->file_store){
892 char *s = vfs_s_fullpath (me, FH->ino);
893 if (!s)
894 res = -1;
895 else
896 res = MEDATA->file_store (me, FH_SUPER, s, FH->ino->localname);
897 vfs_s_invalidate (me, FH_SUPER);
899 if (FH->handle)
900 close (FH->handle);
902 vfs_s_free_inode (me, FH->ino);
903 g_free (fh);
904 return res;
907 /* ------------------------------- mc support ---------------------------- */
909 void
910 vfs_s_fill_names (vfs *me, void (*func)(char *))
912 struct vfs_s_super *a = MEDATA->supers;
913 char *name;
915 while (a){
916 name = g_strconcat ( a->name, "#", me->prefix, "/", /* a->current_dir->name, */ NULL);
917 (*func)(name);
918 g_free (name);
919 a = a->next;
924 vfs_s_ferrno (vfs *me)
926 return me->verrno;
929 void
930 vfs_s_dump (vfs *me, char *prefix, vfs_s_inode *ino)
932 printf ("%s %s %d ", prefix, S_ISDIR (ino->st.st_mode) ? "DIR" : "FILE", ino->st.st_mode);
933 if (!ino->subdir)
934 printf ("FILE\n");
936 else
938 struct vfs_s_entry *ent;
939 ent = ino->subdir;
940 while (ent){
941 char *s;
942 s = g_strconcat (prefix, "/", ent->name, NULL);
943 if (ent->name[0] == '.')
944 printf ("%s IGNORED\n", s);
945 else
946 vfs_s_dump (me, s, ent->ino);
947 g_free(s);
948 ent = ent->next;
953 char *
954 vfs_s_getlocalcopy (vfs *me, char *path)
956 struct vfs_s_inode *ino;
957 char buf[MC_MAXPATHLEN];
959 strcpy (buf, path);
960 ino = vfs_s_inode_from_path (me, path, FL_FOLLOW | FL_NONE);
962 if (!ino->localname)
963 ino->localname = mc_def_getlocalcopy (me, buf);
964 /* FIXME: fd_usage++ missing */
965 return ino->localname;
968 int
969 vfs_s_setctl (vfs *me, char *path, int ctlop, char *arg)
971 vfs_s_inode *ino = vfs_s_inode_from_path (me, path, 0);
972 if (!ino)
973 return 0;
974 switch (ctlop){
975 case MCCTL_WANT_STALE_DATA:
976 ino->super->want_stale = 1;
977 return 1;
978 case MCCTL_NO_STALE_DATA:
979 ino->super->want_stale = 0;
980 vfs_s_invalidate(me, ino->super);
981 return 1;
982 #if 0 /* FIXME: We should implement these */
983 case MCCTL_REMOVELOCALCOPY:
984 return remove_temp_file (path);
985 case MCCTL_FORGET_ABOUT:
986 my_forget(path);
987 return 0;
988 #endif
990 return 0;
994 /* ----------------------------- Stamping support -------------------------- */
996 vfsid
997 vfs_s_getid (vfs *me, char *path, struct vfs_stamping **parent)
999 vfs_s_super *archive;
1000 vfs *v;
1001 char *p;
1002 vfsid id;
1003 struct vfs_stamping *par;
1005 *parent = NULL;
1006 if (!(p = vfs_s_get_path (me, path, &archive, FL_NO_OPEN)))
1007 return (vfsid) -1;
1008 g_free(p);
1009 v = vfs_type (archive->name);
1010 id = (*v->getid) (v, archive->name, &par);
1011 if (id != (vfsid)-1){
1012 *parent = g_new (struct vfs_stamping, 1);
1013 (*parent)->v = v;
1014 (*parent)->id = id;
1015 (*parent)->parent = par;
1016 (*parent)->next = NULL;
1018 return (vfsid) archive;
1022 vfs_s_nothingisopen (vfsid id)
1024 if (((vfs_s_super *)id)->fd_usage <= 0)
1025 return 1;
1026 else
1027 return 0;
1030 void
1031 vfs_s_free (vfsid id)
1033 vfs_s_free_super (((vfs_s_super *)id)->me, (vfs_s_super *)id);
1036 /* ----------- Utility functions for networked filesystems -------------- */
1039 vfs_s_select_on_two (int fd1, int fd2)
1041 fd_set set;
1042 struct timeval timeout;
1043 int v;
1044 int maxfd = (fd1 > fd2 ? fd1 : fd2) + 1;
1046 timeout.tv_sec = 1;
1047 timeout.tv_usec = 0;
1048 FD_ZERO (&set);
1049 FD_SET (fd1, &set);
1050 FD_SET (fd2, &set);
1051 v = select (maxfd, &set, 0, 0, &timeout);
1052 if (v <= 0)
1053 return v;
1054 if (FD_ISSET (fd1, &set))
1055 return 1;
1056 if (FD_ISSET (fd2, &set))
1057 return 2;
1058 return -1;
1062 vfs_s_get_line (vfs *me, int sock, char *buf, int buf_len, char term)
1064 FILE *logfile = MEDATA->logfile;
1065 int i, status;
1066 char c;
1068 for (i = 0; i < buf_len; i++, buf++){
1069 if (read (sock, buf, sizeof(char)) <= 0)
1070 return 0;
1071 if (logfile){
1072 fwrite (buf, 1, 1, logfile);
1073 fflush (logfile);
1075 if (*buf == term){
1076 *buf = 0;
1077 return 1;
1080 *buf = 0;
1081 while ((status = read (sock, &c, sizeof (c))) > 0){
1082 if (logfile){
1083 fwrite (&c, 1, 1, logfile);
1084 fflush (logfile);
1086 if (c == '\n')
1087 return 1;
1089 return 0;
1093 vfs_s_get_line_interruptible (vfs *me, char *buffer, int size, int fd)
1095 int n;
1096 int i = 0;
1098 enable_interrupt_key ();
1099 for (i = 0; i < size-1; i++){
1100 n = read (fd, buffer+i, 1);
1101 disable_interrupt_key ();
1102 if (n == -1 && errno == EINTR){
1103 buffer [i] = 0;
1104 return EINTR;
1106 if (n == 0){
1107 buffer [i] = 0;
1108 return 0;
1110 if (buffer [i] == '\n'){
1111 buffer [i] = 0;
1112 return 1;
1115 buffer [size-1] = 0;
1116 return 0;