direntry.c 41 KB

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