path.c 44 KB

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