interface.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  1. /*
  2. Virtual File System: interface functions
  3. Copyright (C) 2011-2024
  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. /*** forward declarations (file scope functions) *************************************************/
  57. /*** file scope variables ************************************************************************/
  58. /* --------------------------------------------------------------------------------------------- */
  59. /*** file scope functions ************************************************************************/
  60. /* --------------------------------------------------------------------------------------------- */
  61. static vfs_path_t *
  62. mc_def_getlocalcopy (const vfs_path_t *filename_vpath)
  63. {
  64. vfs_path_t *tmp_vpath = NULL;
  65. int fdin, fdout = -1;
  66. ssize_t i;
  67. char buffer[BUF_1K * 8];
  68. struct stat mystat;
  69. fdin = mc_open (filename_vpath, O_RDONLY | O_LINEAR);
  70. if (fdin == -1)
  71. goto fail;
  72. fdout = vfs_mkstemps (&tmp_vpath, "vfs", vfs_path_get_last_path_str (filename_vpath));
  73. if (fdout == -1)
  74. goto fail;
  75. while ((i = mc_read (fdin, buffer, sizeof (buffer))) > 0)
  76. {
  77. if (write (fdout, buffer, i) != i)
  78. goto fail;
  79. }
  80. if (i == -1)
  81. goto fail;
  82. i = mc_close (fdin);
  83. fdin = -1;
  84. if (i == -1)
  85. goto fail;
  86. i = close (fdout);
  87. fdout = -1;
  88. if (i == -1)
  89. goto fail;
  90. if (mc_stat (filename_vpath, &mystat) != -1)
  91. mc_chmod (tmp_vpath, mystat.st_mode);
  92. return tmp_vpath;
  93. fail:
  94. vfs_path_free (tmp_vpath, TRUE);
  95. if (fdout != -1)
  96. close (fdout);
  97. if (fdin != -1)
  98. mc_close (fdin);
  99. return NULL;
  100. }
  101. /* --------------------------------------------------------------------------------------------- */
  102. static int
  103. mc_def_ungetlocalcopy (const vfs_path_t *filename_vpath, const vfs_path_t *local_vpath,
  104. gboolean has_changed)
  105. {
  106. int fdin = -1, fdout = -1;
  107. const char *local;
  108. local = vfs_path_get_last_path_str (local_vpath);
  109. if (has_changed)
  110. {
  111. char buffer[BUF_1K * 8];
  112. ssize_t i;
  113. if (vfs_path_get_last_path_vfs (filename_vpath)->write == NULL)
  114. goto failed;
  115. fdin = open (local, O_RDONLY);
  116. if (fdin == -1)
  117. goto failed;
  118. fdout = mc_open (filename_vpath, O_WRONLY | O_TRUNC);
  119. if (fdout == -1)
  120. goto failed;
  121. while ((i = read (fdin, buffer, sizeof (buffer))) > 0)
  122. if (mc_write (fdout, buffer, (size_t) i) != i)
  123. goto failed;
  124. if (i == -1)
  125. goto failed;
  126. if (close (fdin) == -1)
  127. {
  128. fdin = -1;
  129. goto failed;
  130. }
  131. fdin = -1;
  132. if (mc_close (fdout) == -1)
  133. {
  134. fdout = -1;
  135. goto failed;
  136. }
  137. }
  138. unlink (local);
  139. return 0;
  140. failed:
  141. message (D_ERROR, _ ("Changes to file lost"), "%s",
  142. vfs_path_get_last_path_str (filename_vpath));
  143. if (fdout != -1)
  144. mc_close (fdout);
  145. if (fdin != -1)
  146. close (fdin);
  147. unlink (local);
  148. return (-1);
  149. }
  150. /* --------------------------------------------------------------------------------------------- */
  151. /*** public functions ****************************************************************************/
  152. /* --------------------------------------------------------------------------------------------- */
  153. int
  154. mc_open (const vfs_path_t *vpath, int flags, ...)
  155. {
  156. int result = -1;
  157. mode_t mode = 0;
  158. struct vfs_class *me;
  159. if (vpath == NULL)
  160. return (-1);
  161. /* Get the mode flag */
  162. if ((flags & O_CREAT) != 0)
  163. {
  164. va_list ap;
  165. va_start (ap, flags);
  166. /* We have to use PROMOTED_MODE_T instead of mode_t. Doing 'va_arg (ap, mode_t)'
  167. * fails on systems where 'mode_t' is smaller than 'int' because of C's "default
  168. * argument promotions". */
  169. mode = va_arg (ap, PROMOTED_MODE_T);
  170. va_end (ap);
  171. }
  172. me = VFS_CLASS (vfs_path_get_last_path_vfs (vpath));
  173. if (me != NULL && me->open != NULL)
  174. {
  175. void *info;
  176. /* open must be supported */
  177. info = me->open (vpath, flags, mode);
  178. if (info == NULL)
  179. errno = vfs_ferrno (me);
  180. else
  181. result = vfs_new_handle (me, info);
  182. }
  183. else
  184. errno = ENOTSUP;
  185. return result;
  186. }
  187. /* --------------------------------------------------------------------------------------------- */
  188. /* *INDENT-OFF* */
  189. #define MC_NAMEOP(name, inarg, callarg) \
  190. int mc_##name inarg \
  191. { \
  192. int result; \
  193. struct vfs_class *me; \
  194. \
  195. if (vpath == NULL) \
  196. return (-1); \
  197. \
  198. me = VFS_CLASS (vfs_path_get_last_path_vfs (vpath)); \
  199. if (me == NULL) \
  200. return (-1); \
  201. \
  202. result = me->name != NULL ? me->name callarg : -1; \
  203. if (result == -1) \
  204. errno = me->name != NULL ? vfs_ferrno (me) : ENOTSUP; \
  205. return result; \
  206. }
  207. MC_NAMEOP (chmod, (const vfs_path_t *vpath, mode_t mode), (vpath, mode))
  208. MC_NAMEOP (chown, (const vfs_path_t *vpath, uid_t owner, gid_t group), (vpath, owner, group))
  209. MC_NAMEOP (fgetflags, (const vfs_path_t *vpath, unsigned long *flags), (vpath, flags))
  210. MC_NAMEOP (fsetflags, (const vfs_path_t *vpath, unsigned long flags), (vpath, flags))
  211. MC_NAMEOP (utime, (const vfs_path_t *vpath, mc_timesbuf_t *times), (vpath, times))
  212. MC_NAMEOP (readlink, (const vfs_path_t *vpath, char *buf, size_t bufsiz), (vpath, buf, bufsiz))
  213. MC_NAMEOP (unlink, (const vfs_path_t *vpath), (vpath))
  214. MC_NAMEOP (mkdir, (const vfs_path_t *vpath, mode_t mode), (vpath, mode))
  215. MC_NAMEOP (rmdir, (const vfs_path_t *vpath), (vpath))
  216. MC_NAMEOP (mknod, (const vfs_path_t *vpath, mode_t mode, dev_t dev), (vpath, mode, dev))
  217. /* *INDENT-ON* */
  218. /* --------------------------------------------------------------------------------------------- */
  219. int
  220. mc_symlink (const vfs_path_t *vpath1, const vfs_path_t *vpath2)
  221. {
  222. int result = -1;
  223. if (vpath1 != NULL && vpath2 != NULL)
  224. {
  225. struct vfs_class *me;
  226. me = VFS_CLASS (vfs_path_get_last_path_vfs (vpath2));
  227. if (me != NULL)
  228. {
  229. result = me->symlink != NULL ? me->symlink (vpath1, vpath2) : -1;
  230. if (result == -1)
  231. errno = me->symlink != NULL ? vfs_ferrno (me) : ENOTSUP;
  232. }
  233. }
  234. return result;
  235. }
  236. /* --------------------------------------------------------------------------------------------- */
  237. /* *INDENT-OFF* */
  238. #define MC_HANDLEOP(rettype, name, inarg, callarg) \
  239. rettype mc_##name inarg \
  240. { \
  241. struct vfs_class *vfs; \
  242. void *fsinfo = NULL; \
  243. rettype result; \
  244. \
  245. if (handle == -1) \
  246. return (-1); \
  247. \
  248. vfs = vfs_class_find_by_handle (handle, &fsinfo); \
  249. if (vfs == NULL) \
  250. return (-1); \
  251. \
  252. result = vfs->name != NULL ? vfs->name callarg : -1; \
  253. if (result == -1) \
  254. errno = vfs->name != NULL ? vfs_ferrno (vfs) : ENOTSUP; \
  255. return result; \
  256. }
  257. MC_HANDLEOP (ssize_t, read, (int handle, void *buf, size_t count), (fsinfo, buf, count))
  258. MC_HANDLEOP (ssize_t, write, (int handle, const void *buf, size_t count), (fsinfo, buf, count))
  259. MC_HANDLEOP (int, fstat, (int handle, struct stat *buf), (fsinfo, buf))
  260. /* --------------------------------------------------------------------------------------------- */
  261. #define MC_RENAMEOP(name) \
  262. int mc_##name (const vfs_path_t *vpath1, const vfs_path_t *vpath2) \
  263. { \
  264. int result; \
  265. struct vfs_class *me1, *me2; \
  266. \
  267. if (vpath1 == NULL || vpath2 == NULL) \
  268. return (-1); \
  269. \
  270. me1 = VFS_CLASS (vfs_path_get_last_path_vfs (vpath1)); \
  271. me2 = VFS_CLASS (vfs_path_get_last_path_vfs (vpath2)); \
  272. \
  273. if (me1 == NULL || me2 == NULL || me1 != me2) \
  274. { \
  275. errno = EXDEV; \
  276. return (-1); \
  277. } \
  278. \
  279. result = me1->name != NULL ? me1->name (vpath1, vpath2) : -1; \
  280. if (result == -1) \
  281. errno = me1->name != NULL ? vfs_ferrno (me1) : ENOTSUP; \
  282. return result; \
  283. }
  284. MC_RENAMEOP (link)
  285. MC_RENAMEOP (rename)
  286. /* *INDENT-ON* */
  287. /* --------------------------------------------------------------------------------------------- */
  288. int
  289. mc_ctl (int handle, int ctlop, void *arg)
  290. {
  291. struct vfs_class *vfs;
  292. void *fsinfo = NULL;
  293. vfs = vfs_class_find_by_handle (handle, &fsinfo);
  294. return (vfs == NULL || vfs->ctl == NULL) ? 0 : vfs->ctl (fsinfo, ctlop, arg);
  295. }
  296. /* --------------------------------------------------------------------------------------------- */
  297. int
  298. mc_setctl (const vfs_path_t *vpath, int ctlop, void *arg)
  299. {
  300. int result = -1;
  301. struct vfs_class *me;
  302. if (vpath == NULL)
  303. vfs_die ("You don't want to pass NULL to mc_setctl.");
  304. me = VFS_CLASS (vfs_path_get_last_path_vfs (vpath));
  305. if (me != NULL)
  306. result = me->setctl != NULL ? me->setctl (vpath, ctlop, arg) : 0;
  307. return result;
  308. }
  309. /* --------------------------------------------------------------------------------------------- */
  310. int
  311. mc_close (int handle)
  312. {
  313. struct vfs_class *vfs;
  314. void *fsinfo = NULL;
  315. int result;
  316. if (handle == -1)
  317. return (-1);
  318. vfs = vfs_class_find_by_handle (handle, &fsinfo);
  319. if (vfs == NULL || fsinfo == NULL)
  320. return (-1);
  321. if (handle < 3)
  322. return close (handle);
  323. if (vfs->close == NULL)
  324. vfs_die ("VFS must support close.\n");
  325. result = vfs->close (fsinfo);
  326. vfs_free_handle (handle);
  327. if (result == -1)
  328. errno = vfs_ferrno (vfs);
  329. return result;
  330. }
  331. /* --------------------------------------------------------------------------------------------- */
  332. DIR *
  333. mc_opendir (const vfs_path_t *vpath)
  334. {
  335. int handle, *handlep;
  336. void *info;
  337. vfs_path_element_t *path_element;
  338. if (vpath == NULL)
  339. return NULL;
  340. path_element = (vfs_path_element_t *) vfs_path_get_by_index (vpath, -1);
  341. if (!vfs_path_element_valid (path_element))
  342. {
  343. errno = ENOTSUP;
  344. return NULL;
  345. }
  346. info = path_element->class->opendir ? path_element->class->opendir (vpath) : NULL;
  347. if (info == NULL)
  348. {
  349. errno = path_element->class->opendir ? vfs_ferrno (path_element->class) : ENOTSUP;
  350. return NULL;
  351. }
  352. path_element->dir.info = info;
  353. #ifdef HAVE_CHARSET
  354. path_element->dir.converter = (path_element->encoding != NULL)
  355. ? str_crt_conv_from (path_element->encoding)
  356. : str_cnv_from_term;
  357. if (path_element->dir.converter == INVALID_CONV)
  358. path_element->dir.converter = str_cnv_from_term;
  359. #endif
  360. handle = vfs_new_handle (path_element->class, vfs_path_element_clone (path_element));
  361. handlep = g_new (int, 1);
  362. *handlep = handle;
  363. return (DIR *) handlep;
  364. }
  365. /* --------------------------------------------------------------------------------------------- */
  366. struct vfs_dirent *
  367. mc_readdir (DIR *dirp)
  368. {
  369. int handle;
  370. struct vfs_class *vfs;
  371. void *fsinfo = NULL;
  372. struct vfs_dirent *entry = NULL;
  373. vfs_path_element_t *vfs_path_element;
  374. if (dirp == NULL)
  375. {
  376. errno = EFAULT;
  377. return NULL;
  378. }
  379. handle = *(int *) dirp;
  380. vfs = vfs_class_find_by_handle (handle, &fsinfo);
  381. if (vfs == NULL || fsinfo == NULL)
  382. return NULL;
  383. vfs_path_element = (vfs_path_element_t *) fsinfo;
  384. if (vfs->readdir != NULL)
  385. {
  386. entry = vfs->readdir (vfs_path_element->dir.info);
  387. if (entry == NULL)
  388. return NULL;
  389. g_string_set_size (vfs_str_buffer, 0);
  390. #ifdef HAVE_CHARSET
  391. str_vfs_convert_from (vfs_path_element->dir.converter, entry->d_name, vfs_str_buffer);
  392. #else
  393. g_string_append_len (vfs_str_buffer, entry->d_name, entry->d_len);
  394. #endif
  395. vfs_dirent_assign (mc_readdir_result, vfs_str_buffer->str, entry->d_ino);
  396. vfs_dirent_free (entry);
  397. }
  398. if (entry == NULL)
  399. errno = vfs->readdir ? vfs_ferrno (vfs) : ENOTSUP;
  400. return (entry != NULL) ? mc_readdir_result : NULL;
  401. }
  402. /* --------------------------------------------------------------------------------------------- */
  403. int
  404. mc_closedir (DIR *dirp)
  405. {
  406. int handle;
  407. struct vfs_class *vfs;
  408. void *fsinfo = NULL;
  409. int result = -1;
  410. if (dirp == NULL)
  411. return result;
  412. handle = *(int *) dirp;
  413. vfs = vfs_class_find_by_handle (handle, &fsinfo);
  414. if (vfs != NULL && fsinfo != NULL)
  415. {
  416. vfs_path_element_t *vfs_path_element = (vfs_path_element_t *) fsinfo;
  417. #ifdef HAVE_CHARSET
  418. if (vfs_path_element->dir.converter != str_cnv_from_term)
  419. {
  420. str_close_conv (vfs_path_element->dir.converter);
  421. vfs_path_element->dir.converter = INVALID_CONV;
  422. }
  423. #endif
  424. result = vfs->closedir ? (*vfs->closedir) (vfs_path_element->dir.info) : -1;
  425. vfs_free_handle (handle);
  426. vfs_path_element_free (vfs_path_element);
  427. }
  428. g_free (dirp);
  429. return result;
  430. }
  431. /* --------------------------------------------------------------------------------------------- */
  432. /* *INDENT-OFF* */
  433. #define MC_STATOP(name) \
  434. int mc_##name (const vfs_path_t *vpath, struct stat *buf) \
  435. { \
  436. int result = -1; \
  437. struct vfs_class *me; \
  438. \
  439. if (vpath == NULL) \
  440. return (-1); \
  441. \
  442. me = VFS_CLASS (vfs_path_get_last_path_vfs (vpath)); \
  443. if (me != NULL) \
  444. { \
  445. result = me->name ? me->name (vpath, buf) : -1; \
  446. if (result == -1) \
  447. errno = me->name ? vfs_ferrno (me) : ENOTSUP; \
  448. } \
  449. \
  450. return result; \
  451. }
  452. MC_STATOP (stat)
  453. MC_STATOP (lstat)
  454. /* --------------------------------------------------------------------------------------------- */
  455. vfs_path_t *
  456. mc_getlocalcopy (const vfs_path_t *pathname_vpath)
  457. {
  458. vfs_path_t *result = NULL;
  459. struct vfs_class *me;
  460. if (pathname_vpath == NULL)
  461. return NULL;
  462. me = VFS_CLASS (vfs_path_get_last_path_vfs (pathname_vpath));
  463. if (me != NULL)
  464. {
  465. result = me->getlocalcopy != NULL ? me->getlocalcopy (pathname_vpath)
  466. : mc_def_getlocalcopy (pathname_vpath);
  467. if (result == NULL)
  468. errno = vfs_ferrno (me);
  469. }
  470. return result;
  471. }
  472. /* --------------------------------------------------------------------------------------------- */
  473. int
  474. mc_ungetlocalcopy (const vfs_path_t *pathname_vpath, const vfs_path_t *local_vpath,
  475. gboolean has_changed)
  476. {
  477. int result = -1;
  478. const struct vfs_class *me;
  479. if (pathname_vpath == NULL)
  480. return (-1);
  481. me = vfs_path_get_last_path_vfs (pathname_vpath);
  482. if (me != NULL)
  483. result = me->ungetlocalcopy != NULL
  484. ? me->ungetlocalcopy (pathname_vpath, local_vpath, has_changed)
  485. : mc_def_ungetlocalcopy (pathname_vpath, local_vpath, has_changed);
  486. return result;
  487. }
  488. /* --------------------------------------------------------------------------------------------- */
  489. /**
  490. * VFS chdir.
  491. *
  492. * @param vpath VFS path.
  493. * May be NULL. In this case NULL is returned and errno set to 0.
  494. *
  495. * @return 0 on success, -1 on failure.
  496. */
  497. int
  498. mc_chdir (const vfs_path_t *vpath)
  499. {
  500. struct vfs_class *old_vfs;
  501. vfsid old_vfsid;
  502. int result;
  503. struct vfs_class *me;
  504. const vfs_path_element_t *path_element;
  505. vfs_path_t *cd_vpath;
  506. if (vpath == NULL)
  507. {
  508. errno = 0;
  509. return (-1);
  510. }
  511. if (vpath->relative)
  512. cd_vpath = vfs_path_to_absolute (vpath);
  513. else
  514. cd_vpath = vfs_path_clone (vpath);
  515. me = VFS_CLASS (vfs_path_get_last_path_vfs (cd_vpath));
  516. if (me == NULL)
  517. {
  518. errno = EINVAL;
  519. goto error_end;
  520. }
  521. if (me->chdir == NULL)
  522. {
  523. errno = ENOTSUP;
  524. goto error_end;
  525. }
  526. result = me->chdir (cd_vpath);
  527. if (result == -1)
  528. {
  529. errno = vfs_ferrno (me);
  530. goto error_end;
  531. }
  532. old_vfsid = vfs_getid (vfs_get_raw_current_dir ());
  533. old_vfs = current_vfs;
  534. /* Actually change directory */
  535. vfs_set_raw_current_dir (cd_vpath);
  536. current_vfs = me;
  537. /* This function uses the new current_dir implicitly */
  538. vfs_stamp_create (old_vfs, old_vfsid);
  539. /* Sometimes we assume no trailing slash on cwd */
  540. path_element = vfs_path_get_by_index (vfs_get_raw_current_dir (), -1);
  541. if (vfs_path_element_valid (path_element))
  542. {
  543. if (*path_element->path != '\0')
  544. {
  545. char *p;
  546. p = strchr (path_element->path, 0) - 1;
  547. if (IS_PATH_SEP (*p) && p > path_element->path)
  548. *p = '\0';
  549. }
  550. #ifdef ENABLE_VFS_NET
  551. {
  552. struct vfs_s_super *super;
  553. super = vfs_get_super_by_vpath (vpath);
  554. if (super != NULL && super->path_element != NULL)
  555. {
  556. g_free (super->path_element->path);
  557. super->path_element->path = g_strdup (path_element->path);
  558. }
  559. }
  560. #endif /* ENABLE_VFS_NET */
  561. }
  562. return 0;
  563. error_end:
  564. vfs_path_free (cd_vpath, TRUE);
  565. return (-1);
  566. }
  567. /* --------------------------------------------------------------------------------------------- */
  568. off_t
  569. mc_lseek (int fd, off_t offset, int whence)
  570. {
  571. struct vfs_class *vfs;
  572. void *fsinfo = NULL;
  573. off_t result;
  574. if (fd == -1)
  575. return (-1);
  576. vfs = vfs_class_find_by_handle (fd, &fsinfo);
  577. if (vfs == NULL)
  578. return (-1);
  579. result = vfs->lseek ? vfs->lseek (fsinfo, offset, whence) : -1;
  580. if (result == -1)
  581. errno = vfs->lseek ? vfs_ferrno (vfs) : ENOTSUP;
  582. return result;
  583. }
  584. /* --------------------------------------------------------------------------------------------- */
  585. /* Following code heavily borrows from libiberty, mkstemps.c */
  586. /*
  587. * Arguments:
  588. * pname (output) - pointer to the name of the temp file (needs g_free).
  589. * NULL if the function fails.
  590. * prefix - part of the filename before the random part.
  591. * Prepend $TMPDIR or /tmp if there are no path separators.
  592. * suffix - if not NULL, part of the filename after the random part.
  593. *
  594. * Result:
  595. * handle of the open file or -1 if couldn't open any.
  596. */
  597. int
  598. mc_mkstemps (vfs_path_t **pname_vpath, const char *prefix, const char *suffix)
  599. {
  600. char *p1, *p2;
  601. int fd;
  602. if (strchr (prefix, PATH_SEP) != NULL)
  603. p1 = g_strdup (prefix);
  604. else
  605. {
  606. /* Add prefix first to find the position of XXXXXX */
  607. p1 = g_build_filename (mc_tmpdir (), prefix, (char *) NULL);
  608. }
  609. p2 = g_strconcat (p1, "XXXXXX", suffix, (char *) NULL);
  610. g_free (p1);
  611. fd = g_mkstemp (p2);
  612. if (fd >= 0)
  613. *pname_vpath = vfs_path_from_str (p2);
  614. else
  615. {
  616. *pname_vpath = NULL;
  617. fd = -1;
  618. }
  619. g_free (p2);
  620. return fd;
  621. }
  622. /* --------------------------------------------------------------------------------------------- */
  623. /**
  624. * Return the directory where mc should keep its temporary files.
  625. * This directory is (in Bourne shell terms) "${TMPDIR=/tmp}/mc-XXXXXX"
  626. * When called the first time, the directory is created if needed.
  627. * The first call should be done early, since we are using fprintf()
  628. * and not message() to report possible problems.
  629. */
  630. const char *
  631. mc_tmpdir (void)
  632. {
  633. static char buffer[PATH_MAX];
  634. static const char *tmpdir = NULL;
  635. const char *sys_tmp;
  636. struct stat st;
  637. gchar *template;
  638. /* Check if already correctly initialized */
  639. if (tmpdir != NULL && lstat (tmpdir, &st) == 0 && S_ISDIR (st.st_mode) && st.st_uid == getuid ()
  640. && (st.st_mode & 0777) == 0700)
  641. return tmpdir;
  642. sys_tmp = getenv ("MC_TMPDIR");
  643. if (sys_tmp == NULL || !IS_PATH_SEP (sys_tmp[0]))
  644. {
  645. sys_tmp = getenv ("TMPDIR");
  646. if (sys_tmp == NULL || !IS_PATH_SEP (sys_tmp[0]))
  647. sys_tmp = TMPDIR_DEFAULT;
  648. }
  649. template = g_build_filename (sys_tmp, "mc-XXXXXX", (char *) NULL);
  650. g_strlcpy (buffer, template, sizeof (buffer));
  651. g_free (template);
  652. tmpdir = g_mkdtemp (buffer);
  653. if (tmpdir != NULL)
  654. g_setenv ("MC_TMPDIR", tmpdir, TRUE);
  655. else
  656. {
  657. fprintf (stderr,
  658. _ ("Cannot create temporary directory %s: %s.\n"
  659. "Temporary files will not be created\n"),
  660. buffer, unix_error_string (errno));
  661. g_snprintf (buffer, sizeof (buffer), "%s", "/dev/null/");
  662. fprintf (stderr, "%s\n", _ ("Press any key to continue..."));
  663. getc (stdin);
  664. }
  665. return tmpdir;
  666. }
  667. /* --------------------------------------------------------------------------------------------- */