* syntax/syntax.syntax: Remove highlighting for numbers - it's
[midnight-commander.git] / vfs / sfs.c
bloba9385178afc8259ca73dcff3bdd319c5ec6aac0a
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 if (my_system (EXECUTE_AS_SHELL, "/bin/sh", pad)) {
97 return -1;
100 return 0; /* OK */
103 static char *
104 redirect (vfs *me, char *name)
106 struct cachedfile *cur = head;
107 uid_t uid = vfs_uid;
108 char *cache;
109 int handle;
111 while (cur){
112 if ((!strcmp (name, cur->name)) &&
113 (uid == cur->uid) &&
114 (uptodate (cur->name, cur->cache)))
115 /* FIXME: when not uptodate, we might want to kill cache
116 * file immediately, not to wait until timeout. */ {
117 vfs_stamp (&vfs_sfs_ops, cur);
118 return cur->cache;
120 cur = cur->next;
123 handle = mc_mkstemps (&cache, "sfs", NULL);
125 if (handle == -1) {
126 return "/SOMEONE_PLAYING_DIRTY_TMP_TRICKS_ON_US";
129 close (handle);
131 if (!vfmake (me, name, cache)){
132 cur = g_new (struct cachedfile, 1);
133 cur->name = g_strdup (name);
134 cur->cache = cache;
135 cur->uid = uid;
136 cur->next = head;
137 head = cur;
139 vfs_add_noncurrent_stamps (&vfs_sfs_ops, (vfsid) head, NULL);
140 vfs_rm_parents (NULL);
142 return cache;
145 unlink (cache);
146 g_free (cache);
147 return "/I_MUST_NOT_EXIST";
150 static void *
151 sfs_open (vfs *me, char *path, int flags, int mode)
153 int *sfs_info;
154 int fd;
156 path = redirect (me, path);
157 fd = open (path, NO_LINEAR(flags), mode);
158 if (fd == -1)
159 return 0;
161 sfs_info = g_new (int, 1);
162 *sfs_info = fd;
164 return sfs_info;
167 static int sfs_stat (vfs *me, char *path, struct stat *buf)
169 path = redirect (me, path);
170 return stat (path, buf);
173 static int sfs_lstat (vfs *me, char *path, struct stat *buf)
175 path = redirect (me, path);
176 #ifndef HAVE_STATLSTAT
177 return lstat (path, buf);
178 #else
179 return statlstat (path, buf);
180 #endif
183 static int sfs_chmod (vfs *me, char *path, int mode)
185 path = redirect (me, path);
186 return chmod (path, mode);
189 static int sfs_chown (vfs *me, char *path, int owner, int group)
191 path = redirect (me, path);
192 return chown (path, owner, group);
195 static int sfs_utime (vfs *me, char *path, struct utimbuf *times)
197 path = redirect (me, path);
198 return utime (path, times);
201 static int sfs_readlink (vfs *me, char *path, char *buf, int size)
203 path = redirect (me, path);
204 return readlink (path, buf, size);
207 static vfsid sfs_getid (vfs *me, char *path, struct vfs_stamping **parent)
208 { /* FIXME: what should I do? */
209 vfs *v;
210 vfsid id;
211 struct vfs_stamping *par;
212 struct cachedfile *cur = head;
214 while (cur) {
215 if ((!strcmp( path, cur->name )) &&
216 (vfs_uid == cur->uid))
217 break;
218 cur = cur->next;
221 *parent = NULL;
223 if (!cur)
224 return (vfsid)(-1);
227 char *path2 = g_strdup (path);
228 v = vfs_split (path2, NULL, NULL); /* Strip suffix which led to this being sfs */
229 v = vfs_split (path2, NULL, NULL); /* ... and learn whoever was the parent system */
230 id = (*v->getid) (v, path2, &par);
231 g_free (path2);
234 if (id != (vfsid)-1) {
235 *parent = g_new (struct vfs_stamping, 1);
236 (*parent)->v = v;
237 (*parent)->id = id;
238 (*parent)->parent = par;
239 (*parent)->next = NULL;
241 return (vfsid) cur;
244 static void sfs_free (vfsid id)
246 struct cachedfile *which = (struct cachedfile *) id;
247 struct cachedfile *cur, *prev;
249 for (cur = head, prev = 0; cur && cur != which; prev = cur, cur = cur->next)
251 if (!cur)
252 vfs_die( "Free of thing which is unknown to me\n" );
253 unlink (cur->cache);
255 if (prev)
256 prev->next = cur->next;
257 else
258 head = cur->next;
260 g_free (cur->cache);
261 g_free (cur->name);
262 g_free (cur);
265 static void sfs_fill_names (vfs *me, void (*func)(char *))
267 struct cachedfile *cur = head;
269 while (cur){
270 (*func)(cur->name);
271 cur = cur->next;
275 static int sfs_nothingisopen (vfsid id)
277 /* FIXME: Investigate whether have to guard this like in
278 the other VFSs (see fd_usage in extfs) -- Norbert */
279 return 1;
282 static char *sfs_getlocalcopy (vfs *me, char *path)
284 path = redirect (me, path);
285 return g_strdup (path);
288 static int sfs_ungetlocalcopy (vfs *me, char *path, char *local, int has_changed)
290 g_free(local);
291 return 0;
294 static int sfs_init (vfs *me)
296 char *mc_sfsini;
297 FILE *cfg;
299 mc_sfsini = concat_dir_and_file (mc_home, "extfs" PATH_SEP_STR "sfs.ini");
300 cfg = fopen (mc_sfsini, "r");
302 if (!cfg){
303 fprintf (stderr, _("Warning: file %s not found\n"), mc_sfsini);
304 g_free (mc_sfsini);
305 return 0;
307 g_free (mc_sfsini);
309 sfs_no = 0;
310 while (sfs_no < MAXFS){
311 char key[256];
312 char *c, *semi = NULL, flags = 0;
314 if (!fgets (key, sizeof (key), cfg))
315 break;
317 if (*key == '#')
318 continue;
320 for (c = key; *c; c++)
321 if ((*c == ':') || (*c == '/')){
322 semi = c;
323 if (*c == '/'){
324 *c = 0;
325 flags |= F_FULLMATCH;
327 break;
330 if (!semi){
331 fprintf (stderr, _("Warning: Invalid line in %s:\n%s\n"),
332 "sfs.ini", key);
333 continue;
336 c = semi + 1;
337 while ((*c != ' ') && (*c != '\t')) {
338 switch (*c) {
339 case '1': flags |= F_1; break;
340 case '2': flags |= F_2; break;
341 case 'R': flags |= F_NOLOCALCOPY; break;
342 default:
343 fprintf (stderr, _("Warning: Invalid flag %c in %s:\n%s\n"),
344 *c, "sfs.ini", key);
346 c++;
348 c++;
349 *(semi+1) = 0;
350 if ((semi = strchr (c, '\n')))
351 *semi = 0;
353 sfs_prefix [sfs_no] = g_strdup (key);
354 sfs_command [sfs_no] = g_strdup (c);
355 sfs_flags [sfs_no] = flags;
356 sfs_no++;
358 fclose (cfg);
359 return 1;
362 static void
363 sfs_done (vfs *me)
365 int i;
367 for (i = 0; i < sfs_no; i++){
368 g_free (sfs_prefix [i]);
369 g_free (sfs_command [i]);
370 sfs_prefix [i] = sfs_command [i] = NULL;
372 sfs_no = 0;
375 static int
376 sfs_which (vfs *me, char *path)
378 int i;
380 for (i = 0; i < sfs_no; i++)
381 if (sfs_flags [i] & F_FULLMATCH) {
382 if (!strcmp (path, sfs_prefix [i]))
383 return i;
384 } else
385 if (!strncmp (path, sfs_prefix [i], strlen (sfs_prefix [i])))
386 return i;
388 return -1;
391 vfs vfs_sfs_ops = {
392 NULL, /* This is place of next pointer */
393 "sfs",
394 F_EXEC, /* flags */
395 NULL, /* prefix */
396 NULL, /* data */
397 0, /* errno */
398 sfs_init,
399 sfs_done,
400 sfs_fill_names,
401 sfs_which,
403 sfs_open,
404 local_close,
405 local_read,
406 NULL,
408 NULL,
409 NULL,
410 NULL,
411 NULL,
412 NULL,
414 sfs_stat,
415 sfs_lstat,
416 local_fstat,
418 sfs_chmod,
419 sfs_chown,
420 sfs_utime,
422 sfs_readlink,
423 NULL,
424 NULL,
425 NULL,
427 NULL,
428 NULL,
429 local_errno,
430 local_lseek,
431 NULL,
433 sfs_getid,
434 sfs_nothingisopen,
435 sfs_free,
437 sfs_getlocalcopy,
438 sfs_ungetlocalcopy,
440 NULL,
441 NULL,
442 NULL,
443 NULL
445 #ifdef HAVE_MMAP
446 ,local_mmap,
447 local_munmap
448 #endif