direntry.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740
  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. st.st_mtime = st.st_atime = st.st_ctime = time (NULL);
  823. #ifdef HAVE_STRUCT_STAT_ST_MTIM
  824. st.st_atim.tv_nsec = st.st_mtim.tv_nsec = st.st_ctim.tv_nsec = 0;
  825. #endif
  826. vfs_adjust_stat (&st);
  827. return &st;
  828. }
  829. /* --------------------------------------------------------------------------------------------- */
  830. /**
  831. * Calculate number of st_blocks using st_size and st_blksize.
  832. * In according to stat(2), st_blocks is the size in 512-byte units.
  833. *
  834. * @param s stat info
  835. */
  836. void
  837. vfs_adjust_stat (struct stat *s)
  838. {
  839. #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
  840. if (s->st_size == 0)
  841. s->st_blocks = 0;
  842. else
  843. {
  844. #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
  845. blkcnt_t ioblocks;
  846. blksize_t ioblock_size;
  847. /* 1. Calculate how many IO blocks are occupied */
  848. ioblocks = 1 + (s->st_size - 1) / s->st_blksize;
  849. /* 2. Calculate size of st_blksize in 512-byte units */
  850. ioblock_size = 1 + (s->st_blksize - 1) / 512;
  851. /* 3. Calculate number of blocks */
  852. s->st_blocks = ioblocks * ioblock_size;
  853. #else
  854. /* Let IO block size is 512 bytes */
  855. s->st_blocks = 1 + (s->st_size - 1) / 512;
  856. #endif /* HAVE_STRUCT_STAT_ST_BLKSIZE */
  857. }
  858. #endif /* HAVE_STRUCT_STAT_ST_BLOCKS */
  859. }
  860. /* --------------------------------------------------------------------------------------------- */
  861. struct vfs_s_entry *
  862. vfs_s_generate_entry (struct vfs_class *me, const char *name, struct vfs_s_inode *parent,
  863. mode_t mode)
  864. {
  865. struct vfs_s_inode *inode;
  866. struct stat *st;
  867. st = vfs_s_default_stat (me, mode);
  868. inode = vfs_s_new_inode (me, parent->super, st);
  869. return vfs_s_new_entry (me, name, inode);
  870. }
  871. /* --------------------------------------------------------------------------------------------- */
  872. struct vfs_s_inode *
  873. vfs_s_find_inode (struct vfs_class *me, const struct vfs_s_super *super,
  874. const char *path, int follow, int flags)
  875. {
  876. struct vfs_s_entry *ent;
  877. if (((me->flags & VFSF_REMOTE) == 0) && (*path == '\0'))
  878. return super->root;
  879. ent = VFS_SUBCLASS (me)->find_entry (me, super->root, path, follow, flags);
  880. return (ent != NULL ? ent->ino : NULL);
  881. }
  882. /* --------------------------------------------------------------------------------------------- */
  883. /* Ook, these were functions around directory entries / inodes */
  884. /* -------------------------------- superblock games -------------------------- */
  885. /**
  886. * get superlock object by vpath
  887. *
  888. * @param vpath path
  889. * @return superlock object or NULL if not found
  890. */
  891. struct vfs_s_super *
  892. vfs_get_super_by_vpath (const vfs_path_t *vpath)
  893. {
  894. GList *iter;
  895. void *cookie = NULL;
  896. const vfs_path_element_t *path_element;
  897. struct vfs_s_subclass *subclass;
  898. struct vfs_s_super *super = NULL;
  899. vfs_path_t *vpath_archive;
  900. path_element = vfs_path_get_by_index (vpath, -1);
  901. subclass = VFS_SUBCLASS (path_element->class);
  902. vpath_archive = vfs_path_clone (vpath);
  903. vfs_path_remove_element_by_index (vpath_archive, -1);
  904. if (subclass->archive_check != NULL)
  905. {
  906. cookie = subclass->archive_check (vpath_archive);
  907. if (cookie == NULL)
  908. goto ret;
  909. }
  910. if (subclass->archive_same == NULL)
  911. goto ret;
  912. for (iter = subclass->supers; iter != NULL; iter = g_list_next (iter))
  913. {
  914. int i;
  915. super = VFS_SUPER (iter->data);
  916. /* 0 == other, 1 == same, return it, 2 == other but stop scanning */
  917. i = subclass->archive_same (path_element, super, vpath_archive, cookie);
  918. if (i == 1)
  919. goto ret;
  920. if (i != 0)
  921. break;
  922. super = NULL;
  923. }
  924. ret:
  925. vfs_path_free (vpath_archive, TRUE);
  926. return super;
  927. }
  928. /* --------------------------------------------------------------------------------------------- */
  929. /**
  930. * get path from last VFS-element and create corresponding superblock
  931. *
  932. * @param vpath source path object
  933. * @param archive pointer to object for store newly created superblock
  934. * @param flags flags
  935. *
  936. * @return path from last VFS-element
  937. */
  938. const char *
  939. vfs_s_get_path (const vfs_path_t *vpath, struct vfs_s_super **archive, int flags)
  940. {
  941. const char *retval = "";
  942. int result = -1;
  943. struct vfs_s_super *super;
  944. const vfs_path_element_t *path_element;
  945. struct vfs_s_subclass *subclass;
  946. path_element = vfs_path_get_by_index (vpath, -1);
  947. if (path_element->path != NULL)
  948. retval = path_element->path;
  949. super = vfs_get_super_by_vpath (vpath);
  950. if (super != NULL)
  951. goto return_success;
  952. if ((flags & FL_NO_OPEN) != 0)
  953. {
  954. path_element->class->verrno = EIO;
  955. return NULL;
  956. }
  957. subclass = VFS_SUBCLASS (path_element->class);
  958. super = subclass->new_archive != NULL ?
  959. subclass->new_archive (path_element->class) : vfs_s_new_super (path_element->class);
  960. if (subclass->open_archive != NULL)
  961. {
  962. vfs_path_t *vpath_archive;
  963. vpath_archive = vfs_path_clone (vpath);
  964. vfs_path_remove_element_by_index (vpath_archive, -1);
  965. result = subclass->open_archive (super, vpath_archive, path_element);
  966. vfs_path_free (vpath_archive, TRUE);
  967. }
  968. if (result == -1)
  969. {
  970. vfs_s_free_super (path_element->class, super);
  971. path_element->class->verrno = EIO;
  972. return NULL;
  973. }
  974. if (super->name == NULL)
  975. vfs_die ("You have to fill name\n");
  976. if (super->root == NULL)
  977. vfs_die ("You have to fill root inode\n");
  978. vfs_s_insert_super (path_element->class, super);
  979. vfs_stamp_create (path_element->class, super);
  980. return_success:
  981. *archive = super;
  982. return retval;
  983. }
  984. /* --------------------------------------------------------------------------------------------- */
  985. void
  986. vfs_s_invalidate (struct vfs_class *me, struct vfs_s_super *super)
  987. {
  988. if (!super->want_stale)
  989. {
  990. vfs_s_free_inode (me, super->root);
  991. super->root = vfs_s_new_inode (me, super, vfs_s_default_stat (me, S_IFDIR | 0755));
  992. }
  993. }
  994. /* --------------------------------------------------------------------------------------------- */
  995. char *
  996. vfs_s_fullpath (struct vfs_class *me, struct vfs_s_inode *ino)
  997. {
  998. if (ino->ent == NULL)
  999. ERRNOR (EAGAIN, NULL);
  1000. if ((me->flags & VFSF_USETMP) == 0)
  1001. {
  1002. /* archives */
  1003. char *path;
  1004. path = g_strdup (ino->ent->name);
  1005. while (TRUE)
  1006. {
  1007. char *newpath;
  1008. ino = ino->ent->dir;
  1009. if (ino == ino->super->root)
  1010. break;
  1011. newpath = g_strconcat (ino->ent->name, PATH_SEP_STR, path, (char *) NULL);
  1012. g_free (path);
  1013. path = newpath;
  1014. }
  1015. return path;
  1016. }
  1017. /* remote systems */
  1018. if (ino->ent->dir == NULL || ino->ent->dir->ent == NULL)
  1019. return g_strdup (ino->ent->name);
  1020. return g_strconcat (ino->ent->dir->ent->name, PATH_SEP_STR, ino->ent->name, (char *) NULL);
  1021. }
  1022. /* --------------------------------------------------------------------------------------------- */
  1023. void
  1024. vfs_s_init_fh (vfs_file_handler_t *fh, struct vfs_s_inode *ino, gboolean changed)
  1025. {
  1026. fh->ino = ino;
  1027. fh->handle = -1;
  1028. fh->changed = changed;
  1029. fh->linear = LS_NOT_LINEAR;
  1030. }
  1031. /* --------------------------------------------------------------------------------------------- */
  1032. /* --------------------------- stat and friends ---------------------------- */
  1033. void *
  1034. vfs_s_open (const vfs_path_t *vpath, int flags, mode_t mode)
  1035. {
  1036. gboolean was_changed = FALSE;
  1037. vfs_file_handler_t *fh;
  1038. struct vfs_s_super *super;
  1039. const char *q;
  1040. struct vfs_s_inode *ino;
  1041. struct vfs_class *me;
  1042. struct vfs_s_subclass *s;
  1043. q = vfs_s_get_path (vpath, &super, 0);
  1044. if (q == NULL)
  1045. return NULL;
  1046. me = VFS_CLASS (vfs_path_get_last_path_vfs (vpath));
  1047. ino = vfs_s_find_inode (me, super, q, LINK_FOLLOW, FL_NONE);
  1048. if (ino != NULL && (flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
  1049. {
  1050. me->verrno = EEXIST;
  1051. return NULL;
  1052. }
  1053. s = VFS_SUBCLASS (me);
  1054. if (ino == NULL)
  1055. {
  1056. char *name;
  1057. struct vfs_s_entry *ent;
  1058. struct vfs_s_inode *dir;
  1059. /* If the filesystem is read-only, disable file creation */
  1060. if ((flags & O_CREAT) == 0 || me->write == NULL)
  1061. return NULL;
  1062. name = g_path_get_dirname (q);
  1063. dir = vfs_s_find_inode (me, super, name, LINK_FOLLOW, FL_DIR);
  1064. g_free (name);
  1065. if (dir == NULL)
  1066. return NULL;
  1067. name = g_path_get_basename (q);
  1068. ent = vfs_s_generate_entry (me, name, dir, 0755);
  1069. ino = ent->ino;
  1070. vfs_s_insert_entry (me, dir, ent);
  1071. if ((VFS_CLASS (s)->flags & VFSF_USETMP) != 0)
  1072. {
  1073. int tmp_handle;
  1074. vfs_path_t *tmp_vpath;
  1075. tmp_handle = vfs_mkstemps (&tmp_vpath, me->name, name);
  1076. ino->localname = vfs_path_free (tmp_vpath, FALSE);
  1077. if (tmp_handle == -1)
  1078. {
  1079. g_free (name);
  1080. return NULL;
  1081. }
  1082. close (tmp_handle);
  1083. }
  1084. g_free (name);
  1085. was_changed = TRUE;
  1086. }
  1087. if (S_ISDIR (ino->st.st_mode))
  1088. {
  1089. me->verrno = EISDIR;
  1090. return NULL;
  1091. }
  1092. fh = s->fh_new != NULL ? s->fh_new (ino, was_changed) : vfs_s_new_fh (ino, was_changed);
  1093. if (IS_LINEAR (flags))
  1094. {
  1095. if (s->linear_start != NULL)
  1096. {
  1097. vfs_print_message ("%s", _("Starting linear transfer..."));
  1098. fh->linear = LS_LINEAR_PREOPEN;
  1099. }
  1100. }
  1101. else
  1102. {
  1103. if (s->fh_open != NULL && s->fh_open (me, fh, flags, mode) != 0)
  1104. {
  1105. vfs_s_free_fh (s, fh);
  1106. return NULL;
  1107. }
  1108. }
  1109. if ((VFS_CLASS (s)->flags & VFSF_USETMP) != 0 && fh->ino->localname != NULL)
  1110. {
  1111. fh->handle = open (fh->ino->localname, NO_LINEAR (flags), mode);
  1112. if (fh->handle == -1)
  1113. {
  1114. vfs_s_free_fh (s, fh);
  1115. me->verrno = errno;
  1116. return NULL;
  1117. }
  1118. }
  1119. /* i.e. we had no open files and now we have one */
  1120. vfs_rmstamp (me, (vfsid) super);
  1121. super->fd_usage++;
  1122. fh->ino->st.st_nlink++;
  1123. return fh;
  1124. }
  1125. /* --------------------------------------------------------------------------------------------- */
  1126. int
  1127. vfs_s_stat (const vfs_path_t *vpath, struct stat *buf)
  1128. {
  1129. return vfs_s_internal_stat (vpath, buf, FL_FOLLOW);
  1130. }
  1131. /* --------------------------------------------------------------------------------------------- */
  1132. int
  1133. vfs_s_lstat (const vfs_path_t *vpath, struct stat *buf)
  1134. {
  1135. return vfs_s_internal_stat (vpath, buf, FL_NONE);
  1136. }
  1137. /* --------------------------------------------------------------------------------------------- */
  1138. int
  1139. vfs_s_fstat (void *fh, struct stat *buf)
  1140. {
  1141. *buf = VFS_FILE_HANDLER (fh)->ino->st;
  1142. return 0;
  1143. }
  1144. /* --------------------------------------------------------------------------------------------- */
  1145. int
  1146. vfs_s_retrieve_file (struct vfs_class *me, struct vfs_s_inode *ino)
  1147. {
  1148. /* If you want reget, you'll have to open file with O_LINEAR */
  1149. off_t total = 0;
  1150. char buffer[BUF_8K];
  1151. int handle;
  1152. ssize_t n;
  1153. off_t stat_size = ino->st.st_size;
  1154. vfs_file_handler_t *fh = NULL;
  1155. vfs_path_t *tmp_vpath;
  1156. struct vfs_s_subclass *s = VFS_SUBCLASS (me);
  1157. if ((me->flags & VFSF_USETMP) == 0)
  1158. return (-1);
  1159. handle = vfs_mkstemps (&tmp_vpath, me->name, ino->ent->name);
  1160. ino->localname = vfs_path_free (tmp_vpath, FALSE);
  1161. if (handle == -1)
  1162. {
  1163. me->verrno = errno;
  1164. goto error_4;
  1165. }
  1166. fh = s->fh_new != NULL ? s->fh_new (ino, FALSE) : vfs_s_new_fh (ino, FALSE);
  1167. if (s->linear_start (me, fh, 0) == 0)
  1168. goto error_3;
  1169. /* Clear the interrupt status */
  1170. tty_got_interrupt ();
  1171. tty_enable_interrupt_key ();
  1172. while ((n = s->linear_read (me, fh, buffer, sizeof (buffer))) != 0)
  1173. {
  1174. int t;
  1175. if (n < 0)
  1176. goto error_1;
  1177. total += n;
  1178. vfs_s_print_stats (me->name, _("Getting file"), ino->ent->name, total, stat_size);
  1179. if (tty_got_interrupt ())
  1180. goto error_1;
  1181. t = write (handle, buffer, n);
  1182. if (t != n)
  1183. {
  1184. if (t == -1)
  1185. me->verrno = errno;
  1186. goto error_1;
  1187. }
  1188. }
  1189. s->linear_close (me, fh);
  1190. close (handle);
  1191. tty_disable_interrupt_key ();
  1192. vfs_s_free_fh (s, fh);
  1193. return 0;
  1194. error_1:
  1195. s->linear_close (me, fh);
  1196. error_3:
  1197. tty_disable_interrupt_key ();
  1198. close (handle);
  1199. unlink (ino->localname);
  1200. error_4:
  1201. MC_PTR_FREE (ino->localname);
  1202. if (fh != NULL)
  1203. vfs_s_free_fh (s, fh);
  1204. return (-1);
  1205. }
  1206. /* --------------------------------------------------------------------------------------------- */
  1207. /* ----------------------------- Stamping support -------------------------- */
  1208. /* Initialize one of our subclasses - fill common functions */
  1209. void
  1210. vfs_init_class (struct vfs_class *vclass, const char *name, vfs_flags_t flags, const char *prefix)
  1211. {
  1212. memset (vclass, 0, sizeof (struct vfs_class));
  1213. vclass->name = name;
  1214. vclass->flags = flags;
  1215. vclass->prefix = prefix;
  1216. vclass->fill_names = vfs_s_fill_names;
  1217. vclass->open = vfs_s_open;
  1218. vclass->close = vfs_s_close;
  1219. vclass->read = vfs_s_read;
  1220. if ((vclass->flags & VFSF_READONLY) == 0)
  1221. vclass->write = vfs_s_write;
  1222. vclass->opendir = vfs_s_opendir;
  1223. vclass->readdir = vfs_s_readdir;
  1224. vclass->closedir = vfs_s_closedir;
  1225. vclass->stat = vfs_s_stat;
  1226. vclass->lstat = vfs_s_lstat;
  1227. vclass->fstat = vfs_s_fstat;
  1228. vclass->readlink = vfs_s_readlink;
  1229. vclass->chdir = vfs_s_chdir;
  1230. vclass->ferrno = vfs_s_ferrno;
  1231. vclass->lseek = vfs_s_lseek;
  1232. vclass->getid = vfs_s_getid;
  1233. vclass->nothingisopen = vfs_s_nothingisopen;
  1234. vclass->free = vfs_s_free;
  1235. vclass->setctl = vfs_s_setctl;
  1236. if ((vclass->flags & VFSF_USETMP) != 0)
  1237. {
  1238. vclass->getlocalcopy = vfs_s_getlocalcopy;
  1239. vclass->ungetlocalcopy = vfs_s_ungetlocalcopy;
  1240. }
  1241. }
  1242. /* --------------------------------------------------------------------------------------------- */
  1243. void
  1244. vfs_init_subclass (struct vfs_s_subclass *sub, const char *name, vfs_flags_t flags,
  1245. const char *prefix)
  1246. {
  1247. struct vfs_class *vclass = VFS_CLASS (sub);
  1248. size_t len;
  1249. char *start;
  1250. vfs_init_class (vclass, name, flags, prefix);
  1251. len = sizeof (struct vfs_s_subclass) - sizeof (struct vfs_class);
  1252. start = (char *) sub + sizeof (struct vfs_class);
  1253. memset (start, 0, len);
  1254. if ((vclass->flags & VFSF_USETMP) != 0)
  1255. sub->find_entry = vfs_s_find_entry_linear;
  1256. else if ((vclass->flags & VFSF_REMOTE) != 0)
  1257. sub->find_entry = vfs_s_find_entry_linear;
  1258. else
  1259. sub->find_entry = vfs_s_find_entry_tree;
  1260. sub->dir_uptodate = vfs_s_dir_uptodate;
  1261. }
  1262. /* --------------------------------------------------------------------------------------------- */
  1263. /** Find VFS id for given directory name */
  1264. vfsid
  1265. vfs_getid (const vfs_path_t *vpath)
  1266. {
  1267. const struct vfs_class *me;
  1268. me = vfs_path_get_last_path_vfs (vpath);
  1269. if (me == NULL || me->getid == NULL)
  1270. return NULL;
  1271. return me->getid (vpath);
  1272. }
  1273. /* --------------------------------------------------------------------------------------------- */
  1274. /* ----------- Utility functions for networked filesystems -------------- */
  1275. #ifdef ENABLE_VFS_NET
  1276. int
  1277. vfs_s_select_on_two (int fd1, int fd2)
  1278. {
  1279. fd_set set;
  1280. struct timeval time_out;
  1281. int v;
  1282. int maxfd = MAX (fd1, fd2) + 1;
  1283. time_out.tv_sec = 1;
  1284. time_out.tv_usec = 0;
  1285. FD_ZERO (&set);
  1286. FD_SET (fd1, &set);
  1287. FD_SET (fd2, &set);
  1288. v = select (maxfd, &set, 0, 0, &time_out);
  1289. if (v <= 0)
  1290. return v;
  1291. if (FD_ISSET (fd1, &set))
  1292. return 1;
  1293. if (FD_ISSET (fd2, &set))
  1294. return 2;
  1295. return (-1);
  1296. }
  1297. /* --------------------------------------------------------------------------------------------- */
  1298. int
  1299. vfs_s_get_line (struct vfs_class *me, int sock, char *buf, int buf_len, char term)
  1300. {
  1301. FILE *logfile = me->logfile;
  1302. int i;
  1303. char c;
  1304. for (i = 0; i < buf_len - 1; i++, buf++)
  1305. {
  1306. if (read (sock, buf, sizeof (char)) <= 0)
  1307. return 0;
  1308. if (logfile != NULL)
  1309. {
  1310. size_t ret1;
  1311. int ret2;
  1312. ret1 = fwrite (buf, 1, 1, logfile);
  1313. ret2 = fflush (logfile);
  1314. (void) ret1;
  1315. (void) ret2;
  1316. }
  1317. if (*buf == term)
  1318. {
  1319. *buf = '\0';
  1320. return 1;
  1321. }
  1322. }
  1323. /* Line is too long - terminate buffer and discard the rest of line */
  1324. *buf = '\0';
  1325. while (read (sock, &c, sizeof (c)) > 0)
  1326. {
  1327. if (logfile != NULL)
  1328. {
  1329. size_t ret1;
  1330. int ret2;
  1331. ret1 = fwrite (&c, 1, 1, logfile);
  1332. ret2 = fflush (logfile);
  1333. (void) ret1;
  1334. (void) ret2;
  1335. }
  1336. if (c == '\n')
  1337. return 1;
  1338. }
  1339. return 0;
  1340. }
  1341. /* --------------------------------------------------------------------------------------------- */
  1342. int
  1343. vfs_s_get_line_interruptible (struct vfs_class *me, char *buffer, int size, int fd)
  1344. {
  1345. int i;
  1346. int res = 0;
  1347. (void) me;
  1348. tty_enable_interrupt_key ();
  1349. for (i = 0; i < size - 1; i++)
  1350. {
  1351. ssize_t n;
  1352. n = read (fd, &buffer[i], 1);
  1353. if (n == -1 && errno == EINTR)
  1354. {
  1355. buffer[i] = '\0';
  1356. res = EINTR;
  1357. goto ret;
  1358. }
  1359. if (n == 0)
  1360. {
  1361. buffer[i] = '\0';
  1362. goto ret;
  1363. }
  1364. if (buffer[i] == '\n')
  1365. {
  1366. buffer[i] = '\0';
  1367. res = 1;
  1368. goto ret;
  1369. }
  1370. }
  1371. buffer[size - 1] = '\0';
  1372. ret:
  1373. tty_disable_interrupt_key ();
  1374. return res;
  1375. }
  1376. #endif /* ENABLE_VFS_NET */
  1377. /* --------------------------------------------------------------------------------------------- */
  1378. /**
  1379. * Normalize filenames start position
  1380. */
  1381. void
  1382. vfs_s_normalize_filename_leading_spaces (struct vfs_s_inode *root_inode, size_t final_num_spaces)
  1383. {
  1384. GList *iter;
  1385. for (iter = g_queue_peek_head_link (root_inode->subdir); iter != NULL;
  1386. iter = g_list_next (iter))
  1387. {
  1388. struct vfs_s_entry *entry = VFS_ENTRY (iter->data);
  1389. if ((size_t) entry->leading_spaces > final_num_spaces)
  1390. {
  1391. char *source_name, *spacer;
  1392. source_name = entry->name;
  1393. spacer = g_strnfill ((size_t) entry->leading_spaces - final_num_spaces, ' ');
  1394. entry->name = g_strconcat (spacer, source_name, (char *) NULL);
  1395. g_free (spacer);
  1396. g_free (source_name);
  1397. }
  1398. entry->leading_spaces = -1;
  1399. }
  1400. }
  1401. /* --------------------------------------------------------------------------------------------- */