direntry.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664
  1. /*
  2. Directory cache support
  3. Copyright (C) 1998-2018
  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) != 0 ? (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 == NULL)
  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, FL_NONE);
  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) == NULL)
  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 != 0)
  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) != 0 ? LINK_FOLLOW : LINK_NO_FOLLOW,
  311. flags & ~FL_FOLLOW);
  312. if (ino == NULL && *q == '\0')
  313. /* We are asking about / directory of ftp server: assume it exists */
  314. ino =
  315. vfs_s_find_inode (path_element->class, super, q,
  316. (flags & FL_FOLLOW) != 0 ? LINK_FOLLOW : LINK_NO_FOLLOW,
  317. FL_DIR | (flags & ~FL_FOLLOW));
  318. return ino;
  319. }
  320. /* --------------------------------------------------------------------------------------------- */
  321. static void *
  322. vfs_s_opendir (const vfs_path_t * vpath)
  323. {
  324. struct vfs_s_inode *dir;
  325. struct dirhandle *info;
  326. const vfs_path_element_t *path_element;
  327. path_element = vfs_path_get_by_index (vpath, -1);
  328. dir = vfs_s_inode_from_path (vpath, FL_DIR | FL_FOLLOW);
  329. if (dir == NULL)
  330. return NULL;
  331. if (!S_ISDIR (dir->st.st_mode))
  332. {
  333. path_element->class->verrno = ENOTDIR;
  334. return NULL;
  335. }
  336. dir->st.st_nlink++;
  337. #if 0
  338. if (dir->subdir == NULL) /* This can actually happen if we allow empty directories */
  339. {
  340. path_element->class->verrno = EAGAIN;
  341. return NULL;
  342. }
  343. #endif
  344. info = g_new (struct dirhandle, 1);
  345. info->cur = dir->subdir;
  346. info->dir = dir;
  347. return info;
  348. }
  349. /* --------------------------------------------------------------------------------------------- */
  350. static void *
  351. vfs_s_readdir (void *data)
  352. {
  353. static union vfs_dirent dir;
  354. struct dirhandle *info = (struct dirhandle *) data;
  355. const char *name;
  356. if (info->cur == NULL || info->cur->data == NULL)
  357. return NULL;
  358. name = ((struct vfs_s_entry *) info->cur->data)->name;
  359. if (name != NULL)
  360. g_strlcpy (dir.dent.d_name, name, MC_MAXPATHLEN);
  361. else
  362. vfs_die ("Null in structure-cannot happen");
  363. info->cur = g_list_next (info->cur);
  364. return (void *) &dir;
  365. }
  366. /* --------------------------------------------------------------------------------------------- */
  367. static int
  368. vfs_s_closedir (void *data)
  369. {
  370. struct dirhandle *info = (struct dirhandle *) data;
  371. struct vfs_s_inode *dir = info->dir;
  372. vfs_s_free_inode (dir->super->me, dir);
  373. g_free (data);
  374. return 0;
  375. }
  376. /* --------------------------------------------------------------------------------------------- */
  377. static int
  378. vfs_s_chdir (const vfs_path_t * vpath)
  379. {
  380. void *data;
  381. data = vfs_s_opendir (vpath);
  382. if (data == NULL)
  383. return (-1);
  384. vfs_s_closedir (data);
  385. return 0;
  386. }
  387. /* --------------------------------------------------------------------------------------------- */
  388. /* --------------------------- stat and friends ---------------------------- */
  389. static int
  390. vfs_s_internal_stat (const vfs_path_t * vpath, struct stat *buf, int flag)
  391. {
  392. struct vfs_s_inode *ino;
  393. ino = vfs_s_inode_from_path (vpath, flag);
  394. if (ino == NULL)
  395. return (-1);
  396. *buf = ino->st;
  397. return 0;
  398. }
  399. /* --------------------------------------------------------------------------------------------- */
  400. static int
  401. vfs_s_readlink (const vfs_path_t * vpath, char *buf, size_t size)
  402. {
  403. struct vfs_s_inode *ino;
  404. size_t len;
  405. const vfs_path_element_t *path_element;
  406. path_element = vfs_path_get_by_index (vpath, -1);
  407. ino = vfs_s_inode_from_path (vpath, 0);
  408. if (ino == NULL)
  409. return (-1);
  410. if (!S_ISLNK (ino->st.st_mode))
  411. {
  412. path_element->class->verrno = EINVAL;
  413. return (-1);
  414. }
  415. if (ino->linkname == NULL)
  416. {
  417. path_element->class->verrno = EFAULT;
  418. return (-1);
  419. }
  420. len = strlen (ino->linkname);
  421. if (size < len)
  422. len = size;
  423. /* readlink() does not append a NUL character to buf */
  424. memcpy (buf, ino->linkname, len);
  425. return len;
  426. }
  427. /* --------------------------------------------------------------------------------------------- */
  428. static ssize_t
  429. vfs_s_read (void *fh, char *buffer, size_t count)
  430. {
  431. struct vfs_class *me = FH_SUPER->me;
  432. if (FH->linear == LS_LINEAR_PREOPEN)
  433. {
  434. if (MEDATA->linear_start (me, FH, FH->pos) == 0)
  435. return (-1);
  436. }
  437. if (FH->linear == LS_LINEAR_CLOSED)
  438. vfs_die ("linear_start() did not set linear_state!");
  439. if (FH->linear == LS_LINEAR_OPEN)
  440. return MEDATA->linear_read (me, FH, buffer, count);
  441. if (FH->handle != -1)
  442. {
  443. ssize_t n;
  444. n = read (FH->handle, buffer, count);
  445. if (n < 0)
  446. me->verrno = errno;
  447. return n;
  448. }
  449. vfs_die ("vfs_s_read: This should not happen\n");
  450. return (-1);
  451. }
  452. /* --------------------------------------------------------------------------------------------- */
  453. static ssize_t
  454. vfs_s_write (void *fh, const char *buffer, size_t count)
  455. {
  456. struct vfs_class *me = FH_SUPER->me;
  457. if (FH->linear != LS_NOT_LINEAR)
  458. vfs_die ("no writing to linear files, please");
  459. FH->changed = TRUE;
  460. if (FH->handle != -1)
  461. {
  462. ssize_t n;
  463. n = write (FH->handle, buffer, count);
  464. if (n < 0)
  465. me->verrno = errno;
  466. return n;
  467. }
  468. vfs_die ("vfs_s_write: This should not happen\n");
  469. return 0;
  470. }
  471. /* --------------------------------------------------------------------------------------------- */
  472. static off_t
  473. vfs_s_lseek (void *fh, off_t offset, int whence)
  474. {
  475. off_t size = FH->ino->st.st_size;
  476. if (FH->linear == LS_LINEAR_OPEN)
  477. vfs_die ("cannot lseek() after linear_read!");
  478. if (FH->handle != -1)
  479. { /* If we have local file opened, we want to work with it */
  480. off_t retval;
  481. retval = lseek (FH->handle, offset, whence);
  482. if (retval == -1)
  483. FH->ino->super->me->verrno = errno;
  484. return retval;
  485. }
  486. switch (whence)
  487. {
  488. case SEEK_CUR:
  489. offset += FH->pos;
  490. break;
  491. case SEEK_END:
  492. offset += size;
  493. break;
  494. default:
  495. break;
  496. }
  497. if (offset < 0)
  498. FH->pos = 0;
  499. else if (offset < size)
  500. FH->pos = offset;
  501. else
  502. FH->pos = size;
  503. return FH->pos;
  504. }
  505. /* --------------------------------------------------------------------------------------------- */
  506. static int
  507. vfs_s_close (void *fh)
  508. {
  509. int res = 0;
  510. struct vfs_class *me = FH_SUPER->me;
  511. if (me == NULL)
  512. return (-1);
  513. FH_SUPER->fd_usage--;
  514. if (FH_SUPER->fd_usage == 0)
  515. vfs_stamp_create (me, FH_SUPER);
  516. if (FH->linear == LS_LINEAR_OPEN)
  517. MEDATA->linear_close (me, fh);
  518. if (MEDATA->fh_close != NULL)
  519. res = MEDATA->fh_close (me, fh);
  520. if ((MEDATA->flags & VFS_S_USETMP) != 0 && FH->changed && MEDATA->file_store != NULL)
  521. {
  522. char *s;
  523. s = vfs_s_fullpath (me, FH->ino);
  524. if (s == NULL)
  525. res = -1;
  526. else
  527. {
  528. res = MEDATA->file_store (me, fh, s, FH->ino->localname);
  529. g_free (s);
  530. }
  531. vfs_s_invalidate (me, FH_SUPER);
  532. }
  533. if (FH->handle != -1)
  534. close (FH->handle);
  535. vfs_s_free_inode (me, FH->ino);
  536. if (MEDATA->fh_free_data != NULL)
  537. MEDATA->fh_free_data (fh);
  538. g_free (fh);
  539. return res;
  540. }
  541. /* --------------------------------------------------------------------------------------------- */
  542. static void
  543. vfs_s_print_stats (const char *fs_name, const char *action,
  544. const char *file_name, off_t have, off_t need)
  545. {
  546. if (need != 0)
  547. vfs_print_message (_("%s: %s: %s %3d%% (%lld) bytes transferred"), fs_name, action,
  548. file_name, (int) ((double) have * 100 / need), (long long) have);
  549. else
  550. vfs_print_message (_("%s: %s: %s %lld bytes transferred"), fs_name, action, file_name,
  551. (long long) have);
  552. }
  553. /* --------------------------------------------------------------------------------------------- */
  554. /* ------------------------------- mc support ---------------------------- */
  555. static void
  556. vfs_s_fill_names (struct vfs_class *me, fill_names_f func)
  557. {
  558. GList *iter;
  559. for (iter = MEDATA->supers; iter != NULL; iter = g_list_next (iter))
  560. {
  561. const struct vfs_s_super *super = (const struct vfs_s_super *) iter->data;
  562. char *name;
  563. name = g_strconcat (super->name, PATH_SEP_STR, me->prefix, VFS_PATH_URL_DELIMITER,
  564. /* super->current_dir->name, */ (char *) NULL);
  565. func (name);
  566. g_free (name);
  567. }
  568. }
  569. /* --------------------------------------------------------------------------------------------- */
  570. static int
  571. vfs_s_ferrno (struct vfs_class *me)
  572. {
  573. return me->verrno;
  574. }
  575. /* --------------------------------------------------------------------------------------------- */
  576. /**
  577. * Get local copy of the given file. We reuse the existing file cache
  578. * for remote filesystems. Archives use standard VFS facilities.
  579. */
  580. static vfs_path_t *
  581. vfs_s_getlocalcopy (const vfs_path_t * vpath)
  582. {
  583. vfs_file_handler_t *fh;
  584. vfs_path_t *local = NULL;
  585. if (vpath == NULL)
  586. return NULL;
  587. fh = vfs_s_open (vpath, O_RDONLY, 0);
  588. if (fh != NULL)
  589. {
  590. const struct vfs_class *me;
  591. me = vfs_path_get_by_index (vpath, -1)->class;
  592. if ((MEDATA->flags & VFS_S_USETMP) != 0 && fh->ino != NULL)
  593. local = vfs_path_from_str_flags (fh->ino->localname, VPF_NO_CANON);
  594. vfs_s_close (fh);
  595. }
  596. return local;
  597. }
  598. /* --------------------------------------------------------------------------------------------- */
  599. /**
  600. * Return the local copy. Since we are using our cache, we do nothing -
  601. * the cache will be removed when the archive is closed.
  602. */
  603. static int
  604. vfs_s_ungetlocalcopy (const vfs_path_t * vpath, const vfs_path_t * local, gboolean has_changed)
  605. {
  606. (void) vpath;
  607. (void) local;
  608. (void) has_changed;
  609. return 0;
  610. }
  611. /* --------------------------------------------------------------------------------------------- */
  612. static int
  613. vfs_s_setctl (const vfs_path_t * vpath, int ctlop, void *arg)
  614. {
  615. const vfs_path_element_t *path_element;
  616. path_element = vfs_path_get_by_index (vpath, -1);
  617. switch (ctlop)
  618. {
  619. case VFS_SETCTL_STALE_DATA:
  620. {
  621. struct vfs_s_inode *ino;
  622. ino = vfs_s_inode_from_path (vpath, 0);
  623. if (ino == NULL)
  624. return 0;
  625. if (arg != NULL)
  626. ino->super->want_stale = TRUE;
  627. else
  628. {
  629. ino->super->want_stale = FALSE;
  630. vfs_s_invalidate (path_element->class, ino->super);
  631. }
  632. return 1;
  633. }
  634. case VFS_SETCTL_LOGFILE:
  635. ((struct vfs_s_subclass *) path_element->class->data)->logfile = fopen ((char *) arg, "w");
  636. return 1;
  637. case VFS_SETCTL_FLUSH:
  638. ((struct vfs_s_subclass *) path_element->class->data)->flush = 1;
  639. return 1;
  640. default:
  641. return 0;
  642. }
  643. }
  644. /* --------------------------------------------------------------------------------------------- */
  645. /* ----------------------------- Stamping support -------------------------- */
  646. static vfsid
  647. vfs_s_getid (const vfs_path_t * vpath)
  648. {
  649. struct vfs_s_super *archive = NULL;
  650. const char *p;
  651. p = vfs_s_get_path (vpath, &archive, FL_NO_OPEN);
  652. if (p == NULL)
  653. return NULL;
  654. return (vfsid) archive;
  655. }
  656. /* --------------------------------------------------------------------------------------------- */
  657. static int
  658. vfs_s_nothingisopen (vfsid id)
  659. {
  660. (void) id;
  661. /* Our data structures should survive free of superblock at any time */
  662. return 1;
  663. }
  664. /* --------------------------------------------------------------------------------------------- */
  665. static void
  666. vfs_s_free (vfsid id)
  667. {
  668. vfs_s_free_super (((struct vfs_s_super *) id)->me, (struct vfs_s_super *) id);
  669. }
  670. /* --------------------------------------------------------------------------------------------- */
  671. static int
  672. vfs_s_dir_uptodate (struct vfs_class *me, struct vfs_s_inode *ino)
  673. {
  674. struct timeval tim;
  675. if (MEDATA->flush != 0)
  676. {
  677. MEDATA->flush = 0;
  678. return 0;
  679. }
  680. gettimeofday (&tim, NULL);
  681. return (tim.tv_sec < ino->timestamp.tv_sec ? 1 : 0);
  682. }
  683. /* --------------------------------------------------------------------------------------------- */
  684. /*** public functions ****************************************************************************/
  685. /* --------------------------------------------------------------------------------------------- */
  686. struct vfs_s_inode *
  687. vfs_s_new_inode (struct vfs_class *me, struct vfs_s_super *super, struct stat *initstat)
  688. {
  689. struct vfs_s_inode *ino;
  690. ino = g_try_new0 (struct vfs_s_inode, 1);
  691. if (ino == NULL)
  692. return NULL;
  693. if (initstat != NULL)
  694. ino->st = *initstat;
  695. ino->super = super;
  696. ino->st.st_nlink = 0;
  697. ino->st.st_ino = MEDATA->inode_counter++;
  698. ino->st.st_dev = MEDATA->rdev;
  699. super->ino_usage++;
  700. CALL (init_inode) (me, ino);
  701. return ino;
  702. }
  703. /* --------------------------------------------------------------------------------------------- */
  704. void
  705. vfs_s_free_inode (struct vfs_class *me, struct vfs_s_inode *ino)
  706. {
  707. if (ino == NULL)
  708. vfs_die ("Don't pass NULL to me");
  709. /* ==0 can happen if freshly created entry is deleted */
  710. if (ino->st.st_nlink > 1)
  711. {
  712. ino->st.st_nlink--;
  713. return;
  714. }
  715. while (ino->subdir != NULL)
  716. vfs_s_free_entry (me, (struct vfs_s_entry *) ino->subdir->data);
  717. CALL (free_inode) (me, ino);
  718. g_free (ino->linkname);
  719. if ((MEDATA->flags & VFS_S_USETMP) != 0 && ino->localname != NULL)
  720. {
  721. unlink (ino->localname);
  722. g_free (ino->localname);
  723. }
  724. ino->super->ino_usage--;
  725. g_free (ino);
  726. }
  727. /* --------------------------------------------------------------------------------------------- */
  728. struct vfs_s_entry *
  729. vfs_s_new_entry (struct vfs_class *me, const char *name, struct vfs_s_inode *inode)
  730. {
  731. struct vfs_s_entry *entry;
  732. entry = g_new0 (struct vfs_s_entry, 1);
  733. entry->name = g_strdup (name);
  734. entry->ino = inode;
  735. entry->ino->ent = entry;
  736. CALL (init_entry) (me, entry);
  737. return entry;
  738. }
  739. /* --------------------------------------------------------------------------------------------- */
  740. void
  741. vfs_s_free_entry (struct vfs_class *me, struct vfs_s_entry *ent)
  742. {
  743. if (ent->dir != NULL)
  744. ent->dir->subdir = g_list_remove (ent->dir->subdir, ent);
  745. MC_PTR_FREE (ent->name);
  746. if (ent->ino != NULL)
  747. {
  748. ent->ino->ent = NULL;
  749. vfs_s_free_inode (me, ent->ino);
  750. }
  751. g_free (ent);
  752. }
  753. /* --------------------------------------------------------------------------------------------- */
  754. void
  755. vfs_s_insert_entry (struct vfs_class *me, struct vfs_s_inode *dir, struct vfs_s_entry *ent)
  756. {
  757. (void) me;
  758. ent->dir = dir;
  759. ent->ino->st.st_nlink++;
  760. dir->subdir = g_list_append (dir->subdir, ent);
  761. }
  762. /* --------------------------------------------------------------------------------------------- */
  763. struct stat *
  764. vfs_s_default_stat (struct vfs_class *me, mode_t mode)
  765. {
  766. static struct stat st;
  767. mode_t myumask;
  768. (void) me;
  769. myumask = umask (022);
  770. umask (myumask);
  771. mode &= ~myumask;
  772. st.st_mode = mode;
  773. st.st_ino = 0;
  774. st.st_dev = 0;
  775. #ifdef HAVE_STRUCT_STAT_ST_RDEV
  776. st.st_rdev = 0;
  777. #endif
  778. st.st_uid = getuid ();
  779. st.st_gid = getgid ();
  780. #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
  781. st.st_blksize = 512;
  782. #endif
  783. st.st_size = 0;
  784. st.st_mtime = st.st_atime = st.st_ctime = time (NULL);
  785. #ifdef HAVE_STRUCT_STAT_ST_MTIM
  786. st.st_atim.tv_nsec = st.st_mtim.tv_nsec = st.st_ctim.tv_nsec = 0;
  787. #endif
  788. vfs_adjust_stat (&st);
  789. return &st;
  790. }
  791. /* --------------------------------------------------------------------------------------------- */
  792. /**
  793. * Calculate number of st_blocks using st_size and st_blksize.
  794. * In according to stat(2), st_blocks is the size in 512-byte units.
  795. *
  796. * @param s stat info
  797. */
  798. void
  799. vfs_adjust_stat (struct stat *s)
  800. {
  801. #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
  802. if (s->st_size == 0)
  803. s->st_blocks = 0;
  804. else
  805. {
  806. #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
  807. blkcnt_t ioblocks;
  808. blksize_t ioblock_size;
  809. /* 1. Calculate how many IO blocks are occupied */
  810. ioblocks = 1 + (s->st_size - 1) / s->st_blksize;
  811. /* 2. Calculate size of st_blksize in 512-byte units */
  812. ioblock_size = 1 + (s->st_blksize - 1) / 512;
  813. /* 3. Calculate number of blocks */
  814. s->st_blocks = ioblocks * ioblock_size;
  815. #else
  816. /* Let IO block size is 512 bytes */
  817. s->st_blocks = 1 + (s->st_size - 1) / 512;
  818. #endif /* HAVE_STRUCT_STAT_ST_BLKSIZE */
  819. }
  820. #endif /* HAVE_STRUCT_STAT_ST_BLOCKS */
  821. }
  822. /* --------------------------------------------------------------------------------------------- */
  823. struct vfs_s_entry *
  824. vfs_s_generate_entry (struct vfs_class *me, const char *name, struct vfs_s_inode *parent,
  825. mode_t mode)
  826. {
  827. struct vfs_s_inode *inode;
  828. struct stat *st;
  829. st = vfs_s_default_stat (me, mode);
  830. inode = vfs_s_new_inode (me, parent->super, st);
  831. return vfs_s_new_entry (me, name, inode);
  832. }
  833. /* --------------------------------------------------------------------------------------------- */
  834. struct vfs_s_inode *
  835. vfs_s_find_inode (struct vfs_class *me, const struct vfs_s_super *super,
  836. const char *path, int follow, int flags)
  837. {
  838. struct vfs_s_entry *ent;
  839. if (((MEDATA->flags & VFS_S_REMOTE) == 0) && (*path == '\0'))
  840. return super->root;
  841. ent = MEDATA->find_entry (me, super->root, path, follow, flags);
  842. return (ent != NULL ? ent->ino : NULL);
  843. }
  844. /* --------------------------------------------------------------------------------------------- */
  845. /* Ook, these were functions around directory entries / inodes */
  846. /* -------------------------------- superblock games -------------------------- */
  847. /**
  848. * get superlock object by vpath
  849. *
  850. * @param vpath path
  851. * @return superlock object or NULL if not found
  852. */
  853. struct vfs_s_super *
  854. vfs_get_super_by_vpath (const vfs_path_t * vpath)
  855. {
  856. GList *iter;
  857. void *cookie = NULL;
  858. const vfs_path_element_t *path_element;
  859. struct vfs_s_subclass *subclass;
  860. struct vfs_s_super *super = NULL;
  861. vfs_path_t *vpath_archive;
  862. path_element = vfs_path_get_by_index (vpath, -1);
  863. subclass = (struct vfs_s_subclass *) path_element->class->data;
  864. if (subclass == NULL)
  865. return NULL;
  866. vpath_archive = vfs_path_clone (vpath);
  867. vfs_path_remove_element_by_index (vpath_archive, -1);
  868. if (subclass->archive_check != NULL)
  869. {
  870. cookie = subclass->archive_check (vpath_archive);
  871. if (cookie == NULL)
  872. goto ret;
  873. }
  874. for (iter = subclass->supers; iter != NULL; iter = g_list_next (iter))
  875. {
  876. int i;
  877. super = (struct vfs_s_super *) iter->data;
  878. /* 0 == other, 1 == same, return it, 2 == other but stop scanning */
  879. i = subclass->archive_same (path_element, super, vpath_archive, cookie);
  880. if (i == 1)
  881. goto ret;
  882. if (i != 0)
  883. break;
  884. super = NULL;
  885. }
  886. ret:
  887. vfs_path_free (vpath_archive);
  888. return super;
  889. }
  890. /* --------------------------------------------------------------------------------------------- */
  891. /**
  892. * get path from last VFS-element and create corresponding superblock
  893. *
  894. * @param vpath source path object
  895. * @param archive pointer to object for store newly created superblock
  896. * @param flags flags
  897. *
  898. * @return path from last VFS-element
  899. */
  900. const char *
  901. vfs_s_get_path (const vfs_path_t * vpath, struct vfs_s_super **archive, int flags)
  902. {
  903. const char *retval = "";
  904. int result = -1;
  905. struct vfs_s_super *super;
  906. const vfs_path_element_t *path_element;
  907. struct vfs_s_subclass *subclass;
  908. path_element = vfs_path_get_by_index (vpath, -1);
  909. if (path_element->path != NULL)
  910. retval = path_element->path;
  911. super = vfs_get_super_by_vpath (vpath);
  912. if (super != NULL)
  913. goto return_success;
  914. if ((flags & FL_NO_OPEN) != 0)
  915. {
  916. path_element->class->verrno = EIO;
  917. return NULL;
  918. }
  919. super = vfs_s_new_super (path_element->class);
  920. subclass = (struct vfs_s_subclass *) path_element->class->data;
  921. if (subclass->open_archive != NULL)
  922. {
  923. vfs_path_t *vpath_archive;
  924. vpath_archive = vfs_path_clone (vpath);
  925. vfs_path_remove_element_by_index (vpath_archive, -1);
  926. result = subclass->open_archive (super, vpath_archive, path_element);
  927. vfs_path_free (vpath_archive);
  928. }
  929. if (result == -1)
  930. {
  931. vfs_s_free_super (path_element->class, super);
  932. path_element->class->verrno = EIO;
  933. return NULL;
  934. }
  935. if (super->name == NULL)
  936. vfs_die ("You have to fill name\n");
  937. if (super->root == NULL)
  938. vfs_die ("You have to fill root inode\n");
  939. vfs_s_insert_super (path_element->class, super);
  940. vfs_stamp_create (path_element->class, super);
  941. return_success:
  942. *archive = super;
  943. return retval;
  944. }
  945. /* --------------------------------------------------------------------------------------------- */
  946. void
  947. vfs_s_invalidate (struct vfs_class *me, struct vfs_s_super *super)
  948. {
  949. if (!super->want_stale)
  950. {
  951. vfs_s_free_inode (me, super->root);
  952. super->root = vfs_s_new_inode (me, super, vfs_s_default_stat (me, S_IFDIR | 0755));
  953. }
  954. }
  955. /* --------------------------------------------------------------------------------------------- */
  956. char *
  957. vfs_s_fullpath (struct vfs_class *me, struct vfs_s_inode *ino)
  958. {
  959. if (ino->ent == NULL)
  960. ERRNOR (EAGAIN, NULL);
  961. if ((MEDATA->flags & VFS_S_USETMP) == 0)
  962. {
  963. /* archives */
  964. char *path;
  965. path = g_strdup (ino->ent->name);
  966. while (TRUE)
  967. {
  968. char *newpath;
  969. ino = ino->ent->dir;
  970. if (ino == ino->super->root)
  971. break;
  972. newpath = g_strconcat (ino->ent->name, PATH_SEP_STR, path, (char *) NULL);
  973. g_free (path);
  974. path = newpath;
  975. }
  976. return path;
  977. }
  978. /* remote systems */
  979. if (ino->ent->dir == NULL || ino->ent->dir->ent == NULL)
  980. return g_strdup (ino->ent->name);
  981. return g_strconcat (ino->ent->dir->ent->name, PATH_SEP_STR, ino->ent->name, (char *) NULL);
  982. }
  983. /* --------------------------------------------------------------------------------------------- */
  984. /* --------------------------- stat and friends ---------------------------- */
  985. void *
  986. vfs_s_open (const vfs_path_t * vpath, int flags, mode_t mode)
  987. {
  988. gboolean was_changed = FALSE;
  989. vfs_file_handler_t *fh;
  990. struct vfs_s_super *super;
  991. const char *q;
  992. struct vfs_s_inode *ino;
  993. const vfs_path_element_t *path_element;
  994. path_element = vfs_path_get_by_index (vpath, -1);
  995. q = vfs_s_get_path (vpath, &super, 0);
  996. if (q == NULL)
  997. return NULL;
  998. ino = vfs_s_find_inode (path_element->class, super, q, LINK_FOLLOW, FL_NONE);
  999. if (ino != NULL && (flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
  1000. {
  1001. path_element->class->verrno = EEXIST;
  1002. return NULL;
  1003. }
  1004. if (ino == NULL)
  1005. {
  1006. char *dirname, *name;
  1007. struct vfs_s_entry *ent;
  1008. struct vfs_s_inode *dir;
  1009. /* If the filesystem is read-only, disable file creation */
  1010. if ((flags & O_CREAT) == 0 || path_element->class->write == NULL)
  1011. return NULL;
  1012. dirname = g_path_get_dirname (q);
  1013. name = g_path_get_basename (q);
  1014. dir = vfs_s_find_inode (path_element->class, super, dirname, LINK_FOLLOW, FL_DIR);
  1015. if (dir == NULL)
  1016. {
  1017. g_free (dirname);
  1018. g_free (name);
  1019. return NULL;
  1020. }
  1021. ent = vfs_s_generate_entry (path_element->class, name, dir, 0755);
  1022. ino = ent->ino;
  1023. vfs_s_insert_entry (path_element->class, dir, ent);
  1024. if ((VFSDATA (path_element)->flags & VFS_S_USETMP) != 0)
  1025. {
  1026. int tmp_handle;
  1027. vfs_path_t *tmp_vpath;
  1028. tmp_handle = vfs_mkstemps (&tmp_vpath, path_element->class->name, name);
  1029. ino->localname = g_strdup (vfs_path_as_str (tmp_vpath));
  1030. vfs_path_free (tmp_vpath);
  1031. if (tmp_handle == -1)
  1032. {
  1033. g_free (dirname);
  1034. g_free (name);
  1035. return NULL;
  1036. }
  1037. close (tmp_handle);
  1038. }
  1039. g_free (dirname);
  1040. g_free (name);
  1041. was_changed = TRUE;
  1042. }
  1043. if (S_ISDIR (ino->st.st_mode))
  1044. {
  1045. path_element->class->verrno = EISDIR;
  1046. return NULL;
  1047. }
  1048. fh = g_new (vfs_file_handler_t, 1);
  1049. fh->pos = 0;
  1050. fh->ino = ino;
  1051. fh->handle = -1;
  1052. fh->changed = was_changed;
  1053. fh->linear = LS_NOT_LINEAR;
  1054. fh->data = NULL;
  1055. if (IS_LINEAR (flags))
  1056. {
  1057. if (VFSDATA (path_element)->linear_start != NULL)
  1058. {
  1059. vfs_print_message ("%s", _("Starting linear transfer..."));
  1060. fh->linear = LS_LINEAR_PREOPEN;
  1061. }
  1062. }
  1063. else
  1064. {
  1065. struct vfs_s_subclass *s;
  1066. s = VFSDATA (path_element);
  1067. if (s->fh_open != NULL && s->fh_open (path_element->class, fh, flags, mode) != 0)
  1068. {
  1069. if (s->fh_free_data != NULL)
  1070. s->fh_free_data (fh);
  1071. g_free (fh);
  1072. return NULL;
  1073. }
  1074. }
  1075. if ((VFSDATA (path_element)->flags & VFS_S_USETMP) != 0 && fh->ino->localname != NULL)
  1076. {
  1077. fh->handle = open (fh->ino->localname, NO_LINEAR (flags), mode);
  1078. if (fh->handle == -1)
  1079. {
  1080. g_free (fh);
  1081. path_element->class->verrno = errno;
  1082. return NULL;
  1083. }
  1084. }
  1085. /* i.e. we had no open files and now we have one */
  1086. vfs_rmstamp (path_element->class, (vfsid) super);
  1087. super->fd_usage++;
  1088. fh->ino->st.st_nlink++;
  1089. return fh;
  1090. }
  1091. /* --------------------------------------------------------------------------------------------- */
  1092. int
  1093. vfs_s_stat (const vfs_path_t * vpath, struct stat *buf)
  1094. {
  1095. return vfs_s_internal_stat (vpath, buf, FL_FOLLOW);
  1096. }
  1097. /* --------------------------------------------------------------------------------------------- */
  1098. int
  1099. vfs_s_lstat (const vfs_path_t * vpath, struct stat *buf)
  1100. {
  1101. return vfs_s_internal_stat (vpath, buf, FL_NONE);
  1102. }
  1103. /* --------------------------------------------------------------------------------------------- */
  1104. int
  1105. vfs_s_fstat (void *fh, struct stat *buf)
  1106. {
  1107. *buf = FH->ino->st;
  1108. return 0;
  1109. }
  1110. /* --------------------------------------------------------------------------------------------- */
  1111. int
  1112. vfs_s_retrieve_file (struct vfs_class *me, struct vfs_s_inode *ino)
  1113. {
  1114. /* If you want reget, you'll have to open file with O_LINEAR */
  1115. off_t total = 0;
  1116. char buffer[8192];
  1117. int handle;
  1118. ssize_t n;
  1119. off_t stat_size = ino->st.st_size;
  1120. vfs_file_handler_t fh;
  1121. vfs_path_t *tmp_vpath;
  1122. if ((MEDATA->flags & VFS_S_USETMP) == 0)
  1123. return (-1);
  1124. memset (&fh, 0, sizeof (fh));
  1125. fh.ino = ino;
  1126. fh.handle = -1;
  1127. handle = vfs_mkstemps (&tmp_vpath, me->name, ino->ent->name);
  1128. ino->localname = g_strdup (vfs_path_as_str (tmp_vpath));
  1129. vfs_path_free (tmp_vpath);
  1130. if (handle == -1)
  1131. {
  1132. me->verrno = errno;
  1133. goto error_4;
  1134. }
  1135. if (MEDATA->linear_start (me, &fh, 0) == 0)
  1136. goto error_3;
  1137. /* Clear the interrupt status */
  1138. tty_got_interrupt ();
  1139. tty_enable_interrupt_key ();
  1140. while ((n = MEDATA->linear_read (me, &fh, buffer, sizeof (buffer))) != 0)
  1141. {
  1142. int t;
  1143. if (n < 0)
  1144. goto error_1;
  1145. total += n;
  1146. vfs_s_print_stats (me->name, _("Getting file"), ino->ent->name, total, stat_size);
  1147. if (tty_got_interrupt ())
  1148. goto error_1;
  1149. t = write (handle, buffer, n);
  1150. if (t != n)
  1151. {
  1152. if (t == -1)
  1153. me->verrno = errno;
  1154. goto error_1;
  1155. }
  1156. }
  1157. MEDATA->linear_close (me, &fh);
  1158. close (handle);
  1159. tty_disable_interrupt_key ();
  1160. g_free (fh.data);
  1161. return 0;
  1162. error_1:
  1163. MEDATA->linear_close (me, &fh);
  1164. error_3:
  1165. tty_disable_interrupt_key ();
  1166. close (handle);
  1167. unlink (ino->localname);
  1168. error_4:
  1169. MC_PTR_FREE (ino->localname);
  1170. g_free (fh.data);
  1171. return (-1);
  1172. }
  1173. /* --------------------------------------------------------------------------------------------- */
  1174. /* ----------------------------- Stamping support -------------------------- */
  1175. /* Initialize one of our subclasses - fill common functions */
  1176. void
  1177. vfs_s_init_class (struct vfs_class *vclass, struct vfs_s_subclass *sub)
  1178. {
  1179. vclass->data = sub;
  1180. vclass->fill_names = vfs_s_fill_names;
  1181. vclass->open = vfs_s_open;
  1182. vclass->close = vfs_s_close;
  1183. vclass->read = vfs_s_read;
  1184. if ((sub->flags & VFS_S_READONLY) == 0)
  1185. vclass->write = vfs_s_write;
  1186. vclass->opendir = vfs_s_opendir;
  1187. vclass->readdir = vfs_s_readdir;
  1188. vclass->closedir = vfs_s_closedir;
  1189. vclass->stat = vfs_s_stat;
  1190. vclass->lstat = vfs_s_lstat;
  1191. vclass->fstat = vfs_s_fstat;
  1192. vclass->readlink = vfs_s_readlink;
  1193. vclass->chdir = vfs_s_chdir;
  1194. vclass->ferrno = vfs_s_ferrno;
  1195. vclass->lseek = vfs_s_lseek;
  1196. vclass->getid = vfs_s_getid;
  1197. vclass->nothingisopen = vfs_s_nothingisopen;
  1198. vclass->free = vfs_s_free;
  1199. if ((sub->flags & VFS_S_USETMP) != 0)
  1200. {
  1201. vclass->getlocalcopy = vfs_s_getlocalcopy;
  1202. vclass->ungetlocalcopy = vfs_s_ungetlocalcopy;
  1203. sub->find_entry = vfs_s_find_entry_linear;
  1204. }
  1205. else if ((sub->flags & VFS_S_REMOTE) != 0)
  1206. sub->find_entry = vfs_s_find_entry_linear;
  1207. else
  1208. sub->find_entry = vfs_s_find_entry_tree;
  1209. vclass->setctl = vfs_s_setctl;
  1210. sub->dir_uptodate = vfs_s_dir_uptodate;
  1211. }
  1212. /* --------------------------------------------------------------------------------------------- */
  1213. /** Find VFS id for given directory name */
  1214. vfsid
  1215. vfs_getid (const vfs_path_t * vpath)
  1216. {
  1217. const vfs_path_element_t *path_element;
  1218. path_element = vfs_path_get_by_index (vpath, -1);
  1219. if (!vfs_path_element_valid (path_element) || path_element->class->getid == NULL)
  1220. return NULL;
  1221. return (*path_element->class->getid) (vpath);
  1222. }
  1223. /* --------------------------------------------------------------------------------------------- */
  1224. /* ----------- Utility functions for networked filesystems -------------- */
  1225. #ifdef ENABLE_VFS_NET
  1226. int
  1227. vfs_s_select_on_two (int fd1, int fd2)
  1228. {
  1229. fd_set set;
  1230. struct timeval time_out;
  1231. int v;
  1232. int maxfd = MAX (fd1, fd2) + 1;
  1233. time_out.tv_sec = 1;
  1234. time_out.tv_usec = 0;
  1235. FD_ZERO (&set);
  1236. FD_SET (fd1, &set);
  1237. FD_SET (fd2, &set);
  1238. v = select (maxfd, &set, 0, 0, &time_out);
  1239. if (v <= 0)
  1240. return v;
  1241. if (FD_ISSET (fd1, &set))
  1242. return 1;
  1243. if (FD_ISSET (fd2, &set))
  1244. return 2;
  1245. return (-1);
  1246. }
  1247. /* --------------------------------------------------------------------------------------------- */
  1248. int
  1249. vfs_s_get_line (struct vfs_class *me, int sock, char *buf, int buf_len, char term)
  1250. {
  1251. FILE *logfile = MEDATA->logfile;
  1252. int i;
  1253. char c;
  1254. for (i = 0; i < buf_len - 1; i++, buf++)
  1255. {
  1256. if (read (sock, buf, sizeof (char)) <= 0)
  1257. return 0;
  1258. if (logfile != NULL)
  1259. {
  1260. size_t ret1;
  1261. int ret2;
  1262. ret1 = fwrite (buf, 1, 1, logfile);
  1263. ret2 = fflush (logfile);
  1264. (void) ret1;
  1265. (void) ret2;
  1266. }
  1267. if (*buf == term)
  1268. {
  1269. *buf = '\0';
  1270. return 1;
  1271. }
  1272. }
  1273. /* Line is too long - terminate buffer and discard the rest of line */
  1274. *buf = '\0';
  1275. while (read (sock, &c, sizeof (c)) > 0)
  1276. {
  1277. if (logfile != NULL)
  1278. {
  1279. size_t ret1;
  1280. int ret2;
  1281. ret1 = fwrite (&c, 1, 1, logfile);
  1282. ret2 = fflush (logfile);
  1283. (void) ret1;
  1284. (void) ret2;
  1285. }
  1286. if (c == '\n')
  1287. return 1;
  1288. }
  1289. return 0;
  1290. }
  1291. /* --------------------------------------------------------------------------------------------- */
  1292. int
  1293. vfs_s_get_line_interruptible (struct vfs_class *me, char *buffer, int size, int fd)
  1294. {
  1295. int i;
  1296. int res = 0;
  1297. (void) me;
  1298. tty_enable_interrupt_key ();
  1299. for (i = 0; i < size - 1; i++)
  1300. {
  1301. ssize_t n;
  1302. n = read (fd, &buffer[i], 1);
  1303. if (n == -1 && errno == EINTR)
  1304. {
  1305. buffer[i] = '\0';
  1306. res = EINTR;
  1307. goto ret;
  1308. }
  1309. if (n == 0)
  1310. {
  1311. buffer[i] = '\0';
  1312. goto ret;
  1313. }
  1314. if (buffer[i] == '\n')
  1315. {
  1316. buffer[i] = '\0';
  1317. res = 1;
  1318. goto ret;
  1319. }
  1320. }
  1321. buffer[size - 1] = '\0';
  1322. ret:
  1323. tty_disable_interrupt_key ();
  1324. return res;
  1325. }
  1326. #endif /* ENABLE_VFS_NET */
  1327. /* --------------------------------------------------------------------------------------------- */
  1328. /**
  1329. * Normalize filenames start position
  1330. */
  1331. void
  1332. vfs_s_normalize_filename_leading_spaces (struct vfs_s_inode *root_inode, size_t final_num_spaces)
  1333. {
  1334. GList *iter;
  1335. for (iter = root_inode->subdir; iter != NULL; iter = g_list_next (iter))
  1336. {
  1337. struct vfs_s_entry *entry = (struct vfs_s_entry *) iter->data;
  1338. if ((size_t) entry->ino->data_offset > final_num_spaces)
  1339. {
  1340. char *source_name = entry->name;
  1341. char *spacer;
  1342. spacer = g_strnfill (entry->ino->data_offset - final_num_spaces, ' ');
  1343. entry->name = g_strdup_printf ("%s%s", spacer, source_name);
  1344. g_free (spacer);
  1345. g_free (source_name);
  1346. }
  1347. entry->ino->data_offset = -1;
  1348. }
  1349. }
  1350. /* --------------------------------------------------------------------------------------------- */