direntry.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609
  1. /*
  2. Directory cache support
  3. Copyright (C) 1998-2014
  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 <fcntl.h> /* include fcntl.h -> sys/fcntl.h only */
  52. /* includes fcntl.h see IEEE Std 1003.1-2008 */
  53. #include <time.h>
  54. #include <sys/time.h> /* gettimeofday() */
  55. #include <inttypes.h> /* uintmax_t */
  56. #include <stdarg.h>
  57. #include "lib/global.h"
  58. #include "lib/tty/tty.h" /* enable/disable interrupt key */
  59. #include "lib/util.h" /* custom_canonicalize_pathname() */
  60. #if 0
  61. #include "lib/widget.h" /* message() */
  62. #endif
  63. #include "vfs.h"
  64. #include "utilvfs.h"
  65. #include "xdirentry.h"
  66. #include "gc.h" /* vfs_rmstamp */
  67. /*** global variables ****************************************************************************/
  68. /*** file scope macro definitions ****************************************************************/
  69. #define CALL(x) if (MEDATA->x) MEDATA->x
  70. /*** file scope type declarations ****************************************************************/
  71. struct dirhandle
  72. {
  73. GList *cur;
  74. struct vfs_s_inode *dir;
  75. };
  76. /*** file scope variables ************************************************************************/
  77. static volatile int total_inodes = 0, total_entries = 0;
  78. /*** file scope functions ************************************************************************/
  79. /* --------------------------------------------------------------------------------------------- */
  80. static int
  81. vfs_s_entry_compare (const void *a, const void *b)
  82. {
  83. const struct vfs_s_entry *e = (const struct vfs_s_entry *) a;
  84. const char *name = (const char *) b;
  85. return strcmp (e->name, name);
  86. }
  87. /* --------------------------------------------------------------------------------------------- */
  88. /* We were asked to create entries automagically */
  89. static struct vfs_s_entry *
  90. vfs_s_automake (struct vfs_class *me, struct vfs_s_inode *dir, char *path, int flags)
  91. {
  92. struct vfs_s_entry *res;
  93. char *sep;
  94. sep = strchr (path, PATH_SEP);
  95. if (sep != NULL)
  96. *sep = '\0';
  97. res = vfs_s_generate_entry (me, path, dir, flags & FL_MKDIR ? (0777 | S_IFDIR) : 0777);
  98. vfs_s_insert_entry (me, dir, res);
  99. if (sep != NULL)
  100. *sep = PATH_SEP;
  101. return res;
  102. }
  103. /* --------------------------------------------------------------------------------------------- */
  104. /* If the entry is a symlink, find the entry for its target */
  105. static struct vfs_s_entry *
  106. vfs_s_resolve_symlink (struct vfs_class *me, struct vfs_s_entry *entry, int follow)
  107. {
  108. char *linkname;
  109. char *fullname = NULL;
  110. struct vfs_s_entry *target;
  111. if (follow == LINK_NO_FOLLOW)
  112. return entry;
  113. if (follow == 0)
  114. ERRNOR (ELOOP, NULL);
  115. if (!entry)
  116. ERRNOR (ENOENT, NULL);
  117. if (!S_ISLNK (entry->ino->st.st_mode))
  118. return entry;
  119. linkname = entry->ino->linkname;
  120. if (linkname == NULL)
  121. ERRNOR (EFAULT, NULL);
  122. /* make full path from relative */
  123. if (*linkname != PATH_SEP)
  124. {
  125. char *fullpath = vfs_s_fullpath (me, entry->dir);
  126. if (fullpath)
  127. {
  128. fullname = g_strconcat (fullpath, "/", linkname, (char *) NULL);
  129. linkname = fullname;
  130. g_free (fullpath);
  131. }
  132. }
  133. target = (MEDATA->find_entry) (me, entry->dir->super->root, linkname, follow - 1, 0);
  134. g_free (fullname);
  135. return target;
  136. }
  137. /* --------------------------------------------------------------------------------------------- */
  138. /*
  139. * Follow > 0: follow links, serves as loop protect,
  140. * == -1: do not follow links
  141. */
  142. static struct vfs_s_entry *
  143. vfs_s_find_entry_tree (struct vfs_class *me, struct vfs_s_inode *root,
  144. const char *a_path, int follow, int flags)
  145. {
  146. size_t pseg;
  147. struct vfs_s_entry *ent = NULL;
  148. char *const pathref = g_strdup (a_path);
  149. char *path = pathref;
  150. /* canonicalize as well, but don't remove '../' from path */
  151. custom_canonicalize_pathname (path, CANON_PATH_ALL & (~CANON_PATH_REMDOUBLEDOTS));
  152. while (root != NULL)
  153. {
  154. GList *iter;
  155. while (*path == PATH_SEP) /* Strip leading '/' */
  156. path++;
  157. if (path[0] == '\0')
  158. {
  159. g_free (pathref);
  160. return ent;
  161. }
  162. for (pseg = 0; path[pseg] != '\0' && path[pseg] != PATH_SEP; pseg++)
  163. ;
  164. for (iter = root->subdir; iter != NULL; iter = g_list_next (iter))
  165. {
  166. ent = (struct vfs_s_entry *) iter->data;
  167. if (strlen (ent->name) == pseg && strncmp (ent->name, path, pseg) == 0)
  168. /* FOUND! */
  169. break;
  170. }
  171. ent = iter != NULL ? (struct vfs_s_entry *) iter->data : NULL;
  172. if (ent == NULL && (flags & (FL_MKFILE | FL_MKDIR)) != 0)
  173. ent = vfs_s_automake (me, root, path, flags);
  174. if (ent == NULL)
  175. {
  176. me->verrno = ENOENT;
  177. goto cleanup;
  178. }
  179. path += pseg;
  180. /* here we must follow leading directories always;
  181. only the actual file is optional */
  182. ent = vfs_s_resolve_symlink (me, ent,
  183. strchr (path, PATH_SEP) != NULL ? LINK_FOLLOW : follow);
  184. if (ent == NULL)
  185. goto cleanup;
  186. root = ent->ino;
  187. }
  188. cleanup:
  189. g_free (pathref);
  190. return NULL;
  191. }
  192. /* --------------------------------------------------------------------------------------------- */
  193. static struct vfs_s_entry *
  194. vfs_s_find_entry_linear (struct vfs_class *me, struct vfs_s_inode *root,
  195. const char *a_path, int follow, int flags)
  196. {
  197. struct vfs_s_entry *ent = NULL;
  198. char *const path = g_strdup (a_path);
  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. ent = vfs_s_find_entry_tree (me, ino, name, follow, flags);
  212. g_free (dirname);
  213. g_free (name);
  214. g_free (path);
  215. return ent;
  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. struct vfs_class *me = FH_SUPER->me;
  453. if (FH->linear == LS_LINEAR_PREOPEN)
  454. {
  455. if (!MEDATA->linear_start (me, FH, FH->pos))
  456. return -1;
  457. }
  458. if (FH->linear == LS_LINEAR_CLOSED)
  459. vfs_die ("linear_start() did not set linear_state!");
  460. if (FH->linear == LS_LINEAR_OPEN)
  461. return MEDATA->linear_read (me, FH, buffer, count);
  462. if (FH->handle != -1)
  463. {
  464. ssize_t n;
  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. struct vfs_class *me = FH_SUPER->me;
  478. if (FH->linear)
  479. vfs_die ("no writing to linear files, please");
  480. FH->changed = 1;
  481. if (FH->handle != -1)
  482. {
  483. ssize_t n;
  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. if (me == NULL)
  530. return (-1);
  531. FH_SUPER->fd_usage--;
  532. if (!FH_SUPER->fd_usage)
  533. vfs_stamp_create (me, FH_SUPER);
  534. if (FH->linear == LS_LINEAR_OPEN)
  535. MEDATA->linear_close (me, fh);
  536. if (MEDATA->fh_close)
  537. res = MEDATA->fh_close (me, fh);
  538. if ((MEDATA->flags & VFS_S_USETMP) && FH->changed && MEDATA->file_store)
  539. {
  540. char *s = vfs_s_fullpath (me, FH->ino);
  541. if (!s)
  542. res = -1;
  543. else
  544. {
  545. res = MEDATA->file_store (me, fh, s, FH->ino->localname);
  546. g_free (s);
  547. }
  548. vfs_s_invalidate (me, FH_SUPER);
  549. }
  550. if (FH->handle != -1)
  551. close (FH->handle);
  552. vfs_s_free_inode (me, FH->ino);
  553. if (MEDATA->fh_free_data != NULL)
  554. MEDATA->fh_free_data (fh);
  555. g_free (fh);
  556. return res;
  557. }
  558. /* --------------------------------------------------------------------------------------------- */
  559. static void
  560. vfs_s_print_stats (const char *fs_name, const char *action,
  561. const char *file_name, off_t have, off_t need)
  562. {
  563. static const char *i18n_percent_transf_format = NULL;
  564. static const char *i18n_transf_format = NULL;
  565. if (i18n_percent_transf_format == NULL)
  566. {
  567. i18n_percent_transf_format = "%s: %s: %s %3d%% (%" PRIuMAX " %s";
  568. i18n_transf_format = "%s: %s: %s %" PRIuMAX " %s";
  569. }
  570. if (need)
  571. vfs_print_message (i18n_percent_transf_format, fs_name, action,
  572. file_name, (int) ((double) have * 100 / need), (uintmax_t) have,
  573. _("bytes transferred"));
  574. else
  575. vfs_print_message (i18n_transf_format, fs_name, action, file_name, (uintmax_t) have,
  576. _("bytes transferred"));
  577. }
  578. /* --------------------------------------------------------------------------------------------- */
  579. /* ------------------------------- mc support ---------------------------- */
  580. static void
  581. vfs_s_fill_names (struct vfs_class *me, fill_names_f func)
  582. {
  583. GList *iter;
  584. for (iter = MEDATA->supers; iter != NULL; iter = g_list_next (iter))
  585. {
  586. const struct vfs_s_super *super = (const struct vfs_s_super *) iter->data;
  587. char *name;
  588. name = g_strconcat (super->name, "/", me->prefix, VFS_PATH_URL_DELIMITER,
  589. /* super->current_dir->name, */ (char *) NULL);
  590. func (name);
  591. g_free (name);
  592. }
  593. }
  594. /* --------------------------------------------------------------------------------------------- */
  595. static int
  596. vfs_s_ferrno (struct vfs_class *me)
  597. {
  598. return me->verrno;
  599. }
  600. /* --------------------------------------------------------------------------------------------- */
  601. /**
  602. * Get local copy of the given file. We reuse the existing file cache
  603. * for remote filesystems. Archives use standard VFS facilities.
  604. */
  605. static vfs_path_t *
  606. vfs_s_getlocalcopy (const vfs_path_t * vpath)
  607. {
  608. vfs_file_handler_t *fh;
  609. vfs_path_t *local = NULL;
  610. if (vpath == NULL)
  611. return NULL;
  612. fh = vfs_s_open (vpath, O_RDONLY, 0);
  613. if (fh != NULL)
  614. {
  615. const struct vfs_class *me;
  616. me = vfs_path_get_by_index (vpath, -1)->class;
  617. if ((MEDATA->flags & VFS_S_USETMP) != 0 && (fh->ino != NULL))
  618. local = vfs_path_from_str_flags (fh->ino->localname, VPF_NO_CANON);
  619. vfs_s_close (fh);
  620. }
  621. return local;
  622. }
  623. /* --------------------------------------------------------------------------------------------- */
  624. /**
  625. * Return the local copy. Since we are using our cache, we do nothing -
  626. * the cache will be removed when the archive is closed.
  627. */
  628. static int
  629. vfs_s_ungetlocalcopy (const vfs_path_t * vpath, const vfs_path_t * local, gboolean has_changed)
  630. {
  631. (void) vpath;
  632. (void) local;
  633. (void) has_changed;
  634. return 0;
  635. }
  636. /* --------------------------------------------------------------------------------------------- */
  637. static int
  638. vfs_s_setctl (const vfs_path_t * vpath, int ctlop, void *arg)
  639. {
  640. const vfs_path_element_t *path_element;
  641. path_element = vfs_path_get_by_index (vpath, -1);
  642. switch (ctlop)
  643. {
  644. case VFS_SETCTL_STALE_DATA:
  645. {
  646. struct vfs_s_inode *ino;
  647. ino = vfs_s_inode_from_path (vpath, 0);
  648. if (ino == NULL)
  649. return 0;
  650. if (arg)
  651. ino->super->want_stale = 1;
  652. else
  653. {
  654. ino->super->want_stale = 0;
  655. vfs_s_invalidate (path_element->class, ino->super);
  656. }
  657. return 1;
  658. }
  659. case VFS_SETCTL_LOGFILE:
  660. ((struct vfs_s_subclass *) path_element->class->data)->logfile = fopen ((char *) arg, "w");
  661. return 1;
  662. case VFS_SETCTL_FLUSH:
  663. ((struct vfs_s_subclass *) path_element->class->data)->flush = 1;
  664. return 1;
  665. }
  666. return 0;
  667. }
  668. /* --------------------------------------------------------------------------------------------- */
  669. /* ----------------------------- Stamping support -------------------------- */
  670. static vfsid
  671. vfs_s_getid (const vfs_path_t * vpath)
  672. {
  673. struct vfs_s_super *archive = NULL;
  674. const char *p;
  675. p = vfs_s_get_path (vpath, &archive, FL_NO_OPEN);
  676. if (p == NULL)
  677. return NULL;
  678. return (vfsid) archive;
  679. }
  680. /* --------------------------------------------------------------------------------------------- */
  681. static int
  682. vfs_s_nothingisopen (vfsid id)
  683. {
  684. (void) id;
  685. /* Our data structures should survive free of superblock at any time */
  686. return 1;
  687. }
  688. /* --------------------------------------------------------------------------------------------- */
  689. static void
  690. vfs_s_free (vfsid id)
  691. {
  692. vfs_s_free_super (((struct vfs_s_super *) id)->me, (struct vfs_s_super *) id);
  693. }
  694. /* --------------------------------------------------------------------------------------------- */
  695. static int
  696. vfs_s_dir_uptodate (struct vfs_class *me, struct vfs_s_inode *ino)
  697. {
  698. struct timeval tim;
  699. if (MEDATA->flush)
  700. {
  701. MEDATA->flush = 0;
  702. return 0;
  703. }
  704. gettimeofday (&tim, NULL);
  705. if (tim.tv_sec < ino->timestamp.tv_sec)
  706. return 1;
  707. return 0;
  708. }
  709. /* --------------------------------------------------------------------------------------------- */
  710. /*** public functions ****************************************************************************/
  711. /* --------------------------------------------------------------------------------------------- */
  712. struct vfs_s_inode *
  713. vfs_s_new_inode (struct vfs_class *me, struct vfs_s_super *super, struct stat *initstat)
  714. {
  715. struct vfs_s_inode *ino;
  716. ino = g_try_new0 (struct vfs_s_inode, 1);
  717. if (ino == NULL)
  718. return NULL;
  719. if (initstat)
  720. ino->st = *initstat;
  721. ino->super = super;
  722. ino->st.st_nlink = 0;
  723. ino->st.st_ino = MEDATA->inode_counter++;
  724. ino->st.st_dev = MEDATA->rdev;
  725. super->ino_usage++;
  726. total_inodes++;
  727. CALL (init_inode) (me, ino);
  728. return ino;
  729. }
  730. /* --------------------------------------------------------------------------------------------- */
  731. void
  732. vfs_s_free_inode (struct vfs_class *me, struct vfs_s_inode *ino)
  733. {
  734. if (ino == NULL)
  735. vfs_die ("Don't pass NULL to me");
  736. /* ==0 can happen if freshly created entry is deleted */
  737. if (ino->st.st_nlink > 1)
  738. {
  739. ino->st.st_nlink--;
  740. return;
  741. }
  742. while (ino->subdir != NULL)
  743. vfs_s_free_entry (me, (struct vfs_s_entry *) ino->subdir->data);
  744. CALL (free_inode) (me, ino);
  745. g_free (ino->linkname);
  746. if ((MEDATA->flags & VFS_S_USETMP) != 0 && ino->localname != NULL)
  747. {
  748. unlink (ino->localname);
  749. g_free (ino->localname);
  750. }
  751. total_inodes--;
  752. ino->super->ino_usage--;
  753. g_free (ino);
  754. }
  755. /* --------------------------------------------------------------------------------------------- */
  756. struct vfs_s_entry *
  757. vfs_s_new_entry (struct vfs_class *me, const char *name, struct vfs_s_inode *inode)
  758. {
  759. struct vfs_s_entry *entry;
  760. entry = g_new0 (struct vfs_s_entry, 1);
  761. total_entries++;
  762. entry->name = g_strdup (name);
  763. entry->ino = inode;
  764. entry->ino->ent = entry;
  765. CALL (init_entry) (me, entry);
  766. return entry;
  767. }
  768. /* --------------------------------------------------------------------------------------------- */
  769. void
  770. vfs_s_free_entry (struct vfs_class *me, struct vfs_s_entry *ent)
  771. {
  772. if (ent->dir != NULL)
  773. ent->dir->subdir = g_list_remove (ent->dir->subdir, ent);
  774. g_free (ent->name);
  775. /* ent->name = NULL; */
  776. if (ent->ino != NULL)
  777. {
  778. ent->ino->ent = NULL;
  779. vfs_s_free_inode (me, ent->ino);
  780. }
  781. total_entries--;
  782. g_free (ent);
  783. }
  784. /* --------------------------------------------------------------------------------------------- */
  785. void
  786. vfs_s_insert_entry (struct vfs_class *me, struct vfs_s_inode *dir, struct vfs_s_entry *ent)
  787. {
  788. (void) me;
  789. ent->dir = dir;
  790. ent->ino->st.st_nlink++;
  791. dir->subdir = g_list_append (dir->subdir, ent);
  792. }
  793. /* --------------------------------------------------------------------------------------------- */
  794. struct stat *
  795. vfs_s_default_stat (struct vfs_class *me, mode_t mode)
  796. {
  797. static struct stat st;
  798. int myumask;
  799. (void) me;
  800. myumask = umask (022);
  801. umask (myumask);
  802. mode &= ~myumask;
  803. st.st_mode = mode;
  804. st.st_ino = 0;
  805. st.st_dev = 0;
  806. st.st_rdev = 0;
  807. st.st_uid = getuid ();
  808. st.st_gid = getgid ();
  809. st.st_size = 0;
  810. st.st_mtime = st.st_atime = st.st_ctime = time (NULL);
  811. return &st;
  812. }
  813. /* --------------------------------------------------------------------------------------------- */
  814. struct vfs_s_entry *
  815. vfs_s_generate_entry (struct vfs_class *me, const char *name, struct vfs_s_inode *parent,
  816. mode_t mode)
  817. {
  818. struct vfs_s_inode *inode;
  819. struct stat *st;
  820. st = vfs_s_default_stat (me, mode);
  821. inode = vfs_s_new_inode (me, parent->super, st);
  822. return vfs_s_new_entry (me, name, inode);
  823. }
  824. /* --------------------------------------------------------------------------------------------- */
  825. struct vfs_s_inode *
  826. vfs_s_find_inode (struct vfs_class *me, const struct vfs_s_super *super,
  827. const char *path, int follow, int flags)
  828. {
  829. struct vfs_s_entry *ent;
  830. if (((MEDATA->flags & VFS_S_REMOTE) == 0) && (*path == '\0'))
  831. return super->root;
  832. ent = (MEDATA->find_entry) (me, super->root, path, follow, flags);
  833. return (ent != NULL) ? ent->ino : NULL;
  834. }
  835. /* --------------------------------------------------------------------------------------------- */
  836. /* Ook, these were functions around directory entries / inodes */
  837. /* -------------------------------- superblock games -------------------------- */
  838. /**
  839. * get superlock object by vpath
  840. *
  841. * @param vpath path
  842. * @return superlock object or NULL if not found
  843. */
  844. struct vfs_s_super *
  845. vfs_get_super_by_vpath (const vfs_path_t * vpath)
  846. {
  847. GList *iter;
  848. void *cookie = NULL;
  849. const vfs_path_element_t *path_element;
  850. struct vfs_s_subclass *subclass;
  851. struct vfs_s_super *super = NULL;
  852. vfs_path_t *vpath_archive;
  853. path_element = vfs_path_get_by_index (vpath, -1);
  854. subclass = ((struct vfs_s_subclass *) path_element->class->data);
  855. if (subclass == NULL)
  856. return NULL;
  857. vpath_archive = vfs_path_clone (vpath);
  858. vfs_path_remove_element_by_index (vpath_archive, -1);
  859. if (subclass->archive_check != NULL)
  860. {
  861. cookie = subclass->archive_check (vpath_archive);
  862. if (cookie == NULL)
  863. goto ret;
  864. }
  865. for (iter = subclass->supers; iter != NULL; iter = g_list_next (iter))
  866. {
  867. int i;
  868. super = (struct vfs_s_super *) iter->data;
  869. /* 0 == other, 1 == same, return it, 2 == other but stop scanning */
  870. i = subclass->archive_same (path_element, super, vpath_archive, cookie);
  871. if (i == 1)
  872. goto ret;
  873. if (i != 0)
  874. break;
  875. super = NULL;
  876. }
  877. ret:
  878. vfs_path_free (vpath_archive);
  879. return super;
  880. }
  881. /* --------------------------------------------------------------------------------------------- */
  882. /**
  883. * get path from last VFS-element and create corresponding superblock
  884. *
  885. * @param vpath source path object
  886. * @param archive pointer to object for store newly created superblock
  887. * @param flags flags
  888. *
  889. * @return path from last VFS-element
  890. */
  891. const char *
  892. vfs_s_get_path (const vfs_path_t * vpath, struct vfs_s_super **archive, int flags)
  893. {
  894. const char *retval = "";
  895. int result = -1;
  896. struct vfs_s_super *super;
  897. const vfs_path_element_t *path_element;
  898. struct vfs_s_subclass *subclass;
  899. path_element = vfs_path_get_by_index (vpath, -1);
  900. if (path_element->path != NULL)
  901. retval = path_element->path;
  902. super = vfs_get_super_by_vpath (vpath);
  903. if (super != NULL)
  904. goto return_success;
  905. if (flags & FL_NO_OPEN)
  906. {
  907. path_element->class->verrno = EIO;
  908. return NULL;
  909. }
  910. super = vfs_s_new_super (path_element->class);
  911. subclass = ((struct vfs_s_subclass *) path_element->class->data);
  912. if (subclass->open_archive != NULL)
  913. {
  914. vfs_path_t *vpath_archive;
  915. vpath_archive = vfs_path_clone (vpath);
  916. vfs_path_remove_element_by_index (vpath_archive, -1);
  917. result = subclass->open_archive (super, vpath_archive, path_element);
  918. vfs_path_free (vpath_archive);
  919. }
  920. if (result == -1)
  921. {
  922. vfs_s_free_super (path_element->class, super);
  923. path_element->class->verrno = EIO;
  924. return NULL;
  925. }
  926. if (!super->name)
  927. vfs_die ("You have to fill name\n");
  928. if (!super->root)
  929. vfs_die ("You have to fill root inode\n");
  930. vfs_s_insert_super (path_element->class, super);
  931. vfs_stamp_create (path_element->class, super);
  932. return_success:
  933. *archive = super;
  934. return retval;
  935. }
  936. /* --------------------------------------------------------------------------------------------- */
  937. void
  938. vfs_s_invalidate (struct vfs_class *me, struct vfs_s_super *super)
  939. {
  940. if (!super->want_stale)
  941. {
  942. vfs_s_free_inode (me, super->root);
  943. super->root = vfs_s_new_inode (me, super, vfs_s_default_stat (me, S_IFDIR | 0755));
  944. }
  945. }
  946. /* --------------------------------------------------------------------------------------------- */
  947. char *
  948. vfs_s_fullpath (struct vfs_class *me, struct vfs_s_inode *ino)
  949. {
  950. if (!ino->ent)
  951. ERRNOR (EAGAIN, NULL);
  952. if ((MEDATA->flags & VFS_S_USETMP) == 0)
  953. {
  954. /* archives */
  955. char *newpath;
  956. char *path = g_strdup (ino->ent->name);
  957. while (1)
  958. {
  959. ino = ino->ent->dir;
  960. if (ino == ino->super->root)
  961. break;
  962. newpath = g_strconcat (ino->ent->name, "/", path, (char *) NULL);
  963. g_free (path);
  964. path = newpath;
  965. }
  966. return path;
  967. }
  968. /* remote systems */
  969. if ((!ino->ent->dir) || (!ino->ent->dir->ent))
  970. return g_strdup (ino->ent->name);
  971. return g_strconcat (ino->ent->dir->ent->name, PATH_SEP_STR, ino->ent->name, (char *) NULL);
  972. }
  973. /* --------------------------------------------------------------------------------------------- */
  974. /* --------------------------- stat and friends ---------------------------- */
  975. void *
  976. vfs_s_open (const vfs_path_t * vpath, int flags, mode_t mode)
  977. {
  978. int was_changed = 0;
  979. vfs_file_handler_t *fh;
  980. struct vfs_s_super *super;
  981. const char *q;
  982. struct vfs_s_inode *ino;
  983. const vfs_path_element_t *path_element;
  984. path_element = vfs_path_get_by_index (vpath, -1);
  985. q = vfs_s_get_path (vpath, &super, 0);
  986. if (q == NULL)
  987. return NULL;
  988. ino = vfs_s_find_inode (path_element->class, super, q, LINK_FOLLOW, FL_NONE);
  989. if (ino && ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL)))
  990. {
  991. path_element->class->verrno = EEXIST;
  992. return NULL;
  993. }
  994. if (!ino)
  995. {
  996. char *dirname, *name;
  997. struct vfs_s_entry *ent;
  998. struct vfs_s_inode *dir;
  999. /* If the filesystem is read-only, disable file creation */
  1000. if (!(flags & O_CREAT) || !(path_element->class->write))
  1001. return NULL;
  1002. dirname = g_path_get_dirname (q);
  1003. name = g_path_get_basename (q);
  1004. dir = vfs_s_find_inode (path_element->class, super, dirname, LINK_FOLLOW, FL_DIR);
  1005. if (dir == NULL)
  1006. {
  1007. g_free (dirname);
  1008. g_free (name);
  1009. return NULL;
  1010. }
  1011. ent = vfs_s_generate_entry (path_element->class, name, dir, 0755);
  1012. ino = ent->ino;
  1013. vfs_s_insert_entry (path_element->class, dir, ent);
  1014. if ((VFSDATA (path_element)->flags & VFS_S_USETMP) != 0)
  1015. {
  1016. int tmp_handle;
  1017. vfs_path_t *tmp_vpath;
  1018. tmp_handle = vfs_mkstemps (&tmp_vpath, path_element->class->name, name);
  1019. ino->localname = g_strdup (vfs_path_as_str (tmp_vpath));
  1020. vfs_path_free (tmp_vpath);
  1021. if (tmp_handle == -1)
  1022. {
  1023. g_free (dirname);
  1024. g_free (name);
  1025. return NULL;
  1026. }
  1027. close (tmp_handle);
  1028. }
  1029. g_free (dirname);
  1030. g_free (name);
  1031. was_changed = 1;
  1032. }
  1033. if (S_ISDIR (ino->st.st_mode))
  1034. {
  1035. path_element->class->verrno = EISDIR;
  1036. return NULL;
  1037. }
  1038. fh = g_new (vfs_file_handler_t, 1);
  1039. fh->pos = 0;
  1040. fh->ino = ino;
  1041. fh->handle = -1;
  1042. fh->changed = was_changed;
  1043. fh->linear = 0;
  1044. fh->data = NULL;
  1045. if (IS_LINEAR (flags))
  1046. {
  1047. if (VFSDATA (path_element)->linear_start)
  1048. {
  1049. vfs_print_message (_("Starting linear transfer..."));
  1050. fh->linear = LS_LINEAR_PREOPEN;
  1051. }
  1052. }
  1053. else
  1054. {
  1055. struct vfs_s_subclass *s;
  1056. s = VFSDATA (path_element);
  1057. if (s->fh_open != NULL && s->fh_open (path_element->class, fh, flags, mode) != 0)
  1058. {
  1059. if (s->fh_free_data != NULL)
  1060. s->fh_free_data (fh);
  1061. g_free (fh);
  1062. return NULL;
  1063. }
  1064. }
  1065. if ((VFSDATA (path_element)->flags & VFS_S_USETMP) != 0 && fh->ino->localname != NULL)
  1066. {
  1067. fh->handle = open (fh->ino->localname, NO_LINEAR (flags), mode);
  1068. if (fh->handle == -1)
  1069. {
  1070. g_free (fh);
  1071. path_element->class->verrno = errno;
  1072. return NULL;
  1073. }
  1074. }
  1075. /* i.e. we had no open files and now we have one */
  1076. vfs_rmstamp (path_element->class, (vfsid) super);
  1077. super->fd_usage++;
  1078. fh->ino->st.st_nlink++;
  1079. return fh;
  1080. }
  1081. /* --------------------------------------------------------------------------------------------- */
  1082. int
  1083. vfs_s_retrieve_file (struct vfs_class *me, struct vfs_s_inode *ino)
  1084. {
  1085. /* If you want reget, you'll have to open file with O_LINEAR */
  1086. off_t total = 0;
  1087. char buffer[8192];
  1088. int handle;
  1089. ssize_t n;
  1090. off_t stat_size = ino->st.st_size;
  1091. vfs_file_handler_t fh;
  1092. vfs_path_t *tmp_vpath;
  1093. if ((MEDATA->flags & VFS_S_USETMP) == 0)
  1094. return -1;
  1095. memset (&fh, 0, sizeof (fh));
  1096. fh.ino = ino;
  1097. fh.handle = -1;
  1098. handle = vfs_mkstemps (&tmp_vpath, me->name, ino->ent->name);
  1099. ino->localname = g_strdup (vfs_path_as_str (tmp_vpath));
  1100. vfs_path_free (tmp_vpath);
  1101. if (handle == -1)
  1102. {
  1103. me->verrno = errno;
  1104. goto error_4;
  1105. }
  1106. if (!MEDATA->linear_start (me, &fh, 0))
  1107. goto error_3;
  1108. /* Clear the interrupt status */
  1109. tty_got_interrupt ();
  1110. tty_enable_interrupt_key ();
  1111. while ((n = MEDATA->linear_read (me, &fh, buffer, sizeof (buffer))))
  1112. {
  1113. int t;
  1114. if (n < 0)
  1115. goto error_1;
  1116. total += n;
  1117. vfs_s_print_stats (me->name, _("Getting file"), ino->ent->name, total, stat_size);
  1118. if (tty_got_interrupt ())
  1119. goto error_1;
  1120. t = write (handle, buffer, n);
  1121. if (t != n)
  1122. {
  1123. if (t == -1)
  1124. me->verrno = errno;
  1125. goto error_1;
  1126. }
  1127. }
  1128. MEDATA->linear_close (me, &fh);
  1129. close (handle);
  1130. tty_disable_interrupt_key ();
  1131. g_free (fh.data);
  1132. return 0;
  1133. error_1:
  1134. MEDATA->linear_close (me, &fh);
  1135. error_3:
  1136. tty_disable_interrupt_key ();
  1137. close (handle);
  1138. unlink (ino->localname);
  1139. error_4:
  1140. g_free (ino->localname);
  1141. ino->localname = NULL;
  1142. g_free (fh.data);
  1143. return -1;
  1144. }
  1145. /* --------------------------------------------------------------------------------------------- */
  1146. /* ----------------------------- Stamping support -------------------------- */
  1147. /* Initialize one of our subclasses - fill common functions */
  1148. void
  1149. vfs_s_init_class (struct vfs_class *vclass, struct vfs_s_subclass *sub)
  1150. {
  1151. vclass->data = sub;
  1152. vclass->fill_names = vfs_s_fill_names;
  1153. vclass->open = vfs_s_open;
  1154. vclass->close = vfs_s_close;
  1155. vclass->read = vfs_s_read;
  1156. if (!(sub->flags & VFS_S_READONLY))
  1157. {
  1158. vclass->write = vfs_s_write;
  1159. }
  1160. vclass->opendir = vfs_s_opendir;
  1161. vclass->readdir = vfs_s_readdir;
  1162. vclass->closedir = vfs_s_closedir;
  1163. vclass->stat = vfs_s_stat;
  1164. vclass->lstat = vfs_s_lstat;
  1165. vclass->fstat = vfs_s_fstat;
  1166. vclass->readlink = vfs_s_readlink;
  1167. vclass->chdir = vfs_s_chdir;
  1168. vclass->ferrno = vfs_s_ferrno;
  1169. vclass->lseek = vfs_s_lseek;
  1170. vclass->getid = vfs_s_getid;
  1171. vclass->nothingisopen = vfs_s_nothingisopen;
  1172. vclass->free = vfs_s_free;
  1173. if ((sub->flags & VFS_S_USETMP) != 0)
  1174. {
  1175. vclass->getlocalcopy = vfs_s_getlocalcopy;
  1176. vclass->ungetlocalcopy = vfs_s_ungetlocalcopy;
  1177. sub->find_entry = vfs_s_find_entry_linear;
  1178. }
  1179. else if ((sub->flags & VFS_S_REMOTE) != 0)
  1180. sub->find_entry = vfs_s_find_entry_linear;
  1181. else
  1182. sub->find_entry = vfs_s_find_entry_tree;
  1183. vclass->setctl = vfs_s_setctl;
  1184. sub->dir_uptodate = vfs_s_dir_uptodate;
  1185. }
  1186. /* --------------------------------------------------------------------------------------------- */
  1187. /** Find VFS id for given directory name */
  1188. vfsid
  1189. vfs_getid (const vfs_path_t * vpath)
  1190. {
  1191. const vfs_path_element_t *path_element;
  1192. path_element = vfs_path_get_by_index (vpath, -1);
  1193. if (!vfs_path_element_valid (path_element) || path_element->class->getid == NULL)
  1194. return NULL;
  1195. return (*path_element->class->getid) (vpath);
  1196. }
  1197. /* --------------------------------------------------------------------------------------------- */
  1198. /* ----------- Utility functions for networked filesystems -------------- */
  1199. #ifdef ENABLE_VFS_NET
  1200. int
  1201. vfs_s_select_on_two (int fd1, int fd2)
  1202. {
  1203. fd_set set;
  1204. struct timeval time_out;
  1205. int v;
  1206. int maxfd = (fd1 > fd2 ? fd1 : fd2) + 1;
  1207. time_out.tv_sec = 1;
  1208. time_out.tv_usec = 0;
  1209. FD_ZERO (&set);
  1210. FD_SET (fd1, &set);
  1211. FD_SET (fd2, &set);
  1212. v = select (maxfd, &set, 0, 0, &time_out);
  1213. if (v <= 0)
  1214. return v;
  1215. if (FD_ISSET (fd1, &set))
  1216. return 1;
  1217. if (FD_ISSET (fd2, &set))
  1218. return 2;
  1219. return -1;
  1220. }
  1221. /* --------------------------------------------------------------------------------------------- */
  1222. int
  1223. vfs_s_get_line (struct vfs_class *me, int sock, char *buf, int buf_len, char term)
  1224. {
  1225. FILE *logfile = MEDATA->logfile;
  1226. int i;
  1227. char c;
  1228. for (i = 0; i < buf_len - 1; i++, buf++)
  1229. {
  1230. if (read (sock, buf, sizeof (char)) <= 0)
  1231. return 0;
  1232. if (logfile)
  1233. {
  1234. size_t ret1;
  1235. int ret2;
  1236. ret1 = fwrite (buf, 1, 1, logfile);
  1237. ret2 = fflush (logfile);
  1238. (void) ret1;
  1239. (void) ret2;
  1240. }
  1241. if (*buf == term)
  1242. {
  1243. *buf = 0;
  1244. return 1;
  1245. }
  1246. }
  1247. /* Line is too long - terminate buffer and discard the rest of line */
  1248. *buf = 0;
  1249. while (read (sock, &c, sizeof (c)) > 0)
  1250. {
  1251. if (logfile)
  1252. {
  1253. size_t ret1;
  1254. int ret2;
  1255. ret1 = fwrite (&c, 1, 1, logfile);
  1256. ret2 = fflush (logfile);
  1257. (void) ret1;
  1258. (void) ret2;
  1259. }
  1260. if (c == '\n')
  1261. return 1;
  1262. }
  1263. return 0;
  1264. }
  1265. /* --------------------------------------------------------------------------------------------- */
  1266. int
  1267. vfs_s_get_line_interruptible (struct vfs_class *me, char *buffer, int size, int fd)
  1268. {
  1269. int i;
  1270. (void) me;
  1271. tty_enable_interrupt_key ();
  1272. for (i = 0; i < size - 1; i++)
  1273. {
  1274. int n;
  1275. n = read (fd, buffer + i, 1);
  1276. tty_disable_interrupt_key ();
  1277. if (n == -1 && errno == EINTR)
  1278. {
  1279. buffer[i] = 0;
  1280. return EINTR;
  1281. }
  1282. if (n == 0)
  1283. {
  1284. buffer[i] = 0;
  1285. return 0;
  1286. }
  1287. if (buffer[i] == '\n')
  1288. {
  1289. buffer[i] = 0;
  1290. return 1;
  1291. }
  1292. }
  1293. buffer[size - 1] = 0;
  1294. return 0;
  1295. }
  1296. #endif /* ENABLE_VFS_NET */
  1297. /* --------------------------------------------------------------------------------------------- */
  1298. /**
  1299. * Normalize filenames start position
  1300. */
  1301. void
  1302. vfs_s_normalize_filename_leading_spaces (struct vfs_s_inode *root_inode, size_t final_num_spaces)
  1303. {
  1304. GList *iter;
  1305. for (iter = root_inode->subdir; iter != NULL; iter = g_list_next (iter))
  1306. {
  1307. struct vfs_s_entry *entry = (struct vfs_s_entry *) iter->data;
  1308. if ((size_t) entry->ino->data_offset > final_num_spaces)
  1309. {
  1310. char *source_name = entry->name;
  1311. char *spacer = g_strnfill (entry->ino->data_offset - final_num_spaces, ' ');
  1312. entry->name = g_strdup_printf ("%s%s", spacer, source_name);
  1313. g_free (spacer);
  1314. g_free (source_name);
  1315. }
  1316. entry->ino->data_offset = -1;
  1317. }
  1318. }
  1319. /* --------------------------------------------------------------------------------------------- */