direntry.c 38 KB

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