2 * Single File fileSystem
4 * Copyright 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
5 * Free Software Foundation, Inc.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 * \brief Source: Single File fileSystem
26 * This defines whole class of filesystems which contain single file
27 * inside. It is somehow similar to extfs, except that extfs makes
28 * whole virtual trees and we do only single virtual files.
30 * If you want to gunzip something, you should open it with \verbatim #ugz \endverbatim
31 * suffix, DON'T try to gunzip it yourself.
33 * Namespace: exports vfs_sfs_ops
38 #include <sys/types.h>
44 #include "lib/global.h"
46 #include "lib/widget.h" /* D_ERROR, D_NORMAL */
48 #include "src/execute.h" /* EXECUTE_AS_SHELL */
50 #include "lib/vfs/vfs.h"
51 #include "lib/vfs/utilvfs.h"
52 #include "src/vfs/local/local.h"
53 #include "lib/vfs/gc.h" /* vfs_stamp_create */
57 /*** global variables ****************************************************************************/
59 /*** file scope macro definitions ****************************************************************/
65 #define F_NOLOCALCOPY 4
69 if ((size_t) (t - pad) > sizeof(pad)) \
77 #define COPY_STRING(a) \
78 if ((t - pad) + strlen(a) > sizeof(pad)) \
89 /*** file scope type declarations ****************************************************************/
91 typedef struct cachedfile
97 /*** file scope variables ************************************************************************/
100 static struct vfs_class vfs_sfs_ops
;
102 static int sfs_no
= 0;
103 static char *sfs_prefix
[MAXFS
];
104 static char *sfs_command
[MAXFS
];
105 static int sfs_flags
[MAXFS
];
107 /*** file scope functions ************************************************************************/
110 cachedfile_compare (const void *a
, const void *b
)
112 const cachedfile
*cf
= (const cachedfile
*) a
;
113 const char *name
= (const char *) b
;
115 return strcmp (name
, cf
->name
);
118 /* --------------------------------------------------------------------------------------------- */
121 sfs_vfmake (const vfs_path_t
* vpath
, char *cache
)
127 char *pname
; /* name of parent archive */
128 char *pqname
; /* name of parent archive, quoted */
129 vfs_path_element_t
*path_element
;
131 path_element
= vfs_path_get_by_index (vpath
, -1);
132 pname
= vfs_path_to_str_elements_count (vpath
, -1);
133 w
= (*path_element
->class->which
) (path_element
->class, path_element
->vfs_prefix
);
135 vfs_die ("This cannot happen... Hopefully.\n");
137 if (!(sfs_flags
[w
] & F_1
) && strcmp (pname
, "/"))
143 /* if ((sfs_flags[w] & F_2) || (!inpath) || (!*inpath)); else return -1; */
144 if (!(sfs_flags
[w
] & F_NOLOCALCOPY
))
146 s
= mc_getlocalcopy (pname
);
152 pqname
= name_quote (s
, 0);
157 pqname
= name_quote (pname
, 0);
162 for (s
= sfs_command
[w
]; *s
; s
++)
167 const char *ptr
= NULL
;
176 ptr
= path_element
->path
;
198 if (my_system (EXECUTE_AS_SHELL
, "/bin/sh", pad
))
200 close_error_pipe (D_ERROR
, NULL
);
204 close_error_pipe (D_NORMAL
, NULL
);
208 /* --------------------------------------------------------------------------------------------- */
211 sfs_redirect (const vfs_path_t
* vpath
)
217 vfs_path_element_t
*path_element
;
218 char *path
= vfs_path_to_str (vpath
);
220 path_element
= vfs_path_get_by_index (vpath
, -1);
221 cur
= g_slist_find_custom (head
, path
, cachedfile_compare
);
226 cf
= (cachedfile
*) cur
->data
;
227 vfs_stamp (&vfs_sfs_ops
, cf
);
231 handle
= vfs_mkstemps (&cache
, "sfs", path_element
->path
);
234 return "/SOMEONE_PLAYING_DIRTY_TMP_TRICKS_ON_US";
238 if (sfs_vfmake (vpath
, cache
) == 0)
240 cf
= g_new (cachedfile
, 1);
241 cf
->name
= vfs_path_to_str (vpath
);
243 head
= g_slist_prepend (head
, cf
);
245 vfs_stamp_create (&vfs_sfs_ops
, (cachedfile
*) head
->data
);
251 return "/I_MUST_NOT_EXIST";
254 /* --------------------------------------------------------------------------------------------- */
257 sfs_open (const vfs_path_t
* vpath
/*struct vfs_class *me, const char *path */ , int flags
,
263 fd
= open (sfs_redirect (vpath
), NO_LINEAR (flags
), mode
);
267 sfs_info
= g_new (int, 1);
273 /* --------------------------------------------------------------------------------------------- */
276 sfs_stat (const vfs_path_t
* vpath
, struct stat
*buf
)
278 return stat (sfs_redirect (vpath
), buf
);
281 /* --------------------------------------------------------------------------------------------- */
284 sfs_lstat (const vfs_path_t
* vpath
, struct stat
*buf
)
286 #ifndef HAVE_STATLSTAT
287 return lstat (sfs_redirect (vpath
), buf
);
289 return statlstat (sfs_redirect (vpath
), buf
);
293 /* --------------------------------------------------------------------------------------------- */
296 sfs_chmod (const vfs_path_t
* vpath
, mode_t mode
)
298 return chmod (sfs_redirect (vpath
), mode
);
301 /* --------------------------------------------------------------------------------------------- */
304 sfs_chown (const vfs_path_t
* vpath
, uid_t owner
, gid_t group
)
306 return chown (sfs_redirect (vpath
), owner
, group
);
309 /* --------------------------------------------------------------------------------------------- */
312 sfs_utime (const vfs_path_t
* vpath
, struct utimbuf
*times
)
314 return utime (sfs_redirect (vpath
), times
);
317 /* --------------------------------------------------------------------------------------------- */
320 sfs_readlink (const vfs_path_t
* vpath
, char *buf
, size_t size
)
322 return readlink (sfs_redirect (vpath
), buf
, size
);
325 /* --------------------------------------------------------------------------------------------- */
328 sfs_getid (const vfs_path_t
* vpath
)
331 char *path
= vfs_path_to_str (vpath
);
333 cur
= g_slist_find_custom (head
, path
, cachedfile_compare
);
336 return (vfsid
) (cur
!= NULL
? cur
->data
: NULL
);
339 /* --------------------------------------------------------------------------------------------- */
344 struct cachedfile
*which
;
347 which
= (struct cachedfile
*) id
;
348 cur
= g_slist_find (head
, which
);
350 vfs_die ("Free of thing which is unknown to me\n");
352 which
= (struct cachedfile
*) cur
->data
;
353 unlink (which
->cache
);
354 g_free (which
->cache
);
355 g_free (which
->name
);
358 head
= g_slist_delete_link (head
, cur
);
361 /* --------------------------------------------------------------------------------------------- */
364 sfs_fill_names (struct vfs_class
*me
, fill_names_f func
)
370 for (cur
= head
; cur
!= NULL
; cur
= g_slist_next (cur
))
371 func (((cachedfile
*) cur
->data
)->name
);
374 /* --------------------------------------------------------------------------------------------- */
377 sfs_nothingisopen (vfsid id
)
379 /* FIXME: Investigate whether have to guard this like in
380 the other VFSs (see fd_usage in extfs) -- Norbert */
385 /* --------------------------------------------------------------------------------------------- */
388 sfs_getlocalcopy (const vfs_path_t
* vpath
)
390 return g_strdup (sfs_redirect (vpath
));
393 /* --------------------------------------------------------------------------------------------- */
396 sfs_ungetlocalcopy (const vfs_path_t
* vpath
, const char *local
, int has_changed
)
404 /* --------------------------------------------------------------------------------------------- */
407 sfs_init (struct vfs_class
*me
)
415 mc_sfsini
= g_build_filename (mc_global
.sysconfig_dir
, "sfs.ini", (char *) NULL
);
416 cfg
= fopen (mc_sfsini
, "r");
420 fprintf (stderr
, _("%s: Warning: file %s not found\n"), "sfs_init()", mc_sfsini
);
427 while (sfs_no
< MAXFS
&& fgets (key
, sizeof (key
), cfg
))
429 char *c
, *semi
= NULL
, flags
= 0;
431 if (*key
== '#' || *key
== '\n')
434 for (c
= key
; *c
; c
++)
435 if ((*c
== ':') || (*c
== '/'))
441 flags
|= F_FULLMATCH
;
449 fprintf (stderr
, _("Warning: Invalid line in %s:\n%s\n"), "sfs.ini", key
);
454 while (*c
&& (*c
!= ' ') && (*c
!= '\t'))
465 flags
|= F_NOLOCALCOPY
;
468 fprintf (stderr
, _("Warning: Invalid flag %c in %s:\n%s\n"), *c
, "sfs.ini", key
);
477 semi
= strchr (c
, '\n');
481 sfs_prefix
[sfs_no
] = g_strdup (key
);
482 sfs_command
[sfs_no
] = g_strdup (c
);
483 sfs_flags
[sfs_no
] = flags
;
490 /* --------------------------------------------------------------------------------------------- */
493 sfs_done (struct vfs_class
*me
)
499 for (i
= 0; i
< sfs_no
; i
++)
501 g_free (sfs_prefix
[i
]);
502 g_free (sfs_command
[i
]);
503 sfs_prefix
[i
] = sfs_command
[i
] = NULL
;
508 /* --------------------------------------------------------------------------------------------- */
511 sfs_which (struct vfs_class
*me
, const char *path
)
517 for (i
= 0; i
< sfs_no
; i
++)
518 if (sfs_flags
[i
] & F_FULLMATCH
)
520 if (!strcmp (path
, sfs_prefix
[i
]))
523 else if (!strncmp (path
, sfs_prefix
[i
], strlen (sfs_prefix
[i
])))
529 /* --------------------------------------------------------------------------------------------- */
530 /*** public functions ****************************************************************************/
531 /* --------------------------------------------------------------------------------------------- */
536 vfs_sfs_ops
.name
= "sfs";
537 vfs_sfs_ops
.init
= sfs_init
;
538 vfs_sfs_ops
.done
= sfs_done
;
539 vfs_sfs_ops
.fill_names
= sfs_fill_names
;
540 vfs_sfs_ops
.which
= sfs_which
;
541 vfs_sfs_ops
.open
= sfs_open
;
542 vfs_sfs_ops
.close
= local_close
;
543 vfs_sfs_ops
.read
= local_read
;
544 vfs_sfs_ops
.stat
= sfs_stat
;
545 vfs_sfs_ops
.lstat
= sfs_lstat
;
546 vfs_sfs_ops
.fstat
= local_fstat
;
547 vfs_sfs_ops
.chmod
= sfs_chmod
;
548 vfs_sfs_ops
.chown
= sfs_chown
;
549 vfs_sfs_ops
.utime
= sfs_utime
;
550 vfs_sfs_ops
.readlink
= sfs_readlink
;
551 vfs_sfs_ops
.ferrno
= local_errno
;
552 vfs_sfs_ops
.lseek
= local_lseek
;
553 vfs_sfs_ops
.getid
= sfs_getid
;
554 vfs_sfs_ops
.nothingisopen
= sfs_nothingisopen
;
555 vfs_sfs_ops
.free
= sfs_free
;
556 vfs_sfs_ops
.getlocalcopy
= sfs_getlocalcopy
;
557 vfs_sfs_ops
.ungetlocalcopy
= sfs_ungetlocalcopy
;
558 vfs_register_class (&vfs_sfs_ops
);
561 /* --------------------------------------------------------------------------------------------- */