vfs.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. /*
  2. Virtual File System switch code
  3. Copyright (C) 1995-2015
  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. #include "lib/global.h"
  39. #include "lib/strutil.h"
  40. #include "lib/util.h"
  41. #include "lib/widget.h" /* message() */
  42. #include "lib/event.h"
  43. #ifdef HAVE_CHARSET
  44. #include "lib/charsets.h"
  45. #endif
  46. #include "vfs.h"
  47. #include "utilvfs.h"
  48. #include "gc.h"
  49. extern struct dirent *mc_readdir_result;
  50. /*** global variables ****************************************************************************/
  51. GPtrArray *vfs__classes_list = NULL;
  52. GString *vfs_str_buffer = NULL;
  53. struct vfs_class *current_vfs = NULL;
  54. /*** file scope macro definitions ****************************************************************/
  55. #if defined(_AIX) && !defined(NAME_MAX)
  56. #define NAME_MAX FILENAME_MAX
  57. #endif
  58. #define VFS_FIRST_HANDLE 100
  59. #define ISSLASH(a) (a == '\0' || IS_PATH_SEP (a))
  60. /*** file scope type declarations ****************************************************************/
  61. struct vfs_openfile
  62. {
  63. int handle;
  64. struct vfs_class *vclass;
  65. void *fsinfo;
  66. };
  67. /*** file scope variables ************************************************************************/
  68. /** They keep track of the current directory */
  69. static vfs_path_t *current_path = NULL;
  70. static GPtrArray *vfs_openfiles;
  71. static long vfs_free_handle_list = -1;
  72. /* --------------------------------------------------------------------------------------------- */
  73. /*** file scope functions ************************************************************************/
  74. /* --------------------------------------------------------------------------------------------- */
  75. /* now used only by vfs_translate_path, but could be used in other vfs
  76. * plugin to automatic detect encoding
  77. * path - path to translate
  78. * size - how many bytes from path translate
  79. * defcnv - convertor, that is used as default, when path does not contain any
  80. * #enc: subtring
  81. * buffer - used to store result of translation
  82. */
  83. static estr_t
  84. _vfs_translate_path (const char *path, int size, GIConv defcnv, GString * buffer)
  85. {
  86. estr_t state = ESTR_SUCCESS;
  87. #ifdef HAVE_CHARSET
  88. const char *semi;
  89. if (size == 0)
  90. return ESTR_SUCCESS;
  91. size = (size > 0) ? size : (signed int) strlen (path);
  92. /* try found /#enc: */
  93. semi = g_strrstr_len (path, size, VFS_ENCODING_PREFIX);
  94. if (semi != NULL && (semi == path || IS_PATH_SEP (semi[-1])))
  95. {
  96. char encoding[16];
  97. const char *slash;
  98. GIConv coder = INVALID_CONV;
  99. int ms;
  100. /* first must be translated part before #enc: */
  101. ms = semi - path;
  102. state = _vfs_translate_path (path, ms, defcnv, buffer);
  103. if (state != ESTR_SUCCESS)
  104. return state;
  105. /* now can be translated part after #enc: */
  106. semi += strlen (VFS_ENCODING_PREFIX); /* skip "#enc:" */
  107. slash = strchr (semi, PATH_SEP);
  108. /* ignore slashes after size; */
  109. if (slash - path >= size)
  110. slash = NULL;
  111. ms = (slash != NULL) ? slash - semi : (int) strlen (semi);
  112. ms = min ((unsigned int) ms, sizeof (encoding) - 1);
  113. /* limit encoding size (ms) to path size (size) */
  114. if (semi + ms > path + size)
  115. ms = path + size - semi;
  116. memcpy (encoding, semi, ms);
  117. encoding[ms] = '\0';
  118. if (is_supported_encoding (encoding))
  119. coder = str_crt_conv_to (encoding);
  120. if (coder != INVALID_CONV)
  121. {
  122. if (slash != NULL)
  123. state = str_vfs_convert_to (coder, slash + 1, path + size - slash - 1, buffer);
  124. str_close_conv (coder);
  125. return state;
  126. }
  127. errno = EINVAL;
  128. state = ESTR_FAILURE;
  129. }
  130. else
  131. {
  132. /* path can be translated whole at once */
  133. state = str_vfs_convert_to (defcnv, path, size, buffer);
  134. }
  135. #else
  136. (void) size;
  137. (void) defcnv;
  138. g_string_assign (buffer, path);
  139. #endif /* HAVE_CHARSET */
  140. return state;
  141. }
  142. /* --------------------------------------------------------------------------------------------- */
  143. static struct vfs_openfile *
  144. vfs_get_openfile (int handle)
  145. {
  146. struct vfs_openfile *h;
  147. if (handle < VFS_FIRST_HANDLE || (guint) (handle - VFS_FIRST_HANDLE) >= vfs_openfiles->len)
  148. return NULL;
  149. h = (struct vfs_openfile *) g_ptr_array_index (vfs_openfiles, handle - VFS_FIRST_HANDLE);
  150. if (h == NULL)
  151. return NULL;
  152. g_assert (h->handle == handle);
  153. return h;
  154. }
  155. /* --------------------------------------------------------------------------------------------- */
  156. /*** public functions ****************************************************************************/
  157. /* --------------------------------------------------------------------------------------------- */
  158. /** Free open file data for given file handle */
  159. void
  160. vfs_free_handle (int handle)
  161. {
  162. const int idx = handle - VFS_FIRST_HANDLE;
  163. if (handle >= VFS_FIRST_HANDLE && (guint) idx < vfs_openfiles->len)
  164. {
  165. struct vfs_openfile *h;
  166. h = (struct vfs_openfile *) g_ptr_array_index (vfs_openfiles, idx);
  167. g_free (h);
  168. g_ptr_array_index (vfs_openfiles, idx) = (void *) vfs_free_handle_list;
  169. vfs_free_handle_list = idx;
  170. }
  171. }
  172. /* --------------------------------------------------------------------------------------------- */
  173. /** Find private file data by file handle */
  174. void *
  175. vfs_class_data_find_by_handle (int handle)
  176. {
  177. struct vfs_openfile *h;
  178. h = vfs_get_openfile (handle);
  179. return h == NULL ? NULL : h->fsinfo;
  180. }
  181. /* --------------------------------------------------------------------------------------------- */
  182. /** Find VFS class by file handle */
  183. struct vfs_class *
  184. vfs_class_find_by_handle (int handle)
  185. {
  186. struct vfs_openfile *h;
  187. h = vfs_get_openfile (handle);
  188. return h == NULL ? NULL : h->vclass;
  189. }
  190. /* --------------------------------------------------------------------------------------------- */
  191. /**
  192. * Create new VFS handle and put it to the list
  193. */
  194. int
  195. vfs_new_handle (struct vfs_class *vclass, void *fsinfo)
  196. {
  197. struct vfs_openfile *h;
  198. h = g_new (struct vfs_openfile, 1);
  199. h->fsinfo = fsinfo;
  200. h->vclass = vclass;
  201. /* Allocate the first free handle */
  202. h->handle = vfs_free_handle_list;
  203. if (h->handle == -1)
  204. {
  205. /* No free allocated handles, allocate one */
  206. h->handle = vfs_openfiles->len;
  207. g_ptr_array_add (vfs_openfiles, h);
  208. }
  209. else
  210. {
  211. vfs_free_handle_list = (long) g_ptr_array_index (vfs_openfiles, vfs_free_handle_list);
  212. g_ptr_array_index (vfs_openfiles, h->handle) = h;
  213. }
  214. h->handle += VFS_FIRST_HANDLE;
  215. return h->handle;
  216. }
  217. /* --------------------------------------------------------------------------------------------- */
  218. int
  219. vfs_ferrno (struct vfs_class *vfs)
  220. {
  221. return vfs->ferrno ? (*vfs->ferrno) (vfs) : E_UNKNOWN;
  222. /* Hope that error message is obscure enough ;-) */
  223. }
  224. /* --------------------------------------------------------------------------------------------- */
  225. gboolean
  226. vfs_register_class (struct vfs_class * vfs)
  227. {
  228. if (vfs->init != NULL) /* vfs has own initialization function */
  229. if (!vfs->init (vfs)) /* but it failed */
  230. return FALSE;
  231. g_ptr_array_add (vfs__classes_list, vfs);
  232. return TRUE;
  233. }
  234. /* --------------------------------------------------------------------------------------------- */
  235. /** Strip known vfs suffixes from a filename (possible improvement: strip
  236. * suffix from last path component).
  237. * \return a malloced string which has to be freed.
  238. */
  239. char *
  240. vfs_strip_suffix_from_filename (const char *filename)
  241. {
  242. char *semi, *p, *vfs_prefix;
  243. if (filename == NULL)
  244. vfs_die ("vfs_strip_suffix_from_path got NULL: impossible");
  245. p = g_strdup (filename);
  246. semi = g_strrstr (p, VFS_PATH_URL_DELIMITER);
  247. if (semi == NULL)
  248. return p;
  249. *semi = '\0';
  250. vfs_prefix = strrchr (p, PATH_SEP);
  251. if (vfs_prefix == NULL)
  252. {
  253. *semi = *VFS_PATH_URL_DELIMITER;
  254. return p;
  255. }
  256. *vfs_prefix = '\0';
  257. return p;
  258. }
  259. /* --------------------------------------------------------------------------------------------- */
  260. const char *
  261. vfs_translate_path (const char *path)
  262. {
  263. estr_t state;
  264. g_string_set_size (vfs_str_buffer, 0);
  265. state = _vfs_translate_path (path, -1, str_cnv_from_term, vfs_str_buffer);
  266. return (state != ESTR_FAILURE) ? vfs_str_buffer->str : NULL;
  267. }
  268. /* --------------------------------------------------------------------------------------------- */
  269. char *
  270. vfs_translate_path_n (const char *path)
  271. {
  272. const char *result;
  273. result = vfs_translate_path (path);
  274. return g_strdup (result);
  275. }
  276. /* --------------------------------------------------------------------------------------------- */
  277. /**
  278. * Get current directory without any OS calls.
  279. *
  280. * @return string contain current path
  281. */
  282. char *
  283. vfs_get_current_dir (void)
  284. {
  285. return g_strdup (current_path->str);
  286. }
  287. /* --------------------------------------------------------------------------------------------- */
  288. /**
  289. * Get raw current directory object without any OS calls.
  290. *
  291. * @return object contain current path
  292. */
  293. const vfs_path_t *
  294. vfs_get_raw_current_dir (void)
  295. {
  296. return current_path;
  297. }
  298. /* --------------------------------------------------------------------------------------------- */
  299. /**
  300. * Set current directory object.
  301. *
  302. * @param vpath new path
  303. */
  304. void
  305. vfs_set_raw_current_dir (const vfs_path_t * vpath)
  306. {
  307. vfs_path_free (current_path);
  308. current_path = (vfs_path_t *) vpath;
  309. }
  310. /* --------------------------------------------------------------------------------------------- */
  311. /* Return TRUE is the current VFS class is local */
  312. gboolean
  313. vfs_current_is_local (void)
  314. {
  315. return (current_vfs->flags & VFSF_LOCAL) != 0;
  316. }
  317. /* --------------------------------------------------------------------------------------------- */
  318. /* Return flags of the VFS class of the given filename */
  319. vfs_class_flags_t
  320. vfs_file_class_flags (const vfs_path_t * vpath)
  321. {
  322. const vfs_path_element_t *path_element;
  323. path_element = vfs_path_get_by_index (vpath, -1);
  324. if (!vfs_path_element_valid (path_element))
  325. return VFSF_UNKNOWN;
  326. return path_element->class->flags;
  327. }
  328. /* --------------------------------------------------------------------------------------------- */
  329. void
  330. vfs_init (void)
  331. {
  332. /* create the VFS handle arrays */
  333. vfs__classes_list = g_ptr_array_new ();
  334. /* create the VFS handle array */
  335. vfs_openfiles = g_ptr_array_new ();
  336. vfs_str_buffer = g_string_new ("");
  337. }
  338. /* --------------------------------------------------------------------------------------------- */
  339. void
  340. vfs_setup_work_dir (void)
  341. {
  342. const vfs_path_element_t *path_element;
  343. vfs_setup_cwd ();
  344. /* FIXME: is we really need for this check? */
  345. /*
  346. if (strlen (current_dir) > MC_MAXPATHLEN - 2)
  347. vfs_die ("Current dir too long.\n");
  348. */
  349. path_element = vfs_path_get_by_index (current_path, -1);
  350. current_vfs = path_element->class;
  351. }
  352. /* --------------------------------------------------------------------------------------------- */
  353. void
  354. vfs_shut (void)
  355. {
  356. guint i;
  357. vfs_gc_done ();
  358. vfs_set_raw_current_dir (NULL);
  359. for (i = 0; i < vfs__classes_list->len; i++)
  360. {
  361. struct vfs_class *vfs = (struct vfs_class *) g_ptr_array_index (vfs__classes_list, i);
  362. if (vfs->done != NULL)
  363. vfs->done (vfs);
  364. }
  365. g_ptr_array_free (vfs_openfiles, TRUE);
  366. g_ptr_array_free (vfs__classes_list, TRUE);
  367. g_string_free (vfs_str_buffer, TRUE);
  368. g_free (mc_readdir_result);
  369. }
  370. /* --------------------------------------------------------------------------------------------- */
  371. /**
  372. * These ones grab information from the VFS
  373. * and handles them to an upper layer
  374. */
  375. void
  376. vfs_fill_names (fill_names_f func)
  377. {
  378. guint i;
  379. for (i = 0; i < vfs__classes_list->len; i++)
  380. {
  381. struct vfs_class *vfs = (struct vfs_class *) g_ptr_array_index (vfs__classes_list, i);
  382. if (vfs->fill_names != NULL)
  383. vfs->fill_names (vfs, func);
  384. }
  385. }
  386. /* --------------------------------------------------------------------------------------------- */
  387. gboolean
  388. vfs_file_is_local (const vfs_path_t * vpath)
  389. {
  390. return (vfs_file_class_flags (vpath) & VFSF_LOCAL) != 0;
  391. }
  392. /* --------------------------------------------------------------------------------------------- */
  393. void
  394. vfs_print_message (const char *msg, ...)
  395. {
  396. ev_vfs_print_message_t event_data;
  397. va_start (event_data.ap, msg);
  398. event_data.msg = msg;
  399. mc_event_dispatch (MCEVENT_GROUP_CORE, "vfs_print_message", (gpointer) & event_data, NULL,
  400. NULL);
  401. va_end (event_data.ap);
  402. }
  403. /* --------------------------------------------------------------------------------------------- */
  404. /**
  405. * If it's local, reread the current directory
  406. * from the OS.
  407. */
  408. void
  409. vfs_setup_cwd (void)
  410. {
  411. char *current_dir;
  412. vfs_path_t *tmp_vpath;
  413. const vfs_path_element_t *path_element;
  414. if (vfs_get_raw_current_dir () == NULL)
  415. {
  416. current_dir = g_get_current_dir ();
  417. vfs_set_raw_current_dir (vfs_path_from_str (current_dir));
  418. g_free (current_dir);
  419. current_dir = getenv ("PWD");
  420. tmp_vpath = vfs_path_from_str (current_dir);
  421. if (tmp_vpath != NULL)
  422. {
  423. struct stat my_stat, my_stat2;
  424. if (mc_global.vfs.cd_symlinks
  425. && mc_stat (tmp_vpath, &my_stat) == 0
  426. && mc_stat (vfs_get_raw_current_dir (), &my_stat2) == 0
  427. && my_stat.st_ino == my_stat2.st_ino && my_stat.st_dev == my_stat2.st_dev)
  428. vfs_set_raw_current_dir (tmp_vpath);
  429. else
  430. vfs_path_free (tmp_vpath);
  431. }
  432. }
  433. path_element = vfs_path_get_by_index (vfs_get_raw_current_dir (), -1);
  434. if ((path_element->class->flags & VFSF_LOCAL) != 0)
  435. {
  436. current_dir = g_get_current_dir ();
  437. tmp_vpath = vfs_path_from_str (current_dir);
  438. g_free (current_dir);
  439. if (tmp_vpath != NULL)
  440. { /* One of the directories in the path is not readable */
  441. struct stat my_stat, my_stat2;
  442. /* Check if it is O.K. to use the current_dir */
  443. if (!(mc_global.vfs.cd_symlinks
  444. && mc_stat (tmp_vpath, &my_stat) == 0
  445. && mc_stat (vfs_get_raw_current_dir (), &my_stat2) == 0
  446. && my_stat.st_ino == my_stat2.st_ino && my_stat.st_dev == my_stat2.st_dev))
  447. vfs_set_raw_current_dir (tmp_vpath);
  448. else
  449. vfs_path_free (tmp_vpath);
  450. }
  451. }
  452. }
  453. /* --------------------------------------------------------------------------------------------- */
  454. /**
  455. * Return current directory. If it's local, reread the current directory
  456. * from the OS.
  457. */
  458. char *
  459. _vfs_get_cwd (void)
  460. {
  461. const vfs_path_t *current_dir_vpath;
  462. vfs_setup_cwd ();
  463. current_dir_vpath = vfs_get_raw_current_dir ();
  464. return g_strdup (vfs_path_as_str (current_dir_vpath));
  465. }
  466. /* --------------------------------------------------------------------------------------------- */
  467. /**
  468. * Preallocate space for file in new place for ensure that file
  469. * will be fully copied with less fragmentation.
  470. *
  471. * @param dest_vfs_fd mc VFS file handler
  472. * @param src_fsize source file size
  473. * @param dest_fsize destination file size (if destination exists, otherwise should be 0)
  474. *
  475. * @return 0 if success and non-zero otherwise.
  476. * Note: function doesn't touch errno global variable.
  477. */
  478. int
  479. vfs_preallocate (int dest_vfs_fd, off_t src_fsize, off_t dest_fsize)
  480. {
  481. #ifndef HAVE_POSIX_FALLOCATE
  482. (void) dest_vfs_fd;
  483. (void) src_fsize;
  484. (void) dest_fsize;
  485. return 0;
  486. #else /* HAVE_POSIX_FALLOCATE */
  487. int *dest_fd;
  488. struct vfs_class *dest_class;
  489. if (!mc_global.vfs.preallocate_space)
  490. return 0;
  491. dest_class = vfs_class_find_by_handle (dest_vfs_fd);
  492. if ((dest_class->flags & VFSF_LOCAL) == 0)
  493. return 0;
  494. dest_fd = (int *) vfs_class_data_find_by_handle (dest_vfs_fd);
  495. if (dest_fd == NULL)
  496. return 0;
  497. if (src_fsize == 0)
  498. return 0;
  499. return posix_fallocate (*dest_fd, dest_fsize, src_fsize - dest_fsize);
  500. #endif /* HAVE_POSIX_FALLOCATE */
  501. }
  502. /* --------------------------------------------------------------------------------------------- */