direntry.c 28 KB

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