interface.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  1. /* Virtual File System: interface functions
  2. Copyright (C) 2011 Free Software Foundation, Inc.
  3. Written by:
  4. Slava Zanko <slavazanko@gmail.com>, 2011
  5. This file is part of the Midnight Commander.
  6. The Midnight Commander is free software; you can redistribute it
  7. and/or modify it under the terms of the GNU General Public License as
  8. published by the Free Software Foundation; either version 2 of the
  9. License, or (at your option) any later version.
  10. The Midnight Commander is distributed in the hope that it will be
  11. useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  12. of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  17. MA 02110-1301, USA.
  18. */
  19. /**
  20. * \file
  21. * \brief Source: Virtual File System: path handlers
  22. * \author Slava Zanko
  23. * \date 2011
  24. */
  25. #include <config.h>
  26. #include <stdio.h>
  27. #include <stdlib.h> /* For atol() */
  28. #include <stdarg.h>
  29. #include <string.h>
  30. #include <errno.h>
  31. #include <sys/types.h>
  32. #include <signal.h>
  33. #include <ctype.h> /* is_digit() */
  34. #include <fcntl.h>
  35. #include <sys/stat.h>
  36. #include <unistd.h>
  37. #include <dirent.h>
  38. #include "lib/global.h"
  39. #include "lib/widget.h" /* message() */
  40. #include "lib/strutil.h" /* str_crt_conv_from() */
  41. #include "vfs.h"
  42. #include "utilvfs.h"
  43. #include "path.h"
  44. #include "gc.h"
  45. extern GString *vfs_str_buffer;
  46. extern struct vfs_class *current_vfs;
  47. /*** global variables ****************************************************************************/
  48. struct dirent *mc_readdir_result = NULL;
  49. /*** file scope macro definitions ****************************************************************/
  50. /*** file scope type declarations ****************************************************************/
  51. /*** file scope variables ************************************************************************/
  52. /*** file scope functions ************************************************************************/
  53. /* --------------------------------------------------------------------------------------------- */
  54. static char *
  55. mc_def_getlocalcopy (const char *filename)
  56. {
  57. char *tmp;
  58. int fdin, fdout;
  59. ssize_t i;
  60. char buffer[8192];
  61. struct stat mystat;
  62. fdin = mc_open (filename, O_RDONLY | O_LINEAR);
  63. if (fdin == -1)
  64. return NULL;
  65. fdout = vfs_mkstemps (&tmp, "vfs", filename);
  66. if (fdout == -1)
  67. goto fail;
  68. while ((i = mc_read (fdin, buffer, sizeof (buffer))) > 0)
  69. {
  70. if (write (fdout, buffer, i) != i)
  71. goto fail;
  72. }
  73. if (i == -1)
  74. goto fail;
  75. i = mc_close (fdin);
  76. fdin = -1;
  77. if (i == -1)
  78. goto fail;
  79. if (close (fdout) == -1)
  80. {
  81. fdout = -1;
  82. goto fail;
  83. }
  84. if (mc_stat (filename, &mystat) != -1)
  85. chmod (tmp, mystat.st_mode);
  86. return tmp;
  87. fail:
  88. if (fdout != -1)
  89. close (fdout);
  90. if (fdin != -1)
  91. mc_close (fdin);
  92. g_free (tmp);
  93. return NULL;
  94. }
  95. /* --------------------------------------------------------------------------------------------- */
  96. static int
  97. mc_def_ungetlocalcopy (struct vfs_class *vfs, const char *filename,
  98. const char *local, int has_changed)
  99. {
  100. int fdin = -1, fdout = -1;
  101. if (has_changed)
  102. {
  103. char buffer[8192];
  104. ssize_t i;
  105. if (!vfs->write)
  106. goto failed;
  107. fdin = open (local, O_RDONLY);
  108. if (fdin == -1)
  109. goto failed;
  110. fdout = mc_open (filename, O_WRONLY | O_TRUNC);
  111. if (fdout == -1)
  112. goto failed;
  113. while ((i = read (fdin, buffer, sizeof (buffer))) > 0)
  114. if (mc_write (fdout, buffer, (size_t) i) != i)
  115. goto failed;
  116. if (i == -1)
  117. goto failed;
  118. if (close (fdin) == -1)
  119. {
  120. fdin = -1;
  121. goto failed;
  122. }
  123. fdin = -1;
  124. if (mc_close (fdout) == -1)
  125. {
  126. fdout = -1;
  127. goto failed;
  128. }
  129. }
  130. unlink (local);
  131. return 0;
  132. failed:
  133. message (D_ERROR, _("Changes to file lost"), "%s", filename);
  134. if (fdout != -1)
  135. mc_close (fdout);
  136. if (fdin != -1)
  137. close (fdin);
  138. unlink (local);
  139. return -1;
  140. }
  141. /* --------------------------------------------------------------------------------------------- */
  142. /*** public functions ****************************************************************************/
  143. /* --------------------------------------------------------------------------------------------- */
  144. int
  145. mc_open (const char *filename, int flags, ...)
  146. {
  147. int mode = 0, result = -1;
  148. vfs_path_t *vpath;
  149. vfs_path_element_t *path_element;
  150. vpath = vfs_path_from_str (filename);
  151. if (vpath == NULL)
  152. return -1;
  153. /* Get the mode flag */
  154. if (flags & O_CREAT)
  155. {
  156. va_list ap;
  157. va_start (ap, flags);
  158. mode = va_arg (ap, int);
  159. va_end (ap);
  160. }
  161. path_element = vfs_path_get_by_index (vpath, -1);
  162. if (vfs_path_element_valid (path_element) && path_element->class->open != NULL)
  163. {
  164. void *info;
  165. /* open must be supported */
  166. info = path_element->class->open (vpath, flags, mode);
  167. if (info == NULL)
  168. errno = vfs_ferrno (path_element->class);
  169. else
  170. result = vfs_new_handle (path_element->class, info);
  171. }
  172. else
  173. errno = -EOPNOTSUPP;
  174. vfs_path_free (vpath);
  175. return result;
  176. }
  177. /* --------------------------------------------------------------------------------------------- */
  178. /* *INDENT-OFF* */
  179. #define MC_NAMEOP(name, inarg, callarg) \
  180. int mc_##name inarg \
  181. { \
  182. int result; \
  183. vfs_path_t *vpath; \
  184. vfs_path_element_t *path_element; \
  185. \
  186. vpath = vfs_path_from_str (path); \
  187. if (vpath == NULL) \
  188. return -1; \
  189. \
  190. path_element = vfs_path_get_by_index (vpath, -1); \
  191. if (!vfs_path_element_valid (path_element)) \
  192. { \
  193. vfs_path_free(vpath); \
  194. return -1; \
  195. } \
  196. \
  197. result = path_element->class->name != NULL ? path_element->class->name callarg : -1; \
  198. if (result == -1) \
  199. errno = path_element->class->name != NULL ? vfs_ferrno (path_element->class) : E_NOTSUPP; \
  200. vfs_path_free(vpath); \
  201. return result; \
  202. }
  203. MC_NAMEOP (chmod, (const char *path, mode_t mode), (vpath, mode))
  204. MC_NAMEOP (chown, (const char *path, uid_t owner, gid_t group), (vpath, owner, group))
  205. MC_NAMEOP (utime, (const char *path, struct utimbuf * times), (vpath, times))
  206. MC_NAMEOP (readlink, (const char *path, char *buf, size_t bufsiz), (vpath, buf, bufsiz))
  207. MC_NAMEOP (unlink, (const char *path), (vpath))
  208. MC_NAMEOP (mkdir, (const char *path, mode_t mode), (vpath, mode))
  209. MC_NAMEOP (rmdir, (const char *path), (vpath))
  210. MC_NAMEOP (mknod, (const char *path, mode_t mode, dev_t dev), (vpath, mode, dev))
  211. /* *INDENT-ON* */
  212. /* --------------------------------------------------------------------------------------------- */
  213. int
  214. mc_symlink (const char *name1, const char *path)
  215. {
  216. int result = -1;
  217. vfs_path_t *vpath1, *vpath2;
  218. vpath1 = vfs_path_from_str (path);
  219. if (vpath1 == NULL)
  220. return -1;
  221. vpath2 = vfs_path_from_str_flags (name1, VPF_NO_CANON);
  222. if (vpath2 != NULL)
  223. {
  224. vfs_path_element_t *path_element = vfs_path_get_by_index (vpath1, -1);
  225. if (vfs_path_element_valid (path_element))
  226. {
  227. result =
  228. path_element->class->symlink !=
  229. NULL ? path_element->class->symlink (vpath2, vpath1) : -1;
  230. if (result == -1)
  231. errno =
  232. path_element->class->symlink !=
  233. NULL ? vfs_ferrno (path_element->class) : E_NOTSUPP;
  234. }
  235. }
  236. vfs_path_free (vpath1);
  237. vfs_path_free (vpath2);
  238. return result;
  239. }
  240. /* --------------------------------------------------------------------------------------------- */
  241. /* *INDENT-OFF* */
  242. #define MC_HANDLEOP(name, inarg, callarg) \
  243. ssize_t mc_##name inarg \
  244. { \
  245. struct vfs_class *vfs; \
  246. int result; \
  247. if (handle == -1) \
  248. return -1; \
  249. vfs = vfs_class_find_by_handle (handle); \
  250. if (vfs == NULL) \
  251. return -1; \
  252. result = vfs->name != NULL ? vfs->name callarg : -1; \
  253. if (result == -1) \
  254. errno = vfs->name != NULL ? vfs_ferrno (vfs) : E_NOTSUPP; \
  255. return result; \
  256. }
  257. MC_HANDLEOP (read, (int handle, void *buffer, size_t count), (vfs_class_data_find_by_handle (handle), buffer, count))
  258. MC_HANDLEOP (write, (int handle, const void *buf, size_t nbyte), (vfs_class_data_find_by_handle (handle), buf, nbyte))
  259. /* --------------------------------------------------------------------------------------------- */
  260. #define MC_RENAMEOP(name) \
  261. int mc_##name (const char *fname1, const char *fname2) \
  262. { \
  263. int result; \
  264. vfs_path_t *vpath1, *vpath2; \
  265. vfs_path_element_t *path_element1, *path_element2; \
  266. \
  267. vpath1 = vfs_path_from_str (fname1); \
  268. if (vpath1 == NULL) \
  269. return -1; \
  270. \
  271. vpath2 = vfs_path_from_str (fname2); \
  272. if (vpath2 == NULL) \
  273. { \
  274. vfs_path_free(vpath1); \
  275. return -1; \
  276. }\
  277. path_element1 = vfs_path_get_by_index (vpath1, - 1); \
  278. path_element2 = vfs_path_get_by_index (vpath2, - 1); \
  279. \
  280. if (!vfs_path_element_valid (path_element1) || !vfs_path_element_valid (path_element2) || \
  281. path_element1->class != path_element2->class) \
  282. { \
  283. errno = EXDEV; \
  284. vfs_path_free(vpath1); \
  285. vfs_path_free(vpath2); \
  286. return -1; \
  287. }\
  288. \
  289. result = path_element1->class->name != NULL \
  290. ? path_element1->class->name (vpath1, vpath2) \
  291. : -1; \
  292. if (result == -1) \
  293. errno = path_element1->class->name != NULL ? vfs_ferrno (path_element1->class) : E_NOTSUPP; \
  294. vfs_path_free(vpath1); \
  295. vfs_path_free(vpath2); \
  296. return result; \
  297. }
  298. MC_RENAMEOP (link)
  299. MC_RENAMEOP (rename)
  300. /* *INDENT-ON* */
  301. /* --------------------------------------------------------------------------------------------- */
  302. int
  303. mc_ctl (int handle, int ctlop, void *arg)
  304. {
  305. struct vfs_class *vfs = vfs_class_find_by_handle (handle);
  306. if (vfs == NULL)
  307. return 0;
  308. return vfs->ctl ? (*vfs->ctl) (vfs_class_data_find_by_handle (handle), ctlop, arg) : 0;
  309. }
  310. /* --------------------------------------------------------------------------------------------- */
  311. int
  312. mc_setctl (const char *path, int ctlop, void *arg)
  313. {
  314. int result = -1;
  315. vfs_path_t *vpath;
  316. vfs_path_element_t *path_element;
  317. vpath = vfs_path_from_str (path);
  318. if (vpath == NULL)
  319. vfs_die ("You don't want to pass NULL to mc_setctl.");
  320. path_element = vfs_path_get_by_index (vpath, -1);
  321. if (vfs_path_element_valid (path_element))
  322. result =
  323. path_element->class->setctl != NULL ? path_element->class->setctl (vpath,
  324. ctlop, arg) : 0;
  325. vfs_path_free (vpath);
  326. return result;
  327. }
  328. /* --------------------------------------------------------------------------------------------- */
  329. int
  330. mc_close (int handle)
  331. {
  332. struct vfs_class *vfs;
  333. int result;
  334. if (handle == -1 || !vfs_class_data_find_by_handle (handle))
  335. return -1;
  336. vfs = vfs_class_find_by_handle (handle);
  337. if (vfs == NULL)
  338. return -1;
  339. if (handle < 3)
  340. return close (handle);
  341. if (!vfs->close)
  342. vfs_die ("VFS must support close.\n");
  343. result = (*vfs->close) (vfs_class_data_find_by_handle (handle));
  344. vfs_free_handle (handle);
  345. if (result == -1)
  346. errno = vfs_ferrno (vfs);
  347. return result;
  348. }
  349. /* --------------------------------------------------------------------------------------------- */
  350. DIR *
  351. mc_opendir (const char *dirname)
  352. {
  353. int handle, *handlep;
  354. void *info;
  355. vfs_path_t *vpath;
  356. vfs_path_element_t *path_element;
  357. vpath = vfs_path_from_str (dirname);
  358. if (vpath == NULL)
  359. return NULL;
  360. path_element = vfs_path_get_by_index (vpath, -1);
  361. if (!vfs_path_element_valid (path_element))
  362. {
  363. errno = E_NOTSUPP;
  364. vfs_path_free (vpath);
  365. return NULL;
  366. }
  367. info = path_element->class->opendir ? (*path_element->class->opendir) (vpath) : NULL;
  368. if (info == NULL)
  369. {
  370. errno = path_element->class->opendir ? vfs_ferrno (path_element->class) : E_NOTSUPP;
  371. vfs_path_free (vpath);
  372. return NULL;
  373. }
  374. path_element->dir.info = info;
  375. path_element->dir.converter =
  376. (path_element->encoding !=
  377. NULL) ? str_crt_conv_from (path_element->encoding) : str_cnv_from_term;
  378. if (path_element->dir.converter == INVALID_CONV)
  379. path_element->dir.converter = str_cnv_from_term;
  380. handle = vfs_new_handle (path_element->class, vfs_path_element_clone (path_element));
  381. handlep = g_new (int, 1);
  382. *handlep = handle;
  383. vfs_path_free (vpath);
  384. return (DIR *) handlep;
  385. }
  386. /* --------------------------------------------------------------------------------------------- */
  387. struct dirent *
  388. mc_readdir (DIR * dirp)
  389. {
  390. int handle;
  391. struct vfs_class *vfs;
  392. struct dirent *entry = NULL;
  393. vfs_path_element_t *vfs_path_element;
  394. estr_t state;
  395. if (!mc_readdir_result)
  396. {
  397. /* We can't just allocate struct dirent as (see man dirent.h)
  398. * struct dirent has VERY nonnaive semantics of allocating
  399. * d_name in it. Moreover, linux's glibc-2.9 allocates dirents _less_,
  400. * than 'sizeof (struct dirent)' making full bitwise (sizeof dirent) copy
  401. * heap corrupter. So, allocate longliving dirent with at least
  402. * (MAXNAMLEN + 1) for d_name in it.
  403. * Strictly saying resulting dirent is unusable as we don't adjust internal
  404. * structures, holding dirent size. But we don't use it in libc infrastructure.
  405. * TODO: to make simpler homemade dirent-alike structure.
  406. */
  407. mc_readdir_result = (struct dirent *) g_malloc (sizeof (struct dirent) + MAXNAMLEN + 1);
  408. }
  409. if (!dirp)
  410. {
  411. errno = EFAULT;
  412. return NULL;
  413. }
  414. handle = *(int *) dirp;
  415. vfs = vfs_class_find_by_handle (handle);
  416. if (vfs == NULL)
  417. return NULL;
  418. vfs_path_element = vfs_class_data_find_by_handle (handle);
  419. if (vfs->readdir)
  420. {
  421. entry = (*vfs->readdir) (vfs_path_element->dir.info);
  422. if (entry == NULL)
  423. return NULL;
  424. g_string_set_size (vfs_str_buffer, 0);
  425. state =
  426. str_vfs_convert_from (vfs_path_element->dir.converter, entry->d_name, vfs_str_buffer);
  427. mc_readdir_result->d_ino = entry->d_ino;
  428. g_strlcpy (mc_readdir_result->d_name, vfs_str_buffer->str, MAXNAMLEN + 1);
  429. }
  430. if (entry == NULL)
  431. errno = vfs->readdir ? vfs_ferrno (vfs) : E_NOTSUPP;
  432. return (entry != NULL) ? mc_readdir_result : NULL;
  433. }
  434. /* --------------------------------------------------------------------------------------------- */
  435. int
  436. mc_closedir (DIR * dirp)
  437. {
  438. int handle = *(int *) dirp;
  439. struct vfs_class *vfs;
  440. int result = -1;
  441. vfs = vfs_class_find_by_handle (handle);
  442. if (vfs != NULL)
  443. {
  444. vfs_path_element_t *vfs_path_element;
  445. vfs_path_element = vfs_class_data_find_by_handle (handle);
  446. if (vfs_path_element->dir.converter != str_cnv_from_term)
  447. {
  448. str_close_conv (vfs_path_element->dir.converter);
  449. vfs_path_element->dir.converter = INVALID_CONV;
  450. }
  451. result = vfs->closedir ? (*vfs->closedir) (vfs_path_element->dir.info) : -1;
  452. vfs_free_handle (handle);
  453. vfs_path_element_free (vfs_path_element);
  454. }
  455. g_free (dirp);
  456. return result;
  457. }
  458. /* --------------------------------------------------------------------------------------------- */
  459. int
  460. mc_stat (const char *filename, struct stat *buf)
  461. {
  462. int result = -1;
  463. vfs_path_t *vpath;
  464. vfs_path_element_t *path_element;
  465. vpath = vfs_path_from_str (filename);
  466. if (vpath == NULL)
  467. return -1;
  468. path_element = vfs_path_get_by_index (vpath, -1);
  469. if (vfs_path_element_valid (path_element))
  470. {
  471. result = path_element->class->stat ? (*path_element->class->stat) (vpath, buf) : -1;
  472. if (result == -1)
  473. errno = path_element->class->name ? vfs_ferrno (path_element->class) : E_NOTSUPP;
  474. }
  475. vfs_path_free (vpath);
  476. return result;
  477. }
  478. /* --------------------------------------------------------------------------------------------- */
  479. int
  480. mc_lstat (const char *filename, struct stat *buf)
  481. {
  482. int result = -1;
  483. vfs_path_t *vpath;
  484. vfs_path_element_t *path_element;
  485. vpath = vfs_path_from_str (filename);
  486. if (vpath == NULL)
  487. return -1;
  488. path_element = vfs_path_get_by_index (vpath, -1);
  489. if (vfs_path_element_valid (path_element))
  490. {
  491. result = path_element->class->lstat ? (*path_element->class->lstat) (vpath, buf) : -1;
  492. if (result == -1)
  493. errno = path_element->class->name ? vfs_ferrno (path_element->class) : E_NOTSUPP;
  494. }
  495. vfs_path_free (vpath);
  496. return result;
  497. }
  498. /* --------------------------------------------------------------------------------------------- */
  499. int
  500. mc_fstat (int handle, struct stat *buf)
  501. {
  502. struct vfs_class *vfs;
  503. int result;
  504. if (handle == -1)
  505. return -1;
  506. vfs = vfs_class_find_by_handle (handle);
  507. if (vfs == NULL)
  508. return -1;
  509. result = vfs->fstat ? (*vfs->fstat) (vfs_class_data_find_by_handle (handle), buf) : -1;
  510. if (result == -1)
  511. errno = vfs->name ? vfs_ferrno (vfs) : E_NOTSUPP;
  512. return result;
  513. }
  514. /* --------------------------------------------------------------------------------------------- */
  515. /**
  516. * Return current directory. If it's local, reread the current directory
  517. * from the OS. Put directory to the provided buffer.
  518. */
  519. char *
  520. mc_get_current_wd (char *buffer, size_t size)
  521. {
  522. char *cwd = _vfs_get_cwd ();
  523. g_strlcpy (buffer, cwd, size);
  524. g_free (cwd);
  525. return buffer;
  526. }
  527. /* --------------------------------------------------------------------------------------------- */
  528. char *
  529. mc_getlocalcopy (const char *pathname)
  530. {
  531. char *result = NULL;
  532. vfs_path_t *vpath;
  533. vfs_path_element_t *path_element;
  534. vpath = vfs_path_from_str (pathname);
  535. if (vpath == NULL)
  536. return NULL;
  537. path_element = vfs_path_get_by_index (vpath, -1);
  538. if (vfs_path_element_valid (path_element))
  539. {
  540. result = path_element->class->getlocalcopy != NULL ?
  541. path_element->class->getlocalcopy (vpath) : mc_def_getlocalcopy (pathname);
  542. if (result == NULL)
  543. errno = vfs_ferrno (path_element->class);
  544. }
  545. vfs_path_free (vpath);
  546. return result;
  547. }
  548. /* --------------------------------------------------------------------------------------------- */
  549. int
  550. mc_ungetlocalcopy (const char *pathname, const char *local, int has_changed)
  551. {
  552. int return_value = -1;
  553. vfs_path_t *vpath;
  554. vfs_path_element_t *path_element;
  555. vpath = vfs_path_from_str (pathname);
  556. if (vpath == NULL)
  557. return -1;
  558. path_element = vfs_path_get_by_index (vpath, -1);
  559. if (vfs_path_element_valid (path_element))
  560. {
  561. return_value = path_element->class->ungetlocalcopy != NULL ?
  562. path_element->class->ungetlocalcopy (vpath, local,
  563. has_changed) :
  564. mc_def_ungetlocalcopy (path_element->class, path_element->path, local, has_changed);
  565. }
  566. vfs_path_free (vpath);
  567. return return_value;
  568. }
  569. /* --------------------------------------------------------------------------------------------- */
  570. /**
  571. * VFS chdir.
  572. * Return 0 on success, -1 on failure.
  573. */
  574. int
  575. mc_chdir (const char *path)
  576. {
  577. struct vfs_class *old_vfs;
  578. vfsid old_vfsid;
  579. int result;
  580. vfs_path_t *vpath;
  581. vfs_path_element_t *path_element;
  582. vpath = vfs_path_from_str (path);
  583. if (vpath == NULL)
  584. return -1;
  585. path_element = vfs_path_get_by_index (vpath, -1);
  586. if (!vfs_path_element_valid (path_element) || path_element->class->chdir == NULL)
  587. {
  588. vfs_path_free (vpath);
  589. return -1;
  590. }
  591. result = (*path_element->class->chdir) (vpath);
  592. if (result == -1)
  593. {
  594. errno = vfs_ferrno (path_element->class);
  595. vfs_path_free (vpath);
  596. return -1;
  597. }
  598. old_vfsid = vfs_getid (vfs_get_raw_current_dir ());
  599. old_vfs = current_vfs;
  600. /* Actually change directory */
  601. vfs_set_raw_current_dir (vpath);
  602. current_vfs = path_element->class;
  603. /* This function uses the new current_dir implicitly */
  604. vfs_stamp_create (old_vfs, old_vfsid);
  605. /* Sometimes we assume no trailing slash on cwd */
  606. path_element = vfs_path_get_by_index (vfs_get_raw_current_dir (), -1);
  607. if (vfs_path_element_valid (path_element) && (*path_element->path != '\0'))
  608. {
  609. char *p;
  610. p = strchr (path_element->path, 0) - 1;
  611. if (*p == PATH_SEP && p > path_element->path)
  612. *p = 0;
  613. }
  614. return 0;
  615. }
  616. /* --------------------------------------------------------------------------------------------- */
  617. off_t
  618. mc_lseek (int fd, off_t offset, int whence)
  619. {
  620. struct vfs_class *vfs;
  621. off_t result;
  622. if (fd == -1)
  623. return -1;
  624. vfs = vfs_class_find_by_handle (fd);
  625. if (vfs == NULL)
  626. return -1;
  627. result = vfs->lseek ? (*vfs->lseek) (vfs_class_data_find_by_handle (fd), offset, whence) : -1;
  628. if (result == -1)
  629. errno = vfs->lseek ? vfs_ferrno (vfs) : E_NOTSUPP;
  630. return result;
  631. }
  632. /* --------------------------------------------------------------------------------------------- */