interface.c 25 KB

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