direntry.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737
  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. {
  453. if (VFS_SUBCLASS (me)->linear_start (me, file, file->pos) == 0)
  454. return (-1);
  455. }
  456. if (file->linear == LS_LINEAR_CLOSED)
  457. vfs_die ("linear_start() did not set linear_state!");
  458. if (file->linear == LS_LINEAR_OPEN)
  459. return VFS_SUBCLASS (me)->linear_read (me, file, buffer, count);
  460. if (file->handle != -1)
  461. {
  462. ssize_t n;
  463. n = read (file->handle, buffer, count);
  464. if (n < 0)
  465. me->verrno = errno;
  466. return n;
  467. }
  468. vfs_die ("vfs_s_read: This should not happen\n");
  469. return (-1);
  470. }
  471. /* --------------------------------------------------------------------------------------------- */
  472. static ssize_t
  473. vfs_s_write (void *fh, const char *buffer, size_t count)
  474. {
  475. vfs_file_handler_t *file = VFS_FILE_HANDLER (fh);
  476. struct vfs_class *me = VFS_FILE_HANDLER_SUPER (fh)->me;
  477. if (file->linear != LS_NOT_LINEAR)
  478. vfs_die ("no writing to linear files, please");
  479. file->changed = TRUE;
  480. if (file->handle != -1)
  481. {
  482. ssize_t n;
  483. n = write (file->handle, buffer, count);
  484. if (n < 0)
  485. me->verrno = errno;
  486. return n;
  487. }
  488. vfs_die ("vfs_s_write: This should not happen\n");
  489. return 0;
  490. }
  491. /* --------------------------------------------------------------------------------------------- */
  492. static off_t
  493. vfs_s_lseek (void *fh, off_t offset, int whence)
  494. {
  495. vfs_file_handler_t *file = VFS_FILE_HANDLER (fh);
  496. off_t size = file->ino->st.st_size;
  497. if (file->linear == LS_LINEAR_OPEN)
  498. vfs_die ("cannot lseek() after linear_read!");
  499. if (file->handle != -1)
  500. { /* If we have local file opened, we want to work with it */
  501. off_t retval;
  502. retval = lseek (file->handle, offset, whence);
  503. if (retval == -1)
  504. VFS_FILE_HANDLER_SUPER (fh)->me->verrno = errno;
  505. return retval;
  506. }
  507. switch (whence)
  508. {
  509. case SEEK_CUR:
  510. offset += file->pos;
  511. break;
  512. case SEEK_END:
  513. offset += size;
  514. break;
  515. default:
  516. break;
  517. }
  518. if (offset < 0)
  519. file->pos = 0;
  520. else if (offset < size)
  521. file->pos = offset;
  522. else
  523. file->pos = size;
  524. return file->pos;
  525. }
  526. /* --------------------------------------------------------------------------------------------- */
  527. static int
  528. vfs_s_close (void *fh)
  529. {
  530. vfs_file_handler_t *file = VFS_FILE_HANDLER (fh);
  531. struct vfs_s_super *super = VFS_FILE_HANDLER_SUPER (fh);
  532. struct vfs_class *me = super->me;
  533. struct vfs_s_subclass *sub = VFS_SUBCLASS (me);
  534. int res = 0;
  535. if (me == NULL)
  536. return (-1);
  537. super->fd_usage--;
  538. if (super->fd_usage == 0)
  539. vfs_stamp_create (me, VFS_FILE_HANDLER_SUPER (fh));
  540. if (file->linear == LS_LINEAR_OPEN)
  541. sub->linear_close (me, fh);
  542. if (sub->fh_close != NULL)
  543. res = sub->fh_close (me, fh);
  544. if ((me->flags & VFSF_USETMP) != 0 && file->changed && sub->file_store != NULL)
  545. {
  546. char *s;
  547. s = vfs_s_fullpath (me, file->ino);
  548. if (s == NULL)
  549. res = -1;
  550. else
  551. {
  552. res = sub->file_store (me, fh, s, file->ino->localname);
  553. g_free (s);
  554. }
  555. vfs_s_invalidate (me, super);
  556. }
  557. if (file->handle != -1)
  558. {
  559. close (file->handle);
  560. file->handle = -1;
  561. }
  562. vfs_s_free_inode (me, file->ino);
  563. vfs_s_free_fh (sub, fh);
  564. return res;
  565. }
  566. /* --------------------------------------------------------------------------------------------- */
  567. static void
  568. vfs_s_print_stats (const char *fs_name, const char *action,
  569. const char *file_name, off_t have, off_t need)
  570. {
  571. if (need != 0)
  572. vfs_print_message (_("%s: %s: %s %3d%% (%lld) bytes transferred"), fs_name, action,
  573. file_name, (int) ((double) have * 100 / need), (long long) have);
  574. else
  575. vfs_print_message (_("%s: %s: %s %lld bytes transferred"), fs_name, action, file_name,
  576. (long long) have);
  577. }
  578. /* --------------------------------------------------------------------------------------------- */
  579. /* ------------------------------- mc support ---------------------------- */
  580. static void
  581. vfs_s_fill_names (struct vfs_class *me, fill_names_f func)
  582. {
  583. GList *iter;
  584. for (iter = VFS_SUBCLASS (me)->supers; iter != NULL; iter = g_list_next (iter))
  585. {
  586. const struct vfs_s_super *super = (const struct vfs_s_super *) iter->data;
  587. char *name;
  588. name = g_strconcat (super->name, PATH_SEP_STR, me->prefix, VFS_PATH_URL_DELIMITER,
  589. /* super->current_dir->name, */ (char *) NULL);
  590. func (name);
  591. g_free (name);
  592. }
  593. }
  594. /* --------------------------------------------------------------------------------------------- */
  595. static int
  596. vfs_s_ferrno (struct vfs_class *me)
  597. {
  598. return me->verrno;
  599. }
  600. /* --------------------------------------------------------------------------------------------- */
  601. /**
  602. * Get local copy of the given file. We reuse the existing file cache
  603. * for remote filesystems. Archives use standard VFS facilities.
  604. */
  605. static vfs_path_t *
  606. vfs_s_getlocalcopy (const vfs_path_t *vpath)
  607. {
  608. vfs_file_handler_t *fh;
  609. vfs_path_t *local = NULL;
  610. if (vpath == NULL)
  611. return NULL;
  612. fh = vfs_s_open (vpath, O_RDONLY, 0);
  613. if (fh != NULL)
  614. {
  615. const struct vfs_class *me;
  616. me = vfs_path_get_last_path_vfs (vpath);
  617. if ((me->flags & VFSF_USETMP) != 0 && fh->ino != NULL)
  618. local = vfs_path_from_str_flags (fh->ino->localname, VPF_NO_CANON);
  619. vfs_s_close (fh);
  620. }
  621. return local;
  622. }
  623. /* --------------------------------------------------------------------------------------------- */
  624. /**
  625. * Return the local copy. Since we are using our cache, we do nothing -
  626. * the cache will be removed when the archive is closed.
  627. */
  628. static int
  629. vfs_s_ungetlocalcopy (const vfs_path_t *vpath, const vfs_path_t *local, gboolean has_changed)
  630. {
  631. (void) vpath;
  632. (void) local;
  633. (void) has_changed;
  634. return 0;
  635. }
  636. /* --------------------------------------------------------------------------------------------- */
  637. static int
  638. vfs_s_setctl (const vfs_path_t *vpath, int ctlop, void *arg)
  639. {
  640. struct vfs_class *me;
  641. me = VFS_CLASS (vfs_path_get_last_path_vfs (vpath));
  642. switch (ctlop)
  643. {
  644. case VFS_SETCTL_STALE_DATA:
  645. {
  646. struct vfs_s_inode *ino;
  647. ino = vfs_s_inode_from_path (vpath, 0);
  648. if (ino == NULL)
  649. return 0;
  650. if (arg != NULL)
  651. ino->super->want_stale = TRUE;
  652. else
  653. {
  654. ino->super->want_stale = FALSE;
  655. vfs_s_invalidate (me, ino->super);
  656. }
  657. return 1;
  658. }
  659. case VFS_SETCTL_LOGFILE:
  660. me->logfile = fopen ((char *) arg, "w");
  661. return 1;
  662. case VFS_SETCTL_FLUSH:
  663. me->flush = TRUE;
  664. return 1;
  665. default:
  666. return 0;
  667. }
  668. }
  669. /* --------------------------------------------------------------------------------------------- */
  670. /* ----------------------------- Stamping support -------------------------- */
  671. static vfsid
  672. vfs_s_getid (const vfs_path_t *vpath)
  673. {
  674. struct vfs_s_super *archive = NULL;
  675. const char *p;
  676. p = vfs_s_get_path (vpath, &archive, FL_NO_OPEN);
  677. if (p == NULL)
  678. return NULL;
  679. return (vfsid) archive;
  680. }
  681. /* --------------------------------------------------------------------------------------------- */
  682. static gboolean
  683. vfs_s_nothingisopen (vfsid id)
  684. {
  685. return (VFS_SUPER (id)->fd_usage <= 0);
  686. }
  687. /* --------------------------------------------------------------------------------------------- */
  688. static void
  689. vfs_s_free (vfsid id)
  690. {
  691. vfs_s_free_super (VFS_SUPER (id)->me, VFS_SUPER (id));
  692. }
  693. /* --------------------------------------------------------------------------------------------- */
  694. static gboolean
  695. vfs_s_dir_uptodate (struct vfs_class *me, struct vfs_s_inode *ino)
  696. {
  697. gint64 tim;
  698. if (me->flush)
  699. {
  700. me->flush = FALSE;
  701. return 0;
  702. }
  703. tim = g_get_monotonic_time ();
  704. return (tim < ino->timestamp);
  705. }
  706. /* --------------------------------------------------------------------------------------------- */
  707. /*** public functions ****************************************************************************/
  708. /* --------------------------------------------------------------------------------------------- */
  709. struct vfs_s_inode *
  710. vfs_s_new_inode (struct vfs_class *me, struct vfs_s_super *super, struct stat *initstat)
  711. {
  712. struct vfs_s_inode *ino;
  713. ino = g_try_new0 (struct vfs_s_inode, 1);
  714. if (ino == NULL)
  715. return NULL;
  716. if (initstat != NULL)
  717. ino->st = *initstat;
  718. ino->super = super;
  719. ino->subdir = g_queue_new ();
  720. ino->st.st_nlink = 0;
  721. ino->st.st_ino = VFS_SUBCLASS (me)->inode_counter++;
  722. ino->st.st_dev = VFS_SUBCLASS (me)->rdev;
  723. super->ino_usage++;
  724. CALL (init_inode) (me, ino);
  725. return ino;
  726. }
  727. /* --------------------------------------------------------------------------------------------- */
  728. void
  729. vfs_s_free_inode (struct vfs_class *me, struct vfs_s_inode *ino)
  730. {
  731. if (ino == NULL)
  732. vfs_die ("Don't pass NULL to me");
  733. /* ==0 can happen if freshly created entry is deleted */
  734. if (ino->st.st_nlink > 1)
  735. {
  736. ino->st.st_nlink--;
  737. return;
  738. }
  739. while (g_queue_get_length (ino->subdir) != 0)
  740. {
  741. struct vfs_s_entry *entry;
  742. entry = VFS_ENTRY (g_queue_peek_head (ino->subdir));
  743. vfs_s_free_entry (me, entry);
  744. }
  745. g_queue_free (ino->subdir);
  746. ino->subdir = NULL;
  747. CALL (free_inode) (me, ino);
  748. g_free (ino->linkname);
  749. if ((me->flags & VFSF_USETMP) != 0 && ino->localname != NULL)
  750. {
  751. unlink (ino->localname);
  752. g_free (ino->localname);
  753. }
  754. ino->super->ino_usage--;
  755. g_free (ino);
  756. }
  757. /* --------------------------------------------------------------------------------------------- */
  758. struct vfs_s_entry *
  759. vfs_s_new_entry (struct vfs_class *me, const char *name, struct vfs_s_inode *inode)
  760. {
  761. struct vfs_s_entry *entry;
  762. entry = g_new0 (struct vfs_s_entry, 1);
  763. entry->name = g_strdup (name);
  764. entry->ino = inode;
  765. entry->ino->ent = entry;
  766. CALL (init_entry) (me, entry);
  767. return entry;
  768. }
  769. /* --------------------------------------------------------------------------------------------- */
  770. void
  771. vfs_s_free_entry (struct vfs_class *me, struct vfs_s_entry *ent)
  772. {
  773. if (ent->dir != NULL)
  774. g_queue_remove (ent->dir->subdir, ent);
  775. MC_PTR_FREE (ent->name);
  776. if (ent->ino != NULL)
  777. {
  778. ent->ino->ent = NULL;
  779. vfs_s_free_inode (me, ent->ino);
  780. }
  781. g_free (ent);
  782. }
  783. /* --------------------------------------------------------------------------------------------- */
  784. void
  785. vfs_s_insert_entry (struct vfs_class *me, struct vfs_s_inode *dir, struct vfs_s_entry *ent)
  786. {
  787. (void) me;
  788. ent->dir = dir;
  789. ent->ino->st.st_nlink++;
  790. g_queue_push_tail (dir->subdir, ent);
  791. }
  792. /* --------------------------------------------------------------------------------------------- */
  793. int
  794. vfs_s_entry_compare (const void *a, const void *b)
  795. {
  796. const struct vfs_s_entry *e = (const struct vfs_s_entry *) a;
  797. const char *name = (const char *) b;
  798. return strcmp (e->name, name);
  799. }
  800. /* --------------------------------------------------------------------------------------------- */
  801. struct stat *
  802. vfs_s_default_stat (struct vfs_class *me, mode_t mode)
  803. {
  804. static struct stat st;
  805. mode_t myumask;
  806. (void) me;
  807. myumask = umask (022);
  808. umask (myumask);
  809. mode &= ~myumask;
  810. st.st_mode = mode;
  811. st.st_ino = 0;
  812. st.st_dev = 0;
  813. #ifdef HAVE_STRUCT_STAT_ST_RDEV
  814. st.st_rdev = 0;
  815. #endif
  816. st.st_uid = getuid ();
  817. st.st_gid = getgid ();
  818. #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
  819. st.st_blksize = 512;
  820. #endif
  821. st.st_size = 0;
  822. vfs_zero_stat_times (&st);
  823. vfs_adjust_stat (&st);
  824. return &st;
  825. }
  826. /* --------------------------------------------------------------------------------------------- */
  827. /**
  828. * Calculate number of st_blocks using st_size and st_blksize.
  829. * In according to stat(2), st_blocks is the size in 512-byte units.
  830. *
  831. * @param s stat info
  832. */
  833. void
  834. vfs_adjust_stat (struct stat *s)
  835. {
  836. #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
  837. if (s->st_size == 0)
  838. s->st_blocks = 0;
  839. else
  840. {
  841. #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
  842. blkcnt_t ioblocks;
  843. blksize_t ioblock_size;
  844. /* 1. Calculate how many IO blocks are occupied */
  845. ioblocks = 1 + (s->st_size - 1) / s->st_blksize;
  846. /* 2. Calculate size of st_blksize in 512-byte units */
  847. ioblock_size = 1 + (s->st_blksize - 1) / 512;
  848. /* 3. Calculate number of blocks */
  849. s->st_blocks = ioblocks * ioblock_size;
  850. #else
  851. /* Let IO block size is 512 bytes */
  852. s->st_blocks = 1 + (s->st_size - 1) / 512;
  853. #endif /* HAVE_STRUCT_STAT_ST_BLKSIZE */
  854. }
  855. #endif /* HAVE_STRUCT_STAT_ST_BLOCKS */
  856. }
  857. /* --------------------------------------------------------------------------------------------- */
  858. struct vfs_s_entry *
  859. vfs_s_generate_entry (struct vfs_class *me, const char *name, struct vfs_s_inode *parent,
  860. mode_t mode)
  861. {
  862. struct vfs_s_inode *inode;
  863. struct stat *st;
  864. st = vfs_s_default_stat (me, mode);
  865. inode = vfs_s_new_inode (me, parent->super, st);
  866. return vfs_s_new_entry (me, name, inode);
  867. }
  868. /* --------------------------------------------------------------------------------------------- */
  869. struct vfs_s_inode *
  870. vfs_s_find_inode (struct vfs_class *me, const struct vfs_s_super *super,
  871. const char *path, int follow, int flags)
  872. {
  873. struct vfs_s_entry *ent;
  874. if (((me->flags & VFSF_REMOTE) == 0) && (*path == '\0'))
  875. return super->root;
  876. ent = VFS_SUBCLASS (me)->find_entry (me, super->root, path, follow, flags);
  877. return (ent != NULL ? ent->ino : NULL);
  878. }
  879. /* --------------------------------------------------------------------------------------------- */
  880. /* Ook, these were functions around directory entries / inodes */
  881. /* -------------------------------- superblock games -------------------------- */
  882. /**
  883. * get superlock object by vpath
  884. *
  885. * @param vpath path
  886. * @return superlock object or NULL if not found
  887. */
  888. struct vfs_s_super *
  889. vfs_get_super_by_vpath (const vfs_path_t *vpath)
  890. {
  891. GList *iter;
  892. void *cookie = NULL;
  893. const vfs_path_element_t *path_element;
  894. struct vfs_s_subclass *subclass;
  895. struct vfs_s_super *super = NULL;
  896. vfs_path_t *vpath_archive;
  897. path_element = vfs_path_get_by_index (vpath, -1);
  898. subclass = VFS_SUBCLASS (path_element->class);
  899. vpath_archive = vfs_path_clone (vpath);
  900. vfs_path_remove_element_by_index (vpath_archive, -1);
  901. if (subclass->archive_check != NULL)
  902. {
  903. cookie = subclass->archive_check (vpath_archive);
  904. if (cookie == NULL)
  905. goto ret;
  906. }
  907. if (subclass->archive_same == NULL)
  908. goto ret;
  909. for (iter = subclass->supers; iter != NULL; iter = g_list_next (iter))
  910. {
  911. int i;
  912. super = VFS_SUPER (iter->data);
  913. /* 0 == other, 1 == same, return it, 2 == other but stop scanning */
  914. i = subclass->archive_same (path_element, super, vpath_archive, cookie);
  915. if (i == 1)
  916. goto ret;
  917. if (i != 0)
  918. break;
  919. super = NULL;
  920. }
  921. ret:
  922. vfs_path_free (vpath_archive, TRUE);
  923. return super;
  924. }
  925. /* --------------------------------------------------------------------------------------------- */
  926. /**
  927. * get path from last VFS-element and create corresponding superblock
  928. *
  929. * @param vpath source path object
  930. * @param archive pointer to object for store newly created superblock
  931. * @param flags flags
  932. *
  933. * @return path from last VFS-element
  934. */
  935. const char *
  936. vfs_s_get_path (const vfs_path_t *vpath, struct vfs_s_super **archive, int flags)
  937. {
  938. const char *retval = "";
  939. int result = -1;
  940. struct vfs_s_super *super;
  941. const vfs_path_element_t *path_element;
  942. struct vfs_s_subclass *subclass;
  943. path_element = vfs_path_get_by_index (vpath, -1);
  944. if (path_element->path != NULL)
  945. retval = path_element->path;
  946. super = vfs_get_super_by_vpath (vpath);
  947. if (super != NULL)
  948. goto return_success;
  949. if ((flags & FL_NO_OPEN) != 0)
  950. {
  951. path_element->class->verrno = EIO;
  952. return NULL;
  953. }
  954. subclass = VFS_SUBCLASS (path_element->class);
  955. super = subclass->new_archive != NULL ?
  956. subclass->new_archive (path_element->class) : vfs_s_new_super (path_element->class);
  957. if (subclass->open_archive != NULL)
  958. {
  959. vfs_path_t *vpath_archive;
  960. vpath_archive = vfs_path_clone (vpath);
  961. vfs_path_remove_element_by_index (vpath_archive, -1);
  962. result = subclass->open_archive (super, vpath_archive, path_element);
  963. vfs_path_free (vpath_archive, TRUE);
  964. }
  965. if (result == -1)
  966. {
  967. vfs_s_free_super (path_element->class, super);
  968. path_element->class->verrno = EIO;
  969. return NULL;
  970. }
  971. if (super->name == NULL)
  972. vfs_die ("You have to fill name\n");
  973. if (super->root == NULL)
  974. vfs_die ("You have to fill root inode\n");
  975. vfs_s_insert_super (path_element->class, super);
  976. vfs_stamp_create (path_element->class, super);
  977. return_success:
  978. *archive = super;
  979. return retval;
  980. }
  981. /* --------------------------------------------------------------------------------------------- */
  982. void
  983. vfs_s_invalidate (struct vfs_class *me, struct vfs_s_super *super)
  984. {
  985. if (!super->want_stale)
  986. {
  987. vfs_s_free_inode (me, super->root);
  988. super->root = vfs_s_new_inode (me, super, vfs_s_default_stat (me, S_IFDIR | 0755));
  989. }
  990. }
  991. /* --------------------------------------------------------------------------------------------- */
  992. char *
  993. vfs_s_fullpath (struct vfs_class *me, struct vfs_s_inode *ino)
  994. {
  995. if (ino->ent == NULL)
  996. ERRNOR (EAGAIN, NULL);
  997. if ((me->flags & VFSF_USETMP) == 0)
  998. {
  999. /* archives */
  1000. char *path;
  1001. path = g_strdup (ino->ent->name);
  1002. while (TRUE)
  1003. {
  1004. char *newpath;
  1005. ino = ino->ent->dir;
  1006. if (ino == ino->super->root)
  1007. break;
  1008. newpath = g_strconcat (ino->ent->name, PATH_SEP_STR, path, (char *) NULL);
  1009. g_free (path);
  1010. path = newpath;
  1011. }
  1012. return path;
  1013. }
  1014. /* remote systems */
  1015. if (ino->ent->dir == NULL || ino->ent->dir->ent == NULL)
  1016. return g_strdup (ino->ent->name);
  1017. return g_strconcat (ino->ent->dir->ent->name, PATH_SEP_STR, ino->ent->name, (char *) NULL);
  1018. }
  1019. /* --------------------------------------------------------------------------------------------- */
  1020. void
  1021. vfs_s_init_fh (vfs_file_handler_t *fh, struct vfs_s_inode *ino, gboolean changed)
  1022. {
  1023. fh->ino = ino;
  1024. fh->handle = -1;
  1025. fh->changed = changed;
  1026. fh->linear = LS_NOT_LINEAR;
  1027. }
  1028. /* --------------------------------------------------------------------------------------------- */
  1029. /* --------------------------- stat and friends ---------------------------- */
  1030. void *
  1031. vfs_s_open (const vfs_path_t *vpath, int flags, mode_t mode)
  1032. {
  1033. gboolean was_changed = FALSE;
  1034. vfs_file_handler_t *fh;
  1035. struct vfs_s_super *super;
  1036. const char *q;
  1037. struct vfs_s_inode *ino;
  1038. struct vfs_class *me;
  1039. struct vfs_s_subclass *s;
  1040. q = vfs_s_get_path (vpath, &super, 0);
  1041. if (q == NULL)
  1042. return NULL;
  1043. me = VFS_CLASS (vfs_path_get_last_path_vfs (vpath));
  1044. ino = vfs_s_find_inode (me, super, q, LINK_FOLLOW, FL_NONE);
  1045. if (ino != NULL && (flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
  1046. {
  1047. me->verrno = EEXIST;
  1048. return NULL;
  1049. }
  1050. s = VFS_SUBCLASS (me);
  1051. if (ino == NULL)
  1052. {
  1053. char *name;
  1054. struct vfs_s_entry *ent;
  1055. struct vfs_s_inode *dir;
  1056. /* If the filesystem is read-only, disable file creation */
  1057. if ((flags & O_CREAT) == 0 || me->write == NULL)
  1058. return NULL;
  1059. name = g_path_get_dirname (q);
  1060. dir = vfs_s_find_inode (me, super, name, LINK_FOLLOW, FL_DIR);
  1061. g_free (name);
  1062. if (dir == NULL)
  1063. return NULL;
  1064. name = g_path_get_basename (q);
  1065. ent = vfs_s_generate_entry (me, name, dir, 0755);
  1066. ino = ent->ino;
  1067. vfs_s_insert_entry (me, dir, ent);
  1068. if ((VFS_CLASS (s)->flags & VFSF_USETMP) != 0)
  1069. {
  1070. int tmp_handle;
  1071. vfs_path_t *tmp_vpath;
  1072. tmp_handle = vfs_mkstemps (&tmp_vpath, me->name, name);
  1073. ino->localname = vfs_path_free (tmp_vpath, FALSE);
  1074. if (tmp_handle == -1)
  1075. {
  1076. g_free (name);
  1077. return NULL;
  1078. }
  1079. close (tmp_handle);
  1080. }
  1081. g_free (name);
  1082. was_changed = TRUE;
  1083. }
  1084. if (S_ISDIR (ino->st.st_mode))
  1085. {
  1086. me->verrno = EISDIR;
  1087. return NULL;
  1088. }
  1089. fh = s->fh_new != NULL ? s->fh_new (ino, was_changed) : vfs_s_new_fh (ino, was_changed);
  1090. if (IS_LINEAR (flags))
  1091. {
  1092. if (s->linear_start != NULL)
  1093. {
  1094. vfs_print_message ("%s", _("Starting linear transfer..."));
  1095. fh->linear = LS_LINEAR_PREOPEN;
  1096. }
  1097. }
  1098. else
  1099. {
  1100. if (s->fh_open != NULL && s->fh_open (me, fh, flags, mode) != 0)
  1101. {
  1102. vfs_s_free_fh (s, fh);
  1103. return NULL;
  1104. }
  1105. }
  1106. if ((VFS_CLASS (s)->flags & VFSF_USETMP) != 0 && fh->ino->localname != NULL)
  1107. {
  1108. fh->handle = open (fh->ino->localname, NO_LINEAR (flags), mode);
  1109. if (fh->handle == -1)
  1110. {
  1111. vfs_s_free_fh (s, fh);
  1112. me->verrno = errno;
  1113. return NULL;
  1114. }
  1115. }
  1116. /* i.e. we had no open files and now we have one */
  1117. vfs_rmstamp (me, (vfsid) super);
  1118. super->fd_usage++;
  1119. fh->ino->st.st_nlink++;
  1120. return fh;
  1121. }
  1122. /* --------------------------------------------------------------------------------------------- */
  1123. int
  1124. vfs_s_stat (const vfs_path_t *vpath, struct stat *buf)
  1125. {
  1126. return vfs_s_internal_stat (vpath, buf, FL_FOLLOW);
  1127. }
  1128. /* --------------------------------------------------------------------------------------------- */
  1129. int
  1130. vfs_s_lstat (const vfs_path_t *vpath, struct stat *buf)
  1131. {
  1132. return vfs_s_internal_stat (vpath, buf, FL_NONE);
  1133. }
  1134. /* --------------------------------------------------------------------------------------------- */
  1135. int
  1136. vfs_s_fstat (void *fh, struct stat *buf)
  1137. {
  1138. *buf = VFS_FILE_HANDLER (fh)->ino->st;
  1139. return 0;
  1140. }
  1141. /* --------------------------------------------------------------------------------------------- */
  1142. int
  1143. vfs_s_retrieve_file (struct vfs_class *me, struct vfs_s_inode *ino)
  1144. {
  1145. /* If you want reget, you'll have to open file with O_LINEAR */
  1146. off_t total = 0;
  1147. char buffer[BUF_8K];
  1148. int handle;
  1149. ssize_t n;
  1150. off_t stat_size = ino->st.st_size;
  1151. vfs_file_handler_t *fh = NULL;
  1152. vfs_path_t *tmp_vpath;
  1153. struct vfs_s_subclass *s = VFS_SUBCLASS (me);
  1154. if ((me->flags & VFSF_USETMP) == 0)
  1155. return (-1);
  1156. handle = vfs_mkstemps (&tmp_vpath, me->name, ino->ent->name);
  1157. ino->localname = vfs_path_free (tmp_vpath, FALSE);
  1158. if (handle == -1)
  1159. {
  1160. me->verrno = errno;
  1161. goto error_4;
  1162. }
  1163. fh = s->fh_new != NULL ? s->fh_new (ino, FALSE) : vfs_s_new_fh (ino, FALSE);
  1164. if (s->linear_start (me, fh, 0) == 0)
  1165. goto error_3;
  1166. /* Clear the interrupt status */
  1167. tty_got_interrupt ();
  1168. tty_enable_interrupt_key ();
  1169. while ((n = s->linear_read (me, fh, buffer, sizeof (buffer))) != 0)
  1170. {
  1171. int t;
  1172. if (n < 0)
  1173. goto error_1;
  1174. total += n;
  1175. vfs_s_print_stats (me->name, _("Getting file"), ino->ent->name, total, stat_size);
  1176. if (tty_got_interrupt ())
  1177. goto error_1;
  1178. t = write (handle, buffer, n);
  1179. if (t != n)
  1180. {
  1181. if (t == -1)
  1182. me->verrno = errno;
  1183. goto error_1;
  1184. }
  1185. }
  1186. s->linear_close (me, fh);
  1187. close (handle);
  1188. tty_disable_interrupt_key ();
  1189. vfs_s_free_fh (s, fh);
  1190. return 0;
  1191. error_1:
  1192. s->linear_close (me, fh);
  1193. error_3:
  1194. tty_disable_interrupt_key ();
  1195. close (handle);
  1196. unlink (ino->localname);
  1197. error_4:
  1198. MC_PTR_FREE (ino->localname);
  1199. if (fh != NULL)
  1200. vfs_s_free_fh (s, fh);
  1201. return (-1);
  1202. }
  1203. /* --------------------------------------------------------------------------------------------- */
  1204. /* ----------------------------- Stamping support -------------------------- */
  1205. /* Initialize one of our subclasses - fill common functions */
  1206. void
  1207. vfs_init_class (struct vfs_class *vclass, const char *name, vfs_flags_t flags, const char *prefix)
  1208. {
  1209. memset (vclass, 0, sizeof (struct vfs_class));
  1210. vclass->name = name;
  1211. vclass->flags = flags;
  1212. vclass->prefix = prefix;
  1213. vclass->fill_names = vfs_s_fill_names;
  1214. vclass->open = vfs_s_open;
  1215. vclass->close = vfs_s_close;
  1216. vclass->read = vfs_s_read;
  1217. if ((vclass->flags & VFSF_READONLY) == 0)
  1218. vclass->write = vfs_s_write;
  1219. vclass->opendir = vfs_s_opendir;
  1220. vclass->readdir = vfs_s_readdir;
  1221. vclass->closedir = vfs_s_closedir;
  1222. vclass->stat = vfs_s_stat;
  1223. vclass->lstat = vfs_s_lstat;
  1224. vclass->fstat = vfs_s_fstat;
  1225. vclass->readlink = vfs_s_readlink;
  1226. vclass->chdir = vfs_s_chdir;
  1227. vclass->ferrno = vfs_s_ferrno;
  1228. vclass->lseek = vfs_s_lseek;
  1229. vclass->getid = vfs_s_getid;
  1230. vclass->nothingisopen = vfs_s_nothingisopen;
  1231. vclass->free = vfs_s_free;
  1232. vclass->setctl = vfs_s_setctl;
  1233. if ((vclass->flags & VFSF_USETMP) != 0)
  1234. {
  1235. vclass->getlocalcopy = vfs_s_getlocalcopy;
  1236. vclass->ungetlocalcopy = vfs_s_ungetlocalcopy;
  1237. }
  1238. }
  1239. /* --------------------------------------------------------------------------------------------- */
  1240. void
  1241. vfs_init_subclass (struct vfs_s_subclass *sub, const char *name, vfs_flags_t flags,
  1242. const char *prefix)
  1243. {
  1244. struct vfs_class *vclass = VFS_CLASS (sub);
  1245. size_t len;
  1246. char *start;
  1247. vfs_init_class (vclass, name, flags, prefix);
  1248. len = sizeof (struct vfs_s_subclass) - sizeof (struct vfs_class);
  1249. start = (char *) sub + sizeof (struct vfs_class);
  1250. memset (start, 0, len);
  1251. if ((vclass->flags & VFSF_USETMP) != 0)
  1252. sub->find_entry = vfs_s_find_entry_linear;
  1253. else if ((vclass->flags & VFSF_REMOTE) != 0)
  1254. sub->find_entry = vfs_s_find_entry_linear;
  1255. else
  1256. sub->find_entry = vfs_s_find_entry_tree;
  1257. sub->dir_uptodate = vfs_s_dir_uptodate;
  1258. }
  1259. /* --------------------------------------------------------------------------------------------- */
  1260. /** Find VFS id for given directory name */
  1261. vfsid
  1262. vfs_getid (const vfs_path_t *vpath)
  1263. {
  1264. const struct vfs_class *me;
  1265. me = vfs_path_get_last_path_vfs (vpath);
  1266. if (me == NULL || me->getid == NULL)
  1267. return NULL;
  1268. return me->getid (vpath);
  1269. }
  1270. /* --------------------------------------------------------------------------------------------- */
  1271. /* ----------- Utility functions for networked filesystems -------------- */
  1272. #ifdef ENABLE_VFS_NET
  1273. int
  1274. vfs_s_select_on_two (int fd1, int fd2)
  1275. {
  1276. fd_set set;
  1277. struct timeval time_out;
  1278. int v;
  1279. int maxfd = MAX (fd1, fd2) + 1;
  1280. time_out.tv_sec = 1;
  1281. time_out.tv_usec = 0;
  1282. FD_ZERO (&set);
  1283. FD_SET (fd1, &set);
  1284. FD_SET (fd2, &set);
  1285. v = select (maxfd, &set, 0, 0, &time_out);
  1286. if (v <= 0)
  1287. return v;
  1288. if (FD_ISSET (fd1, &set))
  1289. return 1;
  1290. if (FD_ISSET (fd2, &set))
  1291. return 2;
  1292. return (-1);
  1293. }
  1294. /* --------------------------------------------------------------------------------------------- */
  1295. int
  1296. vfs_s_get_line (struct vfs_class *me, int sock, char *buf, int buf_len, char term)
  1297. {
  1298. FILE *logfile = me->logfile;
  1299. int i;
  1300. char c;
  1301. for (i = 0; i < buf_len - 1; i++, buf++)
  1302. {
  1303. if (read (sock, buf, sizeof (char)) <= 0)
  1304. return 0;
  1305. if (logfile != NULL)
  1306. {
  1307. size_t ret1;
  1308. int ret2;
  1309. ret1 = fwrite (buf, 1, 1, logfile);
  1310. ret2 = fflush (logfile);
  1311. (void) ret1;
  1312. (void) ret2;
  1313. }
  1314. if (*buf == term)
  1315. {
  1316. *buf = '\0';
  1317. return 1;
  1318. }
  1319. }
  1320. /* Line is too long - terminate buffer and discard the rest of line */
  1321. *buf = '\0';
  1322. while (read (sock, &c, sizeof (c)) > 0)
  1323. {
  1324. if (logfile != NULL)
  1325. {
  1326. size_t ret1;
  1327. int ret2;
  1328. ret1 = fwrite (&c, 1, 1, logfile);
  1329. ret2 = fflush (logfile);
  1330. (void) ret1;
  1331. (void) ret2;
  1332. }
  1333. if (c == '\n')
  1334. return 1;
  1335. }
  1336. return 0;
  1337. }
  1338. /* --------------------------------------------------------------------------------------------- */
  1339. int
  1340. vfs_s_get_line_interruptible (struct vfs_class *me, char *buffer, int size, int fd)
  1341. {
  1342. int i;
  1343. int res = 0;
  1344. (void) me;
  1345. tty_enable_interrupt_key ();
  1346. for (i = 0; i < size - 1; i++)
  1347. {
  1348. ssize_t n;
  1349. n = read (fd, &buffer[i], 1);
  1350. if (n == -1 && errno == EINTR)
  1351. {
  1352. buffer[i] = '\0';
  1353. res = EINTR;
  1354. goto ret;
  1355. }
  1356. if (n == 0)
  1357. {
  1358. buffer[i] = '\0';
  1359. goto ret;
  1360. }
  1361. if (buffer[i] == '\n')
  1362. {
  1363. buffer[i] = '\0';
  1364. res = 1;
  1365. goto ret;
  1366. }
  1367. }
  1368. buffer[size - 1] = '\0';
  1369. ret:
  1370. tty_disable_interrupt_key ();
  1371. return res;
  1372. }
  1373. #endif /* ENABLE_VFS_NET */
  1374. /* --------------------------------------------------------------------------------------------- */
  1375. /**
  1376. * Normalize filenames start position
  1377. */
  1378. void
  1379. vfs_s_normalize_filename_leading_spaces (struct vfs_s_inode *root_inode, size_t final_num_spaces)
  1380. {
  1381. GList *iter;
  1382. for (iter = g_queue_peek_head_link (root_inode->subdir); iter != NULL;
  1383. iter = g_list_next (iter))
  1384. {
  1385. struct vfs_s_entry *entry = VFS_ENTRY (iter->data);
  1386. if ((size_t) entry->leading_spaces > final_num_spaces)
  1387. {
  1388. char *source_name, *spacer;
  1389. source_name = entry->name;
  1390. spacer = g_strnfill ((size_t) entry->leading_spaces - final_num_spaces, ' ');
  1391. entry->name = g_strconcat (spacer, source_name, (char *) NULL);
  1392. g_free (spacer);
  1393. g_free (source_name);
  1394. }
  1395. entry->leading_spaces = -1;
  1396. }
  1397. }
  1398. /* --------------------------------------------------------------------------------------------- */