path.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692
  1. /*
  2. Virtual File System path handlers
  3. Copyright (C) 2011-2024
  4. Free Software Foundation, Inc.
  5. Written by:
  6. Slava Zanko <slavazanko@gmail.com>, 2011, 2013
  7. Andrew Borodin <aborodin@vmail.ru>, 2013-2022
  8. This file is part of the Midnight Commander.
  9. The Midnight Commander is free software: you can redistribute it
  10. and/or modify it under the terms of the GNU General Public License as
  11. published by the Free Software Foundation, either version 3 of the License,
  12. or (at your option) any later version.
  13. The Midnight Commander is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. GNU General Public License for more details.
  17. You should have received a copy of the GNU General Public License
  18. along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. /**
  21. * \file
  22. * \brief Source: Virtual File System: path handlers
  23. * \author Slava Zanko
  24. * \date 2011
  25. */
  26. #include <config.h>
  27. #include <errno.h>
  28. #include "lib/global.h"
  29. #include "lib/strutil.h"
  30. #include "lib/util.h" /* mc_build_filename() */
  31. #include "lib/serialize.h"
  32. #include "vfs.h"
  33. #include "utilvfs.h"
  34. #include "xdirentry.h"
  35. #include "path.h"
  36. extern GPtrArray *vfs__classes_list;
  37. /*** global variables ****************************************************************************/
  38. /*** file scope macro definitions ****************************************************************/
  39. /*** file scope type declarations ****************************************************************/
  40. /*** forward declarations (file scope functions) *************************************************/
  41. /*** file scope variables ************************************************************************/
  42. /* --------------------------------------------------------------------------------------------- */
  43. /*** file scope functions ************************************************************************/
  44. /* --------------------------------------------------------------------------------------------- */
  45. static gboolean
  46. path_magic (const char *path)
  47. {
  48. struct stat buf;
  49. return (stat (path, &buf) != 0);
  50. }
  51. /* --------------------------------------------------------------------------------------------- */
  52. /**
  53. * Splits path extracting vfs part.
  54. *
  55. * Splits path
  56. * \verbatim /p1#op/inpath \endverbatim
  57. * into
  58. * \verbatim inpath,op; \endverbatim
  59. * returns which vfs it is.
  60. * What is left in path is p1. You still want to g_free(path), you DON'T
  61. * want to free neither *inpath nor *op
  62. */
  63. static struct vfs_class *
  64. _vfs_split_with_semi_skip_count (char *path, const char **inpath, const char **op,
  65. size_t skip_count)
  66. {
  67. char *semi;
  68. char *slash;
  69. struct vfs_class *ret;
  70. if (path == NULL)
  71. vfs_die ("Cannot split NULL");
  72. semi = strrstr_skip_count (path, "#", skip_count);
  73. if ((semi == NULL) || (!path_magic (path)))
  74. return NULL;
  75. slash = strchr (semi, PATH_SEP);
  76. *semi = '\0';
  77. if (op != NULL)
  78. *op = NULL;
  79. if (inpath != NULL)
  80. *inpath = NULL;
  81. if (slash != NULL)
  82. *slash = '\0';
  83. ret = vfs_prefix_to_class (semi + 1);
  84. if (ret != NULL)
  85. {
  86. if (op != NULL)
  87. *op = semi + 1;
  88. if (inpath != NULL)
  89. *inpath = slash != NULL ? slash + 1 : NULL;
  90. return ret;
  91. }
  92. if (slash != NULL)
  93. *slash = PATH_SEP;
  94. *semi = '#';
  95. ret = _vfs_split_with_semi_skip_count (path, inpath, op, skip_count + 1);
  96. return ret;
  97. }
  98. /* --------------------------------------------------------------------------------------------- */
  99. /**
  100. * remove //, /./ and /../
  101. *
  102. * @return newly allocated string
  103. */
  104. static char *
  105. vfs_canon (const char *path)
  106. {
  107. char *result;
  108. if (path == NULL)
  109. vfs_die ("Cannot canonicalize NULL");
  110. if (!IS_PATH_SEP (*path))
  111. {
  112. /* Relative to current directory */
  113. char *local;
  114. #ifdef HAVE_CHARSET
  115. if (g_str_has_prefix (path, VFS_ENCODING_PREFIX))
  116. {
  117. /*
  118. encoding prefix placed at start of string without the leading slash
  119. should be autofixed by adding the leading slash
  120. */
  121. local = mc_build_filename (PATH_SEP_STR, path, (char *) NULL);
  122. }
  123. else
  124. #endif
  125. {
  126. const char *curr_dir;
  127. curr_dir = vfs_get_current_dir ();
  128. local = mc_build_filename (curr_dir, path, (char *) NULL);
  129. }
  130. result = vfs_canon (local);
  131. g_free (local);
  132. }
  133. else
  134. {
  135. /* Absolute path */
  136. result = g_strdup (path);
  137. canonicalize_pathname (result);
  138. }
  139. return result;
  140. }
  141. /* --------------------------------------------------------------------------------------------- */
  142. #ifdef HAVE_CHARSET
  143. /** get encoding after last #enc: or NULL, if part does not contain #enc:
  144. *
  145. * @param path null-terminated string
  146. * @param len the maximum length of path, where #enc: should be searched
  147. *
  148. * @return newly allocated string.
  149. */
  150. static char *
  151. vfs_get_encoding (const char *path, ssize_t len)
  152. {
  153. char *semi;
  154. /* try found #enc: */
  155. semi = g_strrstr_len (path, len, VFS_ENCODING_PREFIX);
  156. if (semi == NULL)
  157. return NULL;
  158. if (semi == path || IS_PATH_SEP (semi[-1]))
  159. {
  160. char *slash;
  161. semi += strlen (VFS_ENCODING_PREFIX); /* skip "#enc:" */
  162. slash = strchr (semi, PATH_SEP);
  163. if (slash != NULL)
  164. return g_strndup (semi, slash - semi);
  165. return g_strdup (semi);
  166. }
  167. return vfs_get_encoding (path, semi - path);
  168. }
  169. #endif
  170. /* --------------------------------------------------------------------------------------------- */
  171. /** Extract the hostname and username from the path
  172. *
  173. * Format of the path is [user@]hostname:port/remote-dir, e.g.:
  174. *
  175. * ftp://sunsite.unc.edu/pub/linux
  176. * ftp://miguel@sphinx.nuclecu.unam.mx/c/nc
  177. * ftp://tsx-11.mit.edu:8192/
  178. * ftp://joe@foo.edu:11321/private
  179. * ftp://joe:password@foo.se
  180. *
  181. * @param path_element is an input string to be parsed
  182. * @param path is an input string to be parsed
  183. *
  184. * @return g_malloc()ed url info.
  185. * If the user is empty, e.g. ftp://@roxanne/private, and URL_USE_ANONYMOUS
  186. * is not set, then the current login name is supplied.
  187. * Return value is a g_malloc()ed structure with the pathname relative to the
  188. * host.
  189. */
  190. static void
  191. vfs_path_url_split (vfs_path_element_t *path_element, const char *path)
  192. {
  193. char *pcopy;
  194. char *colon, *at, *rest;
  195. path_element->port = 0;
  196. pcopy = g_strdup (path);
  197. /* search for any possible user */
  198. at = strrchr (pcopy, '@');
  199. /* We have a username */
  200. if (at == NULL)
  201. rest = pcopy;
  202. else
  203. {
  204. const char *pend;
  205. char *inner_colon;
  206. pend = strchr (at, '\0');
  207. *at = '\0';
  208. inner_colon = strchr (pcopy, ':');
  209. if (inner_colon != NULL)
  210. {
  211. *inner_colon = '\0';
  212. inner_colon++;
  213. path_element->password = g_strdup (inner_colon);
  214. }
  215. if (*pcopy != '\0')
  216. path_element->user = g_strdup (pcopy);
  217. if (pend == at + 1)
  218. rest = at;
  219. else
  220. rest = at + 1;
  221. }
  222. /* Check if the host comes with a port spec, if so, chop it */
  223. if (*rest != '[')
  224. colon = strchr (rest, ':');
  225. else
  226. {
  227. colon = strchr (++rest, ']');
  228. if (colon != NULL)
  229. {
  230. *colon = '\0';
  231. colon++;
  232. *colon = '\0';
  233. path_element->ipv6 = TRUE;
  234. }
  235. }
  236. if (colon != NULL)
  237. {
  238. *colon = '\0';
  239. /* cppcheck-suppress invalidscanf */
  240. if (sscanf (colon + 1, "%d", &path_element->port) == 1)
  241. {
  242. if (path_element->port <= 0 || path_element->port >= 65536)
  243. path_element->port = 0;
  244. }
  245. else
  246. while (*(++colon) != '\0')
  247. {
  248. switch (*colon)
  249. {
  250. case 'C':
  251. path_element->port = 1;
  252. break;
  253. case 'r':
  254. path_element->port = 2;
  255. break;
  256. default:
  257. break;
  258. }
  259. }
  260. }
  261. path_element->host = g_strdup (rest);
  262. g_free (pcopy);
  263. }
  264. /* --------------------------------------------------------------------------------------------- */
  265. /**
  266. * get VFS class for the given name
  267. *
  268. * @param class_name name of class
  269. *
  270. * @return pointer to class structure or NULL if class not found
  271. */
  272. static struct vfs_class *
  273. vfs_get_class_by_name (const char *class_name)
  274. {
  275. guint i;
  276. if (class_name == NULL)
  277. return NULL;
  278. for (i = 0; i < vfs__classes_list->len; i++)
  279. {
  280. struct vfs_class *vfs = VFS_CLASS (g_ptr_array_index (vfs__classes_list, i));
  281. if ((vfs->name != NULL) && (strcmp (vfs->name, class_name) == 0))
  282. return vfs;
  283. }
  284. return NULL;
  285. }
  286. /* --------------------------------------------------------------------------------------------- */
  287. /**
  288. * Check if path string contain URL-like elements
  289. *
  290. * @param path_str path
  291. *
  292. * @return TRUE if path is deprecated or FALSE otherwise
  293. */
  294. static gboolean
  295. vfs_path_is_str_path_deprecated (const char *path_str)
  296. {
  297. return strstr (path_str, VFS_PATH_URL_DELIMITER) == NULL;
  298. }
  299. /* --------------------------------------------------------------------------------------------- */
  300. /** Split path string to path elements by deprecated algorithm.
  301. *
  302. * @param path_str VFS-path
  303. *
  304. * @return pointer to newly created vfs_path_t object with filled path elements array.
  305. */
  306. static vfs_path_t *
  307. vfs_path_from_str_deprecated_parser (char *path)
  308. {
  309. vfs_path_t *vpath;
  310. vfs_path_element_t *element;
  311. struct vfs_class *class;
  312. const char *local, *op;
  313. vpath = vfs_path_new (FALSE);
  314. while ((class = _vfs_split_with_semi_skip_count (path, &local, &op, 0)) != NULL)
  315. {
  316. char *url_params;
  317. element = g_new0 (vfs_path_element_t, 1);
  318. element->class = class;
  319. if (local == NULL)
  320. local = "";
  321. element->path = vfs_translate_path_n (local);
  322. #ifdef HAVE_CHARSET
  323. element->encoding = vfs_get_encoding (local, -1);
  324. element->dir.converter = (element->encoding != NULL) ? str_crt_conv_from (element->encoding)
  325. : INVALID_CONV;
  326. #endif
  327. url_params = strchr (op, ':'); /* skip VFS prefix */
  328. if (url_params != NULL)
  329. {
  330. *url_params = '\0';
  331. url_params++;
  332. vfs_path_url_split (element, url_params);
  333. }
  334. if (*op != '\0')
  335. element->vfs_prefix = g_strdup (op);
  336. g_array_prepend_val (vpath->path, element);
  337. }
  338. if (path[0] != '\0')
  339. {
  340. element = g_new0 (vfs_path_element_t, 1);
  341. element->class = g_ptr_array_index (vfs__classes_list, 0);
  342. element->path = vfs_translate_path_n (path);
  343. #ifdef HAVE_CHARSET
  344. element->encoding = vfs_get_encoding (path, -1);
  345. element->dir.converter = (element->encoding != NULL) ? str_crt_conv_from (element->encoding)
  346. : INVALID_CONV;
  347. #endif
  348. g_array_prepend_val (vpath->path, element);
  349. }
  350. return vpath;
  351. }
  352. /* --------------------------------------------------------------------------------------------- */
  353. /** Split path string to path elements by URL algorithm.
  354. *
  355. * @param path_str VFS-path
  356. * @param flags flags for converter
  357. *
  358. * @return pointer to newly created vfs_path_t object with filled path elements array.
  359. */
  360. static vfs_path_t *
  361. vfs_path_from_str_uri_parser (char *path)
  362. {
  363. gboolean path_is_absolute;
  364. vfs_path_t *vpath;
  365. vfs_path_element_t *element;
  366. char *url_delimiter;
  367. if (path == NULL)
  368. return vfs_path_new (FALSE);
  369. path_is_absolute = IS_PATH_SEP (*path);
  370. #ifdef HAVE_CHARSET
  371. path_is_absolute = path_is_absolute || g_str_has_prefix (path, VFS_ENCODING_PREFIX);
  372. #endif
  373. vpath = vfs_path_new (!path_is_absolute);
  374. while ((url_delimiter = g_strrstr (path, VFS_PATH_URL_DELIMITER)) != NULL)
  375. {
  376. char *vfs_prefix_start;
  377. char *real_vfs_prefix_start = url_delimiter;
  378. while (real_vfs_prefix_start > path && !IS_PATH_SEP (*real_vfs_prefix_start))
  379. real_vfs_prefix_start--;
  380. vfs_prefix_start = real_vfs_prefix_start;
  381. if (IS_PATH_SEP (*vfs_prefix_start))
  382. vfs_prefix_start++;
  383. *url_delimiter = '\0';
  384. element = g_new0 (vfs_path_element_t, 1);
  385. element->class = vfs_prefix_to_class (vfs_prefix_start);
  386. element->vfs_prefix = g_strdup (vfs_prefix_start);
  387. url_delimiter += strlen (VFS_PATH_URL_DELIMITER);
  388. if (element->class != NULL && (element->class->flags & VFSF_REMOTE) != 0)
  389. {
  390. char *slash_pointer;
  391. slash_pointer = strchr (url_delimiter, PATH_SEP);
  392. if (slash_pointer == NULL)
  393. element->path = g_strdup ("");
  394. else
  395. {
  396. element->path = vfs_translate_path_n (slash_pointer + 1);
  397. #ifdef HAVE_CHARSET
  398. element->encoding = vfs_get_encoding (slash_pointer, -1);
  399. #endif
  400. *slash_pointer = '\0';
  401. }
  402. vfs_path_url_split (element, url_delimiter);
  403. }
  404. else
  405. {
  406. element->path = vfs_translate_path_n (url_delimiter);
  407. #ifdef HAVE_CHARSET
  408. element->encoding = vfs_get_encoding (url_delimiter, -1);
  409. #endif
  410. }
  411. #ifdef HAVE_CHARSET
  412. element->dir.converter = (element->encoding != NULL) ? str_crt_conv_from (element->encoding)
  413. : INVALID_CONV;
  414. #endif
  415. g_array_prepend_val (vpath->path, element);
  416. if ((real_vfs_prefix_start > path && IS_PATH_SEP (*real_vfs_prefix_start))
  417. || (real_vfs_prefix_start == path && !IS_PATH_SEP (*real_vfs_prefix_start)))
  418. *real_vfs_prefix_start = '\0';
  419. else
  420. *(real_vfs_prefix_start + 1) = '\0';
  421. }
  422. if (path[0] != '\0')
  423. {
  424. element = g_new0 (vfs_path_element_t, 1);
  425. element->class = g_ptr_array_index (vfs__classes_list, 0);
  426. element->path = vfs_translate_path_n (path);
  427. #ifdef HAVE_CHARSET
  428. element->encoding = vfs_get_encoding (path, -1);
  429. element->dir.converter = (element->encoding != NULL) ? str_crt_conv_from (element->encoding)
  430. : INVALID_CONV;
  431. #endif
  432. g_array_prepend_val (vpath->path, element);
  433. }
  434. return vpath;
  435. }
  436. /* --------------------------------------------------------------------------------------------- */
  437. /**
  438. * Add element's class info to result string (such as VFS name, host, encoding etc)
  439. * This function used as helper only in vfs_path_tokens_get() function
  440. *
  441. * @param element current path element
  442. * @param ret_tokens total tikens for return
  443. * @param element_tokens accumulated element-only tokens
  444. */
  445. static void
  446. vfs_path_tokens_add_class_info (const vfs_path_element_t *element, GString *ret_tokens,
  447. GString *element_tokens)
  448. {
  449. if (((element->class->flags & VFSF_LOCAL) == 0 || ret_tokens->len > 0)
  450. && element_tokens->len > 0)
  451. {
  452. GString *url_str;
  453. if (ret_tokens->len > 0 && !IS_PATH_SEP (ret_tokens->str[ret_tokens->len - 1]))
  454. g_string_append_c (ret_tokens, PATH_SEP);
  455. g_string_append (ret_tokens, element->vfs_prefix);
  456. g_string_append (ret_tokens, VFS_PATH_URL_DELIMITER);
  457. url_str = vfs_path_build_url_params_str (element, TRUE);
  458. if (url_str != NULL)
  459. {
  460. g_string_append_len (ret_tokens, url_str->str, url_str->len);
  461. g_string_append_c (ret_tokens, PATH_SEP);
  462. g_string_free (url_str, TRUE);
  463. }
  464. }
  465. #ifdef HAVE_CHARSET
  466. if (element->encoding != NULL)
  467. {
  468. if (ret_tokens->len > 0 && !IS_PATH_SEP (ret_tokens->str[ret_tokens->len - 1]))
  469. g_string_append (ret_tokens, PATH_SEP_STR);
  470. g_string_append (ret_tokens, VFS_ENCODING_PREFIX);
  471. g_string_append (ret_tokens, element->encoding);
  472. g_string_append (ret_tokens, PATH_SEP_STR);
  473. }
  474. #endif
  475. g_string_append (ret_tokens, element_tokens->str);
  476. }
  477. /* --------------------------------------------------------------------------------------------- */
  478. /**
  479. * Strip path to home dir.
  480. * @param dir pointer to string contains full path
  481. */
  482. static char *
  483. vfs_path_strip_home (const char *dir)
  484. {
  485. const char *home_dir = mc_config_get_home_dir ();
  486. if (home_dir != NULL)
  487. {
  488. size_t len;
  489. len = strlen (home_dir);
  490. if (strncmp (dir, home_dir, len) == 0 && (IS_PATH_SEP (dir[len]) || dir[len] == '\0'))
  491. return g_strdup_printf ("~%s", dir + len);
  492. }
  493. return g_strdup (dir);
  494. }
  495. /* --------------------------------------------------------------------------------------------- */
  496. /*** public functions ****************************************************************************/
  497. /* --------------------------------------------------------------------------------------------- */
  498. #define vfs_append_from_path(appendfrom, is_relative) \
  499. { \
  500. if ((flags & VPF_STRIP_HOME) && element_index == 0 \
  501. && (element->class->flags & VFSF_LOCAL) != 0) \
  502. { \
  503. char *stripped_home_str; \
  504. stripped_home_str = vfs_path_strip_home (appendfrom); \
  505. g_string_append (buffer, stripped_home_str); \
  506. g_free (stripped_home_str); \
  507. } \
  508. else \
  509. { \
  510. if (!is_relative && !IS_PATH_SEP (*appendfrom) && *appendfrom != '\0' \
  511. && (buffer->len == 0 || !IS_PATH_SEP (buffer->str[buffer->len - 1]))) \
  512. g_string_append_c (buffer, PATH_SEP); \
  513. g_string_append (buffer, appendfrom); \
  514. } \
  515. }
  516. /**
  517. * Convert first elements_count elements from vfs_path_t to string representation with flags.
  518. *
  519. * @param vpath pointer to vfs_path_t object
  520. * @param elements_count count of first elements for convert
  521. * @param flags for converter
  522. *
  523. * @return pointer to newly created string.
  524. */
  525. char *
  526. vfs_path_to_str_flags (const vfs_path_t *vpath, int elements_count, vfs_path_flag_t flags)
  527. {
  528. int element_index;
  529. GString *buffer;
  530. #ifdef HAVE_CHARSET
  531. GString *recode_buffer = NULL;
  532. #endif
  533. if (vpath == NULL)
  534. return NULL;
  535. if (elements_count == 0 || elements_count > vfs_path_elements_count (vpath))
  536. elements_count = vfs_path_elements_count (vpath);
  537. if (elements_count < 0)
  538. elements_count = vfs_path_elements_count (vpath) + elements_count;
  539. buffer = g_string_new ("");
  540. for (element_index = 0; element_index < elements_count; element_index++)
  541. {
  542. const vfs_path_element_t *element;
  543. gboolean is_relative = vpath->relative && (element_index == 0);
  544. element = vfs_path_get_by_index (vpath, element_index);
  545. if (element->vfs_prefix != NULL)
  546. {
  547. GString *url_str;
  548. if (!is_relative && (buffer->len == 0 || !IS_PATH_SEP (buffer->str[buffer->len - 1])))
  549. g_string_append_c (buffer, PATH_SEP);
  550. g_string_append (buffer, element->vfs_prefix);
  551. g_string_append (buffer, VFS_PATH_URL_DELIMITER);
  552. url_str = vfs_path_build_url_params_str (element, (flags & VPF_STRIP_PASSWORD) == 0);
  553. if (url_str != NULL)
  554. {
  555. g_string_append_len (buffer, url_str->str, url_str->len);
  556. g_string_append_c (buffer, PATH_SEP);
  557. g_string_free (url_str, TRUE);
  558. }
  559. }
  560. #ifdef HAVE_CHARSET
  561. if ((flags & VPF_RECODE) == 0 && vfs_path_element_need_cleanup_converter (element))
  562. {
  563. if ((flags & VPF_HIDE_CHARSET) == 0)
  564. {
  565. if ((!is_relative)
  566. && (buffer->len == 0 || !IS_PATH_SEP (buffer->str[buffer->len - 1])))
  567. g_string_append (buffer, PATH_SEP_STR);
  568. g_string_append (buffer, VFS_ENCODING_PREFIX);
  569. g_string_append (buffer, element->encoding);
  570. }
  571. if (recode_buffer == NULL)
  572. recode_buffer = g_string_sized_new (32);
  573. else
  574. g_string_set_size (recode_buffer, 0);
  575. str_vfs_convert_from (element->dir.converter, element->path, recode_buffer);
  576. vfs_append_from_path (recode_buffer->str, is_relative);
  577. }
  578. else
  579. #endif
  580. {
  581. vfs_append_from_path (element->path, is_relative);
  582. }
  583. }
  584. #ifdef HAVE_CHARSET
  585. if (recode_buffer != NULL)
  586. g_string_free (recode_buffer, TRUE);
  587. #endif
  588. return g_string_free (buffer, FALSE);
  589. }
  590. #undef vfs_append_from_path
  591. /* --------------------------------------------------------------------------------------------- */
  592. /**
  593. * Convert first elements_count elements from vfs_path_t to string representation.
  594. *
  595. * @param vpath pointer to vfs_path_t object
  596. * @param elements_count count of first elements for convert
  597. *
  598. * @return pointer to newly created string.
  599. */
  600. char *
  601. vfs_path_to_str_elements_count (const vfs_path_t *vpath, int elements_count)
  602. {
  603. return vfs_path_to_str_flags (vpath, elements_count, VPF_NONE);
  604. }
  605. /* --------------------------------------------------------------------------------------------- */
  606. /**
  607. * Split path string to path elements with flags for change parce process.
  608. *
  609. * @param path_str VFS-path
  610. * @param flags flags for parser
  611. *
  612. * @return pointer to newly created vfs_path_t object with filled path elements array.
  613. */
  614. vfs_path_t *
  615. vfs_path_from_str_flags (const char *path_str, vfs_path_flag_t flags)
  616. {
  617. vfs_path_t *vpath;
  618. char *path;
  619. if (path_str == NULL)
  620. return NULL;
  621. if ((flags & VPF_NO_CANON) == 0)
  622. path = vfs_canon (path_str);
  623. else
  624. path = g_strdup (path_str);
  625. if (path == NULL)
  626. return NULL;
  627. if ((flags & VPF_USE_DEPRECATED_PARSER) != 0 && vfs_path_is_str_path_deprecated (path))
  628. vpath = vfs_path_from_str_deprecated_parser (path);
  629. else
  630. vpath = vfs_path_from_str_uri_parser (path);
  631. vpath->str = vfs_path_to_str_flags (vpath, 0, flags);
  632. g_free (path);
  633. return vpath;
  634. }
  635. /* --------------------------------------------------------------------------------------------- */
  636. /**
  637. * Split path string to path elements.
  638. *
  639. * @param path_str VFS-path
  640. *
  641. * @return pointer to newly created vfs_path_t object with filled path elements array.
  642. */
  643. vfs_path_t *
  644. vfs_path_from_str (const char *path_str)
  645. {
  646. return vfs_path_from_str_flags (path_str, VPF_NONE);
  647. }
  648. /* --------------------------------------------------------------------------------------------- */
  649. /*
  650. * Create new vfs_path_t object.
  651. *
  652. * @return pointer to newly created vfs_path_t object.
  653. */
  654. vfs_path_t *
  655. vfs_path_new (gboolean relative)
  656. {
  657. vfs_path_t *vpath;
  658. vpath = g_new0 (vfs_path_t, 1);
  659. vpath->path = g_array_new (FALSE, TRUE, sizeof (vfs_path_element_t *));
  660. vpath->relative = relative;
  661. return vpath;
  662. }
  663. /* --------------------------------------------------------------------------------------------- */
  664. /*
  665. * Get count of path elements.
  666. *
  667. * @param vpath pointer to vfs_path_t object
  668. *
  669. * @return count of path elements.
  670. */
  671. int
  672. vfs_path_elements_count (const vfs_path_t *vpath)
  673. {
  674. return (vpath != NULL && vpath->path != NULL) ? vpath->path->len : 0;
  675. }
  676. /* --------------------------------------------------------------------------------------------- */
  677. /**
  678. * Add vfs_path_element_t object to end of list in vfs_path_t object
  679. * @param vpath pointer to vfs_path_t object
  680. * @param path_element pointer to vfs_path_element_t object
  681. */
  682. void
  683. vfs_path_add_element (vfs_path_t *vpath, const vfs_path_element_t *path_element)
  684. {
  685. g_array_append_val (vpath->path, path_element);
  686. g_free (vpath->str);
  687. vpath->str = vfs_path_to_str_flags (vpath, 0, VPF_NONE);
  688. }
  689. /* --------------------------------------------------------------------------------------------- */
  690. /*
  691. * Get one path element by index.
  692. *
  693. * @param vpath pointer to vfs_path_t object.
  694. * May be NULL. In this case NULL is returned and errno set to 0.
  695. * @param element_index element index. May have negative value (in this case count was started at
  696. * the end of list). If @element_index is out of range, NULL is returned and
  697. * errno set to EINVAL.
  698. *
  699. * @return path element
  700. */
  701. const vfs_path_element_t *
  702. vfs_path_get_by_index (const vfs_path_t *vpath, int element_index)
  703. {
  704. int n;
  705. if (vpath == NULL)
  706. {
  707. errno = 0;
  708. return NULL;
  709. }
  710. n = vfs_path_elements_count (vpath);
  711. if (element_index < 0)
  712. element_index += n;
  713. if (element_index < 0 || element_index > n)
  714. {
  715. errno = EINVAL;
  716. return NULL;
  717. }
  718. return g_array_index (vpath->path, vfs_path_element_t *, element_index);
  719. }
  720. /* --------------------------------------------------------------------------------------------- */
  721. /*
  722. * Clone one path element
  723. *
  724. * @param element pointer to vfs_path_element_t object
  725. *
  726. * @return Newly allocated path element
  727. */
  728. vfs_path_element_t *
  729. vfs_path_element_clone (const vfs_path_element_t *element)
  730. {
  731. vfs_path_element_t *new_element = g_new (vfs_path_element_t, 1);
  732. new_element->user = g_strdup (element->user);
  733. new_element->password = g_strdup (element->password);
  734. new_element->host = g_strdup (element->host);
  735. new_element->ipv6 = element->ipv6;
  736. new_element->port = element->port;
  737. new_element->path = g_strdup (element->path);
  738. new_element->class = element->class;
  739. new_element->vfs_prefix = g_strdup (element->vfs_prefix);
  740. #ifdef HAVE_CHARSET
  741. new_element->encoding = g_strdup (element->encoding);
  742. if (vfs_path_element_need_cleanup_converter (element) && new_element->encoding != NULL)
  743. new_element->dir.converter = str_crt_conv_from (new_element->encoding);
  744. else
  745. new_element->dir.converter = element->dir.converter;
  746. #endif
  747. new_element->dir.info = element->dir.info;
  748. return new_element;
  749. }
  750. /* --------------------------------------------------------------------------------------------- */
  751. /*
  752. * Free one path element.
  753. *
  754. * @param element pointer to vfs_path_element_t object
  755. *
  756. */
  757. void
  758. vfs_path_element_free (vfs_path_element_t *element)
  759. {
  760. if (element == NULL)
  761. return;
  762. g_free (element->user);
  763. g_free (element->password);
  764. g_free (element->host);
  765. g_free (element->path);
  766. g_free (element->vfs_prefix);
  767. #ifdef HAVE_CHARSET
  768. g_free (element->encoding);
  769. if (vfs_path_element_need_cleanup_converter (element))
  770. str_close_conv (element->dir.converter);
  771. #endif
  772. g_free (element);
  773. }
  774. /* --------------------------------------------------------------------------------------------- */
  775. /*
  776. * Clone path
  777. *
  778. * @param vpath pointer to vfs_path_t object
  779. *
  780. * @return Newly allocated path object
  781. */
  782. vfs_path_t *
  783. vfs_path_clone (const vfs_path_t *vpath)
  784. {
  785. vfs_path_t *new_vpath;
  786. int vpath_element_index;
  787. if (vpath == NULL)
  788. return NULL;
  789. new_vpath = vfs_path_new (vpath->relative);
  790. for (vpath_element_index = 0; vpath_element_index < vfs_path_elements_count (vpath);
  791. vpath_element_index++)
  792. {
  793. vfs_path_element_t *path_element;
  794. path_element = vfs_path_element_clone (vfs_path_get_by_index (vpath, vpath_element_index));
  795. g_array_append_val (new_vpath->path, path_element);
  796. }
  797. new_vpath->str = g_strdup (vpath->str);
  798. return new_vpath;
  799. }
  800. /* --------------------------------------------------------------------------------------------- */
  801. /*
  802. * Free vfs_path_t object.
  803. *
  804. * @param vpath pointer to vfs_path_t object
  805. * @param free_str if TRUE the string representation of vpath is freed as well
  806. *
  807. * @return the string representation of vpath (i.e. NULL if free_str is TRUE)
  808. */
  809. char *
  810. vfs_path_free (vfs_path_t *vpath, gboolean free_str)
  811. {
  812. int vpath_element_index;
  813. char *ret;
  814. if (vpath == NULL)
  815. return NULL;
  816. for (vpath_element_index = 0; vpath_element_index < vfs_path_elements_count (vpath);
  817. vpath_element_index++)
  818. {
  819. vfs_path_element_t *path_element;
  820. path_element = (vfs_path_element_t *) vfs_path_get_by_index (vpath, vpath_element_index);
  821. vfs_path_element_free (path_element);
  822. }
  823. g_array_free (vpath->path, TRUE);
  824. if (!free_str)
  825. ret = vpath->str;
  826. else
  827. {
  828. g_free (vpath->str);
  829. ret = NULL;
  830. }
  831. g_free (vpath);
  832. return ret;
  833. }
  834. /* --------------------------------------------------------------------------------------------- */
  835. /*
  836. * Remove one path element by index
  837. *
  838. * @param vpath pointer to vfs_path_t object
  839. * @param element_index element index. May have negative value (in this case count was started at
  840. * the end of list).
  841. *
  842. */
  843. void
  844. vfs_path_remove_element_by_index (vfs_path_t *vpath, int element_index)
  845. {
  846. vfs_path_element_t *element;
  847. if ((vpath == NULL) || (vfs_path_elements_count (vpath) == 1))
  848. return;
  849. if (element_index < 0)
  850. element_index = vfs_path_elements_count (vpath) + element_index;
  851. element = (vfs_path_element_t *) vfs_path_get_by_index (vpath, element_index);
  852. vpath->path = g_array_remove_index (vpath->path, element_index);
  853. vfs_path_element_free (element);
  854. g_free (vpath->str);
  855. vpath->str = vfs_path_to_str_flags (vpath, 0, VPF_NONE);
  856. }
  857. /* --------------------------------------------------------------------------------------------- */
  858. /** Return VFS class for the given prefix */
  859. struct vfs_class *
  860. vfs_prefix_to_class (const char *prefix)
  861. {
  862. guint i;
  863. /* Avoid first class (localfs) that would accept any prefix */
  864. for (i = 1; i < vfs__classes_list->len; i++)
  865. {
  866. struct vfs_class *vfs;
  867. vfs = VFS_CLASS (g_ptr_array_index (vfs__classes_list, i));
  868. if (vfs->which != NULL)
  869. {
  870. if (vfs->which (vfs, prefix) == -1)
  871. continue;
  872. return vfs;
  873. }
  874. if (vfs->prefix != NULL && strncmp (prefix, vfs->prefix, strlen (vfs->prefix)) == 0)
  875. return vfs;
  876. }
  877. return NULL;
  878. }
  879. /* --------------------------------------------------------------------------------------------- */
  880. #ifdef HAVE_CHARSET
  881. /**
  882. * Check if need cleanup charset converter for vfs_path_element_t
  883. *
  884. * @param element part of path
  885. *
  886. * @return TRUE if need cleanup converter or FALSE otherwise
  887. */
  888. gboolean
  889. vfs_path_element_need_cleanup_converter (const vfs_path_element_t *element)
  890. {
  891. return (element->dir.converter != str_cnv_from_term && element->dir.converter != INVALID_CONV);
  892. }
  893. /* --------------------------------------------------------------------------------------------- */
  894. /**
  895. * Change encoding for last part (vfs_path_element_t) of vpath
  896. *
  897. * @param vpath pointer to path structure
  898. * encoding name of charset
  899. *
  900. * @return pointer to path structure (for use function in another functions)
  901. */
  902. vfs_path_t *
  903. vfs_path_change_encoding (vfs_path_t *vpath, const char *encoding)
  904. {
  905. vfs_path_element_t *path_element;
  906. path_element = (vfs_path_element_t *) vfs_path_get_by_index (vpath, -1);
  907. /* don't add current encoding */
  908. if ((path_element->encoding != NULL) && (strcmp (encoding, path_element->encoding) == 0))
  909. return vpath;
  910. g_free (path_element->encoding);
  911. path_element->encoding = g_strdup (encoding);
  912. if (vfs_path_element_need_cleanup_converter (path_element))
  913. str_close_conv (path_element->dir.converter);
  914. path_element->dir.converter = str_crt_conv_from (path_element->encoding);
  915. g_free (vpath->str);
  916. vpath->str = vfs_path_to_str_flags (vpath, 0, VPF_NONE);
  917. return vpath;
  918. }
  919. #endif
  920. /* --------------------------------------------------------------------------------------------- */
  921. /**
  922. * Serialize vfs_path_t object to string
  923. *
  924. * @param vpath data for serialization
  925. * @param error contain pointer to object for handle error code and message
  926. *
  927. * @return serialized vpath as newly allocated string
  928. */
  929. char *
  930. vfs_path_serialize (const vfs_path_t *vpath, GError **mcerror)
  931. {
  932. mc_config_t *cpath;
  933. ssize_t element_index;
  934. char *ret_value;
  935. mc_return_val_if_error (mcerror, FALSE);
  936. if ((vpath == NULL) || (vfs_path_elements_count (vpath) == 0))
  937. {
  938. mc_propagate_error (mcerror, 0, "%s", "vpath object is empty");
  939. return NULL;
  940. }
  941. cpath = mc_config_init (NULL, FALSE);
  942. for (element_index = 0; element_index < vfs_path_elements_count (vpath); element_index++)
  943. {
  944. char groupname[BUF_TINY];
  945. const vfs_path_element_t *element;
  946. g_snprintf (groupname, sizeof (groupname), "path-element-%zd", element_index);
  947. element = vfs_path_get_by_index (vpath, element_index);
  948. /* convert one element to config group */
  949. mc_config_set_string_raw (cpath, groupname, "path", element->path);
  950. mc_config_set_string_raw (cpath, groupname, "class-name", element->class->name);
  951. #ifdef HAVE_CHARSET
  952. mc_config_set_string_raw (cpath, groupname, "encoding", element->encoding);
  953. #endif
  954. mc_config_set_string_raw (cpath, groupname, "vfs_prefix", element->vfs_prefix);
  955. mc_config_set_string_raw (cpath, groupname, "user", element->user);
  956. mc_config_set_string_raw (cpath, groupname, "password", element->password);
  957. mc_config_set_string_raw (cpath, groupname, "host", element->host);
  958. if (element->port != 0)
  959. mc_config_set_int (cpath, groupname, "port", element->port);
  960. }
  961. ret_value = mc_serialize_config (cpath, mcerror);
  962. mc_config_deinit (cpath);
  963. return ret_value;
  964. }
  965. /* --------------------------------------------------------------------------------------------- */
  966. /**
  967. * Deserialize string to vfs_path_t object
  968. *
  969. * @param data data for serialization
  970. * @param error contain pointer to object for handle error code and message
  971. *
  972. * @return newly allocated vfs_path_t object
  973. */
  974. vfs_path_t *
  975. vfs_path_deserialize (const char *data, GError **mcerror)
  976. {
  977. mc_config_t *cpath;
  978. size_t element_index;
  979. vfs_path_t *vpath;
  980. mc_return_val_if_error (mcerror, FALSE);
  981. cpath = mc_deserialize_config (data, mcerror);
  982. if (cpath == NULL)
  983. return NULL;
  984. vpath = vfs_path_new (FALSE);
  985. for (element_index = 0;; element_index++)
  986. {
  987. struct vfs_class *eclass;
  988. vfs_path_element_t *element;
  989. char *cfg_value;
  990. char groupname[BUF_TINY];
  991. g_snprintf (groupname, sizeof (groupname), "path-element-%zu", element_index);
  992. if (!mc_config_has_group (cpath, groupname))
  993. break;
  994. cfg_value = mc_config_get_string_raw (cpath, groupname, "class-name", NULL);
  995. eclass = vfs_get_class_by_name (cfg_value);
  996. if (eclass == NULL)
  997. {
  998. vfs_path_free (vpath, TRUE);
  999. g_set_error (mcerror, MC_ERROR, 0, "Unable to find VFS class by name '%s'", cfg_value);
  1000. g_free (cfg_value);
  1001. mc_config_deinit (cpath);
  1002. return NULL;
  1003. }
  1004. g_free (cfg_value);
  1005. element = g_new0 (vfs_path_element_t, 1);
  1006. element->class = eclass;
  1007. element->path = mc_config_get_string_raw (cpath, groupname, "path", NULL);
  1008. #ifdef HAVE_CHARSET
  1009. element->encoding = mc_config_get_string_raw (cpath, groupname, "encoding", NULL);
  1010. element->dir.converter = (element->encoding != NULL) ? str_crt_conv_from (element->encoding)
  1011. : INVALID_CONV;
  1012. #endif
  1013. element->vfs_prefix = mc_config_get_string_raw (cpath, groupname, "vfs_prefix", NULL);
  1014. element->user = mc_config_get_string_raw (cpath, groupname, "user", NULL);
  1015. element->password = mc_config_get_string_raw (cpath, groupname, "password", NULL);
  1016. element->host = mc_config_get_string_raw (cpath, groupname, "host", NULL);
  1017. element->port = mc_config_get_int (cpath, groupname, "port", 0);
  1018. vpath->path = g_array_append_val (vpath->path, element);
  1019. }
  1020. mc_config_deinit (cpath);
  1021. if (vfs_path_elements_count (vpath) == 0)
  1022. {
  1023. vfs_path_free (vpath, TRUE);
  1024. g_set_error (mcerror, MC_ERROR, 0, "No any path elements found");
  1025. return NULL;
  1026. }
  1027. vpath->str = vfs_path_to_str_flags (vpath, 0, VPF_NONE);
  1028. return vpath;
  1029. }
  1030. /* --------------------------------------------------------------------------------------------- */
  1031. /**
  1032. * Build vfs_path_t object from arguments.
  1033. *
  1034. * @param first_element of path
  1035. * @param ... path tokens, terminated by NULL
  1036. *
  1037. * @return newly allocated vfs_path_t object
  1038. */
  1039. vfs_path_t *
  1040. vfs_path_build_filename (const char *first_element, ...)
  1041. {
  1042. va_list args;
  1043. char *str_path;
  1044. vfs_path_t *vpath;
  1045. if (first_element == NULL)
  1046. return NULL;
  1047. va_start (args, first_element);
  1048. str_path = mc_build_filenamev (first_element, args);
  1049. va_end (args);
  1050. vpath = vfs_path_from_str (str_path);
  1051. g_free (str_path);
  1052. return vpath;
  1053. }
  1054. /* --------------------------------------------------------------------------------------------- */
  1055. /**
  1056. * Append tokens to path object
  1057. *
  1058. * @param vpath path object
  1059. * @param first_element of path
  1060. * @param ... NULL-terminated strings
  1061. *
  1062. * @return newly allocated path object
  1063. */
  1064. vfs_path_t *
  1065. vfs_path_append_new (const vfs_path_t *vpath, const char *first_element, ...)
  1066. {
  1067. va_list args;
  1068. char *str_path;
  1069. const char *result_str;
  1070. vfs_path_t *ret_vpath;
  1071. if (vpath == NULL || first_element == NULL)
  1072. return NULL;
  1073. va_start (args, first_element);
  1074. str_path = mc_build_filenamev (first_element, args);
  1075. va_end (args);
  1076. result_str = vfs_path_as_str (vpath);
  1077. ret_vpath = vfs_path_build_filename (result_str, str_path, (char *) NULL);
  1078. g_free (str_path);
  1079. return ret_vpath;
  1080. }
  1081. /* --------------------------------------------------------------------------------------------- */
  1082. /**
  1083. * Append vpath_t tokens to path object
  1084. *
  1085. * @param first_vpath vpath objects
  1086. * @param ... NULL-terminated vpath objects
  1087. *
  1088. * @return newly allocated path object
  1089. */
  1090. vfs_path_t *
  1091. vfs_path_append_vpath_new (const vfs_path_t *first_vpath, ...)
  1092. {
  1093. va_list args;
  1094. vfs_path_t *ret_vpath;
  1095. const vfs_path_t *current_vpath = first_vpath;
  1096. if (first_vpath == NULL)
  1097. return NULL;
  1098. ret_vpath = vfs_path_new (FALSE);
  1099. va_start (args, first_vpath);
  1100. do
  1101. {
  1102. int vindex;
  1103. for (vindex = 0; vindex < vfs_path_elements_count (current_vpath); vindex++)
  1104. {
  1105. vfs_path_element_t *path_element;
  1106. path_element = vfs_path_element_clone (vfs_path_get_by_index (current_vpath, vindex));
  1107. g_array_append_val (ret_vpath->path, path_element);
  1108. }
  1109. current_vpath = va_arg (args, const vfs_path_t *);
  1110. }
  1111. while (current_vpath != NULL);
  1112. va_end (args);
  1113. ret_vpath->str = vfs_path_to_str_flags (ret_vpath, 0, VPF_NONE);
  1114. return ret_vpath;
  1115. }
  1116. /* --------------------------------------------------------------------------------------------- */
  1117. /**
  1118. * get tockens count in path.
  1119. *
  1120. * @param vpath path object
  1121. *
  1122. * @return count of tokens
  1123. */
  1124. size_t
  1125. vfs_path_tokens_count (const vfs_path_t *vpath)
  1126. {
  1127. size_t count_tokens = 0;
  1128. int element_index;
  1129. if (vpath == NULL)
  1130. return 0;
  1131. for (element_index = 0; element_index < vfs_path_elements_count (vpath); element_index++)
  1132. {
  1133. const vfs_path_element_t *element;
  1134. const char *token, *prev_token;
  1135. element = vfs_path_get_by_index (vpath, element_index);
  1136. for (prev_token = element->path; (token = strchr (prev_token, PATH_SEP)) != NULL;
  1137. prev_token = token + 1)
  1138. {
  1139. /* skip empty substring */
  1140. if (token != prev_token)
  1141. count_tokens++;
  1142. }
  1143. if (*prev_token != '\0')
  1144. count_tokens++;
  1145. }
  1146. return count_tokens;
  1147. }
  1148. /* --------------------------------------------------------------------------------------------- */
  1149. /**
  1150. * Get subpath by tokens
  1151. *
  1152. * @param vpath path object
  1153. * @param start_position first token for got/ Started from 0.
  1154. * If negative, then position will be relative to end of path
  1155. * @param length count of tokens
  1156. *
  1157. * @return newly allocated string with path tokens separated by slash
  1158. */
  1159. char *
  1160. vfs_path_tokens_get (const vfs_path_t *vpath, ssize_t start_position, ssize_t length)
  1161. {
  1162. GString *ret_tokens, *element_tokens;
  1163. int element_index;
  1164. size_t tokens_count = vfs_path_tokens_count (vpath);
  1165. if (vpath == NULL)
  1166. return NULL;
  1167. if (length == 0)
  1168. length = tokens_count;
  1169. if (length < 0)
  1170. length = tokens_count + length;
  1171. if (start_position < 0)
  1172. start_position = (ssize_t) tokens_count + start_position;
  1173. if (start_position < 0)
  1174. return NULL;
  1175. if (start_position >= (ssize_t) tokens_count)
  1176. return NULL;
  1177. if (start_position + (ssize_t) length > (ssize_t) tokens_count)
  1178. length = tokens_count - start_position;
  1179. ret_tokens = g_string_sized_new (32);
  1180. element_tokens = g_string_sized_new (32);
  1181. for (element_index = 0; element_index < vfs_path_elements_count (vpath); element_index++)
  1182. {
  1183. const vfs_path_element_t *element;
  1184. char **path_tokens, **iterator;
  1185. g_string_assign (element_tokens, "");
  1186. element = vfs_path_get_by_index (vpath, element_index);
  1187. path_tokens = g_strsplit (element->path, PATH_SEP_STR, -1);
  1188. for (iterator = path_tokens; *iterator != NULL; iterator++)
  1189. {
  1190. if (**iterator != '\0')
  1191. {
  1192. if (start_position == 0)
  1193. {
  1194. if (length == 0)
  1195. {
  1196. vfs_path_tokens_add_class_info (element, ret_tokens, element_tokens);
  1197. g_string_free (element_tokens, TRUE);
  1198. g_strfreev (path_tokens);
  1199. return g_string_free (ret_tokens, FALSE);
  1200. }
  1201. length--;
  1202. if (element_tokens->len != 0)
  1203. g_string_append_c (element_tokens, PATH_SEP);
  1204. g_string_append (element_tokens, *iterator);
  1205. }
  1206. else
  1207. start_position--;
  1208. }
  1209. }
  1210. g_strfreev (path_tokens);
  1211. vfs_path_tokens_add_class_info (element, ret_tokens, element_tokens);
  1212. }
  1213. g_string_free (element_tokens, TRUE);
  1214. return g_string_free (ret_tokens, !(start_position == 0 && length == 0));
  1215. }
  1216. /* --------------------------------------------------------------------------------------------- */
  1217. /**
  1218. * Get subpath by tokens
  1219. *
  1220. * @param vpath path object
  1221. * @param start_position first token for got/ Started from 0.
  1222. * If negative, then position will be relative to end of path
  1223. * @param length count of tokens
  1224. *
  1225. * @return newly allocated path object with path tokens separated by slash
  1226. */
  1227. vfs_path_t *
  1228. vfs_path_vtokens_get (const vfs_path_t *vpath, ssize_t start_position, ssize_t length)
  1229. {
  1230. char *str_tokens;
  1231. vfs_path_t *ret_vpath = NULL;
  1232. str_tokens = vfs_path_tokens_get (vpath, start_position, length);
  1233. if (str_tokens != NULL)
  1234. {
  1235. ret_vpath = vfs_path_from_str_flags (str_tokens, VPF_NO_CANON);
  1236. g_free (str_tokens);
  1237. }
  1238. return ret_vpath;
  1239. }
  1240. /* --------------------------------------------------------------------------------------------- */
  1241. /**
  1242. * Build URL parameters (such as user:pass @ host:port) from one path element object
  1243. *
  1244. * @param element path element
  1245. * @param keep_password TRUE or FALSE
  1246. *
  1247. * @return newly allocated non-empty string or NULL
  1248. */
  1249. GString *
  1250. vfs_path_build_url_params_str (const vfs_path_element_t *element, gboolean keep_password)
  1251. {
  1252. GString *buffer;
  1253. if (element == NULL)
  1254. return NULL;
  1255. buffer = g_string_sized_new (64);
  1256. if (element->user != NULL)
  1257. g_string_append (buffer, element->user);
  1258. if (element->password != NULL && keep_password)
  1259. {
  1260. g_string_append_c (buffer, ':');
  1261. g_string_append (buffer, element->password);
  1262. }
  1263. if (element->host != NULL)
  1264. {
  1265. if ((element->user != NULL) || (element->password != NULL))
  1266. g_string_append_c (buffer, '@');
  1267. if (element->ipv6)
  1268. g_string_append_c (buffer, '[');
  1269. g_string_append (buffer, element->host);
  1270. if (element->ipv6)
  1271. g_string_append_c (buffer, ']');
  1272. if (element->port != 0)
  1273. g_string_append_printf (buffer, ":%d", element->port);
  1274. }
  1275. if (buffer->len != 0)
  1276. return buffer;
  1277. g_string_free (buffer, TRUE);
  1278. return NULL;
  1279. }
  1280. /* --------------------------------------------------------------------------------------------- */
  1281. /**
  1282. * Build pretty string representation of one path_element_t object
  1283. *
  1284. * @param element path element
  1285. *
  1286. * @return newly allocated string
  1287. */
  1288. GString *
  1289. vfs_path_element_build_pretty_path_str (const vfs_path_element_t *element)
  1290. {
  1291. GString *url_params, *pretty_path;
  1292. pretty_path = g_string_new (element->class->prefix);
  1293. g_string_append (pretty_path, VFS_PATH_URL_DELIMITER);
  1294. url_params = vfs_path_build_url_params_str (element, FALSE);
  1295. if (url_params != NULL)
  1296. {
  1297. g_string_append_len (pretty_path, url_params->str, url_params->len);
  1298. g_string_free (url_params, TRUE);
  1299. }
  1300. if (!IS_PATH_SEP (*element->path))
  1301. g_string_append_c (pretty_path, PATH_SEP);
  1302. return g_string_append (pretty_path, element->path);
  1303. }
  1304. /* --------------------------------------------------------------------------------------------- */
  1305. /**
  1306. * Compare two path objects as strings
  1307. *
  1308. * @param vpath1 first path object
  1309. * @param vpath2 second vpath object
  1310. *
  1311. * @return integer value like to strcmp.
  1312. */
  1313. gboolean
  1314. vfs_path_equal (const vfs_path_t *vpath1, const vfs_path_t *vpath2)
  1315. {
  1316. const char *path1, *path2;
  1317. gboolean ret_val;
  1318. if (vpath1 == NULL || vpath2 == NULL)
  1319. return FALSE;
  1320. path1 = vfs_path_as_str (vpath1);
  1321. path2 = vfs_path_as_str (vpath2);
  1322. ret_val = strcmp (path1, path2) == 0;
  1323. return ret_val;
  1324. }
  1325. /* --------------------------------------------------------------------------------------------- */
  1326. /**
  1327. * Compare two path objects as strings
  1328. *
  1329. * @param vpath1 first path object
  1330. * @param vpath2 second vpath object
  1331. * @param len number of first 'len' characters
  1332. *
  1333. * @return integer value like to strcmp.
  1334. */
  1335. gboolean
  1336. vfs_path_equal_len (const vfs_path_t *vpath1, const vfs_path_t *vpath2, size_t len)
  1337. {
  1338. const char *path1, *path2;
  1339. gboolean ret_val;
  1340. if (vpath1 == NULL || vpath2 == NULL)
  1341. return FALSE;
  1342. path1 = vfs_path_as_str (vpath1);
  1343. path2 = vfs_path_as_str (vpath2);
  1344. ret_val = strncmp (path1, path2, len) == 0;
  1345. return ret_val;
  1346. }
  1347. /* --------------------------------------------------------------------------------------------- */
  1348. /**
  1349. * Calculate path length in string representation
  1350. *
  1351. * @param vpath path object
  1352. *
  1353. * @return length of path
  1354. */
  1355. size_t
  1356. vfs_path_len (const vfs_path_t *vpath)
  1357. {
  1358. if (vpath == NULL)
  1359. return 0;
  1360. return strlen (vpath->str);
  1361. }
  1362. /* --------------------------------------------------------------------------------------------- */
  1363. /**
  1364. * Convert relative vpath object to absolute
  1365. *
  1366. * @param vpath path object
  1367. *
  1368. * @return absolute path object
  1369. */
  1370. vfs_path_t *
  1371. vfs_path_to_absolute (const vfs_path_t *vpath)
  1372. {
  1373. vfs_path_t *absolute_vpath;
  1374. const char *path_str;
  1375. if (!vpath->relative)
  1376. return vfs_path_clone (vpath);
  1377. path_str = vfs_path_as_str (vpath);
  1378. absolute_vpath = vfs_path_from_str (path_str);
  1379. return absolute_vpath;
  1380. }
  1381. /* --------------------------------------------------------------------------------------------- */