direntry.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333
  1. /** \file
  2. * \brief Source: directory cache support
  3. *
  4. * So that you do not have copy of this in each and every filesystem.
  5. *
  6. * Very loosely based on tar.c from midnight and archives.[ch] from
  7. * avfs by Miklos Szeredi (mszeredi@inf.bme.hu)
  8. *
  9. * Unfortunately, I was unable to keep all filesystems
  10. * uniform. tar-like filesystems use tree structure where each
  11. * directory has pointers to its subdirectories. We can do this
  12. * because we have full information about our archive.
  13. *
  14. * At ftp-like filesystems, situation is a little bit different. When
  15. * you cd /usr/src/linux/drivers/char, you do _not_ want /usr,
  16. * /usr/src, /usr/src/linux and /usr/src/linux/drivers to be
  17. * listed. That means that we do not have complete information, and if
  18. * /usr is symlink to /4, we will not know. Also we have to time out
  19. * entries and things would get messy with tree-like approach. So we
  20. * do different trick: root directory is completely special and
  21. * completely fake, it contains entries such as 'usr', 'usr/src', ...,
  22. * and we'll try to use custom find_entry function.
  23. *
  24. * \author Pavel Machek <pavel@ucw.cz>, distribute under LGPL.
  25. * \date 1998
  26. *
  27. * \warning Paths here do _not_ begin with '/', so root directory of
  28. * archive/site is simply "".
  29. */
  30. #include <config.h>
  31. #include <errno.h>
  32. #include <fcntl.h> /* include fcntl.h -> sys/fcntl.h only */
  33. /* includes fcntl.h see IEEE Std 1003.1-2008 */
  34. #include <time.h>
  35. #include <sys/time.h> /* gettimeofday() */
  36. #include "lib/global.h"
  37. #include "lib/tty/tty.h" /* enable/disable interrupt key */
  38. #include "src/wtools.h" /* message() */
  39. #include "src/main.h" /* print_vfs_message */
  40. #include "vfs.h"
  41. #include "utilvfs.h"
  42. #include "vfs-impl.h"
  43. #include "gc.h" /* vfs_rmstamp */
  44. #include "xdirentry.h"
  45. #define CALL(x) if (MEDATA->x) MEDATA->x
  46. static volatile int total_inodes = 0, total_entries = 0;
  47. struct vfs_s_inode *
  48. vfs_s_new_inode (struct vfs_class *me, struct vfs_s_super *super, struct stat *initstat)
  49. {
  50. struct vfs_s_inode *ino;
  51. ino = g_try_new0 (struct vfs_s_inode, 1);
  52. if (ino == NULL)
  53. return NULL;
  54. if (initstat)
  55. ino->st = *initstat;
  56. ino->super = super;
  57. ino->st.st_nlink = 0;
  58. ino->st.st_ino = MEDATA->inode_counter++;
  59. ino->st.st_dev = MEDATA->rdev;
  60. super->ino_usage++;
  61. total_inodes++;
  62. CALL (init_inode) (me, ino);
  63. return ino;
  64. }
  65. struct vfs_s_entry *
  66. vfs_s_new_entry (struct vfs_class *me, const char *name, struct vfs_s_inode *inode)
  67. {
  68. struct vfs_s_entry *entry;
  69. entry = g_new0 (struct vfs_s_entry, 1);
  70. total_entries++;
  71. if (name)
  72. entry->name = g_strdup (name);
  73. entry->ino = inode;
  74. entry->ino->ent = entry;
  75. CALL (init_entry) (me, entry);
  76. return entry;
  77. }
  78. static void
  79. vfs_s_free_inode (struct vfs_class *me, struct vfs_s_inode *ino)
  80. {
  81. if (!ino)
  82. vfs_die ("Don't pass NULL to me");
  83. /* ==0 can happen if freshly created entry is deleted */
  84. if (ino->st.st_nlink <= 1)
  85. {
  86. while (ino->subdir)
  87. {
  88. vfs_s_free_entry (me, ino->subdir);
  89. }
  90. CALL (free_inode) (me, ino);
  91. g_free (ino->linkname);
  92. if (ino->localname)
  93. {
  94. unlink (ino->localname);
  95. g_free (ino->localname);
  96. }
  97. total_inodes--;
  98. ino->super->ino_usage--;
  99. g_free (ino);
  100. }
  101. else
  102. ino->st.st_nlink--;
  103. }
  104. void
  105. vfs_s_free_entry (struct vfs_class *me, struct vfs_s_entry *ent)
  106. {
  107. if (ent->prevp)
  108. {
  109. /* It is possible that we are deleting freshly created entry */
  110. *ent->prevp = ent->next;
  111. if (ent->next)
  112. ent->next->prevp = ent->prevp;
  113. }
  114. g_free (ent->name);
  115. ent->name = NULL;
  116. if (ent->ino)
  117. {
  118. ent->ino->ent = NULL;
  119. vfs_s_free_inode (me, ent->ino);
  120. ent->ino = NULL;
  121. }
  122. total_entries--;
  123. g_free (ent);
  124. }
  125. void
  126. vfs_s_insert_entry (struct vfs_class *me, struct vfs_s_inode *dir, struct vfs_s_entry *ent)
  127. {
  128. struct vfs_s_entry **ep;
  129. (void) me;
  130. for (ep = &dir->subdir; *ep != NULL; ep = &((*ep)->next))
  131. ;
  132. ent->prevp = ep;
  133. ent->next = NULL;
  134. ent->dir = dir;
  135. *ep = ent;
  136. ent->ino->st.st_nlink++;
  137. }
  138. struct stat *
  139. vfs_s_default_stat (struct vfs_class *me, mode_t mode)
  140. {
  141. static struct stat st;
  142. int myumask;
  143. (void) me;
  144. myumask = umask (022);
  145. umask (myumask);
  146. mode &= ~myumask;
  147. st.st_mode = mode;
  148. st.st_ino = 0;
  149. st.st_dev = 0;
  150. st.st_rdev = 0;
  151. st.st_uid = getuid ();
  152. st.st_gid = getgid ();
  153. st.st_size = 0;
  154. st.st_mtime = st.st_atime = st.st_ctime = time (NULL);
  155. return &st;
  156. }
  157. struct vfs_s_entry *
  158. vfs_s_generate_entry (struct vfs_class *me, const char *name, struct vfs_s_inode *parent,
  159. mode_t mode)
  160. {
  161. struct vfs_s_inode *inode;
  162. struct stat *st;
  163. st = vfs_s_default_stat (me, mode);
  164. inode = vfs_s_new_inode (me, parent->super, st);
  165. return vfs_s_new_entry (me, name, inode);
  166. }
  167. /* We were asked to create entries automagically */
  168. static struct vfs_s_entry *
  169. vfs_s_automake (struct vfs_class *me, struct vfs_s_inode *dir, char *path, int flags)
  170. {
  171. struct vfs_s_entry *res;
  172. char *sep = strchr (path, PATH_SEP);
  173. if (sep)
  174. *sep = 0;
  175. res = vfs_s_generate_entry (me, path, dir, flags & FL_MKDIR ? (0777 | S_IFDIR) : 0777);
  176. vfs_s_insert_entry (me, dir, res);
  177. if (sep)
  178. *sep = PATH_SEP;
  179. return res;
  180. }
  181. /* If the entry is a symlink, find the entry for its target */
  182. static struct vfs_s_entry *
  183. vfs_s_resolve_symlink (struct vfs_class *me, struct vfs_s_entry *entry, int follow)
  184. {
  185. char *linkname;
  186. char *fullname = NULL;
  187. struct vfs_s_entry *target;
  188. if (follow == LINK_NO_FOLLOW)
  189. return entry;
  190. if (follow == 0)
  191. ERRNOR (ELOOP, NULL);
  192. if (!entry)
  193. ERRNOR (ENOENT, NULL);
  194. if (!S_ISLNK (entry->ino->st.st_mode))
  195. return entry;
  196. linkname = entry->ino->linkname;
  197. if (linkname == NULL)
  198. ERRNOR (EFAULT, NULL);
  199. /* make full path from relative */
  200. if (*linkname != PATH_SEP)
  201. {
  202. char *fullpath = vfs_s_fullpath (me, entry->dir);
  203. if (fullpath)
  204. {
  205. fullname = g_strconcat (fullpath, "/", linkname, (char *) NULL);
  206. linkname = fullname;
  207. g_free (fullpath);
  208. }
  209. }
  210. target = (MEDATA->find_entry) (me, entry->dir->super->root, linkname, follow - 1, 0);
  211. g_free (fullname);
  212. return target;
  213. }
  214. /*
  215. * Follow > 0: follow links, serves as loop protect,
  216. * == -1: do not follow links
  217. */
  218. static struct vfs_s_entry *
  219. vfs_s_find_entry_tree (struct vfs_class *me, struct vfs_s_inode *root,
  220. const char *a_path, int follow, int flags)
  221. {
  222. size_t pseg;
  223. struct vfs_s_entry *ent = NULL;
  224. char *const pathref = g_strdup (a_path);
  225. char *path = pathref;
  226. /* canonicalize as well, but don't remove '../' from path */
  227. custom_canonicalize_pathname (path, CANON_PATH_ALL & (~CANON_PATH_REMDOUBLEDOTS));
  228. while (root)
  229. {
  230. while (*path == PATH_SEP) /* Strip leading '/' */
  231. path++;
  232. if (!path[0])
  233. {
  234. g_free (pathref);
  235. return ent;
  236. }
  237. for (pseg = 0; path[pseg] && path[pseg] != PATH_SEP; pseg++);
  238. for (ent = root->subdir; ent != NULL; ent = ent->next)
  239. if (strlen (ent->name) == pseg && (!strncmp (ent->name, path, pseg)))
  240. /* FOUND! */
  241. break;
  242. if (!ent && (flags & (FL_MKFILE | FL_MKDIR)))
  243. ent = vfs_s_automake (me, root, path, flags);
  244. if (!ent)
  245. {
  246. me->verrno = ENOENT;
  247. goto cleanup;
  248. }
  249. path += pseg;
  250. /* here we must follow leading directories always;
  251. only the actual file is optional */
  252. ent = vfs_s_resolve_symlink (me, ent, strchr (path, PATH_SEP) ? LINK_FOLLOW : follow);
  253. if (!ent)
  254. goto cleanup;
  255. root = ent->ino;
  256. }
  257. cleanup:
  258. g_free (pathref);
  259. return NULL;
  260. }
  261. static void
  262. split_dir_name (struct vfs_class *me, char *path, char **dir, char **name, char **save)
  263. {
  264. char *s;
  265. (void) me;
  266. s = strrchr (path, PATH_SEP);
  267. if (s == NULL)
  268. {
  269. *save = NULL;
  270. *name = path;
  271. *dir = path + strlen (path); /* an empty string */
  272. }
  273. else
  274. {
  275. *save = s;
  276. *dir = path;
  277. *s++ = '\0';
  278. *name = s;
  279. }
  280. }
  281. static struct vfs_s_entry *
  282. vfs_s_find_entry_linear (struct vfs_class *me, struct vfs_s_inode *root,
  283. const char *a_path, int follow, int flags)
  284. {
  285. struct vfs_s_entry *ent = NULL;
  286. char *const path = g_strdup (a_path);
  287. struct vfs_s_entry *retval = NULL;
  288. if (root->super->root != root)
  289. vfs_die ("We have to use _real_ root. Always. Sorry.");
  290. /* canonicalize as well, but don't remove '../' from path */
  291. custom_canonicalize_pathname (path, CANON_PATH_ALL & (~CANON_PATH_REMDOUBLEDOTS));
  292. if (!(flags & FL_DIR))
  293. {
  294. char *dirname, *name, *save;
  295. struct vfs_s_inode *ino;
  296. split_dir_name (me, path, &dirname, &name, &save);
  297. ino = vfs_s_find_inode (me, root->super, dirname, follow, flags | FL_DIR);
  298. if (save)
  299. *save = PATH_SEP;
  300. retval = vfs_s_find_entry_tree (me, ino, name, follow, flags);
  301. g_free (path);
  302. return retval;
  303. }
  304. for (ent = root->subdir; ent != NULL; ent = ent->next)
  305. if (!strcmp (ent->name, path))
  306. break;
  307. if (ent && (!(MEDATA->dir_uptodate) (me, ent->ino)))
  308. {
  309. #if 1
  310. print_vfs_message (_("Directory cache expired for %s"), path);
  311. #endif
  312. vfs_s_free_entry (me, ent);
  313. ent = NULL;
  314. }
  315. if (!ent)
  316. {
  317. struct vfs_s_inode *ino;
  318. ino = vfs_s_new_inode (me, root->super, vfs_s_default_stat (me, S_IFDIR | 0755));
  319. ent = vfs_s_new_entry (me, path, ino);
  320. if ((MEDATA->dir_load) (me, ino, path) == -1)
  321. {
  322. vfs_s_free_entry (me, ent);
  323. g_free (path);
  324. return NULL;
  325. }
  326. vfs_s_insert_entry (me, root, ent);
  327. for (ent = root->subdir; ent != NULL; ent = ent->next)
  328. if (!strcmp (ent->name, path))
  329. break;
  330. }
  331. if (!ent)
  332. vfs_die ("find_linear: success but directory is not there\n");
  333. #if 0
  334. if (!vfs_s_resolve_symlink (me, ent, follow))
  335. {
  336. g_free (path);
  337. return NULL;
  338. }
  339. #endif
  340. g_free (path);
  341. return ent;
  342. }
  343. struct vfs_s_inode *
  344. vfs_s_find_inode (struct vfs_class *me, const struct vfs_s_super *super,
  345. const char *path, int follow, int flags)
  346. {
  347. struct vfs_s_entry *ent;
  348. if (((MEDATA->flags & VFS_S_REMOTE) == 0) && (*path == '\0'))
  349. return super->root;
  350. ent = (MEDATA->find_entry) (me, super->root, path, follow, flags);
  351. return (ent != NULL) ? ent->ino : NULL;
  352. }
  353. /* Ook, these were functions around directory entries / inodes */
  354. /* -------------------------------- superblock games -------------------------- */
  355. static struct vfs_s_super *
  356. vfs_s_new_super (struct vfs_class *me)
  357. {
  358. struct vfs_s_super *super;
  359. super = g_new0 (struct vfs_s_super, 1);
  360. super->me = me;
  361. return super;
  362. }
  363. static void
  364. vfs_s_insert_super (struct vfs_class *me, struct vfs_s_super *super)
  365. {
  366. super->next = MEDATA->supers;
  367. super->prevp = &MEDATA->supers;
  368. if (MEDATA->supers != NULL)
  369. MEDATA->supers->prevp = &super->next;
  370. MEDATA->supers = super;
  371. }
  372. static void
  373. vfs_s_free_super (struct vfs_class *me, struct vfs_s_super *super)
  374. {
  375. if (super->root)
  376. {
  377. vfs_s_free_inode (me, super->root);
  378. super->root = NULL;
  379. }
  380. #if 0
  381. /* FIXME: We currently leak small ammount of memory, sometimes. Fix it if you can. */
  382. if (super->ino_usage)
  383. message (D_ERROR, "Direntry warning",
  384. "Super ino_usage is %d, memory leak", super->ino_usage);
  385. if (super->want_stale)
  386. message (D_ERROR, "Direntry warning", "%s", "Super has want_stale set");
  387. #endif
  388. if (super->prevp)
  389. {
  390. *super->prevp = super->next;
  391. if (super->next)
  392. super->next->prevp = super->prevp;
  393. }
  394. CALL (free_archive) (me, super);
  395. g_free (super->name);
  396. g_free (super);
  397. }
  398. /*
  399. * Dissect the path and create corresponding superblock. Note that inname
  400. * can be changed and the result may point inside the original string.
  401. */
  402. const char *
  403. vfs_s_get_path_mangle (struct vfs_class *me, char *inname, struct vfs_s_super **archive, int flags)
  404. {
  405. const char *retval;
  406. char *local, *op;
  407. const char *archive_name;
  408. int result = -1;
  409. struct vfs_s_super *super;
  410. void *cookie = NULL;
  411. archive_name = inname;
  412. vfs_split (inname, &local, &op);
  413. retval = (local != NULL) ? local : "";
  414. if (MEDATA->archive_check != NULL)
  415. {
  416. cookie = MEDATA->archive_check (me, archive_name, op);
  417. if (cookie == NULL)
  418. return NULL;
  419. }
  420. for (super = MEDATA->supers; super != NULL; super = super->next)
  421. {
  422. /* 0 == other, 1 == same, return it, 2 == other but stop scanning */
  423. int i = MEDATA->archive_same (me, super, archive_name, op, cookie);
  424. if (i != 0)
  425. {
  426. if (i == 1)
  427. goto return_success;
  428. else
  429. break;
  430. }
  431. }
  432. if (flags & FL_NO_OPEN)
  433. ERRNOR (EIO, NULL);
  434. super = vfs_s_new_super (me);
  435. result = MEDATA->open_archive (me, super, archive_name, op);
  436. if (result == -1)
  437. {
  438. vfs_s_free_super (me, super);
  439. ERRNOR (EIO, NULL);
  440. }
  441. if (!super->name)
  442. vfs_die ("You have to fill name\n");
  443. if (!super->root)
  444. vfs_die ("You have to fill root inode\n");
  445. vfs_s_insert_super (me, super);
  446. vfs_stamp_create (me, super);
  447. return_success:
  448. *archive = super;
  449. return retval;
  450. }
  451. /*
  452. * Dissect the path and create corresponding superblock.
  453. * The result should be freed.
  454. */
  455. static char *
  456. vfs_s_get_path (struct vfs_class *me, const char *inname, struct vfs_s_super **archive, int flags)
  457. {
  458. char *buf, *retval;
  459. buf = g_strdup (inname);
  460. retval = g_strdup (vfs_s_get_path_mangle (me, buf, archive, flags));
  461. g_free (buf);
  462. return retval;
  463. }
  464. void
  465. vfs_s_invalidate (struct vfs_class *me, struct vfs_s_super *super)
  466. {
  467. if (!super->want_stale)
  468. {
  469. vfs_s_free_inode (me, super->root);
  470. super->root = vfs_s_new_inode (me, super, vfs_s_default_stat (me, S_IFDIR | 0755));
  471. }
  472. }
  473. char *
  474. vfs_s_fullpath (struct vfs_class *me, struct vfs_s_inode *ino)
  475. {
  476. if (!ino->ent)
  477. ERRNOR (EAGAIN, NULL);
  478. if (!(MEDATA->flags & VFS_S_REMOTE))
  479. {
  480. /* archives */
  481. char *newpath;
  482. char *path = g_strdup (ino->ent->name);
  483. while (1)
  484. {
  485. ino = ino->ent->dir;
  486. if (ino == ino->super->root)
  487. break;
  488. newpath = g_strconcat (ino->ent->name, "/", path, (char *) NULL);
  489. g_free (path);
  490. path = newpath;
  491. }
  492. return path;
  493. }
  494. /* remote systems */
  495. if ((!ino->ent->dir) || (!ino->ent->dir->ent))
  496. return g_strdup (ino->ent->name);
  497. return g_strconcat (ino->ent->dir->ent->name, PATH_SEP_STR, ino->ent->name, (char *) NULL);
  498. }
  499. /* Support of archives */
  500. /* ------------------------ readdir & friends ----------------------------- */
  501. static struct vfs_s_inode *
  502. vfs_s_inode_from_path (struct vfs_class *me, const char *name, int flags)
  503. {
  504. struct vfs_s_super *super;
  505. struct vfs_s_inode *ino;
  506. char *q;
  507. if (!(q = vfs_s_get_path (me, name, &super, 0)))
  508. return NULL;
  509. ino =
  510. vfs_s_find_inode (me, super, q,
  511. flags & FL_FOLLOW ? LINK_FOLLOW : LINK_NO_FOLLOW, flags & ~FL_FOLLOW);
  512. if ((!ino) && (!*q))
  513. /* We are asking about / directory of ftp server: assume it exists */
  514. ino =
  515. vfs_s_find_inode (me, super, q,
  516. flags & FL_FOLLOW ? LINK_FOLLOW :
  517. LINK_NO_FOLLOW, FL_DIR | (flags & ~FL_FOLLOW));
  518. g_free (q);
  519. return ino;
  520. }
  521. struct dirhandle
  522. {
  523. struct vfs_s_entry *cur;
  524. struct vfs_s_inode *dir;
  525. };
  526. static void *
  527. vfs_s_opendir (struct vfs_class *me, const char *dirname)
  528. {
  529. struct vfs_s_inode *dir;
  530. struct dirhandle *info;
  531. dir = vfs_s_inode_from_path (me, dirname, FL_DIR | FL_FOLLOW);
  532. if (!dir)
  533. return NULL;
  534. if (!S_ISDIR (dir->st.st_mode))
  535. ERRNOR (ENOTDIR, NULL);
  536. dir->st.st_nlink++;
  537. #if 0
  538. if (!dir->subdir) /* This can actually happen if we allow empty directories */
  539. ERRNOR (EAGAIN, NULL);
  540. #endif
  541. info = g_new (struct dirhandle, 1);
  542. info->cur = dir->subdir;
  543. info->dir = dir;
  544. return info;
  545. }
  546. static void *
  547. vfs_s_readdir (void *data)
  548. {
  549. static union vfs_dirent dir;
  550. struct dirhandle *info = (struct dirhandle *) data;
  551. if (!(info->cur))
  552. return NULL;
  553. if (info->cur->name)
  554. {
  555. g_strlcpy (dir.dent.d_name, info->cur->name, MC_MAXPATHLEN);
  556. }
  557. else
  558. {
  559. vfs_die ("Null in structure-cannot happen");
  560. }
  561. compute_namelen (&dir.dent);
  562. info->cur = info->cur->next;
  563. return (void *) &dir;
  564. }
  565. static int
  566. vfs_s_closedir (void *data)
  567. {
  568. struct dirhandle *info = (struct dirhandle *) data;
  569. struct vfs_s_inode *dir = info->dir;
  570. vfs_s_free_inode (dir->super->me, dir);
  571. g_free (data);
  572. return 0;
  573. }
  574. static int
  575. vfs_s_chdir (struct vfs_class *me, const char *path)
  576. {
  577. void *data;
  578. data = vfs_s_opendir (me, path);
  579. if (data == NULL)
  580. return -1;
  581. vfs_s_closedir (data);
  582. return 0;
  583. }
  584. /* --------------------------- stat and friends ---------------------------- */
  585. static int
  586. vfs_s_internal_stat (struct vfs_class *me, const char *path, struct stat *buf, int flag)
  587. {
  588. struct vfs_s_inode *ino;
  589. ino = vfs_s_inode_from_path (me, path, flag);
  590. if (ino == NULL)
  591. return -1;
  592. *buf = ino->st;
  593. return 0;
  594. }
  595. static int
  596. vfs_s_stat (struct vfs_class *me, const char *path, struct stat *buf)
  597. {
  598. return vfs_s_internal_stat (me, path, buf, FL_FOLLOW);
  599. }
  600. static int
  601. vfs_s_lstat (struct vfs_class *me, const char *path, struct stat *buf)
  602. {
  603. return vfs_s_internal_stat (me, path, buf, FL_NONE);
  604. }
  605. static int
  606. vfs_s_fstat (void *fh, struct stat *buf)
  607. {
  608. *buf = FH->ino->st;
  609. return 0;
  610. }
  611. static int
  612. vfs_s_readlink (struct vfs_class *me, const char *path, char *buf, size_t size)
  613. {
  614. struct vfs_s_inode *ino;
  615. size_t len;
  616. ino = vfs_s_inode_from_path (me, path, 0);
  617. if (!ino)
  618. return -1;
  619. if (!S_ISLNK (ino->st.st_mode))
  620. ERRNOR (EINVAL, -1);
  621. if (ino->linkname == NULL)
  622. ERRNOR (EFAULT, -1);
  623. len = strlen (ino->linkname);
  624. if (size < len)
  625. len = size;
  626. /* readlink() does not append a NUL character to buf */
  627. memcpy (buf, ino->linkname, len);
  628. return len;
  629. }
  630. void *
  631. vfs_s_open (struct vfs_class *me, const char *file, int flags, int mode)
  632. {
  633. int was_changed = 0;
  634. struct vfs_s_fh *fh;
  635. struct vfs_s_super *super;
  636. char *q;
  637. struct vfs_s_inode *ino;
  638. q = vfs_s_get_path (me, file, &super, 0);
  639. if (q == NULL)
  640. return NULL;
  641. ino = vfs_s_find_inode (me, super, q, LINK_FOLLOW, FL_NONE);
  642. if (ino && ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL)))
  643. {
  644. g_free (q);
  645. ERRNOR (EEXIST, NULL);
  646. }
  647. if (!ino)
  648. {
  649. char *dirname, *name, *save;
  650. struct vfs_s_entry *ent;
  651. struct vfs_s_inode *dir;
  652. int tmp_handle;
  653. /* If the filesystem is read-only, disable file creation */
  654. if (!(flags & O_CREAT) || !(me->write))
  655. {
  656. g_free (q);
  657. return NULL;
  658. }
  659. split_dir_name (me, q, &dirname, &name, &save);
  660. /* FIXME: check if vfs_s_find_inode returns NULL */
  661. dir = vfs_s_find_inode (me, super, dirname, LINK_FOLLOW, FL_DIR);
  662. if (save)
  663. *save = PATH_SEP;
  664. ent = vfs_s_generate_entry (me, name, dir, 0755);
  665. ino = ent->ino;
  666. vfs_s_insert_entry (me, dir, ent);
  667. tmp_handle = vfs_mkstemps (&ino->localname, me->name, name);
  668. if (tmp_handle == -1)
  669. {
  670. g_free (q);
  671. return NULL;
  672. }
  673. close (tmp_handle);
  674. was_changed = 1;
  675. }
  676. g_free (q);
  677. if (S_ISDIR (ino->st.st_mode))
  678. ERRNOR (EISDIR, NULL);
  679. fh = g_new (struct vfs_s_fh, 1);
  680. fh->pos = 0;
  681. fh->ino = ino;
  682. fh->handle = -1;
  683. fh->changed = was_changed;
  684. fh->linear = 0;
  685. if (IS_LINEAR (flags))
  686. {
  687. if (MEDATA->linear_start)
  688. {
  689. print_vfs_message (_("Starting linear transfer..."));
  690. fh->linear = LS_LINEAR_PREOPEN;
  691. }
  692. }
  693. else if ((MEDATA->fh_open) && (MEDATA->fh_open (me, fh, flags, mode)))
  694. {
  695. g_free (fh);
  696. return NULL;
  697. }
  698. if (fh->ino->localname)
  699. {
  700. fh->handle = open (fh->ino->localname, NO_LINEAR (flags), mode);
  701. if (fh->handle == -1)
  702. {
  703. g_free (fh);
  704. ERRNOR (errno, NULL);
  705. }
  706. }
  707. /* i.e. we had no open files and now we have one */
  708. vfs_rmstamp (me, (vfsid) super);
  709. super->fd_usage++;
  710. fh->ino->st.st_nlink++;
  711. return fh;
  712. }
  713. static ssize_t
  714. vfs_s_read (void *fh, char *buffer, int count)
  715. {
  716. int n;
  717. struct vfs_class *me = FH_SUPER->me;
  718. if (FH->linear == LS_LINEAR_PREOPEN)
  719. {
  720. if (!MEDATA->linear_start (me, FH, FH->pos))
  721. return -1;
  722. }
  723. if (FH->linear == LS_LINEAR_CLOSED)
  724. vfs_die ("linear_start() did not set linear_state!");
  725. if (FH->linear == LS_LINEAR_OPEN)
  726. return MEDATA->linear_read (me, FH, buffer, count);
  727. if (FH->handle != -1)
  728. {
  729. n = read (FH->handle, buffer, count);
  730. if (n < 0)
  731. me->verrno = errno;
  732. return n;
  733. }
  734. vfs_die ("vfs_s_read: This should not happen\n");
  735. return -1;
  736. }
  737. static ssize_t
  738. vfs_s_write (void *fh, const char *buffer, int count)
  739. {
  740. int n;
  741. struct vfs_class *me = FH_SUPER->me;
  742. if (FH->linear)
  743. vfs_die ("no writing to linear files, please");
  744. FH->changed = 1;
  745. if (FH->handle != -1)
  746. {
  747. n = write (FH->handle, buffer, count);
  748. if (n < 0)
  749. me->verrno = errno;
  750. return n;
  751. }
  752. vfs_die ("vfs_s_write: This should not happen\n");
  753. return 0;
  754. }
  755. static off_t
  756. vfs_s_lseek (void *fh, off_t offset, int whence)
  757. {
  758. off_t size = FH->ino->st.st_size;
  759. if (FH->linear == LS_LINEAR_OPEN)
  760. vfs_die ("cannot lseek() after linear_read!");
  761. if (FH->handle != -1)
  762. { /* If we have local file opened, we want to work with it */
  763. int retval = lseek (FH->handle, offset, whence);
  764. if (retval == -1)
  765. FH->ino->super->me->verrno = errno;
  766. return retval;
  767. }
  768. switch (whence)
  769. {
  770. case SEEK_CUR:
  771. offset += FH->pos;
  772. break;
  773. case SEEK_END:
  774. offset += size;
  775. break;
  776. }
  777. if (offset < 0)
  778. FH->pos = 0;
  779. else if (offset < size)
  780. FH->pos = offset;
  781. else
  782. FH->pos = size;
  783. return FH->pos;
  784. }
  785. static int
  786. vfs_s_close (void *fh)
  787. {
  788. int res = 0;
  789. struct vfs_class *me = FH_SUPER->me;
  790. FH_SUPER->fd_usage--;
  791. if (!FH_SUPER->fd_usage)
  792. vfs_stamp_create (me, FH_SUPER);
  793. if (FH->linear == LS_LINEAR_OPEN)
  794. MEDATA->linear_close (me, fh);
  795. if (MEDATA->fh_close)
  796. res = MEDATA->fh_close (me, fh);
  797. if (FH->changed && MEDATA->file_store)
  798. {
  799. char *s = vfs_s_fullpath (me, FH->ino);
  800. if (!s)
  801. res = -1;
  802. else
  803. {
  804. res = MEDATA->file_store (me, fh, s, FH->ino->localname);
  805. g_free (s);
  806. }
  807. vfs_s_invalidate (me, FH_SUPER);
  808. }
  809. if (FH->handle != -1)
  810. close (FH->handle);
  811. vfs_s_free_inode (me, FH->ino);
  812. g_free (fh);
  813. return res;
  814. }
  815. static void
  816. vfs_s_print_stats (const char *fs_name, const char *action,
  817. const char *file_name, off_t have, off_t need)
  818. {
  819. static const char *i18n_percent_transf_format = NULL;
  820. static const char *i18n_transf_format = NULL;
  821. if (i18n_percent_transf_format == NULL)
  822. {
  823. i18n_percent_transf_format = _("%s: %s: %s %3d%% (%lu bytes transferred)");
  824. i18n_transf_format = _("%s: %s: %s %lu bytes transferred");
  825. }
  826. if (need)
  827. print_vfs_message (i18n_percent_transf_format, fs_name, action,
  828. file_name, (int) ((double) have * 100 / need), (unsigned long) have);
  829. else
  830. print_vfs_message (i18n_transf_format, fs_name, action, file_name, (unsigned long) have);
  831. }
  832. int
  833. vfs_s_retrieve_file (struct vfs_class *me, struct vfs_s_inode *ino)
  834. {
  835. /* If you want reget, you'll have to open file with O_LINEAR */
  836. off_t total = 0;
  837. char buffer[8192];
  838. int handle, n;
  839. off_t stat_size = ino->st.st_size;
  840. struct vfs_s_fh fh;
  841. memset (&fh, 0, sizeof (fh));
  842. fh.ino = ino;
  843. fh.handle = -1;
  844. handle = vfs_mkstemps (&ino->localname, me->name, ino->ent->name);
  845. if (handle == -1)
  846. {
  847. me->verrno = errno;
  848. goto error_4;
  849. }
  850. if (!MEDATA->linear_start (me, &fh, 0))
  851. goto error_3;
  852. /* Clear the interrupt status */
  853. tty_got_interrupt ();
  854. tty_enable_interrupt_key ();
  855. while ((n = MEDATA->linear_read (me, &fh, buffer, sizeof (buffer))))
  856. {
  857. int t;
  858. if (n < 0)
  859. goto error_1;
  860. total += n;
  861. vfs_s_print_stats (me->name, _("Getting file"), ino->ent->name, total, stat_size);
  862. if (tty_got_interrupt ())
  863. goto error_1;
  864. t = write (handle, buffer, n);
  865. if (t != n)
  866. {
  867. if (t == -1)
  868. me->verrno = errno;
  869. goto error_1;
  870. }
  871. }
  872. MEDATA->linear_close (me, &fh);
  873. close (handle);
  874. tty_disable_interrupt_key ();
  875. return 0;
  876. error_1:
  877. MEDATA->linear_close (me, &fh);
  878. error_3:
  879. tty_disable_interrupt_key ();
  880. close (handle);
  881. unlink (ino->localname);
  882. error_4:
  883. g_free (ino->localname);
  884. ino->localname = NULL;
  885. return -1;
  886. }
  887. /* ------------------------------- mc support ---------------------------- */
  888. static void
  889. vfs_s_fill_names (struct vfs_class *me, fill_names_f func)
  890. {
  891. struct vfs_s_super *a = MEDATA->supers;
  892. char *name;
  893. while (a)
  894. {
  895. name = g_strconcat (a->name, "#", me->prefix, "/",
  896. /* a->current_dir->name, */ (char *) NULL);
  897. (*func) (name);
  898. g_free (name);
  899. a = a->next;
  900. }
  901. }
  902. static int
  903. vfs_s_ferrno (struct vfs_class *me)
  904. {
  905. return me->verrno;
  906. }
  907. /*
  908. * Get local copy of the given file. We reuse the existing file cache
  909. * for remote filesystems. Archives use standard VFS facilities.
  910. */
  911. static char *
  912. vfs_s_getlocalcopy (struct vfs_class *me, const char *path)
  913. {
  914. struct vfs_s_fh *fh;
  915. char *local = NULL;
  916. fh = vfs_s_open (me, path, O_RDONLY, 0);
  917. if (fh != NULL)
  918. {
  919. if ((fh->ino != NULL) && (fh->ino->localname != NULL))
  920. local = g_strdup (fh->ino->localname);
  921. vfs_s_close (fh);
  922. }
  923. return local;
  924. }
  925. /*
  926. * Return the local copy. Since we are using our cache, we do nothing -
  927. * the cache will be removed when the archive is closed.
  928. */
  929. static int
  930. vfs_s_ungetlocalcopy (struct vfs_class *me, const char *path, const char *local, int has_changed)
  931. {
  932. (void) me;
  933. (void) path;
  934. (void) local;
  935. (void) has_changed;
  936. return 0;
  937. }
  938. static int
  939. vfs_s_setctl (struct vfs_class *me, const char *path, int ctlop, void *arg)
  940. {
  941. switch (ctlop)
  942. {
  943. case VFS_SETCTL_STALE_DATA:
  944. {
  945. struct vfs_s_inode *ino = vfs_s_inode_from_path (me, path, 0);
  946. if (ino == NULL)
  947. return 0;
  948. if (arg)
  949. ino->super->want_stale = 1;
  950. else
  951. {
  952. ino->super->want_stale = 0;
  953. vfs_s_invalidate (me, ino->super);
  954. }
  955. return 1;
  956. }
  957. case VFS_SETCTL_LOGFILE:
  958. MEDATA->logfile = fopen ((char *) arg, "w");
  959. return 1;
  960. case VFS_SETCTL_FLUSH:
  961. MEDATA->flush = 1;
  962. return 1;
  963. }
  964. return 0;
  965. }
  966. /* ----------------------------- Stamping support -------------------------- */
  967. static vfsid
  968. vfs_s_getid (struct vfs_class *me, const char *path)
  969. {
  970. struct vfs_s_super *archive = NULL;
  971. char *p;
  972. p = vfs_s_get_path (me, path, &archive, FL_NO_OPEN);
  973. if (p == NULL)
  974. return NULL;
  975. g_free (p);
  976. return (vfsid) archive;
  977. }
  978. static int
  979. vfs_s_nothingisopen (vfsid id)
  980. {
  981. (void) id;
  982. /* Our data structures should survive free of superblock at any time */
  983. return 1;
  984. }
  985. static void
  986. vfs_s_free (vfsid id)
  987. {
  988. vfs_s_free_super (((struct vfs_s_super *) id)->me, (struct vfs_s_super *) id);
  989. }
  990. static int
  991. vfs_s_dir_uptodate (struct vfs_class *me, struct vfs_s_inode *ino)
  992. {
  993. struct timeval tim;
  994. if (MEDATA->flush)
  995. {
  996. MEDATA->flush = 0;
  997. return 0;
  998. }
  999. gettimeofday (&tim, NULL);
  1000. if (tim.tv_sec < ino->timestamp.tv_sec)
  1001. return 1;
  1002. return 0;
  1003. }
  1004. /* Initialize one of our subclasses - fill common functions */
  1005. void
  1006. vfs_s_init_class (struct vfs_class *vclass, struct vfs_s_subclass *sub)
  1007. {
  1008. vclass->data = sub;
  1009. vclass->fill_names = vfs_s_fill_names;
  1010. vclass->open = vfs_s_open;
  1011. vclass->close = vfs_s_close;
  1012. vclass->read = vfs_s_read;
  1013. if (!(sub->flags & VFS_S_READONLY))
  1014. {
  1015. vclass->write = vfs_s_write;
  1016. }
  1017. vclass->opendir = vfs_s_opendir;
  1018. vclass->readdir = vfs_s_readdir;
  1019. vclass->closedir = vfs_s_closedir;
  1020. vclass->stat = vfs_s_stat;
  1021. vclass->lstat = vfs_s_lstat;
  1022. vclass->fstat = vfs_s_fstat;
  1023. vclass->readlink = vfs_s_readlink;
  1024. vclass->chdir = vfs_s_chdir;
  1025. vclass->ferrno = vfs_s_ferrno;
  1026. vclass->lseek = vfs_s_lseek;
  1027. vclass->getid = vfs_s_getid;
  1028. vclass->nothingisopen = vfs_s_nothingisopen;
  1029. vclass->free = vfs_s_free;
  1030. if (sub->flags & VFS_S_REMOTE)
  1031. {
  1032. vclass->getlocalcopy = vfs_s_getlocalcopy;
  1033. vclass->ungetlocalcopy = vfs_s_ungetlocalcopy;
  1034. sub->find_entry = vfs_s_find_entry_linear;
  1035. }
  1036. else
  1037. {
  1038. sub->find_entry = vfs_s_find_entry_tree;
  1039. }
  1040. vclass->setctl = vfs_s_setctl;
  1041. sub->dir_uptodate = vfs_s_dir_uptodate;
  1042. }
  1043. /* ----------- Utility functions for networked filesystems -------------- */
  1044. #ifdef USE_NETCODE
  1045. int
  1046. vfs_s_select_on_two (int fd1, int fd2)
  1047. {
  1048. fd_set set;
  1049. struct timeval time_out;
  1050. int v;
  1051. int maxfd = (fd1 > fd2 ? fd1 : fd2) + 1;
  1052. time_out.tv_sec = 1;
  1053. time_out.tv_usec = 0;
  1054. FD_ZERO (&set);
  1055. FD_SET (fd1, &set);
  1056. FD_SET (fd2, &set);
  1057. v = select (maxfd, &set, 0, 0, &time_out);
  1058. if (v <= 0)
  1059. return v;
  1060. if (FD_ISSET (fd1, &set))
  1061. return 1;
  1062. if (FD_ISSET (fd2, &set))
  1063. return 2;
  1064. return -1;
  1065. }
  1066. int
  1067. vfs_s_get_line (struct vfs_class *me, int sock, char *buf, int buf_len, char term)
  1068. {
  1069. FILE *logfile = MEDATA->logfile;
  1070. int i;
  1071. char c;
  1072. for (i = 0; i < buf_len - 1; i++, buf++)
  1073. {
  1074. if (read (sock, buf, sizeof (char)) <= 0)
  1075. return 0;
  1076. if (logfile)
  1077. {
  1078. size_t ret1;
  1079. int ret2;
  1080. ret1 = fwrite (buf, 1, 1, logfile);
  1081. ret2 = fflush (logfile);
  1082. }
  1083. if (*buf == term)
  1084. {
  1085. *buf = 0;
  1086. return 1;
  1087. }
  1088. }
  1089. /* Line is too long - terminate buffer and discard the rest of line */
  1090. *buf = 0;
  1091. while (read (sock, &c, sizeof (c)) > 0)
  1092. {
  1093. if (logfile)
  1094. {
  1095. size_t ret1;
  1096. int ret2;
  1097. ret1 = fwrite (&c, 1, 1, logfile);
  1098. ret2 = fflush (logfile);
  1099. }
  1100. if (c == '\n')
  1101. return 1;
  1102. }
  1103. return 0;
  1104. }
  1105. int
  1106. vfs_s_get_line_interruptible (struct vfs_class *me, char *buffer, int size, int fd)
  1107. {
  1108. int n;
  1109. int i;
  1110. (void) me;
  1111. tty_enable_interrupt_key ();
  1112. for (i = 0; i < size - 1; i++)
  1113. {
  1114. n = read (fd, buffer + i, 1);
  1115. tty_disable_interrupt_key ();
  1116. if (n == -1 && errno == EINTR)
  1117. {
  1118. buffer[i] = 0;
  1119. return EINTR;
  1120. }
  1121. if (n == 0)
  1122. {
  1123. buffer[i] = 0;
  1124. return 0;
  1125. }
  1126. if (buffer[i] == '\n')
  1127. {
  1128. buffer[i] = 0;
  1129. return 1;
  1130. }
  1131. }
  1132. buffer[size - 1] = 0;
  1133. return 0;
  1134. }
  1135. #endif /* USE_NETCODE */