vfs.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  1. /*
  2. Virtual File System switch code
  3. Copyright (C) 1995-2019
  4. Free Software Foundation, Inc.
  5. Written by: 1995 Miguel de Icaza
  6. Jakub Jelinek, 1995
  7. Pavel Machek, 1998
  8. Slava Zanko <slavazanko@gmail.com>, 2013
  9. This file is part of the Midnight Commander.
  10. The Midnight Commander is free software: you can redistribute it
  11. and/or modify it under the terms of the GNU General Public License as
  12. published by the Free Software Foundation, either version 3 of the License,
  13. or (at your option) any later version.
  14. The Midnight Commander is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. GNU General Public License for more details.
  18. You should have received a copy of the GNU General Public License
  19. along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. /**
  22. * \file
  23. * \brief Source: Virtual File System switch code
  24. * \author Miguel de Icaza
  25. * \author Jakub Jelinek
  26. * \author Pavel Machek
  27. * \date 1995, 1998
  28. * \warning functions like extfs_lstat() have right to destroy any
  29. * strings you pass to them. This is acutally ok as you g_strdup what
  30. * you are passing to them, anyway; still, beware.
  31. *
  32. * Namespace: exports *many* functions with vfs_ prefix; exports
  33. * parse_ls_lga and friends which do not have that prefix.
  34. */
  35. #include <config.h>
  36. #include <errno.h>
  37. #include <stdlib.h>
  38. #ifdef __linux__
  39. #ifdef HAVE_LINUX_FS_H
  40. #include <linux/fs.h>
  41. #endif /* HAVE_LINUX_FS_H */
  42. #ifdef HAVE_SYS_IOCTL_H
  43. #include <sys/ioctl.h>
  44. #endif /* HAVE_SYS_IOCTL_H */
  45. #endif /* __linux__ */
  46. #include "lib/global.h"
  47. #include "lib/strutil.h"
  48. #include "lib/util.h"
  49. #include "lib/widget.h" /* message() */
  50. #include "lib/event.h"
  51. #ifdef HAVE_CHARSET
  52. #include "lib/charsets.h"
  53. #endif
  54. #include "vfs.h"
  55. #include "utilvfs.h"
  56. #include "gc.h"
  57. /* TODO: move it to the separate .h */
  58. extern struct dirent *mc_readdir_result;
  59. extern GPtrArray *vfs__classes_list;
  60. extern GString *vfs_str_buffer;
  61. extern vfs_class *current_vfs;
  62. /*** global variables ****************************************************************************/
  63. GPtrArray *vfs__classes_list = NULL;
  64. GString *vfs_str_buffer = NULL;
  65. vfs_class *current_vfs = NULL;
  66. /*** file scope macro definitions ****************************************************************/
  67. #define VFS_FIRST_HANDLE 100
  68. /*** file scope type declarations ****************************************************************/
  69. struct vfs_openfile
  70. {
  71. int handle;
  72. vfs_class *vclass;
  73. void *fsinfo;
  74. };
  75. /*** file scope variables ************************************************************************/
  76. /** They keep track of the current directory */
  77. static vfs_path_t *current_path = NULL;
  78. static GPtrArray *vfs_openfiles = NULL;
  79. static long vfs_free_handle_list = -1;
  80. /* --------------------------------------------------------------------------------------------- */
  81. /*** file scope functions ************************************************************************/
  82. /* --------------------------------------------------------------------------------------------- */
  83. /* now used only by vfs_translate_path, but could be used in other vfs
  84. * plugin to automatic detect encoding
  85. * path - path to translate
  86. * size - how many bytes from path translate
  87. * defcnv - convertor, that is used as default, when path does not contain any
  88. * #enc: subtring
  89. * buffer - used to store result of translation
  90. */
  91. static estr_t
  92. _vfs_translate_path (const char *path, int size, GIConv defcnv, GString * buffer)
  93. {
  94. estr_t state = ESTR_SUCCESS;
  95. #ifdef HAVE_CHARSET
  96. const char *semi;
  97. if (size == 0)
  98. return ESTR_SUCCESS;
  99. size = (size > 0) ? size : (signed int) strlen (path);
  100. /* try found /#enc: */
  101. semi = g_strrstr_len (path, size, VFS_ENCODING_PREFIX);
  102. if (semi != NULL && (semi == path || IS_PATH_SEP (semi[-1])))
  103. {
  104. char encoding[16];
  105. const char *slash;
  106. GIConv coder = INVALID_CONV;
  107. int ms;
  108. /* first must be translated part before #enc: */
  109. ms = semi - path;
  110. state = _vfs_translate_path (path, ms, defcnv, buffer);
  111. if (state != ESTR_SUCCESS)
  112. return state;
  113. /* now can be translated part after #enc: */
  114. semi += strlen (VFS_ENCODING_PREFIX); /* skip "#enc:" */
  115. slash = strchr (semi, PATH_SEP);
  116. /* ignore slashes after size; */
  117. if (slash - path >= size)
  118. slash = NULL;
  119. ms = (slash != NULL) ? slash - semi : (int) strlen (semi);
  120. ms = MIN ((unsigned int) ms, sizeof (encoding) - 1);
  121. /* limit encoding size (ms) to path size (size) */
  122. if (semi + ms > path + size)
  123. ms = path + size - semi;
  124. memcpy (encoding, semi, ms);
  125. encoding[ms] = '\0';
  126. if (is_supported_encoding (encoding))
  127. coder = str_crt_conv_to (encoding);
  128. if (coder != INVALID_CONV)
  129. {
  130. if (slash != NULL)
  131. state = str_vfs_convert_to (coder, slash + 1, path + size - slash - 1, buffer);
  132. str_close_conv (coder);
  133. return state;
  134. }
  135. errno = EINVAL;
  136. state = ESTR_FAILURE;
  137. }
  138. else
  139. {
  140. /* path can be translated whole at once */
  141. state = str_vfs_convert_to (defcnv, path, size, buffer);
  142. }
  143. #else
  144. (void) size;
  145. (void) defcnv;
  146. g_string_assign (buffer, path);
  147. #endif /* HAVE_CHARSET */
  148. return state;
  149. }
  150. /* --------------------------------------------------------------------------------------------- */
  151. static struct vfs_openfile *
  152. vfs_get_openfile (int handle)
  153. {
  154. struct vfs_openfile *h;
  155. if (handle < VFS_FIRST_HANDLE || (guint) (handle - VFS_FIRST_HANDLE) >= vfs_openfiles->len)
  156. return NULL;
  157. h = (struct vfs_openfile *) g_ptr_array_index (vfs_openfiles, handle - VFS_FIRST_HANDLE);
  158. if (h == NULL)
  159. return NULL;
  160. g_assert (h->handle == handle);
  161. return h;
  162. }
  163. /* --------------------------------------------------------------------------------------------- */
  164. static gboolean
  165. vfs_test_current_dir (const vfs_path_t * vpath)
  166. {
  167. struct stat my_stat, my_stat2;
  168. return (mc_global.vfs.cd_symlinks && mc_stat (vpath, &my_stat) == 0
  169. && mc_stat (vfs_get_raw_current_dir (), &my_stat2) == 0
  170. && my_stat.st_ino == my_stat2.st_ino && my_stat.st_dev == my_stat2.st_dev);
  171. }
  172. /* --------------------------------------------------------------------------------------------- */
  173. /*** public functions ****************************************************************************/
  174. /* --------------------------------------------------------------------------------------------- */
  175. /** Free open file data for given file handle */
  176. void
  177. vfs_free_handle (int handle)
  178. {
  179. const int idx = handle - VFS_FIRST_HANDLE;
  180. if (handle >= VFS_FIRST_HANDLE && (guint) idx < vfs_openfiles->len)
  181. {
  182. struct vfs_openfile *h;
  183. h = (struct vfs_openfile *) g_ptr_array_index (vfs_openfiles, idx);
  184. g_free (h);
  185. g_ptr_array_index (vfs_openfiles, idx) = (void *) vfs_free_handle_list;
  186. vfs_free_handle_list = idx;
  187. }
  188. }
  189. /* --------------------------------------------------------------------------------------------- */
  190. /** Find VFS class by file handle */
  191. struct vfs_class *
  192. vfs_class_find_by_handle (int handle, void **fsinfo)
  193. {
  194. struct vfs_openfile *h;
  195. h = vfs_get_openfile (handle);
  196. if (h == NULL)
  197. return NULL;
  198. if (fsinfo != NULL)
  199. *fsinfo = h->fsinfo;
  200. return h->vclass;
  201. }
  202. /* --------------------------------------------------------------------------------------------- */
  203. /**
  204. * Create new VFS handle and put it to the list
  205. */
  206. int
  207. vfs_new_handle (struct vfs_class *vclass, void *fsinfo)
  208. {
  209. struct vfs_openfile *h;
  210. h = g_new (struct vfs_openfile, 1);
  211. h->fsinfo = fsinfo;
  212. h->vclass = vclass;
  213. /* Allocate the first free handle */
  214. h->handle = vfs_free_handle_list;
  215. if (h->handle == -1)
  216. {
  217. /* No free allocated handles, allocate one */
  218. h->handle = vfs_openfiles->len;
  219. g_ptr_array_add (vfs_openfiles, h);
  220. }
  221. else
  222. {
  223. vfs_free_handle_list = (long) g_ptr_array_index (vfs_openfiles, vfs_free_handle_list);
  224. g_ptr_array_index (vfs_openfiles, h->handle) = h;
  225. }
  226. h->handle += VFS_FIRST_HANDLE;
  227. return h->handle;
  228. }
  229. /* --------------------------------------------------------------------------------------------- */
  230. int
  231. vfs_ferrno (struct vfs_class *vfs)
  232. {
  233. return vfs->ferrno ? (*vfs->ferrno) (vfs) : E_UNKNOWN;
  234. /* Hope that error message is obscure enough ;-) */
  235. }
  236. /* --------------------------------------------------------------------------------------------- */
  237. gboolean
  238. vfs_register_class (struct vfs_class * vfs)
  239. {
  240. if (vfs->init != NULL) /* vfs has own initialization function */
  241. if (!vfs->init (vfs)) /* but it failed */
  242. return FALSE;
  243. g_ptr_array_add (vfs__classes_list, vfs);
  244. return TRUE;
  245. }
  246. /* --------------------------------------------------------------------------------------------- */
  247. void
  248. vfs_unregister_class (struct vfs_class *vfs)
  249. {
  250. if (vfs->done != NULL)
  251. vfs->done (vfs);
  252. g_ptr_array_remove (vfs__classes_list, vfs);
  253. }
  254. /* --------------------------------------------------------------------------------------------- */
  255. /** Strip known vfs suffixes from a filename (possible improvement: strip
  256. * suffix from last path component).
  257. * \return a malloced string which has to be freed.
  258. */
  259. char *
  260. vfs_strip_suffix_from_filename (const char *filename)
  261. {
  262. char *semi, *p;
  263. if (filename == NULL)
  264. vfs_die ("vfs_strip_suffix_from_path got NULL: impossible");
  265. p = g_strdup (filename);
  266. semi = g_strrstr (p, VFS_PATH_URL_DELIMITER);
  267. if (semi != NULL)
  268. {
  269. char *vfs_prefix;
  270. *semi = '\0';
  271. vfs_prefix = strrchr (p, PATH_SEP);
  272. if (vfs_prefix == NULL)
  273. *semi = *VFS_PATH_URL_DELIMITER;
  274. else
  275. *vfs_prefix = '\0';
  276. }
  277. return p;
  278. }
  279. /* --------------------------------------------------------------------------------------------- */
  280. const char *
  281. vfs_translate_path (const char *path)
  282. {
  283. estr_t state;
  284. g_string_set_size (vfs_str_buffer, 0);
  285. state = _vfs_translate_path (path, -1, str_cnv_from_term, vfs_str_buffer);
  286. return (state != ESTR_FAILURE) ? vfs_str_buffer->str : NULL;
  287. }
  288. /* --------------------------------------------------------------------------------------------- */
  289. char *
  290. vfs_translate_path_n (const char *path)
  291. {
  292. const char *result;
  293. result = vfs_translate_path (path);
  294. return g_strdup (result);
  295. }
  296. /* --------------------------------------------------------------------------------------------- */
  297. /**
  298. * Get current directory without any OS calls.
  299. *
  300. * @return string contains current path
  301. */
  302. const char *
  303. vfs_get_current_dir (void)
  304. {
  305. return current_path->str;
  306. }
  307. /* --------------------------------------------------------------------------------------------- */
  308. /**
  309. * Get current directory without any OS calls.
  310. *
  311. * @return newly allocated string contains current path
  312. */
  313. char *
  314. vfs_get_current_dir_n (void)
  315. {
  316. return g_strdup (current_path->str);
  317. }
  318. /* --------------------------------------------------------------------------------------------- */
  319. /**
  320. * Get raw current directory object without any OS calls.
  321. *
  322. * @return object contain current path
  323. */
  324. const vfs_path_t *
  325. vfs_get_raw_current_dir (void)
  326. {
  327. return current_path;
  328. }
  329. /* --------------------------------------------------------------------------------------------- */
  330. /**
  331. * Set current directory object.
  332. *
  333. * @param vpath new path
  334. */
  335. void
  336. vfs_set_raw_current_dir (const vfs_path_t * vpath)
  337. {
  338. vfs_path_free (current_path);
  339. current_path = (vfs_path_t *) vpath;
  340. }
  341. /* --------------------------------------------------------------------------------------------- */
  342. /* Return TRUE is the current VFS class is local */
  343. gboolean
  344. vfs_current_is_local (void)
  345. {
  346. return (current_vfs->flags & VFS_LOCAL) != 0;
  347. }
  348. /* --------------------------------------------------------------------------------------------- */
  349. /* Return flags of the VFS class of the given filename */
  350. vfs_flags_t
  351. vfs_file_class_flags (const vfs_path_t * vpath)
  352. {
  353. const vfs_path_element_t *path_element;
  354. path_element = vfs_path_get_by_index (vpath, -1);
  355. if (!vfs_path_element_valid (path_element))
  356. return VFS_UNKNOWN;
  357. return path_element->class->flags;
  358. }
  359. /* --------------------------------------------------------------------------------------------- */
  360. void
  361. vfs_init (void)
  362. {
  363. /* create the VFS handle arrays */
  364. vfs__classes_list = g_ptr_array_new ();
  365. /* create the VFS handle array */
  366. vfs_openfiles = g_ptr_array_new ();
  367. vfs_str_buffer = g_string_new ("");
  368. }
  369. /* --------------------------------------------------------------------------------------------- */
  370. void
  371. vfs_setup_work_dir (void)
  372. {
  373. const vfs_path_element_t *path_element;
  374. vfs_setup_cwd ();
  375. /* FIXME: is we really need for this check? */
  376. /*
  377. if (strlen (current_dir) > MC_MAXPATHLEN - 2)
  378. vfs_die ("Current dir too long.\n");
  379. */
  380. path_element = vfs_path_get_by_index (current_path, -1);
  381. current_vfs = path_element->class;
  382. }
  383. /* --------------------------------------------------------------------------------------------- */
  384. void
  385. vfs_shut (void)
  386. {
  387. guint i;
  388. vfs_gc_done ();
  389. vfs_set_raw_current_dir (NULL);
  390. for (i = 0; i < vfs__classes_list->len; i++)
  391. {
  392. struct vfs_class *vfs = VFS_CLASS (g_ptr_array_index (vfs__classes_list, i));
  393. if (vfs->done != NULL)
  394. vfs->done (vfs);
  395. }
  396. /* NULL-ize pointers to make unit tests happy */
  397. g_ptr_array_free (vfs_openfiles, TRUE);
  398. vfs_openfiles = NULL;
  399. g_ptr_array_free (vfs__classes_list, TRUE);
  400. vfs__classes_list = NULL;
  401. g_string_free (vfs_str_buffer, TRUE);
  402. vfs_str_buffer = NULL;
  403. current_vfs = NULL;
  404. vfs_free_handle_list = -1;
  405. MC_PTR_FREE (mc_readdir_result);
  406. }
  407. /* --------------------------------------------------------------------------------------------- */
  408. /**
  409. * These ones grab information from the VFS
  410. * and handles them to an upper layer
  411. */
  412. void
  413. vfs_fill_names (fill_names_f func)
  414. {
  415. guint i;
  416. for (i = 0; i < vfs__classes_list->len; i++)
  417. {
  418. struct vfs_class *vfs = VFS_CLASS (g_ptr_array_index (vfs__classes_list, i));
  419. if (vfs->fill_names != NULL)
  420. vfs->fill_names (vfs, func);
  421. }
  422. }
  423. /* --------------------------------------------------------------------------------------------- */
  424. gboolean
  425. vfs_file_is_local (const vfs_path_t * vpath)
  426. {
  427. return (vfs_file_class_flags (vpath) & VFS_LOCAL) != 0;
  428. }
  429. /* --------------------------------------------------------------------------------------------- */
  430. void
  431. vfs_print_message (const char *msg, ...)
  432. {
  433. ev_vfs_print_message_t event_data;
  434. va_list ap;
  435. va_start (ap, msg);
  436. event_data.msg = g_strdup_vprintf (msg, ap);
  437. va_end (ap);
  438. mc_event_raise (MCEVENT_GROUP_CORE, "vfs_print_message", (gpointer) & event_data);
  439. }
  440. /* --------------------------------------------------------------------------------------------- */
  441. /**
  442. * If it's local, reread the current directory
  443. * from the OS.
  444. */
  445. void
  446. vfs_setup_cwd (void)
  447. {
  448. char *current_dir;
  449. vfs_path_t *tmp_vpath;
  450. const vfs_path_element_t *path_element;
  451. if (vfs_get_raw_current_dir () == NULL)
  452. {
  453. current_dir = g_get_current_dir ();
  454. vfs_set_raw_current_dir (vfs_path_from_str (current_dir));
  455. g_free (current_dir);
  456. current_dir = getenv ("PWD");
  457. tmp_vpath = vfs_path_from_str (current_dir);
  458. if (tmp_vpath != NULL)
  459. {
  460. if (vfs_test_current_dir (tmp_vpath))
  461. vfs_set_raw_current_dir (tmp_vpath);
  462. else
  463. vfs_path_free (tmp_vpath);
  464. }
  465. }
  466. path_element = vfs_path_get_by_index (vfs_get_raw_current_dir (), -1);
  467. if ((path_element->class->flags & VFS_LOCAL) != 0)
  468. {
  469. current_dir = g_get_current_dir ();
  470. tmp_vpath = vfs_path_from_str (current_dir);
  471. g_free (current_dir);
  472. if (tmp_vpath != NULL)
  473. {
  474. /* One of directories in the path is not readable */
  475. /* Check if it is O.K. to use the current_dir */
  476. if (!vfs_test_current_dir (tmp_vpath))
  477. vfs_set_raw_current_dir (tmp_vpath);
  478. else
  479. vfs_path_free (tmp_vpath);
  480. }
  481. }
  482. }
  483. /* --------------------------------------------------------------------------------------------- */
  484. /**
  485. * Return current directory. If it's local, reread the current directory
  486. * from the OS.
  487. */
  488. char *
  489. _vfs_get_cwd (void)
  490. {
  491. const vfs_path_t *current_dir_vpath;
  492. vfs_setup_cwd ();
  493. current_dir_vpath = vfs_get_raw_current_dir ();
  494. return g_strdup (vfs_path_as_str (current_dir_vpath));
  495. }
  496. /* --------------------------------------------------------------------------------------------- */
  497. /**
  498. * Preallocate space for file in new place for ensure that file
  499. * will be fully copied with less fragmentation.
  500. *
  501. * @param dest_vfs_fd mc VFS file handler
  502. * @param src_fsize source file size
  503. * @param dest_fsize destination file size (if destination exists, otherwise should be 0)
  504. *
  505. * @return 0 if success and non-zero otherwise.
  506. * Note: function doesn't touch errno global variable.
  507. */
  508. int
  509. vfs_preallocate (int dest_vfs_fd, off_t src_fsize, off_t dest_fsize)
  510. {
  511. #ifndef HAVE_POSIX_FALLOCATE
  512. (void) dest_vfs_fd;
  513. (void) src_fsize;
  514. (void) dest_fsize;
  515. return 0;
  516. #else /* HAVE_POSIX_FALLOCATE */
  517. void *dest_fd = NULL;
  518. struct vfs_class *dest_class;
  519. if (src_fsize == 0)
  520. return 0;
  521. dest_class = vfs_class_find_by_handle (dest_vfs_fd, &dest_fd);
  522. if ((dest_class->flags & VFS_LOCAL) == 0 || dest_fd == NULL)
  523. return 0;
  524. return posix_fallocate (*(int *) dest_fd, dest_fsize, src_fsize - dest_fsize);
  525. #endif /* HAVE_POSIX_FALLOCATE */
  526. }
  527. /* --------------------------------------------------------------------------------------------- */
  528. int
  529. vfs_clone_file (int dest_vfs_fd, int src_vfs_fd)
  530. {
  531. #ifdef FICLONE
  532. void *dest_fd = NULL;
  533. void *src_fd = NULL;
  534. struct vfs_class *dest_class;
  535. struct vfs_class *src_class;
  536. dest_class = vfs_class_find_by_handle (dest_vfs_fd, &dest_fd);
  537. if ((dest_class->flags & VFS_LOCAL) == 0)
  538. {
  539. errno = EOPNOTSUPP;
  540. return (-1);
  541. }
  542. if (dest_fd == NULL)
  543. {
  544. errno = EBADF;
  545. return (-1);
  546. }
  547. src_class = vfs_class_find_by_handle (src_vfs_fd, &src_fd);
  548. if ((src_class->flags & VFS_LOCAL) == 0)
  549. {
  550. errno = EOPNOTSUPP;
  551. return (-1);
  552. }
  553. if (src_fd == NULL)
  554. {
  555. errno = EBADF;
  556. return (-1);
  557. }
  558. return ioctl (*(int *) dest_fd, FICLONE, *(int *) src_fd);
  559. #else
  560. (void) dest_vfs_fd;
  561. (void) src_vfs_fd;
  562. errno = EOPNOTSUPP;
  563. return (-1);
  564. #endif
  565. }
  566. /* --------------------------------------------------------------------------------------------- */