direntry.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744
  1. /*
  2. Directory cache support
  3. Copyright (C) 1998-2019
  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 <inttypes.h> /* uintmax_t */
  52. #include <stdarg.h>
  53. #ifdef HAVE_SYS_SELECT_H
  54. #include <sys/select.h>
  55. #endif
  56. #include <sys/time.h> /* gettimeofday() */
  57. #include <sys/types.h>
  58. #include <unistd.h>
  59. #include "lib/global.h"
  60. #include "lib/tty/tty.h" /* enable/disable interrupt key */
  61. #include "lib/util.h" /* custom_canonicalize_pathname() */
  62. #if 0
  63. #include "lib/widget.h" /* message() */
  64. #endif
  65. #include "vfs.h"
  66. #include "utilvfs.h"
  67. #include "xdirentry.h"
  68. #include "gc.h" /* vfs_rmstamp */
  69. /*** global variables ****************************************************************************/
  70. /*** file scope macro definitions ****************************************************************/
  71. #define CALL(x) \
  72. if (VFS_SUBCLASS (me)->x != NULL) \
  73. VFS_SUBCLASS (me)->x
  74. /*** file scope type declarations ****************************************************************/
  75. struct dirhandle
  76. {
  77. GList *cur;
  78. struct vfs_s_inode *dir;
  79. };
  80. /*** file scope variables ************************************************************************/
  81. /* --------------------------------------------------------------------------------------------- */
  82. /*** file scope functions ************************************************************************/
  83. /* --------------------------------------------------------------------------------------------- */
  84. /* We were asked to create entries automagically */
  85. static struct vfs_s_entry *
  86. vfs_s_automake (struct vfs_class *me, struct vfs_s_inode *dir, char *path, int flags)
  87. {
  88. struct vfs_s_entry *res;
  89. char *sep;
  90. sep = strchr (path, PATH_SEP);
  91. if (sep != NULL)
  92. *sep = '\0';
  93. res = vfs_s_generate_entry (me, path, dir, (flags & FL_MKDIR) != 0 ? (0777 | S_IFDIR) : 0777);
  94. vfs_s_insert_entry (me, dir, res);
  95. if (sep != NULL)
  96. *sep = PATH_SEP;
  97. return res;
  98. }
  99. /* --------------------------------------------------------------------------------------------- */
  100. /* If the entry is a symlink, find the entry for its target */
  101. static struct vfs_s_entry *
  102. vfs_s_resolve_symlink (struct vfs_class *me, struct vfs_s_entry *entry, int follow)
  103. {
  104. char *linkname;
  105. char *fullname = NULL;
  106. struct vfs_s_entry *target;
  107. if (follow == LINK_NO_FOLLOW)
  108. return entry;
  109. if (follow == 0)
  110. ERRNOR (ELOOP, NULL);
  111. if (entry == NULL)
  112. ERRNOR (ENOENT, NULL);
  113. if (!S_ISLNK (entry->ino->st.st_mode))
  114. return entry;
  115. linkname = entry->ino->linkname;
  116. if (linkname == NULL)
  117. ERRNOR (EFAULT, NULL);
  118. /* make full path from relative */
  119. if (!IS_PATH_SEP (*linkname))
  120. {
  121. char *fullpath;
  122. fullpath = vfs_s_fullpath (me, entry->dir);
  123. if (fullpath != NULL)
  124. {
  125. fullname = g_strconcat (fullpath, PATH_SEP_STR, linkname, (char *) NULL);
  126. linkname = fullname;
  127. g_free (fullpath);
  128. }
  129. }
  130. target =
  131. VFS_SUBCLASS (me)->find_entry (me, entry->dir->super->root, linkname, follow - 1, FL_NONE);
  132. g_free (fullname);
  133. return target;
  134. }
  135. /* --------------------------------------------------------------------------------------------- */
  136. /*
  137. * Follow > 0: follow links, serves as loop protect,
  138. * == -1: do not follow links
  139. */
  140. static struct vfs_s_entry *
  141. vfs_s_find_entry_tree (struct vfs_class *me, struct vfs_s_inode *root,
  142. const char *a_path, int follow, int flags)
  143. {
  144. size_t pseg;
  145. struct vfs_s_entry *ent = NULL;
  146. char *const pathref = g_strdup (a_path);
  147. char *path = pathref;
  148. /* canonicalize as well, but don't remove '../' from path */
  149. custom_canonicalize_pathname (path, CANON_PATH_ALL & (~CANON_PATH_REMDOUBLEDOTS));
  150. while (root != NULL)
  151. {
  152. GList *iter;
  153. while (IS_PATH_SEP (*path)) /* Strip leading '/' */
  154. path++;
  155. if (path[0] == '\0')
  156. {
  157. g_free (pathref);
  158. return ent;
  159. }
  160. for (pseg = 0; path[pseg] != '\0' && !IS_PATH_SEP (path[pseg]); pseg++)
  161. ;
  162. for (iter = g_queue_peek_head_link (root->subdir); iter != NULL; iter = g_list_next (iter))
  163. {
  164. ent = VFS_ENTRY (iter->data);
  165. if (strlen (ent->name) == pseg && strncmp (ent->name, path, pseg) == 0)
  166. /* FOUND! */
  167. break;
  168. }
  169. ent = iter != NULL ? VFS_ENTRY (iter->data) : NULL;
  170. if (ent == NULL && (flags & (FL_MKFILE | FL_MKDIR)) != 0)
  171. ent = vfs_s_automake (me, root, path, flags);
  172. if (ent == NULL)
  173. {
  174. me->verrno = ENOENT;
  175. goto cleanup;
  176. }
  177. path += pseg;
  178. /* here we must follow leading directories always;
  179. only the actual file is optional */
  180. ent = vfs_s_resolve_symlink (me, ent,
  181. strchr (path, PATH_SEP) != NULL ? LINK_FOLLOW : follow);
  182. if (ent == NULL)
  183. goto cleanup;
  184. root = ent->ino;
  185. }
  186. cleanup:
  187. g_free (pathref);
  188. return NULL;
  189. }
  190. /* --------------------------------------------------------------------------------------------- */
  191. static struct vfs_s_entry *
  192. vfs_s_find_entry_linear (struct vfs_class *me, struct vfs_s_inode *root,
  193. const char *a_path, int follow, int flags)
  194. {
  195. struct vfs_s_entry *ent = NULL;
  196. char *const path = g_strdup (a_path);
  197. GList *iter;
  198. if (root->super->root != root)
  199. vfs_die ("We have to use _real_ root. Always. Sorry.");
  200. /* canonicalize as well, but don't remove '../' from path */
  201. custom_canonicalize_pathname (path, CANON_PATH_ALL & (~CANON_PATH_REMDOUBLEDOTS));
  202. if ((flags & FL_DIR) == 0)
  203. {
  204. char *dirname, *name;
  205. struct vfs_s_inode *ino;
  206. dirname = g_path_get_dirname (path);
  207. name = g_path_get_basename (path);
  208. ino = vfs_s_find_inode (me, root->super, dirname, follow, flags | FL_DIR);
  209. ent = vfs_s_find_entry_tree (me, ino, name, follow, flags);
  210. g_free (dirname);
  211. g_free (name);
  212. g_free (path);
  213. return ent;
  214. }
  215. iter = g_queue_find_custom (root->subdir, path, (GCompareFunc) vfs_s_entry_compare);
  216. ent = iter != NULL ? VFS_ENTRY (iter->data) : NULL;
  217. if (ent != NULL && !VFS_SUBCLASS (me)->dir_uptodate (me, ent->ino))
  218. {
  219. #if 1
  220. vfs_print_message (_("Directory cache expired for %s"), path);
  221. #endif
  222. vfs_s_free_entry (me, ent);
  223. ent = NULL;
  224. }
  225. if (ent == NULL)
  226. {
  227. struct vfs_s_inode *ino;
  228. ino = vfs_s_new_inode (me, root->super, vfs_s_default_stat (me, S_IFDIR | 0755));
  229. ent = vfs_s_new_entry (me, path, ino);
  230. if (VFS_SUBCLASS (me)->dir_load (me, ino, path) == -1)
  231. {
  232. vfs_s_free_entry (me, ent);
  233. g_free (path);
  234. return NULL;
  235. }
  236. vfs_s_insert_entry (me, root, ent);
  237. iter = g_queue_find_custom (root->subdir, path, (GCompareFunc) vfs_s_entry_compare);
  238. ent = iter != NULL ? VFS_ENTRY (iter->data) : NULL;
  239. }
  240. if (ent == NULL)
  241. vfs_die ("find_linear: success but directory is not there\n");
  242. #if 0
  243. if (vfs_s_resolve_symlink (me, ent, follow) == NULL)
  244. {
  245. g_free (path);
  246. return NULL;
  247. }
  248. #endif
  249. g_free (path);
  250. return ent;
  251. }
  252. /* --------------------------------------------------------------------------------------------- */
  253. /* Ook, these were functions around directory entries / inodes */
  254. /* -------------------------------- superblock games -------------------------- */
  255. static struct vfs_s_super *
  256. vfs_s_new_super (struct vfs_class *me)
  257. {
  258. struct vfs_s_super *super;
  259. super = g_new0 (struct vfs_s_super, 1);
  260. super->me = me;
  261. return super;
  262. }
  263. /* --------------------------------------------------------------------------------------------- */
  264. static inline void
  265. vfs_s_insert_super (struct vfs_class *me, struct vfs_s_super *super)
  266. {
  267. VFS_SUBCLASS (me)->supers = g_list_prepend (VFS_SUBCLASS (me)->supers, super);
  268. }
  269. /* --------------------------------------------------------------------------------------------- */
  270. static void
  271. vfs_s_free_super (struct vfs_class *me, struct vfs_s_super *super)
  272. {
  273. if (super->root != NULL)
  274. {
  275. vfs_s_free_inode (me, super->root);
  276. super->root = NULL;
  277. }
  278. #if 0
  279. /* FIXME: We currently leak small ammount of memory, sometimes. Fix it if you can. */
  280. if (super->ino_usage != 0)
  281. message (D_ERROR, "Direntry warning",
  282. "Super ino_usage is %d, memory leak", super->ino_usage);
  283. if (super->want_stale)
  284. message (D_ERROR, "Direntry warning", "%s", "Super has want_stale set");
  285. #endif
  286. VFS_SUBCLASS (me)->supers = g_list_remove (VFS_SUBCLASS (me)->supers, super);
  287. CALL (free_archive) (me, super);
  288. #ifdef ENABLE_VFS_NET
  289. vfs_path_element_free (super->path_element);
  290. #endif
  291. g_free (super->name);
  292. g_free (super);
  293. }
  294. /* --------------------------------------------------------------------------------------------- */
  295. static vfs_file_handler_t *
  296. vfs_s_new_fh (struct vfs_s_inode *ino, gboolean changed)
  297. {
  298. vfs_file_handler_t *fh;
  299. fh = g_new0 (vfs_file_handler_t, 1);
  300. vfs_s_init_fh (fh, ino, changed);
  301. return fh;
  302. }
  303. /* --------------------------------------------------------------------------------------------- */
  304. static void
  305. vfs_s_free_fh (struct vfs_s_subclass *s, vfs_file_handler_t * fh)
  306. {
  307. if (s->fh_free != NULL)
  308. s->fh_free (fh);
  309. g_free (fh);
  310. }
  311. /* --------------------------------------------------------------------------------------------- */
  312. /* Support of archives */
  313. /* ------------------------ readdir & friends ----------------------------- */
  314. static struct vfs_s_inode *
  315. vfs_s_inode_from_path (const vfs_path_t * vpath, int flags)
  316. {
  317. struct vfs_s_super *super;
  318. struct vfs_s_inode *ino;
  319. const char *q;
  320. const vfs_path_element_t *path_element;
  321. q = vfs_s_get_path (vpath, &super, 0);
  322. if (q == NULL)
  323. return NULL;
  324. path_element = vfs_path_get_by_index (vpath, -1);
  325. ino =
  326. vfs_s_find_inode (path_element->class, super, q,
  327. (flags & FL_FOLLOW) != 0 ? LINK_FOLLOW : LINK_NO_FOLLOW,
  328. flags & ~FL_FOLLOW);
  329. if (ino == NULL && *q == '\0')
  330. /* We are asking about / directory of ftp server: assume it exists */
  331. ino =
  332. vfs_s_find_inode (path_element->class, super, q,
  333. (flags & FL_FOLLOW) != 0 ? LINK_FOLLOW : LINK_NO_FOLLOW,
  334. FL_DIR | (flags & ~FL_FOLLOW));
  335. return ino;
  336. }
  337. /* --------------------------------------------------------------------------------------------- */
  338. static void *
  339. vfs_s_opendir (const vfs_path_t * vpath)
  340. {
  341. struct vfs_s_inode *dir;
  342. struct dirhandle *info;
  343. const vfs_path_element_t *path_element;
  344. dir = vfs_s_inode_from_path (vpath, FL_DIR | FL_FOLLOW);
  345. if (dir == NULL)
  346. return NULL;
  347. path_element = vfs_path_get_by_index (vpath, -1);
  348. if (!S_ISDIR (dir->st.st_mode))
  349. {
  350. path_element->class->verrno = ENOTDIR;
  351. return NULL;
  352. }
  353. dir->st.st_nlink++;
  354. #if 0
  355. if (dir->subdir == NULL) /* This can actually happen if we allow empty directories */
  356. {
  357. path_element->class->verrno = EAGAIN;
  358. return NULL;
  359. }
  360. #endif
  361. info = g_new (struct dirhandle, 1);
  362. info->cur = g_queue_peek_head_link (dir->subdir);
  363. info->dir = dir;
  364. return info;
  365. }
  366. /* --------------------------------------------------------------------------------------------- */
  367. static void *
  368. vfs_s_readdir (void *data)
  369. {
  370. static union vfs_dirent dir;
  371. struct dirhandle *info = (struct dirhandle *) data;
  372. const char *name;
  373. if (info->cur == NULL || info->cur->data == NULL)
  374. return NULL;
  375. name = VFS_ENTRY (info->cur->data)->name;
  376. if (name != NULL)
  377. g_strlcpy (dir.dent.d_name, name, MC_MAXPATHLEN);
  378. else
  379. vfs_die ("Null in structure-cannot happen");
  380. info->cur = g_list_next (info->cur);
  381. return (void *) &dir;
  382. }
  383. /* --------------------------------------------------------------------------------------------- */
  384. static int
  385. vfs_s_closedir (void *data)
  386. {
  387. struct dirhandle *info = (struct dirhandle *) data;
  388. struct vfs_s_inode *dir = info->dir;
  389. vfs_s_free_inode (dir->super->me, dir);
  390. g_free (data);
  391. return 0;
  392. }
  393. /* --------------------------------------------------------------------------------------------- */
  394. static int
  395. vfs_s_chdir (const vfs_path_t * vpath)
  396. {
  397. void *data;
  398. data = vfs_s_opendir (vpath);
  399. if (data == NULL)
  400. return (-1);
  401. vfs_s_closedir (data);
  402. return 0;
  403. }
  404. /* --------------------------------------------------------------------------------------------- */
  405. /* --------------------------- stat and friends ---------------------------- */
  406. static int
  407. vfs_s_internal_stat (const vfs_path_t * vpath, struct stat *buf, int flag)
  408. {
  409. struct vfs_s_inode *ino;
  410. ino = vfs_s_inode_from_path (vpath, flag);
  411. if (ino == NULL)
  412. return (-1);
  413. *buf = ino->st;
  414. return 0;
  415. }
  416. /* --------------------------------------------------------------------------------------------- */
  417. static int
  418. vfs_s_readlink (const vfs_path_t * vpath, char *buf, size_t size)
  419. {
  420. struct vfs_s_inode *ino;
  421. size_t len;
  422. const vfs_path_element_t *path_element;
  423. ino = vfs_s_inode_from_path (vpath, 0);
  424. if (ino == NULL)
  425. return (-1);
  426. path_element = vfs_path_get_by_index (vpath, -1);
  427. if (!S_ISLNK (ino->st.st_mode))
  428. {
  429. path_element->class->verrno = EINVAL;
  430. return (-1);
  431. }
  432. if (ino->linkname == NULL)
  433. {
  434. path_element->class->verrno = EFAULT;
  435. return (-1);
  436. }
  437. len = strlen (ino->linkname);
  438. if (size < len)
  439. len = size;
  440. /* readlink() does not append a NUL character to buf */
  441. memcpy (buf, ino->linkname, len);
  442. return len;
  443. }
  444. /* --------------------------------------------------------------------------------------------- */
  445. static ssize_t
  446. vfs_s_read (void *fh, char *buffer, size_t count)
  447. {
  448. vfs_file_handler_t *file = VFS_FILE_HANDLER (fh);
  449. struct vfs_class *me = VFS_FILE_HANDLER_SUPER (fh)->me;
  450. if (file->linear == LS_LINEAR_PREOPEN)
  451. {
  452. if (VFS_SUBCLASS (me)->linear_start (me, file, file->pos) == 0)
  453. return (-1);
  454. }
  455. if (file->linear == LS_LINEAR_CLOSED)
  456. vfs_die ("linear_start() did not set linear_state!");
  457. if (file->linear == LS_LINEAR_OPEN)
  458. return VFS_SUBCLASS (me)->linear_read (me, file, buffer, count);
  459. if (file->handle != -1)
  460. {
  461. ssize_t n;
  462. n = read (file->handle, buffer, count);
  463. if (n < 0)
  464. me->verrno = errno;
  465. return n;
  466. }
  467. vfs_die ("vfs_s_read: This should not happen\n");
  468. return (-1);
  469. }
  470. /* --------------------------------------------------------------------------------------------- */
  471. static ssize_t
  472. vfs_s_write (void *fh, const char *buffer, size_t count)
  473. {
  474. vfs_file_handler_t *file = VFS_FILE_HANDLER (fh);
  475. struct vfs_class *me = VFS_FILE_HANDLER_SUPER (fh)->me;
  476. if (file->linear != LS_NOT_LINEAR)
  477. vfs_die ("no writing to linear files, please");
  478. file->changed = TRUE;
  479. if (file->handle != -1)
  480. {
  481. ssize_t n;
  482. n = write (file->handle, buffer, count);
  483. if (n < 0)
  484. me->verrno = errno;
  485. return n;
  486. }
  487. vfs_die ("vfs_s_write: This should not happen\n");
  488. return 0;
  489. }
  490. /* --------------------------------------------------------------------------------------------- */
  491. static off_t
  492. vfs_s_lseek (void *fh, off_t offset, int whence)
  493. {
  494. vfs_file_handler_t *file = VFS_FILE_HANDLER (fh);
  495. off_t size = file->ino->st.st_size;
  496. if (file->linear == LS_LINEAR_OPEN)
  497. vfs_die ("cannot lseek() after linear_read!");
  498. if (file->handle != -1)
  499. { /* If we have local file opened, we want to work with it */
  500. off_t retval;
  501. retval = lseek (file->handle, offset, whence);
  502. if (retval == -1)
  503. VFS_FILE_HANDLER_SUPER (fh)->me->verrno = errno;
  504. return retval;
  505. }
  506. switch (whence)
  507. {
  508. case SEEK_CUR:
  509. offset += file->pos;
  510. break;
  511. case SEEK_END:
  512. offset += size;
  513. break;
  514. default:
  515. break;
  516. }
  517. if (offset < 0)
  518. file->pos = 0;
  519. else if (offset < size)
  520. file->pos = offset;
  521. else
  522. file->pos = size;
  523. return file->pos;
  524. }
  525. /* --------------------------------------------------------------------------------------------- */
  526. static int
  527. vfs_s_close (void *fh)
  528. {
  529. vfs_file_handler_t *file = VFS_FILE_HANDLER (fh);
  530. struct vfs_s_super *super = VFS_FILE_HANDLER_SUPER (fh);
  531. struct vfs_class *me = super->me;
  532. struct vfs_s_subclass *sub = VFS_SUBCLASS (me);
  533. int res = 0;
  534. if (me == NULL)
  535. return (-1);
  536. super->fd_usage--;
  537. if (super->fd_usage == 0)
  538. vfs_stamp_create (me, VFS_FILE_HANDLER_SUPER (fh));
  539. if (file->linear == LS_LINEAR_OPEN)
  540. sub->linear_close (me, fh);
  541. if (sub->fh_close != NULL)
  542. res = sub->fh_close (me, fh);
  543. if ((me->flags & VFSF_USETMP) != 0 && file->changed && sub->file_store != NULL)
  544. {
  545. char *s;
  546. s = vfs_s_fullpath (me, file->ino);
  547. if (s == NULL)
  548. res = -1;
  549. else
  550. {
  551. res = sub->file_store (me, fh, s, file->ino->localname);
  552. g_free (s);
  553. }
  554. vfs_s_invalidate (me, super);
  555. }
  556. if (file->handle != -1)
  557. {
  558. close (file->handle);
  559. file->handle = -1;
  560. }
  561. vfs_s_free_inode (me, file->ino);
  562. vfs_s_free_fh (sub, fh);
  563. return res;
  564. }
  565. /* --------------------------------------------------------------------------------------------- */
  566. static void
  567. vfs_s_print_stats (const char *fs_name, const char *action,
  568. const char *file_name, off_t have, off_t need)
  569. {
  570. if (need != 0)
  571. vfs_print_message (_("%s: %s: %s %3d%% (%lld) bytes transferred"), fs_name, action,
  572. file_name, (int) ((double) have * 100 / need), (long long) have);
  573. else
  574. vfs_print_message (_("%s: %s: %s %lld bytes transferred"), fs_name, action, file_name,
  575. (long long) have);
  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 = VFS_SUBCLASS (me)->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, PATH_SEP_STR, 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 ((me->flags & VFSF_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 != NULL)
  650. ino->super->want_stale = TRUE;
  651. else
  652. {
  653. ino->super->want_stale = FALSE;
  654. vfs_s_invalidate (path_element->class, ino->super);
  655. }
  656. return 1;
  657. }
  658. case VFS_SETCTL_LOGFILE:
  659. path_element->class->logfile = fopen ((char *) arg, "w");
  660. return 1;
  661. case VFS_SETCTL_FLUSH:
  662. path_element->class->flush = TRUE;
  663. return 1;
  664. default:
  665. return 0;
  666. }
  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 gboolean
  682. vfs_s_nothingisopen (vfsid id)
  683. {
  684. return (VFS_SUPER (id)->fd_usage <= 0);
  685. }
  686. /* --------------------------------------------------------------------------------------------- */
  687. static void
  688. vfs_s_free (vfsid id)
  689. {
  690. vfs_s_free_super (VFS_SUPER (id)->me, VFS_SUPER (id));
  691. }
  692. /* --------------------------------------------------------------------------------------------- */
  693. static int
  694. vfs_s_dir_uptodate (struct vfs_class *me, struct vfs_s_inode *ino)
  695. {
  696. struct timeval tim;
  697. if (me->flush)
  698. {
  699. me->flush = FALSE;
  700. return 0;
  701. }
  702. gettimeofday (&tim, NULL);
  703. return (tim.tv_sec < ino->timestamp.tv_sec ? 1 : 0);
  704. }
  705. /* --------------------------------------------------------------------------------------------- */
  706. /*** public functions ****************************************************************************/
  707. /* --------------------------------------------------------------------------------------------- */
  708. struct vfs_s_inode *
  709. vfs_s_new_inode (struct vfs_class *me, struct vfs_s_super *super, struct stat *initstat)
  710. {
  711. struct vfs_s_inode *ino;
  712. ino = g_try_new0 (struct vfs_s_inode, 1);
  713. if (ino == NULL)
  714. return NULL;
  715. if (initstat != NULL)
  716. ino->st = *initstat;
  717. ino->super = super;
  718. ino->subdir = g_queue_new ();
  719. ino->st.st_nlink = 0;
  720. ino->st.st_ino = VFS_SUBCLASS (me)->inode_counter++;
  721. ino->st.st_dev = VFS_SUBCLASS (me)->rdev;
  722. super->ino_usage++;
  723. CALL (init_inode) (me, ino);
  724. return ino;
  725. }
  726. /* --------------------------------------------------------------------------------------------- */
  727. void
  728. vfs_s_free_inode (struct vfs_class *me, struct vfs_s_inode *ino)
  729. {
  730. if (ino == NULL)
  731. vfs_die ("Don't pass NULL to me");
  732. /* ==0 can happen if freshly created entry is deleted */
  733. if (ino->st.st_nlink > 1)
  734. {
  735. ino->st.st_nlink--;
  736. return;
  737. }
  738. while (g_queue_get_length (ino->subdir) != 0)
  739. {
  740. struct vfs_s_entry *entry;
  741. entry = VFS_ENTRY (g_queue_peek_head (ino->subdir));
  742. vfs_s_free_entry (me, entry);
  743. }
  744. g_queue_free (ino->subdir);
  745. ino->subdir = NULL;
  746. CALL (free_inode) (me, ino);
  747. g_free (ino->linkname);
  748. if ((me->flags & VFSF_USETMP) != 0 && ino->localname != NULL)
  749. {
  750. unlink (ino->localname);
  751. g_free (ino->localname);
  752. }
  753. ino->super->ino_usage--;
  754. g_free (ino);
  755. }
  756. /* --------------------------------------------------------------------------------------------- */
  757. struct vfs_s_entry *
  758. vfs_s_new_entry (struct vfs_class *me, const char *name, struct vfs_s_inode *inode)
  759. {
  760. struct vfs_s_entry *entry;
  761. entry = g_new0 (struct vfs_s_entry, 1);
  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. g_queue_remove (ent->dir->subdir, ent);
  774. MC_PTR_FREE (ent->name);
  775. if (ent->ino != NULL)
  776. {
  777. ent->ino->ent = NULL;
  778. vfs_s_free_inode (me, ent->ino);
  779. }
  780. g_free (ent);
  781. }
  782. /* --------------------------------------------------------------------------------------------- */
  783. void
  784. vfs_s_insert_entry (struct vfs_class *me, struct vfs_s_inode *dir, struct vfs_s_entry *ent)
  785. {
  786. (void) me;
  787. ent->dir = dir;
  788. ent->ino->st.st_nlink++;
  789. g_queue_push_tail (dir->subdir, ent);
  790. }
  791. /* --------------------------------------------------------------------------------------------- */
  792. int
  793. vfs_s_entry_compare (const void *a, const void *b)
  794. {
  795. const struct vfs_s_entry *e = (const struct vfs_s_entry *) a;
  796. const char *name = (const char *) b;
  797. return strcmp (e->name, name);
  798. }
  799. /* --------------------------------------------------------------------------------------------- */
  800. struct stat *
  801. vfs_s_default_stat (struct vfs_class *me, mode_t mode)
  802. {
  803. static struct stat st;
  804. mode_t myumask;
  805. (void) me;
  806. myumask = umask (022);
  807. umask (myumask);
  808. mode &= ~myumask;
  809. st.st_mode = mode;
  810. st.st_ino = 0;
  811. st.st_dev = 0;
  812. #ifdef HAVE_STRUCT_STAT_ST_RDEV
  813. st.st_rdev = 0;
  814. #endif
  815. st.st_uid = getuid ();
  816. st.st_gid = getgid ();
  817. #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
  818. st.st_blksize = 512;
  819. #endif
  820. st.st_size = 0;
  821. st.st_mtime = st.st_atime = st.st_ctime = time (NULL);
  822. #ifdef HAVE_STRUCT_STAT_ST_MTIM
  823. st.st_atim.tv_nsec = st.st_mtim.tv_nsec = st.st_ctim.tv_nsec = 0;
  824. #endif
  825. vfs_adjust_stat (&st);
  826. return &st;
  827. }
  828. /* --------------------------------------------------------------------------------------------- */
  829. /**
  830. * Calculate number of st_blocks using st_size and st_blksize.
  831. * In according to stat(2), st_blocks is the size in 512-byte units.
  832. *
  833. * @param s stat info
  834. */
  835. void
  836. vfs_adjust_stat (struct stat *s)
  837. {
  838. #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
  839. if (s->st_size == 0)
  840. s->st_blocks = 0;
  841. else
  842. {
  843. #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
  844. blkcnt_t ioblocks;
  845. blksize_t ioblock_size;
  846. /* 1. Calculate how many IO blocks are occupied */
  847. ioblocks = 1 + (s->st_size - 1) / s->st_blksize;
  848. /* 2. Calculate size of st_blksize in 512-byte units */
  849. ioblock_size = 1 + (s->st_blksize - 1) / 512;
  850. /* 3. Calculate number of blocks */
  851. s->st_blocks = ioblocks * ioblock_size;
  852. #else
  853. /* Let IO block size is 512 bytes */
  854. s->st_blocks = 1 + (s->st_size - 1) / 512;
  855. #endif /* HAVE_STRUCT_STAT_ST_BLKSIZE */
  856. }
  857. #endif /* HAVE_STRUCT_STAT_ST_BLOCKS */
  858. }
  859. /* --------------------------------------------------------------------------------------------- */
  860. struct vfs_s_entry *
  861. vfs_s_generate_entry (struct vfs_class *me, const char *name, struct vfs_s_inode *parent,
  862. mode_t mode)
  863. {
  864. struct vfs_s_inode *inode;
  865. struct stat *st;
  866. st = vfs_s_default_stat (me, mode);
  867. inode = vfs_s_new_inode (me, parent->super, st);
  868. return vfs_s_new_entry (me, name, inode);
  869. }
  870. /* --------------------------------------------------------------------------------------------- */
  871. struct vfs_s_inode *
  872. vfs_s_find_inode (struct vfs_class *me, const struct vfs_s_super *super,
  873. const char *path, int follow, int flags)
  874. {
  875. struct vfs_s_entry *ent;
  876. if (((me->flags & VFSF_REMOTE) == 0) && (*path == '\0'))
  877. return super->root;
  878. ent = VFS_SUBCLASS (me)->find_entry (me, super->root, path, follow, flags);
  879. return (ent != NULL ? ent->ino : NULL);
  880. }
  881. /* --------------------------------------------------------------------------------------------- */
  882. /* Ook, these were functions around directory entries / inodes */
  883. /* -------------------------------- superblock games -------------------------- */
  884. /**
  885. * get superlock object by vpath
  886. *
  887. * @param vpath path
  888. * @return superlock object or NULL if not found
  889. */
  890. struct vfs_s_super *
  891. vfs_get_super_by_vpath (const vfs_path_t * vpath)
  892. {
  893. GList *iter;
  894. void *cookie = NULL;
  895. const vfs_path_element_t *path_element;
  896. struct vfs_s_subclass *subclass;
  897. struct vfs_s_super *super = NULL;
  898. vfs_path_t *vpath_archive;
  899. path_element = vfs_path_get_by_index (vpath, -1);
  900. subclass = VFS_SUBCLASS (path_element->class);
  901. vpath_archive = vfs_path_clone (vpath);
  902. vfs_path_remove_element_by_index (vpath_archive, -1);
  903. if (subclass->archive_check != NULL)
  904. {
  905. cookie = subclass->archive_check (vpath_archive);
  906. if (cookie == NULL)
  907. goto ret;
  908. }
  909. if (subclass->archive_same == NULL)
  910. goto ret;
  911. for (iter = subclass->supers; iter != NULL; iter = g_list_next (iter))
  912. {
  913. int i;
  914. super = VFS_SUPER (iter->data);
  915. /* 0 == other, 1 == same, return it, 2 == other but stop scanning */
  916. i = subclass->archive_same (path_element, super, vpath_archive, cookie);
  917. if (i == 1)
  918. goto ret;
  919. if (i != 0)
  920. break;
  921. super = NULL;
  922. }
  923. ret:
  924. vfs_path_free (vpath_archive);
  925. return super;
  926. }
  927. /* --------------------------------------------------------------------------------------------- */
  928. /**
  929. * get path from last VFS-element and create corresponding superblock
  930. *
  931. * @param vpath source path object
  932. * @param archive pointer to object for store newly created superblock
  933. * @param flags flags
  934. *
  935. * @return path from last VFS-element
  936. */
  937. const char *
  938. vfs_s_get_path (const vfs_path_t * vpath, struct vfs_s_super **archive, int flags)
  939. {
  940. const char *retval = "";
  941. int result = -1;
  942. struct vfs_s_super *super;
  943. const vfs_path_element_t *path_element;
  944. struct vfs_s_subclass *subclass;
  945. path_element = vfs_path_get_by_index (vpath, -1);
  946. if (path_element->path != NULL)
  947. retval = path_element->path;
  948. super = vfs_get_super_by_vpath (vpath);
  949. if (super != NULL)
  950. goto return_success;
  951. if ((flags & FL_NO_OPEN) != 0)
  952. {
  953. path_element->class->verrno = EIO;
  954. return NULL;
  955. }
  956. subclass = VFS_SUBCLASS (path_element->class);
  957. super = subclass->new_archive != NULL ?
  958. subclass->new_archive (path_element->class) : vfs_s_new_super (path_element->class);
  959. if (subclass->open_archive != NULL)
  960. {
  961. vfs_path_t *vpath_archive;
  962. vpath_archive = vfs_path_clone (vpath);
  963. vfs_path_remove_element_by_index (vpath_archive, -1);
  964. result = subclass->open_archive (super, vpath_archive, path_element);
  965. vfs_path_free (vpath_archive);
  966. }
  967. if (result == -1)
  968. {
  969. vfs_s_free_super (path_element->class, super);
  970. path_element->class->verrno = EIO;
  971. return NULL;
  972. }
  973. if (super->name == NULL)
  974. vfs_die ("You have to fill name\n");
  975. if (super->root == NULL)
  976. vfs_die ("You have to fill root inode\n");
  977. vfs_s_insert_super (path_element->class, super);
  978. vfs_stamp_create (path_element->class, super);
  979. return_success:
  980. *archive = super;
  981. return retval;
  982. }
  983. /* --------------------------------------------------------------------------------------------- */
  984. void
  985. vfs_s_invalidate (struct vfs_class *me, struct vfs_s_super *super)
  986. {
  987. if (!super->want_stale)
  988. {
  989. vfs_s_free_inode (me, super->root);
  990. super->root = vfs_s_new_inode (me, super, vfs_s_default_stat (me, S_IFDIR | 0755));
  991. }
  992. }
  993. /* --------------------------------------------------------------------------------------------- */
  994. char *
  995. vfs_s_fullpath (struct vfs_class *me, struct vfs_s_inode *ino)
  996. {
  997. if (ino->ent == NULL)
  998. ERRNOR (EAGAIN, NULL);
  999. if ((me->flags & VFSF_USETMP) == 0)
  1000. {
  1001. /* archives */
  1002. char *path;
  1003. path = g_strdup (ino->ent->name);
  1004. while (TRUE)
  1005. {
  1006. char *newpath;
  1007. ino = ino->ent->dir;
  1008. if (ino == ino->super->root)
  1009. break;
  1010. newpath = g_strconcat (ino->ent->name, PATH_SEP_STR, path, (char *) NULL);
  1011. g_free (path);
  1012. path = newpath;
  1013. }
  1014. return path;
  1015. }
  1016. /* remote systems */
  1017. if (ino->ent->dir == NULL || ino->ent->dir->ent == NULL)
  1018. return g_strdup (ino->ent->name);
  1019. return g_strconcat (ino->ent->dir->ent->name, PATH_SEP_STR, ino->ent->name, (char *) NULL);
  1020. }
  1021. /* --------------------------------------------------------------------------------------------- */
  1022. void
  1023. vfs_s_init_fh (vfs_file_handler_t * fh, struct vfs_s_inode *ino, gboolean changed)
  1024. {
  1025. fh->ino = ino;
  1026. fh->handle = -1;
  1027. fh->changed = changed;
  1028. fh->linear = LS_NOT_LINEAR;
  1029. }
  1030. /* --------------------------------------------------------------------------------------------- */
  1031. /* --------------------------- stat and friends ---------------------------- */
  1032. void *
  1033. vfs_s_open (const vfs_path_t * vpath, int flags, mode_t mode)
  1034. {
  1035. gboolean was_changed = FALSE;
  1036. vfs_file_handler_t *fh;
  1037. struct vfs_s_super *super;
  1038. const char *q;
  1039. struct vfs_s_inode *ino;
  1040. const vfs_path_element_t *path_element;
  1041. struct vfs_s_subclass *s;
  1042. q = vfs_s_get_path (vpath, &super, 0);
  1043. if (q == NULL)
  1044. return NULL;
  1045. path_element = vfs_path_get_by_index (vpath, -1);
  1046. ino = vfs_s_find_inode (path_element->class, super, q, LINK_FOLLOW, FL_NONE);
  1047. if (ino != NULL && (flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
  1048. {
  1049. path_element->class->verrno = EEXIST;
  1050. return NULL;
  1051. }
  1052. s = VFS_SUBCLASS (path_element->class);
  1053. if (ino == NULL)
  1054. {
  1055. char *dirname, *name;
  1056. struct vfs_s_entry *ent;
  1057. struct vfs_s_inode *dir;
  1058. /* If the filesystem is read-only, disable file creation */
  1059. if ((flags & O_CREAT) == 0 || path_element->class->write == NULL)
  1060. return NULL;
  1061. dirname = g_path_get_dirname (q);
  1062. name = g_path_get_basename (q);
  1063. dir = vfs_s_find_inode (path_element->class, super, dirname, LINK_FOLLOW, FL_DIR);
  1064. if (dir == NULL)
  1065. {
  1066. g_free (dirname);
  1067. g_free (name);
  1068. return NULL;
  1069. }
  1070. ent = vfs_s_generate_entry (path_element->class, name, dir, 0755);
  1071. ino = ent->ino;
  1072. vfs_s_insert_entry (path_element->class, dir, ent);
  1073. if ((VFS_CLASS (s)->flags & VFSF_USETMP) != 0)
  1074. {
  1075. int tmp_handle;
  1076. vfs_path_t *tmp_vpath;
  1077. tmp_handle = vfs_mkstemps (&tmp_vpath, path_element->class->name, name);
  1078. ino->localname = g_strdup (vfs_path_as_str (tmp_vpath));
  1079. vfs_path_free (tmp_vpath);
  1080. if (tmp_handle == -1)
  1081. {
  1082. g_free (dirname);
  1083. g_free (name);
  1084. return NULL;
  1085. }
  1086. close (tmp_handle);
  1087. }
  1088. g_free (dirname);
  1089. g_free (name);
  1090. was_changed = TRUE;
  1091. }
  1092. if (S_ISDIR (ino->st.st_mode))
  1093. {
  1094. path_element->class->verrno = EISDIR;
  1095. return NULL;
  1096. }
  1097. fh = s->fh_new != NULL ? s->fh_new (ino, was_changed) : vfs_s_new_fh (ino, was_changed);
  1098. if (IS_LINEAR (flags))
  1099. {
  1100. if (s->linear_start != NULL)
  1101. {
  1102. vfs_print_message ("%s", _("Starting linear transfer..."));
  1103. fh->linear = LS_LINEAR_PREOPEN;
  1104. }
  1105. }
  1106. else
  1107. {
  1108. if (s->fh_open != NULL && s->fh_open (path_element->class, fh, flags, mode) != 0)
  1109. {
  1110. vfs_s_free_fh (s, fh);
  1111. return NULL;
  1112. }
  1113. }
  1114. if ((VFS_CLASS (s)->flags & VFSF_USETMP) != 0 && fh->ino->localname != NULL)
  1115. {
  1116. fh->handle = open (fh->ino->localname, NO_LINEAR (flags), mode);
  1117. if (fh->handle == -1)
  1118. {
  1119. vfs_s_free_fh (s, fh);
  1120. path_element->class->verrno = errno;
  1121. return NULL;
  1122. }
  1123. }
  1124. /* i.e. we had no open files and now we have one */
  1125. vfs_rmstamp (path_element->class, (vfsid) super);
  1126. super->fd_usage++;
  1127. fh->ino->st.st_nlink++;
  1128. return fh;
  1129. }
  1130. /* --------------------------------------------------------------------------------------------- */
  1131. int
  1132. vfs_s_stat (const vfs_path_t * vpath, struct stat *buf)
  1133. {
  1134. return vfs_s_internal_stat (vpath, buf, FL_FOLLOW);
  1135. }
  1136. /* --------------------------------------------------------------------------------------------- */
  1137. int
  1138. vfs_s_lstat (const vfs_path_t * vpath, struct stat *buf)
  1139. {
  1140. return vfs_s_internal_stat (vpath, buf, FL_NONE);
  1141. }
  1142. /* --------------------------------------------------------------------------------------------- */
  1143. int
  1144. vfs_s_fstat (void *fh, struct stat *buf)
  1145. {
  1146. *buf = VFS_FILE_HANDLER (fh)->ino->st;
  1147. return 0;
  1148. }
  1149. /* --------------------------------------------------------------------------------------------- */
  1150. int
  1151. vfs_s_retrieve_file (struct vfs_class *me, struct vfs_s_inode *ino)
  1152. {
  1153. /* If you want reget, you'll have to open file with O_LINEAR */
  1154. off_t total = 0;
  1155. char buffer[8192];
  1156. int handle;
  1157. ssize_t n;
  1158. off_t stat_size = ino->st.st_size;
  1159. vfs_file_handler_t *fh = NULL;
  1160. vfs_path_t *tmp_vpath;
  1161. struct vfs_s_subclass *s = VFS_SUBCLASS (me);
  1162. if ((me->flags & VFSF_USETMP) == 0)
  1163. return (-1);
  1164. handle = vfs_mkstemps (&tmp_vpath, me->name, ino->ent->name);
  1165. ino->localname = g_strdup (vfs_path_as_str (tmp_vpath));
  1166. vfs_path_free (tmp_vpath);
  1167. if (handle == -1)
  1168. {
  1169. me->verrno = errno;
  1170. goto error_4;
  1171. }
  1172. fh = s->fh_new != NULL ? s->fh_new (ino, FALSE) : vfs_s_new_fh (ino, FALSE);
  1173. if (s->linear_start (me, fh, 0) == 0)
  1174. goto error_3;
  1175. /* Clear the interrupt status */
  1176. tty_got_interrupt ();
  1177. tty_enable_interrupt_key ();
  1178. while ((n = s->linear_read (me, fh, buffer, sizeof (buffer))) != 0)
  1179. {
  1180. int t;
  1181. if (n < 0)
  1182. goto error_1;
  1183. total += n;
  1184. vfs_s_print_stats (me->name, _("Getting file"), ino->ent->name, total, stat_size);
  1185. if (tty_got_interrupt ())
  1186. goto error_1;
  1187. t = write (handle, buffer, n);
  1188. if (t != n)
  1189. {
  1190. if (t == -1)
  1191. me->verrno = errno;
  1192. goto error_1;
  1193. }
  1194. }
  1195. s->linear_close (me, fh);
  1196. close (handle);
  1197. tty_disable_interrupt_key ();
  1198. vfs_s_free_fh (s, fh);
  1199. return 0;
  1200. error_1:
  1201. s->linear_close (me, fh);
  1202. error_3:
  1203. tty_disable_interrupt_key ();
  1204. close (handle);
  1205. unlink (ino->localname);
  1206. error_4:
  1207. MC_PTR_FREE (ino->localname);
  1208. if (fh != NULL)
  1209. vfs_s_free_fh (s, fh);
  1210. return (-1);
  1211. }
  1212. /* --------------------------------------------------------------------------------------------- */
  1213. /* ----------------------------- Stamping support -------------------------- */
  1214. /* Initialize one of our subclasses - fill common functions */
  1215. void
  1216. vfs_init_class (struct vfs_class *vclass, const char *name, vfs_flags_t flags, const char *prefix)
  1217. {
  1218. memset (vclass, 0, sizeof (struct vfs_class));
  1219. vclass->name = name;
  1220. vclass->flags = flags;
  1221. vclass->prefix = prefix;
  1222. vclass->fill_names = vfs_s_fill_names;
  1223. vclass->open = vfs_s_open;
  1224. vclass->close = vfs_s_close;
  1225. vclass->read = vfs_s_read;
  1226. if ((vclass->flags & VFSF_READONLY) == 0)
  1227. vclass->write = vfs_s_write;
  1228. vclass->opendir = vfs_s_opendir;
  1229. vclass->readdir = vfs_s_readdir;
  1230. vclass->closedir = vfs_s_closedir;
  1231. vclass->stat = vfs_s_stat;
  1232. vclass->lstat = vfs_s_lstat;
  1233. vclass->fstat = vfs_s_fstat;
  1234. vclass->readlink = vfs_s_readlink;
  1235. vclass->chdir = vfs_s_chdir;
  1236. vclass->ferrno = vfs_s_ferrno;
  1237. vclass->lseek = vfs_s_lseek;
  1238. vclass->getid = vfs_s_getid;
  1239. vclass->nothingisopen = vfs_s_nothingisopen;
  1240. vclass->free = vfs_s_free;
  1241. vclass->setctl = vfs_s_setctl;
  1242. if ((vclass->flags & VFSF_USETMP) != 0)
  1243. {
  1244. vclass->getlocalcopy = vfs_s_getlocalcopy;
  1245. vclass->ungetlocalcopy = vfs_s_ungetlocalcopy;
  1246. }
  1247. }
  1248. /* --------------------------------------------------------------------------------------------- */
  1249. void
  1250. vfs_init_subclass (struct vfs_s_subclass *sub, const char *name, vfs_flags_t flags,
  1251. const char *prefix)
  1252. {
  1253. struct vfs_class *vclass = VFS_CLASS (sub);
  1254. size_t len;
  1255. char *start;
  1256. vfs_init_class (vclass, name, flags, prefix);
  1257. len = sizeof (struct vfs_s_subclass) - sizeof (struct vfs_class);
  1258. start = (char *) sub + sizeof (struct vfs_class);
  1259. memset (start, 0, len);
  1260. if ((vclass->flags & VFSF_USETMP) != 0)
  1261. sub->find_entry = vfs_s_find_entry_linear;
  1262. else if ((vclass->flags & VFSF_REMOTE) != 0)
  1263. sub->find_entry = vfs_s_find_entry_linear;
  1264. else
  1265. sub->find_entry = vfs_s_find_entry_tree;
  1266. sub->dir_uptodate = vfs_s_dir_uptodate;
  1267. }
  1268. /* --------------------------------------------------------------------------------------------- */
  1269. /** Find VFS id for given directory name */
  1270. vfsid
  1271. vfs_getid (const vfs_path_t * vpath)
  1272. {
  1273. const vfs_path_element_t *path_element;
  1274. path_element = vfs_path_get_by_index (vpath, -1);
  1275. if (!vfs_path_element_valid (path_element) || path_element->class->getid == NULL)
  1276. return NULL;
  1277. return (*path_element->class->getid) (vpath);
  1278. }
  1279. /* --------------------------------------------------------------------------------------------- */
  1280. /* ----------- Utility functions for networked filesystems -------------- */
  1281. #ifdef ENABLE_VFS_NET
  1282. int
  1283. vfs_s_select_on_two (int fd1, int fd2)
  1284. {
  1285. fd_set set;
  1286. struct timeval time_out;
  1287. int v;
  1288. int maxfd = MAX (fd1, fd2) + 1;
  1289. time_out.tv_sec = 1;
  1290. time_out.tv_usec = 0;
  1291. FD_ZERO (&set);
  1292. FD_SET (fd1, &set);
  1293. FD_SET (fd2, &set);
  1294. v = select (maxfd, &set, 0, 0, &time_out);
  1295. if (v <= 0)
  1296. return v;
  1297. if (FD_ISSET (fd1, &set))
  1298. return 1;
  1299. if (FD_ISSET (fd2, &set))
  1300. return 2;
  1301. return (-1);
  1302. }
  1303. /* --------------------------------------------------------------------------------------------- */
  1304. int
  1305. vfs_s_get_line (struct vfs_class *me, int sock, char *buf, int buf_len, char term)
  1306. {
  1307. FILE *logfile = me->logfile;
  1308. int i;
  1309. char c;
  1310. for (i = 0; i < buf_len - 1; i++, buf++)
  1311. {
  1312. if (read (sock, buf, sizeof (char)) <= 0)
  1313. return 0;
  1314. if (logfile != NULL)
  1315. {
  1316. size_t ret1;
  1317. int ret2;
  1318. ret1 = fwrite (buf, 1, 1, logfile);
  1319. ret2 = fflush (logfile);
  1320. (void) ret1;
  1321. (void) ret2;
  1322. }
  1323. if (*buf == term)
  1324. {
  1325. *buf = '\0';
  1326. return 1;
  1327. }
  1328. }
  1329. /* Line is too long - terminate buffer and discard the rest of line */
  1330. *buf = '\0';
  1331. while (read (sock, &c, sizeof (c)) > 0)
  1332. {
  1333. if (logfile != NULL)
  1334. {
  1335. size_t ret1;
  1336. int ret2;
  1337. ret1 = fwrite (&c, 1, 1, logfile);
  1338. ret2 = fflush (logfile);
  1339. (void) ret1;
  1340. (void) ret2;
  1341. }
  1342. if (c == '\n')
  1343. return 1;
  1344. }
  1345. return 0;
  1346. }
  1347. /* --------------------------------------------------------------------------------------------- */
  1348. int
  1349. vfs_s_get_line_interruptible (struct vfs_class *me, char *buffer, int size, int fd)
  1350. {
  1351. int i;
  1352. int res = 0;
  1353. (void) me;
  1354. tty_enable_interrupt_key ();
  1355. for (i = 0; i < size - 1; i++)
  1356. {
  1357. ssize_t n;
  1358. n = read (fd, &buffer[i], 1);
  1359. if (n == -1 && errno == EINTR)
  1360. {
  1361. buffer[i] = '\0';
  1362. res = EINTR;
  1363. goto ret;
  1364. }
  1365. if (n == 0)
  1366. {
  1367. buffer[i] = '\0';
  1368. goto ret;
  1369. }
  1370. if (buffer[i] == '\n')
  1371. {
  1372. buffer[i] = '\0';
  1373. res = 1;
  1374. goto ret;
  1375. }
  1376. }
  1377. buffer[size - 1] = '\0';
  1378. ret:
  1379. tty_disable_interrupt_key ();
  1380. return res;
  1381. }
  1382. #endif /* ENABLE_VFS_NET */
  1383. /* --------------------------------------------------------------------------------------------- */
  1384. /**
  1385. * Normalize filenames start position
  1386. */
  1387. void
  1388. vfs_s_normalize_filename_leading_spaces (struct vfs_s_inode *root_inode, size_t final_num_spaces)
  1389. {
  1390. GList *iter;
  1391. for (iter = g_queue_peek_head_link (root_inode->subdir); iter != NULL;
  1392. iter = g_list_next (iter))
  1393. {
  1394. struct vfs_s_entry *entry = VFS_ENTRY (iter->data);
  1395. if ((size_t) entry->ino->data_offset > final_num_spaces)
  1396. {
  1397. char *source_name, *spacer;
  1398. source_name = entry->name;
  1399. spacer = g_strnfill (entry->ino->data_offset - final_num_spaces, ' ');
  1400. entry->name = g_strconcat (spacer, source_name, (char *) NULL);
  1401. g_free (spacer);
  1402. g_free (source_name);
  1403. }
  1404. entry->ino->data_offset = -1;
  1405. }
  1406. }
  1407. /* --------------------------------------------------------------------------------------------- */