direntry.c 46 KB

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