direntry.c 44 KB

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