path.c 44 KB

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