interface.c 27 KB

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