direntry.c 43 KB

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