interface.c 26 KB

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