direntry.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495
  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%% (%" PRIuMAX " %s";
  582. i18n_transf_format = "%s: %s: %s %" PRIuMAX " %s";
  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. _("bytes transferred"));
  588. else
  589. print_vfs_message (i18n_transf_format, fs_name, action, file_name, (uintmax_t) have,
  590. _("bytes transferred"));
  591. }
  592. /* --------------------------------------------------------------------------------------------- */
  593. /* ------------------------------- mc support ---------------------------- */
  594. static void
  595. vfs_s_fill_names (struct vfs_class *me, fill_names_f func)
  596. {
  597. struct vfs_s_super *a = MEDATA->supers;
  598. char *name;
  599. while (a)
  600. {
  601. name = g_strconcat (a->name, "#", me->prefix, "/",
  602. /* a->current_dir->name, */ (char *) NULL);
  603. (*func) (name);
  604. g_free (name);
  605. a = a->next;
  606. }
  607. }
  608. /* --------------------------------------------------------------------------------------------- */
  609. static int
  610. vfs_s_ferrno (struct vfs_class *me)
  611. {
  612. return me->verrno;
  613. }
  614. /* --------------------------------------------------------------------------------------------- */
  615. /**
  616. * Get local copy of the given file. We reuse the existing file cache
  617. * for remote filesystems. Archives use standard VFS facilities.
  618. */
  619. static char *
  620. vfs_s_getlocalcopy (struct vfs_class *me, const char *path)
  621. {
  622. struct vfs_s_fh *fh;
  623. char *local = NULL;
  624. fh = vfs_s_open (me, path, O_RDONLY, 0);
  625. if (fh != NULL)
  626. {
  627. if ((fh->ino != NULL) && (fh->ino->localname != NULL))
  628. local = g_strdup (fh->ino->localname);
  629. vfs_s_close (fh);
  630. }
  631. return local;
  632. }
  633. /* --------------------------------------------------------------------------------------------- */
  634. /**
  635. * Return the local copy. Since we are using our cache, we do nothing -
  636. * the cache will be removed when the archive is closed.
  637. */
  638. static int
  639. vfs_s_ungetlocalcopy (struct vfs_class *me, const char *path, const char *local, int has_changed)
  640. {
  641. (void) me;
  642. (void) path;
  643. (void) local;
  644. (void) has_changed;
  645. return 0;
  646. }
  647. /* --------------------------------------------------------------------------------------------- */
  648. static int
  649. vfs_s_setctl (struct vfs_class *me, const char *path, int ctlop, void *arg)
  650. {
  651. switch (ctlop)
  652. {
  653. case VFS_SETCTL_STALE_DATA:
  654. {
  655. struct vfs_s_inode *ino = vfs_s_inode_from_path (me, path, 0);
  656. if (ino == NULL)
  657. return 0;
  658. if (arg)
  659. ino->super->want_stale = 1;
  660. else
  661. {
  662. ino->super->want_stale = 0;
  663. vfs_s_invalidate (me, ino->super);
  664. }
  665. return 1;
  666. }
  667. case VFS_SETCTL_LOGFILE:
  668. MEDATA->logfile = fopen ((char *) arg, "w");
  669. return 1;
  670. case VFS_SETCTL_FLUSH:
  671. MEDATA->flush = 1;
  672. return 1;
  673. }
  674. return 0;
  675. }
  676. /* --------------------------------------------------------------------------------------------- */
  677. /* ----------------------------- Stamping support -------------------------- */
  678. static vfsid
  679. vfs_s_getid (struct vfs_class *me, const char *path)
  680. {
  681. struct vfs_s_super *archive = NULL;
  682. char *p;
  683. p = vfs_s_get_path (me, path, &archive, FL_NO_OPEN);
  684. if (p == NULL)
  685. return NULL;
  686. g_free (p);
  687. return (vfsid) archive;
  688. }
  689. /* --------------------------------------------------------------------------------------------- */
  690. static int
  691. vfs_s_nothingisopen (vfsid id)
  692. {
  693. (void) id;
  694. /* Our data structures should survive free of superblock at any time */
  695. return 1;
  696. }
  697. /* --------------------------------------------------------------------------------------------- */
  698. static void
  699. vfs_s_free (vfsid id)
  700. {
  701. vfs_s_free_super (((struct vfs_s_super *) id)->me, (struct vfs_s_super *) id);
  702. }
  703. /* --------------------------------------------------------------------------------------------- */
  704. static int
  705. vfs_s_dir_uptodate (struct vfs_class *me, struct vfs_s_inode *ino)
  706. {
  707. struct timeval tim;
  708. if (MEDATA->flush)
  709. {
  710. MEDATA->flush = 0;
  711. return 0;
  712. }
  713. gettimeofday (&tim, NULL);
  714. if (tim.tv_sec < ino->timestamp.tv_sec)
  715. return 1;
  716. return 0;
  717. }
  718. /* --------------------------------------------------------------------------------------------- */
  719. /*** public functions ****************************************************************************/
  720. /* --------------------------------------------------------------------------------------------- */
  721. struct vfs_s_inode *
  722. vfs_s_new_inode (struct vfs_class *me, struct vfs_s_super *super, struct stat *initstat)
  723. {
  724. struct vfs_s_inode *ino;
  725. ino = g_try_new0 (struct vfs_s_inode, 1);
  726. if (ino == NULL)
  727. return NULL;
  728. if (initstat)
  729. ino->st = *initstat;
  730. ino->super = super;
  731. ino->st.st_nlink = 0;
  732. ino->st.st_ino = MEDATA->inode_counter++;
  733. ino->st.st_dev = MEDATA->rdev;
  734. super->ino_usage++;
  735. total_inodes++;
  736. CALL (init_inode) (me, ino);
  737. return ino;
  738. }
  739. /* --------------------------------------------------------------------------------------------- */
  740. struct vfs_s_entry *
  741. vfs_s_new_entry (struct vfs_class *me, const char *name, struct vfs_s_inode *inode)
  742. {
  743. struct vfs_s_entry *entry;
  744. entry = g_new0 (struct vfs_s_entry, 1);
  745. total_entries++;
  746. if (name)
  747. entry->name = g_strdup (name);
  748. entry->ino = inode;
  749. entry->ino->ent = entry;
  750. CALL (init_entry) (me, entry);
  751. return entry;
  752. }
  753. /* --------------------------------------------------------------------------------------------- */
  754. void
  755. vfs_s_free_entry (struct vfs_class *me, struct vfs_s_entry *ent)
  756. {
  757. if (ent->prevp)
  758. {
  759. /* It is possible that we are deleting freshly created entry */
  760. *ent->prevp = ent->next;
  761. if (ent->next)
  762. ent->next->prevp = ent->prevp;
  763. }
  764. g_free (ent->name);
  765. ent->name = NULL;
  766. if (ent->ino)
  767. {
  768. ent->ino->ent = NULL;
  769. vfs_s_free_inode (me, ent->ino);
  770. ent->ino = NULL;
  771. }
  772. total_entries--;
  773. g_free (ent);
  774. }
  775. /* --------------------------------------------------------------------------------------------- */
  776. void
  777. vfs_s_insert_entry (struct vfs_class *me, struct vfs_s_inode *dir, struct vfs_s_entry *ent)
  778. {
  779. struct vfs_s_entry **ep;
  780. (void) me;
  781. for (ep = &dir->subdir; *ep != NULL; ep = &((*ep)->next))
  782. ;
  783. ent->prevp = ep;
  784. ent->next = NULL;
  785. ent->dir = dir;
  786. *ep = ent;
  787. ent->ino->st.st_nlink++;
  788. }
  789. /* --------------------------------------------------------------------------------------------- */
  790. struct stat *
  791. vfs_s_default_stat (struct vfs_class *me, mode_t mode)
  792. {
  793. static struct stat st;
  794. int myumask;
  795. (void) me;
  796. myumask = umask (022);
  797. umask (myumask);
  798. mode &= ~myumask;
  799. st.st_mode = mode;
  800. st.st_ino = 0;
  801. st.st_dev = 0;
  802. st.st_rdev = 0;
  803. st.st_uid = getuid ();
  804. st.st_gid = getgid ();
  805. st.st_size = 0;
  806. st.st_mtime = st.st_atime = st.st_ctime = time (NULL);
  807. return &st;
  808. }
  809. /* --------------------------------------------------------------------------------------------- */
  810. struct vfs_s_entry *
  811. vfs_s_generate_entry (struct vfs_class *me, const char *name, struct vfs_s_inode *parent,
  812. mode_t mode)
  813. {
  814. struct vfs_s_inode *inode;
  815. struct stat *st;
  816. st = vfs_s_default_stat (me, mode);
  817. inode = vfs_s_new_inode (me, parent->super, st);
  818. return vfs_s_new_entry (me, name, inode);
  819. }
  820. /* --------------------------------------------------------------------------------------------- */
  821. struct vfs_s_inode *
  822. vfs_s_find_inode (struct vfs_class *me, const struct vfs_s_super *super,
  823. const char *path, int follow, int flags)
  824. {
  825. struct vfs_s_entry *ent;
  826. if (((MEDATA->flags & VFS_S_REMOTE) == 0) && (*path == '\0'))
  827. return super->root;
  828. ent = (MEDATA->find_entry) (me, super->root, path, follow, flags);
  829. return (ent != NULL) ? ent->ino : NULL;
  830. }
  831. /* --------------------------------------------------------------------------------------------- */
  832. /* Ook, these were functions around directory entries / inodes */
  833. /* -------------------------------- superblock games -------------------------- */
  834. /*
  835. * Dissect the path and create corresponding superblock. Note that inname
  836. * can be changed and the result may point inside the original string.
  837. */
  838. const char *
  839. vfs_s_get_path_mangle (struct vfs_class *me, char *inname, struct vfs_s_super **archive, int flags)
  840. {
  841. const char *retval;
  842. char *local, *op;
  843. const char *archive_name;
  844. int result = -1;
  845. struct vfs_s_super *super;
  846. void *cookie = NULL;
  847. archive_name = inname;
  848. vfs_split (inname, &local, &op);
  849. retval = (local != NULL) ? local : "";
  850. if (MEDATA->archive_check != NULL)
  851. {
  852. cookie = MEDATA->archive_check (me, archive_name, op);
  853. if (cookie == NULL)
  854. return NULL;
  855. }
  856. for (super = MEDATA->supers; super != NULL; super = super->next)
  857. {
  858. /* 0 == other, 1 == same, return it, 2 == other but stop scanning */
  859. int i = MEDATA->archive_same (me, super, archive_name, op, cookie);
  860. if (i != 0)
  861. {
  862. if (i == 1)
  863. goto return_success;
  864. else
  865. break;
  866. }
  867. }
  868. if (flags & FL_NO_OPEN)
  869. ERRNOR (EIO, NULL);
  870. super = vfs_s_new_super (me);
  871. result = MEDATA->open_archive (me, super, archive_name, op);
  872. if (result == -1)
  873. {
  874. vfs_s_free_super (me, super);
  875. ERRNOR (EIO, NULL);
  876. }
  877. if (!super->name)
  878. vfs_die ("You have to fill name\n");
  879. if (!super->root)
  880. vfs_die ("You have to fill root inode\n");
  881. vfs_s_insert_super (me, super);
  882. vfs_stamp_create (me, super);
  883. return_success:
  884. *archive = super;
  885. return retval;
  886. }
  887. /* --------------------------------------------------------------------------------------------- */
  888. void
  889. vfs_s_invalidate (struct vfs_class *me, struct vfs_s_super *super)
  890. {
  891. if (!super->want_stale)
  892. {
  893. vfs_s_free_inode (me, super->root);
  894. super->root = vfs_s_new_inode (me, super, vfs_s_default_stat (me, S_IFDIR | 0755));
  895. }
  896. }
  897. /* --------------------------------------------------------------------------------------------- */
  898. char *
  899. vfs_s_fullpath (struct vfs_class *me, struct vfs_s_inode *ino)
  900. {
  901. if (!ino->ent)
  902. ERRNOR (EAGAIN, NULL);
  903. if (!(MEDATA->flags & VFS_S_REMOTE))
  904. {
  905. /* archives */
  906. char *newpath;
  907. char *path = g_strdup (ino->ent->name);
  908. while (1)
  909. {
  910. ino = ino->ent->dir;
  911. if (ino == ino->super->root)
  912. break;
  913. newpath = g_strconcat (ino->ent->name, "/", path, (char *) NULL);
  914. g_free (path);
  915. path = newpath;
  916. }
  917. return path;
  918. }
  919. /* remote systems */
  920. if ((!ino->ent->dir) || (!ino->ent->dir->ent))
  921. return g_strdup (ino->ent->name);
  922. return g_strconcat (ino->ent->dir->ent->name, PATH_SEP_STR, ino->ent->name, (char *) NULL);
  923. }
  924. /* --------------------------------------------------------------------------------------------- */
  925. /* --------------------------- stat and friends ---------------------------- */
  926. void *
  927. vfs_s_open (struct vfs_class *me, const char *file, int flags, mode_t mode)
  928. {
  929. int was_changed = 0;
  930. struct vfs_s_fh *fh;
  931. struct vfs_s_super *super;
  932. char *q;
  933. struct vfs_s_inode *ino;
  934. q = vfs_s_get_path (me, file, &super, 0);
  935. if (q == NULL)
  936. return NULL;
  937. ino = vfs_s_find_inode (me, super, q, LINK_FOLLOW, FL_NONE);
  938. if (ino && ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL)))
  939. {
  940. g_free (q);
  941. ERRNOR (EEXIST, NULL);
  942. }
  943. if (!ino)
  944. {
  945. char *dirname, *name, *save;
  946. struct vfs_s_entry *ent;
  947. struct vfs_s_inode *dir;
  948. int tmp_handle;
  949. /* If the filesystem is read-only, disable file creation */
  950. if (!(flags & O_CREAT) || !(me->write))
  951. {
  952. g_free (q);
  953. return NULL;
  954. }
  955. split_dir_name (me, q, &dirname, &name, &save);
  956. dir = vfs_s_find_inode (me, super, dirname, LINK_FOLLOW, FL_DIR);
  957. if (dir == NULL)
  958. {
  959. g_free (q);
  960. return NULL;
  961. }
  962. if (save)
  963. *save = PATH_SEP;
  964. ent = vfs_s_generate_entry (me, name, dir, 0755);
  965. ino = ent->ino;
  966. vfs_s_insert_entry (me, dir, ent);
  967. tmp_handle = vfs_mkstemps (&ino->localname, me->name, name);
  968. if (tmp_handle == -1)
  969. {
  970. g_free (q);
  971. return NULL;
  972. }
  973. close (tmp_handle);
  974. was_changed = 1;
  975. }
  976. g_free (q);
  977. if (S_ISDIR (ino->st.st_mode))
  978. ERRNOR (EISDIR, NULL);
  979. fh = g_new (struct vfs_s_fh, 1);
  980. fh->pos = 0;
  981. fh->ino = ino;
  982. fh->handle = -1;
  983. fh->changed = was_changed;
  984. fh->linear = 0;
  985. if (IS_LINEAR (flags))
  986. {
  987. if (MEDATA->linear_start)
  988. {
  989. print_vfs_message (_("Starting linear transfer..."));
  990. fh->linear = LS_LINEAR_PREOPEN;
  991. }
  992. }
  993. else if ((MEDATA->fh_open) && (MEDATA->fh_open (me, fh, flags, mode)))
  994. {
  995. g_free (fh);
  996. return NULL;
  997. }
  998. if (fh->ino->localname)
  999. {
  1000. fh->handle = open (fh->ino->localname, NO_LINEAR (flags), mode);
  1001. if (fh->handle == -1)
  1002. {
  1003. g_free (fh);
  1004. ERRNOR (errno, NULL);
  1005. }
  1006. }
  1007. /* i.e. we had no open files and now we have one */
  1008. vfs_rmstamp (me, (vfsid) super);
  1009. super->fd_usage++;
  1010. fh->ino->st.st_nlink++;
  1011. return fh;
  1012. }
  1013. /* --------------------------------------------------------------------------------------------- */
  1014. int
  1015. vfs_s_retrieve_file (struct vfs_class *me, struct vfs_s_inode *ino)
  1016. {
  1017. /* If you want reget, you'll have to open file with O_LINEAR */
  1018. off_t total = 0;
  1019. char buffer[8192];
  1020. int handle, n;
  1021. off_t stat_size = ino->st.st_size;
  1022. struct vfs_s_fh fh;
  1023. memset (&fh, 0, sizeof (fh));
  1024. fh.ino = ino;
  1025. fh.handle = -1;
  1026. handle = vfs_mkstemps (&ino->localname, me->name, ino->ent->name);
  1027. if (handle == -1)
  1028. {
  1029. me->verrno = errno;
  1030. goto error_4;
  1031. }
  1032. if (!MEDATA->linear_start (me, &fh, 0))
  1033. goto error_3;
  1034. /* Clear the interrupt status */
  1035. tty_got_interrupt ();
  1036. tty_enable_interrupt_key ();
  1037. while ((n = MEDATA->linear_read (me, &fh, buffer, sizeof (buffer))))
  1038. {
  1039. int t;
  1040. if (n < 0)
  1041. goto error_1;
  1042. total += n;
  1043. vfs_s_print_stats (me->name, _("Getting file"), ino->ent->name, total, stat_size);
  1044. if (tty_got_interrupt ())
  1045. goto error_1;
  1046. t = write (handle, buffer, n);
  1047. if (t != n)
  1048. {
  1049. if (t == -1)
  1050. me->verrno = errno;
  1051. goto error_1;
  1052. }
  1053. }
  1054. MEDATA->linear_close (me, &fh);
  1055. close (handle);
  1056. tty_disable_interrupt_key ();
  1057. return 0;
  1058. error_1:
  1059. MEDATA->linear_close (me, &fh);
  1060. error_3:
  1061. tty_disable_interrupt_key ();
  1062. close (handle);
  1063. unlink (ino->localname);
  1064. error_4:
  1065. g_free (ino->localname);
  1066. ino->localname = NULL;
  1067. return -1;
  1068. }
  1069. /* --------------------------------------------------------------------------------------------- */
  1070. /* ----------------------------- Stamping support -------------------------- */
  1071. /* Initialize one of our subclasses - fill common functions */
  1072. void
  1073. vfs_s_init_class (struct vfs_class *vclass, struct vfs_s_subclass *sub)
  1074. {
  1075. vclass->data = sub;
  1076. vclass->fill_names = vfs_s_fill_names;
  1077. vclass->open = vfs_s_open;
  1078. vclass->close = vfs_s_close;
  1079. vclass->read = vfs_s_read;
  1080. if (!(sub->flags & VFS_S_READONLY))
  1081. {
  1082. vclass->write = vfs_s_write;
  1083. }
  1084. vclass->opendir = vfs_s_opendir;
  1085. vclass->readdir = vfs_s_readdir;
  1086. vclass->closedir = vfs_s_closedir;
  1087. vclass->stat = vfs_s_stat;
  1088. vclass->lstat = vfs_s_lstat;
  1089. vclass->fstat = vfs_s_fstat;
  1090. vclass->readlink = vfs_s_readlink;
  1091. vclass->chdir = vfs_s_chdir;
  1092. vclass->ferrno = vfs_s_ferrno;
  1093. vclass->lseek = vfs_s_lseek;
  1094. vclass->getid = vfs_s_getid;
  1095. vclass->nothingisopen = vfs_s_nothingisopen;
  1096. vclass->free = vfs_s_free;
  1097. if (sub->flags & VFS_S_REMOTE)
  1098. {
  1099. vclass->getlocalcopy = vfs_s_getlocalcopy;
  1100. vclass->ungetlocalcopy = vfs_s_ungetlocalcopy;
  1101. sub->find_entry = vfs_s_find_entry_linear;
  1102. }
  1103. else
  1104. {
  1105. sub->find_entry = vfs_s_find_entry_tree;
  1106. }
  1107. vclass->setctl = vfs_s_setctl;
  1108. sub->dir_uptodate = vfs_s_dir_uptodate;
  1109. }
  1110. /* --------------------------------------------------------------------------------------------- */
  1111. /** Find VFS id for given directory name */
  1112. vfsid
  1113. vfs_getid (struct vfs_class *vclass, const char *dir)
  1114. {
  1115. char *dir1;
  1116. vfsid id = NULL;
  1117. /* append slash if needed */
  1118. dir1 = concat_dir_and_file (dir, "");
  1119. if (vclass->getid)
  1120. id = (*vclass->getid) (vclass, dir1);
  1121. g_free (dir1);
  1122. return id;
  1123. }
  1124. /* --------------------------------------------------------------------------------------------- */
  1125. /* ----------- Utility functions for networked filesystems -------------- */
  1126. #ifdef ENABLE_VFS_NET
  1127. int
  1128. vfs_s_select_on_two (int fd1, int fd2)
  1129. {
  1130. fd_set set;
  1131. struct timeval time_out;
  1132. int v;
  1133. int maxfd = (fd1 > fd2 ? fd1 : fd2) + 1;
  1134. time_out.tv_sec = 1;
  1135. time_out.tv_usec = 0;
  1136. FD_ZERO (&set);
  1137. FD_SET (fd1, &set);
  1138. FD_SET (fd2, &set);
  1139. v = select (maxfd, &set, 0, 0, &time_out);
  1140. if (v <= 0)
  1141. return v;
  1142. if (FD_ISSET (fd1, &set))
  1143. return 1;
  1144. if (FD_ISSET (fd2, &set))
  1145. return 2;
  1146. return -1;
  1147. }
  1148. /* --------------------------------------------------------------------------------------------- */
  1149. int
  1150. vfs_s_get_line (struct vfs_class *me, int sock, char *buf, int buf_len, char term)
  1151. {
  1152. FILE *logfile = MEDATA->logfile;
  1153. int i;
  1154. char c;
  1155. for (i = 0; i < buf_len - 1; i++, buf++)
  1156. {
  1157. if (read (sock, buf, sizeof (char)) <= 0)
  1158. return 0;
  1159. if (logfile)
  1160. {
  1161. size_t ret1;
  1162. int ret2;
  1163. ret1 = fwrite (buf, 1, 1, logfile);
  1164. ret2 = fflush (logfile);
  1165. }
  1166. if (*buf == term)
  1167. {
  1168. *buf = 0;
  1169. return 1;
  1170. }
  1171. }
  1172. /* Line is too long - terminate buffer and discard the rest of line */
  1173. *buf = 0;
  1174. while (read (sock, &c, sizeof (c)) > 0)
  1175. {
  1176. if (logfile)
  1177. {
  1178. size_t ret1;
  1179. int ret2;
  1180. ret1 = fwrite (&c, 1, 1, logfile);
  1181. ret2 = fflush (logfile);
  1182. }
  1183. if (c == '\n')
  1184. return 1;
  1185. }
  1186. return 0;
  1187. }
  1188. /* --------------------------------------------------------------------------------------------- */
  1189. int
  1190. vfs_s_get_line_interruptible (struct vfs_class *me, char *buffer, int size, int fd)
  1191. {
  1192. int n;
  1193. int i;
  1194. (void) me;
  1195. tty_enable_interrupt_key ();
  1196. for (i = 0; i < size - 1; i++)
  1197. {
  1198. n = read (fd, buffer + i, 1);
  1199. tty_disable_interrupt_key ();
  1200. if (n == -1 && errno == EINTR)
  1201. {
  1202. buffer[i] = 0;
  1203. return EINTR;
  1204. }
  1205. if (n == 0)
  1206. {
  1207. buffer[i] = 0;
  1208. return 0;
  1209. }
  1210. if (buffer[i] == '\n')
  1211. {
  1212. buffer[i] = 0;
  1213. return 1;
  1214. }
  1215. }
  1216. buffer[size - 1] = 0;
  1217. return 0;
  1218. }
  1219. #endif /* ENABLE_VFS_NET */
  1220. /* --------------------------------------------------------------------------------------------- */
  1221. /**
  1222. * Normalize filenames start position
  1223. */
  1224. void
  1225. vfs_s_normalize_filename_leading_spaces (struct vfs_s_inode *root_inode, size_t final_num_spaces)
  1226. {
  1227. struct vfs_s_entry *entry;
  1228. for (entry = root_inode->subdir; entry != NULL; entry = entry->next)
  1229. {
  1230. if ((size_t) entry->ino->data_offset > final_num_spaces)
  1231. {
  1232. char *source_name = entry->name;
  1233. char *spacer = g_strnfill (entry->ino->data_offset - final_num_spaces, ' ');
  1234. entry->name = g_strdup_printf ("%s%s", spacer, source_name);
  1235. g_free (spacer);
  1236. g_free (source_name);
  1237. }
  1238. entry->ino->data_offset = -1;
  1239. }
  1240. }
  1241. /* --------------------------------------------------------------------------------------------- */