interface.c 26 KB

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