path.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619
  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;
  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. cpath = mc_config_init (NULL, FALSE);
  885. for (element_index = 0; element_index < vfs_path_elements_count (vpath); element_index++)
  886. {
  887. char *groupname;
  888. const vfs_path_element_t *element;
  889. groupname = g_strdup_printf ("path-element-%zd", element_index);
  890. element = vfs_path_get_by_index (vpath, element_index);
  891. /* convert one element to config group */
  892. mc_config_set_string_raw (cpath, groupname, "path", element->path);
  893. mc_config_set_string_raw (cpath, groupname, "class-name", element->class->name);
  894. #ifdef HAVE_CHARSET
  895. mc_config_set_string_raw (cpath, groupname, "encoding", element->encoding);
  896. #endif
  897. mc_config_set_string_raw (cpath, groupname, "vfs_prefix", element->vfs_prefix);
  898. mc_config_set_string_raw (cpath, groupname, "user", element->user);
  899. mc_config_set_string_raw (cpath, groupname, "password", element->password);
  900. mc_config_set_string_raw (cpath, groupname, "host", element->host);
  901. if (element->port != 0)
  902. mc_config_set_int (cpath, groupname, "port", element->port);
  903. g_free (groupname);
  904. }
  905. ret_value = mc_serialize_config (cpath, error);
  906. mc_config_deinit (cpath);
  907. return ret_value;
  908. }
  909. /* --------------------------------------------------------------------------------------------- */
  910. /**
  911. * Deserialize string to vfs_path_t object
  912. *
  913. * @param data data for serialization
  914. * @param error contain pointer to object for handle error code and message
  915. *
  916. * @return newly allocated vfs_path_t object
  917. */
  918. vfs_path_t *
  919. vfs_path_deserialize (const char *data, GError ** error)
  920. {
  921. mc_config_t *cpath;
  922. size_t element_index = 0;
  923. vfs_path_t *vpath;
  924. cpath = mc_deserialize_config (data, error);
  925. if (cpath == NULL)
  926. return NULL;
  927. vpath = vfs_path_new ();
  928. while (TRUE)
  929. {
  930. vfs_path_element_t *element;
  931. char *cfg_value;
  932. char *groupname;
  933. groupname = g_strdup_printf ("path-element-%zd", element_index);
  934. if (!mc_config_has_group (cpath, groupname))
  935. {
  936. g_free (groupname);
  937. break;
  938. }
  939. element = g_new0 (vfs_path_element_t, 1);
  940. cfg_value = mc_config_get_string_raw (cpath, groupname, "class-name", NULL);
  941. element->class = vfs_get_class_by_name (cfg_value);
  942. if (element->class == NULL)
  943. {
  944. g_free (element);
  945. vfs_path_free (vpath);
  946. g_set_error (error, MC_ERROR, -1, "Unable to find VFS class by name '%s'", cfg_value);
  947. g_free (cfg_value);
  948. mc_config_deinit (cpath);
  949. return NULL;
  950. }
  951. g_free (cfg_value);
  952. element->path = mc_config_get_string_raw (cpath, groupname, "path", NULL);
  953. #ifdef HAVE_CHARSET
  954. element->encoding = mc_config_get_string_raw (cpath, groupname, "encoding", NULL);
  955. element->dir.converter =
  956. (element->encoding != NULL) ? str_crt_conv_from (element->encoding) : INVALID_CONV;
  957. #endif
  958. element->vfs_prefix = mc_config_get_string_raw (cpath, groupname, "vfs_prefix", NULL);
  959. element->user = mc_config_get_string_raw (cpath, groupname, "user", NULL);
  960. element->password = mc_config_get_string_raw (cpath, groupname, "password", NULL);
  961. element->host = mc_config_get_string_raw (cpath, groupname, "host", NULL);
  962. element->port = mc_config_get_int (cpath, groupname, "port", 0);
  963. vpath->path = g_array_append_val (vpath->path, element);
  964. g_free (groupname);
  965. element_index++;
  966. }
  967. mc_config_deinit (cpath);
  968. if (vfs_path_elements_count (vpath) == 0)
  969. {
  970. vfs_path_free (vpath);
  971. g_set_error (error, MC_ERROR, -1, "No any path elements found");
  972. return NULL;
  973. }
  974. return vpath;
  975. }
  976. /* --------------------------------------------------------------------------------------------- */
  977. /**
  978. * Build vfs_path_t object from arguments.
  979. *
  980. * @param ... path tokens, terminated by NULL
  981. *
  982. * @return newly allocated vfs_path_t object
  983. */
  984. vfs_path_t *
  985. vfs_path_build_filename (const char *first_element, ...)
  986. {
  987. va_list args;
  988. char *str_path;
  989. vfs_path_t *vpath;
  990. if (first_element == NULL)
  991. return NULL;
  992. va_start (args, first_element);
  993. str_path = mc_build_filenamev (first_element, args);
  994. va_end (args);
  995. vpath = vfs_path_from_str (str_path);
  996. g_free (str_path);
  997. return vpath;
  998. }
  999. /* --------------------------------------------------------------------------------------------- */
  1000. /**
  1001. * Append tokens to path object
  1002. *
  1003. * @param vpath path object
  1004. * @param ... NULL-terminated strings
  1005. *
  1006. * @return newly allocated path object
  1007. */
  1008. vfs_path_t *
  1009. vfs_path_append_new (const vfs_path_t * vpath, const char *first_element, ...)
  1010. {
  1011. va_list args;
  1012. char *str_path, *result_str;
  1013. vfs_path_t *ret_vpath;
  1014. if (vpath == NULL || first_element == NULL)
  1015. return NULL;
  1016. va_start (args, first_element);
  1017. str_path = mc_build_filenamev (first_element, args);
  1018. va_end (args);
  1019. result_str = vfs_path_to_str (vpath);
  1020. ret_vpath = vfs_path_build_filename (result_str, str_path, NULL);
  1021. g_free (result_str);
  1022. g_free (str_path);
  1023. return ret_vpath;
  1024. }
  1025. /* --------------------------------------------------------------------------------------------- */
  1026. /**
  1027. * Append vpath_t tokens to path object
  1028. *
  1029. * @param ... NULL-terminated vpath objects
  1030. *
  1031. * @return newly allocated path object
  1032. */
  1033. vfs_path_t *
  1034. vfs_path_append_vpath_new (const vfs_path_t * first_vpath, ...)
  1035. {
  1036. va_list args;
  1037. vfs_path_t *ret_vpath;
  1038. const vfs_path_t *current_vpath = first_vpath;
  1039. if (first_vpath == NULL)
  1040. return NULL;
  1041. ret_vpath = vfs_path_new ();
  1042. va_start (args, first_vpath);
  1043. do
  1044. {
  1045. int vindex;
  1046. for (vindex = 0; vindex < vfs_path_elements_count (current_vpath); vindex++)
  1047. {
  1048. vfs_path_element_t *path_element;
  1049. path_element = vfs_path_element_clone (vfs_path_get_by_index (current_vpath, vindex));
  1050. g_array_append_val (ret_vpath->path, path_element);
  1051. }
  1052. current_vpath = va_arg (args, const vfs_path_t *);
  1053. }
  1054. while (current_vpath != NULL);
  1055. va_end (args);
  1056. return ret_vpath;
  1057. }
  1058. /* --------------------------------------------------------------------------------------------- */
  1059. /**
  1060. * get tockens count in path.
  1061. *
  1062. * @param vpath path object
  1063. *
  1064. * @return count of tokens
  1065. */
  1066. size_t
  1067. vfs_path_tokens_count (const vfs_path_t * vpath)
  1068. {
  1069. size_t count_tokens = 0;
  1070. int element_index;
  1071. if (vpath == NULL)
  1072. return 0;
  1073. for (element_index = 0; element_index < vfs_path_elements_count (vpath); element_index++)
  1074. {
  1075. const vfs_path_element_t *element;
  1076. char **path_tokens, **iterator;
  1077. element = vfs_path_get_by_index (vpath, element_index);
  1078. path_tokens = iterator = g_strsplit (element->path, PATH_SEP_STR, -1);
  1079. while (*iterator != NULL)
  1080. {
  1081. if (**iterator != '\0')
  1082. count_tokens++;
  1083. iterator++;
  1084. }
  1085. g_strfreev (path_tokens);
  1086. }
  1087. return count_tokens;
  1088. }
  1089. /* --------------------------------------------------------------------------------------------- */
  1090. /**
  1091. * Get subpath by tokens
  1092. *
  1093. * @param vpath path object
  1094. * @param start_position first token for got/ Started from 0.
  1095. * If negative, then position will be relative to end of path
  1096. * @param length count of tokens
  1097. *
  1098. * @return newly allocated string with path tokens separated by slash
  1099. */
  1100. char *
  1101. vfs_path_tokens_get (const vfs_path_t * vpath, ssize_t start_position, ssize_t length)
  1102. {
  1103. GString *ret_tokens, *element_tokens;
  1104. int element_index;
  1105. size_t tokens_count = vfs_path_tokens_count (vpath);
  1106. if (vpath == NULL)
  1107. return NULL;
  1108. if (length == 0)
  1109. length = tokens_count;
  1110. if (length < 0)
  1111. length = tokens_count + length;
  1112. if (start_position < 0)
  1113. start_position = (ssize_t) tokens_count + start_position;
  1114. if (start_position < 0)
  1115. return NULL;
  1116. if (start_position >= (ssize_t) tokens_count)
  1117. return NULL;
  1118. if (start_position + (ssize_t) length > (ssize_t) tokens_count)
  1119. length = tokens_count - start_position;
  1120. ret_tokens = g_string_sized_new (32);
  1121. element_tokens = g_string_sized_new (32);
  1122. for (element_index = 0; element_index < vfs_path_elements_count (vpath); element_index++)
  1123. {
  1124. const vfs_path_element_t *element;
  1125. char **path_tokens, **iterator;
  1126. g_string_assign (element_tokens, "");
  1127. element = vfs_path_get_by_index (vpath, element_index);
  1128. path_tokens = iterator = g_strsplit (element->path, PATH_SEP_STR, -1);
  1129. while (*iterator != NULL)
  1130. {
  1131. if (**iterator != '\0')
  1132. {
  1133. if (start_position == 0)
  1134. {
  1135. if (length == 0)
  1136. {
  1137. vfs_path_tokens_add_class_info (element, ret_tokens, element_tokens);
  1138. g_string_free (element_tokens, TRUE);
  1139. g_strfreev (path_tokens);
  1140. return g_string_free (ret_tokens, FALSE);
  1141. }
  1142. length--;
  1143. if (element_tokens->len != 0)
  1144. g_string_append_c (element_tokens, PATH_SEP);
  1145. g_string_append (element_tokens, *iterator);
  1146. }
  1147. else
  1148. start_position--;
  1149. }
  1150. iterator++;
  1151. }
  1152. g_strfreev (path_tokens);
  1153. vfs_path_tokens_add_class_info (element, ret_tokens, element_tokens);
  1154. }
  1155. g_string_free (element_tokens, TRUE);
  1156. return g_string_free (ret_tokens, !(start_position == 0 && length == 0));
  1157. }
  1158. /* --------------------------------------------------------------------------------------------- */
  1159. /**
  1160. * Get subpath by tokens
  1161. *
  1162. * @param vpath path object
  1163. * @param start_position first token for got/ Started from 0.
  1164. * If negative, then position will be relative to end of path
  1165. * @param length count of tokens
  1166. *
  1167. * @return newly allocated path object with path tokens separated by slash
  1168. */
  1169. vfs_path_t *
  1170. vfs_path_vtokens_get (const vfs_path_t * vpath, ssize_t start_position, ssize_t length)
  1171. {
  1172. char *str_tokens;
  1173. vfs_path_t *ret_vpath = NULL;
  1174. str_tokens = vfs_path_tokens_get (vpath, start_position, length);
  1175. if (str_tokens != NULL)
  1176. {
  1177. ret_vpath = vfs_path_from_str_flags (str_tokens, VPF_NO_CANON);
  1178. g_free (str_tokens);
  1179. }
  1180. return ret_vpath;
  1181. }
  1182. /* --------------------------------------------------------------------------------------------- */
  1183. /**
  1184. * Build URL parameters (such as user:pass@host:port) from one path element object
  1185. *
  1186. * @param element path element
  1187. *
  1188. * @return newly allocated string
  1189. */
  1190. char *
  1191. vfs_path_build_url_params_str (const vfs_path_element_t * element, gboolean keep_password)
  1192. {
  1193. GString *buffer;
  1194. if (element == NULL)
  1195. return NULL;
  1196. buffer = g_string_new ("");
  1197. if (element->user != NULL)
  1198. g_string_append (buffer, element->user);
  1199. if (element->password != NULL && keep_password)
  1200. {
  1201. g_string_append_c (buffer, ':');
  1202. g_string_append (buffer, element->password);
  1203. }
  1204. if (element->host != NULL)
  1205. {
  1206. if ((element->user != NULL) || (element->password != NULL))
  1207. g_string_append_c (buffer, '@');
  1208. if (element->ipv6)
  1209. g_string_append_c (buffer, '[');
  1210. g_string_append (buffer, element->host);
  1211. if (element->ipv6)
  1212. g_string_append_c (buffer, ']');
  1213. }
  1214. if ((element->port) != 0 && (element->host != NULL))
  1215. {
  1216. g_string_append_c (buffer, ':');
  1217. g_string_append_printf (buffer, "%d", element->port);
  1218. }
  1219. return g_string_free (buffer, FALSE);
  1220. }
  1221. /* --------------------------------------------------------------------------------------------- */
  1222. /**
  1223. * Build pretty string representation of one path_element_t object
  1224. *
  1225. * @param element path element
  1226. *
  1227. * @return newly allocated string
  1228. */
  1229. char *
  1230. vfs_path_element_build_pretty_path_str (const vfs_path_element_t * element)
  1231. {
  1232. char *url_params;
  1233. GString *pretty_path;
  1234. pretty_path = g_string_new (element->class->prefix);
  1235. g_string_append (pretty_path, VFS_PATH_URL_DELIMITER);
  1236. url_params = vfs_path_build_url_params_str (element, FALSE);
  1237. g_string_append (pretty_path, url_params);
  1238. g_free (url_params);
  1239. if (*element->path != PATH_SEP)
  1240. g_string_append_c (pretty_path, PATH_SEP);
  1241. g_string_append (pretty_path, element->path);
  1242. return g_string_free (pretty_path, FALSE);
  1243. }
  1244. /* --------------------------------------------------------------------------------------------- */
  1245. /**
  1246. * Compare two path objects as strings
  1247. *
  1248. * @param vpath1 first path object
  1249. * @param vpath2 second vpath object
  1250. *
  1251. * @return integer value like to strcmp.
  1252. */
  1253. int
  1254. vfs_path_cmp (const vfs_path_t * vpath1, const vfs_path_t * vpath2)
  1255. {
  1256. char *path1;
  1257. char *path2;
  1258. int ret_val;
  1259. if (vpath1 == NULL || vpath2 == NULL)
  1260. return -1;
  1261. path1 = vfs_path_to_str (vpath1);
  1262. path2 = vfs_path_to_str (vpath2);
  1263. ret_val = strcmp (path1, path2);
  1264. g_free (path1);
  1265. g_free (path2);
  1266. return ret_val;
  1267. }
  1268. /* --------------------------------------------------------------------------------------------- */
  1269. /**
  1270. * Compare two path objects as strings
  1271. *
  1272. * @param vpath1 first path object
  1273. * @param vpath2 second vpath object
  1274. * @param len number of first 'len' characters
  1275. *
  1276. * @return integer value like to strcmp.
  1277. */
  1278. int
  1279. vfs_path_ncmp (const vfs_path_t * vpath1, const vfs_path_t * vpath2, size_t len)
  1280. {
  1281. char *path1;
  1282. char *path2;
  1283. int ret_val;
  1284. if (vpath1 == NULL || vpath2 == NULL)
  1285. return -1;
  1286. path1 = vfs_path_to_str (vpath1);
  1287. path2 = vfs_path_to_str (vpath2);
  1288. ret_val = strncmp (path1, path2, len);
  1289. g_free (path1);
  1290. g_free (path2);
  1291. return ret_val;
  1292. }
  1293. /* --------------------------------------------------------------------------------------------- */
  1294. /**
  1295. * Calculate path length in string representation
  1296. *
  1297. * @param vpath path object
  1298. *
  1299. * @return length of path
  1300. */
  1301. size_t
  1302. vfs_path_len (const vfs_path_t * vpath)
  1303. {
  1304. char *path;
  1305. size_t ret_val;
  1306. if (vpath == NULL)
  1307. return 0;
  1308. path = vfs_path_to_str (vpath);
  1309. ret_val = strlen (path);
  1310. g_free (path);
  1311. return ret_val;
  1312. }
  1313. /* --------------------------------------------------------------------------------------------- */
  1314. /**
  1315. * Convert relative vpath object to absolute
  1316. *
  1317. * @param vpath path object
  1318. *
  1319. * @return absolute path object
  1320. */
  1321. vfs_path_t *
  1322. vfs_path_to_absolute (const vfs_path_t * vpath)
  1323. {
  1324. vfs_path_t *absolute_vpath;
  1325. char *path_str;
  1326. if (!vpath->relative)
  1327. return vfs_path_clone (vpath);
  1328. path_str = vfs_path_to_str (vpath);
  1329. absolute_vpath = vfs_path_from_str (path_str);
  1330. g_free (path_str);
  1331. return absolute_vpath;
  1332. }
  1333. /* --------------------------------------------------------------------------------------------- */