interface.c 22 KB

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