mc.sh & mc.csh creation fixed...
[midnight-commander.git] / vfs / sfs.c
blob8adb31fcbc2321cb0ed34012f80eaf0db885949a
1 /*
2 * Single File fileSystem
4 * Copyright 1998 Pavel Machek, distribute under GPL
6 * This defines whole class of filesystems which contain single file
7 * inside. It is somehow similar to extfs, except that extfs makes
8 * whole virtual trees and we do only single virtual files.
10 * If you want to gunzip something, you should open it with #ugz
11 * suffix, DON'T try to gunzip it yourself.
13 * Namespace: exports vfs_sfs_ops */
15 #include <config.h>
16 #include <errno.h>
17 #include <sys/types.h>
18 #include <unistd.h>
19 #include <stdio.h>
20 #include <string.h>
22 #include "utilvfs.h"
24 #include "vfs.h"
25 #include "local.h"
27 struct cachedfile {
28 char *name, *cache;
29 uid_t uid;
30 struct cachedfile *next;
33 static struct cachedfile *head;
35 #define MAXFS 32
36 static int sfs_no = 0;
37 static char *sfs_prefix[ MAXFS ];
38 static char *sfs_command[ MAXFS ];
39 static int sfs_flags[ MAXFS ];
40 #define F_1 1
41 #define F_2 2
42 #define F_NOLOCALCOPY 4
43 #define F_FULLMATCH 8
45 static int uptodate (char *name, char *cache)
47 return 1;
50 static int vfmake (vfs *me, char *name, char *cache)
52 char *inpath, *op;
53 int w;
54 char pad [10240];
55 char *s, *t = pad;
56 int was_percent = 0;
58 vfs_split (name, &inpath, &op);
59 if ((w = (*me->which) (me, op)) == -1)
60 vfs_die ("This cannot happen... Hopefully.\n");
62 if ((sfs_flags[w] & F_1) || (!strcmp (name, "/"))) ; else return -1;
63 /* if ((sfs_flags[w] & F_2) || (!inpath) || (!*inpath)); else return -1; */
64 if (!(sfs_flags[w] & F_NOLOCALCOPY)) {
65 s = mc_getlocalcopy (name);
66 if (!s)
67 return -1;
68 name = name_quote (s, 0);
69 g_free (s);
70 } else
71 name = name_quote (name, 0);
72 #define COPY_CHAR if (t-pad>sizeof(pad)) { g_free (name); return -1; } else *t++ = *s;
73 #define COPY_STRING(a) if ((t-pad)+strlen(a)>sizeof(pad)) { g_free (name); return -1; } else { strcpy (t, a); t+= strlen(a); }
74 for (s = sfs_command[w]; *s; s++) {
75 if (was_percent) {
77 char *ptr = NULL;
78 was_percent = 0;
80 switch (*s) {
81 case '1': ptr = name; break;
82 case '2': ptr = op + strlen (sfs_prefix[w]); break;
83 case '3': ptr = cache; break;
84 case '%': COPY_CHAR; continue;
86 COPY_STRING (ptr);
87 } else {
88 if (*s == '%')
89 was_percent = 1;
90 else
91 COPY_CHAR;
94 g_free (name);
96 open_error_pipe ();
97 if (my_system (EXECUTE_AS_SHELL, "/bin/sh", pad)) {
98 close_error_pipe (1, NULL);
99 return -1;
102 close_error_pipe (0, NULL);
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, NO_LINEAR(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;
263 g_free (cur->cache);
264 g_free (cur->name);
265 g_free (cur);
268 static void sfs_fill_names (vfs *me, void (*func)(char *))
270 struct cachedfile *cur = head;
272 while (cur){
273 (*func)(cur->name);
274 cur = cur->next;
278 static int sfs_nothingisopen (vfsid id)
280 /* FIXME: Investigate whether have to guard this like in
281 the other VFSs (see fd_usage in extfs) -- Norbert */
282 return 1;
285 static char *sfs_getlocalcopy (vfs *me, char *path)
287 path = redirect (me, path);
288 return g_strdup (path);
291 static int sfs_ungetlocalcopy (vfs *me, char *path, char *local, int has_changed)
293 g_free(local);
294 return 0;
297 static int sfs_init (vfs *me)
299 char *mc_sfsini;
300 FILE *cfg;
302 mc_sfsini = concat_dir_and_file (mc_home, "extfs" PATH_SEP_STR "sfs.ini");
303 cfg = fopen (mc_sfsini, "r");
305 if (!cfg){
306 fprintf (stderr, _("Warning: file %s not found\n"), mc_sfsini);
307 g_free (mc_sfsini);
308 return 0;
310 g_free (mc_sfsini);
312 sfs_no = 0;
313 while (sfs_no < MAXFS){
314 char key[256];
315 char *c, *semi = NULL, flags = 0;
317 if (!fgets (key, sizeof (key), cfg))
318 break;
320 if (*key == '#')
321 continue;
323 for (c = key; *c; c++)
324 if ((*c == ':') || (*c == '/')){
325 semi = c;
326 if (*c == '/'){
327 *c = 0;
328 flags |= F_FULLMATCH;
330 break;
333 if (!semi){
334 fprintf (stderr, _("Warning: Invalid line in %s:\n%s\n"),
335 "sfs.ini", key);
336 continue;
339 c = semi + 1;
340 while ((*c != ' ') && (*c != '\t')) {
341 switch (*c) {
342 case '1': flags |= F_1; break;
343 case '2': flags |= F_2; break;
344 case 'R': flags |= F_NOLOCALCOPY; break;
345 default:
346 fprintf (stderr, _("Warning: Invalid flag %c in %s:\n%s\n"),
347 *c, "sfs.ini", key);
349 c++;
351 c++;
352 *(semi+1) = 0;
353 if ((semi = strchr (c, '\n')))
354 *semi = 0;
356 sfs_prefix [sfs_no] = g_strdup (key);
357 sfs_command [sfs_no] = g_strdup (c);
358 sfs_flags [sfs_no] = flags;
359 sfs_no++;
361 fclose (cfg);
362 return 1;
365 static void
366 sfs_done (vfs *me)
368 int i;
370 for (i = 0; i < sfs_no; i++){
371 g_free (sfs_prefix [i]);
372 g_free (sfs_command [i]);
373 sfs_prefix [i] = sfs_command [i] = NULL;
375 sfs_no = 0;
378 static int
379 sfs_which (vfs *me, char *path)
381 int i;
383 for (i = 0; i < sfs_no; i++)
384 if (sfs_flags [i] & F_FULLMATCH) {
385 if (!strcmp (path, sfs_prefix [i]))
386 return i;
387 } else
388 if (!strncmp (path, sfs_prefix [i], strlen (sfs_prefix [i])))
389 return i;
391 return -1;
394 vfs vfs_sfs_ops = {
395 NULL, /* This is place of next pointer */
396 "sfs",
397 F_EXEC, /* flags */
398 NULL, /* prefix */
399 NULL, /* data */
400 0, /* errno */
401 sfs_init,
402 sfs_done,
403 sfs_fill_names,
404 sfs_which,
406 sfs_open,
407 local_close,
408 local_read,
409 NULL,
411 NULL,
412 NULL,
413 NULL,
414 NULL,
415 NULL,
417 sfs_stat,
418 sfs_lstat,
419 local_fstat,
421 sfs_chmod,
422 sfs_chown,
423 sfs_utime,
425 sfs_readlink,
426 NULL,
427 NULL,
428 NULL,
430 NULL,
431 NULL,
432 local_errno,
433 local_lseek,
434 NULL,
436 sfs_getid,
437 sfs_nothingisopen,
438 sfs_free,
440 sfs_getlocalcopy,
441 sfs_ungetlocalcopy,
443 NULL,
444 NULL,
445 NULL,
446 NULL
448 #ifdef HAVE_MMAP
449 ,local_mmap,
450 local_munmap
451 #endif