direntry.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562
  1. /*
  2. Directory cache support
  3. Copyright (C) 1998, 2011
  4. The Free Software Foundation, Inc.
  5. Written by:
  6. Pavel Machek <pavel@ucw.cz>, 1998
  7. This file is part of the Midnight Commander.
  8. The Midnight Commander is free software: you can redistribute it
  9. and/or modify it under the terms of the GNU General Public License as
  10. published by the Free Software Foundation, either version 3 of the License,
  11. or (at your option) any later version.
  12. The Midnight Commander is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. \warning Paths here do _not_ begin with '/', so root directory of
  19. archive/site is simply "".
  20. */
  21. /** \file
  22. * \brief Source: directory cache support
  23. *
  24. * So that you do not have copy of this in each and every filesystem.
  25. *
  26. * Very loosely based on tar.c from midnight and archives.[ch] from
  27. * avfs by Miklos Szeredi (mszeredi@inf.bme.hu)
  28. *
  29. * Unfortunately, I was unable to keep all filesystems
  30. * uniform. tar-like filesystems use tree structure where each
  31. * directory has pointers to its subdirectories. We can do this
  32. * because we have full information about our archive.
  33. *
  34. * At ftp-like filesystems, situation is a little bit different. When
  35. * you cd /usr/src/linux/drivers/char, you do _not_ want /usr,
  36. * /usr/src, /usr/src/linux and /usr/src/linux/drivers to be
  37. * listed. That means that we do not have complete information, and if
  38. * /usr is symlink to /4, we will not know. Also we have to time out
  39. * entries and things would get messy with tree-like approach. So we
  40. * do different trick: root directory is completely special and
  41. * completely fake, it contains entries such as 'usr', 'usr/src', ...,
  42. * and we'll try to use custom find_entry function.
  43. *
  44. * \author Pavel Machek <pavel@ucw.cz>
  45. * \date 1998
  46. *
  47. */
  48. #include <config.h>
  49. #include <errno.h>
  50. #include <fcntl.h> /* include fcntl.h -> sys/fcntl.h only */
  51. /* includes fcntl.h see IEEE Std 1003.1-2008 */
  52. #include <time.h>
  53. #include <sys/time.h> /* gettimeofday() */
  54. #include <inttypes.h> /* uintmax_t */
  55. #include <stdarg.h>
  56. #include "lib/global.h"
  57. #include "lib/tty/tty.h" /* enable/disable interrupt key */
  58. #include "lib/util.h" /* concat_dir_and_file */
  59. #if 0
  60. #include "lib/widget.h" /* message() */
  61. #endif
  62. #include "vfs.h"
  63. #include "utilvfs.h"
  64. #include "xdirentry.h"
  65. #include "gc.h" /* vfs_rmstamp */
  66. /*** global variables ****************************************************************************/
  67. /*** file scope macro definitions ****************************************************************/
  68. #define CALL(x) if (MEDATA->x) MEDATA->x
  69. /*** file scope type declarations ****************************************************************/
  70. struct dirhandle
  71. {
  72. GList *cur;
  73. struct vfs_s_inode *dir;
  74. };
  75. /*** file scope variables ************************************************************************/
  76. static volatile int total_inodes = 0, total_entries = 0;
  77. /*** file scope functions ************************************************************************/
  78. /* --------------------------------------------------------------------------------------------- */
  79. static int
  80. vfs_s_entry_compare (const void *a, const void *b)
  81. {
  82. const struct vfs_s_entry *e = (const struct vfs_s_entry *) a;
  83. const char *name = (const char *) b;
  84. return strcmp (e->name, name);
  85. }
  86. /* --------------------------------------------------------------------------------------------- */
  87. static void
  88. vfs_s_free_inode (struct vfs_class *me, struct vfs_s_inode *ino)
  89. {
  90. if (ino == NULL)
  91. vfs_die ("Don't pass NULL to me");
  92. /* ==0 can happen if freshly created entry is deleted */
  93. if (ino->st.st_nlink > 1)
  94. {
  95. ino->st.st_nlink--;
  96. return;
  97. }
  98. while (ino->subdir != NULL)
  99. vfs_s_free_entry (me, (struct vfs_s_entry *) ino->subdir->data);
  100. CALL (free_inode) (me, ino);
  101. g_free (ino->linkname);
  102. if (ino->localname != NULL)
  103. {
  104. unlink (ino->localname);
  105. g_free (ino->localname);
  106. }
  107. total_inodes--;
  108. ino->super->ino_usage--;
  109. g_free (ino);
  110. }
  111. /* --------------------------------------------------------------------------------------------- */
  112. /* We were asked to create entries automagically */
  113. static struct vfs_s_entry *
  114. vfs_s_automake (struct vfs_class *me, struct vfs_s_inode *dir, char *path, int flags)
  115. {
  116. struct vfs_s_entry *res;
  117. char *sep;
  118. sep = strchr (path, PATH_SEP);
  119. if (sep != NULL)
  120. *sep = '\0';
  121. res = vfs_s_generate_entry (me, path, dir, flags & FL_MKDIR ? (0777 | S_IFDIR) : 0777);
  122. vfs_s_insert_entry (me, dir, res);
  123. if (sep != NULL)
  124. *sep = PATH_SEP;
  125. return res;
  126. }
  127. /* --------------------------------------------------------------------------------------------- */
  128. /* If the entry is a symlink, find the entry for its target */
  129. static struct vfs_s_entry *
  130. vfs_s_resolve_symlink (struct vfs_class *me, struct vfs_s_entry *entry, int follow)
  131. {
  132. char *linkname;
  133. char *fullname = NULL;
  134. struct vfs_s_entry *target;
  135. if (follow == LINK_NO_FOLLOW)
  136. return entry;
  137. if (follow == 0)
  138. ERRNOR (ELOOP, NULL);
  139. if (!entry)
  140. ERRNOR (ENOENT, NULL);
  141. if (!S_ISLNK (entry->ino->st.st_mode))
  142. return entry;
  143. linkname = entry->ino->linkname;
  144. if (linkname == NULL)
  145. ERRNOR (EFAULT, NULL);
  146. /* make full path from relative */
  147. if (*linkname != PATH_SEP)
  148. {
  149. char *fullpath = vfs_s_fullpath (me, entry->dir);
  150. if (fullpath)
  151. {
  152. fullname = g_strconcat (fullpath, "/", linkname, (char *) NULL);
  153. linkname = fullname;
  154. g_free (fullpath);
  155. }
  156. }
  157. target = (MEDATA->find_entry) (me, entry->dir->super->root, linkname, follow - 1, 0);
  158. g_free (fullname);
  159. return target;
  160. }
  161. /* --------------------------------------------------------------------------------------------- */
  162. /*
  163. * Follow > 0: follow links, serves as loop protect,
  164. * == -1: do not follow links
  165. */
  166. static struct vfs_s_entry *
  167. vfs_s_find_entry_tree (struct vfs_class *me, struct vfs_s_inode *root,
  168. const char *a_path, int follow, int flags)
  169. {
  170. size_t pseg;
  171. struct vfs_s_entry *ent = NULL;
  172. char *const pathref = g_strdup (a_path);
  173. char *path = pathref;
  174. /* canonicalize as well, but don't remove '../' from path */
  175. custom_canonicalize_pathname (path, CANON_PATH_ALL & (~CANON_PATH_REMDOUBLEDOTS));
  176. while (root != NULL)
  177. {
  178. GList *iter;
  179. while (*path == PATH_SEP) /* Strip leading '/' */
  180. path++;
  181. if (path[0] == '\0')
  182. {
  183. g_free (pathref);
  184. return ent;
  185. }
  186. for (pseg = 0; path[pseg] != '\0' && path[pseg] != PATH_SEP; pseg++)
  187. ;
  188. for (iter = root->subdir; iter != NULL; iter = g_list_next (iter))
  189. {
  190. ent = (struct vfs_s_entry *) iter->data;
  191. if (strlen (ent->name) == pseg && strncmp (ent->name, path, pseg) == 0)
  192. /* FOUND! */
  193. break;
  194. }
  195. ent = iter != NULL ? (struct vfs_s_entry *) iter->data : NULL;
  196. if (ent == NULL && (flags & (FL_MKFILE | FL_MKDIR)) != 0)
  197. ent = vfs_s_automake (me, root, path, flags);
  198. if (ent == NULL)
  199. {
  200. me->verrno = ENOENT;
  201. goto cleanup;
  202. }
  203. path += pseg;
  204. /* here we must follow leading directories always;
  205. only the actual file is optional */
  206. ent = vfs_s_resolve_symlink (me, ent,
  207. strchr (path, PATH_SEP) != NULL ? LINK_FOLLOW : follow);
  208. if (ent == NULL)
  209. goto cleanup;
  210. root = ent->ino;
  211. }
  212. cleanup:
  213. g_free (pathref);
  214. return NULL;
  215. }
  216. /* --------------------------------------------------------------------------------------------- */
  217. static void
  218. split_dir_name (struct vfs_class *me, char *path, char **dir, char **name, char **save)
  219. {
  220. char *s;
  221. (void) me;
  222. s = strrchr (path, PATH_SEP);
  223. if (s == NULL)
  224. {
  225. *save = NULL;
  226. *name = path;
  227. *dir = path + strlen (path); /* an empty string */
  228. }
  229. else
  230. {
  231. *save = s;
  232. *dir = path;
  233. *s++ = '\0';
  234. *name = s;
  235. }
  236. }
  237. /* --------------------------------------------------------------------------------------------- */
  238. static struct vfs_s_entry *
  239. vfs_s_find_entry_linear (struct vfs_class *me, struct vfs_s_inode *root,
  240. const char *a_path, int follow, int flags)
  241. {
  242. struct vfs_s_entry *ent = NULL;
  243. char *const path = g_strdup (a_path);
  244. struct vfs_s_entry *retval = NULL;
  245. GList *iter;
  246. if (root->super->root != root)
  247. vfs_die ("We have to use _real_ root. Always. Sorry.");
  248. /* canonicalize as well, but don't remove '../' from path */
  249. custom_canonicalize_pathname (path, CANON_PATH_ALL & (~CANON_PATH_REMDOUBLEDOTS));
  250. if ((flags & FL_DIR) == 0)
  251. {
  252. char *dirname, *name, *save;
  253. struct vfs_s_inode *ino;
  254. split_dir_name (me, path, &dirname, &name, &save);
  255. ino = vfs_s_find_inode (me, root->super, dirname, follow, flags | FL_DIR);
  256. if (save != NULL)
  257. *save = PATH_SEP;
  258. retval = vfs_s_find_entry_tree (me, ino, name, follow, flags);
  259. g_free (path);
  260. return retval;
  261. }
  262. iter = g_list_find_custom (root->subdir, path, (GCompareFunc) vfs_s_entry_compare);
  263. ent = iter != NULL ? (struct vfs_s_entry *) iter->data : NULL;
  264. if (ent != NULL && !MEDATA->dir_uptodate (me, ent->ino))
  265. {
  266. #if 1
  267. vfs_print_message (_("Directory cache expired for %s"), path);
  268. #endif
  269. vfs_s_free_entry (me, ent);
  270. ent = NULL;
  271. }
  272. if (ent == NULL)
  273. {
  274. struct vfs_s_inode *ino;
  275. ino = vfs_s_new_inode (me, root->super, vfs_s_default_stat (me, S_IFDIR | 0755));
  276. ent = vfs_s_new_entry (me, path, ino);
  277. if (MEDATA->dir_load (me, ino, path) == -1)
  278. {
  279. vfs_s_free_entry (me, ent);
  280. g_free (path);
  281. return NULL;
  282. }
  283. vfs_s_insert_entry (me, root, ent);
  284. iter = g_list_find_custom (root->subdir, path, (GCompareFunc) vfs_s_entry_compare);
  285. ent = iter != NULL ? (struct vfs_s_entry *) iter->data : NULL;
  286. }
  287. if (ent == NULL)
  288. vfs_die ("find_linear: success but directory is not there\n");
  289. #if 0
  290. if (!vfs_s_resolve_symlink (me, ent, follow))
  291. {
  292. g_free (path);
  293. return NULL;
  294. }
  295. #endif
  296. g_free (path);
  297. return ent;
  298. }
  299. /* --------------------------------------------------------------------------------------------- */
  300. /* Ook, these were functions around directory entries / inodes */
  301. /* -------------------------------- superblock games -------------------------- */
  302. static struct vfs_s_super *
  303. vfs_s_new_super (struct vfs_class *me)
  304. {
  305. struct vfs_s_super *super;
  306. super = g_new0 (struct vfs_s_super, 1);
  307. super->me = me;
  308. return super;
  309. }
  310. /* --------------------------------------------------------------------------------------------- */
  311. static inline void
  312. vfs_s_insert_super (struct vfs_class *me, struct vfs_s_super *super)
  313. {
  314. MEDATA->supers = g_list_prepend (MEDATA->supers, super);
  315. }
  316. /* --------------------------------------------------------------------------------------------- */
  317. static void
  318. vfs_s_free_super (struct vfs_class *me, struct vfs_s_super *super)
  319. {
  320. if (super->root != NULL)
  321. {
  322. vfs_s_free_inode (me, super->root);
  323. super->root = NULL;
  324. }
  325. #if 0
  326. /* FIXME: We currently leak small ammount of memory, sometimes. Fix it if you can. */
  327. if (super->ino_usage)
  328. message (D_ERROR, "Direntry warning",
  329. "Super ino_usage is %d, memory leak", super->ino_usage);
  330. if (super->want_stale)
  331. message (D_ERROR, "Direntry warning", "%s", "Super has want_stale set");
  332. #endif
  333. MEDATA->supers = g_list_remove (MEDATA->supers, super);
  334. CALL (free_archive) (me, super);
  335. #ifdef ENABLE_VFS_NET
  336. vfs_path_element_free (super->path_element);
  337. #endif
  338. g_free (super->name);
  339. g_free (super);
  340. }
  341. /* --------------------------------------------------------------------------------------------- */
  342. /* Support of archives */
  343. /* ------------------------ readdir & friends ----------------------------- */
  344. static struct vfs_s_inode *
  345. vfs_s_inode_from_path (const vfs_path_t * vpath, int flags)
  346. {
  347. struct vfs_s_super *super;
  348. struct vfs_s_inode *ino;
  349. const char *q;
  350. vfs_path_element_t *path_element;
  351. q = vfs_s_get_path (vpath, &super, 0);
  352. if (q == NULL)
  353. return NULL;
  354. path_element = vfs_path_get_by_index (vpath, -1);
  355. ino =
  356. vfs_s_find_inode (path_element->class, super, q,
  357. flags & FL_FOLLOW ? LINK_FOLLOW : LINK_NO_FOLLOW, flags & ~FL_FOLLOW);
  358. if ((!ino) && (!*q))
  359. /* We are asking about / directory of ftp server: assume it exists */
  360. ino =
  361. vfs_s_find_inode (path_element->class, super, q,
  362. flags & FL_FOLLOW ? LINK_FOLLOW :
  363. LINK_NO_FOLLOW, FL_DIR | (flags & ~FL_FOLLOW));
  364. return ino;
  365. }
  366. /* --------------------------------------------------------------------------------------------- */
  367. static void *
  368. vfs_s_opendir (const vfs_path_t * vpath)
  369. {
  370. struct vfs_s_inode *dir;
  371. struct dirhandle *info;
  372. vfs_path_element_t *path_element;
  373. path_element = vfs_path_get_by_index (vpath, -1);
  374. dir = vfs_s_inode_from_path (vpath, FL_DIR | FL_FOLLOW);
  375. if (dir == NULL)
  376. return NULL;
  377. if (!S_ISDIR (dir->st.st_mode))
  378. {
  379. path_element->class->verrno = ENOTDIR;
  380. return NULL;
  381. }
  382. dir->st.st_nlink++;
  383. #if 0
  384. if (dir->subdir == NULL) /* This can actually happen if we allow empty directories */
  385. {
  386. path_element->class->verrno = EAGAIN;
  387. return NULL;
  388. }
  389. #endif
  390. info = g_new (struct dirhandle, 1);
  391. info->cur = dir->subdir;
  392. info->dir = dir;
  393. return info;
  394. }
  395. /* --------------------------------------------------------------------------------------------- */
  396. static void *
  397. vfs_s_readdir (void *data)
  398. {
  399. static union vfs_dirent dir;
  400. struct dirhandle *info = (struct dirhandle *) data;
  401. const char *name;
  402. if (info->cur == NULL || info->cur->data == NULL)
  403. return NULL;
  404. name = ((struct vfs_s_entry *) info->cur->data)->name;
  405. if (name != NULL)
  406. g_strlcpy (dir.dent.d_name, name, MC_MAXPATHLEN);
  407. else
  408. vfs_die ("Null in structure-cannot happen");
  409. compute_namelen (&dir.dent);
  410. info->cur = g_list_next (info->cur);
  411. return (void *) &dir;
  412. }
  413. /* --------------------------------------------------------------------------------------------- */
  414. static int
  415. vfs_s_closedir (void *data)
  416. {
  417. struct dirhandle *info = (struct dirhandle *) data;
  418. struct vfs_s_inode *dir = info->dir;
  419. vfs_s_free_inode (dir->super->me, dir);
  420. g_free (data);
  421. return 0;
  422. }
  423. /* --------------------------------------------------------------------------------------------- */
  424. static int
  425. vfs_s_chdir (const vfs_path_t * vpath)
  426. {
  427. void *data;
  428. data = vfs_s_opendir (vpath);
  429. if (data == NULL)
  430. return -1;
  431. vfs_s_closedir (data);
  432. return 0;
  433. }
  434. /* --------------------------------------------------------------------------------------------- */
  435. /* --------------------------- stat and friends ---------------------------- */
  436. static int
  437. vfs_s_internal_stat (const vfs_path_t * vpath, struct stat *buf, int flag)
  438. {
  439. struct vfs_s_inode *ino;
  440. ino = vfs_s_inode_from_path (vpath, flag);
  441. if (ino == NULL)
  442. return -1;
  443. *buf = ino->st;
  444. return 0;
  445. }
  446. /* --------------------------------------------------------------------------------------------- */
  447. static int
  448. vfs_s_stat (const vfs_path_t * vpath, struct stat *buf)
  449. {
  450. return vfs_s_internal_stat (vpath, buf, FL_FOLLOW);
  451. }
  452. /* --------------------------------------------------------------------------------------------- */
  453. static int
  454. vfs_s_lstat (const vfs_path_t * vpath, struct stat *buf)
  455. {
  456. return vfs_s_internal_stat (vpath, buf, FL_NONE);
  457. }
  458. /* --------------------------------------------------------------------------------------------- */
  459. static int
  460. vfs_s_fstat (void *fh, struct stat *buf)
  461. {
  462. *buf = FH->ino->st;
  463. return 0;
  464. }
  465. /* --------------------------------------------------------------------------------------------- */
  466. static int
  467. vfs_s_readlink (const vfs_path_t * vpath, char *buf, size_t size)
  468. {
  469. struct vfs_s_inode *ino;
  470. size_t len;
  471. vfs_path_element_t *path_element;
  472. path_element = vfs_path_get_by_index (vpath, -1);
  473. ino = vfs_s_inode_from_path (vpath, 0);
  474. if (!ino)
  475. return -1;
  476. if (!S_ISLNK (ino->st.st_mode))
  477. {
  478. path_element->class->verrno = EINVAL;
  479. return -1;
  480. }
  481. if (ino->linkname == NULL)
  482. {
  483. path_element->class->verrno = EFAULT;
  484. return -1;
  485. }
  486. len = strlen (ino->linkname);
  487. if (size < len)
  488. len = size;
  489. /* readlink() does not append a NUL character to buf */
  490. memcpy (buf, ino->linkname, len);
  491. return len;
  492. }
  493. /* --------------------------------------------------------------------------------------------- */
  494. static ssize_t
  495. vfs_s_read (void *fh, char *buffer, size_t count)
  496. {
  497. int n;
  498. struct vfs_class *me = FH_SUPER->me;
  499. if (FH->linear == LS_LINEAR_PREOPEN)
  500. {
  501. if (!MEDATA->linear_start (me, FH, FH->pos))
  502. return -1;
  503. }
  504. if (FH->linear == LS_LINEAR_CLOSED)
  505. vfs_die ("linear_start() did not set linear_state!");
  506. if (FH->linear == LS_LINEAR_OPEN)
  507. return MEDATA->linear_read (me, FH, buffer, count);
  508. if (FH->handle != -1)
  509. {
  510. n = read (FH->handle, buffer, count);
  511. if (n < 0)
  512. me->verrno = errno;
  513. return n;
  514. }
  515. vfs_die ("vfs_s_read: This should not happen\n");
  516. return -1;
  517. }
  518. /* --------------------------------------------------------------------------------------------- */
  519. static ssize_t
  520. vfs_s_write (void *fh, const char *buffer, size_t count)
  521. {
  522. int n;
  523. struct vfs_class *me = FH_SUPER->me;
  524. if (FH->linear)
  525. vfs_die ("no writing to linear files, please");
  526. FH->changed = 1;
  527. if (FH->handle != -1)
  528. {
  529. n = write (FH->handle, buffer, count);
  530. if (n < 0)
  531. me->verrno = errno;
  532. return n;
  533. }
  534. vfs_die ("vfs_s_write: This should not happen\n");
  535. return 0;
  536. }
  537. /* --------------------------------------------------------------------------------------------- */
  538. static off_t
  539. vfs_s_lseek (void *fh, off_t offset, int whence)
  540. {
  541. off_t size = FH->ino->st.st_size;
  542. if (FH->linear == LS_LINEAR_OPEN)
  543. vfs_die ("cannot lseek() after linear_read!");
  544. if (FH->handle != -1)
  545. { /* If we have local file opened, we want to work with it */
  546. off_t retval = lseek (FH->handle, offset, whence);
  547. if (retval == -1)
  548. FH->ino->super->me->verrno = errno;
  549. return retval;
  550. }
  551. switch (whence)
  552. {
  553. case SEEK_CUR:
  554. offset += FH->pos;
  555. break;
  556. case SEEK_END:
  557. offset += size;
  558. break;
  559. }
  560. if (offset < 0)
  561. FH->pos = 0;
  562. else if (offset < size)
  563. FH->pos = offset;
  564. else
  565. FH->pos = size;
  566. return FH->pos;
  567. }
  568. /* --------------------------------------------------------------------------------------------- */
  569. static int
  570. vfs_s_close (void *fh)
  571. {
  572. int res = 0;
  573. struct vfs_class *me = FH_SUPER->me;
  574. FH_SUPER->fd_usage--;
  575. if (!FH_SUPER->fd_usage)
  576. vfs_stamp_create (me, FH_SUPER);
  577. if (FH->linear == LS_LINEAR_OPEN)
  578. MEDATA->linear_close (me, fh);
  579. if (MEDATA->fh_close)
  580. res = MEDATA->fh_close (me, fh);
  581. if (FH->changed && MEDATA->file_store)
  582. {
  583. char *s = vfs_s_fullpath (me, FH->ino);
  584. if (!s)
  585. res = -1;
  586. else
  587. {
  588. res = MEDATA->file_store (me, fh, s, FH->ino->localname);
  589. g_free (s);
  590. }
  591. vfs_s_invalidate (me, FH_SUPER);
  592. }
  593. if (FH->handle != -1)
  594. close (FH->handle);
  595. vfs_s_free_inode (me, FH->ino);
  596. if (MEDATA->fh_free_data != NULL)
  597. MEDATA->fh_free_data (fh);
  598. g_free (fh);
  599. return res;
  600. }
  601. /* --------------------------------------------------------------------------------------------- */
  602. static void
  603. vfs_s_print_stats (const char *fs_name, const char *action,
  604. const char *file_name, off_t have, off_t need)
  605. {
  606. static const char *i18n_percent_transf_format = NULL;
  607. static const char *i18n_transf_format = NULL;
  608. if (i18n_percent_transf_format == NULL)
  609. {
  610. i18n_percent_transf_format = "%s: %s: %s %3d%% (%" PRIuMAX " %s";
  611. i18n_transf_format = "%s: %s: %s %" PRIuMAX " %s";
  612. }
  613. if (need)
  614. vfs_print_message (i18n_percent_transf_format, fs_name, action,
  615. file_name, (int) ((double) have * 100 / need), (uintmax_t) have,
  616. _("bytes transferred"));
  617. else
  618. vfs_print_message (i18n_transf_format, fs_name, action, file_name, (uintmax_t) have,
  619. _("bytes transferred"));
  620. }
  621. /* --------------------------------------------------------------------------------------------- */
  622. /* ------------------------------- mc support ---------------------------- */
  623. static void
  624. vfs_s_fill_names (struct vfs_class *me, fill_names_f func)
  625. {
  626. GList *iter;
  627. for (iter = MEDATA->supers; iter != NULL; iter = g_list_next (iter))
  628. {
  629. const struct vfs_s_super *super = (const struct vfs_s_super *) iter->data;
  630. char *name;
  631. name = g_strconcat (super->name, "/", me->prefix, VFS_PATH_URL_DELIMITER,
  632. /* super->current_dir->name, */ (char *) NULL);
  633. func (name);
  634. g_free (name);
  635. }
  636. }
  637. /* --------------------------------------------------------------------------------------------- */
  638. static int
  639. vfs_s_ferrno (struct vfs_class *me)
  640. {
  641. return me->verrno;
  642. }
  643. /* --------------------------------------------------------------------------------------------- */
  644. /**
  645. * Get local copy of the given file. We reuse the existing file cache
  646. * for remote filesystems. Archives use standard VFS facilities.
  647. */
  648. static char *
  649. vfs_s_getlocalcopy (const vfs_path_t * vpath)
  650. {
  651. vfs_file_handler_t *fh;
  652. char *local = NULL;
  653. fh = vfs_s_open (vpath, O_RDONLY, 0);
  654. if (fh != NULL)
  655. {
  656. if ((fh->ino != NULL) && (fh->ino->localname != NULL))
  657. local = g_strdup (fh->ino->localname);
  658. vfs_s_close (fh);
  659. }
  660. return local;
  661. }
  662. /* --------------------------------------------------------------------------------------------- */
  663. /**
  664. * Return the local copy. Since we are using our cache, we do nothing -
  665. * the cache will be removed when the archive is closed.
  666. */
  667. static int
  668. vfs_s_ungetlocalcopy (const vfs_path_t * vpath, const char *local, int has_changed)
  669. {
  670. (void) vpath;
  671. (void) local;
  672. (void) has_changed;
  673. return 0;
  674. }
  675. /* --------------------------------------------------------------------------------------------- */
  676. static int
  677. vfs_s_setctl (const vfs_path_t * vpath, int ctlop, void *arg)
  678. {
  679. vfs_path_element_t *path_element;
  680. path_element = vfs_path_get_by_index (vpath, -1);
  681. switch (ctlop)
  682. {
  683. case VFS_SETCTL_STALE_DATA:
  684. {
  685. struct vfs_s_inode *ino = vfs_s_inode_from_path (vpath, 0);
  686. if (ino == NULL)
  687. return 0;
  688. if (arg)
  689. ino->super->want_stale = 1;
  690. else
  691. {
  692. ino->super->want_stale = 0;
  693. vfs_s_invalidate (path_element->class, ino->super);
  694. }
  695. return 1;
  696. }
  697. case VFS_SETCTL_LOGFILE:
  698. ((struct vfs_s_subclass *) path_element->class->data)->logfile = fopen ((char *) arg, "w");
  699. return 1;
  700. case VFS_SETCTL_FLUSH:
  701. ((struct vfs_s_subclass *) path_element->class->data)->flush = 1;
  702. return 1;
  703. }
  704. return 0;
  705. }
  706. /* --------------------------------------------------------------------------------------------- */
  707. /* ----------------------------- Stamping support -------------------------- */
  708. static vfsid
  709. vfs_s_getid (const vfs_path_t * vpath)
  710. {
  711. struct vfs_s_super *archive = NULL;
  712. const char *p;
  713. p = vfs_s_get_path (vpath, &archive, FL_NO_OPEN);
  714. if (p == NULL)
  715. return NULL;
  716. return (vfsid) archive;
  717. }
  718. /* --------------------------------------------------------------------------------------------- */
  719. static int
  720. vfs_s_nothingisopen (vfsid id)
  721. {
  722. (void) id;
  723. /* Our data structures should survive free of superblock at any time */
  724. return 1;
  725. }
  726. /* --------------------------------------------------------------------------------------------- */
  727. static void
  728. vfs_s_free (vfsid id)
  729. {
  730. vfs_s_free_super (((struct vfs_s_super *) id)->me, (struct vfs_s_super *) id);
  731. }
  732. /* --------------------------------------------------------------------------------------------- */
  733. static int
  734. vfs_s_dir_uptodate (struct vfs_class *me, struct vfs_s_inode *ino)
  735. {
  736. struct timeval tim;
  737. if (MEDATA->flush)
  738. {
  739. MEDATA->flush = 0;
  740. return 0;
  741. }
  742. gettimeofday (&tim, NULL);
  743. if (tim.tv_sec < ino->timestamp.tv_sec)
  744. return 1;
  745. return 0;
  746. }
  747. /* --------------------------------------------------------------------------------------------- */
  748. /*** public functions ****************************************************************************/
  749. /* --------------------------------------------------------------------------------------------- */
  750. struct vfs_s_inode *
  751. vfs_s_new_inode (struct vfs_class *me, struct vfs_s_super *super, struct stat *initstat)
  752. {
  753. struct vfs_s_inode *ino;
  754. ino = g_try_new0 (struct vfs_s_inode, 1);
  755. if (ino == NULL)
  756. return NULL;
  757. if (initstat)
  758. ino->st = *initstat;
  759. ino->super = super;
  760. ino->st.st_nlink = 0;
  761. ino->st.st_ino = MEDATA->inode_counter++;
  762. ino->st.st_dev = MEDATA->rdev;
  763. super->ino_usage++;
  764. total_inodes++;
  765. CALL (init_inode) (me, ino);
  766. return ino;
  767. }
  768. /* --------------------------------------------------------------------------------------------- */
  769. struct vfs_s_entry *
  770. vfs_s_new_entry (struct vfs_class *me, const char *name, struct vfs_s_inode *inode)
  771. {
  772. struct vfs_s_entry *entry;
  773. entry = g_new0 (struct vfs_s_entry, 1);
  774. total_entries++;
  775. entry->name = g_strdup (name);
  776. entry->ino = inode;
  777. entry->ino->ent = entry;
  778. CALL (init_entry) (me, entry);
  779. return entry;
  780. }
  781. /* --------------------------------------------------------------------------------------------- */
  782. void
  783. vfs_s_free_entry (struct vfs_class *me, struct vfs_s_entry *ent)
  784. {
  785. if (ent->dir != NULL)
  786. ent->dir->subdir = g_list_remove (ent->dir->subdir, ent);
  787. g_free (ent->name);
  788. /* ent->name = NULL; */
  789. if (ent->ino != NULL)
  790. {
  791. ent->ino->ent = NULL;
  792. vfs_s_free_inode (me, ent->ino);
  793. }
  794. total_entries--;
  795. g_free (ent);
  796. }
  797. /* --------------------------------------------------------------------------------------------- */
  798. void
  799. vfs_s_insert_entry (struct vfs_class *me, struct vfs_s_inode *dir, struct vfs_s_entry *ent)
  800. {
  801. (void) me;
  802. ent->dir = dir;
  803. ent->ino->st.st_nlink++;
  804. dir->subdir = g_list_append (dir->subdir, ent);
  805. }
  806. /* --------------------------------------------------------------------------------------------- */
  807. struct stat *
  808. vfs_s_default_stat (struct vfs_class *me, mode_t mode)
  809. {
  810. static struct stat st;
  811. int myumask;
  812. (void) me;
  813. myumask = umask (022);
  814. umask (myumask);
  815. mode &= ~myumask;
  816. st.st_mode = mode;
  817. st.st_ino = 0;
  818. st.st_dev = 0;
  819. st.st_rdev = 0;
  820. st.st_uid = getuid ();
  821. st.st_gid = getgid ();
  822. st.st_size = 0;
  823. st.st_mtime = st.st_atime = st.st_ctime = time (NULL);
  824. return &st;
  825. }
  826. /* --------------------------------------------------------------------------------------------- */
  827. struct vfs_s_entry *
  828. vfs_s_generate_entry (struct vfs_class *me, const char *name, struct vfs_s_inode *parent,
  829. mode_t mode)
  830. {
  831. struct vfs_s_inode *inode;
  832. struct stat *st;
  833. st = vfs_s_default_stat (me, mode);
  834. inode = vfs_s_new_inode (me, parent->super, st);
  835. return vfs_s_new_entry (me, name, inode);
  836. }
  837. /* --------------------------------------------------------------------------------------------- */
  838. struct vfs_s_inode *
  839. vfs_s_find_inode (struct vfs_class *me, const struct vfs_s_super *super,
  840. const char *path, int follow, int flags)
  841. {
  842. struct vfs_s_entry *ent;
  843. if (((MEDATA->flags & VFS_S_REMOTE) == 0) && (*path == '\0'))
  844. return super->root;
  845. ent = (MEDATA->find_entry) (me, super->root, path, follow, flags);
  846. return (ent != NULL) ? ent->ino : NULL;
  847. }
  848. /* --------------------------------------------------------------------------------------------- */
  849. /* Ook, these were functions around directory entries / inodes */
  850. /* -------------------------------- superblock games -------------------------- */
  851. /**
  852. * get path from last VFS-element and create corresponding superblock
  853. *
  854. * @param vpath source path object
  855. * @param archive pointer to object for store newly created superblock
  856. * @param flags flags
  857. *
  858. * @return path from last VFS-element
  859. */
  860. const char *
  861. vfs_s_get_path (const vfs_path_t * vpath, struct vfs_s_super **archive, int flags)
  862. {
  863. GList *iter;
  864. const char *retval;
  865. int result = -1;
  866. struct vfs_s_super *super;
  867. void *cookie = NULL;
  868. vfs_path_element_t *path_element;
  869. vfs_path_t *vpath_archive;
  870. struct vfs_s_subclass *subclass;
  871. path_element = vfs_path_get_by_index (vpath, -1);
  872. subclass = ((struct vfs_s_subclass *) path_element->class->data);
  873. vpath_archive = vfs_path_clone (vpath);
  874. vfs_path_remove_element_by_index (vpath_archive, -1);
  875. retval = (path_element->path != NULL) ? path_element->path : "";
  876. if (subclass->archive_check != NULL)
  877. {
  878. cookie = subclass->archive_check (vpath_archive);
  879. if (cookie == NULL)
  880. {
  881. vfs_path_free (vpath_archive);
  882. return NULL;
  883. }
  884. }
  885. for (iter = subclass->supers; iter != NULL; iter = g_list_next (iter))
  886. {
  887. int i;
  888. super = (struct vfs_s_super *) iter->data;
  889. /* 0 == other, 1 == same, return it, 2 == other but stop scanning */
  890. i = subclass->archive_same (path_element, super, vpath_archive, cookie);
  891. if (i != 0)
  892. {
  893. if (i == 1)
  894. goto return_success;
  895. break;
  896. }
  897. }
  898. if (flags & FL_NO_OPEN)
  899. {
  900. path_element->class->verrno = EIO;
  901. vfs_path_free (vpath_archive);
  902. return NULL;
  903. }
  904. super = vfs_s_new_super (path_element->class);
  905. if (subclass->open_archive != NULL)
  906. result = subclass->open_archive (super, vpath_archive, path_element);
  907. if (result == -1)
  908. {
  909. vfs_s_free_super (path_element->class, super);
  910. path_element->class->verrno = EIO;
  911. vfs_path_free (vpath_archive);
  912. return NULL;
  913. }
  914. if (!super->name)
  915. vfs_die ("You have to fill name\n");
  916. if (!super->root)
  917. vfs_die ("You have to fill root inode\n");
  918. vfs_s_insert_super (path_element->class, super);
  919. vfs_stamp_create (path_element->class, super);
  920. return_success:
  921. *archive = super;
  922. vfs_path_free (vpath_archive);
  923. return retval;
  924. }
  925. /* --------------------------------------------------------------------------------------------- */
  926. void
  927. vfs_s_invalidate (struct vfs_class *me, struct vfs_s_super *super)
  928. {
  929. if (!super->want_stale)
  930. {
  931. vfs_s_free_inode (me, super->root);
  932. super->root = vfs_s_new_inode (me, super, vfs_s_default_stat (me, S_IFDIR | 0755));
  933. }
  934. }
  935. /* --------------------------------------------------------------------------------------------- */
  936. char *
  937. vfs_s_fullpath (struct vfs_class *me, struct vfs_s_inode *ino)
  938. {
  939. if (!ino->ent)
  940. ERRNOR (EAGAIN, NULL);
  941. if (!(MEDATA->flags & VFS_S_REMOTE))
  942. {
  943. /* archives */
  944. char *newpath;
  945. char *path = g_strdup (ino->ent->name);
  946. while (1)
  947. {
  948. ino = ino->ent->dir;
  949. if (ino == ino->super->root)
  950. break;
  951. newpath = g_strconcat (ino->ent->name, "/", path, (char *) NULL);
  952. g_free (path);
  953. path = newpath;
  954. }
  955. return path;
  956. }
  957. /* remote systems */
  958. if ((!ino->ent->dir) || (!ino->ent->dir->ent))
  959. return g_strdup (ino->ent->name);
  960. return g_strconcat (ino->ent->dir->ent->name, PATH_SEP_STR, ino->ent->name, (char *) NULL);
  961. }
  962. /* --------------------------------------------------------------------------------------------- */
  963. /* --------------------------- stat and friends ---------------------------- */
  964. void *
  965. vfs_s_open (const vfs_path_t * vpath, int flags, mode_t mode)
  966. {
  967. int was_changed = 0;
  968. vfs_file_handler_t *fh;
  969. struct vfs_s_super *super;
  970. const char *q;
  971. struct vfs_s_inode *ino;
  972. vfs_path_element_t *path_element;
  973. path_element = vfs_path_get_by_index (vpath, -1);
  974. q = vfs_s_get_path (vpath, &super, 0);
  975. if (q == NULL)
  976. return NULL;
  977. ino = vfs_s_find_inode (path_element->class, super, q, LINK_FOLLOW, FL_NONE);
  978. if (ino && ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL)))
  979. {
  980. path_element->class->verrno = EEXIST;
  981. return NULL;
  982. }
  983. if (!ino)
  984. {
  985. char *dirname, *name, *save, *q_mangle;
  986. struct vfs_s_entry *ent;
  987. struct vfs_s_inode *dir;
  988. int tmp_handle;
  989. /* If the filesystem is read-only, disable file creation */
  990. if (!(flags & O_CREAT) || !(path_element->class->write))
  991. return NULL;
  992. q_mangle = g_strdup (q);
  993. split_dir_name (path_element->class, q_mangle, &dirname, &name, &save);
  994. dir = vfs_s_find_inode (path_element->class, super, dirname, LINK_FOLLOW, FL_DIR);
  995. if (dir == NULL)
  996. {
  997. g_free (q_mangle);
  998. return NULL;
  999. }
  1000. if (save)
  1001. *save = PATH_SEP;
  1002. ent = vfs_s_generate_entry (path_element->class, name, dir, 0755);
  1003. ino = ent->ino;
  1004. vfs_s_insert_entry (path_element->class, dir, ent);
  1005. tmp_handle = vfs_mkstemps (&ino->localname, path_element->class->name, name);
  1006. g_free (q_mangle);
  1007. if (tmp_handle == -1)
  1008. return NULL;
  1009. close (tmp_handle);
  1010. was_changed = 1;
  1011. }
  1012. if (S_ISDIR (ino->st.st_mode))
  1013. {
  1014. path_element->class->verrno = EISDIR;
  1015. return NULL;
  1016. }
  1017. fh = g_new (vfs_file_handler_t, 1);
  1018. fh->pos = 0;
  1019. fh->ino = ino;
  1020. fh->handle = -1;
  1021. fh->changed = was_changed;
  1022. fh->linear = 0;
  1023. fh->data = NULL;
  1024. if (IS_LINEAR (flags))
  1025. {
  1026. if (VFSDATA (path_element)->linear_start)
  1027. {
  1028. vfs_print_message (_("Starting linear transfer..."));
  1029. fh->linear = LS_LINEAR_PREOPEN;
  1030. }
  1031. }
  1032. else
  1033. {
  1034. struct vfs_s_subclass *s;
  1035. s = VFSDATA (path_element);
  1036. if (s->fh_open != NULL && s->fh_open (path_element->class, fh, flags, mode) != 0)
  1037. {
  1038. if (s->fh_free_data != NULL)
  1039. s->fh_free_data (fh);
  1040. g_free (fh);
  1041. return NULL;
  1042. }
  1043. }
  1044. if (fh->ino->localname)
  1045. {
  1046. fh->handle = open (fh->ino->localname, NO_LINEAR (flags), mode);
  1047. if (fh->handle == -1)
  1048. {
  1049. g_free (fh);
  1050. path_element->class->verrno = errno;
  1051. return NULL;
  1052. }
  1053. }
  1054. /* i.e. we had no open files and now we have one */
  1055. vfs_rmstamp (path_element->class, (vfsid) super);
  1056. super->fd_usage++;
  1057. fh->ino->st.st_nlink++;
  1058. return fh;
  1059. }
  1060. /* --------------------------------------------------------------------------------------------- */
  1061. int
  1062. vfs_s_retrieve_file (struct vfs_class *me, struct vfs_s_inode *ino)
  1063. {
  1064. /* If you want reget, you'll have to open file with O_LINEAR */
  1065. off_t total = 0;
  1066. char buffer[8192];
  1067. int handle, n;
  1068. off_t stat_size = ino->st.st_size;
  1069. vfs_file_handler_t fh;
  1070. memset (&fh, 0, sizeof (fh));
  1071. fh.ino = ino;
  1072. fh.handle = -1;
  1073. handle = vfs_mkstemps (&ino->localname, me->name, ino->ent->name);
  1074. if (handle == -1)
  1075. {
  1076. me->verrno = errno;
  1077. goto error_4;
  1078. }
  1079. if (!MEDATA->linear_start (me, &fh, 0))
  1080. goto error_3;
  1081. /* Clear the interrupt status */
  1082. tty_got_interrupt ();
  1083. tty_enable_interrupt_key ();
  1084. while ((n = MEDATA->linear_read (me, &fh, buffer, sizeof (buffer))))
  1085. {
  1086. int t;
  1087. if (n < 0)
  1088. goto error_1;
  1089. total += n;
  1090. vfs_s_print_stats (me->name, _("Getting file"), ino->ent->name, total, stat_size);
  1091. if (tty_got_interrupt ())
  1092. goto error_1;
  1093. t = write (handle, buffer, n);
  1094. if (t != n)
  1095. {
  1096. if (t == -1)
  1097. me->verrno = errno;
  1098. goto error_1;
  1099. }
  1100. }
  1101. MEDATA->linear_close (me, &fh);
  1102. close (handle);
  1103. tty_disable_interrupt_key ();
  1104. g_free (fh.data);
  1105. return 0;
  1106. error_1:
  1107. MEDATA->linear_close (me, &fh);
  1108. error_3:
  1109. tty_disable_interrupt_key ();
  1110. close (handle);
  1111. unlink (ino->localname);
  1112. error_4:
  1113. g_free (ino->localname);
  1114. ino->localname = NULL;
  1115. g_free (fh.data);
  1116. return -1;
  1117. }
  1118. /* --------------------------------------------------------------------------------------------- */
  1119. /* ----------------------------- Stamping support -------------------------- */
  1120. /* Initialize one of our subclasses - fill common functions */
  1121. void
  1122. vfs_s_init_class (struct vfs_class *vclass, struct vfs_s_subclass *sub)
  1123. {
  1124. vclass->data = sub;
  1125. vclass->fill_names = vfs_s_fill_names;
  1126. vclass->open = vfs_s_open;
  1127. vclass->close = vfs_s_close;
  1128. vclass->read = vfs_s_read;
  1129. if (!(sub->flags & VFS_S_READONLY))
  1130. {
  1131. vclass->write = vfs_s_write;
  1132. }
  1133. vclass->opendir = vfs_s_opendir;
  1134. vclass->readdir = vfs_s_readdir;
  1135. vclass->closedir = vfs_s_closedir;
  1136. vclass->stat = vfs_s_stat;
  1137. vclass->lstat = vfs_s_lstat;
  1138. vclass->fstat = vfs_s_fstat;
  1139. vclass->readlink = vfs_s_readlink;
  1140. vclass->chdir = vfs_s_chdir;
  1141. vclass->ferrno = vfs_s_ferrno;
  1142. vclass->lseek = vfs_s_lseek;
  1143. vclass->getid = vfs_s_getid;
  1144. vclass->nothingisopen = vfs_s_nothingisopen;
  1145. vclass->free = vfs_s_free;
  1146. if (sub->flags & VFS_S_REMOTE)
  1147. {
  1148. vclass->getlocalcopy = vfs_s_getlocalcopy;
  1149. vclass->ungetlocalcopy = vfs_s_ungetlocalcopy;
  1150. sub->find_entry = vfs_s_find_entry_linear;
  1151. }
  1152. else
  1153. {
  1154. sub->find_entry = vfs_s_find_entry_tree;
  1155. }
  1156. vclass->setctl = vfs_s_setctl;
  1157. sub->dir_uptodate = vfs_s_dir_uptodate;
  1158. }
  1159. /* --------------------------------------------------------------------------------------------- */
  1160. /** Find VFS id for given directory name */
  1161. vfsid
  1162. vfs_getid (const vfs_path_t * vpath)
  1163. {
  1164. vfs_path_element_t *path_element;
  1165. path_element = vfs_path_get_by_index (vpath, -1);
  1166. if (!vfs_path_element_valid (path_element) || path_element->class->getid == NULL)
  1167. return NULL;
  1168. return (*path_element->class->getid) (vpath);
  1169. }
  1170. /* --------------------------------------------------------------------------------------------- */
  1171. /* ----------- Utility functions for networked filesystems -------------- */
  1172. #ifdef ENABLE_VFS_NET
  1173. int
  1174. vfs_s_select_on_two (int fd1, int fd2)
  1175. {
  1176. fd_set set;
  1177. struct timeval time_out;
  1178. int v;
  1179. int maxfd = (fd1 > fd2 ? fd1 : fd2) + 1;
  1180. time_out.tv_sec = 1;
  1181. time_out.tv_usec = 0;
  1182. FD_ZERO (&set);
  1183. FD_SET (fd1, &set);
  1184. FD_SET (fd2, &set);
  1185. v = select (maxfd, &set, 0, 0, &time_out);
  1186. if (v <= 0)
  1187. return v;
  1188. if (FD_ISSET (fd1, &set))
  1189. return 1;
  1190. if (FD_ISSET (fd2, &set))
  1191. return 2;
  1192. return -1;
  1193. }
  1194. /* --------------------------------------------------------------------------------------------- */
  1195. int
  1196. vfs_s_get_line (struct vfs_class *me, int sock, char *buf, int buf_len, char term)
  1197. {
  1198. FILE *logfile = MEDATA->logfile;
  1199. int i;
  1200. char c;
  1201. for (i = 0; i < buf_len - 1; i++, buf++)
  1202. {
  1203. if (read (sock, buf, sizeof (char)) <= 0)
  1204. return 0;
  1205. if (logfile)
  1206. {
  1207. size_t ret1;
  1208. int ret2;
  1209. ret1 = fwrite (buf, 1, 1, logfile);
  1210. ret2 = fflush (logfile);
  1211. }
  1212. if (*buf == term)
  1213. {
  1214. *buf = 0;
  1215. return 1;
  1216. }
  1217. }
  1218. /* Line is too long - terminate buffer and discard the rest of line */
  1219. *buf = 0;
  1220. while (read (sock, &c, sizeof (c)) > 0)
  1221. {
  1222. if (logfile)
  1223. {
  1224. size_t ret1;
  1225. int ret2;
  1226. ret1 = fwrite (&c, 1, 1, logfile);
  1227. ret2 = fflush (logfile);
  1228. }
  1229. if (c == '\n')
  1230. return 1;
  1231. }
  1232. return 0;
  1233. }
  1234. /* --------------------------------------------------------------------------------------------- */
  1235. int
  1236. vfs_s_get_line_interruptible (struct vfs_class *me, char *buffer, int size, int fd)
  1237. {
  1238. int n;
  1239. int i;
  1240. (void) me;
  1241. tty_enable_interrupt_key ();
  1242. for (i = 0; i < size - 1; i++)
  1243. {
  1244. n = read (fd, buffer + i, 1);
  1245. tty_disable_interrupt_key ();
  1246. if (n == -1 && errno == EINTR)
  1247. {
  1248. buffer[i] = 0;
  1249. return EINTR;
  1250. }
  1251. if (n == 0)
  1252. {
  1253. buffer[i] = 0;
  1254. return 0;
  1255. }
  1256. if (buffer[i] == '\n')
  1257. {
  1258. buffer[i] = 0;
  1259. return 1;
  1260. }
  1261. }
  1262. buffer[size - 1] = 0;
  1263. return 0;
  1264. }
  1265. #endif /* ENABLE_VFS_NET */
  1266. /* --------------------------------------------------------------------------------------------- */
  1267. /**
  1268. * Normalize filenames start position
  1269. */
  1270. void
  1271. vfs_s_normalize_filename_leading_spaces (struct vfs_s_inode *root_inode, size_t final_num_spaces)
  1272. {
  1273. GList *iter;
  1274. for (iter = root_inode->subdir; iter != NULL; iter = g_list_next (iter))
  1275. {
  1276. struct vfs_s_entry *entry = (struct vfs_s_entry *) iter->data;
  1277. if ((size_t) entry->ino->data_offset > final_num_spaces)
  1278. {
  1279. char *source_name = entry->name;
  1280. char *spacer = g_strnfill (entry->ino->data_offset - final_num_spaces, ' ');
  1281. entry->name = g_strdup_printf ("%s%s", spacer, source_name);
  1282. g_free (spacer);
  1283. g_free (source_name);
  1284. }
  1285. entry->ino->data_offset = -1;
  1286. }
  1287. }
  1288. /* --------------------------------------------------------------------------------------------- */