path.c 46 KB

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