path.c 47 KB

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