4 Copyright 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
6 The Free Software Foundation, Inc.
8 This file is part of the Midnight Commander.
10 The Midnight Commander is free software: you can redistribute it
11 and/or modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation, either version 3 of the License,
13 or (at your option) any later version.
15 The Midnight Commander is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 * \brief Source: Single File fileSystem
28 * This defines whole class of filesystems which contain single file
29 * inside. It is somehow similar to extfs, except that extfs makes
30 * whole virtual trees and we do only single virtual files.
32 * If you want to gunzip something, you should open it with \verbatim #ugz \endverbatim
33 * suffix, DON'T try to gunzip it yourself.
35 * Namespace: exports vfs_sfs_ops
40 #include <sys/types.h>
46 #include "lib/global.h"
48 #include "lib/widget.h" /* D_ERROR, D_NORMAL */
50 #include "src/execute.h" /* EXECUTE_AS_SHELL */
52 #include "lib/vfs/vfs.h"
53 #include "lib/vfs/utilvfs.h"
54 #include "src/vfs/local/local.h"
55 #include "lib/vfs/gc.h" /* vfs_stamp_create */
59 /*** global variables ****************************************************************************/
61 /*** file scope macro definitions ****************************************************************/
67 #define F_NOLOCALCOPY 4
71 if ((size_t) (t - pad) > sizeof(pad)) \
79 #define COPY_STRING(a) \
80 if ((t - pad) + strlen(a) > sizeof(pad)) \
91 /*** file scope type declarations ****************************************************************/
93 typedef struct cachedfile
99 /*** file scope variables ************************************************************************/
102 static struct vfs_class vfs_sfs_ops
;
104 static int sfs_no
= 0;
105 static char *sfs_prefix
[MAXFS
];
106 static char *sfs_command
[MAXFS
];
107 static int sfs_flags
[MAXFS
];
109 /*** file scope functions ************************************************************************/
112 cachedfile_compare (const void *a
, const void *b
)
114 const cachedfile
*cf
= (const cachedfile
*) a
;
115 const char *name
= (const char *) b
;
117 return strcmp (name
, cf
->name
);
120 /* --------------------------------------------------------------------------------------------- */
123 sfs_vfmake (const vfs_path_t
* vpath
, vfs_path_t
* cache_vpath
)
127 char *s_iter
, *t
= pad
;
129 vfs_path_t
*pname
, *s
; /* name of parent archive */
130 char *pqname
; /* name of parent archive, quoted */
131 const vfs_path_element_t
*path_element
;
133 path_element
= vfs_path_get_by_index (vpath
, -1);
134 pname
= vfs_path_clone (vpath
);
135 vfs_path_remove_element_by_index (pname
, -1);
137 w
= (*path_element
->class->which
) (path_element
->class, path_element
->vfs_prefix
);
139 vfs_die ("This cannot happen... Hopefully.\n");
141 if ((sfs_flags
[w
] & F_1
) == 0 && strcmp (vfs_path_get_last_path_str (pname
), PATH_SEP_STR
) != 0)
143 vfs_path_free (pname
);
147 /* if ((sfs_flags[w] & F_2) || (!inpath) || (!*inpath)); else return -1; */
148 if ((sfs_flags
[w
] & F_NOLOCALCOPY
) == 0)
150 s
= mc_getlocalcopy (pname
);
153 vfs_path_free (pname
);
156 pqname
= name_quote (vfs_path_get_last_path_str (s
), 0);
163 pname_str
= vfs_path_to_str (pname
);
164 pqname
= name_quote (pname_str
, 0);
167 vfs_path_free (pname
);
170 for (s_iter
= sfs_command
[w
]; *s_iter
!= '\0'; s_iter
++)
174 const char *ptr
= NULL
;
184 ptr
= path_element
->path
;
187 ptr
= vfs_path_get_by_index (cache_vpath
, -1)->path
;
206 if (my_system (EXECUTE_AS_SHELL
, "/bin/sh", pad
))
208 close_error_pipe (D_ERROR
, NULL
);
212 close_error_pipe (D_NORMAL
, NULL
);
216 /* --------------------------------------------------------------------------------------------- */
219 sfs_redirect (const vfs_path_t
* vpath
)
223 vfs_path_t
*cache_vpath
;
225 const vfs_path_element_t
*path_element
;
228 path
= vfs_path_to_str (vpath
);
229 path_element
= vfs_path_get_by_index (vpath
, -1);
230 cur
= g_slist_find_custom (head
, path
, cachedfile_compare
);
235 cf
= (cachedfile
*) cur
->data
;
236 vfs_stamp (&vfs_sfs_ops
, cf
);
240 handle
= vfs_mkstemps (&cache_vpath
, "sfs", path_element
->path
);
243 return "/SOMEONE_PLAYING_DIRTY_TMP_TRICKS_ON_US";
247 if (sfs_vfmake (vpath
, cache_vpath
) == 0)
249 cf
= g_new (cachedfile
, 1);
250 cf
->name
= vfs_path_to_str (vpath
);
251 cf
->cache
= vfs_path_to_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
);
259 mc_unlink (cache_vpath
);
260 vfs_path_free (cache_vpath
);
261 return "/I_MUST_NOT_EXIST";
264 /* --------------------------------------------------------------------------------------------- */
267 sfs_open (const vfs_path_t
* vpath
/*struct vfs_class *me, const char *path */ , int flags
,
273 fd
= open (sfs_redirect (vpath
), NO_LINEAR (flags
), mode
);
277 sfs_info
= g_new (int, 1);
283 /* --------------------------------------------------------------------------------------------- */
286 sfs_stat (const vfs_path_t
* vpath
, struct stat
*buf
)
288 return stat (sfs_redirect (vpath
), buf
);
291 /* --------------------------------------------------------------------------------------------- */
294 sfs_lstat (const vfs_path_t
* vpath
, struct stat
*buf
)
296 #ifndef HAVE_STATLSTAT
297 return lstat (sfs_redirect (vpath
), buf
);
299 return statlstat (sfs_redirect (vpath
), buf
);
303 /* --------------------------------------------------------------------------------------------- */
306 sfs_chmod (const vfs_path_t
* vpath
, mode_t mode
)
308 return chmod (sfs_redirect (vpath
), mode
);
311 /* --------------------------------------------------------------------------------------------- */
314 sfs_chown (const vfs_path_t
* vpath
, uid_t owner
, gid_t group
)
316 return chown (sfs_redirect (vpath
), owner
, group
);
319 /* --------------------------------------------------------------------------------------------- */
322 sfs_utime (const vfs_path_t
* vpath
, struct utimbuf
*times
)
324 return utime (sfs_redirect (vpath
), times
);
327 /* --------------------------------------------------------------------------------------------- */
330 sfs_readlink (const vfs_path_t
* vpath
, char *buf
, size_t size
)
332 return readlink (sfs_redirect (vpath
), buf
, size
);
335 /* --------------------------------------------------------------------------------------------- */
338 sfs_getid (const vfs_path_t
* vpath
)
341 char *path
= vfs_path_to_str (vpath
);
343 cur
= g_slist_find_custom (head
, path
, cachedfile_compare
);
346 return (vfsid
) (cur
!= NULL
? cur
->data
: NULL
);
349 /* --------------------------------------------------------------------------------------------- */
354 struct cachedfile
*which
;
357 which
= (struct cachedfile
*) id
;
358 cur
= g_slist_find (head
, which
);
360 vfs_die ("Free of thing which is unknown to me\n");
362 which
= (struct cachedfile
*) cur
->data
;
363 unlink (which
->cache
);
364 g_free (which
->cache
);
365 g_free (which
->name
);
368 head
= g_slist_delete_link (head
, cur
);
371 /* --------------------------------------------------------------------------------------------- */
374 sfs_fill_names (struct vfs_class
*me
, fill_names_f func
)
380 for (cur
= head
; cur
!= NULL
; cur
= g_slist_next (cur
))
381 func (((cachedfile
*) cur
->data
)->name
);
384 /* --------------------------------------------------------------------------------------------- */
387 sfs_nothingisopen (vfsid id
)
389 /* FIXME: Investigate whether have to guard this like in
390 the other VFSs (see fd_usage in extfs) -- Norbert */
395 /* --------------------------------------------------------------------------------------------- */
398 sfs_getlocalcopy (const vfs_path_t
* vpath
)
400 return vfs_path_from_str (sfs_redirect (vpath
));
403 /* --------------------------------------------------------------------------------------------- */
406 sfs_ungetlocalcopy (const vfs_path_t
* vpath
, const vfs_path_t
* local
, gboolean has_changed
)
414 /* --------------------------------------------------------------------------------------------- */
417 sfs_init (struct vfs_class
*me
)
425 mc_sfsini
= g_build_filename (mc_global
.sysconfig_dir
, "sfs.ini", (char *) NULL
);
426 cfg
= fopen (mc_sfsini
, "r");
430 fprintf (stderr
, _("%s: Warning: file %s not found\n"), "sfs_init()", mc_sfsini
);
437 while (sfs_no
< MAXFS
&& fgets (key
, sizeof (key
), cfg
))
439 char *c
, *semi
= NULL
, flags
= 0;
441 if (*key
== '#' || *key
== '\n')
444 for (c
= key
; *c
; c
++)
445 if ((*c
== ':') || (*c
== '/'))
451 flags
|= F_FULLMATCH
;
459 fprintf (stderr
, _("Warning: Invalid line in %s:\n%s\n"), "sfs.ini", key
);
464 while (*c
&& (*c
!= ' ') && (*c
!= '\t'))
475 flags
|= F_NOLOCALCOPY
;
478 fprintf (stderr
, _("Warning: Invalid flag %c in %s:\n%s\n"), *c
, "sfs.ini", key
);
487 semi
= strchr (c
, '\n');
491 sfs_prefix
[sfs_no
] = g_strdup (key
);
492 sfs_command
[sfs_no
] = g_strdup (c
);
493 sfs_flags
[sfs_no
] = flags
;
500 /* --------------------------------------------------------------------------------------------- */
503 sfs_done (struct vfs_class
*me
)
509 for (i
= 0; i
< sfs_no
; i
++)
511 g_free (sfs_prefix
[i
]);
512 g_free (sfs_command
[i
]);
513 sfs_prefix
[i
] = sfs_command
[i
] = NULL
;
518 /* --------------------------------------------------------------------------------------------- */
521 sfs_which (struct vfs_class
*me
, const char *path
)
527 for (i
= 0; i
< sfs_no
; i
++)
528 if (sfs_flags
[i
] & F_FULLMATCH
)
530 if (!strcmp (path
, sfs_prefix
[i
]))
533 else if (!strncmp (path
, sfs_prefix
[i
], strlen (sfs_prefix
[i
])))
539 /* --------------------------------------------------------------------------------------------- */
540 /*** public functions ****************************************************************************/
541 /* --------------------------------------------------------------------------------------------- */
546 vfs_sfs_ops
.name
= "sfs";
547 vfs_sfs_ops
.init
= sfs_init
;
548 vfs_sfs_ops
.done
= sfs_done
;
549 vfs_sfs_ops
.fill_names
= sfs_fill_names
;
550 vfs_sfs_ops
.which
= sfs_which
;
551 vfs_sfs_ops
.open
= sfs_open
;
552 vfs_sfs_ops
.close
= local_close
;
553 vfs_sfs_ops
.read
= local_read
;
554 vfs_sfs_ops
.stat
= sfs_stat
;
555 vfs_sfs_ops
.lstat
= sfs_lstat
;
556 vfs_sfs_ops
.fstat
= local_fstat
;
557 vfs_sfs_ops
.chmod
= sfs_chmod
;
558 vfs_sfs_ops
.chown
= sfs_chown
;
559 vfs_sfs_ops
.utime
= sfs_utime
;
560 vfs_sfs_ops
.readlink
= sfs_readlink
;
561 vfs_sfs_ops
.ferrno
= local_errno
;
562 vfs_sfs_ops
.lseek
= local_lseek
;
563 vfs_sfs_ops
.getid
= sfs_getid
;
564 vfs_sfs_ops
.nothingisopen
= sfs_nothingisopen
;
565 vfs_sfs_ops
.free
= sfs_free
;
566 vfs_sfs_ops
.getlocalcopy
= sfs_getlocalcopy
;
567 vfs_sfs_ops
.ungetlocalcopy
= sfs_ungetlocalcopy
;
568 vfs_register_class (&vfs_sfs_ops
);
571 /* --------------------------------------------------------------------------------------------- */