cppcheck: reduce variable scope.
[midnight-commander.git] / src / vfs / sfs / sfs.c
blob1947cc36cf4d5a5011cd5a8f41d4db5d656e1135
1 /*
2 Single File fileSystem
4 Copyright 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
5 2011, 2013
6 The Free Software Foundation, Inc.
8 Written by:
9 Slava Zanko <slavazanko@gmail.com>, 2013
11 This file is part of the Midnight Commander.
13 The Midnight Commander is free software: you can redistribute it
14 and/or modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation, either version 3 of the License,
16 or (at your option) any later version.
18 The Midnight Commander is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 /**
28 * \file
29 * \brief Source: Single File fileSystem
31 * This defines whole class of filesystems which contain single file
32 * inside. It is somehow similar to extfs, except that extfs makes
33 * whole virtual trees and we do only single virtual files.
35 * If you want to gunzip something, you should open it with \verbatim #ugz \endverbatim
36 * suffix, DON'T try to gunzip it yourself.
38 * Namespace: exports vfs_sfs_ops
41 #include <config.h>
42 #include <errno.h>
43 #include <sys/types.h>
44 #include <unistd.h>
45 #include <stdio.h>
46 #include <string.h>
47 #include <fcntl.h>
49 #include "lib/global.h"
50 #include "lib/util.h"
51 #include "lib/widget.h" /* D_ERROR, D_NORMAL */
53 #include "src/execute.h" /* EXECUTE_AS_SHELL */
55 #include "lib/vfs/vfs.h"
56 #include "lib/vfs/utilvfs.h"
57 #include "src/vfs/local/local.h"
58 #include "lib/vfs/gc.h" /* vfs_stamp_create */
60 #include "sfs.h"
62 /*** global variables ****************************************************************************/
64 /*** file scope macro definitions ****************************************************************/
66 #define MAXFS 32
68 #define F_1 1
69 #define F_2 2
70 #define F_NOLOCALCOPY 4
71 #define F_FULLMATCH 8
73 #define COPY_CHAR \
74 if ((size_t) (t - pad) > sizeof(pad)) \
75 { \
76 g_free (pqname); \
77 return -1; \
78 } \
79 else \
80 *t++ = *s_iter;
82 #define COPY_STRING(a) \
83 if ((t - pad) + strlen(a) > sizeof(pad)) \
84 { \
85 g_free (pqname); \
86 return -1; \
87 } \
88 else \
89 { \
90 strcpy (t, a); \
91 t += strlen (a); \
94 /*** file scope type declarations ****************************************************************/
96 typedef struct cachedfile
98 char *name;
99 char *cache;
100 } cachedfile;
102 /*** file scope variables ************************************************************************/
104 static GSList *head;
105 static struct vfs_class vfs_sfs_ops;
107 static int sfs_no = 0;
108 static char *sfs_prefix[MAXFS];
109 static char *sfs_command[MAXFS];
110 static int sfs_flags[MAXFS];
112 /*** file scope functions ************************************************************************/
114 static int
115 cachedfile_compare (const void *a, const void *b)
117 const cachedfile *cf = (const cachedfile *) a;
118 const char *name = (const char *) b;
120 return strcmp (name, cf->name);
123 /* --------------------------------------------------------------------------------------------- */
125 static int
126 sfs_vfmake (const vfs_path_t * vpath, vfs_path_t * cache_vpath)
128 int w;
129 char pad[10240];
130 char *s_iter, *t = pad;
131 int was_percent = 0;
132 vfs_path_t *pname; /* name of parent archive */
133 char *pqname; /* name of parent archive, quoted */
134 const vfs_path_element_t *path_element;
136 path_element = vfs_path_get_by_index (vpath, -1);
137 pname = vfs_path_clone (vpath);
138 vfs_path_remove_element_by_index (pname, -1);
140 w = (*path_element->class->which) (path_element->class, path_element->vfs_prefix);
141 if (w == -1)
142 vfs_die ("This cannot happen... Hopefully.\n");
144 if ((sfs_flags[w] & F_1) == 0 && strcmp (vfs_path_get_last_path_str (pname), PATH_SEP_STR) != 0)
146 vfs_path_free (pname);
147 return -1;
150 /* if ((sfs_flags[w] & F_2) || (!inpath) || (!*inpath)); else return -1; */
151 if ((sfs_flags[w] & F_NOLOCALCOPY) == 0)
153 vfs_path_t *s;
155 s = mc_getlocalcopy (pname);
156 if (s == NULL)
158 vfs_path_free (pname);
159 return -1;
161 pqname = name_quote (vfs_path_get_last_path_str (s), 0);
162 vfs_path_free (s);
164 else
165 pqname = name_quote (vfs_path_as_str (pname), 0);
167 vfs_path_free (pname);
170 for (s_iter = sfs_command[w]; *s_iter != '\0'; s_iter++)
172 if (was_percent)
174 const char *ptr = NULL;
176 was_percent = 0;
178 switch (*s_iter)
180 case '1':
181 ptr = pqname;
182 break;
183 case '2':
184 ptr = path_element->path;
185 break;
186 case '3':
187 ptr = vfs_path_get_by_index (cache_vpath, -1)->path;
188 break;
189 case '%':
190 COPY_CHAR;
191 continue;
193 if (ptr != NULL)
195 COPY_STRING (ptr);
198 else
200 if (*s_iter == '%')
201 was_percent = 1;
202 else
203 COPY_CHAR;
207 g_free (pqname);
208 open_error_pipe ();
209 if (my_system (EXECUTE_AS_SHELL, "/bin/sh", pad))
211 close_error_pipe (D_ERROR, NULL);
212 return -1;
215 close_error_pipe (D_NORMAL, NULL);
216 return 0; /* OK */
219 /* --------------------------------------------------------------------------------------------- */
221 static const char *
222 sfs_redirect (const vfs_path_t * vpath)
224 GSList *cur;
225 cachedfile *cf;
226 vfs_path_t *cache_vpath;
227 int handle;
228 const vfs_path_element_t *path_element;
230 path_element = vfs_path_get_by_index (vpath, -1);
231 cur = g_slist_find_custom (head, vfs_path_as_str (vpath), cachedfile_compare);
233 if (cur != NULL)
235 cf = (cachedfile *) cur->data;
236 vfs_stamp (&vfs_sfs_ops, cf);
237 return cf->cache;
240 handle = vfs_mkstemps (&cache_vpath, "sfs", path_element->path);
242 if (handle == -1)
243 return "/SOMEONE_PLAYING_DIRTY_TMP_TRICKS_ON_US";
245 close (handle);
247 if (sfs_vfmake (vpath, cache_vpath) == 0)
249 cf = g_new (cachedfile, 1);
250 cf->name = g_strdup (vfs_path_as_str (vpath));
251 cf->cache = g_strdup (vfs_path_as_str (cache_vpath));
252 head = g_slist_prepend (head, cf);
253 vfs_path_free (cache_vpath);
255 vfs_stamp_create (&vfs_sfs_ops, (cachedfile *) head->data);
256 return cf->cache;
259 mc_unlink (cache_vpath);
260 vfs_path_free (cache_vpath);
261 return "/I_MUST_NOT_EXIST";
264 /* --------------------------------------------------------------------------------------------- */
266 static void *
267 sfs_open (const vfs_path_t * vpath /*struct vfs_class *me, const char *path */ , int flags,
268 mode_t mode)
270 int *sfs_info;
271 int fd;
273 fd = open (sfs_redirect (vpath), NO_LINEAR (flags), mode);
274 if (fd == -1)
275 return 0;
277 sfs_info = g_new (int, 1);
278 *sfs_info = fd;
280 return sfs_info;
283 /* --------------------------------------------------------------------------------------------- */
285 static int
286 sfs_stat (const vfs_path_t * vpath, struct stat *buf)
288 return stat (sfs_redirect (vpath), buf);
291 /* --------------------------------------------------------------------------------------------- */
293 static int
294 sfs_lstat (const vfs_path_t * vpath, struct stat *buf)
296 #ifndef HAVE_STATLSTAT
297 return lstat (sfs_redirect (vpath), buf);
298 #else
299 return statlstat (sfs_redirect (vpath), buf);
300 #endif
303 /* --------------------------------------------------------------------------------------------- */
305 static int
306 sfs_chmod (const vfs_path_t * vpath, mode_t mode)
308 return chmod (sfs_redirect (vpath), mode);
311 /* --------------------------------------------------------------------------------------------- */
313 static int
314 sfs_chown (const vfs_path_t * vpath, uid_t owner, gid_t group)
316 return chown (sfs_redirect (vpath), owner, group);
319 /* --------------------------------------------------------------------------------------------- */
321 static int
322 sfs_utime (const vfs_path_t * vpath, struct utimbuf *times)
324 return utime (sfs_redirect (vpath), times);
327 /* --------------------------------------------------------------------------------------------- */
329 static int
330 sfs_readlink (const vfs_path_t * vpath, char *buf, size_t size)
332 return readlink (sfs_redirect (vpath), buf, size);
335 /* --------------------------------------------------------------------------------------------- */
337 static vfsid
338 sfs_getid (const vfs_path_t * vpath)
340 GSList *cur;
342 cur = g_slist_find_custom (head, vfs_path_as_str (vpath), cachedfile_compare);
344 return (vfsid) (cur != NULL ? cur->data : NULL);
347 /* --------------------------------------------------------------------------------------------- */
349 static void
350 sfs_free (vfsid id)
352 struct cachedfile *which;
353 GSList *cur;
355 which = (struct cachedfile *) id;
356 cur = g_slist_find (head, which);
357 if (cur == NULL)
358 vfs_die ("Free of thing which is unknown to me\n");
360 which = (struct cachedfile *) cur->data;
361 unlink (which->cache);
362 g_free (which->cache);
363 g_free (which->name);
364 g_free (which);
366 head = g_slist_delete_link (head, cur);
369 /* --------------------------------------------------------------------------------------------- */
371 static void
372 sfs_fill_names (struct vfs_class *me, fill_names_f func)
374 GSList *cur;
376 (void) me;
378 for (cur = head; cur != NULL; cur = g_slist_next (cur))
379 func (((cachedfile *) cur->data)->name);
382 /* --------------------------------------------------------------------------------------------- */
384 static int
385 sfs_nothingisopen (vfsid id)
387 /* FIXME: Investigate whether have to guard this like in
388 the other VFSs (see fd_usage in extfs) -- Norbert */
389 (void) id;
390 return 1;
393 /* --------------------------------------------------------------------------------------------- */
395 static vfs_path_t *
396 sfs_getlocalcopy (const vfs_path_t * vpath)
398 return vfs_path_from_str (sfs_redirect (vpath));
401 /* --------------------------------------------------------------------------------------------- */
403 static int
404 sfs_ungetlocalcopy (const vfs_path_t * vpath, const vfs_path_t * local, gboolean has_changed)
406 (void) vpath;
407 (void) local;
408 (void) has_changed;
409 return 0;
412 /* --------------------------------------------------------------------------------------------- */
414 static int
415 sfs_init (struct vfs_class *me)
417 char *mc_sfsini;
418 FILE *cfg;
419 char key[256];
421 (void) me;
423 mc_sfsini = g_build_filename (mc_global.sysconfig_dir, "sfs.ini", (char *) NULL);
424 cfg = fopen (mc_sfsini, "r");
426 if (cfg == NULL)
428 fprintf (stderr, _("%s: Warning: file %s not found\n"), "sfs_init()", mc_sfsini);
429 g_free (mc_sfsini);
430 return 0;
432 g_free (mc_sfsini);
434 sfs_no = 0;
435 while (sfs_no < MAXFS && fgets (key, sizeof (key), cfg))
437 char *c, *semi = NULL, flags = 0;
439 if (*key == '#' || *key == '\n')
440 continue;
442 for (c = key; *c; c++)
443 if ((*c == ':') || (*c == '/'))
445 semi = c;
446 if (*c == '/')
448 *c = 0;
449 flags |= F_FULLMATCH;
451 break;
454 if (!semi)
456 invalid_line:
457 fprintf (stderr, _("Warning: Invalid line in %s:\n%s\n"), "sfs.ini", key);
458 continue;
461 c = semi + 1;
462 while (*c && (*c != ' ') && (*c != '\t'))
464 switch (*c)
466 case '1':
467 flags |= F_1;
468 break;
469 case '2':
470 flags |= F_2;
471 break;
472 case 'R':
473 flags |= F_NOLOCALCOPY;
474 break;
475 default:
476 fprintf (stderr, _("Warning: Invalid flag %c in %s:\n%s\n"), *c, "sfs.ini", key);
478 c++;
480 if (!*c)
481 goto invalid_line;
483 c++;
484 *(semi + 1) = 0;
485 semi = strchr (c, '\n');
486 if (semi != NULL)
487 *semi = 0;
489 sfs_prefix[sfs_no] = g_strdup (key);
490 sfs_command[sfs_no] = g_strdup (c);
491 sfs_flags[sfs_no] = flags;
492 sfs_no++;
494 fclose (cfg);
495 return 1;
498 /* --------------------------------------------------------------------------------------------- */
500 static void
501 sfs_done (struct vfs_class *me)
503 int i;
505 (void) me;
507 for (i = 0; i < sfs_no; i++)
509 g_free (sfs_prefix[i]);
510 g_free (sfs_command[i]);
511 sfs_prefix[i] = sfs_command[i] = NULL;
513 sfs_no = 0;
516 /* --------------------------------------------------------------------------------------------- */
518 static int
519 sfs_which (struct vfs_class *me, const char *path)
521 int i;
523 (void) me;
525 for (i = 0; i < sfs_no; i++)
526 if (sfs_flags[i] & F_FULLMATCH)
528 if (!strcmp (path, sfs_prefix[i]))
529 return i;
531 else if (!strncmp (path, sfs_prefix[i], strlen (sfs_prefix[i])))
532 return i;
534 return -1;
537 /* --------------------------------------------------------------------------------------------- */
538 /*** public functions ****************************************************************************/
539 /* --------------------------------------------------------------------------------------------- */
541 void
542 init_sfs (void)
544 vfs_sfs_ops.name = "sfs";
545 vfs_sfs_ops.init = sfs_init;
546 vfs_sfs_ops.done = sfs_done;
547 vfs_sfs_ops.fill_names = sfs_fill_names;
548 vfs_sfs_ops.which = sfs_which;
549 vfs_sfs_ops.open = sfs_open;
550 vfs_sfs_ops.close = local_close;
551 vfs_sfs_ops.read = local_read;
552 vfs_sfs_ops.stat = sfs_stat;
553 vfs_sfs_ops.lstat = sfs_lstat;
554 vfs_sfs_ops.fstat = local_fstat;
555 vfs_sfs_ops.chmod = sfs_chmod;
556 vfs_sfs_ops.chown = sfs_chown;
557 vfs_sfs_ops.utime = sfs_utime;
558 vfs_sfs_ops.readlink = sfs_readlink;
559 vfs_sfs_ops.ferrno = local_errno;
560 vfs_sfs_ops.lseek = local_lseek;
561 vfs_sfs_ops.getid = sfs_getid;
562 vfs_sfs_ops.nothingisopen = sfs_nothingisopen;
563 vfs_sfs_ops.free = sfs_free;
564 vfs_sfs_ops.getlocalcopy = sfs_getlocalcopy;
565 vfs_sfs_ops.ungetlocalcopy = sfs_ungetlocalcopy;
566 vfs_register_class (&vfs_sfs_ops);
569 /* --------------------------------------------------------------------------------------------- */