extfs.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615
  1. /* Virtual File System: External file system.
  2. Copyright (C) 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
  3. 2006, 2007, 2009 Free Software Foundation, Inc.
  4. Written by: 1995 Jakub Jelinek
  5. Rewritten by: 1998 Pavel Machek
  6. Additional changes by: 1999 Andrew T. Veliath
  7. This program is free software; you can redistribute it and/or
  8. modify it under the terms of the GNU Library General Public License
  9. as published by the Free Software Foundation; either version 2 of
  10. the License, or (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU Library General Public License for more details.
  15. You should have received a copy of the GNU Library General Public
  16. License along with this program; if not, write to the Free Software
  17. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
  18. /**
  19. * \file
  20. * \brief Source: Virtual File System: External file system
  21. * \author Jakub Jelinek
  22. * \author Pavel Machek
  23. * \author Andrew T. Veliath
  24. * \date 1995, 1998, 1999
  25. */
  26. /* Namespace: init_extfs */
  27. #include <config.h>
  28. #include <stdio.h>
  29. #include <ctype.h>
  30. #include <string.h>
  31. #include <stdlib.h>
  32. #include <sys/types.h>
  33. #include <sys/stat.h>
  34. #include <unistd.h>
  35. #include <signal.h>
  36. #include <fcntl.h>
  37. #include <errno.h>
  38. #include <sys/wait.h>
  39. #include <unistd.h>
  40. #include "lib/global.h"
  41. #include "lib/fileloc.h"
  42. #include "src/wtools.h" /* message() */
  43. #include "src/main.h" /* print_vfs_message */
  44. #include "src/execute.h" /* For shell_execute */
  45. #include "utilvfs.h"
  46. #include "vfs.h"
  47. #include "vfs-impl.h"
  48. #include "gc.h" /* vfs_rmstamp */
  49. #undef ERRNOR
  50. #define ERRNOR(x,y) do { my_errno = x; return y; } while(0)
  51. struct inode
  52. {
  53. nlink_t nlink;
  54. struct entry *first_in_subdir; /* only used if this is a directory */
  55. struct entry *last_in_subdir;
  56. ino_t inode; /* This is inode # */
  57. dev_t dev; /* This is an internal identification of the extfs archive */
  58. struct archive *archive; /* And this is an archive structure */
  59. dev_t rdev;
  60. mode_t mode;
  61. uid_t uid;
  62. gid_t gid;
  63. off_t size;
  64. time_t mtime;
  65. char *linkname;
  66. time_t atime;
  67. time_t ctime;
  68. char *local_filename;
  69. };
  70. struct entry
  71. {
  72. struct entry *next_in_dir;
  73. struct entry *dir;
  74. char *name;
  75. struct inode *inode;
  76. };
  77. struct pseudofile
  78. {
  79. struct archive *archive;
  80. gboolean has_changed;
  81. int local_handle;
  82. struct entry *entry;
  83. };
  84. struct archive
  85. {
  86. int fstype;
  87. char *name;
  88. char *local_name;
  89. struct stat local_stat;
  90. dev_t rdev;
  91. int fd_usage;
  92. ino_t inode_counter;
  93. struct entry *root_entry;
  94. struct archive *next;
  95. };
  96. typedef struct
  97. {
  98. char *path;
  99. char *prefix;
  100. gboolean need_archive;
  101. } extfs_plugin_info_t;
  102. static gboolean errloop;
  103. static gboolean notadir;
  104. static void extfs_remove_entry (struct entry *e);
  105. static void extfs_free (vfsid id);
  106. static void extfs_free_entry (struct entry *e);
  107. static struct entry *extfs_resolve_symlinks_int (struct entry *entry, GSList * list);
  108. static struct vfs_class vfs_extfs_ops;
  109. static struct archive *first_archive = NULL;
  110. static int my_errno = 0;
  111. GArray *extfs_plugins = NULL;
  112. static void
  113. extfs_make_dots (struct entry *ent)
  114. {
  115. struct entry *entry = g_new (struct entry, 1);
  116. struct entry *parentry = ent->dir;
  117. struct inode *inode = ent->inode, *parent;
  118. parent = (parentry != NULL) ? parentry->inode : NULL;
  119. entry->name = g_strdup (".");
  120. entry->inode = inode;
  121. entry->dir = ent;
  122. inode->local_filename = NULL;
  123. inode->first_in_subdir = entry;
  124. inode->nlink++;
  125. entry->next_in_dir = g_new (struct entry, 1);
  126. entry = entry->next_in_dir;
  127. entry->name = g_strdup ("..");
  128. inode->last_in_subdir = entry;
  129. entry->next_in_dir = NULL;
  130. if (parent != NULL)
  131. {
  132. entry->inode = parent;
  133. entry->dir = parentry;
  134. parent->nlink++;
  135. }
  136. else
  137. {
  138. entry->inode = inode;
  139. entry->dir = ent;
  140. inode->nlink++;
  141. }
  142. }
  143. static struct entry *
  144. extfs_generate_entry (struct archive *archive,
  145. const char *name, struct entry *parentry, mode_t mode)
  146. {
  147. mode_t myumask;
  148. struct inode *inode, *parent;
  149. struct entry *entry;
  150. parent = (parentry != NULL) ? parentry->inode : NULL;
  151. entry = g_new (struct entry, 1);
  152. entry->name = g_strdup (name);
  153. entry->next_in_dir = NULL;
  154. entry->dir = parentry;
  155. if (parent != NULL)
  156. {
  157. parent->last_in_subdir->next_in_dir = entry;
  158. parent->last_in_subdir = entry;
  159. }
  160. inode = g_new (struct inode, 1);
  161. entry->inode = inode;
  162. inode->local_filename = NULL;
  163. inode->linkname = NULL;
  164. inode->last_in_subdir = NULL;
  165. inode->inode = (archive->inode_counter)++;
  166. inode->dev = archive->rdev;
  167. inode->archive = archive;
  168. myumask = umask (022);
  169. umask (myumask);
  170. inode->mode = mode & ~myumask;
  171. mode = inode->mode;
  172. inode->rdev = 0;
  173. inode->uid = getuid ();
  174. inode->gid = getgid ();
  175. inode->size = 0;
  176. inode->mtime = time (NULL);
  177. inode->atime = inode->mtime;
  178. inode->ctime = inode->mtime;
  179. inode->nlink = 1;
  180. if (S_ISDIR (mode))
  181. extfs_make_dots (entry);
  182. return entry;
  183. }
  184. static struct entry *
  185. extfs_find_entry_int (struct entry *dir, char *name, GSList * list,
  186. gboolean make_dirs, gboolean make_file)
  187. {
  188. struct entry *pent, *pdir;
  189. char *p, *q, *name_end;
  190. char c = PATH_SEP;
  191. if (g_path_is_absolute (name))
  192. {
  193. /* Handle absolute paths */
  194. name = (char *) g_path_skip_root (name);
  195. dir = dir->inode->archive->root_entry;
  196. }
  197. pent = dir;
  198. p = name;
  199. name_end = name + strlen (name);
  200. q = strchr (p, PATH_SEP);
  201. if (q == '\0')
  202. q = strchr (p, '\0');
  203. while ((pent != NULL) && (c != '\0') && (*p != '\0'))
  204. {
  205. c = *q;
  206. *q = '\0';
  207. if (strcmp (p, ".") != 0)
  208. {
  209. if (strcmp (p, "..") == 0)
  210. pent = pent->dir;
  211. else
  212. {
  213. pent = extfs_resolve_symlinks_int (pent, list);
  214. if (pent == NULL)
  215. {
  216. *q = c;
  217. return NULL;
  218. }
  219. if (!S_ISDIR (pent->inode->mode))
  220. {
  221. *q = c;
  222. notadir = TRUE;
  223. return NULL;
  224. }
  225. pdir = pent;
  226. for (pent = pent->inode->first_in_subdir; pent != NULL; pent = pent->next_in_dir)
  227. /* Hack: I keep the original semanthic unless
  228. q+1 would break in the strchr */
  229. if (strcmp (pent->name, p) == 0)
  230. {
  231. if (q + 1 > name_end)
  232. {
  233. *q = c;
  234. notadir = !S_ISDIR (pent->inode->mode);
  235. return pent;
  236. }
  237. break;
  238. }
  239. /* When we load archive, we create automagically
  240. * non-existant directories
  241. */
  242. if (pent == NULL && make_dirs)
  243. pent = extfs_generate_entry (dir->inode->archive, p, pdir, S_IFDIR | 0777);
  244. if (pent == NULL && make_file)
  245. pent = extfs_generate_entry (dir->inode->archive, p, pdir, S_IFREG | 0666);
  246. }
  247. }
  248. /* Next iteration */
  249. *q = c;
  250. p = q + 1;
  251. q = strchr (p, PATH_SEP);
  252. if (q == '\0')
  253. q = strchr (p, '\0');
  254. }
  255. if (pent == NULL)
  256. my_errno = ENOENT;
  257. return pent;
  258. }
  259. static struct entry *
  260. extfs_find_entry (struct entry *dir, char *name, gboolean make_dirs, gboolean make_file)
  261. {
  262. struct entry *res;
  263. errloop = FALSE;
  264. notadir = FALSE;
  265. res = extfs_find_entry_int (dir, name, NULL, make_dirs, make_file);
  266. if (res == NULL)
  267. {
  268. if (errloop)
  269. my_errno = ELOOP;
  270. else if (notadir)
  271. my_errno = ENOTDIR;
  272. }
  273. return res;
  274. }
  275. static void
  276. extfs_fill_names (struct vfs_class *me, fill_names_f func)
  277. {
  278. struct archive *a = first_archive;
  279. (void) me;
  280. while (a != NULL)
  281. {
  282. extfs_plugin_info_t *info;
  283. char *name;
  284. info = &g_array_index (extfs_plugins, extfs_plugin_info_t, a->fstype);
  285. name = g_strconcat (a->name ? a->name : "", "#", info->prefix, (char *) NULL);
  286. func (name);
  287. g_free (name);
  288. a = a->next;
  289. }
  290. }
  291. static void
  292. extfs_free_archive (struct archive *archive)
  293. {
  294. extfs_free_entry (archive->root_entry);
  295. if (archive->local_name != NULL)
  296. {
  297. struct stat my;
  298. mc_stat (archive->local_name, &my);
  299. mc_ungetlocalcopy (archive->name, archive->local_name,
  300. archive->local_stat.st_mtime != my.st_mtime);
  301. g_free (archive->local_name);
  302. }
  303. g_free (archive->name);
  304. g_free (archive);
  305. }
  306. static FILE *
  307. extfs_open_archive (int fstype, const char *name, struct archive **pparc)
  308. {
  309. const extfs_plugin_info_t *info;
  310. static dev_t archive_counter = 0;
  311. FILE *result;
  312. mode_t mode;
  313. char *cmd;
  314. struct stat mystat;
  315. struct archive *current_archive;
  316. struct entry *root_entry;
  317. char *local_name = NULL, *tmp = NULL;
  318. info = &g_array_index (extfs_plugins, extfs_plugin_info_t, fstype);
  319. if (info->need_archive)
  320. {
  321. if (mc_stat (name, &mystat) == -1)
  322. return NULL;
  323. if (!vfs_file_is_local (name))
  324. {
  325. local_name = mc_getlocalcopy (name);
  326. if (local_name == NULL)
  327. return NULL;
  328. }
  329. tmp = name_quote (name, 0);
  330. }
  331. cmd = g_strconcat (info->path, info->prefix, " list ",
  332. local_name != NULL ? local_name : tmp, (char *) NULL);
  333. g_free (tmp);
  334. open_error_pipe ();
  335. result = popen (cmd, "r");
  336. g_free (cmd);
  337. if (result == NULL)
  338. {
  339. close_error_pipe (D_ERROR, NULL);
  340. if (local_name != NULL)
  341. {
  342. mc_ungetlocalcopy (name, local_name, 0);
  343. g_free (local_name);
  344. }
  345. return NULL;
  346. }
  347. #ifdef ___QNXNTO__
  348. setvbuf (result, NULL, _IONBF, 0);
  349. #endif
  350. current_archive = g_new (struct archive, 1);
  351. current_archive->fstype = fstype;
  352. current_archive->name = name ? g_strdup (name) : NULL;
  353. current_archive->local_name = local_name;
  354. if (local_name != NULL)
  355. mc_stat (local_name, &current_archive->local_stat);
  356. current_archive->inode_counter = 0;
  357. current_archive->fd_usage = 0;
  358. current_archive->rdev = archive_counter++;
  359. current_archive->next = first_archive;
  360. first_archive = current_archive;
  361. mode = mystat.st_mode & 07777;
  362. if (mode & 0400)
  363. mode |= 0100;
  364. if (mode & 0040)
  365. mode |= 0010;
  366. if (mode & 0004)
  367. mode |= 0001;
  368. mode |= S_IFDIR;
  369. root_entry = extfs_generate_entry (current_archive, PATH_SEP_STR, NULL, mode);
  370. root_entry->inode->uid = mystat.st_uid;
  371. root_entry->inode->gid = mystat.st_gid;
  372. root_entry->inode->atime = mystat.st_atime;
  373. root_entry->inode->ctime = mystat.st_ctime;
  374. root_entry->inode->mtime = mystat.st_mtime;
  375. current_archive->root_entry = root_entry;
  376. *pparc = current_archive;
  377. return result;
  378. }
  379. /*
  380. * Main loop for reading an archive.
  381. * Return 0 on success, -1 on error.
  382. */
  383. static int
  384. extfs_read_archive (int fstype, const char *name, struct archive **pparc)
  385. {
  386. FILE *extfsd;
  387. const extfs_plugin_info_t *info;
  388. char *buffer;
  389. struct archive *current_archive;
  390. char *current_file_name, *current_link_name;
  391. info = &g_array_index (extfs_plugins, extfs_plugin_info_t, fstype);
  392. extfsd = extfs_open_archive (fstype, name, &current_archive);
  393. if (extfsd == NULL)
  394. {
  395. message (D_ERROR, MSG_ERROR, _("Cannot open %s archive\n%s"), info->prefix, name);
  396. return -1;
  397. }
  398. buffer = g_malloc (BUF_4K);
  399. while (fgets (buffer, BUF_4K, extfsd) != NULL)
  400. {
  401. struct stat hstat;
  402. current_link_name = NULL;
  403. if (vfs_parse_ls_lga (buffer, &hstat, &current_file_name, &current_link_name))
  404. {
  405. struct entry *entry, *pent;
  406. struct inode *inode;
  407. char *p, *q, *cfn = current_file_name;
  408. if (*cfn != '\0')
  409. {
  410. if (*cfn == PATH_SEP)
  411. cfn++;
  412. p = strchr (cfn, '\0');
  413. if (p != cfn && *(p - 1) == PATH_SEP)
  414. *(p - 1) = '\0';
  415. p = strrchr (cfn, PATH_SEP);
  416. if (p == NULL)
  417. {
  418. p = cfn;
  419. q = strchr (cfn, '\0');
  420. }
  421. else
  422. {
  423. *(p++) = '\0';
  424. q = cfn;
  425. }
  426. if (S_ISDIR (hstat.st_mode) && (strcmp (p, ".") == 0 || strcmp (p, "..") == 0))
  427. goto read_extfs_continue;
  428. pent = extfs_find_entry (current_archive->root_entry, q, TRUE, FALSE);
  429. if (pent == NULL)
  430. {
  431. /* FIXME: Should clean everything one day */
  432. g_free (buffer);
  433. pclose (extfsd);
  434. close_error_pipe (D_ERROR, _("Inconsistent extfs archive"));
  435. return -1;
  436. }
  437. entry = g_new (struct entry, 1);
  438. entry->name = g_strdup (p);
  439. entry->next_in_dir = NULL;
  440. entry->dir = pent;
  441. if (pent->inode->last_in_subdir)
  442. {
  443. pent->inode->last_in_subdir->next_in_dir = entry;
  444. pent->inode->last_in_subdir = entry;
  445. }
  446. if (!S_ISLNK (hstat.st_mode) && (current_link_name != NULL))
  447. {
  448. pent = extfs_find_entry (current_archive->root_entry,
  449. current_link_name, FALSE, FALSE);
  450. if (pent == NULL)
  451. {
  452. /* FIXME: Should clean everything one day */
  453. g_free (buffer);
  454. pclose (extfsd);
  455. close_error_pipe (D_ERROR, _("Inconsistent extfs archive"));
  456. return -1;
  457. }
  458. entry->inode = pent->inode;
  459. pent->inode->nlink++;
  460. }
  461. else
  462. {
  463. inode = g_new (struct inode, 1);
  464. entry->inode = inode;
  465. inode->local_filename = NULL;
  466. inode->inode = (current_archive->inode_counter)++;
  467. inode->nlink = 1;
  468. inode->dev = current_archive->rdev;
  469. inode->archive = current_archive;
  470. inode->mode = hstat.st_mode;
  471. #ifdef HAVE_STRUCT_STAT_ST_RDEV
  472. inode->rdev = hstat.st_rdev;
  473. #else
  474. inode->rdev = 0;
  475. #endif
  476. inode->uid = hstat.st_uid;
  477. inode->gid = hstat.st_gid;
  478. inode->size = hstat.st_size;
  479. inode->mtime = hstat.st_mtime;
  480. inode->atime = hstat.st_atime;
  481. inode->ctime = hstat.st_ctime;
  482. inode->first_in_subdir = NULL;
  483. inode->last_in_subdir = NULL;
  484. if (current_link_name != NULL && S_ISLNK (hstat.st_mode))
  485. {
  486. inode->linkname = current_link_name;
  487. current_link_name = NULL;
  488. }
  489. else
  490. {
  491. if (S_ISLNK (hstat.st_mode))
  492. inode->mode &= ~S_IFLNK; /* You *DON'T* want to do this always */
  493. inode->linkname = NULL;
  494. }
  495. if (S_ISDIR (hstat.st_mode))
  496. extfs_make_dots (entry);
  497. }
  498. }
  499. read_extfs_continue:
  500. g_free (current_file_name);
  501. g_free (current_link_name);
  502. }
  503. }
  504. g_free (buffer);
  505. /* Check if extfs 'list' returned 0 */
  506. if (pclose (extfsd) != 0)
  507. {
  508. extfs_free (current_archive);
  509. close_error_pipe (D_ERROR, _("Inconsistent extfs archive"));
  510. return -1;
  511. }
  512. close_error_pipe (D_ERROR, NULL);
  513. *pparc = current_archive;
  514. return 0;
  515. }
  516. static int
  517. extfs_which (struct vfs_class *me, const char *path)
  518. {
  519. size_t path_len;
  520. size_t i;
  521. (void) me;
  522. path_len = strlen (path);
  523. for (i = 0; i < extfs_plugins->len; i++)
  524. {
  525. extfs_plugin_info_t *info;
  526. info = &g_array_index (extfs_plugins, extfs_plugin_info_t, i);
  527. if ((strncmp (path, info->prefix, path_len) == 0)
  528. && ((info->prefix[path_len] == '\0') || (info->prefix[path_len] == '+')))
  529. return i;
  530. }
  531. return -1;
  532. }
  533. /*
  534. * Dissect the path and create corresponding superblock. Note that inname
  535. * can be changed and the result may point inside the original string.
  536. */
  537. static char *
  538. extfs_get_path_mangle (struct vfs_class *me, char *inname, struct archive **archive,
  539. gboolean do_not_open)
  540. {
  541. char *local, *op;
  542. const char *archive_name;
  543. int result = -1;
  544. struct archive *parc;
  545. int fstype;
  546. archive_name = inname;
  547. vfs_split (inname, &local, &op);
  548. fstype = extfs_which (me, op);
  549. if (fstype == -1)
  550. return NULL;
  551. if (local == NULL)
  552. local = inname + strlen (inname);
  553. /*
  554. * All filesystems should have some local archive, at least
  555. * it can be PATH_SEP ('/').
  556. */
  557. for (parc = first_archive; parc != NULL; parc = parc->next)
  558. if (parc->name != NULL)
  559. {
  560. if (strcmp (parc->name, archive_name) == 0)
  561. {
  562. vfs_stamp (&vfs_extfs_ops, (vfsid) parc);
  563. goto return_success;
  564. }
  565. }
  566. result = do_not_open ? -1 : extfs_read_archive (fstype, archive_name, &parc);
  567. if (result == -1)
  568. ERRNOR (EIO, NULL);
  569. return_success:
  570. *archive = parc;
  571. return local;
  572. }
  573. /*
  574. * Dissect the path and create corresponding superblock.
  575. * The result should be freed.
  576. */
  577. static char *
  578. extfs_get_path (struct vfs_class *me, const char *inname,
  579. struct archive **archive, gboolean do_not_open)
  580. {
  581. char *buf, *res, *res2;
  582. buf = g_strdup (inname);
  583. res = extfs_get_path_mangle (me, buf, archive, do_not_open);
  584. res2 = g_strdup (res);
  585. g_free (buf);
  586. return res2;
  587. }
  588. /* Return allocated path (without leading slash) inside the archive */
  589. static char *
  590. extfs_get_path_from_entry (struct entry *entry)
  591. {
  592. GString *localpath;
  593. localpath = g_string_new ("");
  594. while (entry->dir != NULL)
  595. {
  596. g_string_prepend (localpath, entry->name);
  597. if (entry->dir->dir != NULL)
  598. g_string_prepend_c (localpath, PATH_SEP);
  599. entry = entry->dir;
  600. }
  601. return g_string_free (localpath, FALSE);
  602. }
  603. static struct entry *
  604. extfs_resolve_symlinks_int (struct entry *entry, GSList * list)
  605. {
  606. struct entry *pent = NULL;
  607. if (!S_ISLNK (entry->inode->mode))
  608. return entry;
  609. if (g_slist_find (list, entry) != NULL)
  610. {
  611. /* Here we protect us against symlink looping */
  612. errloop = TRUE;
  613. }
  614. else
  615. {
  616. GSList *looping;
  617. looping = g_slist_prepend (list, entry);
  618. pent = extfs_find_entry_int (entry->dir, entry->inode->linkname, looping, FALSE, FALSE);
  619. looping = g_slist_delete_link (looping, looping);
  620. if (pent == NULL)
  621. my_errno = ENOENT;
  622. }
  623. return pent;
  624. }
  625. static struct entry *
  626. extfs_resolve_symlinks (struct entry *entry)
  627. {
  628. struct entry *res;
  629. errloop = FALSE;
  630. notadir = FALSE;
  631. res = extfs_resolve_symlinks_int (entry, NULL);
  632. if (res == NULL)
  633. {
  634. if (errloop)
  635. my_errno = ELOOP;
  636. else if (notadir)
  637. my_errno = ENOTDIR;
  638. }
  639. return res;
  640. }
  641. static const char *
  642. extfs_get_archive_name (struct archive *archive)
  643. {
  644. const char *archive_name;
  645. if (archive->local_name)
  646. archive_name = archive->local_name;
  647. else
  648. archive_name = archive->name;
  649. if (!archive_name || !*archive_name)
  650. return "no_archive_name";
  651. else
  652. return archive_name;
  653. }
  654. /* Don't pass localname as NULL */
  655. static int
  656. extfs_cmd (const char *str_extfs_cmd, struct archive *archive,
  657. struct entry *entry, const char *localname)
  658. {
  659. char *file;
  660. char *quoted_file;
  661. char *quoted_localname;
  662. char *archive_name;
  663. const extfs_plugin_info_t *info;
  664. char *cmd;
  665. int retval;
  666. file = extfs_get_path_from_entry (entry);
  667. quoted_file = name_quote (file, 0);
  668. g_free (file);
  669. archive_name = name_quote (extfs_get_archive_name (archive), 0);
  670. quoted_localname = name_quote (localname, 0);
  671. info = &g_array_index (extfs_plugins, extfs_plugin_info_t, archive->fstype);
  672. cmd = g_strconcat (info->path, info->prefix, str_extfs_cmd,
  673. archive_name, " ", quoted_file, " ", quoted_localname, (char *) NULL);
  674. g_free (quoted_file);
  675. g_free (quoted_localname);
  676. g_free (archive_name);
  677. open_error_pipe ();
  678. retval = my_system (EXECUTE_AS_SHELL, shell, cmd);
  679. g_free (cmd);
  680. close_error_pipe (D_ERROR, NULL);
  681. return retval;
  682. }
  683. static void
  684. extfs_run (struct vfs_class *me, const char *file)
  685. {
  686. struct archive *archive = NULL;
  687. char *p, *q, *archive_name;
  688. char *cmd;
  689. const extfs_plugin_info_t *info;
  690. p = extfs_get_path (me, file, &archive, FALSE);
  691. if (p == NULL)
  692. return;
  693. q = name_quote (p, 0);
  694. g_free (p);
  695. archive_name = name_quote (extfs_get_archive_name (archive), 0);
  696. info = &g_array_index (extfs_plugins, extfs_plugin_info_t, archive->fstype);
  697. cmd = g_strconcat (info->path, info->prefix, " run ", archive_name, " ", q, (char *) NULL);
  698. g_free (archive_name);
  699. g_free (q);
  700. shell_execute (cmd, 0);
  701. g_free (cmd);
  702. }
  703. static void *
  704. extfs_open (struct vfs_class *me, const char *file, int flags, int mode)
  705. {
  706. struct pseudofile *extfs_info;
  707. struct archive *archive = NULL;
  708. char *q;
  709. struct entry *entry;
  710. int local_handle;
  711. gboolean created = FALSE;
  712. q = extfs_get_path (me, file, &archive, FALSE);
  713. if (q == NULL)
  714. return NULL;
  715. entry = extfs_find_entry (archive->root_entry, q, FALSE, FALSE);
  716. if ((entry == NULL) && ((flags & O_CREAT) != 0))
  717. {
  718. /* Create new entry */
  719. entry = extfs_find_entry (archive->root_entry, q, FALSE, TRUE);
  720. created = (entry != NULL);
  721. }
  722. g_free (q);
  723. if (entry == NULL)
  724. return NULL;
  725. entry = extfs_resolve_symlinks (entry);
  726. if (entry == NULL)
  727. return NULL;
  728. if (S_ISDIR (entry->inode->mode))
  729. ERRNOR (EISDIR, NULL);
  730. if (entry->inode->local_filename == NULL)
  731. {
  732. char *local_filename;
  733. local_handle = vfs_mkstemps (&local_filename, "extfs", entry->name);
  734. if (local_handle == -1)
  735. return NULL;
  736. close (local_handle);
  737. if (!created && ((flags & O_TRUNC) == 0)
  738. && extfs_cmd (" copyout ", archive, entry, local_filename))
  739. {
  740. unlink (local_filename);
  741. g_free (local_filename);
  742. my_errno = EIO;
  743. return NULL;
  744. }
  745. entry->inode->local_filename = local_filename;
  746. }
  747. local_handle = open (entry->inode->local_filename, NO_LINEAR (flags), mode);
  748. if (local_handle == -1)
  749. {
  750. /* file exists(may be). Need to drop O_CREAT flag and truncate file content */
  751. flags = ~O_CREAT & (NO_LINEAR (flags) | O_TRUNC);
  752. local_handle = open (entry->inode->local_filename, flags, mode);
  753. }
  754. if (local_handle == -1)
  755. ERRNOR (EIO, NULL);
  756. extfs_info = g_new (struct pseudofile, 1);
  757. extfs_info->archive = archive;
  758. extfs_info->entry = entry;
  759. extfs_info->has_changed = created;
  760. extfs_info->local_handle = local_handle;
  761. /* i.e. we had no open files and now we have one */
  762. vfs_rmstamp (&vfs_extfs_ops, (vfsid) archive);
  763. archive->fd_usage++;
  764. return extfs_info;
  765. }
  766. static ssize_t
  767. extfs_read (void *data, char *buffer, int count)
  768. {
  769. struct pseudofile *file = (struct pseudofile *) data;
  770. return read (file->local_handle, buffer, count);
  771. }
  772. static int
  773. extfs_close (void *data)
  774. {
  775. struct pseudofile *file;
  776. int errno_code = 0;
  777. file = (struct pseudofile *) data;
  778. close (file->local_handle);
  779. /* Commit the file if it has changed */
  780. if (file->has_changed)
  781. {
  782. struct stat file_status;
  783. if (extfs_cmd (" copyin ", file->archive, file->entry, file->entry->inode->local_filename))
  784. errno_code = EIO;
  785. if (stat (file->entry->inode->local_filename, &file_status) != 0)
  786. errno_code = EIO;
  787. else
  788. file->entry->inode->size = file_status.st_size;
  789. file->entry->inode->mtime = time (NULL);
  790. }
  791. if (--file->archive->fd_usage == 0)
  792. vfs_stamp_create (&vfs_extfs_ops, file->archive);
  793. g_free (data);
  794. if (errno_code != 0)
  795. ERRNOR (EIO, -1);
  796. return 0;
  797. }
  798. static int
  799. extfs_errno (struct vfs_class *me)
  800. {
  801. (void) me;
  802. return my_errno;
  803. }
  804. static void *
  805. extfs_opendir (struct vfs_class *me, const char *dirname)
  806. {
  807. struct archive *archive = NULL;
  808. char *q;
  809. struct entry *entry;
  810. struct entry **info;
  811. q = extfs_get_path (me, dirname, &archive, FALSE);
  812. if (q == NULL)
  813. return NULL;
  814. entry = extfs_find_entry (archive->root_entry, q, FALSE, FALSE);
  815. g_free (q);
  816. if (entry == NULL)
  817. return NULL;
  818. entry = extfs_resolve_symlinks (entry);
  819. if (entry == NULL)
  820. return NULL;
  821. if (!S_ISDIR (entry->inode->mode))
  822. ERRNOR (ENOTDIR, NULL);
  823. info = g_new (struct entry *, 2);
  824. info[0] = entry->inode->first_in_subdir;
  825. info[1] = entry->inode->first_in_subdir;
  826. return info;
  827. }
  828. static void *
  829. extfs_readdir (void *data)
  830. {
  831. static union vfs_dirent dir;
  832. struct entry **info = (struct entry **) data;
  833. if (*info == NULL)
  834. return NULL;
  835. g_strlcpy (dir.dent.d_name, (*info)->name, MC_MAXPATHLEN);
  836. compute_namelen (&dir.dent);
  837. *info = (*info)->next_in_dir;
  838. return (void *) &dir;
  839. }
  840. static int
  841. extfs_closedir (void *data)
  842. {
  843. g_free (data);
  844. return 0;
  845. }
  846. #define RECORDSIZE 512
  847. static void
  848. extfs_stat_move (struct stat *buf, const struct inode *inode)
  849. {
  850. buf->st_dev = inode->dev;
  851. buf->st_ino = inode->inode;
  852. buf->st_mode = inode->mode;
  853. buf->st_nlink = inode->nlink;
  854. buf->st_uid = inode->uid;
  855. buf->st_gid = inode->gid;
  856. #ifdef HAVE_STRUCT_STAT_ST_RDEV
  857. buf->st_rdev = inode->rdev;
  858. #endif
  859. buf->st_size = inode->size;
  860. #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
  861. buf->st_blksize = RECORDSIZE;
  862. #endif
  863. #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
  864. buf->st_blocks = (inode->size + RECORDSIZE - 1) / RECORDSIZE;
  865. #endif
  866. buf->st_atime = inode->atime;
  867. buf->st_mtime = inode->mtime;
  868. buf->st_ctime = inode->ctime;
  869. }
  870. static int
  871. extfs_internal_stat (struct vfs_class *me, const char *path, struct stat *buf, gboolean resolve)
  872. {
  873. struct archive *archive;
  874. char *q, *mpath;
  875. struct entry *entry;
  876. int result = -1;
  877. mpath = g_strdup (path);
  878. q = extfs_get_path_mangle (me, mpath, &archive, FALSE);
  879. if (q == NULL)
  880. goto cleanup;
  881. entry = extfs_find_entry (archive->root_entry, q, FALSE, FALSE);
  882. if (entry == NULL)
  883. goto cleanup;
  884. if (resolve)
  885. {
  886. entry = extfs_resolve_symlinks (entry);
  887. if (entry == NULL)
  888. goto cleanup;
  889. }
  890. extfs_stat_move (buf, entry->inode);
  891. result = 0;
  892. cleanup:
  893. g_free (mpath);
  894. return result;
  895. }
  896. static int
  897. extfs_stat (struct vfs_class *me, const char *path, struct stat *buf)
  898. {
  899. return extfs_internal_stat (me, path, buf, TRUE);
  900. }
  901. static int
  902. extfs_lstat (struct vfs_class *me, const char *path, struct stat *buf)
  903. {
  904. return extfs_internal_stat (me, path, buf, FALSE);
  905. }
  906. static int
  907. extfs_fstat (void *data, struct stat *buf)
  908. {
  909. struct pseudofile *file = (struct pseudofile *) data;
  910. extfs_stat_move (buf, file->entry->inode);
  911. return 0;
  912. }
  913. static int
  914. extfs_readlink (struct vfs_class *me, const char *path, char *buf, size_t size)
  915. {
  916. struct archive *archive;
  917. char *q, *mpath;
  918. size_t len;
  919. struct entry *entry;
  920. int result = -1;
  921. mpath = g_strdup (path);
  922. q = extfs_get_path_mangle (me, mpath, &archive, FALSE);
  923. if (q == NULL)
  924. goto cleanup;
  925. entry = extfs_find_entry (archive->root_entry, q, FALSE, FALSE);
  926. if (entry == NULL)
  927. goto cleanup;
  928. if (!S_ISLNK (entry->inode->mode))
  929. {
  930. me->verrno = EINVAL;
  931. goto cleanup;
  932. }
  933. len = strlen (entry->inode->linkname);
  934. if (size < len)
  935. len = size;
  936. /* readlink() does not append a NUL character to buf */
  937. result = len;
  938. memcpy (buf, entry->inode->linkname, result);
  939. cleanup:
  940. g_free (mpath);
  941. return result;
  942. }
  943. static int
  944. extfs_chown (struct vfs_class *me, const char *path, int owner, int group)
  945. {
  946. (void) me;
  947. (void) path;
  948. (void) owner;
  949. (void) group;
  950. return 0;
  951. }
  952. static int
  953. extfs_chmod (struct vfs_class *me, const char *path, int mode)
  954. {
  955. (void) me;
  956. (void) path;
  957. (void) mode;
  958. return 0;
  959. }
  960. static ssize_t
  961. extfs_write (void *data, const char *buf, int nbyte)
  962. {
  963. struct pseudofile *file = (struct pseudofile *) data;
  964. file->has_changed = TRUE;
  965. return write (file->local_handle, buf, nbyte);
  966. }
  967. static int
  968. extfs_unlink (struct vfs_class *me, const char *file)
  969. {
  970. struct archive *archive;
  971. char *q, *mpath;
  972. struct entry *entry;
  973. int result = -1;
  974. mpath = g_strdup (file);
  975. q = extfs_get_path_mangle (me, mpath, &archive, FALSE);
  976. if (q == NULL)
  977. goto cleanup;
  978. entry = extfs_find_entry (archive->root_entry, q, FALSE, FALSE);
  979. if (entry == NULL)
  980. goto cleanup;
  981. entry = extfs_resolve_symlinks (entry);
  982. if (entry == NULL)
  983. goto cleanup;
  984. if (S_ISDIR (entry->inode->mode))
  985. {
  986. me->verrno = EISDIR;
  987. goto cleanup;
  988. }
  989. if (extfs_cmd (" rm ", archive, entry, ""))
  990. {
  991. my_errno = EIO;
  992. goto cleanup;
  993. }
  994. extfs_remove_entry (entry);
  995. result = 0;
  996. cleanup:
  997. g_free (mpath);
  998. return result;
  999. }
  1000. static int
  1001. extfs_mkdir (struct vfs_class *me, const char *path, mode_t mode)
  1002. {
  1003. struct archive *archive;
  1004. char *q, *mpath;
  1005. struct entry *entry;
  1006. int result = -1;
  1007. (void) mode;
  1008. mpath = g_strdup (path);
  1009. q = extfs_get_path_mangle (me, mpath, &archive, FALSE);
  1010. if (q == NULL)
  1011. goto cleanup;
  1012. entry = extfs_find_entry (archive->root_entry, q, FALSE, FALSE);
  1013. if (entry != NULL)
  1014. {
  1015. me->verrno = EEXIST;
  1016. goto cleanup;
  1017. }
  1018. entry = extfs_find_entry (archive->root_entry, q, TRUE, FALSE);
  1019. if (entry == NULL)
  1020. goto cleanup;
  1021. entry = extfs_resolve_symlinks (entry);
  1022. if (entry == NULL)
  1023. goto cleanup;
  1024. if (!S_ISDIR (entry->inode->mode))
  1025. {
  1026. me->verrno = ENOTDIR;
  1027. goto cleanup;
  1028. }
  1029. if (extfs_cmd (" mkdir ", archive, entry, ""))
  1030. {
  1031. my_errno = EIO;
  1032. extfs_remove_entry (entry);
  1033. goto cleanup;
  1034. }
  1035. result = 0;
  1036. cleanup:
  1037. g_free (mpath);
  1038. return result;
  1039. }
  1040. static int
  1041. extfs_rmdir (struct vfs_class *me, const char *path)
  1042. {
  1043. struct archive *archive;
  1044. char *q, *mpath;
  1045. struct entry *entry;
  1046. int result = -1;
  1047. mpath = g_strdup (path);
  1048. q = extfs_get_path_mangle (me, mpath, &archive, FALSE);
  1049. if (q == NULL)
  1050. goto cleanup;
  1051. entry = extfs_find_entry (archive->root_entry, q, FALSE, FALSE);
  1052. if (entry == NULL)
  1053. goto cleanup;
  1054. entry = extfs_resolve_symlinks (entry);
  1055. if (entry == NULL)
  1056. goto cleanup;
  1057. if (!S_ISDIR (entry->inode->mode))
  1058. {
  1059. me->verrno = ENOTDIR;
  1060. goto cleanup;
  1061. }
  1062. if (extfs_cmd (" rmdir ", archive, entry, ""))
  1063. {
  1064. my_errno = EIO;
  1065. goto cleanup;
  1066. }
  1067. extfs_remove_entry (entry);
  1068. result = 0;
  1069. cleanup:
  1070. g_free (mpath);
  1071. return result;
  1072. }
  1073. static int
  1074. extfs_chdir (struct vfs_class *me, const char *path)
  1075. {
  1076. struct archive *archive = NULL;
  1077. char *q;
  1078. struct entry *entry;
  1079. my_errno = ENOTDIR;
  1080. q = extfs_get_path (me, path, &archive, FALSE);
  1081. if (q == NULL)
  1082. return -1;
  1083. entry = extfs_find_entry (archive->root_entry, q, FALSE, FALSE);
  1084. g_free (q);
  1085. if (entry == NULL)
  1086. return -1;
  1087. entry = extfs_resolve_symlinks (entry);
  1088. if ((entry == NULL) || (!S_ISDIR (entry->inode->mode)))
  1089. return -1;
  1090. my_errno = 0;
  1091. return 0;
  1092. }
  1093. static off_t
  1094. extfs_lseek (void *data, off_t offset, int whence)
  1095. {
  1096. struct pseudofile *file = (struct pseudofile *) data;
  1097. return lseek (file->local_handle, offset, whence);
  1098. }
  1099. static vfsid
  1100. extfs_getid (struct vfs_class *me, const char *path)
  1101. {
  1102. struct archive *archive = NULL;
  1103. char *p;
  1104. p = extfs_get_path (me, path, &archive, TRUE);
  1105. if (p == NULL)
  1106. return NULL;
  1107. g_free (p);
  1108. return (vfsid) archive;
  1109. }
  1110. static int
  1111. extfs_nothingisopen (vfsid id)
  1112. {
  1113. return (((struct archive *) id)->fd_usage <= 0);
  1114. }
  1115. static void
  1116. extfs_remove_entry (struct entry *e)
  1117. {
  1118. int i = --e->inode->nlink;
  1119. struct entry *pe, *ent, *prev;
  1120. if (S_ISDIR (e->inode->mode) && e->inode->first_in_subdir != NULL)
  1121. {
  1122. struct entry *f = e->inode->first_in_subdir;
  1123. e->inode->first_in_subdir = NULL;
  1124. extfs_remove_entry (f);
  1125. }
  1126. pe = e->dir;
  1127. if (e == pe->inode->first_in_subdir)
  1128. pe->inode->first_in_subdir = e->next_in_dir;
  1129. prev = NULL;
  1130. for (ent = pe->inode->first_in_subdir; ent && ent->next_in_dir; ent = ent->next_in_dir)
  1131. if (e == ent->next_in_dir)
  1132. {
  1133. prev = ent;
  1134. break;
  1135. }
  1136. if (prev)
  1137. prev->next_in_dir = e->next_in_dir;
  1138. if (e == pe->inode->last_in_subdir)
  1139. pe->inode->last_in_subdir = prev;
  1140. if (i <= 0)
  1141. {
  1142. if (e->inode->local_filename != NULL)
  1143. {
  1144. unlink (e->inode->local_filename);
  1145. g_free (e->inode->local_filename);
  1146. }
  1147. g_free (e->inode->linkname);
  1148. g_free (e->inode);
  1149. }
  1150. g_free (e->name);
  1151. g_free (e);
  1152. }
  1153. static void
  1154. extfs_free_entry (struct entry *e)
  1155. {
  1156. int i = --e->inode->nlink;
  1157. if (S_ISDIR (e->inode->mode) && e->inode->first_in_subdir != NULL)
  1158. {
  1159. struct entry *f = e->inode->first_in_subdir;
  1160. e->inode->first_in_subdir = NULL;
  1161. extfs_free_entry (f);
  1162. }
  1163. if (i <= 0)
  1164. {
  1165. if (e->inode->local_filename != NULL)
  1166. {
  1167. unlink (e->inode->local_filename);
  1168. g_free (e->inode->local_filename);
  1169. }
  1170. g_free (e->inode->linkname);
  1171. g_free (e->inode);
  1172. }
  1173. if (e->next_in_dir != NULL)
  1174. extfs_free_entry (e->next_in_dir);
  1175. g_free (e->name);
  1176. g_free (e);
  1177. }
  1178. static void
  1179. extfs_free (vfsid id)
  1180. {
  1181. struct archive *archive = (struct archive *) id;
  1182. if (archive == first_archive)
  1183. {
  1184. first_archive = archive->next;
  1185. }
  1186. else
  1187. {
  1188. struct archive *parc;
  1189. for (parc = first_archive; parc != NULL; parc = parc->next)
  1190. if (parc->next == archive)
  1191. {
  1192. parc->next = archive->next;
  1193. break;
  1194. }
  1195. }
  1196. extfs_free_archive (archive);
  1197. }
  1198. static char *
  1199. extfs_getlocalcopy (struct vfs_class *me, const char *path)
  1200. {
  1201. struct pseudofile *fp;
  1202. char *p;
  1203. fp = (struct pseudofile *) extfs_open (me, path, O_RDONLY, 0);
  1204. if (fp == NULL)
  1205. return NULL;
  1206. if (fp->entry->inode->local_filename == NULL)
  1207. {
  1208. extfs_close ((void *) fp);
  1209. return NULL;
  1210. }
  1211. p = g_strdup (fp->entry->inode->local_filename);
  1212. fp->archive->fd_usage++;
  1213. extfs_close ((void *) fp);
  1214. return p;
  1215. }
  1216. static int
  1217. extfs_ungetlocalcopy (struct vfs_class *me, const char *path, const char *local, int has_changed)
  1218. {
  1219. struct pseudofile *fp;
  1220. fp = (struct pseudofile *) extfs_open (me, path, O_RDONLY, 0);
  1221. if (fp == NULL)
  1222. return 0;
  1223. if (strcmp (fp->entry->inode->local_filename, local) == 0)
  1224. {
  1225. fp->archive->fd_usage--;
  1226. if (has_changed != 0)
  1227. fp->has_changed = TRUE;
  1228. extfs_close ((void *) fp);
  1229. return 0;
  1230. }
  1231. else
  1232. {
  1233. /* Should not happen */
  1234. extfs_close ((void *) fp);
  1235. return 0;
  1236. }
  1237. }
  1238. static gboolean
  1239. extfs_get_plugins (const char *where, gboolean silent)
  1240. {
  1241. char *dirname;
  1242. GDir *dir;
  1243. const char *filename;
  1244. dirname = g_build_path (PATH_SEP_STR, where, MC_EXTFS_DIR, (char *) NULL);
  1245. dir = g_dir_open (dirname, 0, NULL);
  1246. /* We may not use vfs_die() message or message or similar,
  1247. * UI is not initialized at this time and message would not
  1248. * appear on screen. */
  1249. if (dir == NULL)
  1250. {
  1251. if (!silent)
  1252. fprintf (stderr, _("Warning: cannot open %s directory\n"), dirname);
  1253. g_free (dirname);
  1254. return FALSE;
  1255. }
  1256. if (extfs_plugins == NULL)
  1257. extfs_plugins = g_array_sized_new (FALSE, TRUE, sizeof (extfs_plugin_info_t), 32);
  1258. while ((filename = g_dir_read_name (dir)) != NULL)
  1259. {
  1260. char fullname[MC_MAXPATHLEN];
  1261. struct stat s;
  1262. g_snprintf (fullname, sizeof (fullname), "%s" PATH_SEP_STR "%s", dirname, filename);
  1263. if ((stat (fullname, &s) == 0)
  1264. && S_ISREG (s.st_mode) && !S_ISDIR (s.st_mode)
  1265. && (((s.st_mode & S_IXOTH) != 0) ||
  1266. ((s.st_mode & S_IXUSR) != 0) || ((s.st_mode & S_IXGRP) != 0)))
  1267. {
  1268. int f;
  1269. f = open (fullname, O_RDONLY);
  1270. if (f > 0)
  1271. {
  1272. size_t len, i;
  1273. extfs_plugin_info_t info;
  1274. gboolean found = FALSE;
  1275. close (f);
  1276. /* Handle those with a trailing '+', those flag that the
  1277. * file system does not require an archive to work
  1278. */
  1279. len = strlen (filename);
  1280. info.need_archive = (filename[len - 1] != '+');
  1281. info.path = g_strconcat (dirname, PATH_SEP_STR, (char *) NULL);
  1282. info.prefix = g_strdup (filename);
  1283. /* prepare to compare file names without trailing '+' */
  1284. if (!info.need_archive)
  1285. info.prefix[len - 1] = '\0';
  1286. /* don't overload already found plugin */
  1287. for (i = 0; i < extfs_plugins->len; i++)
  1288. {
  1289. extfs_plugin_info_t *p;
  1290. p = &g_array_index (extfs_plugins, extfs_plugin_info_t, i);
  1291. /* 2 files with same names cannot be in a directory */
  1292. if ((strcmp (info.path, p->path) != 0)
  1293. && (strcmp (info.prefix, p->prefix) == 0))
  1294. {
  1295. found = TRUE;
  1296. break;
  1297. }
  1298. }
  1299. if (found)
  1300. {
  1301. g_free (info.path);
  1302. g_free (info.prefix);
  1303. }
  1304. else
  1305. {
  1306. /* restore file name */
  1307. if (!info.need_archive)
  1308. info.prefix[len - 1] = '+';
  1309. g_array_append_val (extfs_plugins, info);
  1310. }
  1311. }
  1312. }
  1313. }
  1314. g_dir_close (dir);
  1315. g_free (dirname);
  1316. return TRUE;
  1317. }
  1318. static int
  1319. extfs_init (struct vfs_class *me)
  1320. {
  1321. gboolean d1, d2;
  1322. char *dirname;
  1323. (void) me;
  1324. /* 1st: scan user directory */
  1325. dirname = g_build_path (PATH_SEP_STR, home_dir, MC_USERCONF_DIR, (char *) NULL);
  1326. d1 = extfs_get_plugins (dirname, TRUE); /* silent about user dir */
  1327. g_free (dirname);
  1328. /* 2nd: scan system dir */
  1329. d2 = extfs_get_plugins (LIBEXECDIR, d1);
  1330. return (d1 || d2 ? 1 : 0);
  1331. }
  1332. static void
  1333. extfs_done (struct vfs_class *me)
  1334. {
  1335. size_t i;
  1336. struct archive *ar;
  1337. (void) me;
  1338. for (ar = first_archive; ar != NULL;)
  1339. {
  1340. extfs_free ((vfsid) ar);
  1341. ar = first_archive;
  1342. }
  1343. for (i = 0; i < extfs_plugins->len; i++)
  1344. {
  1345. extfs_plugin_info_t *info;
  1346. info = &g_array_index (extfs_plugins, extfs_plugin_info_t, i);
  1347. g_free (info->path);
  1348. g_free (info->prefix);
  1349. }
  1350. g_array_free (extfs_plugins, TRUE);
  1351. }
  1352. static int
  1353. extfs_setctl (struct vfs_class *me, const char *path, int ctlop, void *arg)
  1354. {
  1355. (void) arg;
  1356. if (ctlop == VFS_SETCTL_RUN)
  1357. {
  1358. extfs_run (me, path);
  1359. return 1;
  1360. }
  1361. return 0;
  1362. }
  1363. void
  1364. init_extfs (void)
  1365. {
  1366. vfs_extfs_ops.name = "extfs";
  1367. vfs_extfs_ops.init = extfs_init;
  1368. vfs_extfs_ops.done = extfs_done;
  1369. vfs_extfs_ops.fill_names = extfs_fill_names;
  1370. vfs_extfs_ops.which = extfs_which;
  1371. vfs_extfs_ops.open = extfs_open;
  1372. vfs_extfs_ops.close = extfs_close;
  1373. vfs_extfs_ops.read = extfs_read;
  1374. vfs_extfs_ops.write = extfs_write;
  1375. vfs_extfs_ops.opendir = extfs_opendir;
  1376. vfs_extfs_ops.readdir = extfs_readdir;
  1377. vfs_extfs_ops.closedir = extfs_closedir;
  1378. vfs_extfs_ops.stat = extfs_stat;
  1379. vfs_extfs_ops.lstat = extfs_lstat;
  1380. vfs_extfs_ops.fstat = extfs_fstat;
  1381. vfs_extfs_ops.chmod = extfs_chmod;
  1382. vfs_extfs_ops.chown = extfs_chown;
  1383. vfs_extfs_ops.readlink = extfs_readlink;
  1384. vfs_extfs_ops.unlink = extfs_unlink;
  1385. vfs_extfs_ops.chdir = extfs_chdir;
  1386. vfs_extfs_ops.ferrno = extfs_errno;
  1387. vfs_extfs_ops.lseek = extfs_lseek;
  1388. vfs_extfs_ops.getid = extfs_getid;
  1389. vfs_extfs_ops.nothingisopen = extfs_nothingisopen;
  1390. vfs_extfs_ops.free = extfs_free;
  1391. vfs_extfs_ops.getlocalcopy = extfs_getlocalcopy;
  1392. vfs_extfs_ops.ungetlocalcopy = extfs_ungetlocalcopy;
  1393. vfs_extfs_ops.mkdir = extfs_mkdir;
  1394. vfs_extfs_ops.rmdir = extfs_rmdir;
  1395. vfs_extfs_ops.setctl = extfs_setctl;
  1396. vfs_register_class (&vfs_extfs_ops);
  1397. }