sfs.c 10.0 KB

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