direntry.c 43 KB

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