direntry.c 46 KB

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