interface.c 26 KB

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