4 Copyright 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
6 The Free Software Foundation, Inc.
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/>.
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
43 #include <sys/types.h>
49 #include "lib/global.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 */
62 /*** global variables ****************************************************************************/
64 /*** file scope macro definitions ****************************************************************/
70 #define F_NOLOCALCOPY 4
74 if ((size_t) (t - pad) > sizeof(pad)) \
82 #define COPY_STRING(a) \
83 if ((t - pad) + strlen(a) > sizeof(pad)) \
94 /*** file scope type declarations ****************************************************************/
96 typedef struct cachedfile
102 /*** file scope variables ************************************************************************/
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 ************************************************************************/
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 /* --------------------------------------------------------------------------------------------- */
126 sfs_vfmake (const vfs_path_t
* vpath
, vfs_path_t
* cache_vpath
)
130 char *s_iter
, *t
= pad
;
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
);
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
);
150 /* if ((sfs_flags[w] & F_2) || (!inpath) || (!*inpath)); else return -1; */
151 if ((sfs_flags
[w
] & F_NOLOCALCOPY
) == 0)
155 s
= mc_getlocalcopy (pname
);
158 vfs_path_free (pname
);
161 pqname
= name_quote (vfs_path_get_last_path_str (s
), 0);
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
++)
174 const char *ptr
= NULL
;
184 ptr
= path_element
->path
;
187 ptr
= vfs_path_get_by_index (cache_vpath
, -1)->path
;
209 if (my_system (EXECUTE_AS_SHELL
, "/bin/sh", pad
))
211 close_error_pipe (D_ERROR
, NULL
);
215 close_error_pipe (D_NORMAL
, NULL
);
219 /* --------------------------------------------------------------------------------------------- */
222 sfs_redirect (const vfs_path_t
* vpath
)
226 vfs_path_t
*cache_vpath
;
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
);
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
= 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
);
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
)
342 cur
= g_slist_find_custom (head
, vfs_path_as_str (vpath
), cachedfile_compare
);
344 return (vfsid
) (cur
!= NULL
? cur
->data
: NULL
);
347 /* --------------------------------------------------------------------------------------------- */
352 struct cachedfile
*which
;
355 which
= (struct cachedfile
*) id
;
356 cur
= g_slist_find (head
, which
);
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
);
366 head
= g_slist_delete_link (head
, cur
);
369 /* --------------------------------------------------------------------------------------------- */
372 sfs_fill_names (struct vfs_class
*me
, fill_names_f func
)
378 for (cur
= head
; cur
!= NULL
; cur
= g_slist_next (cur
))
379 func (((cachedfile
*) cur
->data
)->name
);
382 /* --------------------------------------------------------------------------------------------- */
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 */
393 /* --------------------------------------------------------------------------------------------- */
396 sfs_getlocalcopy (const vfs_path_t
* vpath
)
398 return vfs_path_from_str (sfs_redirect (vpath
));
401 /* --------------------------------------------------------------------------------------------- */
404 sfs_ungetlocalcopy (const vfs_path_t
* vpath
, const vfs_path_t
* local
, gboolean has_changed
)
412 /* --------------------------------------------------------------------------------------------- */
415 sfs_init (struct vfs_class
*me
)
423 mc_sfsini
= g_build_filename (mc_global
.sysconfig_dir
, "sfs.ini", (char *) NULL
);
424 cfg
= fopen (mc_sfsini
, "r");
428 fprintf (stderr
, _("%s: Warning: file %s not found\n"), "sfs_init()", mc_sfsini
);
435 while (sfs_no
< MAXFS
&& fgets (key
, sizeof (key
), cfg
))
437 char *c
, *semi
= NULL
, flags
= 0;
439 if (*key
== '#' || *key
== '\n')
442 for (c
= key
; *c
; c
++)
443 if ((*c
== ':') || (*c
== '/'))
449 flags
|= F_FULLMATCH
;
457 fprintf (stderr
, _("Warning: Invalid line in %s:\n%s\n"), "sfs.ini", key
);
462 while (*c
&& (*c
!= ' ') && (*c
!= '\t'))
473 flags
|= F_NOLOCALCOPY
;
476 fprintf (stderr
, _("Warning: Invalid flag %c in %s:\n%s\n"), *c
, "sfs.ini", key
);
485 semi
= strchr (c
, '\n');
489 sfs_prefix
[sfs_no
] = g_strdup (key
);
490 sfs_command
[sfs_no
] = g_strdup (c
);
491 sfs_flags
[sfs_no
] = flags
;
498 /* --------------------------------------------------------------------------------------------- */
501 sfs_done (struct vfs_class
*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
;
516 /* --------------------------------------------------------------------------------------------- */
519 sfs_which (struct vfs_class
*me
, const char *path
)
525 for (i
= 0; i
< sfs_no
; i
++)
526 if (sfs_flags
[i
] & F_FULLMATCH
)
528 if (!strcmp (path
, sfs_prefix
[i
]))
531 else if (!strncmp (path
, sfs_prefix
[i
], strlen (sfs_prefix
[i
])))
537 /* --------------------------------------------------------------------------------------------- */
538 /*** public functions ****************************************************************************/
539 /* --------------------------------------------------------------------------------------------- */
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 /* --------------------------------------------------------------------------------------------- */