interface.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923
  1. /*
  2. Virtual File System: interface functions
  3. Copyright (C) 2011-2023
  4. Free Software Foundation, Inc.
  5. Written by:
  6. Slava Zanko <slavazanko@gmail.com>, 2011, 2013
  7. Andrew Borodin <aborodin@vmail.ru>, 2011-2022
  8. This file is part of the Midnight Commander.
  9. The Midnight Commander is free software: you can redistribute it
  10. and/or modify it under the terms of the GNU General Public License as
  11. published by the Free Software Foundation, either version 3 of the License,
  12. or (at your option) any later version.
  13. The Midnight Commander is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. GNU General Public License for more details.
  17. You should have received a copy of the GNU General Public License
  18. along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. /**
  21. * \file
  22. * \brief Source: Virtual File System: path handlers
  23. * \author Slava Zanko
  24. * \date 2011
  25. */
  26. #include <config.h>
  27. #include <stdio.h>
  28. #include <stdlib.h> /* For atol() */
  29. #include <stdarg.h>
  30. #include <string.h>
  31. #include <errno.h>
  32. #include <sys/types.h>
  33. #include <signal.h>
  34. #include <ctype.h> /* is_digit() */
  35. #include <sys/stat.h>
  36. #include <unistd.h>
  37. #include <dirent.h>
  38. #include <pwd.h>
  39. #include <grp.h>
  40. #include "lib/global.h"
  41. #include "lib/widget.h" /* message() */
  42. #include "lib/strutil.h" /* str_crt_conv_from() */
  43. #include "lib/util.h"
  44. #include "vfs.h"
  45. #include "utilvfs.h"
  46. #include "path.h"
  47. #include "gc.h"
  48. #include "xdirentry.h"
  49. /* TODO: move it to separate private .h */
  50. extern GString *vfs_str_buffer;
  51. extern vfs_class *current_vfs;
  52. extern struct vfs_dirent *mc_readdir_result;
  53. /*** global variables ****************************************************************************/
  54. /*** file scope macro definitions ****************************************************************/
  55. /*** file scope type declarations ****************************************************************/
  56. /*** file scope variables ************************************************************************/
  57. /*** file scope functions ************************************************************************/
  58. /* --------------------------------------------------------------------------------------------- */
  59. static vfs_path_t *
  60. mc_def_getlocalcopy (const vfs_path_t * filename_vpath)
  61. {
  62. vfs_path_t *tmp_vpath = NULL;
  63. int fdin, fdout = -1;
  64. ssize_t i;
  65. char buffer[BUF_1K * 8];
  66. struct stat mystat;
  67. fdin = mc_open (filename_vpath, O_RDONLY | O_LINEAR);
  68. if (fdin == -1)
  69. goto fail;
  70. fdout = vfs_mkstemps (&tmp_vpath, "vfs", vfs_path_get_last_path_str (filename_vpath));
  71. if (fdout == -1)
  72. goto fail;
  73. while ((i = mc_read (fdin, buffer, sizeof (buffer))) > 0)
  74. {
  75. if (write (fdout, buffer, i) != i)
  76. goto fail;
  77. }
  78. if (i == -1)
  79. goto fail;
  80. i = mc_close (fdin);
  81. fdin = -1;
  82. if (i == -1)
  83. goto fail;
  84. i = close (fdout);
  85. fdout = -1;
  86. if (i == -1)
  87. goto fail;
  88. if (mc_stat (filename_vpath, &mystat) != -1)
  89. mc_chmod (tmp_vpath, mystat.st_mode);
  90. return tmp_vpath;
  91. fail:
  92. vfs_path_free (tmp_vpath, TRUE);
  93. if (fdout != -1)
  94. close (fdout);
  95. if (fdin != -1)
  96. mc_close (fdin);
  97. return NULL;
  98. }
  99. /* --------------------------------------------------------------------------------------------- */
  100. static int
  101. mc_def_ungetlocalcopy (const vfs_path_t * filename_vpath,
  102. const vfs_path_t * local_vpath, gboolean has_changed)
  103. {
  104. int fdin = -1, fdout = -1;
  105. const char *local;
  106. local = vfs_path_get_last_path_str (local_vpath);
  107. if (has_changed)
  108. {
  109. char buffer[BUF_1K * 8];
  110. ssize_t i;
  111. if (vfs_path_get_last_path_vfs (filename_vpath)->write == NULL)
  112. goto failed;
  113. fdin = open (local, O_RDONLY);
  114. if (fdin == -1)
  115. goto failed;
  116. fdout = mc_open (filename_vpath, O_WRONLY | O_TRUNC);
  117. if (fdout == -1)
  118. goto failed;
  119. while ((i = read (fdin, buffer, sizeof (buffer))) > 0)
  120. if (mc_write (fdout, buffer, (size_t) i) != i)
  121. goto failed;
  122. if (i == -1)
  123. goto failed;
  124. if (close (fdin) == -1)
  125. {
  126. fdin = -1;
  127. goto failed;
  128. }
  129. fdin = -1;
  130. if (mc_close (fdout) == -1)
  131. {
  132. fdout = -1;
  133. goto failed;
  134. }
  135. }
  136. unlink (local);
  137. return 0;
  138. failed:
  139. message (D_ERROR, _("Changes to file lost"), "%s", vfs_path_get_last_path_str (filename_vpath));
  140. if (fdout != -1)
  141. mc_close (fdout);
  142. if (fdin != -1)
  143. close (fdin);
  144. unlink (local);
  145. return (-1);
  146. }
  147. /* --------------------------------------------------------------------------------------------- */
  148. /*** public functions ****************************************************************************/
  149. /* --------------------------------------------------------------------------------------------- */
  150. int
  151. mc_open (const vfs_path_t * vpath, int flags, ...)
  152. {
  153. int result = -1;
  154. mode_t mode = 0;
  155. const vfs_path_element_t *path_element;
  156. if (vpath == NULL)
  157. return (-1);
  158. /* Get the mode flag */
  159. if ((flags & O_CREAT) != 0)
  160. {
  161. va_list ap;
  162. va_start (ap, flags);
  163. /* We have to use PROMOTED_MODE_T instead of mode_t. Doing 'va_arg (ap, mode_t)'
  164. * fails on systems where 'mode_t' is smaller than 'int' because of C's "default
  165. * argument promotions". */
  166. mode = va_arg (ap, PROMOTED_MODE_T);
  167. va_end (ap);
  168. }
  169. path_element = vfs_path_get_by_index (vpath, -1);
  170. if (vfs_path_element_valid (path_element) && path_element->class->open != NULL)
  171. {
  172. void *info;
  173. /* open must be supported */
  174. info = path_element->class->open (vpath, flags, mode);
  175. if (info == NULL)
  176. errno = vfs_ferrno (path_element->class);
  177. else
  178. result = vfs_new_handle (path_element->class, info);
  179. }
  180. else
  181. errno = ENOTSUP;
  182. return result;
  183. }
  184. /* --------------------------------------------------------------------------------------------- */
  185. /* *INDENT-OFF* */
  186. #define MC_NAMEOP(name, inarg, callarg) \
  187. int mc_##name inarg \
  188. { \
  189. int result; \
  190. const vfs_path_element_t *path_element; \
  191. \
  192. if (vpath == NULL) \
  193. return (-1); \
  194. \
  195. path_element = vfs_path_get_by_index (vpath, -1); \
  196. if (!vfs_path_element_valid (path_element)) \
  197. return (-1); \
  198. \
  199. result = path_element->class->name != NULL ? path_element->class->name callarg : -1; \
  200. if (result == -1) \
  201. errno = path_element->class->name != NULL ? vfs_ferrno (path_element->class) : ENOTSUP; \
  202. return result; \
  203. }
  204. MC_NAMEOP (chmod, (const vfs_path_t *vpath, mode_t mode), (vpath, mode))
  205. MC_NAMEOP (chown, (const vfs_path_t *vpath, uid_t owner, gid_t group), (vpath, owner, group))
  206. MC_NAMEOP (utime, (const vfs_path_t *vpath, mc_timesbuf_t * times), (vpath, times))
  207. MC_NAMEOP (readlink, (const vfs_path_t *vpath, char *buf, size_t bufsiz), (vpath, buf, bufsiz))
  208. MC_NAMEOP (unlink, (const vfs_path_t *vpath), (vpath))
  209. MC_NAMEOP (mkdir, (const vfs_path_t *vpath, mode_t mode), (vpath, mode))
  210. MC_NAMEOP (rmdir, (const vfs_path_t *vpath), (vpath))
  211. MC_NAMEOP (mknod, (const vfs_path_t *vpath, mode_t mode, dev_t dev), (vpath, mode, dev))
  212. /* *INDENT-ON* */
  213. /* --------------------------------------------------------------------------------------------- */
  214. int
  215. mc_symlink (const vfs_path_t * vpath1, const vfs_path_t * vpath2)
  216. {
  217. int result = -1;
  218. if (vpath1 != NULL && vpath2 != NULL)
  219. {
  220. const vfs_path_element_t *path_element;
  221. path_element = vfs_path_get_by_index (vpath2, -1);
  222. if (vfs_path_element_valid (path_element))
  223. {
  224. result =
  225. path_element->class->symlink != NULL ?
  226. path_element->class->symlink (vpath1, vpath2) : -1;
  227. if (result == -1)
  228. errno =
  229. path_element->class->symlink != NULL ?
  230. vfs_ferrno (path_element->class) : ENOTSUP;
  231. }
  232. }
  233. return result;
  234. }
  235. /* --------------------------------------------------------------------------------------------- */
  236. /* *INDENT-OFF* */
  237. #define MC_HANDLEOP(name) \
  238. ssize_t mc_##name (int handle, C void *buf, size_t count) \
  239. { \
  240. struct vfs_class *vfs; \
  241. void *fsinfo = NULL; \
  242. int result; \
  243. \
  244. if (handle == -1) \
  245. return (-1); \
  246. \
  247. vfs = vfs_class_find_by_handle (handle, &fsinfo); \
  248. if (vfs == NULL) \
  249. return (-1); \
  250. \
  251. result = vfs->name != NULL ? vfs->name (fsinfo, buf, count) : -1; \
  252. if (result == -1) \
  253. errno = vfs->name != NULL ? vfs_ferrno (vfs) : ENOTSUP; \
  254. return result; \
  255. }
  256. #define C
  257. MC_HANDLEOP (read)
  258. #undef C
  259. #define C const
  260. MC_HANDLEOP (write)
  261. #undef C
  262. /* --------------------------------------------------------------------------------------------- */
  263. #define MC_RENAMEOP(name) \
  264. int mc_##name (const vfs_path_t *vpath1, const vfs_path_t *vpath2) \
  265. { \
  266. int result; \
  267. const vfs_path_element_t *path_element1; \
  268. const vfs_path_element_t *path_element2; \
  269. \
  270. if (vpath1 == NULL || vpath2 == NULL) \
  271. return (-1); \
  272. \
  273. path_element1 = vfs_path_get_by_index (vpath1, (-1)); \
  274. path_element2 = vfs_path_get_by_index (vpath2, (-1)); \
  275. \
  276. if (!vfs_path_element_valid (path_element1) || !vfs_path_element_valid (path_element2) || \
  277. path_element1->class != path_element2->class) \
  278. { \
  279. errno = EXDEV; \
  280. return (-1); \
  281. } \
  282. \
  283. result = path_element1->class->name != NULL \
  284. ? path_element1->class->name (vpath1, vpath2) : -1; \
  285. if (result == -1) \
  286. errno = path_element1->class->name != NULL ? vfs_ferrno (path_element1->class) : ENOTSUP; \
  287. return result; \
  288. }
  289. MC_RENAMEOP (link)
  290. MC_RENAMEOP (rename)
  291. /* *INDENT-ON* */
  292. /* --------------------------------------------------------------------------------------------- */
  293. int
  294. mc_ctl (int handle, int ctlop, void *arg)
  295. {
  296. struct vfs_class *vfs;
  297. void *fsinfo = NULL;
  298. vfs = vfs_class_find_by_handle (handle, &fsinfo);
  299. return (vfs == NULL || vfs->ctl == NULL) ? 0 : vfs->ctl (fsinfo, ctlop, arg);
  300. }
  301. /* --------------------------------------------------------------------------------------------- */
  302. int
  303. mc_setctl (const vfs_path_t * vpath, int ctlop, void *arg)
  304. {
  305. int result = -1;
  306. const vfs_path_element_t *path_element;
  307. if (vpath == NULL)
  308. vfs_die ("You don't want to pass NULL to mc_setctl.");
  309. path_element = vfs_path_get_by_index (vpath, -1);
  310. if (vfs_path_element_valid (path_element))
  311. result =
  312. path_element->class->setctl != NULL ? path_element->class->setctl (vpath,
  313. ctlop, arg) : 0;
  314. return result;
  315. }
  316. /* --------------------------------------------------------------------------------------------- */
  317. int
  318. mc_close (int handle)
  319. {
  320. struct vfs_class *vfs;
  321. void *fsinfo = NULL;
  322. int result;
  323. if (handle == -1)
  324. return (-1);
  325. vfs = vfs_class_find_by_handle (handle, &fsinfo);
  326. if (vfs == NULL || fsinfo == NULL)
  327. return (-1);
  328. if (handle < 3)
  329. return close (handle);
  330. if (vfs->close == NULL)
  331. vfs_die ("VFS must support close.\n");
  332. result = vfs->close (fsinfo);
  333. vfs_free_handle (handle);
  334. if (result == -1)
  335. errno = vfs_ferrno (vfs);
  336. return result;
  337. }
  338. /* --------------------------------------------------------------------------------------------- */
  339. DIR *
  340. mc_opendir (const vfs_path_t * vpath)
  341. {
  342. int handle, *handlep;
  343. void *info;
  344. vfs_path_element_t *path_element;
  345. if (vpath == NULL)
  346. return NULL;
  347. path_element = (vfs_path_element_t *) vfs_path_get_by_index (vpath, -1);
  348. if (!vfs_path_element_valid (path_element))
  349. {
  350. errno = ENOTSUP;
  351. return NULL;
  352. }
  353. info = path_element->class->opendir ? path_element->class->opendir (vpath) : NULL;
  354. if (info == NULL)
  355. {
  356. errno = path_element->class->opendir ? vfs_ferrno (path_element->class) : ENOTSUP;
  357. return NULL;
  358. }
  359. path_element->dir.info = info;
  360. #ifdef HAVE_CHARSET
  361. path_element->dir.converter = (path_element->encoding != NULL) ?
  362. str_crt_conv_from (path_element->encoding) : str_cnv_from_term;
  363. if (path_element->dir.converter == INVALID_CONV)
  364. path_element->dir.converter = str_cnv_from_term;
  365. #endif
  366. handle = vfs_new_handle (path_element->class, vfs_path_element_clone (path_element));
  367. handlep = g_new (int, 1);
  368. *handlep = handle;
  369. return (DIR *) handlep;
  370. }
  371. /* --------------------------------------------------------------------------------------------- */
  372. struct vfs_dirent *
  373. mc_readdir (DIR * dirp)
  374. {
  375. int handle;
  376. struct vfs_class *vfs;
  377. void *fsinfo = NULL;
  378. struct vfs_dirent *entry = NULL;
  379. vfs_path_element_t *vfs_path_element;
  380. if (dirp == NULL)
  381. {
  382. errno = EFAULT;
  383. return NULL;
  384. }
  385. handle = *(int *) dirp;
  386. vfs = vfs_class_find_by_handle (handle, &fsinfo);
  387. if (vfs == NULL || fsinfo == NULL)
  388. return NULL;
  389. vfs_path_element = (vfs_path_element_t *) fsinfo;
  390. if (vfs->readdir != NULL)
  391. {
  392. entry = vfs->readdir (vfs_path_element->dir.info);
  393. if (entry == NULL)
  394. return NULL;
  395. g_string_set_size (vfs_str_buffer, 0);
  396. #ifdef HAVE_CHARSET
  397. str_vfs_convert_from (vfs_path_element->dir.converter, entry->d_name, vfs_str_buffer);
  398. #else
  399. g_string_assign (vfs_str_buffer, entry->d_name);
  400. #endif
  401. vfs_dirent_assign (mc_readdir_result, vfs_str_buffer->str, entry->d_ino);
  402. vfs_dirent_free (entry);
  403. }
  404. if (entry == NULL)
  405. errno = vfs->readdir ? vfs_ferrno (vfs) : ENOTSUP;
  406. return (entry != NULL) ? mc_readdir_result : NULL;
  407. }
  408. /* --------------------------------------------------------------------------------------------- */
  409. int
  410. mc_closedir (DIR * dirp)
  411. {
  412. int handle;
  413. struct vfs_class *vfs;
  414. void *fsinfo = NULL;
  415. int result = -1;
  416. if (dirp == NULL)
  417. return result;
  418. handle = *(int *) dirp;
  419. vfs = vfs_class_find_by_handle (handle, &fsinfo);
  420. if (vfs != NULL && fsinfo != NULL)
  421. {
  422. vfs_path_element_t *vfs_path_element = (vfs_path_element_t *) fsinfo;
  423. #ifdef HAVE_CHARSET
  424. if (vfs_path_element->dir.converter != str_cnv_from_term)
  425. {
  426. str_close_conv (vfs_path_element->dir.converter);
  427. vfs_path_element->dir.converter = INVALID_CONV;
  428. }
  429. #endif
  430. result = vfs->closedir ? (*vfs->closedir) (vfs_path_element->dir.info) : -1;
  431. vfs_free_handle (handle);
  432. vfs_path_element_free (vfs_path_element);
  433. }
  434. g_free (dirp);
  435. return result;
  436. }
  437. /* --------------------------------------------------------------------------------------------- */
  438. int
  439. mc_stat (const vfs_path_t * vpath, struct stat *buf)
  440. {
  441. int result = -1;
  442. const vfs_path_element_t *path_element;
  443. if (vpath == NULL)
  444. return (-1);
  445. path_element = vfs_path_get_by_index (vpath, -1);
  446. if (vfs_path_element_valid (path_element))
  447. {
  448. result = path_element->class->stat ? path_element->class->stat (vpath, buf) : -1;
  449. if (result == -1)
  450. errno = path_element->class->name ? vfs_ferrno (path_element->class) : ENOTSUP;
  451. }
  452. return result;
  453. }
  454. /* --------------------------------------------------------------------------------------------- */
  455. int
  456. mc_lstat (const vfs_path_t * vpath, struct stat *buf)
  457. {
  458. int result = -1;
  459. const vfs_path_element_t *path_element;
  460. if (vpath == NULL)
  461. return (-1);
  462. path_element = vfs_path_get_by_index (vpath, -1);
  463. if (vfs_path_element_valid (path_element))
  464. {
  465. result = path_element->class->lstat ? path_element->class->lstat (vpath, buf) : -1;
  466. if (result == -1)
  467. errno = path_element->class->name ? vfs_ferrno (path_element->class) : ENOTSUP;
  468. }
  469. return result;
  470. }
  471. /* --------------------------------------------------------------------------------------------- */
  472. int
  473. mc_fstat (int handle, struct stat *buf)
  474. {
  475. struct vfs_class *vfs;
  476. void *fsinfo = NULL;
  477. int result;
  478. if (handle == -1)
  479. return (-1);
  480. vfs = vfs_class_find_by_handle (handle, &fsinfo);
  481. if (vfs == NULL)
  482. return (-1);
  483. result = vfs->fstat ? vfs->fstat (fsinfo, buf) : -1;
  484. if (result == -1)
  485. errno = vfs->fstat ? vfs_ferrno (vfs) : ENOTSUP;
  486. return result;
  487. }
  488. /* --------------------------------------------------------------------------------------------- */
  489. vfs_path_t *
  490. mc_getlocalcopy (const vfs_path_t * pathname_vpath)
  491. {
  492. vfs_path_t *result = NULL;
  493. const vfs_path_element_t *path_element;
  494. if (pathname_vpath == NULL)
  495. return NULL;
  496. path_element = vfs_path_get_by_index (pathname_vpath, -1);
  497. if (vfs_path_element_valid (path_element))
  498. {
  499. result = path_element->class->getlocalcopy != NULL ?
  500. path_element->class->getlocalcopy (pathname_vpath) :
  501. mc_def_getlocalcopy (pathname_vpath);
  502. if (result == NULL)
  503. errno = vfs_ferrno (path_element->class);
  504. }
  505. return result;
  506. }
  507. /* --------------------------------------------------------------------------------------------- */
  508. int
  509. mc_ungetlocalcopy (const vfs_path_t * pathname_vpath, const vfs_path_t * local_vpath,
  510. gboolean has_changed)
  511. {
  512. int result = -1;
  513. const vfs_path_element_t *path_element;
  514. if (pathname_vpath == NULL)
  515. return (-1);
  516. path_element = vfs_path_get_by_index (pathname_vpath, -1);
  517. if (vfs_path_element_valid (path_element))
  518. result = path_element->class->ungetlocalcopy != NULL ?
  519. path_element->class->ungetlocalcopy (pathname_vpath, local_vpath, has_changed) :
  520. mc_def_ungetlocalcopy (pathname_vpath, local_vpath, has_changed);
  521. return result;
  522. }
  523. /* --------------------------------------------------------------------------------------------- */
  524. /**
  525. * VFS chdir.
  526. *
  527. * @param vpath VFS path.
  528. * May be NULL. In this case NULL is returned and errno set to 0.
  529. *
  530. * @return 0 on success, -1 on failure.
  531. */
  532. int
  533. mc_chdir (const vfs_path_t * vpath)
  534. {
  535. struct vfs_class *old_vfs;
  536. vfsid old_vfsid;
  537. int result;
  538. const vfs_path_element_t *path_element;
  539. vfs_path_t *cd_vpath;
  540. if (vpath == NULL)
  541. {
  542. errno = 0;
  543. return (-1);
  544. }
  545. if (vpath->relative)
  546. cd_vpath = vfs_path_to_absolute (vpath);
  547. else
  548. cd_vpath = vfs_path_clone (vpath);
  549. path_element = vfs_path_get_by_index (cd_vpath, -1);
  550. if (!vfs_path_element_valid (path_element))
  551. {
  552. errno = EINVAL;
  553. goto error_end;
  554. }
  555. if (path_element->class->chdir == NULL)
  556. {
  557. errno = ENOTSUP;
  558. goto error_end;
  559. }
  560. result = path_element->class->chdir (cd_vpath);
  561. if (result == -1)
  562. {
  563. errno = vfs_ferrno (path_element->class);
  564. goto error_end;
  565. }
  566. old_vfsid = vfs_getid (vfs_get_raw_current_dir ());
  567. old_vfs = current_vfs;
  568. /* Actually change directory */
  569. vfs_set_raw_current_dir (cd_vpath);
  570. current_vfs = path_element->class;
  571. /* This function uses the new current_dir implicitly */
  572. vfs_stamp_create (old_vfs, old_vfsid);
  573. /* Sometimes we assume no trailing slash on cwd */
  574. path_element = vfs_path_get_by_index (vfs_get_raw_current_dir (), -1);
  575. if (vfs_path_element_valid (path_element))
  576. {
  577. if (*path_element->path != '\0')
  578. {
  579. char *p;
  580. p = strchr (path_element->path, 0) - 1;
  581. if (IS_PATH_SEP (*p) && p > path_element->path)
  582. *p = '\0';
  583. }
  584. #ifdef ENABLE_VFS_NET
  585. {
  586. struct vfs_s_super *super;
  587. super = vfs_get_super_by_vpath (vpath);
  588. if (super != NULL && super->path_element != NULL)
  589. {
  590. g_free (super->path_element->path);
  591. super->path_element->path = g_strdup (path_element->path);
  592. }
  593. }
  594. #endif /* ENABLE_VFS_NET */
  595. }
  596. return 0;
  597. error_end:
  598. vfs_path_free (cd_vpath, TRUE);
  599. return (-1);
  600. }
  601. /* --------------------------------------------------------------------------------------------- */
  602. off_t
  603. mc_lseek (int fd, off_t offset, int whence)
  604. {
  605. struct vfs_class *vfs;
  606. void *fsinfo = NULL;
  607. off_t result;
  608. if (fd == -1)
  609. return (-1);
  610. vfs = vfs_class_find_by_handle (fd, &fsinfo);
  611. if (vfs == NULL)
  612. return (-1);
  613. result = vfs->lseek ? vfs->lseek (fsinfo, offset, whence) : -1;
  614. if (result == -1)
  615. errno = vfs->lseek ? vfs_ferrno (vfs) : ENOTSUP;
  616. return result;
  617. }
  618. /* --------------------------------------------------------------------------------------------- */
  619. /* Following code heavily borrows from libiberty, mkstemps.c */
  620. /*
  621. * Arguments:
  622. * pname (output) - pointer to the name of the temp file (needs g_free).
  623. * NULL if the function fails.
  624. * prefix - part of the filename before the random part.
  625. * Prepend $TMPDIR or /tmp if there are no path separators.
  626. * suffix - if not NULL, part of the filename after the random part.
  627. *
  628. * Result:
  629. * handle of the open file or -1 if couldn't open any.
  630. */
  631. int
  632. mc_mkstemps (vfs_path_t ** pname_vpath, const char *prefix, const char *suffix)
  633. {
  634. char *p1, *p2;
  635. int fd;
  636. if (strchr (prefix, PATH_SEP) != NULL)
  637. p1 = g_strdup (prefix);
  638. else
  639. {
  640. /* Add prefix first to find the position of XXXXXX */
  641. p1 = g_build_filename (mc_tmpdir (), prefix, (char *) NULL);
  642. }
  643. p2 = g_strconcat (p1, "XXXXXX", suffix, (char *) NULL);
  644. g_free (p1);
  645. fd = g_mkstemp (p2);
  646. if (fd >= 0)
  647. *pname_vpath = vfs_path_from_str (p2);
  648. else
  649. {
  650. *pname_vpath = NULL;
  651. fd = -1;
  652. }
  653. g_free (p2);
  654. return fd;
  655. }
  656. /* --------------------------------------------------------------------------------------------- */
  657. /**
  658. * Return the directory where mc should keep its temporary files.
  659. * This directory is (in Bourne shell terms) "${TMPDIR=/tmp}/mc-$USER"
  660. * When called the first time, the directory is created if needed.
  661. * The first call should be done early, since we are using fprintf()
  662. * and not message() to report possible problems.
  663. */
  664. const char *
  665. mc_tmpdir (void)
  666. {
  667. static char buffer[PATH_MAX];
  668. static const char *tmpdir = NULL;
  669. const char *sys_tmp;
  670. struct passwd *pwd;
  671. struct stat st;
  672. const char *error = NULL;
  673. /* Check if already correctly initialized */
  674. if (tmpdir != NULL && lstat (tmpdir, &st) == 0 && S_ISDIR (st.st_mode) &&
  675. st.st_uid == getuid () && (st.st_mode & 0777) == 0700)
  676. return tmpdir;
  677. sys_tmp = getenv ("MC_TMPDIR");
  678. if (sys_tmp == NULL || !IS_PATH_SEP (sys_tmp[0]))
  679. {
  680. sys_tmp = getenv ("TMPDIR");
  681. if (sys_tmp == NULL || !IS_PATH_SEP (sys_tmp[0]))
  682. sys_tmp = TMPDIR_DEFAULT;
  683. }
  684. pwd = getpwuid (getuid ());
  685. if (pwd != NULL)
  686. g_snprintf (buffer, sizeof (buffer), "%s/mc-%s", sys_tmp, pwd->pw_name);
  687. else
  688. g_snprintf (buffer, sizeof (buffer), "%s/mc-%lu", sys_tmp, (unsigned long) getuid ());
  689. canonicalize_pathname (buffer);
  690. /* Try to create directory */
  691. if (mkdir (buffer, S_IRWXU) != 0)
  692. {
  693. if (errno == EEXIST && lstat (buffer, &st) == 0)
  694. {
  695. /* Sanity check for existing directory */
  696. if (!S_ISDIR (st.st_mode))
  697. error = _("%s is not a directory\n");
  698. else if (st.st_uid != getuid ())
  699. error = _("Directory %s is not owned by you\n");
  700. else if (((st.st_mode & 0777) != 0700) && (chmod (buffer, 0700) != 0))
  701. error = _("Cannot set correct permissions for directory %s\n");
  702. }
  703. else
  704. {
  705. fprintf (stderr,
  706. _("Cannot create temporary directory %s: %s\n"),
  707. buffer, unix_error_string (errno));
  708. error = "";
  709. }
  710. }
  711. if (error != NULL)
  712. {
  713. int test_fd;
  714. char *fallback_prefix;
  715. gboolean fallback_ok = FALSE;
  716. vfs_path_t *test_vpath;
  717. if (*error != '\0')
  718. fprintf (stderr, error, buffer);
  719. /* Test if sys_tmp is suitable for temporary files */
  720. fallback_prefix = g_strdup_printf ("%s/mctest", sys_tmp);
  721. test_fd = mc_mkstemps (&test_vpath, fallback_prefix, NULL);
  722. g_free (fallback_prefix);
  723. if (test_fd != -1)
  724. {
  725. close (test_fd);
  726. test_fd = open (vfs_path_as_str (test_vpath), O_RDONLY);
  727. if (test_fd != -1)
  728. {
  729. close (test_fd);
  730. unlink (vfs_path_as_str (test_vpath));
  731. fallback_ok = TRUE;
  732. }
  733. }
  734. if (fallback_ok)
  735. {
  736. fprintf (stderr, _("Temporary files will be created in %s\n"), sys_tmp);
  737. g_snprintf (buffer, sizeof (buffer), "%s", sys_tmp);
  738. error = NULL;
  739. }
  740. else
  741. {
  742. fprintf (stderr, _("Temporary files will not be created\n"));
  743. g_snprintf (buffer, sizeof (buffer), "%s", "/dev/null/");
  744. }
  745. vfs_path_free (test_vpath, TRUE);
  746. fprintf (stderr, "%s\n", _("Press any key to continue..."));
  747. getc (stdin);
  748. }
  749. tmpdir = buffer;
  750. if (error == NULL)
  751. g_setenv ("MC_TMPDIR", tmpdir, TRUE);
  752. return tmpdir;
  753. }
  754. /* --------------------------------------------------------------------------------------------- */