path.c 46 KB

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