path.c 46 KB

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