path.c 44 KB

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