vfs.c 16 KB

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