extfs.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019
  1. /* Virtual File System: External file system.
  2. Copyright (C) 1995 The Free Software Foundation
  3. Written by: 1995 Jakub Jelinek
  4. Rewritten by: 1998 Pavel Machek
  5. This program is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Library General Public License
  7. as published by the Free Software Foundation; either version 2 of
  8. the License, or (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU Library General Public License for more details.
  13. You should have received a copy of the GNU Library General Public
  14. License along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
  16. /* Namespace: exports only vfs_extfs_ops */
  17. #include <config.h>
  18. #include <stdio.h>
  19. #include <ctype.h>
  20. #include <string.h>
  21. #include <stdlib.h>
  22. #include <sys/types.h>
  23. #include <sys/stat.h>
  24. #include <fcntl.h>
  25. #include <unistd.h>
  26. #include <signal.h>
  27. #ifdef HAVE_SYS_WAIT_H
  28. #include <sys/wait.h>
  29. #endif
  30. #include <errno.h>
  31. #ifdef SCO_FLAVOR
  32. #include <sys/timeb.h> /* alex: for struct timeb definition */
  33. #endif /* SCO_FLAVOR */
  34. #include <time.h>
  35. #include "../src/fs.h"
  36. #include "../src/util.h"
  37. #include "../src/dialog.h"
  38. #include "../src/mem.h"
  39. #include "../src/mad.h"
  40. #include "../src/main.h" /* For shell_execute */
  41. #include "vfs.h"
  42. #include "extfs.h"
  43. #define ERRNOR(x,y) do { my_errno = x; return y; } while(0)
  44. static struct entry *
  45. find_entry (struct entry *dir, char *name, int make_dirs, int make_file);
  46. static int extfs_which (vfs *me, char *path);
  47. static struct archive *first_archive = NULL;
  48. static int my_errno = 0;
  49. static struct stat hstat; /* Stat struct corresponding */
  50. #define MAXEXTFS 32
  51. static char *extfs_prefixes [MAXEXTFS];
  52. static char extfs_need_archive [MAXEXTFS];
  53. static int extfs_no = 0;
  54. static void extfs_fill_names (vfs *me, void (*func)(char *))
  55. {
  56. struct archive *a = first_archive;
  57. char *name;
  58. while (a){
  59. name = copy_strings (extfs_prefixes [a->fstype], "#",
  60. (a->name ? a->name : ""), "/",
  61. a->current_dir->name, 0);
  62. (*func)(name);
  63. free (name);
  64. a = a->next;
  65. }
  66. }
  67. static void make_dot_doubledot (struct entry *ent)
  68. {
  69. struct entry *entry = (struct entry *)
  70. xmalloc (sizeof (struct entry), "Extfs: entry");
  71. struct entry *parentry = ent->dir;
  72. struct inode *inode = ent->inode, *parent;
  73. parent = (parentry != NULL) ? parentry->inode : NULL;
  74. entry->name = strdup (".");
  75. entry->has_changed = 0;
  76. entry->inode = inode;
  77. entry->dir = ent;
  78. inode->local_filename = NULL;
  79. inode->first_in_subdir = entry;
  80. inode->last_in_subdir = entry;
  81. inode->nlink++;
  82. entry->next_in_dir = (struct entry *)
  83. xmalloc (sizeof (struct entry), "Extfs: entry");
  84. entry=entry->next_in_dir;
  85. entry->name = strdup ("..");
  86. entry->has_changed = 0;
  87. inode->last_in_subdir = entry;
  88. entry->next_in_dir = NULL;
  89. if (parent != NULL) {
  90. entry->inode = parent;
  91. entry->dir = parentry;
  92. parent->nlink++;
  93. } else {
  94. entry->inode = inode;
  95. entry->dir = ent;
  96. inode->nlink++;
  97. }
  98. }
  99. static struct entry *generate_entry (struct archive *archive,
  100. char *name, struct entry *parentry, mode_t mode)
  101. {
  102. mode_t myumask;
  103. struct inode *inode, *parent;
  104. struct entry *entry;
  105. parent = (parentry != NULL) ? parentry->inode : NULL;
  106. entry = (struct entry *)
  107. xmalloc (sizeof (struct entry), "Extfs: entry");
  108. entry->name = strdup (name);
  109. entry->has_changed = 0;
  110. entry->next_in_dir = NULL;
  111. entry->dir = parentry;
  112. if (parent != NULL) {
  113. parent->last_in_subdir->next_in_dir = entry;
  114. parent->last_in_subdir = entry;
  115. }
  116. inode = (struct inode *)
  117. xmalloc (sizeof (struct inode), "Extfs: inode");
  118. entry->inode = inode;
  119. inode->has_changed = 0;
  120. inode->local_filename = NULL;
  121. inode->linkname = 0;
  122. inode->inode = (archive->__inode_counter)++;
  123. inode->dev = archive->rdev;
  124. inode->archive = archive;
  125. myumask = umask (022);
  126. umask (myumask);
  127. inode->mode = mode & ~myumask;
  128. mode = inode->mode;
  129. inode->rdev = 0;
  130. inode->uid = getuid ();
  131. inode->gid = getgid ();
  132. inode->size = 0;
  133. inode->mtime = time (NULL);
  134. inode->atime = inode->mtime;
  135. inode->ctime = inode->mtime;
  136. inode->nlink = 1;
  137. if (S_ISDIR (mode))
  138. make_dot_doubledot (entry);
  139. return entry;
  140. }
  141. static void free_entries (struct entry *entry)
  142. {
  143. return;
  144. }
  145. static void free_archive (struct archive *archive)
  146. {
  147. free_entries (archive->root_entry);
  148. if (archive->local_name != NULL) {
  149. struct stat my;
  150. mc_stat (archive->local_name, &my);
  151. mc_ungetlocalcopy (archive->name, archive->local_name,
  152. archive->local_stat.st_mtime != my.st_mtime);
  153. /* ungetlocalcopy frees local_name for us */
  154. }
  155. if (archive->name)
  156. free (archive->name);
  157. free (archive);
  158. }
  159. static FILE *open_archive (int fstype, char *name, struct archive **pparc)
  160. {
  161. static dev_t __extfs_no = 0;
  162. FILE *result;
  163. mode_t mode;
  164. char *cmd;
  165. char *mc_extfsdir;
  166. struct stat mystat;
  167. struct archive *current_archive;
  168. struct entry *root_entry;
  169. char *local_name = NULL, *tmp = 0;
  170. int uses_archive = extfs_need_archive [fstype];
  171. if (uses_archive){
  172. if (mc_stat (name, &mystat) == -1)
  173. return NULL;
  174. if (!vfs_file_is_local (name)) {
  175. local_name = mc_getlocalcopy (name);
  176. if (local_name == NULL)
  177. return NULL;
  178. }
  179. tmp = name_quote (name, 0);
  180. }
  181. #if 0
  182. /* Sorry, what is this good for? */
  183. if (uses_archive == EFS_NEED_ARG){
  184. tmp = name_quote (name, 0);
  185. }
  186. #endif
  187. mc_extfsdir = concat_dir_and_file (mc_home, "extfs/");
  188. cmd = copy_strings (mc_extfsdir, extfs_prefixes [fstype],
  189. " list ", local_name ? local_name : tmp, 0);
  190. if (tmp)
  191. free (tmp);
  192. result = popen (cmd, "r");
  193. free (cmd);
  194. free (mc_extfsdir);
  195. if (result == NULL) {
  196. if (local_name != NULL && uses_archive)
  197. mc_ungetlocalcopy (name, local_name, 0);
  198. return NULL;
  199. }
  200. current_archive = (struct archive *)
  201. xmalloc (sizeof (struct archive), "Extfs archive");
  202. current_archive->fstype = fstype;
  203. current_archive->name = name ? strdup (name): name;
  204. current_archive->local_name = local_name;
  205. if (local_name != NULL)
  206. mc_stat (local_name, &current_archive->local_stat);
  207. current_archive->__inode_counter = 0;
  208. current_archive->fd_usage = 0;
  209. current_archive->extfsstat = mystat;
  210. current_archive->rdev = __extfs_no++;
  211. current_archive->next = first_archive;
  212. first_archive = current_archive;
  213. mode = current_archive->extfsstat.st_mode & 07777;
  214. if (mode & 0400)
  215. mode |= 0100;
  216. if (mode & 0040)
  217. mode |= 0010;
  218. if (mode & 0004)
  219. mode |= 0001;
  220. mode |= S_IFDIR;
  221. root_entry = generate_entry (current_archive, "/", NULL, mode);
  222. root_entry->inode->uid = current_archive->extfsstat.st_uid;
  223. root_entry->inode->gid = current_archive->extfsstat.st_gid;
  224. root_entry->inode->atime = current_archive->extfsstat.st_atime;
  225. root_entry->inode->ctime = current_archive->extfsstat.st_ctime;
  226. root_entry->inode->mtime = current_archive->extfsstat.st_mtime;
  227. current_archive->root_entry = root_entry;
  228. current_archive->current_dir = root_entry;
  229. *pparc = current_archive;
  230. return result;
  231. }
  232. /*
  233. * Main loop for reading an archive.
  234. * Returns 0 on success, -1 on error.
  235. */
  236. static int read_archive (int fstype, char *name, struct archive **pparc)
  237. {
  238. FILE *extfsd;
  239. char *buffer;
  240. struct archive *current_archive;
  241. char *current_file_name, *current_link_name;
  242. if ((extfsd = open_archive (fstype, name, &current_archive)) == NULL) {
  243. message_3s (1, MSG_ERROR, _("Couldn't open %s archive\n%s"),
  244. extfs_prefixes [fstype], name);
  245. return -1;
  246. }
  247. buffer = xmalloc (4096, "Extfs: buffer");
  248. while (fgets (buffer, 4096, extfsd) != NULL) {
  249. current_link_name = NULL;
  250. if (vfs_parse_ls_lga (buffer, &hstat, &current_file_name, &current_link_name)) {
  251. struct entry *entry, *pent;
  252. struct inode *inode;
  253. char *p, *q, *cfn = current_file_name;
  254. if (*cfn) {
  255. if (*cfn == '/')
  256. cfn++;
  257. p = strchr (cfn, 0);
  258. if (p != NULL && p != cfn && *(p - 1) == '/')
  259. *(p - 1) = 0;
  260. p = strrchr (cfn, '/');
  261. if (p == NULL) {
  262. p = cfn;
  263. q = strchr (cfn, 0);
  264. } else {
  265. *(p++) = 0;
  266. q = cfn;
  267. }
  268. if (S_ISDIR (hstat.st_mode) &&
  269. (!strcmp (p, ".") || !strcmp (p, "..")))
  270. goto read_extfs_continue;
  271. pent = find_entry (current_archive->root_entry, q, 1, 0) ;
  272. if (pent == NULL) {
  273. message_1s (1, MSG_ERROR, _("Inconsistent extfs archive"));
  274. /* FIXME: Should clean everything one day */
  275. free (buffer);
  276. pclose (extfsd);
  277. return -1;
  278. }
  279. entry = (struct entry *) xmalloc (sizeof (struct entry), "Extfs: entry");
  280. entry->name = strdup (p);
  281. entry->has_changed = 0;
  282. entry->next_in_dir = NULL;
  283. entry->dir = pent;
  284. if (pent != NULL) {
  285. if (pent->inode->last_in_subdir){
  286. pent->inode->last_in_subdir->next_in_dir = entry;
  287. pent->inode->last_in_subdir = entry;
  288. }
  289. }
  290. if (!S_ISLNK (hstat.st_mode) && current_link_name != NULL) {
  291. pent = find_entry (current_archive->root_entry, current_link_name, 0, 0);
  292. if (pent == NULL) {
  293. message_1s (1, MSG_ERROR, _("Inconsistent extfs archive"));
  294. /* FIXME: Should clean everything one day */
  295. free (buffer);
  296. pclose (extfsd);
  297. return -1;
  298. } else {
  299. entry->inode = pent->inode;
  300. pent->inode->nlink++;
  301. }
  302. } else {
  303. inode = (struct inode *) xmalloc (sizeof (struct inode), "Extfs: inode");
  304. entry->inode = inode;
  305. inode->local_filename = NULL;
  306. inode->has_changed = 0;
  307. inode->inode = (current_archive->__inode_counter)++;
  308. inode->nlink = 1;
  309. inode->dev = current_archive->rdev;
  310. inode->archive = current_archive;
  311. inode->mode = hstat.st_mode;
  312. #ifdef HAVE_ST_RDEV
  313. inode->rdev = hstat.st_rdev;
  314. #else
  315. inode->rdev = 0;
  316. #endif
  317. inode->uid = hstat.st_uid;
  318. inode->gid = hstat.st_gid;
  319. inode->size = hstat.st_size;
  320. inode->mtime = hstat.st_mtime;
  321. inode->atime = hstat.st_atime;
  322. inode->ctime = hstat.st_ctime;
  323. if (current_link_name != NULL && S_ISLNK (hstat.st_mode)) {
  324. inode->linkname = current_link_name;
  325. current_link_name = NULL;
  326. } else {
  327. if (S_ISLNK( hstat.st_mode))
  328. inode->mode &= ~S_IFLNK; /* You *DON'T* want to do this always */
  329. inode->linkname = NULL;
  330. }
  331. if (S_ISDIR (hstat.st_mode))
  332. make_dot_doubledot (entry);
  333. }
  334. }
  335. read_extfs_continue:
  336. free (current_file_name);
  337. if (current_link_name != NULL)
  338. free (current_link_name);
  339. }
  340. }
  341. pclose (extfsd);
  342. #ifdef SCO_FLAVOR
  343. waitpid(-1,NULL,WNOHANG);
  344. #endif /* SCO_FLAVOR */
  345. *pparc = current_archive;
  346. free (buffer);
  347. return 0;
  348. }
  349. static char *get_path (char *inname, struct archive **archive, int is_dir,
  350. int do_not_open);
  351. /* Returns path inside argument. Returned char* is inside inname, which is mangled
  352. * by this operation (so you must not free it's return value)
  353. */
  354. static char *get_path_mangle (char *inname, struct archive **archive, int is_dir,
  355. int do_not_open)
  356. {
  357. char *local, *archive_name, *op;
  358. int result = -1;
  359. struct archive *parc;
  360. struct vfs_stamping *parent;
  361. vfs *v;
  362. int fstype;
  363. archive_name = inname;
  364. vfs_split( inname, &local, &op );
  365. fstype = extfs_which( NULL, op ); /* FIXME: we really should pass
  366. self pointer. But as we know that extfs_which does not touch vfs
  367. *me, it does not matter for now */
  368. if (!local)
  369. local = "";
  370. if (fstype == -1)
  371. return NULL;
  372. /* All filesystems should have some local archive, at least
  373. * it can be '/'.
  374. *
  375. * Actually, we should implement an alias mechanism that would
  376. * translate: "a:" to "dos:a.
  377. *
  378. */
  379. for (parc = first_archive; parc != NULL; parc = parc->next)
  380. if (parc->name) {
  381. if (!strcmp (parc->name, archive_name)) {
  382. struct stat *s=&(parc->extfsstat);
  383. if (vfs_uid && (!(s->st_mode & 0004)))
  384. if ((s->st_gid != vfs_gid) || !(s->st_mode & 0040))
  385. if ((s->st_uid != vfs_uid) || !(s->st_mode & 0400))
  386. return NULL;
  387. /* This is not too secure - in some cases (/#mtools) files created
  388. under user a are probably visible to everyone else since / usually
  389. has permissions 755 */
  390. vfs_stamp (&vfs_extfs_ops, (vfsid) parc);
  391. goto return_success;
  392. }
  393. }
  394. result = do_not_open ? -1 : read_archive (fstype, archive_name, &parc);
  395. if (result == -1) ERRNOR (EIO, NULL);
  396. if (archive_name){
  397. v = vfs_type (archive_name);
  398. if (v == &vfs_local_ops) {
  399. parent = NULL;
  400. } else {
  401. parent = xmalloc (sizeof (struct vfs_stamping), "vfs stamping");
  402. parent->v = v;
  403. parent->next = 0;
  404. parent->id = (*v->getid) (v, archive_name, &(parent->parent));
  405. }
  406. vfs_add_noncurrent_stamps (&vfs_extfs_ops, (vfsid) parc, parent);
  407. vfs_rm_parents (parent);
  408. }
  409. return_success:
  410. *archive = parc;
  411. return local;
  412. }
  413. /* Returns allocated path (without leading slash) inside the archive */
  414. static char *get_path_from_entry (struct entry *entry)
  415. {
  416. struct list {
  417. struct list *next;
  418. char *name;
  419. } *head, *p;
  420. char *localpath;
  421. size_t len;
  422. for (len = 0, head = 0; entry->dir; entry = entry->dir) {
  423. p = xmalloc (sizeof (struct list), "Extfs: list");
  424. p->next = head;
  425. p->name = entry->name;
  426. head = p;
  427. len += strlen (entry->name) + 1;
  428. }
  429. localpath = xmalloc (len, "Extfs: localpath");
  430. *localpath = '\0';
  431. for ( ; head; ) {
  432. strcat (localpath, head->name);
  433. if (head->next)
  434. strcat (localpath, "/");
  435. p = head;
  436. head = head->next;
  437. free (p);
  438. }
  439. return (localpath);
  440. }
  441. struct loop_protect {
  442. struct entry *entry;
  443. struct loop_protect *next;
  444. };
  445. static int errloop;
  446. static int notadir;
  447. static struct entry *
  448. __find_entry (struct entry *dir, char *name,
  449. struct loop_protect *list, int make_dirs, int make_file);
  450. static struct entry *
  451. __resolve_symlinks (struct entry *entry,
  452. struct loop_protect *list)
  453. {
  454. struct entry *pent;
  455. struct loop_protect *looping;
  456. if (!S_ISLNK (entry->inode->mode))
  457. return entry;
  458. for (looping = list; looping != NULL; looping = looping->next)
  459. if (entry == looping->entry) { /* Here we protect us against symlink looping */
  460. errloop = 1;
  461. return NULL;
  462. }
  463. looping = (struct loop_protect *)
  464. xmalloc (sizeof (struct loop_protect),
  465. "Extfs: symlink looping protection");
  466. looping->entry = entry;
  467. looping->next = list;
  468. pent = __find_entry (entry->dir, entry->inode->linkname, looping, 0, 0);
  469. free (looping);
  470. if (pent == NULL)
  471. my_errno = ENOENT;
  472. return pent;
  473. }
  474. static struct entry *my_resolve_symlinks (struct entry *entry)
  475. {
  476. struct entry *res;
  477. errloop = 0;
  478. notadir = 0;
  479. res = __resolve_symlinks (entry, NULL);
  480. if (res == NULL) {
  481. if (errloop)
  482. my_errno = ELOOP;
  483. else if (notadir)
  484. my_errno = ENOTDIR;
  485. }
  486. return res;
  487. }
  488. struct pseudofile {
  489. struct archive *archive;
  490. unsigned int has_changed:1;
  491. int local_handle;
  492. struct entry *entry;
  493. };
  494. static char *get_archive_name (struct archive *archive)
  495. {
  496. char *archive_name;
  497. if (archive->local_name)
  498. archive_name = archive->local_name;
  499. else
  500. archive_name = archive->name;
  501. if (!archive_name || !*archive_name)
  502. return "no_archive_name";
  503. else
  504. return archive_name;
  505. }
  506. /* FIXME: we really should not have non-static procedures - it
  507. * pollutes namespace. */
  508. void extfs_run (char *file)
  509. {
  510. struct archive *archive;
  511. char *p, *q, *cmd, *archive_name, *mc_extfsdir;
  512. if ((p = get_path (file, &archive, 0, 0)) == NULL)
  513. return;
  514. q = name_quote (p, 0);
  515. archive_name = name_quote (get_archive_name(archive), 0);
  516. mc_extfsdir = concat_dir_and_file (mc_home, "extfs/");
  517. cmd = copy_strings (mc_extfsdir, extfs_prefixes [archive->fstype],
  518. " run ", archive_name, " ", q, 0);
  519. free (mc_extfsdir);
  520. free (archive_name);
  521. free (q);
  522. #ifndef VFS_STANDALONE
  523. shell_execute(cmd, 0);
  524. #else
  525. vfs_die( "shell_execute: implement me!" );
  526. #endif
  527. free(cmd);
  528. free(p);
  529. }
  530. static void *extfs_open (vfs *me, char *file, int flags, int mode)
  531. {
  532. struct pseudofile *extfs_info;
  533. struct archive *archive;
  534. char *q;
  535. char *mc_extfsdir;
  536. struct entry *entry;
  537. int local_handle;
  538. const int do_create = (flags & O_ACCMODE) != O_RDONLY;
  539. if ((q = get_path_mangle (file, &archive, 0, 0)) == NULL)
  540. return NULL;
  541. entry = find_entry (archive->root_entry, q, 0, do_create);
  542. if (entry == NULL)
  543. return NULL;
  544. if ((entry = my_resolve_symlinks (entry)) == NULL)
  545. return NULL;
  546. if (S_ISDIR (entry->inode->mode)) ERRNOR (EISDIR, NULL);
  547. if (entry->inode->local_filename == NULL) {
  548. char *cmd, *archive_name, *p;
  549. entry->inode->local_filename = strdup (tempnam (NULL, "extfs"));
  550. p = get_path_from_entry (entry);
  551. q = name_quote (p, 0);
  552. free (p);
  553. archive_name = name_quote (get_archive_name (archive), 0);
  554. mc_extfsdir = concat_dir_and_file (mc_home, "extfs/");
  555. cmd = copy_strings (mc_extfsdir, extfs_prefixes [archive->fstype],
  556. " copyout ",
  557. archive_name,
  558. " ", q, " ", entry->inode->local_filename, 0);
  559. free (q);
  560. free (mc_extfsdir);
  561. free (archive_name);
  562. if (my_system (EXECUTE_AS_SHELL | EXECUTE_SETUID, shell, cmd) && !do_create){
  563. free (entry->inode->local_filename);
  564. entry->inode->local_filename = NULL;
  565. free (cmd);
  566. my_errno = EIO;
  567. return NULL;
  568. }
  569. free (cmd);
  570. }
  571. local_handle = open (entry->inode->local_filename, flags, mode);
  572. if (local_handle == -1) ERRNOR (EIO, NULL);
  573. extfs_info = (struct pseudofile *) xmalloc (sizeof (struct pseudofile), "Extfs: extfs_open");
  574. extfs_info->archive = archive;
  575. extfs_info->entry = entry;
  576. extfs_info->has_changed = 0;
  577. extfs_info->local_handle = local_handle;
  578. /* i.e. we had no open files and now we have one */
  579. vfs_rmstamp (&vfs_extfs_ops, (vfsid) archive, 1);
  580. archive->fd_usage++;
  581. return extfs_info;
  582. }
  583. static int extfs_read (void *data, char *buffer, int count)
  584. {
  585. struct pseudofile *file = (struct pseudofile *)data;
  586. return read (file->local_handle, buffer, count);
  587. }
  588. static int extfs_close (void *data)
  589. {
  590. struct pseudofile *file;
  591. int errno_code = 0;
  592. file = (struct pseudofile *)data;
  593. close (file->local_handle);
  594. /* Commit the file if it has changed */
  595. if (file->has_changed){
  596. struct archive *archive;
  597. char *archive_name, *file_name;
  598. char *cmd;
  599. char *mc_extfsdir;
  600. char *p;
  601. archive = file->archive;
  602. archive_name = name_quote (get_archive_name (archive), 0);
  603. p = get_path_from_entry (file->entry);
  604. file_name = name_quote (p, 0);
  605. free (p);
  606. mc_extfsdir = concat_dir_and_file (mc_home, "extfs/");
  607. cmd = copy_strings (mc_extfsdir,
  608. extfs_prefixes [archive->fstype],
  609. " copyin ", archive_name, " ",
  610. file_name, " ",
  611. file->entry->inode->local_filename, 0);
  612. free (archive_name);
  613. free (file_name);
  614. if (my_system (EXECUTE_AS_SHELL | EXECUTE_SETUID, shell, cmd))
  615. errno_code = EIO;
  616. free (cmd);
  617. free (mc_extfsdir);
  618. {
  619. struct stat file_status;
  620. if( stat(file->entry->inode->local_filename,&file_status) != 0 )
  621. errno_code = EIO;
  622. else file->entry->inode->size = file_status.st_size;
  623. }
  624. file->entry->inode->mtime = time (NULL);
  625. }
  626. file->archive->fd_usage--;
  627. if (!file->archive->fd_usage) {
  628. struct vfs_stamping *parent;
  629. vfs *v;
  630. if (!file->archive->name || !*file->archive->name || (v = vfs_type (file->archive->name)) == &vfs_local_ops) {
  631. parent = NULL;
  632. } else {
  633. parent = xmalloc (sizeof (struct vfs_stamping), "vfs stamping");
  634. parent->v = v;
  635. parent->next = 0;
  636. parent->id = (*v->getid) (v, file->archive->name, &(parent->parent));
  637. }
  638. vfs_add_noncurrent_stamps (&vfs_extfs_ops, (vfsid) (file->archive), parent);
  639. vfs_rm_parents (parent);
  640. }
  641. free (data);
  642. if (errno_code) ERRNOR (EIO, -1);
  643. return 0;
  644. }
  645. #define RECORDSIZE 512
  646. #include "shared_tar_ext.c"
  647. static int extfs_chmod (vfs *me, char *path, int mode)
  648. {
  649. return 0;
  650. }
  651. static int extfs_write (void *data, char *buf, int nbyte)
  652. {
  653. struct pseudofile *file = (struct pseudofile *)data;
  654. file->has_changed = 1;
  655. return write (file->local_handle, buf, nbyte);
  656. }
  657. static int extfs_chdir (vfs *me, char *path)
  658. {
  659. struct archive *archive;
  660. char *q, *res;
  661. struct entry *entry;
  662. my_errno = ENOTDIR;
  663. if ((q = get_path_mangle (path, &archive, 1, 0)) == NULL)
  664. return -1;
  665. entry = find_entry (archive->root_entry, q, 0, 0);
  666. if (!entry)
  667. return -1;
  668. entry = my_resolve_symlinks (entry);
  669. if ((!entry) || (!S_ISDIR (entry->inode->mode)))
  670. return -1;
  671. entry->inode->archive->current_dir = entry;
  672. res = copy_strings (
  673. entry->inode->archive->name, "#", extfs_prefixes [entry->inode->archive->fstype],
  674. "/", q, NULL);
  675. my_errno = 0;
  676. return 0;
  677. }
  678. static int extfs_lseek (void *data, off_t offset, int whence)
  679. {
  680. struct pseudofile *file = (struct pseudofile *) data;
  681. return lseek (file->local_handle, offset, whence);
  682. }
  683. static vfsid extfs_getid (vfs *me, char *path, struct vfs_stamping **parent)
  684. {
  685. struct archive *archive;
  686. vfs *v;
  687. vfsid id;
  688. struct vfs_stamping *par;
  689. char *p;
  690. *parent = NULL;
  691. if (!(p = get_path (path, &archive, 1, 1)))
  692. return (vfsid) -1;
  693. free(p);
  694. if (archive->name){
  695. v = vfs_type (archive->name);
  696. id = (*v->getid) (v, archive->name, &par);
  697. if (id != (vfsid)-1) {
  698. *parent = xmalloc (sizeof (struct vfs_stamping), "vfs stamping");
  699. (*parent)->v = v;
  700. (*parent)->id = id;
  701. (*parent)->parent = par;
  702. (*parent)->next = NULL;
  703. }
  704. }
  705. return (vfsid) archive;
  706. }
  707. static int extfs_nothingisopen (vfsid id)
  708. {
  709. if (((struct archive *)id)->fd_usage <= 0)
  710. return 1;
  711. else
  712. return 0;
  713. }
  714. static void free_entry (struct entry *e)
  715. {
  716. int i = --(e->inode->nlink);
  717. if (S_ISDIR (e->inode->mode) && e->inode->first_in_subdir != NULL) {
  718. struct entry *f = e->inode->first_in_subdir;
  719. e->inode->first_in_subdir = NULL;
  720. free_entry (f);
  721. }
  722. if (i <= 0) {
  723. if (e->inode->local_filename != NULL) {
  724. unlink (e->inode->local_filename);
  725. free (e->inode->local_filename);
  726. }
  727. if (e->inode->linkname != NULL)
  728. free (e->inode->linkname);
  729. free (e->inode);
  730. }
  731. if (e->next_in_dir != NULL)
  732. free_entry (e->next_in_dir);
  733. free (e->name);
  734. free (e);
  735. }
  736. static void extfs_free (vfsid id)
  737. {
  738. struct archive *parc;
  739. struct archive *archive = (struct archive *)id;
  740. free_entry (archive->root_entry);
  741. if (archive == first_archive) {
  742. first_archive = archive->next;
  743. } else {
  744. for (parc = first_archive; parc != NULL; parc = parc->next)
  745. if (parc->next == archive)
  746. break;
  747. if (parc != NULL)
  748. parc->next = archive->next;
  749. }
  750. free_archive (archive);
  751. }
  752. static char *extfs_getlocalcopy (vfs *me, char *path)
  753. {
  754. struct pseudofile *fp =
  755. (struct pseudofile *) extfs_open (me, path, O_RDONLY, 0);
  756. char *p;
  757. if (fp == NULL)
  758. return NULL;
  759. if (fp->entry->inode->local_filename == NULL) {
  760. extfs_close ((void *) fp);
  761. return NULL;
  762. }
  763. p = strdup (fp->entry->inode->local_filename);
  764. fp->archive->fd_usage++;
  765. extfs_close ((void *) fp);
  766. return p;
  767. }
  768. static void extfs_ungetlocalcopy (vfs *me, char *path, char *local, int has_changed)
  769. {
  770. struct pseudofile *fp =
  771. (struct pseudofile *) extfs_open (me, path, O_WRONLY, 0);
  772. if (fp == NULL)
  773. return;
  774. if (!strcmp (fp->entry->inode->local_filename, local)) {
  775. fp->entry->inode->has_changed = has_changed;
  776. fp->archive->fd_usage--;
  777. extfs_close ((void *) fp);
  778. return;
  779. } else {
  780. /* Should not happen */
  781. extfs_close ((void *) fp);
  782. mc_def_ungetlocalcopy (me, path, local, has_changed);
  783. }
  784. }
  785. #include "../src/profile.h"
  786. static int extfs_init (vfs *me)
  787. {
  788. FILE *cfg;
  789. char *mc_extfsini;
  790. mc_extfsini = concat_dir_and_file (mc_home, "extfs/extfs.ini");
  791. cfg = fopen (mc_extfsini, "r");
  792. free (mc_extfsini);
  793. if (!cfg) {
  794. fprintf( stderr, "Warning: " LIBDIR "extfs/extfs.ini not found\n" );
  795. return 0;
  796. }
  797. extfs_no = 0;
  798. while ( extfs_no < MAXEXTFS ) {
  799. char key[256];
  800. char *c;
  801. if (!fgets( key, 250, cfg ))
  802. break;
  803. /* Handle those with a trailing ':', those flag that the
  804. * file system does not require an archive to work
  805. */
  806. if (*key == '[') {
  807. /* We may not use vfs_die() message or message_1s or similar,
  808. * UI is not initialized at this time and message would not
  809. * appear on screen. */
  810. fprintf( stderr, "Warning: You need to update your " LIBDIR "extfs/extfs.ini file.\n" );
  811. fclose(cfg);
  812. return 0;
  813. }
  814. if (*key == '#')
  815. continue;
  816. if ((c = strchr( key, '\n')))
  817. *c = 0;
  818. c = &key [strlen (key)-1];
  819. extfs_need_archive [extfs_no] = !(*c==':');
  820. if (*c==':') *c = 0;
  821. if (!(*key))
  822. continue;
  823. extfs_prefixes [extfs_no] = strdup (key);
  824. extfs_no++;
  825. }
  826. fclose(cfg);
  827. return 1;
  828. }
  829. /* Do NOT use me argument in this function */
  830. static int extfs_which (vfs *me, char *path)
  831. {
  832. int i;
  833. for (i = 0; i < extfs_no; i++)
  834. if (!strcmp (path, extfs_prefixes [i]))
  835. return i;
  836. return -1;
  837. }
  838. static void extfs_done (vfs *me)
  839. {
  840. int i;
  841. for (i = 0; i < extfs_no; i++ )
  842. free (extfs_prefixes [i]);
  843. extfs_no = 0;
  844. }
  845. static int extfs_setctl (vfs *me, char *path, int ctlop, char *arg)
  846. {
  847. if (ctlop == MCCTL_EXTFS_RUN) {
  848. extfs_run (path);
  849. return 1;
  850. }
  851. return 0;
  852. }
  853. vfs vfs_extfs_ops = {
  854. NULL, /* This is place of next pointer */
  855. "Extended filesystems",
  856. F_EXEC, /* flags */
  857. NULL, /* prefix */
  858. NULL, /* data */
  859. 0, /* errno */
  860. extfs_init,
  861. extfs_done,
  862. extfs_fill_names,
  863. extfs_which,
  864. extfs_open,
  865. extfs_close,
  866. extfs_read,
  867. extfs_write,
  868. s_opendir,
  869. s_readdir,
  870. s_closedir,
  871. s_telldir,
  872. s_seekdir,
  873. s_stat,
  874. s_lstat,
  875. s_fstat,
  876. extfs_chmod, /* chmod ... strange, returns success? */
  877. NULL,
  878. NULL,
  879. s_readlink,
  880. NULL, /* symlink */
  881. NULL,
  882. NULL,
  883. NULL,
  884. extfs_chdir,
  885. s_errno,
  886. extfs_lseek,
  887. NULL,
  888. extfs_getid,
  889. extfs_nothingisopen,
  890. extfs_free,
  891. extfs_getlocalcopy,
  892. extfs_ungetlocalcopy,
  893. NULL, /* mkdir */
  894. NULL,
  895. NULL,
  896. extfs_setctl
  897. MMAPNULL
  898. };