direntry.c 44 KB

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