vfs.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  1. /*
  2. Virtual File System switch code
  3. Copyright (C) 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
  4. 2007, 2011, 2013
  5. The Free Software Foundation, Inc.
  6. Written by: 1995 Miguel de Icaza
  7. Jakub Jelinek, 1995
  8. Pavel Machek, 1998
  9. Slava Zanko <slavazanko@gmail.com>, 2013
  10. This file is part of the Midnight Commander.
  11. The Midnight Commander is free software: you can redistribute it
  12. and/or modify it under the terms of the GNU General Public License as
  13. published by the Free Software Foundation, either version 3 of the License,
  14. or (at your option) any later version.
  15. The Midnight Commander is distributed in the hope that it will be useful,
  16. but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. GNU General Public License for more details.
  19. You should have received a copy of the GNU General Public License
  20. along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. /**
  23. * \file
  24. * \brief Source: Virtual File System switch code
  25. * \author Miguel de Icaza
  26. * \author Jakub Jelinek
  27. * \author Pavel Machek
  28. * \date 1995, 1998
  29. * \warning funtions like extfs_lstat() have right to destroy any
  30. * strings you pass to them. This is acutally ok as you g_strdup what
  31. * you are passing to them, anyway; still, beware.
  32. *
  33. * Namespace: exports *many* functions with vfs_ prefix; exports
  34. * parse_ls_lga and friends which do not have that prefix.
  35. */
  36. #include <config.h>
  37. #include <errno.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 || (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. const char *slash;
  90. if (size == 0)
  91. return ESTR_SUCCESS;
  92. size = (size > 0) ? size : (signed int) strlen (path);
  93. /* try found /#enc: */
  94. semi = g_strrstr_len (path, size, VFS_ENCODING_PREFIX);
  95. if (semi != NULL && (semi == path || *(semi - 1) == PATH_SEP))
  96. {
  97. char encoding[16];
  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. 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. /*
  267. strict version
  268. return (state == 0) ? vfs_str_buffer->data : NULL;
  269. */
  270. return (state != ESTR_FAILURE) ? vfs_str_buffer->str : NULL;
  271. }
  272. /* --------------------------------------------------------------------------------------------- */
  273. char *
  274. vfs_translate_path_n (const char *path)
  275. {
  276. char *result;
  277. result = vfs_translate_path (path);
  278. return (result != NULL) ? g_strdup (result) : NULL;
  279. }
  280. /* --------------------------------------------------------------------------------------------- */
  281. /**
  282. * Get current directory without any OS calls.
  283. *
  284. * @return string contain current path
  285. */
  286. char *
  287. vfs_get_current_dir (void)
  288. {
  289. return g_strdup (current_path->str);
  290. }
  291. /* --------------------------------------------------------------------------------------------- */
  292. /**
  293. * Get raw current directory object without any OS calls.
  294. *
  295. * @return object contain current path
  296. */
  297. const vfs_path_t *
  298. vfs_get_raw_current_dir (void)
  299. {
  300. return current_path;
  301. }
  302. /* --------------------------------------------------------------------------------------------- */
  303. /**
  304. * Set current directory object.
  305. *
  306. * @param vpath new path
  307. */
  308. void
  309. vfs_set_raw_current_dir (const vfs_path_t * vpath)
  310. {
  311. vfs_path_free (current_path);
  312. current_path = (vfs_path_t *) vpath;
  313. }
  314. /* --------------------------------------------------------------------------------------------- */
  315. /* Return TRUE is the current VFS class is local */
  316. gboolean
  317. vfs_current_is_local (void)
  318. {
  319. return (current_vfs->flags & VFSF_LOCAL) != 0;
  320. }
  321. /* --------------------------------------------------------------------------------------------- */
  322. /* Return flags of the VFS class of the given filename */
  323. vfs_class_flags_t
  324. vfs_file_class_flags (const vfs_path_t * vpath)
  325. {
  326. const vfs_path_element_t *path_element;
  327. path_element = vfs_path_get_by_index (vpath, -1);
  328. if (!vfs_path_element_valid (path_element))
  329. return VFSF_UNKNOWN;
  330. return path_element->class->flags;
  331. }
  332. /* --------------------------------------------------------------------------------------------- */
  333. void
  334. vfs_init (void)
  335. {
  336. /* create the VFS handle arrays */
  337. vfs__classes_list = g_ptr_array_new ();
  338. /* create the VFS handle array */
  339. vfs_openfiles = g_ptr_array_new ();
  340. vfs_str_buffer = g_string_new ("");
  341. }
  342. /* --------------------------------------------------------------------------------------------- */
  343. void
  344. vfs_setup_work_dir (void)
  345. {
  346. const vfs_path_element_t *path_element;
  347. vfs_setup_cwd ();
  348. /* FIXME: is we really need for this check? */
  349. /*
  350. if (strlen (current_dir) > MC_MAXPATHLEN - 2)
  351. vfs_die ("Current dir too long.\n");
  352. */
  353. path_element = vfs_path_get_by_index (current_path, -1);
  354. current_vfs = path_element->class;
  355. }
  356. /* --------------------------------------------------------------------------------------------- */
  357. void
  358. vfs_shut (void)
  359. {
  360. guint i;
  361. vfs_gc_done ();
  362. vfs_set_raw_current_dir (NULL);
  363. for (i = 0; i < vfs__classes_list->len; i++)
  364. {
  365. struct vfs_class *vfs = (struct vfs_class *) g_ptr_array_index (vfs__classes_list, i);
  366. if (vfs->done != NULL)
  367. vfs->done (vfs);
  368. }
  369. g_ptr_array_free (vfs_openfiles, TRUE);
  370. g_ptr_array_free (vfs__classes_list, TRUE);
  371. g_string_free (vfs_str_buffer, TRUE);
  372. g_free (mc_readdir_result);
  373. }
  374. /* --------------------------------------------------------------------------------------------- */
  375. /**
  376. * These ones grab information from the VFS
  377. * and handles them to an upper layer
  378. */
  379. void
  380. vfs_fill_names (fill_names_f func)
  381. {
  382. guint i;
  383. for (i = 0; i < vfs__classes_list->len; i++)
  384. {
  385. struct vfs_class *vfs = (struct vfs_class *) g_ptr_array_index (vfs__classes_list, i);
  386. if (vfs->fill_names != NULL)
  387. vfs->fill_names (vfs, func);
  388. }
  389. }
  390. /* --------------------------------------------------------------------------------------------- */
  391. gboolean
  392. vfs_file_is_local (const vfs_path_t * vpath)
  393. {
  394. return (vfs_file_class_flags (vpath) & VFSF_LOCAL) != 0;
  395. }
  396. /* --------------------------------------------------------------------------------------------- */
  397. void
  398. vfs_print_message (const char *msg, ...)
  399. {
  400. ev_vfs_print_message_t event_data;
  401. va_start (event_data.ap, msg);
  402. event_data.msg = msg;
  403. mc_event_raise (MCEVENT_GROUP_CORE, "vfs_print_message", (gpointer) & event_data);
  404. va_end (event_data.ap);
  405. }
  406. /* --------------------------------------------------------------------------------------------- */
  407. /**
  408. * If it's local, reread the current directory
  409. * from the OS.
  410. */
  411. void
  412. vfs_setup_cwd (void)
  413. {
  414. const vfs_path_element_t *path_element;
  415. if (vfs_get_raw_current_dir () == NULL)
  416. {
  417. char *tmp;
  418. tmp = g_get_current_dir ();
  419. vfs_set_raw_current_dir (vfs_path_from_str (tmp));
  420. g_free (tmp);
  421. }
  422. path_element = vfs_path_get_by_index (vfs_get_raw_current_dir (), -1);
  423. if ((path_element->class->flags & VFSF_LOCAL) != 0)
  424. {
  425. char *current_dir;
  426. vfs_path_t *tmp_vpath;
  427. current_dir = g_get_current_dir ();
  428. tmp_vpath = vfs_path_from_str (current_dir);
  429. g_free (current_dir);
  430. if (tmp_vpath != NULL)
  431. { /* One of the directories in the path is not readable */
  432. struct stat my_stat, my_stat2;
  433. /* Check if it is O.K. to use the current_dir */
  434. if (!(mc_global.vfs.cd_symlinks
  435. && mc_stat (tmp_vpath, &my_stat) == 0
  436. && mc_stat (vfs_get_raw_current_dir (), &my_stat2) == 0
  437. && my_stat.st_ino == my_stat2.st_ino && my_stat.st_dev == my_stat2.st_dev))
  438. vfs_set_raw_current_dir (tmp_vpath);
  439. else
  440. vfs_path_free (tmp_vpath);
  441. }
  442. }
  443. }
  444. /* --------------------------------------------------------------------------------------------- */
  445. /**
  446. * Return current directory. If it's local, reread the current directory
  447. * from the OS.
  448. */
  449. char *
  450. _vfs_get_cwd (void)
  451. {
  452. const vfs_path_t *current_dir_vpath;
  453. vfs_setup_cwd ();
  454. current_dir_vpath = vfs_get_raw_current_dir ();
  455. return g_strdup (vfs_path_as_str (current_dir_vpath));
  456. }
  457. /* --------------------------------------------------------------------------------------------- */
  458. #ifdef HAVE_CHARSET
  459. /**
  460. * Change encoding for last part (vfs_path_element_t) of vpath
  461. *
  462. * @param vpath pointer to path structure
  463. * encoding name of charset
  464. *
  465. * @return pointer to path structure (for use function in anoter functions)
  466. */
  467. vfs_path_t *
  468. vfs_change_encoding (vfs_path_t * vpath, const char *encoding)
  469. {
  470. vfs_path_element_t *path_element;
  471. path_element = (vfs_path_element_t *) vfs_path_get_by_index (vpath, -1);
  472. /* don't add current encoding */
  473. if ((path_element->encoding != NULL) && (strcmp (encoding, path_element->encoding) == 0))
  474. return vpath;
  475. g_free (path_element->encoding);
  476. path_element->encoding = g_strdup (encoding);
  477. if (vfs_path_element_need_cleanup_converter (path_element))
  478. str_close_conv (path_element->dir.converter);
  479. path_element->dir.converter = str_crt_conv_from (path_element->encoding);
  480. return vpath;
  481. }
  482. #endif /* HAVE_CHARSET */
  483. /* --------------------------------------------------------------------------------------------- */
  484. /**
  485. * Preallocate space for file in new place for ensure that file
  486. * will be fully copied with less fragmentation.
  487. *
  488. * @param dest_vfs_fd mc VFS file handler
  489. * @param src_fsize source file size
  490. * @param dest_fsize destination file size (if destination exists, otherwise should be 0)
  491. *
  492. * @return 0 if success and non-zero otherwise.
  493. * Note: function doesn't touch errno global variable.
  494. */
  495. int
  496. vfs_preallocate (int dest_vfs_fd, off_t src_fsize, off_t dest_fsize)
  497. {
  498. #ifndef HAVE_POSIX_FALLOCATE
  499. (void) dest_vfs_fd;
  500. (void) src_fsize;
  501. (void) dest_fsize;
  502. return 0;
  503. #else /* HAVE_POSIX_FALLOCATE */
  504. int *dest_fd;
  505. struct vfs_class *dest_class;
  506. if (!mc_global.vfs.preallocate_space)
  507. return 0;
  508. dest_class = vfs_class_find_by_handle (dest_vfs_fd);
  509. if ((dest_class->flags & VFSF_LOCAL) == 0)
  510. return 0;
  511. dest_fd = (int *) vfs_class_data_find_by_handle (dest_vfs_fd);
  512. if (dest_fd == NULL)
  513. return 0;
  514. if (src_fsize == 0)
  515. return 0;
  516. return posix_fallocate (*dest_fd, dest_fsize, src_fsize - dest_fsize);
  517. #endif /* HAVE_POSIX_FALLOCATE */
  518. }
  519. /* --------------------------------------------------------------------------------------------- */