interface.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  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. /* *INDENT-ON* */
  453. /* --------------------------------------------------------------------------------------------- */
  454. vfs_path_t *
  455. mc_getlocalcopy (const vfs_path_t *pathname_vpath)
  456. {
  457. vfs_path_t *result = NULL;
  458. struct vfs_class *me;
  459. if (pathname_vpath == NULL)
  460. return NULL;
  461. me = VFS_CLASS (vfs_path_get_last_path_vfs (pathname_vpath));
  462. if (me != NULL)
  463. {
  464. result = me->getlocalcopy != NULL ?
  465. me->getlocalcopy (pathname_vpath) : mc_def_getlocalcopy (pathname_vpath);
  466. if (result == NULL)
  467. errno = vfs_ferrno (me);
  468. }
  469. return result;
  470. }
  471. /* --------------------------------------------------------------------------------------------- */
  472. int
  473. mc_ungetlocalcopy (const vfs_path_t *pathname_vpath, const vfs_path_t *local_vpath,
  474. gboolean has_changed)
  475. {
  476. int result = -1;
  477. const struct vfs_class *me;
  478. if (pathname_vpath == NULL)
  479. return (-1);
  480. me = vfs_path_get_last_path_vfs (pathname_vpath);
  481. if (me != NULL)
  482. result = me->ungetlocalcopy != NULL ?
  483. me->ungetlocalcopy (pathname_vpath, local_vpath, has_changed) :
  484. mc_def_ungetlocalcopy (pathname_vpath, local_vpath, has_changed);
  485. return result;
  486. }
  487. /* --------------------------------------------------------------------------------------------- */
  488. /**
  489. * VFS chdir.
  490. *
  491. * @param vpath VFS path.
  492. * May be NULL. In this case NULL is returned and errno set to 0.
  493. *
  494. * @return 0 on success, -1 on failure.
  495. */
  496. int
  497. mc_chdir (const vfs_path_t *vpath)
  498. {
  499. struct vfs_class *old_vfs;
  500. vfsid old_vfsid;
  501. int result;
  502. struct vfs_class *me;
  503. const vfs_path_element_t *path_element;
  504. vfs_path_t *cd_vpath;
  505. if (vpath == NULL)
  506. {
  507. errno = 0;
  508. return (-1);
  509. }
  510. if (vpath->relative)
  511. cd_vpath = vfs_path_to_absolute (vpath);
  512. else
  513. cd_vpath = vfs_path_clone (vpath);
  514. me = VFS_CLASS (vfs_path_get_last_path_vfs (cd_vpath));
  515. if (me == NULL)
  516. {
  517. errno = EINVAL;
  518. goto error_end;
  519. }
  520. if (me->chdir == NULL)
  521. {
  522. errno = ENOTSUP;
  523. goto error_end;
  524. }
  525. result = me->chdir (cd_vpath);
  526. if (result == -1)
  527. {
  528. errno = vfs_ferrno (me);
  529. goto error_end;
  530. }
  531. old_vfsid = vfs_getid (vfs_get_raw_current_dir ());
  532. old_vfs = current_vfs;
  533. /* Actually change directory */
  534. vfs_set_raw_current_dir (cd_vpath);
  535. current_vfs = me;
  536. /* This function uses the new current_dir implicitly */
  537. vfs_stamp_create (old_vfs, old_vfsid);
  538. /* Sometimes we assume no trailing slash on cwd */
  539. path_element = vfs_path_get_by_index (vfs_get_raw_current_dir (), -1);
  540. if (vfs_path_element_valid (path_element))
  541. {
  542. if (*path_element->path != '\0')
  543. {
  544. char *p;
  545. p = strchr (path_element->path, 0) - 1;
  546. if (IS_PATH_SEP (*p) && p > path_element->path)
  547. *p = '\0';
  548. }
  549. #ifdef ENABLE_VFS_NET
  550. {
  551. struct vfs_s_super *super;
  552. super = vfs_get_super_by_vpath (vpath);
  553. if (super != NULL && super->path_element != NULL)
  554. {
  555. g_free (super->path_element->path);
  556. super->path_element->path = g_strdup (path_element->path);
  557. }
  558. }
  559. #endif /* ENABLE_VFS_NET */
  560. }
  561. return 0;
  562. error_end:
  563. vfs_path_free (cd_vpath, TRUE);
  564. return (-1);
  565. }
  566. /* --------------------------------------------------------------------------------------------- */
  567. off_t
  568. mc_lseek (int fd, off_t offset, int whence)
  569. {
  570. struct vfs_class *vfs;
  571. void *fsinfo = NULL;
  572. off_t result;
  573. if (fd == -1)
  574. return (-1);
  575. vfs = vfs_class_find_by_handle (fd, &fsinfo);
  576. if (vfs == NULL)
  577. return (-1);
  578. result = vfs->lseek ? vfs->lseek (fsinfo, offset, whence) : -1;
  579. if (result == -1)
  580. errno = vfs->lseek ? vfs_ferrno (vfs) : ENOTSUP;
  581. return result;
  582. }
  583. /* --------------------------------------------------------------------------------------------- */
  584. /* Following code heavily borrows from libiberty, mkstemps.c */
  585. /*
  586. * Arguments:
  587. * pname (output) - pointer to the name of the temp file (needs g_free).
  588. * NULL if the function fails.
  589. * prefix - part of the filename before the random part.
  590. * Prepend $TMPDIR or /tmp if there are no path separators.
  591. * suffix - if not NULL, part of the filename after the random part.
  592. *
  593. * Result:
  594. * handle of the open file or -1 if couldn't open any.
  595. */
  596. int
  597. mc_mkstemps (vfs_path_t **pname_vpath, const char *prefix, const char *suffix)
  598. {
  599. char *p1, *p2;
  600. int fd;
  601. if (strchr (prefix, PATH_SEP) != NULL)
  602. p1 = g_strdup (prefix);
  603. else
  604. {
  605. /* Add prefix first to find the position of XXXXXX */
  606. p1 = g_build_filename (mc_tmpdir (), prefix, (char *) NULL);
  607. }
  608. p2 = g_strconcat (p1, "XXXXXX", suffix, (char *) NULL);
  609. g_free (p1);
  610. fd = g_mkstemp (p2);
  611. if (fd >= 0)
  612. *pname_vpath = vfs_path_from_str (p2);
  613. else
  614. {
  615. *pname_vpath = NULL;
  616. fd = -1;
  617. }
  618. g_free (p2);
  619. return fd;
  620. }
  621. /* --------------------------------------------------------------------------------------------- */
  622. /**
  623. * Return the directory where mc should keep its temporary files.
  624. * This directory is (in Bourne shell terms) "${TMPDIR=/tmp}/mc-XXXXXX"
  625. * When called the first time, the directory is created if needed.
  626. * The first call should be done early, since we are using fprintf()
  627. * and not message() to report possible problems.
  628. */
  629. const char *
  630. mc_tmpdir (void)
  631. {
  632. static char buffer[PATH_MAX];
  633. static const char *tmpdir = NULL;
  634. const char *sys_tmp;
  635. gchar *template;
  636. /* Check if already correctly initialized */
  637. if (tmpdir != NULL)
  638. {
  639. struct stat st;
  640. if (lstat (tmpdir, &st) == 0 && S_ISDIR (st.st_mode) && st.st_uid == getuid ()
  641. && (st.st_mode & 0777) == 0700)
  642. return tmpdir;
  643. }
  644. sys_tmp = getenv ("MC_TMPDIR");
  645. if (sys_tmp == NULL || !IS_PATH_SEP (sys_tmp[0]))
  646. {
  647. sys_tmp = getenv ("TMPDIR");
  648. if (sys_tmp == NULL || !IS_PATH_SEP (sys_tmp[0]))
  649. sys_tmp = TMPDIR_DEFAULT;
  650. }
  651. template = g_build_filename (sys_tmp, "mc-XXXXXX", (char *) NULL);
  652. g_strlcpy (buffer, template, sizeof (buffer));
  653. g_free (template);
  654. tmpdir = g_mkdtemp (buffer);
  655. if (tmpdir != NULL)
  656. g_setenv ("MC_TMPDIR", tmpdir, TRUE);
  657. else
  658. {
  659. fprintf (stderr, _("Cannot create temporary directory %s: %s.\n"
  660. "Temporary files will not be created\n"), buffer,
  661. unix_error_string (errno));
  662. g_snprintf (buffer, sizeof (buffer), "%s", "/dev/null/");
  663. fprintf (stderr, "%s\n", _("Press any key to continue..."));
  664. getc (stdin);
  665. }
  666. return tmpdir;
  667. }
  668. /* --------------------------------------------------------------------------------------------- */