* README.smbfs: Remove. It contained no useful information
[midnight-commander.git] / vfs / sfs.c
blobb603b5582e492ec1f6c8bdfbcb48c83685d6f91a
1 /*
2 * Single File fileSystem
4 * Copyright 1998 Pavel Machek, distribute under GPL
6 * $Id$
8 * This defines whole class of filesystems which contain single file
9 * inside. It is somehow similar to extfs, except that extfs makes
10 * whole virtual trees and we do only single virtual files.
12 * If you want to gunzip something, you should open it with #ugz
13 * suffix, DON'T try to gunzip it yourself.
15 * Namespace: exports vfs_sfs_ops */
17 #include <config.h>
18 #include <errno.h>
19 #include <sys/types.h>
20 #include <unistd.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <fcntl.h>
25 #include "utilvfs.h"
27 #include "vfs.h"
28 #include "local.h"
30 struct cachedfile {
31 char *name, *cache;
32 uid_t uid;
33 struct cachedfile *next;
36 static struct cachedfile *head;
38 #define MAXFS 32
39 static int sfs_no = 0;
40 static char *sfs_prefix[ MAXFS ];
41 static char *sfs_command[ MAXFS ];
42 static int sfs_flags[ MAXFS ];
43 #define F_1 1
44 #define F_2 2
45 #define F_NOLOCALCOPY 4
46 #define F_FULLMATCH 8
48 static int uptodate (char *name, char *cache)
50 return 1;
53 static int vfmake (vfs *me, char *name, char *cache)
55 char *inpath, *op;
56 int w;
57 char pad [10240];
58 char *s, *t = pad;
59 int was_percent = 0;
61 vfs_split (name, &inpath, &op);
62 if ((w = (*me->which) (me, op)) == -1)
63 vfs_die ("This cannot happen... Hopefully.\n");
65 if ((sfs_flags[w] & F_1) || (!strcmp (name, "/"))) ; else return -1;
66 /* if ((sfs_flags[w] & F_2) || (!inpath) || (!*inpath)); else return -1; */
67 if (!(sfs_flags[w] & F_NOLOCALCOPY)) {
68 s = mc_getlocalcopy (name);
69 if (!s)
70 return -1;
71 name = name_quote (s, 0);
72 g_free (s);
73 } else
74 name = name_quote (name, 0);
75 #define COPY_CHAR if (t-pad>sizeof(pad)) { g_free (name); return -1; } else *t++ = *s;
76 #define COPY_STRING(a) if ((t-pad)+strlen(a)>sizeof(pad)) { g_free (name); return -1; } else { strcpy (t, a); t+= strlen(a); }
77 for (s = sfs_command[w]; *s; s++) {
78 if (was_percent) {
80 char *ptr = NULL;
81 was_percent = 0;
83 switch (*s) {
84 case '1': ptr = name; break;
85 case '2': ptr = op + strlen (sfs_prefix[w]); break;
86 case '3': ptr = cache; break;
87 case '%': COPY_CHAR; continue;
89 COPY_STRING (ptr);
90 } else {
91 if (*s == '%')
92 was_percent = 1;
93 else
94 COPY_CHAR;
97 g_free (name);
99 if (my_system (EXECUTE_AS_SHELL | EXECUTE_SETUID | EXECUTE_WAIT, "/bin/sh", pad)) {
100 return -1;
103 return 0; /* OK */
106 static char *
107 redirect (vfs *me, char *name)
109 struct cachedfile *cur = head;
110 uid_t uid = vfs_uid;
111 char *cache;
112 int handle;
114 while (cur){
115 if ((!strcmp (name, cur->name)) &&
116 (uid == cur->uid) &&
117 (uptodate (cur->name, cur->cache)))
118 /* FIXME: when not uptodate, we might want to kill cache
119 * file immediately, not to wait until timeout. */ {
120 vfs_stamp (&vfs_sfs_ops, cur);
121 return cur->cache;
123 cur = cur->next;
126 handle = mc_mkstemps (&cache, "sfs", NULL);
128 if (handle == -1) {
129 return "/SOMEONE_PLAYING_DIRTY_TMP_TRICKS_ON_US";
132 close (handle);
134 if (!vfmake (me, name, cache)){
135 cur = g_new (struct cachedfile, 1);
136 cur->name = g_strdup (name);
137 cur->cache = cache;
138 cur->uid = uid;
139 cur->next = head;
140 head = cur;
142 vfs_add_noncurrent_stamps (&vfs_sfs_ops, (vfsid) head, NULL);
143 vfs_rm_parents (NULL);
145 return cache;
148 unlink (cache);
149 g_free (cache);
150 return "/I_MUST_NOT_EXIST";
153 static void *
154 sfs_open (vfs *me, char *path, int flags, int mode)
156 int *sfs_info;
157 int fd;
159 path = redirect (me, path);
160 fd = open (path, flags, mode);
161 if (fd == -1)
162 return 0;
164 sfs_info = g_new (int, 1);
165 *sfs_info = fd;
167 return sfs_info;
170 static int sfs_stat (vfs *me, char *path, struct stat *buf)
172 path = redirect (me, path);
173 return stat (path, buf);
176 static int sfs_lstat (vfs *me, char *path, struct stat *buf)
178 path = redirect (me, path);
179 #ifndef HAVE_STATLSTAT
180 return lstat (path, buf);
181 #else
182 return statlstat (path, buf);
183 #endif
186 static int sfs_chmod (vfs *me, char *path, int mode)
188 path = redirect (me, path);
189 return chmod (path, mode);
192 static int sfs_chown (vfs *me, char *path, int owner, int group)
194 path = redirect (me, path);
195 return chown (path, owner, group);
198 static int sfs_utime (vfs *me, char *path, struct utimbuf *times)
200 path = redirect (me, path);
201 return utime (path, times);
204 static int sfs_readlink (vfs *me, char *path, char *buf, int size)
206 path = redirect (me, path);
207 return readlink (path, buf, size);
210 static vfsid sfs_getid (vfs *me, char *path, struct vfs_stamping **parent)
211 { /* FIXME: what should I do? */
212 vfs *v;
213 vfsid id;
214 struct vfs_stamping *par;
215 struct cachedfile *cur = head;
217 while (cur) {
218 if ((!strcmp( path, cur->name )) &&
219 (vfs_uid == cur->uid))
220 break;
221 cur = cur->next;
224 *parent = NULL;
226 if (!cur)
227 return (vfsid)(-1);
230 char *path2 = g_strdup (path);
231 v = vfs_split (path2, NULL, NULL); /* Strip suffix which led to this being sfs */
232 v = vfs_split (path2, NULL, NULL); /* ... and learn whoever was the parent system */
233 id = (*v->getid) (v, path2, &par);
234 g_free (path2);
237 if (id != (vfsid)-1) {
238 *parent = g_new (struct vfs_stamping, 1);
239 (*parent)->v = v;
240 (*parent)->id = id;
241 (*parent)->parent = par;
242 (*parent)->next = NULL;
244 return (vfsid) cur;
247 static void sfs_free (vfsid id)
249 struct cachedfile *which = (struct cachedfile *) id;
250 struct cachedfile *cur, *prev;
252 for (cur = head, prev = 0; cur && cur != which; prev = cur, cur = cur->next)
254 if (!cur)
255 vfs_die( "Free of thing which is unknown to me\n" );
256 unlink (cur->cache);
258 if (prev)
259 prev->next = cur->next;
260 else
261 head = cur->next;
264 static void sfs_fill_names (vfs *me, void (*func)(char *))
266 struct cachedfile *cur = head;
268 while (cur){
269 (*func)(cur->name);
270 cur = cur->next;
274 static int sfs_nothingisopen (vfsid id)
276 /* FIXME: Investigate whether have to guard this like in
277 the other VFSs (see fd_usage in extfs) -- Norbert */
278 return 1;
281 static char *sfs_getlocalcopy (vfs *me, char *path)
283 path = redirect (me, path);
284 return g_strdup (path);
287 static int sfs_ungetlocalcopy (vfs *me, char *path, char *local, int has_changed)
289 g_free(local);
290 return 0;
293 static int sfs_init (vfs *me)
295 char *mc_sfsini;
296 FILE *cfg;
298 mc_sfsini = concat_dir_and_file (mc_home, "extfs/sfs.ini");
299 cfg = fopen (mc_sfsini, "r");
301 if (!cfg){
302 fprintf (stderr, _("Warning: file %s not found\n"), mc_sfsini);
303 g_free (mc_sfsini);
304 return 0;
306 g_free (mc_sfsini);
308 sfs_no = 0;
309 while (sfs_no < MAXFS){
310 char key[256];
311 char *c, *semi = NULL, flags = 0;
313 if (!fgets (key, sizeof (key), cfg))
314 break;
316 if (*key == '#')
317 continue;
319 for (c = key; *c; c++)
320 if ((*c == ':') || (*c == '/')){
321 semi = c;
322 if (*c == '/'){
323 *c = 0;
324 flags |= F_FULLMATCH;
326 break;
329 if (!semi){
330 fprintf (stderr, _("Warning: Invalid line in sfs.ini:\n%s\n"), key);
331 continue;
334 c = semi + 1;
335 while ((*c != ' ') && (*c != '\t')) {
336 switch (*c) {
337 case '1': flags |= F_1; break;
338 case '2': flags |= F_2; break;
339 case 'R': flags |= F_NOLOCALCOPY; break;
340 default:
341 fprintf (stderr, _("Warning: Invalid flag %c in sfs.ini:\n%s\n"), *c, key);
343 c++;
345 c++;
346 *(semi+1) = 0;
347 if ((semi = strchr (c, '\n')))
348 *semi = 0;
350 sfs_prefix [sfs_no] = g_strdup (key);
351 sfs_command [sfs_no] = g_strdup (c);
352 sfs_flags [sfs_no] = flags;
353 sfs_no++;
355 fclose (cfg);
356 return 1;
359 static void
360 sfs_done (vfs *me)
362 int i;
364 for (i = 0; i < sfs_no; i++){
365 g_free (sfs_prefix [i]);
366 g_free (sfs_command [i]);
367 sfs_prefix [i] = sfs_command [i] = NULL;
369 sfs_no = 0;
372 static int
373 sfs_which (vfs *me, char *path)
375 int i;
377 for (i = 0; i < sfs_no; i++)
378 if (sfs_flags [i] & F_FULLMATCH) {
379 if (!strcmp (path, sfs_prefix [i]))
380 return i;
381 } else
382 if (!strncmp (path, sfs_prefix [i], strlen (sfs_prefix [i])))
383 return i;
385 return -1;
388 vfs vfs_sfs_ops = {
389 NULL, /* This is place of next pointer */
390 "sfs",
391 F_EXEC, /* flags */
392 NULL, /* prefix */
393 NULL, /* data */
394 0, /* errno */
395 sfs_init,
396 sfs_done,
397 sfs_fill_names,
398 sfs_which,
400 sfs_open,
401 local_close,
402 local_read,
403 NULL,
405 NULL,
406 NULL,
407 NULL,
408 NULL,
409 NULL,
411 sfs_stat,
412 sfs_lstat,
413 local_fstat,
415 sfs_chmod,
416 sfs_chown,
417 sfs_utime,
419 sfs_readlink,
420 NULL,
421 NULL,
422 NULL,
424 NULL,
425 NULL,
426 local_errno,
427 local_lseek,
428 NULL,
430 sfs_getid,
431 sfs_nothingisopen,
432 sfs_free,
434 sfs_getlocalcopy,
435 sfs_ungetlocalcopy,
437 NULL,
438 NULL,
439 NULL,
440 NULL
442 #ifdef HAVE_MMAP
443 ,local_mmap,
444 local_munmap
445 #endif