direntry.c 29 KB

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