vfs.c 18 KB

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