path.c 46 KB

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