direntry.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265
  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 "../vfs/vfs.h"
  40. #include "utilvfs.h"
  41. #include "vfs-impl.h"
  42. #include "gc.h" /* vfs_rmstamp */
  43. #include "xdirentry.h"
  44. #define CALL(x) if (MEDATA->x) MEDATA->x
  45. static volatile int total_inodes = 0, total_entries = 0;
  46. struct vfs_s_inode *
  47. vfs_s_new_inode (struct vfs_class *me, struct vfs_s_super *super, struct stat *initstat)
  48. {
  49. struct vfs_s_inode *ino;
  50. ino = g_try_new0 (struct vfs_s_inode, 1);
  51. if (ino == NULL)
  52. return NULL;
  53. if (initstat)
  54. ino->st = *initstat;
  55. ino->super = super;
  56. ino->st.st_nlink = 0;
  57. ino->st.st_ino = MEDATA->inode_counter++;
  58. ino->st.st_dev = MEDATA->rdev;
  59. super->ino_usage++;
  60. total_inodes++;
  61. CALL (init_inode) (me, ino);
  62. return ino;
  63. }
  64. struct vfs_s_entry *
  65. vfs_s_new_entry (struct vfs_class *me, const char *name, struct vfs_s_inode *inode)
  66. {
  67. struct vfs_s_entry *entry;
  68. entry = g_new0 (struct vfs_s_entry, 1);
  69. total_entries++;
  70. if (name)
  71. entry->name = g_strdup (name);
  72. entry->ino = inode;
  73. entry->ino->ent = entry;
  74. CALL (init_entry) (me, entry);
  75. return entry;
  76. }
  77. static void
  78. vfs_s_free_inode (struct vfs_class *me, struct vfs_s_inode *ino)
  79. {
  80. if (!ino)
  81. vfs_die ("Don't pass NULL to me");
  82. /* ==0 can happen if freshly created entry is deleted */
  83. if (ino->st.st_nlink <= 1){
  84. while (ino->subdir){
  85. vfs_s_free_entry (me, ino->subdir);
  86. }
  87. CALL (free_inode) (me, ino);
  88. g_free (ino->linkname);
  89. if (ino->localname){
  90. unlink (ino->localname);
  91. g_free(ino->localname);
  92. }
  93. total_inodes--;
  94. ino->super->ino_usage--;
  95. g_free(ino);
  96. } else ino->st.st_nlink--;
  97. }
  98. void
  99. vfs_s_free_entry (struct vfs_class *me, struct vfs_s_entry *ent)
  100. {
  101. if (ent->prevp){ /* It is possible that we are deleting freshly created entry */
  102. *ent->prevp = ent->next;
  103. if (ent->next)
  104. ent->next->prevp = ent->prevp;
  105. }
  106. g_free (ent->name);
  107. ent->name = NULL;
  108. if (ent->ino){
  109. ent->ino->ent = NULL;
  110. vfs_s_free_inode (me, ent->ino);
  111. ent->ino = NULL;
  112. }
  113. total_entries--;
  114. g_free(ent);
  115. }
  116. void
  117. vfs_s_insert_entry (struct vfs_class *me, struct vfs_s_inode *dir, struct vfs_s_entry *ent)
  118. {
  119. struct vfs_s_entry **ep;
  120. (void) me;
  121. for (ep = &dir->subdir; *ep != NULL; ep = &((*ep)->next))
  122. ;
  123. ent->prevp = ep;
  124. ent->next = NULL;
  125. ent->dir = dir;
  126. *ep = ent;
  127. ent->ino->st.st_nlink++;
  128. }
  129. struct stat *
  130. vfs_s_default_stat (struct vfs_class *me, mode_t mode)
  131. {
  132. static struct stat st;
  133. int myumask;
  134. (void) me;
  135. myumask = umask (022);
  136. umask (myumask);
  137. mode &= ~myumask;
  138. st.st_mode = mode;
  139. st.st_ino = 0;
  140. st.st_dev = 0;
  141. st.st_rdev = 0;
  142. st.st_uid = getuid ();
  143. st.st_gid = getgid ();
  144. st.st_size = 0;
  145. st.st_mtime = st.st_atime = st.st_ctime = time (NULL);
  146. return &st;
  147. }
  148. struct vfs_s_entry *
  149. vfs_s_generate_entry (struct vfs_class *me, const char *name, struct vfs_s_inode *parent, mode_t mode)
  150. {
  151. struct vfs_s_inode *inode;
  152. struct stat *st;
  153. st = vfs_s_default_stat (me, mode);
  154. inode = vfs_s_new_inode (me, parent->super, st);
  155. return vfs_s_new_entry (me, name, inode);
  156. }
  157. /* We were asked to create entries automagically */
  158. static struct vfs_s_entry *
  159. vfs_s_automake (struct vfs_class *me, struct vfs_s_inode *dir, char *path, int flags)
  160. {
  161. struct vfs_s_entry *res;
  162. char *sep = strchr (path, PATH_SEP);
  163. if (sep)
  164. *sep = 0;
  165. res = vfs_s_generate_entry (me, path, dir, flags & FL_MKDIR ? (0777 | S_IFDIR) : 0777);
  166. vfs_s_insert_entry (me, dir, res);
  167. if (sep)
  168. *sep = PATH_SEP;
  169. return res;
  170. }
  171. /* If the entry is a symlink, find the entry for its target */
  172. static struct vfs_s_entry *
  173. vfs_s_resolve_symlink (struct vfs_class *me, struct vfs_s_entry *entry,
  174. int follow)
  175. {
  176. char *linkname;
  177. char *fullname = NULL;
  178. struct vfs_s_entry *target;
  179. if (follow == LINK_NO_FOLLOW)
  180. return entry;
  181. if (follow == 0)
  182. ERRNOR (ELOOP, NULL);
  183. if (!entry)
  184. ERRNOR (ENOENT, NULL);
  185. if (!S_ISLNK (entry->ino->st.st_mode))
  186. return entry;
  187. linkname = entry->ino->linkname;
  188. if (linkname == NULL)
  189. ERRNOR (EFAULT, NULL);
  190. /* make full path from relative */
  191. if (*linkname != PATH_SEP) {
  192. char *fullpath = vfs_s_fullpath (me, entry->dir);
  193. if (fullpath) {
  194. fullname = g_strconcat (fullpath, "/", linkname, (char *) NULL);
  195. linkname = fullname;
  196. g_free (fullpath);
  197. }
  198. }
  199. target =
  200. (MEDATA->find_entry) (me, entry->dir->super->root, linkname,
  201. follow - 1, 0);
  202. g_free (fullname);
  203. return target;
  204. }
  205. /*
  206. * Follow > 0: follow links, serves as loop protect,
  207. * == -1: do not follow links
  208. */
  209. static struct vfs_s_entry *
  210. vfs_s_find_entry_tree (struct vfs_class *me, struct vfs_s_inode *root,
  211. const char *a_path, int follow, int flags)
  212. {
  213. size_t pseg;
  214. struct vfs_s_entry *ent = NULL;
  215. char * const pathref = g_strdup (a_path);
  216. char *path = pathref;
  217. /* canonicalize as well, but don't remove '../' from path */
  218. custom_canonicalize_pathname (path, CANON_PATH_ALL & (~CANON_PATH_REMDOUBLEDOTS));
  219. while (root) {
  220. while (*path == PATH_SEP) /* Strip leading '/' */
  221. path++;
  222. if (!path[0]) {
  223. g_free (pathref);
  224. return ent;
  225. }
  226. for (pseg = 0; path[pseg] && path[pseg] != PATH_SEP; pseg++);
  227. for (ent = root->subdir; ent != NULL; ent = ent->next)
  228. if (strlen (ent->name) == pseg
  229. && (!strncmp (ent->name, path, pseg)))
  230. /* FOUND! */
  231. break;
  232. if (!ent && (flags & (FL_MKFILE | FL_MKDIR)))
  233. ent = vfs_s_automake (me, root, path, flags);
  234. if (!ent) {
  235. me->verrno = ENOENT;
  236. goto cleanup;
  237. }
  238. path += pseg;
  239. /* here we must follow leading directories always;
  240. only the actual file is optional */
  241. ent =
  242. vfs_s_resolve_symlink (me, ent,
  243. strchr (path,
  244. PATH_SEP) ? LINK_FOLLOW :
  245. follow);
  246. if (!ent)
  247. goto cleanup;
  248. root = ent->ino;
  249. }
  250. cleanup:
  251. g_free (pathref);
  252. return NULL;
  253. }
  254. static void
  255. split_dir_name (struct vfs_class *me, char *path, char **dir, char **name, char **save)
  256. {
  257. char *s;
  258. (void) me;
  259. s = strrchr (path, PATH_SEP);
  260. if (s == NULL) {
  261. *save = NULL;
  262. *name = path;
  263. *dir = path + strlen(path); /* an empty string */
  264. } else {
  265. *save = s;
  266. *dir = path;
  267. *s++ = '\0';
  268. *name = s;
  269. }
  270. }
  271. static struct vfs_s_entry *
  272. vfs_s_find_entry_linear (struct vfs_class *me, struct vfs_s_inode *root,
  273. const char *a_path, int follow, int flags)
  274. {
  275. struct vfs_s_entry *ent = NULL;
  276. char * const path = g_strdup (a_path);
  277. struct vfs_s_entry *retval = NULL;
  278. if (root->super->root != root)
  279. vfs_die ("We have to use _real_ root. Always. Sorry.");
  280. /* canonicalize as well, but don't remove '../' from path */
  281. custom_canonicalize_pathname (path, CANON_PATH_ALL & (~CANON_PATH_REMDOUBLEDOTS));
  282. if (!(flags & FL_DIR)) {
  283. char *dirname, *name, *save;
  284. struct vfs_s_inode *ino;
  285. split_dir_name (me, path, &dirname, &name, &save);
  286. ino =
  287. vfs_s_find_inode (me, root->super, dirname, follow,
  288. flags | FL_DIR);
  289. if (save)
  290. *save = PATH_SEP;
  291. retval = vfs_s_find_entry_tree (me, ino, name, follow, flags);
  292. g_free (path);
  293. return retval;
  294. }
  295. for (ent = root->subdir; ent != NULL; ent = ent->next)
  296. if (!strcmp (ent->name, path))
  297. break;
  298. if (ent && (!(MEDATA->dir_uptodate) (me, ent->ino))) {
  299. #if 1
  300. print_vfs_message (_("Directory cache expired for %s"), path);
  301. #endif
  302. vfs_s_free_entry (me, ent);
  303. ent = NULL;
  304. }
  305. if (!ent) {
  306. struct vfs_s_inode *ino;
  307. ino =
  308. vfs_s_new_inode (me, root->super,
  309. vfs_s_default_stat (me, S_IFDIR | 0755));
  310. ent = vfs_s_new_entry (me, path, ino);
  311. if ((MEDATA->dir_load) (me, ino, path) == -1) {
  312. vfs_s_free_entry (me, ent);
  313. g_free (path);
  314. return NULL;
  315. }
  316. vfs_s_insert_entry (me, root, ent);
  317. for (ent = root->subdir; ent != NULL; ent = ent->next)
  318. if (!strcmp (ent->name, path))
  319. break;
  320. }
  321. if (!ent)
  322. vfs_die ("find_linear: success but directory is not there\n");
  323. #if 0
  324. if (!vfs_s_resolve_symlink (me, ent, follow)) {
  325. g_free (path);
  326. return NULL;
  327. }
  328. #endif
  329. g_free (path);
  330. return ent;
  331. }
  332. struct vfs_s_inode *
  333. vfs_s_find_inode (struct vfs_class *me, const struct vfs_s_super *super,
  334. const char *path, int follow, int flags)
  335. {
  336. struct vfs_s_entry *ent;
  337. if (((MEDATA->flags & VFS_S_REMOTE) == 0) && (*path == '\0'))
  338. return super->root;
  339. ent = (MEDATA->find_entry) (me, super->root, path, follow, flags);
  340. return (ent != NULL) ? ent->ino : NULL;
  341. }
  342. /* Ook, these were functions around directory entries / inodes */
  343. /* -------------------------------- superblock games -------------------------- */
  344. static struct vfs_s_super *
  345. vfs_s_new_super (struct vfs_class *me)
  346. {
  347. struct vfs_s_super *super;
  348. super = g_new0 (struct vfs_s_super, 1);
  349. super->me = me;
  350. return super;
  351. }
  352. static void
  353. vfs_s_insert_super (struct vfs_class *me, struct vfs_s_super *super)
  354. {
  355. super->next = MEDATA->supers;
  356. super->prevp = &MEDATA->supers;
  357. if (MEDATA->supers != NULL)
  358. MEDATA->supers->prevp = &super->next;
  359. MEDATA->supers = super;
  360. }
  361. static void
  362. vfs_s_free_super (struct vfs_class *me, struct vfs_s_super *super)
  363. {
  364. if (super->root){
  365. vfs_s_free_inode (me, super->root);
  366. super->root = NULL;
  367. }
  368. #if 0
  369. /* FIXME: We currently leak small ammount of memory, sometimes. Fix it if you can. */
  370. if (super->ino_usage)
  371. message (D_ERROR, " Direntry warning ",
  372. "Super ino_usage is %d, memory leak",
  373. super->ino_usage);
  374. if (super->want_stale)
  375. message (D_ERROR, " Direntry warning ", "Super has want_stale set");
  376. #endif
  377. if (super->prevp){
  378. *super->prevp = super->next;
  379. if (super->next)
  380. super->next->prevp = super->prevp;
  381. }
  382. CALL (free_archive) (me, super);
  383. g_free (super->name);
  384. g_free(super);
  385. }
  386. /*
  387. * Dissect the path and create corresponding superblock. Note that inname
  388. * can be changed and the result may point inside the original string.
  389. */
  390. const char *
  391. vfs_s_get_path_mangle (struct vfs_class *me, char *inname,
  392. struct vfs_s_super **archive, int flags)
  393. {
  394. const char *retval;
  395. char *local, *op;
  396. const char *archive_name;
  397. int result = -1;
  398. struct vfs_s_super *super;
  399. void *cookie = NULL;
  400. archive_name = inname;
  401. vfs_split (inname, &local, &op);
  402. retval = (local) ? 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. /* 0 == other, 1 == same, return it, 2 == other but stop scanning */
  408. int i = MEDATA->archive_same (me, super, archive_name, op, cookie);
  409. if (i != 0) {
  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, "/",
  854. /* a->current_dir->name, */ (char *) NULL);
  855. (*func)(name);
  856. g_free (name);
  857. a = a->next;
  858. }
  859. }
  860. static int
  861. vfs_s_ferrno (struct vfs_class *me)
  862. {
  863. return me->verrno;
  864. }
  865. /*
  866. * Get local copy of the given file. We reuse the existing file cache
  867. * for remote filesystems. Archives use standard VFS facilities.
  868. */
  869. static char *
  870. vfs_s_getlocalcopy (struct vfs_class *me, const char *path)
  871. {
  872. struct vfs_s_fh *fh;
  873. char *local;
  874. fh = vfs_s_open (me, path, O_RDONLY, 0);
  875. if (!fh || !fh->ino || !fh->ino->localname)
  876. return NULL;
  877. local = g_strdup (fh->ino->localname);
  878. vfs_s_close (fh);
  879. return local;
  880. }
  881. /*
  882. * Return the local copy. Since we are using our cache, we do nothing -
  883. * the cache will be removed when the archive is closed.
  884. */
  885. static int
  886. vfs_s_ungetlocalcopy (struct vfs_class *me, const char *path,
  887. const char *local, int has_changed)
  888. {
  889. (void) me;
  890. (void) path;
  891. (void) local;
  892. (void) has_changed;
  893. return 0;
  894. }
  895. static int
  896. vfs_s_setctl (struct vfs_class *me, const char *path, int ctlop, void *arg)
  897. {
  898. switch (ctlop) {
  899. case VFS_SETCTL_STALE_DATA:
  900. {
  901. struct vfs_s_inode *ino = vfs_s_inode_from_path (me, path, 0);
  902. if (!ino)
  903. return 0;
  904. if (arg)
  905. ino->super->want_stale = 1;
  906. else {
  907. ino->super->want_stale = 0;
  908. vfs_s_invalidate (me, ino->super);
  909. }
  910. return 1;
  911. }
  912. case VFS_SETCTL_LOGFILE:
  913. MEDATA->logfile = fopen ((char *) arg, "w");
  914. return 1;
  915. case VFS_SETCTL_FLUSH:
  916. MEDATA->flush = 1;
  917. return 1;
  918. }
  919. return 0;
  920. }
  921. /* ----------------------------- Stamping support -------------------------- */
  922. static vfsid
  923. vfs_s_getid (struct vfs_class *me, const char *path)
  924. {
  925. struct vfs_s_super *archive;
  926. char *p;
  927. if (!(p = vfs_s_get_path (me, path, &archive, FL_NO_OPEN)))
  928. return NULL;
  929. g_free(p);
  930. return (vfsid) archive;
  931. }
  932. static int
  933. vfs_s_nothingisopen (vfsid id)
  934. {
  935. (void) id;
  936. /* Our data structures should survive free of superblock at any time */
  937. return 1;
  938. }
  939. static void
  940. vfs_s_free (vfsid id)
  941. {
  942. vfs_s_free_super (((struct vfs_s_super *)id)->me, (struct vfs_s_super *)id);
  943. }
  944. static int
  945. vfs_s_dir_uptodate (struct vfs_class *me, struct vfs_s_inode *ino)
  946. {
  947. struct timeval tim;
  948. if (MEDATA->flush) {
  949. MEDATA->flush = 0;
  950. return 0;
  951. }
  952. gettimeofday(&tim, NULL);
  953. if (tim.tv_sec < ino->timestamp.tv_sec)
  954. return 1;
  955. return 0;
  956. }
  957. /* Initialize one of our subclasses - fill common functions */
  958. void
  959. vfs_s_init_class (struct vfs_class *vclass, struct vfs_s_subclass *sub)
  960. {
  961. vclass->data = sub;
  962. vclass->fill_names = vfs_s_fill_names;
  963. vclass->open = vfs_s_open;
  964. vclass->close = vfs_s_close;
  965. vclass->read = vfs_s_read;
  966. if (!(sub->flags & VFS_S_READONLY)) {
  967. vclass->write = vfs_s_write;
  968. }
  969. vclass->opendir = vfs_s_opendir;
  970. vclass->readdir = vfs_s_readdir;
  971. vclass->closedir = vfs_s_closedir;
  972. vclass->stat = vfs_s_stat;
  973. vclass->lstat = vfs_s_lstat;
  974. vclass->fstat = vfs_s_fstat;
  975. vclass->readlink = vfs_s_readlink;
  976. vclass->chdir = vfs_s_chdir;
  977. vclass->ferrno = vfs_s_ferrno;
  978. vclass->lseek = vfs_s_lseek;
  979. vclass->getid = vfs_s_getid;
  980. vclass->nothingisopen = vfs_s_nothingisopen;
  981. vclass->free = vfs_s_free;
  982. if (sub->flags & VFS_S_REMOTE) {
  983. vclass->getlocalcopy = vfs_s_getlocalcopy;
  984. vclass->ungetlocalcopy = vfs_s_ungetlocalcopy;
  985. sub->find_entry = vfs_s_find_entry_linear;
  986. } else {
  987. sub->find_entry = vfs_s_find_entry_tree;
  988. }
  989. vclass->setctl = vfs_s_setctl;
  990. sub->dir_uptodate = vfs_s_dir_uptodate;
  991. }
  992. /* ----------- Utility functions for networked filesystems -------------- */
  993. #ifdef USE_NETCODE
  994. int
  995. vfs_s_select_on_two (int fd1, int fd2)
  996. {
  997. fd_set set;
  998. struct timeval timeout;
  999. int v;
  1000. int maxfd = (fd1 > fd2 ? fd1 : fd2) + 1;
  1001. timeout.tv_sec = 1;
  1002. timeout.tv_usec = 0;
  1003. FD_ZERO (&set);
  1004. FD_SET (fd1, &set);
  1005. FD_SET (fd2, &set);
  1006. v = select (maxfd, &set, 0, 0, &timeout);
  1007. if (v <= 0)
  1008. return v;
  1009. if (FD_ISSET (fd1, &set))
  1010. return 1;
  1011. if (FD_ISSET (fd2, &set))
  1012. return 2;
  1013. return -1;
  1014. }
  1015. int
  1016. vfs_s_get_line (struct vfs_class *me, int sock, char *buf, int buf_len, char term)
  1017. {
  1018. FILE *logfile = MEDATA->logfile;
  1019. int i;
  1020. char c;
  1021. for (i = 0; i < buf_len - 1; i++, buf++){
  1022. if (read (sock, buf, sizeof(char)) <= 0)
  1023. return 0;
  1024. if (logfile){
  1025. fwrite (buf, 1, 1, logfile);
  1026. fflush (logfile);
  1027. }
  1028. if (*buf == term){
  1029. *buf = 0;
  1030. return 1;
  1031. }
  1032. }
  1033. /* Line is too long - terminate buffer and discard the rest of line */
  1034. *buf = 0;
  1035. while (read (sock, &c, sizeof (c)) > 0) {
  1036. if (logfile){
  1037. fwrite (&c, 1, 1, logfile);
  1038. fflush (logfile);
  1039. }
  1040. if (c == '\n')
  1041. return 1;
  1042. }
  1043. return 0;
  1044. }
  1045. int
  1046. vfs_s_get_line_interruptible (struct vfs_class *me, char *buffer, int size, int fd)
  1047. {
  1048. int n;
  1049. int i;
  1050. (void) me;
  1051. tty_enable_interrupt_key ();
  1052. for (i = 0; i < size-1; i++){
  1053. n = read (fd, buffer+i, 1);
  1054. tty_disable_interrupt_key ();
  1055. if (n == -1 && errno == EINTR){
  1056. buffer [i] = 0;
  1057. return EINTR;
  1058. }
  1059. if (n == 0){
  1060. buffer [i] = 0;
  1061. return 0;
  1062. }
  1063. if (buffer [i] == '\n'){
  1064. buffer [i] = 0;
  1065. return 1;
  1066. }
  1067. }
  1068. buffer [size-1] = 0;
  1069. return 0;
  1070. }
  1071. #endif /* USE_NETCODE */