direntry.c 44 KB

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