vfs.c 17 KB

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