sfs.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. /*
  2. * Single File fileSystem
  3. *
  4. * Copyright 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
  5. * Free Software Foundation, Inc.
  6. *
  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.
  11. *
  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.
  16. *
  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.
  20. *
  21. * This defines whole class of filesystems which contain single file
  22. * inside. It is somehow similar to extfs, except that extfs makes
  23. * whole virtual trees and we do only single virtual files.
  24. *
  25. * If you want to gunzip something, you should open it with #ugz
  26. * suffix, DON'T try to gunzip it yourself.
  27. *
  28. * Namespace: exports vfs_sfs_ops */
  29. #include <config.h>
  30. #include <errno.h>
  31. #include <sys/types.h>
  32. #include <unistd.h>
  33. #include <stdio.h>
  34. #include <string.h>
  35. #include "../src/global.h"
  36. #include "../src/tty.h" /* enable/disable interrupt key */
  37. #include "../src/wtools.h" /* message() */
  38. #include "../src/main.h" /* print_vfs_message */
  39. #include "utilvfs.h"
  40. #include "vfs.h"
  41. #include "vfs-impl.h"
  42. #include "gc.h" /* vfs_stamp_create */
  43. #include "local.h"
  44. #include "../src/execute.h" /* EXECUTE_AS_SHELL */
  45. struct cachedfile {
  46. char *name, *cache;
  47. struct cachedfile *next;
  48. };
  49. static struct cachedfile *head;
  50. static struct vfs_class vfs_sfs_ops;
  51. #define MAXFS 32
  52. static int sfs_no = 0;
  53. static char *sfs_prefix[ MAXFS ];
  54. static char *sfs_command[ MAXFS ];
  55. static int sfs_flags[ MAXFS ];
  56. #define F_1 1
  57. #define F_2 2
  58. #define F_NOLOCALCOPY 4
  59. #define F_FULLMATCH 8
  60. static int
  61. sfs_vfmake (struct vfs_class *me, const char *name, char *cache)
  62. {
  63. char *inpath, *op;
  64. int w;
  65. char pad[10240];
  66. char *s, *t = pad;
  67. int was_percent = 0;
  68. char *pname; /* name of parent archive */
  69. char *pqname; /* name of parent archive, quoted */
  70. pname = g_strdup (name);
  71. vfs_split (pname, &inpath, &op);
  72. if ((w = (*me->which) (me, op)) == -1)
  73. vfs_die ("This cannot happen... Hopefully.\n");
  74. if (!(sfs_flags[w] & F_1) && strcmp (pname, "/")) {
  75. g_free (pname);
  76. return -1;
  77. }
  78. /* if ((sfs_flags[w] & F_2) || (!inpath) || (!*inpath)); else return -1; */
  79. if (!(sfs_flags[w] & F_NOLOCALCOPY)) {
  80. s = mc_getlocalcopy (pname);
  81. if (!s) {
  82. g_free (pname);
  83. return -1;
  84. }
  85. pqname = name_quote (s, 0);
  86. g_free (s);
  87. } else {
  88. pqname = name_quote (pname, 0);
  89. }
  90. g_free (pname);
  91. #define COPY_CHAR \
  92. if ((size_t) (t-pad) > sizeof(pad)) { \
  93. g_free (pqname); \
  94. return -1; \
  95. } \
  96. else \
  97. *t++ = *s;
  98. #define COPY_STRING(a) \
  99. if ((t-pad)+strlen(a)>sizeof(pad)) { \
  100. g_free (pqname); \
  101. return -1; \
  102. } else { \
  103. strcpy (t, a); \
  104. t+= strlen(a); \
  105. }
  106. for (s = sfs_command[w]; *s; s++) {
  107. if (was_percent) {
  108. const char *ptr = NULL;
  109. was_percent = 0;
  110. switch (*s) {
  111. case '1':
  112. ptr = pqname;
  113. break;
  114. case '2':
  115. ptr = op + strlen (sfs_prefix[w]);
  116. break;
  117. case '3':
  118. ptr = cache;
  119. break;
  120. case '%':
  121. COPY_CHAR;
  122. continue;
  123. }
  124. COPY_STRING (ptr);
  125. } else {
  126. if (*s == '%')
  127. was_percent = 1;
  128. else
  129. COPY_CHAR;
  130. }
  131. }
  132. g_free (pqname);
  133. open_error_pipe ();
  134. if (my_system (EXECUTE_AS_SHELL, "/bin/sh", pad)) {
  135. close_error_pipe (D_ERROR, NULL);
  136. return -1;
  137. }
  138. close_error_pipe (D_NORMAL, NULL);
  139. return 0; /* OK */
  140. }
  141. static const char *
  142. sfs_redirect (struct vfs_class *me, const char *name)
  143. {
  144. struct cachedfile *cur = head;
  145. char *cache;
  146. int handle;
  147. while (cur) {
  148. if (!strcmp (name, cur->name)) {
  149. vfs_stamp (&vfs_sfs_ops, cur);
  150. return cur->cache;
  151. }
  152. cur = cur->next;
  153. }
  154. handle = vfs_mkstemps (&cache, "sfs", name);
  155. if (handle == -1) {
  156. return "/SOMEONE_PLAYING_DIRTY_TMP_TRICKS_ON_US";
  157. }
  158. close (handle);
  159. if (!sfs_vfmake (me, name, cache)) {
  160. cur = g_new (struct cachedfile, 1);
  161. cur->name = g_strdup (name);
  162. cur->cache = cache;
  163. cur->next = head;
  164. head = cur;
  165. vfs_stamp_create (&vfs_sfs_ops, head);
  166. return cache;
  167. }
  168. unlink (cache);
  169. g_free (cache);
  170. return "/I_MUST_NOT_EXIST";
  171. }
  172. static void *
  173. sfs_open (struct vfs_class *me, const char *path, int flags, int mode)
  174. {
  175. int *sfs_info;
  176. int fd;
  177. path = sfs_redirect (me, path);
  178. fd = open (path, NO_LINEAR (flags), mode);
  179. if (fd == -1)
  180. return 0;
  181. sfs_info = g_new (int, 1);
  182. *sfs_info = fd;
  183. return sfs_info;
  184. }
  185. static int sfs_stat (struct vfs_class *me, const char *path, struct stat *buf)
  186. {
  187. path = sfs_redirect (me, path);
  188. return stat (path, buf);
  189. }
  190. static int sfs_lstat (struct vfs_class *me, const char *path, struct stat *buf)
  191. {
  192. path = sfs_redirect (me, path);
  193. #ifndef HAVE_STATLSTAT
  194. return lstat (path, buf);
  195. #else
  196. return statlstat (path, buf);
  197. #endif
  198. }
  199. static int sfs_chmod (struct vfs_class *me, const char *path, int mode)
  200. {
  201. path = sfs_redirect (me, path);
  202. return chmod (path, mode);
  203. }
  204. static int sfs_chown (struct vfs_class *me, const char *path, int owner, int group)
  205. {
  206. path = sfs_redirect (me, path);
  207. return chown (path, owner, group);
  208. }
  209. static int sfs_utime (struct vfs_class *me, const char *path, struct utimbuf *times)
  210. {
  211. path = sfs_redirect (me, path);
  212. return utime (path, times);
  213. }
  214. static int
  215. sfs_readlink (struct vfs_class *me, const char *path, char *buf, size_t size)
  216. {
  217. path = sfs_redirect (me, path);
  218. return readlink (path, buf, size);
  219. }
  220. static vfsid
  221. sfs_getid (struct vfs_class *me, const char *path)
  222. {
  223. struct cachedfile *cur = head;
  224. (void) me;
  225. while (cur) {
  226. if (!strcmp (path, cur->name))
  227. break;
  228. cur = cur->next;
  229. }
  230. return (vfsid) cur;
  231. }
  232. static void sfs_free (vfsid id)
  233. {
  234. struct cachedfile *which = (struct cachedfile *) id;
  235. struct cachedfile *cur, *prev;
  236. for (cur = head, prev = 0; cur && cur != which; prev = cur, cur = cur->next)
  237. ;
  238. if (!cur)
  239. vfs_die( "Free of thing which is unknown to me\n" );
  240. unlink (cur->cache);
  241. if (prev)
  242. prev->next = cur->next;
  243. else
  244. head = cur->next;
  245. g_free (cur->cache);
  246. g_free (cur->name);
  247. g_free (cur);
  248. }
  249. static void sfs_fill_names (struct vfs_class *me, fill_names_f func)
  250. {
  251. struct cachedfile *cur = head;
  252. (void) me;
  253. while (cur){
  254. (*func)(cur->name);
  255. cur = cur->next;
  256. }
  257. }
  258. static int sfs_nothingisopen (vfsid id)
  259. {
  260. /* FIXME: Investigate whether have to guard this like in
  261. the other VFSs (see fd_usage in extfs) -- Norbert */
  262. (void) id;
  263. return 1;
  264. }
  265. static char *
  266. sfs_getlocalcopy (struct vfs_class *me, const char *path)
  267. {
  268. path = sfs_redirect (me, path);
  269. return g_strdup (path);
  270. }
  271. static int
  272. sfs_ungetlocalcopy (struct vfs_class *me, const char *path,
  273. const char *local, int has_changed)
  274. {
  275. (void) me;
  276. (void) path;
  277. (void) local;
  278. (void) has_changed;
  279. return 0;
  280. }
  281. static int sfs_init (struct vfs_class *me)
  282. {
  283. char *mc_sfsini;
  284. FILE *cfg;
  285. char key[256];
  286. (void) me;
  287. mc_sfsini = concat_dir_and_file (mc_home, "extfs" PATH_SEP_STR "sfs.ini");
  288. cfg = fopen (mc_sfsini, "r");
  289. if (!cfg){
  290. fprintf (stderr, _("Warning: file %s not found\n"), mc_sfsini);
  291. g_free (mc_sfsini);
  292. return 0;
  293. }
  294. g_free (mc_sfsini);
  295. sfs_no = 0;
  296. while (sfs_no < MAXFS && fgets (key, sizeof (key), cfg)) {
  297. char *c, *semi = NULL, flags = 0;
  298. if (*key == '#' || *key == '\n')
  299. continue;
  300. for (c = key; *c; c++)
  301. if ((*c == ':') || (*c == '/')){
  302. semi = c;
  303. if (*c == '/'){
  304. *c = 0;
  305. flags |= F_FULLMATCH;
  306. }
  307. break;
  308. }
  309. if (!semi){
  310. invalid_line:
  311. fprintf (stderr, _("Warning: Invalid line in %s:\n%s\n"),
  312. "sfs.ini", key);
  313. continue;
  314. }
  315. c = semi + 1;
  316. while (*c && (*c != ' ') && (*c != '\t')) {
  317. switch (*c) {
  318. case '1': flags |= F_1; break;
  319. case '2': flags |= F_2; break;
  320. case 'R': flags |= F_NOLOCALCOPY; break;
  321. default:
  322. fprintf (stderr, _("Warning: Invalid flag %c in %s:\n%s\n"),
  323. *c, "sfs.ini", key);
  324. }
  325. c++;
  326. }
  327. if (!*c)
  328. goto invalid_line;
  329. c++;
  330. *(semi+1) = 0;
  331. if ((semi = strchr (c, '\n')))
  332. *semi = 0;
  333. sfs_prefix [sfs_no] = g_strdup (key);
  334. sfs_command [sfs_no] = g_strdup (c);
  335. sfs_flags [sfs_no] = flags;
  336. sfs_no++;
  337. }
  338. fclose (cfg);
  339. return 1;
  340. }
  341. static void
  342. sfs_done (struct vfs_class *me)
  343. {
  344. int i;
  345. (void) me;
  346. for (i = 0; i < sfs_no; i++){
  347. g_free (sfs_prefix [i]);
  348. g_free (sfs_command [i]);
  349. sfs_prefix [i] = sfs_command [i] = NULL;
  350. }
  351. sfs_no = 0;
  352. }
  353. static int
  354. sfs_which (struct vfs_class *me, const char *path)
  355. {
  356. int i;
  357. (void) me;
  358. for (i = 0; i < sfs_no; i++)
  359. if (sfs_flags [i] & F_FULLMATCH) {
  360. if (!strcmp (path, sfs_prefix [i]))
  361. return i;
  362. } else
  363. if (!strncmp (path, sfs_prefix [i], strlen (sfs_prefix [i])))
  364. return i;
  365. return -1;
  366. }
  367. void
  368. init_sfs (void)
  369. {
  370. vfs_sfs_ops.name = "sfs";
  371. vfs_sfs_ops.init = sfs_init;
  372. vfs_sfs_ops.done = sfs_done;
  373. vfs_sfs_ops.fill_names = sfs_fill_names;
  374. vfs_sfs_ops.which = sfs_which;
  375. vfs_sfs_ops.open = sfs_open;
  376. vfs_sfs_ops.close = local_close;
  377. vfs_sfs_ops.read = local_read;
  378. vfs_sfs_ops.stat = sfs_stat;
  379. vfs_sfs_ops.lstat = sfs_lstat;
  380. vfs_sfs_ops.fstat = local_fstat;
  381. vfs_sfs_ops.chmod = sfs_chmod;
  382. vfs_sfs_ops.chown = sfs_chown;
  383. vfs_sfs_ops.utime = sfs_utime;
  384. vfs_sfs_ops.readlink = sfs_readlink;
  385. vfs_sfs_ops.ferrno = local_errno;
  386. vfs_sfs_ops.lseek = local_lseek;
  387. vfs_sfs_ops.getid = sfs_getid;
  388. vfs_sfs_ops.nothingisopen = sfs_nothingisopen;
  389. vfs_sfs_ops.free = sfs_free;
  390. vfs_sfs_ops.getlocalcopy = sfs_getlocalcopy;
  391. vfs_sfs_ops.ungetlocalcopy = sfs_ungetlocalcopy;
  392. vfs_register_class (&vfs_sfs_ops);
  393. }