direntry.c 47 KB

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