1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690 |
- #include <config.h>
- #include <errno.h>
- #include "lib/global.h"
- #include "lib/strutil.h"
- #include "lib/util.h"
- #include "lib/serialize.h"
- #include "vfs.h"
- #include "utilvfs.h"
- #include "xdirentry.h"
- #include "path.h"
- extern GPtrArray *vfs__classes_list;
- static gboolean
- path_magic (const char *path)
- {
- struct stat buf;
- return (stat (path, &buf) != 0);
- }
- static struct vfs_class *
- _vfs_split_with_semi_skip_count (char *path, const char **inpath, const char **op,
- size_t skip_count)
- {
- char *semi;
- char *slash;
- struct vfs_class *ret;
- if (path == NULL)
- vfs_die ("Cannot split NULL");
- semi = strrstr_skip_count (path, "#", skip_count);
- if ((semi == NULL) || (!path_magic (path)))
- return NULL;
- slash = strchr (semi, PATH_SEP);
- *semi = '\0';
- if (op != NULL)
- *op = NULL;
- if (inpath != NULL)
- *inpath = NULL;
- if (slash != NULL)
- *slash = '\0';
- ret = vfs_prefix_to_class (semi + 1);
- if (ret != NULL)
- {
- if (op != NULL)
- *op = semi + 1;
- if (inpath != NULL)
- *inpath = slash != NULL ? slash + 1 : NULL;
- return ret;
- }
- if (slash != NULL)
- *slash = PATH_SEP;
- *semi = '#';
- ret = _vfs_split_with_semi_skip_count (path, inpath, op, skip_count + 1);
- return ret;
- }
- static char *
- vfs_canon (const char *path)
- {
- char *result;
- if (path == NULL)
- vfs_die ("Cannot canonicalize NULL");
- if (!IS_PATH_SEP (*path))
- {
-
- char *local;
- #ifdef HAVE_CHARSET
- if (g_str_has_prefix (path, VFS_ENCODING_PREFIX))
- {
-
- local = mc_build_filename (PATH_SEP_STR, path, (char *) NULL);
- }
- else
- #endif
- {
- const char *curr_dir;
- curr_dir = vfs_get_current_dir ();
- local = mc_build_filename (curr_dir, path, (char *) NULL);
- }
- result = vfs_canon (local);
- g_free (local);
- }
- else
- {
-
- result = g_strdup (path);
- canonicalize_pathname (result);
- }
- return result;
- }
- static void
- vfs_path_url_split (vfs_path_element_t *path_element, const char *path)
- {
- char *pcopy;
- char *colon, *at, *rest;
- path_element->port = 0;
- pcopy = g_strdup (path);
-
- at = strrchr (pcopy, '@');
-
- if (at == NULL)
- rest = pcopy;
- else
- {
- const char *pend;
- char *inner_colon;
- pend = strchr (at, '\0');
- *at = '\0';
- inner_colon = strchr (pcopy, ':');
- if (inner_colon != NULL)
- {
- *inner_colon = '\0';
- inner_colon++;
- path_element->password = g_strdup (inner_colon);
- }
- if (*pcopy != '\0')
- path_element->user = g_strdup (pcopy);
- if (pend == at + 1)
- rest = at;
- else
- rest = at + 1;
- }
-
- if (*rest != '[')
- colon = strchr (rest, ':');
- else
- {
- colon = strchr (++rest, ']');
- if (colon != NULL)
- {
- *colon = '\0';
- colon++;
- *colon = '\0';
- path_element->ipv6 = TRUE;
- }
- }
- if (colon != NULL)
- {
- *colon = '\0';
-
- if (sscanf (colon + 1, "%d", &path_element->port) == 1)
- {
- if (path_element->port <= 0 || path_element->port >= 65536)
- path_element->port = 0;
- }
- else
- while (*(++colon) != '\0')
- {
- switch (*colon)
- {
- case 'C':
- path_element->port = 1;
- break;
- case 'r':
- path_element->port = 2;
- break;
- default:
- break;
- }
- }
- }
- path_element->host = g_strdup (rest);
- g_free (pcopy);
- }
- static struct vfs_class *
- vfs_get_class_by_name (const char *class_name)
- {
- guint i;
- if (class_name == NULL)
- return NULL;
- for (i = 0; i < vfs__classes_list->len; i++)
- {
- struct vfs_class *vfs = VFS_CLASS (g_ptr_array_index (vfs__classes_list, i));
- if ((vfs->name != NULL) && (strcmp (vfs->name, class_name) == 0))
- return vfs;
- }
- return NULL;
- }
- static gboolean
- vfs_path_is_str_path_deprecated (const char *path_str)
- {
- return strstr (path_str, VFS_PATH_URL_DELIMITER) == NULL;
- }
- static vfs_path_t *
- vfs_path_from_str_deprecated_parser (char *path)
- {
- vfs_path_t *vpath;
- vfs_path_element_t *element;
- struct vfs_class *class;
- const char *local, *op;
- vpath = vfs_path_new (FALSE);
- while ((class = _vfs_split_with_semi_skip_count (path, &local, &op, 0)) != NULL)
- {
- char *url_params;
- element = g_new0 (vfs_path_element_t, 1);
- element->class = class;
- if (local == NULL)
- local = "";
- element->path = vfs_translate_path_n (local);
- #ifdef HAVE_CHARSET
- element->encoding = vfs_get_encoding (local, -1);
- element->dir.converter =
- (element->encoding != NULL) ? str_crt_conv_from (element->encoding) : INVALID_CONV;
- #endif
- url_params = strchr (op, ':');
- if (url_params != NULL)
- {
- *url_params = '\0';
- url_params++;
- vfs_path_url_split (element, url_params);
- }
- if (*op != '\0')
- element->vfs_prefix = g_strdup (op);
- g_array_prepend_val (vpath->path, element);
- }
- if (path[0] != '\0')
- {
- element = g_new0 (vfs_path_element_t, 1);
- element->class = g_ptr_array_index (vfs__classes_list, 0);
- element->path = vfs_translate_path_n (path);
- #ifdef HAVE_CHARSET
- element->encoding = vfs_get_encoding (path, -1);
- element->dir.converter =
- (element->encoding != NULL) ? str_crt_conv_from (element->encoding) : INVALID_CONV;
- #endif
- g_array_prepend_val (vpath->path, element);
- }
- return vpath;
- }
- static vfs_path_t *
- vfs_path_from_str_uri_parser (char *path)
- {
- gboolean path_is_absolute;
- vfs_path_t *vpath;
- vfs_path_element_t *element;
- char *url_delimiter;
- if (path == NULL)
- return vfs_path_new (FALSE);
- path_is_absolute = IS_PATH_SEP (*path);
- #ifdef HAVE_CHARSET
- path_is_absolute = path_is_absolute || g_str_has_prefix (path, VFS_ENCODING_PREFIX);
- #endif
- vpath = vfs_path_new (!path_is_absolute);
- while ((url_delimiter = g_strrstr (path, VFS_PATH_URL_DELIMITER)) != NULL)
- {
- char *vfs_prefix_start;
- char *real_vfs_prefix_start = url_delimiter;
- while (real_vfs_prefix_start > path && !IS_PATH_SEP (*real_vfs_prefix_start))
- real_vfs_prefix_start--;
- vfs_prefix_start = real_vfs_prefix_start;
- if (IS_PATH_SEP (*vfs_prefix_start))
- vfs_prefix_start++;
- *url_delimiter = '\0';
- element = g_new0 (vfs_path_element_t, 1);
- element->class = vfs_prefix_to_class (vfs_prefix_start);
- element->vfs_prefix = g_strdup (vfs_prefix_start);
- url_delimiter += strlen (VFS_PATH_URL_DELIMITER);
- if (element->class != NULL && (element->class->flags & VFSF_REMOTE) != 0)
- {
- char *slash_pointer;
- slash_pointer = strchr (url_delimiter, PATH_SEP);
- if (slash_pointer == NULL)
- element->path = g_strdup ("");
- else
- {
- element->path = vfs_translate_path_n (slash_pointer + 1);
- #ifdef HAVE_CHARSET
- element->encoding = vfs_get_encoding (slash_pointer, -1);
- #endif
- *slash_pointer = '\0';
- }
- vfs_path_url_split (element, url_delimiter);
- }
- else
- {
- element->path = vfs_translate_path_n (url_delimiter);
- #ifdef HAVE_CHARSET
- element->encoding = vfs_get_encoding (url_delimiter, -1);
- #endif
- }
- #ifdef HAVE_CHARSET
- element->dir.converter =
- (element->encoding != NULL) ? str_crt_conv_from (element->encoding) : INVALID_CONV;
- #endif
- g_array_prepend_val (vpath->path, element);
- if ((real_vfs_prefix_start > path && IS_PATH_SEP (*real_vfs_prefix_start)) ||
- (real_vfs_prefix_start == path && !IS_PATH_SEP (*real_vfs_prefix_start)))
- *real_vfs_prefix_start = '\0';
- else
- *(real_vfs_prefix_start + 1) = '\0';
- }
- if (path[0] != '\0')
- {
- element = g_new0 (vfs_path_element_t, 1);
- element->class = g_ptr_array_index (vfs__classes_list, 0);
- element->path = vfs_translate_path_n (path);
- #ifdef HAVE_CHARSET
- element->encoding = vfs_get_encoding (path, -1);
- element->dir.converter =
- (element->encoding != NULL) ? str_crt_conv_from (element->encoding) : INVALID_CONV;
- #endif
- g_array_prepend_val (vpath->path, element);
- }
- return vpath;
- }
- static void
- vfs_path_tokens_add_class_info (const vfs_path_element_t *element, GString *ret_tokens,
- GString *element_tokens)
- {
- if (((element->class->flags & VFSF_LOCAL) == 0 || ret_tokens->len > 0)
- && element_tokens->len > 0)
- {
- GString *url_str;
- if (ret_tokens->len > 0 && !IS_PATH_SEP (ret_tokens->str[ret_tokens->len - 1]))
- g_string_append_c (ret_tokens, PATH_SEP);
- g_string_append (ret_tokens, element->vfs_prefix);
- g_string_append (ret_tokens, VFS_PATH_URL_DELIMITER);
- url_str = vfs_path_build_url_params_str (element, TRUE);
- if (url_str != NULL)
- {
- g_string_append_len (ret_tokens, url_str->str, url_str->len);
- g_string_append_c (ret_tokens, PATH_SEP);
- g_string_free (url_str, TRUE);
- }
- }
- #ifdef HAVE_CHARSET
- if (element->encoding != NULL)
- {
- if (ret_tokens->len > 0 && !IS_PATH_SEP (ret_tokens->str[ret_tokens->len - 1]))
- g_string_append (ret_tokens, PATH_SEP_STR);
- g_string_append (ret_tokens, VFS_ENCODING_PREFIX);
- g_string_append (ret_tokens, element->encoding);
- g_string_append (ret_tokens, PATH_SEP_STR);
- }
- #endif
- g_string_append (ret_tokens, element_tokens->str);
- }
- static char *
- vfs_path_strip_home (const char *dir)
- {
- const char *home_dir = mc_config_get_home_dir ();
- if (home_dir != NULL)
- {
- size_t len;
- len = strlen (home_dir);
- if (strncmp (dir, home_dir, len) == 0 && (IS_PATH_SEP (dir[len]) || dir[len] == '\0'))
- return g_strdup_printf ("~%s", dir + len);
- }
- return g_strdup (dir);
- }
- #define vfs_append_from_path(appendfrom, is_relative) \
- { \
- if ((flags & VPF_STRIP_HOME) && element_index == 0 && \
- (element->class->flags & VFSF_LOCAL) != 0) \
- { \
- char *stripped_home_str; \
- stripped_home_str = vfs_path_strip_home (appendfrom); \
- g_string_append (buffer, stripped_home_str); \
- g_free (stripped_home_str); \
- } \
- else \
- { \
- if (!is_relative && !IS_PATH_SEP (*appendfrom) && *appendfrom != '\0' \
- && (buffer->len == 0 || !IS_PATH_SEP (buffer->str[buffer->len - 1]))) \
- g_string_append_c (buffer, PATH_SEP); \
- g_string_append (buffer, appendfrom); \
- } \
- }
- char *
- vfs_path_to_str_flags (const vfs_path_t *vpath, int elements_count, vfs_path_flag_t flags)
- {
- int element_index;
- GString *buffer;
- #ifdef HAVE_CHARSET
- GString *recode_buffer = NULL;
- #endif
- if (vpath == NULL)
- return NULL;
- if (elements_count == 0 || elements_count > vfs_path_elements_count (vpath))
- elements_count = vfs_path_elements_count (vpath);
- if (elements_count < 0)
- elements_count = vfs_path_elements_count (vpath) + elements_count;
- buffer = g_string_new ("");
- for (element_index = 0; element_index < elements_count; element_index++)
- {
- const vfs_path_element_t *element;
- gboolean is_relative = vpath->relative && (element_index == 0);
- element = vfs_path_get_by_index (vpath, element_index);
- if (element->vfs_prefix != NULL)
- {
- GString *url_str;
- if (!is_relative && (buffer->len == 0 || !IS_PATH_SEP (buffer->str[buffer->len - 1])))
- g_string_append_c (buffer, PATH_SEP);
- g_string_append (buffer, element->vfs_prefix);
- g_string_append (buffer, VFS_PATH_URL_DELIMITER);
- url_str = vfs_path_build_url_params_str (element, (flags & VPF_STRIP_PASSWORD) == 0);
- if (url_str != NULL)
- {
- g_string_append_len (buffer, url_str->str, url_str->len);
- g_string_append_c (buffer, PATH_SEP);
- g_string_free (url_str, TRUE);
- }
- }
- #ifdef HAVE_CHARSET
- if ((flags & VPF_RECODE) == 0 && vfs_path_element_need_cleanup_converter (element))
- {
- if ((flags & VPF_HIDE_CHARSET) == 0)
- {
- if ((!is_relative)
- && (buffer->len == 0 || !IS_PATH_SEP (buffer->str[buffer->len - 1])))
- g_string_append (buffer, PATH_SEP_STR);
- g_string_append (buffer, VFS_ENCODING_PREFIX);
- g_string_append (buffer, element->encoding);
- }
- if (recode_buffer == NULL)
- recode_buffer = g_string_sized_new (32);
- else
- g_string_set_size (recode_buffer, 0);
- str_vfs_convert_from (element->dir.converter, element->path, recode_buffer);
- vfs_append_from_path (recode_buffer->str, is_relative);
- }
- else
- #endif
- {
- vfs_append_from_path (element->path, is_relative);
- }
- }
- #ifdef HAVE_CHARSET
- if (recode_buffer != NULL)
- g_string_free (recode_buffer, TRUE);
- #endif
- return g_string_free (buffer, FALSE);
- }
- #undef vfs_append_from_path
- char *
- vfs_path_to_str_elements_count (const vfs_path_t *vpath, int elements_count)
- {
- return vfs_path_to_str_flags (vpath, elements_count, VPF_NONE);
- }
- vfs_path_t *
- vfs_path_from_str_flags (const char *path_str, vfs_path_flag_t flags)
- {
- vfs_path_t *vpath;
- char *path;
- if (path_str == NULL)
- return NULL;
- if ((flags & VPF_NO_CANON) == 0)
- path = vfs_canon (path_str);
- else
- path = g_strdup (path_str);
- if (path == NULL)
- return NULL;
- if ((flags & VPF_USE_DEPRECATED_PARSER) != 0 && vfs_path_is_str_path_deprecated (path))
- vpath = vfs_path_from_str_deprecated_parser (path);
- else
- vpath = vfs_path_from_str_uri_parser (path);
- vpath->str = vfs_path_to_str_flags (vpath, 0, flags);
- g_free (path);
- return vpath;
- }
- vfs_path_t *
- vfs_path_from_str (const char *path_str)
- {
- return vfs_path_from_str_flags (path_str, VPF_NONE);
- }
- vfs_path_t *
- vfs_path_new (gboolean relative)
- {
- vfs_path_t *vpath;
- vpath = g_new0 (vfs_path_t, 1);
- vpath->path = g_array_new (FALSE, TRUE, sizeof (vfs_path_element_t *));
- vpath->relative = relative;
- return vpath;
- }
- int
- vfs_path_elements_count (const vfs_path_t *vpath)
- {
- return (vpath != NULL && vpath->path != NULL) ? vpath->path->len : 0;
- }
- void
- vfs_path_add_element (vfs_path_t *vpath, const vfs_path_element_t *path_element)
- {
- g_array_append_val (vpath->path, path_element);
- g_free (vpath->str);
- vpath->str = vfs_path_to_str_flags (vpath, 0, VPF_NONE);
- }
- const vfs_path_element_t *
- vfs_path_get_by_index (const vfs_path_t *vpath, int element_index)
- {
- int n;
- if (vpath == NULL)
- {
- errno = 0;
- return NULL;
- }
- n = vfs_path_elements_count (vpath);
- if (element_index < 0)
- element_index += n;
- if (element_index < 0 || element_index > n)
- {
- errno = EINVAL;
- return NULL;
- }
- return g_array_index (vpath->path, vfs_path_element_t *, element_index);
- }
- vfs_path_element_t *
- vfs_path_element_clone (const vfs_path_element_t *element)
- {
- vfs_path_element_t *new_element = g_new (vfs_path_element_t, 1);
- new_element->user = g_strdup (element->user);
- new_element->password = g_strdup (element->password);
- new_element->host = g_strdup (element->host);
- new_element->ipv6 = element->ipv6;
- new_element->port = element->port;
- new_element->path = g_strdup (element->path);
- new_element->class = element->class;
- new_element->vfs_prefix = g_strdup (element->vfs_prefix);
- #ifdef HAVE_CHARSET
- new_element->encoding = g_strdup (element->encoding);
- if (vfs_path_element_need_cleanup_converter (element) && element->encoding != NULL)
- new_element->dir.converter = str_crt_conv_from (element->encoding);
- else
- new_element->dir.converter = element->dir.converter;
- #endif
- new_element->dir.info = element->dir.info;
- return new_element;
- }
- void
- vfs_path_element_free (vfs_path_element_t *element)
- {
- if (element == NULL)
- return;
- g_free (element->user);
- g_free (element->password);
- g_free (element->host);
- g_free (element->path);
- g_free (element->vfs_prefix);
- #ifdef HAVE_CHARSET
- g_free (element->encoding);
- if (vfs_path_element_need_cleanup_converter (element))
- str_close_conv (element->dir.converter);
- #endif
- g_free (element);
- }
- vfs_path_t *
- vfs_path_clone (const vfs_path_t *vpath)
- {
- vfs_path_t *new_vpath;
- int vpath_element_index;
- if (vpath == NULL)
- return NULL;
- new_vpath = vfs_path_new (vpath->relative);
- for (vpath_element_index = 0; vpath_element_index < vfs_path_elements_count (vpath);
- vpath_element_index++)
- {
- vfs_path_element_t *path_element;
- path_element = vfs_path_element_clone (vfs_path_get_by_index (vpath, vpath_element_index));
- g_array_append_val (new_vpath->path, path_element);
- }
- new_vpath->str = g_strdup (vpath->str);
- return new_vpath;
- }
- char *
- vfs_path_free (vfs_path_t *vpath, gboolean free_str)
- {
- int vpath_element_index;
- char *ret;
- if (vpath == NULL)
- return NULL;
- for (vpath_element_index = 0; vpath_element_index < vfs_path_elements_count (vpath);
- vpath_element_index++)
- {
- vfs_path_element_t *path_element;
- path_element = (vfs_path_element_t *) vfs_path_get_by_index (vpath, vpath_element_index);
- vfs_path_element_free (path_element);
- }
- g_array_free (vpath->path, TRUE);
- if (!free_str)
- ret = vpath->str;
- else
- {
- g_free (vpath->str);
- ret = NULL;
- }
- g_free (vpath);
- return ret;
- }
- void
- vfs_path_remove_element_by_index (vfs_path_t *vpath, int element_index)
- {
- vfs_path_element_t *element;
- if ((vpath == NULL) || (vfs_path_elements_count (vpath) == 1))
- return;
- if (element_index < 0)
- element_index = vfs_path_elements_count (vpath) + element_index;
- element = (vfs_path_element_t *) vfs_path_get_by_index (vpath, element_index);
- vpath->path = g_array_remove_index (vpath->path, element_index);
- vfs_path_element_free (element);
- g_free (vpath->str);
- vpath->str = vfs_path_to_str_flags (vpath, 0, VPF_NONE);
- }
- struct vfs_class *
- vfs_prefix_to_class (const char *prefix)
- {
- guint i;
-
- for (i = 1; i < vfs__classes_list->len; i++)
- {
- struct vfs_class *vfs;
- vfs = VFS_CLASS (g_ptr_array_index (vfs__classes_list, i));
- if (vfs->which != NULL)
- {
- if (vfs->which (vfs, prefix) == -1)
- continue;
- return vfs;
- }
- if (vfs->prefix != NULL && strncmp (prefix, vfs->prefix, strlen (vfs->prefix)) == 0)
- return vfs;
- }
- return NULL;
- }
- #ifdef HAVE_CHARSET
- char *
- vfs_get_encoding (const char *path, ssize_t len)
- {
- char *semi;
-
- semi = g_strrstr_len (path, len, VFS_ENCODING_PREFIX);
- if (semi == NULL)
- return NULL;
- if (semi == path || IS_PATH_SEP (semi[-1]))
- {
- char *slash;
- semi += strlen (VFS_ENCODING_PREFIX);
- slash = strchr (semi, PATH_SEP);
- if (slash != NULL)
- return g_strndup (semi, slash - semi);
- return g_strdup (semi);
- }
- return vfs_get_encoding (path, semi - path);
- }
- gboolean
- vfs_path_element_need_cleanup_converter (const vfs_path_element_t *element)
- {
- return (element->dir.converter != str_cnv_from_term && element->dir.converter != INVALID_CONV);
- }
- vfs_path_t *
- vfs_path_change_encoding (vfs_path_t *vpath, const char *encoding)
- {
- vfs_path_element_t *path_element;
- path_element = (vfs_path_element_t *) vfs_path_get_by_index (vpath, -1);
-
- if ((path_element->encoding != NULL) && (strcmp (encoding, path_element->encoding) == 0))
- return vpath;
- g_free (path_element->encoding);
- path_element->encoding = g_strdup (encoding);
- if (vfs_path_element_need_cleanup_converter (path_element))
- str_close_conv (path_element->dir.converter);
- path_element->dir.converter = str_crt_conv_from (path_element->encoding);
- g_free (vpath->str);
- vpath->str = vfs_path_to_str_flags (vpath, 0, VPF_NONE);
- return vpath;
- }
- #endif
- char *
- vfs_path_serialize (const vfs_path_t *vpath, GError **mcerror)
- {
- mc_config_t *cpath;
- ssize_t element_index;
- char *ret_value;
- mc_return_val_if_error (mcerror, FALSE);
- if ((vpath == NULL) || (vfs_path_elements_count (vpath) == 0))
- {
- mc_propagate_error (mcerror, 0, "%s", "vpath object is empty");
- return NULL;
- }
- cpath = mc_config_init (NULL, FALSE);
- for (element_index = 0; element_index < vfs_path_elements_count (vpath); element_index++)
- {
- char groupname[BUF_TINY];
- const vfs_path_element_t *element;
- g_snprintf (groupname, sizeof (groupname), "path-element-%zd", element_index);
- element = vfs_path_get_by_index (vpath, element_index);
-
- mc_config_set_string_raw (cpath, groupname, "path", element->path);
- mc_config_set_string_raw (cpath, groupname, "class-name", element->class->name);
- #ifdef HAVE_CHARSET
- mc_config_set_string_raw (cpath, groupname, "encoding", element->encoding);
- #endif
- mc_config_set_string_raw (cpath, groupname, "vfs_prefix", element->vfs_prefix);
- mc_config_set_string_raw (cpath, groupname, "user", element->user);
- mc_config_set_string_raw (cpath, groupname, "password", element->password);
- mc_config_set_string_raw (cpath, groupname, "host", element->host);
- if (element->port != 0)
- mc_config_set_int (cpath, groupname, "port", element->port);
- }
- ret_value = mc_serialize_config (cpath, mcerror);
- mc_config_deinit (cpath);
- return ret_value;
- }
- vfs_path_t *
- vfs_path_deserialize (const char *data, GError **mcerror)
- {
- mc_config_t *cpath;
- size_t element_index;
- vfs_path_t *vpath;
- mc_return_val_if_error (mcerror, FALSE);
- cpath = mc_deserialize_config (data, mcerror);
- if (cpath == NULL)
- return NULL;
- vpath = vfs_path_new (FALSE);
- for (element_index = 0;; element_index++)
- {
- struct vfs_class *eclass;
- vfs_path_element_t *element;
- char *cfg_value;
- char groupname[BUF_TINY];
- g_snprintf (groupname, sizeof (groupname), "path-element-%zu", element_index);
- if (!mc_config_has_group (cpath, groupname))
- break;
- cfg_value = mc_config_get_string_raw (cpath, groupname, "class-name", NULL);
- eclass = vfs_get_class_by_name (cfg_value);
- if (eclass == NULL)
- {
- vfs_path_free (vpath, TRUE);
- g_set_error (mcerror, MC_ERROR, 0, "Unable to find VFS class by name '%s'", cfg_value);
- g_free (cfg_value);
- mc_config_deinit (cpath);
- return NULL;
- }
- g_free (cfg_value);
- element = g_new0 (vfs_path_element_t, 1);
- element->class = eclass;
- element->path = mc_config_get_string_raw (cpath, groupname, "path", NULL);
- #ifdef HAVE_CHARSET
- element->encoding = mc_config_get_string_raw (cpath, groupname, "encoding", NULL);
- element->dir.converter =
- (element->encoding != NULL) ? str_crt_conv_from (element->encoding) : INVALID_CONV;
- #endif
- element->vfs_prefix = mc_config_get_string_raw (cpath, groupname, "vfs_prefix", NULL);
- element->user = mc_config_get_string_raw (cpath, groupname, "user", NULL);
- element->password = mc_config_get_string_raw (cpath, groupname, "password", NULL);
- element->host = mc_config_get_string_raw (cpath, groupname, "host", NULL);
- element->port = mc_config_get_int (cpath, groupname, "port", 0);
- vpath->path = g_array_append_val (vpath->path, element);
- }
- mc_config_deinit (cpath);
- if (vfs_path_elements_count (vpath) == 0)
- {
- vfs_path_free (vpath, TRUE);
- g_set_error (mcerror, MC_ERROR, 0, "No any path elements found");
- return NULL;
- }
- vpath->str = vfs_path_to_str_flags (vpath, 0, VPF_NONE);
- return vpath;
- }
- vfs_path_t *
- vfs_path_build_filename (const char *first_element, ...)
- {
- va_list args;
- char *str_path;
- vfs_path_t *vpath;
- if (first_element == NULL)
- return NULL;
- va_start (args, first_element);
- str_path = mc_build_filenamev (first_element, args);
- va_end (args);
- vpath = vfs_path_from_str (str_path);
- g_free (str_path);
- return vpath;
- }
- vfs_path_t *
- vfs_path_append_new (const vfs_path_t *vpath, const char *first_element, ...)
- {
- va_list args;
- char *str_path;
- const char *result_str;
- vfs_path_t *ret_vpath;
- if (vpath == NULL || first_element == NULL)
- return NULL;
- va_start (args, first_element);
- str_path = mc_build_filenamev (first_element, args);
- va_end (args);
- result_str = vfs_path_as_str (vpath);
- ret_vpath = vfs_path_build_filename (result_str, str_path, (char *) NULL);
- g_free (str_path);
- return ret_vpath;
- }
- vfs_path_t *
- vfs_path_append_vpath_new (const vfs_path_t *first_vpath, ...)
- {
- va_list args;
- vfs_path_t *ret_vpath;
- const vfs_path_t *current_vpath = first_vpath;
- if (first_vpath == NULL)
- return NULL;
- ret_vpath = vfs_path_new (FALSE);
- va_start (args, first_vpath);
- do
- {
- int vindex;
- for (vindex = 0; vindex < vfs_path_elements_count (current_vpath); vindex++)
- {
- vfs_path_element_t *path_element;
- path_element = vfs_path_element_clone (vfs_path_get_by_index (current_vpath, vindex));
- g_array_append_val (ret_vpath->path, path_element);
- }
- current_vpath = va_arg (args, const vfs_path_t *);
- }
- while (current_vpath != NULL);
- va_end (args);
- ret_vpath->str = vfs_path_to_str_flags (ret_vpath, 0, VPF_NONE);
- return ret_vpath;
- }
- size_t
- vfs_path_tokens_count (const vfs_path_t *vpath)
- {
- size_t count_tokens = 0;
- int element_index;
- if (vpath == NULL)
- return 0;
- for (element_index = 0; element_index < vfs_path_elements_count (vpath); element_index++)
- {
- const vfs_path_element_t *element;
- const char *token, *prev_token;
- element = vfs_path_get_by_index (vpath, element_index);
- for (prev_token = element->path; (token = strchr (prev_token, PATH_SEP)) != NULL;
- prev_token = token + 1)
- {
-
- if (token != prev_token)
- count_tokens++;
- }
- if (*prev_token != '\0')
- count_tokens++;
- }
- return count_tokens;
- }
- char *
- vfs_path_tokens_get (const vfs_path_t *vpath, ssize_t start_position, ssize_t length)
- {
- GString *ret_tokens, *element_tokens;
- int element_index;
- size_t tokens_count = vfs_path_tokens_count (vpath);
- if (vpath == NULL)
- return NULL;
- if (length == 0)
- length = tokens_count;
- if (length < 0)
- length = tokens_count + length;
- if (start_position < 0)
- start_position = (ssize_t) tokens_count + start_position;
- if (start_position < 0)
- return NULL;
- if (start_position >= (ssize_t) tokens_count)
- return NULL;
- if (start_position + (ssize_t) length > (ssize_t) tokens_count)
- length = tokens_count - start_position;
- ret_tokens = g_string_sized_new (32);
- element_tokens = g_string_sized_new (32);
- for (element_index = 0; element_index < vfs_path_elements_count (vpath); element_index++)
- {
- const vfs_path_element_t *element;
- char **path_tokens, **iterator;
- g_string_assign (element_tokens, "");
- element = vfs_path_get_by_index (vpath, element_index);
- path_tokens = g_strsplit (element->path, PATH_SEP_STR, -1);
- for (iterator = path_tokens; *iterator != NULL; iterator++)
- {
- if (**iterator != '\0')
- {
- if (start_position == 0)
- {
- if (length == 0)
- {
- vfs_path_tokens_add_class_info (element, ret_tokens, element_tokens);
- g_string_free (element_tokens, TRUE);
- g_strfreev (path_tokens);
- return g_string_free (ret_tokens, FALSE);
- }
- length--;
- if (element_tokens->len != 0)
- g_string_append_c (element_tokens, PATH_SEP);
- g_string_append (element_tokens, *iterator);
- }
- else
- start_position--;
- }
- }
- g_strfreev (path_tokens);
- vfs_path_tokens_add_class_info (element, ret_tokens, element_tokens);
- }
- g_string_free (element_tokens, TRUE);
- return g_string_free (ret_tokens, !(start_position == 0 && length == 0));
- }
- vfs_path_t *
- vfs_path_vtokens_get (const vfs_path_t *vpath, ssize_t start_position, ssize_t length)
- {
- char *str_tokens;
- vfs_path_t *ret_vpath = NULL;
- str_tokens = vfs_path_tokens_get (vpath, start_position, length);
- if (str_tokens != NULL)
- {
- ret_vpath = vfs_path_from_str_flags (str_tokens, VPF_NO_CANON);
- g_free (str_tokens);
- }
- return ret_vpath;
- }
- GString *
- vfs_path_build_url_params_str (const vfs_path_element_t *element, gboolean keep_password)
- {
- GString *buffer;
- if (element == NULL)
- return NULL;
- buffer = g_string_sized_new (64);
- if (element->user != NULL)
- g_string_append (buffer, element->user);
- if (element->password != NULL && keep_password)
- {
- g_string_append_c (buffer, ':');
- g_string_append (buffer, element->password);
- }
- if (element->host != NULL)
- {
- if ((element->user != NULL) || (element->password != NULL))
- g_string_append_c (buffer, '@');
- if (element->ipv6)
- g_string_append_c (buffer, '[');
- g_string_append (buffer, element->host);
- if (element->ipv6)
- g_string_append_c (buffer, ']');
- if (element->port != 0)
- g_string_append_printf (buffer, ":%d", element->port);
- }
- if (buffer->len != 0)
- return buffer;
- g_string_free (buffer, TRUE);
- return NULL;
- }
- GString *
- vfs_path_element_build_pretty_path_str (const vfs_path_element_t *element)
- {
- GString *url_params, *pretty_path;
- pretty_path = g_string_new (element->class->prefix);
- g_string_append (pretty_path, VFS_PATH_URL_DELIMITER);
- url_params = vfs_path_build_url_params_str (element, FALSE);
- if (url_params != NULL)
- {
- g_string_append_len (pretty_path, url_params->str, url_params->len);
- g_string_free (url_params, TRUE);
- }
- if (!IS_PATH_SEP (*element->path))
- g_string_append_c (pretty_path, PATH_SEP);
- return g_string_append (pretty_path, element->path);
- }
- gboolean
- vfs_path_equal (const vfs_path_t *vpath1, const vfs_path_t *vpath2)
- {
- const char *path1, *path2;
- gboolean ret_val;
- if (vpath1 == NULL || vpath2 == NULL)
- return FALSE;
- path1 = vfs_path_as_str (vpath1);
- path2 = vfs_path_as_str (vpath2);
- ret_val = strcmp (path1, path2) == 0;
- return ret_val;
- }
- gboolean
- vfs_path_equal_len (const vfs_path_t *vpath1, const vfs_path_t *vpath2, size_t len)
- {
- const char *path1, *path2;
- gboolean ret_val;
- if (vpath1 == NULL || vpath2 == NULL)
- return FALSE;
- path1 = vfs_path_as_str (vpath1);
- path2 = vfs_path_as_str (vpath2);
- ret_val = strncmp (path1, path2, len) == 0;
- return ret_val;
- }
- size_t
- vfs_path_len (const vfs_path_t *vpath)
- {
- if (vpath == NULL)
- return 0;
- return strlen (vpath->str);
- }
- vfs_path_t *
- vfs_path_to_absolute (const vfs_path_t *vpath)
- {
- vfs_path_t *absolute_vpath;
- const char *path_str;
- if (!vpath->relative)
- return vfs_path_clone (vpath);
- path_str = vfs_path_as_str (vpath);
- absolute_vpath = vfs_path_from_str (path_str);
- return absolute_vpath;
- }
|