path.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675
  1. /*
  2. Virtual File System path handlers
  3. Copyright (C) 2011-2017
  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. const char *pend;
  190. char *colon, *at, *rest;
  191. path_element->port = 0;
  192. pcopy = g_strdup (path);
  193. pend = pcopy + strlen (pcopy);
  194. /* search for any possible user */
  195. at = strrchr (pcopy, '@');
  196. /* We have a username */
  197. if (at == NULL)
  198. rest = pcopy;
  199. else
  200. {
  201. char *inner_colon;
  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] = '\0';
  226. colon[1] = '\0';
  227. colon++;
  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 || vfs_path_is_stdio (vpath))
  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 && !vfs_path_is_stdio (vpath)
  659. && 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 (vfs_path_t * vpath, const vfs_path_element_t * path_element)
  669. {
  670. if (vpath != NULL && !vfs_path_is_stdio (vpath) && path_element != NULL)
  671. {
  672. g_array_append_val (vpath->path, path_element);
  673. g_free (vpath->str);
  674. vpath->str = vfs_path_to_str_flags (vpath, 0, VPF_NONE);
  675. }
  676. }
  677. /* --------------------------------------------------------------------------------------------- */
  678. /*
  679. * Get one path element by index.
  680. *
  681. * @param vpath pointer to vfs_path_t object
  682. * @param element_index element index. May have negative value (in this case count was started
  683. * at the end of list).
  684. *
  685. * @return path element.
  686. */
  687. const vfs_path_element_t *
  688. vfs_path_get_by_index (const vfs_path_t * vpath, int element_index)
  689. {
  690. if (vpath == NULL || vfs_path_is_stdio (vpath))
  691. return NULL;
  692. if (element_index < 0)
  693. element_index += vfs_path_elements_count (vpath);
  694. if (element_index < 0)
  695. vfs_die ("vfs_path_get_by_index: incorrect index!");
  696. return g_array_index (vpath->path, vfs_path_element_t *, element_index);
  697. }
  698. /* --------------------------------------------------------------------------------------------- */
  699. /*
  700. * Clone one path element
  701. *
  702. * @param element pointer to vfs_path_element_t object
  703. *
  704. * @return Newly allocated path element
  705. */
  706. vfs_path_element_t *
  707. vfs_path_element_clone (const vfs_path_element_t * element)
  708. {
  709. vfs_path_element_t *new_element = g_new (vfs_path_element_t, 1);
  710. new_element->user = g_strdup (element->user);
  711. new_element->password = g_strdup (element->password);
  712. new_element->host = g_strdup (element->host);
  713. new_element->ipv6 = element->ipv6;
  714. new_element->port = element->port;
  715. new_element->path = g_strdup (element->path);
  716. new_element->class = element->class;
  717. new_element->vfs_prefix = g_strdup (element->vfs_prefix);
  718. #ifdef HAVE_CHARSET
  719. new_element->encoding = g_strdup (element->encoding);
  720. if (vfs_path_element_need_cleanup_converter (element) && new_element->encoding != NULL)
  721. new_element->dir.converter = str_crt_conv_from (new_element->encoding);
  722. else
  723. new_element->dir.converter = element->dir.converter;
  724. #endif
  725. new_element->dir.info = element->dir.info;
  726. return new_element;
  727. }
  728. /* --------------------------------------------------------------------------------------------- */
  729. /*
  730. * Free one path element.
  731. *
  732. * @param element pointer to vfs_path_element_t object
  733. *
  734. */
  735. void
  736. vfs_path_element_free (vfs_path_element_t * element)
  737. {
  738. if (element == NULL)
  739. return;
  740. g_free (element->user);
  741. g_free (element->password);
  742. g_free (element->host);
  743. g_free (element->path);
  744. g_free (element->vfs_prefix);
  745. #ifdef HAVE_CHARSET
  746. g_free (element->encoding);
  747. if (vfs_path_element_need_cleanup_converter (element))
  748. str_close_conv (element->dir.converter);
  749. #endif
  750. g_free (element);
  751. }
  752. /* --------------------------------------------------------------------------------------------- */
  753. /*
  754. * Clone path
  755. *
  756. * @param vpath pointer to vfs_path_t object
  757. *
  758. * @return Newly allocated path object
  759. */
  760. vfs_path_t *
  761. vfs_path_clone (const vfs_path_t * vpath)
  762. {
  763. vfs_path_t *new_vpath;
  764. int vpath_element_index;
  765. if (vpath == NULL || vfs_path_is_stdio (vpath))
  766. return VFS_PATH (vpath);
  767. new_vpath = vfs_path_new ();
  768. new_vpath->relative = vpath->relative;
  769. for (vpath_element_index = 0; vpath_element_index < vfs_path_elements_count (vpath);
  770. vpath_element_index++)
  771. {
  772. vfs_path_element_t *path_element;
  773. path_element = vfs_path_element_clone (vfs_path_get_by_index (vpath, vpath_element_index));
  774. g_array_append_val (new_vpath->path, path_element);
  775. }
  776. new_vpath->str = g_strdup (vpath->str);
  777. return new_vpath;
  778. }
  779. /* --------------------------------------------------------------------------------------------- */
  780. /*
  781. * Free vfs_path_t object.
  782. *
  783. * @param vpath pointer to vfs_path_t object
  784. *
  785. */
  786. void
  787. vfs_path_free (vfs_path_t * vpath)
  788. {
  789. int vpath_element_index;
  790. if (vpath == NULL || vfs_path_is_stdio (vpath))
  791. return;
  792. for (vpath_element_index = 0; vpath_element_index < vfs_path_elements_count (vpath);
  793. vpath_element_index++)
  794. {
  795. vfs_path_element_t *path_element;
  796. path_element = (vfs_path_element_t *) vfs_path_get_by_index (vpath, vpath_element_index);
  797. vfs_path_element_free (path_element);
  798. }
  799. g_array_free (vpath->path, TRUE);
  800. g_free (vpath->str);
  801. g_free (vpath);
  802. }
  803. /* --------------------------------------------------------------------------------------------- */
  804. /*
  805. * Remove one path element by index
  806. *
  807. * @param vpath pointer to vfs_path_t object
  808. * @param element_index element index. May have negative value (in this case count was started at the end of list).
  809. *
  810. */
  811. void
  812. vfs_path_remove_element_by_index (vfs_path_t * vpath, int element_index)
  813. {
  814. vfs_path_element_t *element;
  815. if (vpath == NULL || vfs_path_is_stdio (vpath) || vfs_path_elements_count (vpath) == 1)
  816. return;
  817. if (element_index < 0)
  818. element_index = vfs_path_elements_count (vpath) + element_index;
  819. element = (vfs_path_element_t *) vfs_path_get_by_index (vpath, element_index);
  820. vpath->path = g_array_remove_index (vpath->path, element_index);
  821. vfs_path_element_free (element);
  822. g_free (vpath->str);
  823. vpath->str = vfs_path_to_str_flags (vpath, 0, VPF_NONE);
  824. }
  825. /* --------------------------------------------------------------------------------------------- */
  826. /** Return VFS class for the given prefix */
  827. struct vfs_class *
  828. vfs_prefix_to_class (const char *prefix)
  829. {
  830. guint i;
  831. /* Avoid first class (localfs) that would accept any prefix */
  832. for (i = 1; i < vfs__classes_list->len; i++)
  833. {
  834. struct vfs_class *vfs;
  835. vfs = (struct vfs_class *) g_ptr_array_index (vfs__classes_list, i);
  836. if (vfs->which != NULL)
  837. {
  838. if (vfs->which (vfs, prefix) == -1)
  839. continue;
  840. return vfs;
  841. }
  842. if (vfs->prefix != NULL && strncmp (prefix, vfs->prefix, strlen (vfs->prefix)) == 0)
  843. return vfs;
  844. }
  845. return NULL;
  846. }
  847. /* --------------------------------------------------------------------------------------------- */
  848. #ifdef HAVE_CHARSET
  849. /**
  850. * Check if need cleanup charset converter for vfs_path_element_t
  851. *
  852. * @param element part of path
  853. *
  854. * @return TRUE if need cleanup converter or FALSE otherwise
  855. */
  856. gboolean
  857. vfs_path_element_need_cleanup_converter (const vfs_path_element_t * element)
  858. {
  859. return (element->dir.converter != str_cnv_from_term && element->dir.converter != INVALID_CONV);
  860. }
  861. /* --------------------------------------------------------------------------------------------- */
  862. /**
  863. * Change encoding for last part (vfs_path_element_t) of vpath
  864. *
  865. * @param vpath pointer to path structure
  866. * encoding name of charset
  867. *
  868. * @return pointer to path structure (for use function in anoter functions)
  869. */
  870. vfs_path_t *
  871. vfs_path_change_encoding (vfs_path_t * vpath, const char *encoding)
  872. {
  873. vfs_path_element_t *path_element;
  874. if (vpath == NULL || vfs_path_is_stdio (vpath))
  875. return VFS_PATH (vpath);
  876. path_element = (vfs_path_element_t *) vfs_path_get_by_index (vpath, -1);
  877. /* don't add current encoding */
  878. if ((path_element->encoding != NULL) && (strcmp (encoding, path_element->encoding) == 0))
  879. return vpath;
  880. g_free (path_element->encoding);
  881. path_element->encoding = g_strdup (encoding);
  882. if (vfs_path_element_need_cleanup_converter (path_element))
  883. str_close_conv (path_element->dir.converter);
  884. path_element->dir.converter = str_crt_conv_from (path_element->encoding);
  885. g_free (vpath->str);
  886. vpath->str = vfs_path_to_str_flags (vpath, 0, VPF_NONE);
  887. return vpath;
  888. }
  889. #endif
  890. /* --------------------------------------------------------------------------------------------- */
  891. /**
  892. * Serialize vfs_path_t object to string
  893. *
  894. * @param vpath data for serialization
  895. * @param error contain pointer to object for handle error code and message
  896. *
  897. * @return serialized vpath as newly allocated string
  898. */
  899. char *
  900. vfs_path_serialize (const vfs_path_t * vpath, GError ** mcerror)
  901. {
  902. mc_config_t *cpath;
  903. ssize_t element_index;
  904. char *ret_value;
  905. mc_return_val_if_error (mcerror, FALSE);
  906. if (vfs_path_is_stdio (vpath))
  907. return NULL;
  908. if ((vpath == NULL) || (vfs_path_elements_count (vpath) == 0))
  909. {
  910. mc_propagate_error (mcerror, 0, "%s", "vpath object is empty");
  911. return NULL;
  912. }
  913. cpath = mc_config_init (NULL, FALSE);
  914. for (element_index = 0; element_index < vfs_path_elements_count (vpath); element_index++)
  915. {
  916. char groupname[BUF_TINY];
  917. const vfs_path_element_t *element;
  918. g_snprintf (groupname, sizeof (groupname), "path-element-%zd", element_index);
  919. element = vfs_path_get_by_index (vpath, element_index);
  920. /* convert one element to config group */
  921. mc_config_set_string_raw (cpath, groupname, "path", element->path);
  922. mc_config_set_string_raw (cpath, groupname, "class-name", element->class->name);
  923. #ifdef HAVE_CHARSET
  924. mc_config_set_string_raw (cpath, groupname, "encoding", element->encoding);
  925. #endif
  926. mc_config_set_string_raw (cpath, groupname, "vfs_prefix", element->vfs_prefix);
  927. mc_config_set_string_raw (cpath, groupname, "user", element->user);
  928. mc_config_set_string_raw (cpath, groupname, "password", element->password);
  929. mc_config_set_string_raw (cpath, groupname, "host", element->host);
  930. if (element->port != 0)
  931. mc_config_set_int (cpath, groupname, "port", element->port);
  932. }
  933. ret_value = mc_serialize_config (cpath, mcerror);
  934. mc_config_deinit (cpath);
  935. return ret_value;
  936. }
  937. /* --------------------------------------------------------------------------------------------- */
  938. /**
  939. * Deserialize string to vfs_path_t object
  940. *
  941. * @param data data for serialization
  942. * @param error contain pointer to object for handle error code and message
  943. *
  944. * @return newly allocated vfs_path_t object
  945. */
  946. vfs_path_t *
  947. vfs_path_deserialize (const char *data, GError ** mcerror)
  948. {
  949. mc_config_t *cpath;
  950. size_t element_index;
  951. vfs_path_t *vpath;
  952. mc_return_val_if_error (mcerror, FALSE);
  953. cpath = mc_deserialize_config (data, mcerror);
  954. if (cpath == NULL)
  955. return NULL;
  956. vpath = vfs_path_new ();
  957. for (element_index = 0;; element_index++)
  958. {
  959. struct vfs_class *eclass;
  960. vfs_path_element_t *element;
  961. char *cfg_value;
  962. char groupname[BUF_TINY];
  963. g_snprintf (groupname, sizeof (groupname), "path-element-%zu", element_index);
  964. if (!mc_config_has_group (cpath, groupname))
  965. break;
  966. cfg_value = mc_config_get_string_raw (cpath, groupname, "class-name", NULL);
  967. eclass = vfs_get_class_by_name (cfg_value);
  968. if (eclass == NULL)
  969. {
  970. vfs_path_free (vpath);
  971. g_set_error (mcerror, MC_ERROR, 0, "Unable to find VFS class by name '%s'", cfg_value);
  972. g_free (cfg_value);
  973. mc_config_deinit (cpath);
  974. return NULL;
  975. }
  976. g_free (cfg_value);
  977. element = g_new0 (vfs_path_element_t, 1);
  978. element->class = eclass;
  979. element->path = mc_config_get_string_raw (cpath, groupname, "path", NULL);
  980. #ifdef HAVE_CHARSET
  981. element->encoding = mc_config_get_string_raw (cpath, groupname, "encoding", NULL);
  982. element->dir.converter =
  983. (element->encoding != NULL) ? str_crt_conv_from (element->encoding) : INVALID_CONV;
  984. #endif
  985. element->vfs_prefix = mc_config_get_string_raw (cpath, groupname, "vfs_prefix", NULL);
  986. element->user = mc_config_get_string_raw (cpath, groupname, "user", NULL);
  987. element->password = mc_config_get_string_raw (cpath, groupname, "password", NULL);
  988. element->host = mc_config_get_string_raw (cpath, groupname, "host", NULL);
  989. element->port = mc_config_get_int (cpath, groupname, "port", 0);
  990. vpath->path = g_array_append_val (vpath->path, element);
  991. }
  992. mc_config_deinit (cpath);
  993. if (vfs_path_elements_count (vpath) == 0)
  994. {
  995. vfs_path_free (vpath);
  996. g_set_error (mcerror, MC_ERROR, 0, "No any path elements found");
  997. return NULL;
  998. }
  999. vpath->str = vfs_path_to_str_flags (vpath, 0, VPF_NONE);
  1000. return vpath;
  1001. }
  1002. /* --------------------------------------------------------------------------------------------- */
  1003. /**
  1004. * Build vfs_path_t object from arguments.
  1005. *
  1006. * @param first_element of path
  1007. * @param ... path tokens, terminated by NULL
  1008. *
  1009. * @return newly allocated vfs_path_t object
  1010. */
  1011. vfs_path_t *
  1012. vfs_path_build_filename (const char *first_element, ...)
  1013. {
  1014. va_list args;
  1015. char *str_path;
  1016. vfs_path_t *vpath;
  1017. if (first_element == NULL)
  1018. return NULL;
  1019. va_start (args, first_element);
  1020. str_path = mc_build_filenamev (first_element, args);
  1021. va_end (args);
  1022. vpath = vfs_path_from_str (str_path);
  1023. g_free (str_path);
  1024. return vpath;
  1025. }
  1026. /* --------------------------------------------------------------------------------------------- */
  1027. /**
  1028. * Append tokens to path object
  1029. *
  1030. * @param vpath path object
  1031. * @param first_element of path
  1032. * @param ... NULL-terminated strings
  1033. *
  1034. * @return newly allocated path object
  1035. */
  1036. vfs_path_t *
  1037. vfs_path_append_new (const vfs_path_t * vpath, const char *first_element, ...)
  1038. {
  1039. va_list args;
  1040. char *str_path;
  1041. const char *result_str;
  1042. vfs_path_t *ret_vpath;
  1043. if (vpath == NULL || vfs_path_is_stdio (vpath) || first_element == NULL)
  1044. return NULL;
  1045. va_start (args, first_element);
  1046. str_path = mc_build_filenamev (first_element, args);
  1047. va_end (args);
  1048. result_str = vfs_path_as_str (vpath);
  1049. ret_vpath = vfs_path_build_filename (result_str, str_path, (char *) NULL);
  1050. g_free (str_path);
  1051. return ret_vpath;
  1052. }
  1053. /* --------------------------------------------------------------------------------------------- */
  1054. /**
  1055. * Append vpath_t tokens to path object
  1056. *
  1057. * @param first_vpath vpath objects
  1058. * @param ... NULL-terminated vpath objects
  1059. *
  1060. * @return newly allocated path object
  1061. */
  1062. vfs_path_t *
  1063. vfs_path_append_vpath_new (const vfs_path_t * first_vpath, ...)
  1064. {
  1065. va_list args;
  1066. vfs_path_t *ret_vpath;
  1067. const vfs_path_t *current_vpath = first_vpath;
  1068. if (first_vpath == NULL || vfs_path_is_stdio (first_vpath))
  1069. return NULL;
  1070. ret_vpath = vfs_path_new ();
  1071. va_start (args, first_vpath);
  1072. do
  1073. {
  1074. int vindex;
  1075. for (vindex = 0; vindex < vfs_path_elements_count (current_vpath); vindex++)
  1076. {
  1077. vfs_path_element_t *path_element;
  1078. path_element = vfs_path_element_clone (vfs_path_get_by_index (current_vpath, vindex));
  1079. g_array_append_val (ret_vpath->path, path_element);
  1080. }
  1081. current_vpath = va_arg (args, const vfs_path_t *);
  1082. }
  1083. while (current_vpath != NULL);
  1084. va_end (args);
  1085. ret_vpath->str = vfs_path_to_str_flags (ret_vpath, 0, VPF_NONE);
  1086. return ret_vpath;
  1087. }
  1088. /* --------------------------------------------------------------------------------------------- */
  1089. /**
  1090. * get tockens count in path.
  1091. *
  1092. * @param vpath path object
  1093. *
  1094. * @return count of tokens
  1095. */
  1096. size_t
  1097. vfs_path_tokens_count (const vfs_path_t * vpath)
  1098. {
  1099. size_t count_tokens = 0;
  1100. int element_index;
  1101. if (vpath == NULL || vfs_path_is_stdio (vpath))
  1102. return 0;
  1103. for (element_index = 0; element_index < vfs_path_elements_count (vpath); element_index++)
  1104. {
  1105. const vfs_path_element_t *element;
  1106. const char *token, *prev_token;
  1107. element = vfs_path_get_by_index (vpath, element_index);
  1108. for (prev_token = element->path; (token = strchr (prev_token, PATH_SEP)) != NULL;
  1109. prev_token = token + 1)
  1110. {
  1111. /* skip empty substring */
  1112. if (token != prev_token)
  1113. count_tokens++;
  1114. }
  1115. if (*prev_token != '\0')
  1116. count_tokens++;
  1117. }
  1118. return count_tokens;
  1119. }
  1120. /* --------------------------------------------------------------------------------------------- */
  1121. /**
  1122. * Get subpath by tokens
  1123. *
  1124. * @param vpath path object
  1125. * @param start_position first token for got/ Started from 0.
  1126. * If negative, then position will be relative to end of path
  1127. * @param length count of tokens
  1128. *
  1129. * @return newly allocated string with path tokens separated by slash
  1130. */
  1131. char *
  1132. vfs_path_tokens_get (const vfs_path_t * vpath, ssize_t start_position, ssize_t length)
  1133. {
  1134. GString *ret_tokens, *element_tokens;
  1135. int element_index;
  1136. size_t tokens_count;
  1137. if (vpath == NULL || vfs_path_is_stdio (vpath))
  1138. return NULL;
  1139. tokens_count = vfs_path_tokens_count (vpath);
  1140. if (length == 0)
  1141. length = tokens_count;
  1142. if (length < 0)
  1143. length = tokens_count + length;
  1144. if (start_position < 0)
  1145. start_position = (ssize_t) tokens_count + start_position;
  1146. if (start_position < 0)
  1147. return NULL;
  1148. if (start_position >= (ssize_t) tokens_count)
  1149. return NULL;
  1150. if (start_position + (ssize_t) length > (ssize_t) tokens_count)
  1151. length = tokens_count - start_position;
  1152. ret_tokens = g_string_sized_new (32);
  1153. element_tokens = g_string_sized_new (32);
  1154. for (element_index = 0; element_index < vfs_path_elements_count (vpath); element_index++)
  1155. {
  1156. const vfs_path_element_t *element;
  1157. char **path_tokens, **iterator;
  1158. g_string_assign (element_tokens, "");
  1159. element = vfs_path_get_by_index (vpath, element_index);
  1160. path_tokens = g_strsplit (element->path, PATH_SEP_STR, -1);
  1161. for (iterator = path_tokens; *iterator != NULL; iterator++)
  1162. {
  1163. if (**iterator != '\0')
  1164. {
  1165. if (start_position == 0)
  1166. {
  1167. if (length == 0)
  1168. {
  1169. vfs_path_tokens_add_class_info (element, ret_tokens, element_tokens);
  1170. g_string_free (element_tokens, TRUE);
  1171. g_strfreev (path_tokens);
  1172. return g_string_free (ret_tokens, FALSE);
  1173. }
  1174. length--;
  1175. if (element_tokens->len != 0)
  1176. g_string_append_c (element_tokens, PATH_SEP);
  1177. g_string_append (element_tokens, *iterator);
  1178. }
  1179. else
  1180. start_position--;
  1181. }
  1182. }
  1183. g_strfreev (path_tokens);
  1184. vfs_path_tokens_add_class_info (element, ret_tokens, element_tokens);
  1185. }
  1186. g_string_free (element_tokens, TRUE);
  1187. return g_string_free (ret_tokens, !(start_position == 0 && length == 0));
  1188. }
  1189. /* --------------------------------------------------------------------------------------------- */
  1190. /**
  1191. * Get subpath by tokens
  1192. *
  1193. * @param vpath path object
  1194. * @param start_position first token for got. Started from 0.
  1195. * If negative, then position will be relative to end of path
  1196. * @param length count of tokens
  1197. *
  1198. * @return newly allocated path object with path tokens separated by slash
  1199. */
  1200. vfs_path_t *
  1201. vfs_path_vtokens_get (const vfs_path_t * vpath, ssize_t start_position, ssize_t length)
  1202. {
  1203. vfs_path_t *ret_vpath = NULL;
  1204. if (vpath != NULL && !vfs_path_is_stdio (vpath))
  1205. {
  1206. char *str_tokens;
  1207. str_tokens = vfs_path_tokens_get (vpath, start_position, length);
  1208. if (str_tokens != NULL)
  1209. {
  1210. ret_vpath = vfs_path_from_str_flags (str_tokens, VPF_NO_CANON);
  1211. g_free (str_tokens);
  1212. }
  1213. }
  1214. return ret_vpath;
  1215. }
  1216. /* --------------------------------------------------------------------------------------------- */
  1217. /**
  1218. * Build URL parameters (such as user:pass @ host:port) from one path element object
  1219. *
  1220. * @param element path element
  1221. * @param keep_password TRUE or FALSE
  1222. *
  1223. * @return newly allocated string
  1224. */
  1225. char *
  1226. vfs_path_build_url_params_str (const vfs_path_element_t * element, gboolean keep_password)
  1227. {
  1228. GString *buffer;
  1229. if (element == NULL)
  1230. return NULL;
  1231. buffer = g_string_new ("");
  1232. if (element->user != NULL)
  1233. g_string_append (buffer, element->user);
  1234. if (element->password != NULL && keep_password)
  1235. {
  1236. g_string_append_c (buffer, ':');
  1237. g_string_append (buffer, element->password);
  1238. }
  1239. if (element->host != NULL)
  1240. {
  1241. if ((element->user != NULL) || (element->password != NULL))
  1242. g_string_append_c (buffer, '@');
  1243. if (element->ipv6)
  1244. g_string_append_c (buffer, '[');
  1245. g_string_append (buffer, element->host);
  1246. if (element->ipv6)
  1247. g_string_append_c (buffer, ']');
  1248. }
  1249. if ((element->port) != 0 && (element->host != NULL))
  1250. {
  1251. g_string_append_c (buffer, ':');
  1252. g_string_append_printf (buffer, "%d", element->port);
  1253. }
  1254. return g_string_free (buffer, FALSE);
  1255. }
  1256. /* --------------------------------------------------------------------------------------------- */
  1257. /**
  1258. * Build pretty string representation of one path_element_t object
  1259. *
  1260. * @param element path element
  1261. *
  1262. * @return newly allocated string
  1263. */
  1264. char *
  1265. vfs_path_element_build_pretty_path_str (const vfs_path_element_t * element)
  1266. {
  1267. char *url_params;
  1268. GString *pretty_path;
  1269. pretty_path = g_string_new (element->class->prefix);
  1270. g_string_append (pretty_path, VFS_PATH_URL_DELIMITER);
  1271. url_params = vfs_path_build_url_params_str (element, FALSE);
  1272. g_string_append (pretty_path, url_params);
  1273. g_free (url_params);
  1274. if (!IS_PATH_SEP (*element->path))
  1275. g_string_append_c (pretty_path, PATH_SEP);
  1276. g_string_append (pretty_path, element->path);
  1277. return g_string_free (pretty_path, FALSE);
  1278. }
  1279. /* --------------------------------------------------------------------------------------------- */
  1280. /**
  1281. * Compare two path objects as strings
  1282. *
  1283. * @param vpath1 first path object
  1284. * @param vpath2 second vpath object
  1285. *
  1286. * @return integer value like to strcmp.
  1287. */
  1288. gboolean
  1289. vfs_path_equal (const vfs_path_t * vpath1, const vfs_path_t * vpath2)
  1290. {
  1291. const char *path1, *path2;
  1292. gboolean stdio1, stdio2;
  1293. if (vpath1 == NULL)
  1294. return FALSE;
  1295. stdio1 = vfs_path_is_stdio (vpath1);
  1296. if (vpath2 == NULL)
  1297. return FALSE;
  1298. stdio2 = vfs_path_is_stdio (vpath2);
  1299. if (stdio1 && stdio2)
  1300. return (vpath1 == vpath2);
  1301. path1 = vfs_path_as_str (vpath1);
  1302. path2 = vfs_path_as_str (vpath2);
  1303. return (path1 != NULL && path2 != NULL && strcmp (path1, path2) == 0);
  1304. }
  1305. /* --------------------------------------------------------------------------------------------- */
  1306. /**
  1307. * Compare two path objects as strings
  1308. *
  1309. * @param vpath1 first path object
  1310. * @param vpath2 second vpath object
  1311. * @param len number of first 'len' characters
  1312. *
  1313. * @return integer value like to strcmp.
  1314. */
  1315. gboolean
  1316. vfs_path_equal_len (const vfs_path_t * vpath1, const vfs_path_t * vpath2, size_t len)
  1317. {
  1318. const char *path1, *path2;
  1319. gboolean stdio1, stdio2;
  1320. if (vpath1 == NULL)
  1321. return FALSE;
  1322. stdio1 = vfs_path_is_stdio (vpath1);
  1323. if (vpath2 == NULL)
  1324. return FALSE;
  1325. stdio2 = vfs_path_is_stdio (vpath2);
  1326. if (stdio1 && stdio2)
  1327. return (vpath1 == vpath2);
  1328. path1 = vfs_path_as_str (vpath1);
  1329. path2 = vfs_path_as_str (vpath2);
  1330. return (path1 != NULL && path2 != NULL && strncmp (path1, path2, len) == 0);
  1331. }
  1332. /* --------------------------------------------------------------------------------------------- */
  1333. /**
  1334. * Calculate path length in string representation
  1335. *
  1336. * @param vpath path object
  1337. *
  1338. * @return length of path
  1339. */
  1340. size_t
  1341. vfs_path_len (const vfs_path_t * vpath)
  1342. {
  1343. return (vpath == NULL || vfs_path_is_stdio (vpath) ? 0 : strlen (vpath->str));
  1344. }
  1345. /* --------------------------------------------------------------------------------------------- */
  1346. /**
  1347. * Convert relative vpath object to absolute
  1348. *
  1349. * @param vpath path object
  1350. *
  1351. * @return absolute path object
  1352. */
  1353. vfs_path_t *
  1354. vfs_path_to_absolute (const vfs_path_t * vpath)
  1355. {
  1356. vfs_path_t *absolute_vpath;
  1357. const char *path_str;
  1358. if (vpath == NULL || vfs_path_is_stdio (vpath))
  1359. return VFS_PATH (vpath);
  1360. if (!vpath->relative)
  1361. return vfs_path_clone (vpath);
  1362. path_str = vfs_path_as_str (vpath);
  1363. absolute_vpath = vfs_path_from_str (path_str);
  1364. return absolute_vpath;
  1365. }
  1366. /* --------------------------------------------------------------------------------------------- */