direntry.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231
  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. super->name = NULL;
  377. g_free(super);
  378. }
  379. /*
  380. * Dissect the path and create corresponding superblock. Note that inname
  381. * can be changed and the result may point inside the original string.
  382. */
  383. const char *
  384. vfs_s_get_path_mangle (struct vfs_class *me, char *inname,
  385. struct vfs_s_super **archive, int flags)
  386. {
  387. const char *retval;
  388. char *local, *op;
  389. const char *archive_name;
  390. int result = -1;
  391. struct vfs_s_super *super;
  392. void *cookie = NULL;
  393. archive_name = inname;
  394. vfs_split (inname, &local, &op);
  395. retval = (local) ? local : "";
  396. if (MEDATA->archive_check)
  397. if (!(cookie = MEDATA->archive_check (me, archive_name, op)))
  398. return NULL;
  399. for (super = MEDATA->supers; super != NULL; super = super->next) {
  400. /* 0 == other, 1 == same, return it, 2 == other but stop scanning */
  401. int i;
  402. if ((i =
  403. MEDATA->archive_same (me, super, archive_name, op, cookie))) {
  404. if (i == 1)
  405. goto return_success;
  406. else
  407. break;
  408. }
  409. }
  410. if (flags & FL_NO_OPEN)
  411. ERRNOR (EIO, NULL);
  412. super = vfs_s_new_super (me);
  413. result = MEDATA->open_archive (me, super, archive_name, op);
  414. if (result == -1) {
  415. vfs_s_free_super (me, super);
  416. ERRNOR (EIO, NULL);
  417. }
  418. if (!super->name)
  419. vfs_die ("You have to fill name\n");
  420. if (!super->root)
  421. vfs_die ("You have to fill root inode\n");
  422. vfs_s_insert_super (me, super);
  423. vfs_stamp_create (me, super);
  424. return_success:
  425. *archive = super;
  426. return retval;
  427. }
  428. /*
  429. * Dissect the path and create corresponding superblock.
  430. * The result should be freed.
  431. */
  432. static char *
  433. vfs_s_get_path (struct vfs_class *me, const char *inname,
  434. struct vfs_s_super **archive, int flags)
  435. {
  436. char *buf, *retval;
  437. buf = g_strdup (inname);
  438. retval = g_strdup (vfs_s_get_path_mangle (me, buf, archive, flags));
  439. g_free (buf);
  440. return retval;
  441. }
  442. void
  443. vfs_s_invalidate (struct vfs_class *me, struct vfs_s_super *super)
  444. {
  445. if (!super->want_stale){
  446. vfs_s_free_inode (me, super->root);
  447. super->root = vfs_s_new_inode (me, super, vfs_s_default_stat (me, S_IFDIR | 0755));
  448. }
  449. }
  450. char *
  451. vfs_s_fullpath (struct vfs_class *me, struct vfs_s_inode *ino)
  452. {
  453. if (!ino->ent)
  454. ERRNOR (EAGAIN, NULL);
  455. if (!(MEDATA->flags & VFS_S_REMOTE)) {
  456. /* archives */
  457. char *newpath;
  458. char *path = g_strdup (ino->ent->name);
  459. while (1) {
  460. ino = ino->ent->dir;
  461. if (ino == ino->super->root)
  462. break;
  463. newpath = g_strconcat (ino->ent->name, "/", path, (char *) NULL);
  464. g_free (path);
  465. path = newpath;
  466. }
  467. return path;
  468. }
  469. /* remote systems */
  470. if ((!ino->ent->dir) || (!ino->ent->dir->ent))
  471. return g_strdup (ino->ent->name);
  472. return g_strconcat (ino->ent->dir->ent->name, PATH_SEP_STR,
  473. ino->ent->name, (char *) NULL);
  474. }
  475. /* Support of archives */
  476. /* ------------------------ readdir & friends ----------------------------- */
  477. static struct vfs_s_inode *
  478. vfs_s_inode_from_path (struct vfs_class *me, const char *name, int flags)
  479. {
  480. struct vfs_s_super *super;
  481. struct vfs_s_inode *ino;
  482. char *q;
  483. if (!(q = vfs_s_get_path (me, name, &super, 0)))
  484. return NULL;
  485. ino =
  486. vfs_s_find_inode (me, super, q,
  487. flags & FL_FOLLOW ? LINK_FOLLOW : LINK_NO_FOLLOW,
  488. flags & ~FL_FOLLOW);
  489. if ((!ino) && (!*q))
  490. /* We are asking about / directory of ftp server: assume it exists */
  491. ino =
  492. vfs_s_find_inode (me, super, q,
  493. flags & FL_FOLLOW ? LINK_FOLLOW :
  494. LINK_NO_FOLLOW,
  495. FL_DIR | (flags & ~FL_FOLLOW));
  496. g_free (q);
  497. return ino;
  498. }
  499. struct dirhandle {
  500. struct vfs_s_entry *cur;
  501. struct vfs_s_inode *dir;
  502. };
  503. static void *
  504. vfs_s_opendir (struct vfs_class *me, const char *dirname)
  505. {
  506. struct vfs_s_inode *dir;
  507. struct dirhandle *info;
  508. dir = vfs_s_inode_from_path (me, dirname, FL_DIR | FL_FOLLOW);
  509. if (!dir)
  510. return NULL;
  511. if (!S_ISDIR (dir->st.st_mode))
  512. ERRNOR (ENOTDIR, NULL);
  513. dir->st.st_nlink++;
  514. #if 0
  515. if (!dir->subdir) /* This can actually happen if we allow empty directories */
  516. ERRNOR (EAGAIN, NULL);
  517. #endif
  518. info = g_new (struct dirhandle, 1);
  519. info->cur = dir->subdir;
  520. info->dir = dir;
  521. return info;
  522. }
  523. static void *
  524. vfs_s_readdir(void *data)
  525. {
  526. static union vfs_dirent dir;
  527. struct dirhandle *info = (struct dirhandle *) data;
  528. if (!(info->cur))
  529. return NULL;
  530. if (info->cur->name) {
  531. g_strlcpy (dir.dent.d_name, info->cur->name, MC_MAXPATHLEN);
  532. } else {
  533. vfs_die("Null in structure-cannot happen");
  534. }
  535. compute_namelen(&dir.dent);
  536. info->cur = info->cur->next;
  537. return (void *) &dir;
  538. }
  539. static int
  540. vfs_s_closedir (void *data)
  541. {
  542. struct dirhandle *info = (struct dirhandle *) data;
  543. struct vfs_s_inode *dir = info->dir;
  544. vfs_s_free_inode (dir->super->me, dir);
  545. g_free (data);
  546. return 0;
  547. }
  548. static int
  549. vfs_s_chdir (struct vfs_class *me, const char *path)
  550. {
  551. void *data;
  552. if (!(data = vfs_s_opendir (me, path)))
  553. return -1;
  554. vfs_s_closedir (data);
  555. return 0;
  556. }
  557. /* --------------------------- stat and friends ---------------------------- */
  558. static int
  559. vfs_s_internal_stat (struct vfs_class *me, const char *path, struct stat *buf, int flag)
  560. {
  561. struct vfs_s_inode *ino;
  562. if (!(ino = vfs_s_inode_from_path (me, path, flag)))
  563. return -1;
  564. *buf = ino->st;
  565. return 0;
  566. }
  567. static int
  568. vfs_s_stat (struct vfs_class *me, const char *path, struct stat *buf)
  569. {
  570. return vfs_s_internal_stat (me, path, buf, FL_FOLLOW);
  571. }
  572. static int
  573. vfs_s_lstat (struct vfs_class *me, const char *path, struct stat *buf)
  574. {
  575. return vfs_s_internal_stat (me, path, buf, FL_NONE);
  576. }
  577. static int
  578. vfs_s_fstat (void *fh, struct stat *buf)
  579. {
  580. *buf = FH->ino->st;
  581. return 0;
  582. }
  583. static int
  584. vfs_s_readlink (struct vfs_class *me, const char *path, char *buf, size_t size)
  585. {
  586. struct vfs_s_inode *ino;
  587. size_t len;
  588. ino = vfs_s_inode_from_path (me, path, 0);
  589. if (!ino)
  590. return -1;
  591. if (!S_ISLNK (ino->st.st_mode))
  592. ERRNOR (EINVAL, -1);
  593. if (ino->linkname == NULL)
  594. ERRNOR (EFAULT, -1);
  595. len = strlen (ino->linkname);
  596. if (size < len)
  597. len = size;
  598. /* readlink() does not append a NUL character to buf */
  599. memcpy (buf, ino->linkname, len);
  600. return len;
  601. }
  602. static void *
  603. vfs_s_open (struct vfs_class *me, const char *file, int flags, int mode)
  604. {
  605. int was_changed = 0;
  606. struct vfs_s_fh *fh;
  607. struct vfs_s_super *super;
  608. char *q;
  609. struct vfs_s_inode *ino;
  610. if ((q = vfs_s_get_path (me, file, &super, 0)) == NULL)
  611. return NULL;
  612. ino = vfs_s_find_inode (me, super, q, LINK_FOLLOW, FL_NONE);
  613. if (ino && ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))) {
  614. g_free (q);
  615. ERRNOR (EEXIST, NULL);
  616. }
  617. if (!ino) {
  618. char *dirname, *name, *save;
  619. struct vfs_s_entry *ent;
  620. struct vfs_s_inode *dir;
  621. int tmp_handle;
  622. /* If the filesystem is read-only, disable file creation */
  623. if (!(flags & O_CREAT) || !(me->write)) {
  624. g_free (q);
  625. return NULL;
  626. }
  627. split_dir_name (me, q, &dirname, &name, &save);
  628. /* FIXME: check if vfs_s_find_inode returns NULL */
  629. dir = vfs_s_find_inode (me, super, dirname, LINK_FOLLOW, FL_DIR);
  630. if (save)
  631. *save = PATH_SEP;
  632. ent = vfs_s_generate_entry (me, name, dir, 0755);
  633. ino = ent->ino;
  634. vfs_s_insert_entry (me, dir, ent);
  635. tmp_handle = vfs_mkstemps (&ino->localname, me->name, name);
  636. if (tmp_handle == -1) {
  637. g_free (q);
  638. return NULL;
  639. }
  640. close (tmp_handle);
  641. was_changed = 1;
  642. }
  643. g_free (q);
  644. if (S_ISDIR (ino->st.st_mode))
  645. ERRNOR (EISDIR, NULL);
  646. fh = g_new (struct vfs_s_fh, 1);
  647. fh->pos = 0;
  648. fh->ino = ino;
  649. fh->handle = -1;
  650. fh->changed = was_changed;
  651. fh->linear = 0;
  652. if (IS_LINEAR (flags)) {
  653. if (MEDATA->linear_start) {
  654. print_vfs_message (_("Starting linear transfer..."));
  655. if (!MEDATA->linear_start (me, fh, 0)) {
  656. g_free (fh);
  657. return NULL;
  658. }
  659. }
  660. } else if ((MEDATA->fh_open)
  661. && (MEDATA->fh_open (me, fh, flags, mode))) {
  662. g_free (fh);
  663. return NULL;
  664. }
  665. if (fh->ino->localname) {
  666. fh->handle = open (fh->ino->localname, NO_LINEAR (flags), mode);
  667. if (fh->handle == -1) {
  668. g_free (fh);
  669. ERRNOR (errno, NULL);
  670. }
  671. }
  672. /* i.e. we had no open files and now we have one */
  673. vfs_rmstamp (me, (vfsid) super);
  674. super->fd_usage++;
  675. fh->ino->st.st_nlink++;
  676. return fh;
  677. }
  678. static int
  679. vfs_s_read (void *fh, char *buffer, int count)
  680. {
  681. int n;
  682. struct vfs_class *me = FH_SUPER->me;
  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->handle != -1){ /* If we have local file opened, we want to work with it */
  718. int retval = lseek (FH->handle, offset, whence);
  719. if (retval == -1)
  720. FH->ino->super->me->verrno = errno;
  721. return retval;
  722. }
  723. switch (whence){
  724. case SEEK_CUR:
  725. offset += FH->pos; break;
  726. case SEEK_END:
  727. offset += size; break;
  728. }
  729. if (offset < 0)
  730. FH->pos = 0;
  731. else if (offset < size)
  732. FH->pos = offset;
  733. else
  734. FH->pos = size;
  735. return FH->pos;
  736. }
  737. static int
  738. vfs_s_close (void *fh)
  739. {
  740. int res = 0;
  741. struct vfs_class *me = FH_SUPER->me;
  742. FH_SUPER->fd_usage--;
  743. if (!FH_SUPER->fd_usage)
  744. vfs_stamp_create (me, FH_SUPER);
  745. if (FH->linear == LS_LINEAR_OPEN)
  746. MEDATA->linear_close (me, fh);
  747. if (MEDATA->fh_close)
  748. res = MEDATA->fh_close (me, fh);
  749. if (FH->changed && MEDATA->file_store){
  750. char *s = vfs_s_fullpath (me, FH->ino);
  751. if (!s)
  752. res = -1;
  753. else {
  754. res = MEDATA->file_store (me, fh, s, FH->ino->localname);
  755. g_free (s);
  756. }
  757. vfs_s_invalidate (me, FH_SUPER);
  758. }
  759. if (FH->handle != -1)
  760. close (FH->handle);
  761. vfs_s_free_inode (me, FH->ino);
  762. g_free (fh);
  763. return res;
  764. }
  765. static void
  766. vfs_s_print_stats (const char *fs_name, const char *action,
  767. const char *file_name, off_t have, off_t need)
  768. {
  769. static const char *i18n_percent_transf_format = NULL;
  770. static const char *i18n_transf_format = NULL;
  771. if (i18n_percent_transf_format == NULL) {
  772. i18n_percent_transf_format =
  773. _("%s: %s: %s %3d%% (%lu bytes transferred)");
  774. i18n_transf_format = _("%s: %s: %s %lu bytes transferred");
  775. }
  776. if (need)
  777. print_vfs_message (i18n_percent_transf_format, fs_name, action,
  778. file_name, (int) ((double) have * 100 / need),
  779. (unsigned long) have);
  780. else
  781. print_vfs_message (i18n_transf_format, fs_name, action, file_name,
  782. (unsigned long) have);
  783. }
  784. int
  785. vfs_s_retrieve_file (struct vfs_class *me, struct vfs_s_inode *ino)
  786. {
  787. /* If you want reget, you'll have to open file with O_LINEAR */
  788. off_t total = 0;
  789. char buffer[8192];
  790. int handle, n;
  791. off_t stat_size = ino->st.st_size;
  792. struct vfs_s_fh fh;
  793. memset (&fh, 0, sizeof (fh));
  794. fh.ino = ino;
  795. fh.handle = -1;
  796. handle = vfs_mkstemps (&ino->localname, me->name, ino->ent->name);
  797. if (handle == -1) {
  798. me->verrno = errno;
  799. goto error_4;
  800. }
  801. if (!MEDATA->linear_start (me, &fh, 0))
  802. goto error_3;
  803. /* Clear the interrupt status */
  804. got_interrupt ();
  805. enable_interrupt_key ();
  806. while ((n = MEDATA->linear_read (me, &fh, buffer, sizeof (buffer)))) {
  807. int t;
  808. if (n < 0)
  809. goto error_1;
  810. total += n;
  811. vfs_s_print_stats (me->name, _("Getting file"), ino->ent->name,
  812. total, stat_size);
  813. if (got_interrupt ())
  814. goto error_1;
  815. t = write (handle, buffer, n);
  816. if (t != n) {
  817. if (t == -1)
  818. me->verrno = errno;
  819. goto error_1;
  820. }
  821. }
  822. MEDATA->linear_close (me, &fh);
  823. close (handle);
  824. disable_interrupt_key ();
  825. return 0;
  826. error_1:
  827. MEDATA->linear_close (me, &fh);
  828. error_3:
  829. disable_interrupt_key ();
  830. close (handle);
  831. unlink (ino->localname);
  832. error_4:
  833. g_free (ino->localname);
  834. ino->localname = NULL;
  835. return -1;
  836. }
  837. /* ------------------------------- mc support ---------------------------- */
  838. static void
  839. vfs_s_fill_names (struct vfs_class *me, fill_names_f func)
  840. {
  841. struct vfs_s_super *a = MEDATA->supers;
  842. char *name;
  843. while (a){
  844. name = g_strconcat ( a->name, "#", me->prefix, "/", /* a->current_dir->name, */ NULL);
  845. (*func)(name);
  846. g_free (name);
  847. a = a->next;
  848. }
  849. }
  850. static int
  851. vfs_s_ferrno (struct vfs_class *me)
  852. {
  853. return me->verrno;
  854. }
  855. /*
  856. * Get local copy of the given file. We reuse the existing file cache
  857. * for remote filesystems. Archives use standard VFS facilities.
  858. */
  859. static char *
  860. vfs_s_getlocalcopy (struct vfs_class *me, const char *path)
  861. {
  862. struct vfs_s_fh *fh;
  863. char *local;
  864. fh = vfs_s_open (me, path, O_RDONLY, 0);
  865. if (!fh || !fh->ino || !fh->ino->localname)
  866. return NULL;
  867. local = g_strdup (fh->ino->localname);
  868. vfs_s_close (fh);
  869. return local;
  870. }
  871. /*
  872. * Return the local copy. Since we are using our cache, we do nothing -
  873. * the cache will be removed when the archive is closed.
  874. */
  875. static int
  876. vfs_s_ungetlocalcopy (struct vfs_class *me, const char *path,
  877. const char *local, int has_changed)
  878. {
  879. (void) me;
  880. (void) path;
  881. (void) local;
  882. (void) has_changed;
  883. return 0;
  884. }
  885. static int
  886. vfs_s_setctl (struct vfs_class *me, const char *path, int ctlop, void *arg)
  887. {
  888. switch (ctlop) {
  889. case VFS_SETCTL_STALE_DATA:
  890. {
  891. struct vfs_s_inode *ino = vfs_s_inode_from_path (me, path, 0);
  892. if (!ino)
  893. return 0;
  894. if (arg)
  895. ino->super->want_stale = 1;
  896. else {
  897. ino->super->want_stale = 0;
  898. vfs_s_invalidate (me, ino->super);
  899. }
  900. return 1;
  901. }
  902. case VFS_SETCTL_LOGFILE:
  903. MEDATA->logfile = fopen ((char *) arg, "w");
  904. return 1;
  905. case VFS_SETCTL_FLUSH:
  906. MEDATA->flush = 1;
  907. return 1;
  908. }
  909. return 0;
  910. }
  911. /* ----------------------------- Stamping support -------------------------- */
  912. static vfsid
  913. vfs_s_getid (struct vfs_class *me, const char *path)
  914. {
  915. struct vfs_s_super *archive;
  916. char *p;
  917. if (!(p = vfs_s_get_path (me, path, &archive, FL_NO_OPEN)))
  918. return NULL;
  919. g_free(p);
  920. return (vfsid) archive;
  921. }
  922. static int
  923. vfs_s_nothingisopen (vfsid id)
  924. {
  925. (void) id;
  926. /* Our data structures should survive free of superblock at any time */
  927. return 1;
  928. }
  929. static void
  930. vfs_s_free (vfsid id)
  931. {
  932. vfs_s_free_super (((struct vfs_s_super *)id)->me, (struct vfs_s_super *)id);
  933. }
  934. /* Initialize one of our subclasses - fill common functions */
  935. void
  936. vfs_s_init_class (struct vfs_class *vclass, struct vfs_s_subclass *sub)
  937. {
  938. vclass->data = sub;
  939. vclass->fill_names = vfs_s_fill_names;
  940. vclass->open = vfs_s_open;
  941. vclass->close = vfs_s_close;
  942. vclass->read = vfs_s_read;
  943. if (!(sub->flags & VFS_S_READONLY)) {
  944. vclass->write = vfs_s_write;
  945. }
  946. vclass->opendir = vfs_s_opendir;
  947. vclass->readdir = vfs_s_readdir;
  948. vclass->closedir = vfs_s_closedir;
  949. vclass->stat = vfs_s_stat;
  950. vclass->lstat = vfs_s_lstat;
  951. vclass->fstat = vfs_s_fstat;
  952. vclass->readlink = vfs_s_readlink;
  953. vclass->chdir = vfs_s_chdir;
  954. vclass->ferrno = vfs_s_ferrno;
  955. vclass->lseek = vfs_s_lseek;
  956. vclass->getid = vfs_s_getid;
  957. vclass->nothingisopen = vfs_s_nothingisopen;
  958. vclass->free = vfs_s_free;
  959. if (sub->flags & VFS_S_REMOTE) {
  960. vclass->getlocalcopy = vfs_s_getlocalcopy;
  961. vclass->ungetlocalcopy = vfs_s_ungetlocalcopy;
  962. sub->find_entry = vfs_s_find_entry_linear;
  963. } else {
  964. sub->find_entry = vfs_s_find_entry_tree;
  965. }
  966. vclass->setctl = vfs_s_setctl;
  967. }
  968. /* ----------- Utility functions for networked filesystems -------------- */
  969. #ifdef USE_NETCODE
  970. int
  971. vfs_s_select_on_two (int fd1, int fd2)
  972. {
  973. fd_set set;
  974. struct timeval timeout;
  975. int v;
  976. int maxfd = (fd1 > fd2 ? fd1 : fd2) + 1;
  977. timeout.tv_sec = 1;
  978. timeout.tv_usec = 0;
  979. FD_ZERO (&set);
  980. FD_SET (fd1, &set);
  981. FD_SET (fd2, &set);
  982. v = select (maxfd, &set, 0, 0, &timeout);
  983. if (v <= 0)
  984. return v;
  985. if (FD_ISSET (fd1, &set))
  986. return 1;
  987. if (FD_ISSET (fd2, &set))
  988. return 2;
  989. return -1;
  990. }
  991. int
  992. vfs_s_get_line (struct vfs_class *me, int sock, char *buf, int buf_len, char term)
  993. {
  994. FILE *logfile = MEDATA->logfile;
  995. int i;
  996. char c;
  997. for (i = 0; i < buf_len - 1; i++, buf++){
  998. if (read (sock, buf, sizeof(char)) <= 0)
  999. return 0;
  1000. if (logfile){
  1001. fwrite (buf, 1, 1, logfile);
  1002. fflush (logfile);
  1003. }
  1004. if (*buf == term){
  1005. *buf = 0;
  1006. return 1;
  1007. }
  1008. }
  1009. /* Line is too long - terminate buffer and discard the rest of line */
  1010. *buf = 0;
  1011. while (read (sock, &c, sizeof (c)) > 0) {
  1012. if (logfile){
  1013. fwrite (&c, 1, 1, logfile);
  1014. fflush (logfile);
  1015. }
  1016. if (c == '\n')
  1017. return 1;
  1018. }
  1019. return 0;
  1020. }
  1021. int
  1022. vfs_s_get_line_interruptible (struct vfs_class *me, char *buffer, int size, int fd)
  1023. {
  1024. int n;
  1025. int i;
  1026. (void) me;
  1027. enable_interrupt_key ();
  1028. for (i = 0; i < size-1; i++){
  1029. n = read (fd, buffer+i, 1);
  1030. disable_interrupt_key ();
  1031. if (n == -1 && errno == EINTR){
  1032. buffer [i] = 0;
  1033. return EINTR;
  1034. }
  1035. if (n == 0){
  1036. buffer [i] = 0;
  1037. return 0;
  1038. }
  1039. if (buffer [i] == '\n'){
  1040. buffer [i] = 0;
  1041. return 1;
  1042. }
  1043. }
  1044. buffer [size-1] = 0;
  1045. return 0;
  1046. }
  1047. #endif /* USE_NETCODE */