namesys/path: Renamed the function names to match the rest of the project.
This commit is contained in:
parent
bf9ddfd6f6
commit
496ae3ec6c
11 changed files with 147 additions and 147 deletions
52
path/path.c
52
path/path.c
|
@ -5,7 +5,7 @@
|
|||
#include <ipfs/path/path.h>
|
||||
|
||||
// FromCid safely converts a cid.Cid type to a Path type
|
||||
char* PathFromCid (struct Cid *c)
|
||||
char* ipfs_path_from_cid (struct Cid *c)
|
||||
{
|
||||
const char prefix[] = "/ipfs/";
|
||||
char *rpath, *cidstr = CidString(c);
|
||||
|
@ -17,7 +17,7 @@ char* PathFromCid (struct Cid *c)
|
|||
return rpath;
|
||||
}
|
||||
|
||||
char** SplitN (char *p, char *delim, int n)
|
||||
char** ipfs_path_split_n (char *p, char *delim, int n)
|
||||
{
|
||||
char *c, **r, *rbuf;
|
||||
int i, dlen = strlen(delim);
|
||||
|
@ -62,15 +62,15 @@ char** SplitN (char *p, char *delim, int n)
|
|||
return r;
|
||||
}
|
||||
|
||||
char** Segments (char *p)
|
||||
char** ipfs_path_split_segments (char *p)
|
||||
{
|
||||
if (*p == '/') p++; // Ignore leading slash
|
||||
|
||||
return SplitN (p, "/", -1);
|
||||
return ipfs_path_split_n (p, "/", -1);
|
||||
}
|
||||
|
||||
// Count Segments
|
||||
int SegmentsLength (char **s)
|
||||
// Count segments
|
||||
int ipfs_path_segments_length (char **s)
|
||||
{
|
||||
int r = 0;
|
||||
|
||||
|
@ -81,8 +81,8 @@ int SegmentsLength (char **s)
|
|||
return r;
|
||||
}
|
||||
|
||||
// free memory allocated by Segments
|
||||
void FreeSegments (char ***s)
|
||||
// free memory allocated by ipfs_path_split_segments
|
||||
void ipfs_path_free_segments (char ***s)
|
||||
{
|
||||
if (*s && **s) {
|
||||
free(**s); // free string buffer
|
||||
|
@ -91,25 +91,25 @@ void FreeSegments (char ***s)
|
|||
}
|
||||
}
|
||||
|
||||
// IsJustAKey returns true if the path is of the form <key> or /ipfs/<key>.
|
||||
int IsJustAKey (char *p)
|
||||
// ipfs_path_is_just_a_key returns true if the path is of the form <key> or /ipfs/<key>.
|
||||
int ipfs_path_is_just_a_key (char *p)
|
||||
{
|
||||
char **parts;
|
||||
int ret = 0;
|
||||
parts = Segments (p);
|
||||
parts = ipfs_path_split_segments (p);
|
||||
if (parts) {
|
||||
if (SegmentsLength (parts) == 2 && strcmp (parts[0], "ipfs") == 0) ret++;
|
||||
FreeSegments(&parts);
|
||||
if (ipfs_path_segments_length (parts) == 2 && strcmp (parts[0], "ipfs") == 0) ret++;
|
||||
ipfs_path_free_segments(&parts);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
// PopLastSegment returns a new Path without its final segment, and the final
|
||||
// ipfs_path_pop_last_segment returns a new Path without its final segment, and the final
|
||||
// segment, separately. If there is no more to pop (the path is just a key),
|
||||
// the original path is returned.
|
||||
int PopLastSegment (char **str, char *p)
|
||||
int ipfs_path_pop_last_segment (char **str, char *p)
|
||||
{
|
||||
if (IsJustAKey(p)) return 0;
|
||||
if (ipfs_path_is_just_a_key(p)) return 0;
|
||||
*str = strrchr(p, '/');
|
||||
if (!*str) return ErrBadPath; // error
|
||||
**str = '\0';
|
||||
|
@ -117,7 +117,7 @@ int PopLastSegment (char **str, char *p)
|
|||
return 0;
|
||||
}
|
||||
|
||||
char *PathFromSegments(char *prefix, char **seg)
|
||||
char *ipfs_path_from_segments(char *prefix, char **seg)
|
||||
{
|
||||
int retlen, i;
|
||||
char *ret;
|
||||
|
@ -140,7 +140,7 @@ char *PathFromSegments(char *prefix, char **seg)
|
|||
return ret;
|
||||
}
|
||||
|
||||
int ParseCidToPath (char *dst, char *txt)
|
||||
int ipfs_path_parse_from_cid (char *dst, char *txt)
|
||||
{
|
||||
struct Cid *c;
|
||||
char *r;
|
||||
|
@ -153,7 +153,7 @@ int ParseCidToPath (char *dst, char *txt)
|
|||
return ErrCidDecode;
|
||||
}
|
||||
|
||||
r = PathFromCid(c);
|
||||
r = ipfs_path_from_cid(c);
|
||||
|
||||
if (!r) {
|
||||
return ErrCidDecode;
|
||||
|
@ -163,7 +163,7 @@ int ParseCidToPath (char *dst, char *txt)
|
|||
return 0;
|
||||
}
|
||||
|
||||
int ParsePath (char *dst, char *txt)
|
||||
int ipfs_path_parse (char *dst, char *txt)
|
||||
{
|
||||
int err, i;
|
||||
char *c;
|
||||
|
@ -176,10 +176,10 @@ int ParsePath (char *dst, char *txt)
|
|||
if (*txt == '/') {
|
||||
txt++;
|
||||
}
|
||||
err = ParseCidToPath (dst+plen, txt);
|
||||
if (err == 0) { // only change dst if ParseCidToPath returned success.
|
||||
err = ipfs_path_parse_from_cid (dst+plen, txt);
|
||||
if (err == 0) { // only change dst if ipfs_path_parse_from_cid returned success.
|
||||
// Use memcpy instead of strcpy to avoid overwriting
|
||||
// result of ParseCidToPath with a null terminator.
|
||||
// result of ipfs_path_parse_from_cid with a null terminator.
|
||||
memcpy (dst, prefix, plen);
|
||||
}
|
||||
return err;
|
||||
|
@ -194,15 +194,15 @@ int ParsePath (char *dst, char *txt)
|
|||
strcpy (buf, txt+6); // copy to temp buffer.
|
||||
c = strchr(buf, '/');
|
||||
if (c) *c = '\0';
|
||||
return ParseCidToPath(dst, buf);
|
||||
return ipfs_path_parse_from_cid(dst, buf);
|
||||
} else if (strcmp (txt, "/ipns/") != 0) {
|
||||
return ErrBadPath;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int PathIsValid (char *p)
|
||||
int ipfs_path_is_valid (char *p)
|
||||
{
|
||||
char buf[4096];
|
||||
return ParsePath(buf, p);
|
||||
return ipfs_path_parse(buf, p);
|
||||
}
|
||||
|
|
|
@ -1,20 +1,20 @@
|
|||
#include "ipfs/cid/cid.h"
|
||||
#include "ipfs/path/path.h"
|
||||
|
||||
Resolver* NewBasicResolver (DAGService *ds)
|
||||
Resolver* ipfs_path_new_basic_resolver (DAGService *ds)
|
||||
{
|
||||
Resolver *ret = malloc(sizeof(Resolver));
|
||||
if (!ret) return NULL;
|
||||
ret->DAG = ds;
|
||||
ret->ResolveOnce = ResolveSingle;
|
||||
ret->ResolveOnce = ipfs_path_resolve_single;
|
||||
return ret;
|
||||
}
|
||||
|
||||
// SplitAbsPath clean up and split fpath. It extracts the first component (which
|
||||
// ipfs_path_split_abs_path clean up and split fpath. It extracts the first component (which
|
||||
// must be a Multihash) and return it separately.
|
||||
int SplitAbsPath (struct Cid* cid, char ***parts, char *fpath)
|
||||
int ipfs_path_split_abs_path (struct Cid* cid, char ***parts, char *fpath)
|
||||
{
|
||||
*parts = Segments(fpath);
|
||||
*parts = ipfs_path_split_segments(fpath);
|
||||
|
||||
if (strcmp (**parts, "ipfs") == 0) *parts++;
|
||||
|
||||
|
@ -26,9 +26,9 @@ int SplitAbsPath (struct Cid* cid, char ***parts, char *fpath)
|
|||
return 0;
|
||||
}
|
||||
|
||||
// ResolvePath fetches the node for given path. It returns the last item
|
||||
// returned by ResolvePathComponents.
|
||||
int ResolvePath(Node **nd, Context ctx, Resolver *s, char *fpath)
|
||||
// ipfs_path_resolve_path fetches the node for given path. It returns the last item
|
||||
// returned by ipfs_path_resolve_path_components.
|
||||
int ipfs_path_resolve_path(Node **nd, Context ctx, Resolver *s, char *fpath)
|
||||
{
|
||||
int err = IsValid(fpath);
|
||||
Node **ndd;
|
||||
|
@ -36,7 +36,7 @@ int ResolvePath(Node **nd, Context ctx, Resolver *s, char *fpath)
|
|||
if (err) {
|
||||
return err;
|
||||
}
|
||||
err = ResolvePathComponents(&ndd, ctx, s, fpath);
|
||||
err = ipfs_path_resolve_path_components(&ndd, ctx, s, fpath);
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
|
@ -50,21 +50,21 @@ int ResolvePath(Node **nd, Context ctx, Resolver *s, char *fpath)
|
|||
return 0;
|
||||
}
|
||||
|
||||
int ResolveSingle(NodeLink **lnk, Context ctx, DAGService *ds, Node **nd, char *name)
|
||||
int ipfs_path_resolve_single(NodeLink **lnk, Context ctx, DAGService *ds, Node **nd, char *name)
|
||||
{
|
||||
return ResolveLink(lnk, name);
|
||||
return ipfs_path_resolve_link(lnk, name);
|
||||
}
|
||||
|
||||
// ResolvePathComponents fetches the nodes for each segment of the given path.
|
||||
// ipfs_path_resolve_path_components fetches the nodes for each segment of the given path.
|
||||
// It uses the first path component as a hash (key) of the first node, then
|
||||
// resolves all other components walking the links, with ResolveLinks.
|
||||
int ResolvePathComponents(Node ***nd, Context ctx, Resolver *s, char *fpath)
|
||||
// resolves all other components walking the links, with ipfs_path_resolve_links.
|
||||
int ipfs_path_resolve_path_components(Node ***nd, Context ctx, Resolver *s, char *fpath)
|
||||
{
|
||||
int err;
|
||||
struct Cid h;
|
||||
char **parts;
|
||||
|
||||
err = SplitAbsPath(&h, &parts, fpath);
|
||||
err = ipfs_path_split_abs_path(&h, &parts, fpath);
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
|
@ -75,27 +75,27 @@ int ResolvePathComponents(Node ***nd, Context ctx, Resolver *s, char *fpath)
|
|||
// return DAG_ERR_VAL;
|
||||
//}
|
||||
|
||||
return ResolveLinks(ctx, *nd, parts);
|
||||
return ipfs_path_resolve_links(ctx, *nd, parts);
|
||||
}
|
||||
|
||||
// ResolveLinks iteratively resolves names by walking the link hierarchy.
|
||||
// ipfs_path_resolve_links iteratively resolves names by walking the link hierarchy.
|
||||
// Every node is fetched from the DAGService, resolving the next name.
|
||||
// Returns the list of nodes forming the path, starting with ndd. This list is
|
||||
// guaranteed never to be empty.
|
||||
//
|
||||
// ResolveLinks(nd, []string{"foo", "bar", "baz"})
|
||||
// ipfs_path_resolve_links(nd, []string{"foo", "bar", "baz"})
|
||||
// would retrieve "baz" in ("bar" in ("foo" in nd.Links).Links).Links
|
||||
int ResolveLinks(Node ***result, Context ctx, Node *ndd, char **names)
|
||||
int ipfs_path_resolve_links(Node ***result, Context ctx, Node *ndd, char **names)
|
||||
{
|
||||
int err, idx = 0;
|
||||
NodeLink *lnk;
|
||||
Node *nd;
|
||||
|
||||
*result = calloc (sizeof(Node*), SegmentsLength(names) + 1);
|
||||
*result = calloc (sizeof(Node*), ipfs_path_segments_length(names) + 1);
|
||||
if (!*result) {
|
||||
return -1;
|
||||
}
|
||||
memset (*result, NULL, sizeof(Node*) * (SegmentsLength(names)+1));
|
||||
memset (*result, NULL, sizeof(Node*) * (ipfs_path_segments_length(names)+1));
|
||||
|
||||
*result[idx++] = ndd;
|
||||
nd = ndd; // dup arg workaround
|
||||
|
@ -107,7 +107,7 @@ int ResolveLinks(Node ***result, Context ctx, Node *ndd, char **names)
|
|||
//defer cancel()
|
||||
|
||||
// for each of the path components
|
||||
err = ResolveLink(&lnk, *names);
|
||||
err = ipfs_path_resolve_link(&lnk, *names);
|
||||
if (err) {
|
||||
char msg[51];
|
||||
*result[idx] = NULL;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue