direntry.c 27 KB

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