path.c 47 KB

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