interface.c 27 KB

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