namesys/path: Renamed the function names to match the rest of the project.

yamux
Jose Marcial Vieira Bisneto 2016-12-05 08:55:17 -03:00
parent bf9ddfd6f6
commit 496ae3ec6c
11 changed files with 147 additions and 147 deletions

View File

@ -65,7 +65,7 @@
typedef struct s_mpns { typedef struct s_mpns {
resolvers *resolver; resolvers *resolver;
publishers *Publisher; publishers *publisher;
} mpns; } mpns;
typedef struct s_tlds { typedef struct s_tlds {
@ -73,30 +73,30 @@
int condition; int condition;
} tlds; } tlds;
int resolve (resolver *r, char **p, char *str, int depth, char **prefixes); int ipfs_namesys_resolve (resolver *r, char **p, char *str, int depth, char **prefixes);
int Resolve(char **path, char *name); int ipfs_namesys_resolve(char **path, char *name);
int ResolveN(char **path, char *name, int depth); int ipfs_namesys_resolve_n(char **path, char *name, int depth);
int resolveOnce (char **path, char *name); int ipfs_namesys_resolve_once (char **path, char *name);
int Publish (char *proto, ciPrivKey name, char *value); int ipfs_namesys_publish (char *proto, ciPrivKey name, char *value);
int PublishWithEOL (char *proto, ciPrivKey name, char *value, time_t eol); int ipfs_namesys_publish_with_eol (char *proto, ciPrivKey name, char *value, time_t eol);
int ProquintIsProquint(char *str); int ipfs_proquint_is_proquint(char *str);
char *ProquintEncode(char *buf, int size); char *ipfs_proquint_encode(char *buf, int size);
char *ProquintDecode(char *str); char *ipfs_proquint_decode(char *str);
int ProquintResolveOnce (char **p, char *name); int ipfs_proquint_resolve_once (char **p, char *name);
int domainMatchString (char *d); int ipfs_isdomain_match_string (char *d);
int IsICANNTLD(char *s); int ipfs_isdomain_is_icann_tld(char *s);
int IsExtendedTLD (char *s); int ipfs_isdomain_is_extended_tld (char *s);
int IsTLD (char *s); int ipfs_isdomain_is_tld (char *s);
int IsDomain (char *s); int ipfs_isdomain_is_domain (char *s);
typedef struct s_DNSResolver { typedef struct s_DNSResolver {
// TODO // TODO
} DNSResolver; } DNSResolver;
int DNSResolverResolveOnce (DNSResolver *r, char **path, char *name); int ipfs_dns_resolver_resolve_once (DNSResolver *r, char **path, char *name);
int workDomain (int output, DNSResolver *r, char *name); int ipfs_dns_work_domain (int output, DNSResolver *r, char *name);
int parseEntry (char **Path, char *txt); int ipfs_dns_parse_entry (char **Path, char *txt);
int tryParseDnsLink (char **Path, char *txt); int ipfs_dns_try_parse_dns_link (char **Path, char *txt);
#endif //NAMESYS_H #endif //NAMESYS_H

View File

@ -24,15 +24,15 @@
ErrNoLinkFmt ErrNoLinkFmt
} PathErrs; } PathErrs;
char* PathFromCid (struct Cid *c); char* ipfs_path_from_cid (struct Cid *c);
char** SplitN (char *p, char *delim, int n); char** ipfs_path_split_n (char *p, char *delim, int n);
char** Segments (char *p); char** ipfs_path_split_segments (char *p);
int SegmentsLength (char **s); int ipfs_path_segments_length (char **s);
void FreeSegments (char ***s); void ipfs_path_free_segments (char ***s);
int IsJustAKey (char *p); int ipfs_path_is_just_a_key (char *p);
int PopLastSegment (char **str, char *p); int ipfs_path_pop_last_segment (char **str, char *p);
char *PathFromSegments(char *prefix, char **seg); char *ipfs_path_from_segments(char *prefix, char **seg);
int ParseCidToPath (char *dst, char *txt); int ipfs_path_parse_from_cid (char *dst, char *txt);
int ParsePath (char *dst, char *txt); int ipfs_path_parse (char *dst, char *txt);
int PathIsValid (char *p); int ipfs_path_is_valid (char *p);
#endif // IPFS_PATH_H #endif // IPFS_PATH_H

View File

@ -4,7 +4,7 @@
#include "ipfs/path/path.h" #include "ipfs/path/path.h"
#include "ipfs/namesys/namesys.h" #include "ipfs/namesys/namesys.h"
int resolve (resolver *r, char **p, char *str, int depth, char **prefixes) int ipfs_namesys_base_resolve (resolver *r, char **p, char *str, int depth, char **prefixes)
{ {
int err, i; int err, i;
char ipfs_prefix[] = "/ipfs/"; char ipfs_prefix[] = "/ipfs/";
@ -26,7 +26,7 @@ int resolve (resolver *r, char **p, char *str, int depth, char **prefixes)
} }
for (i = 0 ; prefixes[i] ; i++) { for (i = 0 ; prefixes[i] ; i++) {
if (memcmp(*p, prefixes[i], strlen(prefixes[i])) == 0) { if (memcmp(*p, prefixes[i], strlen(prefixes[i])) == 0) {
if (SegmentsLength(prefixes) == 1) { if (ipfs_path_segments_length(prefixes) == 1) {
str += strlen(prefixes[i]); str += strlen(prefixes[i]);
} }
break; break;

View File

@ -48,18 +48,18 @@ type lookupRes struct {
// resolveOnce implements resolver. // resolveOnce implements resolver.
// TXT records for a given domain name should contain a b58 // TXT records for a given domain name should contain a b58
// encoded multihash. // encoded multihash.
int DNSResolverResolveOnce (char **path, char *name) int ipfs_dns_resolver_resolve_once (char **path, char *name)
{ {
char **segments, *domain, *dnslink, buf[500], dlprefix[] = "_dnslink."; char **segments, *domain, *dnslink, buf[500], dlprefix[] = "_dnslink.";
int p1[2], p2[2], r, c=2; int p1[2], p2[2], r, c=2;
struct pollfd event[2], *e; struct pollfd event[2], *e;
segments = SplitN(name, "/", 2); segments = ipfs_path_split_n(name, "/", 2);
domain = segments[0]; domain = segments[0];
*path = NULL; *path = NULL;
if (!IsDomain(domain)) { if (!ipfs_isdomain_is_domain(domain)) {
return ErrInvalidDomain; return ErrInvalidDomain;
} }
//log.Infof("DNSResolver resolving %s", domain); //log.Infof("DNSResolver resolving %s", domain);
@ -74,7 +74,7 @@ int DNSResolverResolveOnce (char **path, char *name)
return ErrPipe; return ErrPipe;
case 0: // child case 0: // child
close(p1[STDIN_FILENO]); // we don't need to read at child process. close(p1[STDIN_FILENO]); // we don't need to read at child process.
return workDomain (p1[STDOUT_FILENO], r, domain); return ipfs_dns_work_domain (p1[STDOUT_FILENO], r, domain);
} }
close(p1[STDOUT_FILENO]); // we don't need to write at main process. close(p1[STDOUT_FILENO]); // we don't need to write at main process.
r = fork(); r = fork();
@ -91,7 +91,7 @@ int DNSResolverResolveOnce (char **path, char *name)
strcpy (dnslink, dlprefix); strcpy (dnslink, dlprefix);
strcat (dnslink, domain); strcat (dnslink, domain);
return workDomain (p2[STDOUT_FILENO], r, dnslink); return ipfs_dns_work_domain (p2[STDOUT_FILENO], r, dnslink);
} }
close(p2[STDOUT_FILENO]); // we don't need to write at main process. close(p2[STDOUT_FILENO]); // we don't need to write at main process.
@ -137,23 +137,23 @@ int DNSResolverResolveOnce (char **path, char *name)
return ErrResolveFailed; return ErrResolveFailed;
} }
if (SegmentsLength (segments) > 1) { if (ipfs_path_segments_length (segments) > 1) {
name = *path + strlen(*path) - 1; name = *path + strlen(*path) - 1;
while (*name == '/') { while (*name == '/') {
*name-- = '\0'; *name-- = '\0';
} }
name = *path; name = *path;
*path = PathFromSegments (name, segments+1); *path = ipfs_path_from_segments (name, segments+1);
free (name); free (name);
if (!*path) { if (!*path) {
return ErrResolveFailed; return ErrResolveFailed;
} }
} }
FreeSegments (&segments); ipfs_path_free_segments (&segments);
return 0; return 0;
} }
int workDomain (int output, DNSResolver *r, char *name) int ipfs_dns_work_domain (int output, DNSResolver *r, char *name)
{ {
char **txt, *path; char **txt, *path;
int i, err = r->lookupTXT(&txt, name); int i, err = r->lookupTXT(&txt, name);
@ -163,7 +163,7 @@ int workDomain (int output, DNSResolver *r, char *name)
} }
for (i = 0 ; txt[i] ; i++) { for (i = 0 ; txt[i] ; i++) {
err = parseEntry (&path, txt[i]); err = ipfs_dns_parse_entry (&path, txt[i]);
if (!err) { if (!err) {
err = (write (output, path, strlen(path)) != strlen(path)); err = (write (output, path, strlen(path)) != strlen(path));
free (path); free (path);
@ -176,12 +176,12 @@ int workDomain (int output, DNSResolver *r, char *name)
return ErrResolveFailed; return ErrResolveFailed;
} }
int parseEntry (char **path, char *txt) int ipfs_dns_parse_entry (char **path, char *txt)
{ {
char buf[500]; char buf[500];
int err; int err;
err = ParseCidToPath(buf, txt); // bare IPFS multihashes err = ipfs_path_parse_from_cid(buf, txt); // bare IPFS multihashes
if (! err) { if (! err) {
*path = malloc(strlen(buf) + 1); *path = malloc(strlen(buf) + 1);
if (!*path) { if (!*path) {
@ -190,16 +190,16 @@ int parseEntry (char **path, char *txt)
strcpy(*path, buf); strcpy(*path, buf);
return 0; return 0;
} }
return tryParseDnsLink(path, txt); return ipfs_dns_try_parse_dns_link(path, txt);
} }
int tryParseDnsLink(char **path, char *txt) int ipfs_dns_try_parse_dns_link(char **path, char *txt)
{ {
char **parts = SplitN(txt, "=", 2), buf[500]; char **parts = ipfs_path_split_n(txt, "=", 2), buf[500];
int err; int err;
if (SegmentsLength(parts) == 2 && strcmp(parts[0], "dnslink")==0) { if (ipfs_path_segments_length(parts) == 2 && strcmp(parts[0], "dnslink")==0) {
err = ParsePath(buf, parts[1]); err = ipfs_path_parse(buf, parts[1]);
if (err == 0) { if (err == 0) {
*parts = malloc(strlen(buf) + 1); *parts = malloc(strlen(buf) + 1);
if (! *parts) { if (! *parts) {

View File

@ -25,7 +25,7 @@ int main(int argc, char **argv)
}; };
for (i = 0 ; chk[i].str ; i++) { for (i = 0 ; chk[i].str ; i++) {
if (IsDomain (chk[i].str) != chk[i].condition) { if (ipfs_isdomain_is_domain (chk[i].str) != chk[i].condition) {
printf ("Misclassification: %s should be %d\n", chk[i].str, chk[i].condition); printf ("Misclassification: %s should be %d\n", chk[i].str, chk[i].condition);
} }
} }

View File

@ -4,7 +4,7 @@
#include "ipfs/namesys/namesys.h" #include "ipfs/namesys/namesys.h"
#include "ipfs/namesys/isdomain.h" #include "ipfs/namesys/isdomain.h"
void ToUpper(char *dst, char *src) void ipfs_isdomain_to_upper(char *dst, char *src)
{ {
while(*src) { while(*src) {
*dst++ = toupper(*src++); *dst++ = toupper(*src++);
@ -12,7 +12,7 @@ void ToUpper(char *dst, char *src)
*dst = '\0'; *dst = '\0';
} }
int HasSuffix (char *s, char *suf) int ipfs_isdomain_has_suffix (char *s, char *suf)
{ {
char *p; char *p;
@ -20,11 +20,11 @@ int HasSuffix (char *s, char *suf)
return strcmp(p, suf) == 0; return strcmp(p, suf) == 0;
} }
int IsAtArray(tlds *a, char *s) int ipfs_isdomain_is_at_array(tlds *a, char *s)
{ {
char str[strlen(s)+1]; char str[strlen(s)+1];
ToUpper(str, s); ipfs_isdomain_to_upper(str, s);
while(a->str) { while(a->str) {
if (strcmp(a->str, str) == 0) { if (strcmp(a->str, str) == 0) {
return a->condition; return a->condition;
@ -34,11 +34,11 @@ int IsAtArray(tlds *a, char *s)
return 0; return 0;
} }
int domainMatchString (char *d) int ipfs_isdomain_match_string (char *d)
{ {
char str[strlen(d)+1], *p = str, *l; char str[strlen(d)+1], *p = str, *l;
ToUpper(str, d); ipfs_isdomain_to_upper(str, d);
// l point to last two chars. // l point to last two chars.
l = p + strlen(p) - 2; l = p + strlen(p) - 2;
@ -64,31 +64,31 @@ int domainMatchString (char *d)
return 1; // valid return 1; // valid
} }
// IsICANNTLD returns whether the given string is a TLD (Top Level Domain), // ipfs_isdomain_is_icann_tld returns whether the given string is a TLD (Top Level Domain),
// according to ICANN. Well, really according to the TLDs listed in this // according to ICANN. Well, really according to the TLDs listed in this
// package. // package.
int IsICANNTLD(char *s) int ipfs_isdomain_is_icann_tld(char *s)
{ {
return IsAtArray (TLDs, s); return ipfs_isdomain_is_at_array (TLDs, s);
} }
// IsExtendedTLD returns whether the given string is a TLD (Top Level Domain), // ipfs_isdomain_is_extended_tld returns whether the given string is a TLD (Top Level Domain),
// extended with a few other "TLDs", .bit, .onion // extended with a few other "TLDs", .bit, .onion
int IsExtendedTLD (char *s) int ipfs_isdomain_is_extended_tld (char *s)
{ {
return IsAtArray (ExtendedTLDs, s); return ipfs_isdomain_is_at_array (ExtendedTLDs, s);
} }
// IsTLD returns whether the given string is a TLD (according to ICANN, or // ipfs_isdomain_is_tld returns whether the given string is a TLD (according to ICANN, or
// in the set of ExtendedTLDs listed in this package. // in the set of ExtendedTLDs listed in this package.
int IsTLD (char *s) int ipfs_isdomain_is_tld (char *s)
{ {
return IsICANNTLD (s) || IsExtendedTLD(s); return ipfs_isdomain_is_icann_tld (s) || ipfs_isdomain_is_extended_tld(s);
} }
// IsDomain returns whether given string is a domain. // ipfs_isdomain_is_domain returns whether given string is a domain.
// It first checks the TLD, and then uses a regular expression. // It first checks the TLD, and then uses a regular expression.
int IsDomain (char *s) int ipfs_isdomain_is_domain (char *s)
{ {
char str[strlen(s)]; char str[strlen(s)];
char *tld; char *tld;
@ -96,7 +96,7 @@ int IsDomain (char *s)
strcpy(str, s); strcpy(str, s);
s = str; // work with local copy. s = str; // work with local copy.
if (HasSuffix (s, ".")) { if (ipfs_isdomain_has_suffix (s, ".")) {
s[strlen(s) - 1] = '\0'; s[strlen(s) - 1] = '\0';
} }
@ -108,9 +108,9 @@ int IsDomain (char *s)
tld++; // ignore last dot tld++; // ignore last dot
if (!IsTLD (tld)) { if (!ipfs_isdomain_is_tld (tld)) {
return 0; return 0;
} }
return domainMatchString(s); return ipfs_isdomain_match_string(s);
} }

View File

@ -34,14 +34,14 @@ func NewNameSystem(r routing.ValueStore, ds ds.Datastore, cachesize int) NameSys
const DefaultResolverCacheTTL = time.Minute; const DefaultResolverCacheTTL = time.Minute;
// Resolve implements Resolver. // ipfs_namesys_resolve implements Resolver.
int Resolve(char **path, char *name) int ipfs_namesys_resolve(char **path, char *name)
{ {
return ResolveN(path, name, DefaultDepthLimit); return ipfs_namesys_resolve_n(path, name, DefaultDepthLimit);
} }
// ResolveN implements Resolver. // ipfs_namesys_resolve_n implements Resolver.
int ResolveN(char **path, char *name, int depth) int ipfs_namesys_resolve_n(char **path, char *name, int depth)
{ {
char ipfs_prefix[] = "/ipfs/"; char ipfs_prefix[] = "/ipfs/";
char p[500]; char p[500];
@ -49,10 +49,10 @@ int ResolveN(char **path, char *name, int depth)
int err; int err;
resolver r; resolver r;
r.resolveOnce = resolveOnce; r.resolveOnce = ipfs_namesys_resolve_once;
if (memcmp(name, ipfs_prefix, strlen(ipfs_prefix)) == 0) { if (memcmp(name, ipfs_prefix, strlen(ipfs_prefix)) == 0) {
ParsePath(p, name); ipfs_path_parse(p, name);
*path = malloc(strlen(p) + 1); *path = malloc(strlen(p) + 1);
if (*p) { if (*p) {
strcpy(*path, p); strcpy(*path, p);
@ -70,7 +70,7 @@ int ResolveN(char **path, char *name, int depth)
} }
strcpy(str, ipfs_prefix); strcpy(str, ipfs_prefix);
strcat(str, name+1); // ignore inital / from name, because ipfs_prefix already has it. strcat(str, name+1); // ignore inital / from name, because ipfs_prefix already has it.
err = ParsePath(p, str); // save return value. err = ipfs_path_parse(p, str); // save return value.
free (str); // so we can free allocated memory before return. free (str); // so we can free allocated memory before return.
*path = malloc(strlen(p) + 1); *path = malloc(strlen(p) + 1);
if (*p) { if (*p) {
@ -81,11 +81,11 @@ int ResolveN(char **path, char *name, int depth)
return err; return err;
} }
return resolve(&r, path, name, depth, ps); return ipfs_namesys_resolve(&r, path, name, depth, ps);
} }
// resolveOnce implements resolver. // ipfs_namesys_resolve_once implements resolver.
int resolveOnce (char **path, char *name) int ipfs_namesys_resolve_once (char **path, char *name)
{ {
char ipns_prefix[] = "/ipns/"; char ipns_prefix[] = "/ipns/";
char *ptr = NULL; char *ptr = NULL;
@ -103,13 +103,13 @@ int resolveOnce (char **path, char *name)
} }
strcpy(ptr, ipns_prefix); strcpy(ptr, ipns_prefix);
strcat(ptr, name); strcat(ptr, name);
segs = Segments(ptr); segs = ipfs_path_split_segments(ptr);
free (ptr); free (ptr);
} else { } else {
segs = Segments(name); segs = ipfs_path_split_segments(name);
} }
if (!segs || SegmentsLength(segs) < 2) { if (!segs || ipfs_path_segments_length(segs) < 2) {
//log.Warningf("Invalid name syntax for %s", name); //log.Warningf("Invalid name syntax for %s", name);
return ErrResolveFailed; return ErrResolveFailed;
} }
@ -119,8 +119,8 @@ int resolveOnce (char **path, char *name)
//log.Debugf("Attempting to resolve %s with %s", segments[1], ns[i]->resolver->protocol); //log.Debugf("Attempting to resolve %s with %s", segments[1], ns[i]->resolver->protocol);
err = ns[i]->resolver->func(&p, segs[1]); err = ns[i]->resolver->func(&p, segs[1]);
if (!err) { if (!err) {
if (SegmentsLength(segs) > 2) { if (ipfs_path_segments_length(segs) > 2) {
*path = PathFromSegments(p, segs+2); *path = ipfs_path_from_segments(p, segs+2);
} else { } else {
*path = p; *path = p;
} }
@ -131,8 +131,8 @@ int resolveOnce (char **path, char *name)
return ErrResolveFailed; return ErrResolveFailed;
} }
// Publish implements Publisher // ipfs_namesys_publish implements Publisher
int Publish (char *proto, ciPrivKey name, char *value) int ipfs_namesys_publish (char *proto, ciPrivKey name, char *value)
{ {
int i; int i;
@ -144,7 +144,7 @@ int Publish (char *proto, ciPrivKey name, char *value)
return ErrPublishFailed; return ErrPublishFailed;
} }
int PublishWithEOL (char *proto, ciPrivKey name, char *value, time_t eol) int ipfs_namesys_publish_with_eol (char *proto, ciPrivKey name, char *value, time_t eol)
{ {
int i; int i;

View File

@ -44,7 +44,7 @@ static inline int vowsd(char c)
* @return {bool} Whether or not it qualifies. * @return {bool} Whether or not it qualifies.
* @return {error} Error * @return {error} Error
*/ */
int ProquintIsProquint(char *str) int ipfs_proquint_is_proquint(char *str)
{ {
int i, c, l = strlen(str); int i, c, l = strlen(str);
@ -89,7 +89,7 @@ int ProquintIsProquint(char *str)
* *
* @return {string} The given byte slice as an identifier. * @return {string} The given byte slice as an identifier.
*/ */
char *ProquintEncode(char *buf, int size) char *ipfs_proquint_encode(char *buf, int size)
{ {
char *ret; char *ret;
int i, c; int i, c;
@ -132,14 +132,14 @@ char *ProquintEncode(char *buf, int size)
* *
* @return {[]byte} The identifier as a byte slice. * @return {[]byte} The identifier as a byte slice.
*/ */
char *ProquintDecode(char *str) char *ipfs_proquint_decode(char *str)
{ {
char *ret; char *ret;
int i, c, l = strlen(str); int i, c, l = strlen(str);
uint16_t x; uint16_t x;
// make sure its a valid Proquint string. // make sure its a valid Proquint string.
if (!ProquintIsProquint(str) && ((l+1) % 3)==0) { if (!ipfs_proquint_is_proquint(str) && ((l+1) % 3)==0) {
return NULL; return NULL;
} }
@ -163,16 +163,16 @@ char *ProquintDecode(char *str)
} }
// resolveOnce implements resolver. Decodes the proquint string. // resolveOnce implements resolver. Decodes the proquint string.
int ProquintResolveOnce (char **p, char *name) int ipfs_proquint_resolve_once (char **p, char *name)
{ {
int err = ProquintIsProquint(name); int err = ipfs_proquint_is_proquint(name);
char buf[500]; char buf[500];
if (err) { if (err) {
*p = NULL; *p = NULL;
err = ErrInvalidProquint; err = ErrInvalidProquint;
} else { } else {
err = ParsePath(buf, ProquintDecode(name)); err = ipfs_path_parse(buf, ipfs_proquint_decode(name));
if (!err) { if (!err) {
*p = malloc (strlen(buf) + 1); *p = malloc (strlen(buf) + 1);
if (p) { if (p) {

View File

@ -16,12 +16,12 @@ int main(void) {
NULL}; NULL};
for (i = 0 ; p[i] ; i++) { for (i = 0 ; p[i] ; i++) {
r = ProquintDecode (p[i]); r = ipfs_proquint_decode (p[i]);
if (r) { if (r) {
struct in_addr ip_addr; struct in_addr ip_addr;
memcpy (&(ip_addr.s_addr), r, sizeof(ip_addr.s_addr)); memcpy (&(ip_addr.s_addr), r, sizeof(ip_addr.s_addr));
printf ("%s\t%s", p[i], inet_ntoa(ip_addr)); printf ("%s\t%s", p[i], inet_ntoa(ip_addr));
s = ProquintEncode(r, sizeof(ip_addr.s_addr)); s = ipfs_proquint_encode(r, sizeof(ip_addr.s_addr));
free (r); free (r);
if (s) { if (s) {
printf ("\t%s", s); printf ("\t%s", s);

View File

@ -5,7 +5,7 @@
#include <ipfs/path/path.h> #include <ipfs/path/path.h>
// FromCid safely converts a cid.Cid type to a Path type // 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/"; const char prefix[] = "/ipfs/";
char *rpath, *cidstr = CidString(c); char *rpath, *cidstr = CidString(c);
@ -17,7 +17,7 @@ char* PathFromCid (struct Cid *c)
return rpath; 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; char *c, **r, *rbuf;
int i, dlen = strlen(delim); int i, dlen = strlen(delim);
@ -62,15 +62,15 @@ char** SplitN (char *p, char *delim, int n)
return r; return r;
} }
char** Segments (char *p) char** ipfs_path_split_segments (char *p)
{ {
if (*p == '/') p++; // Ignore leading slash if (*p == '/') p++; // Ignore leading slash
return SplitN (p, "/", -1); return ipfs_path_split_n (p, "/", -1);
} }
// Count Segments // Count segments
int SegmentsLength (char **s) int ipfs_path_segments_length (char **s)
{ {
int r = 0; int r = 0;
@ -81,8 +81,8 @@ int SegmentsLength (char **s)
return r; return r;
} }
// free memory allocated by Segments // free memory allocated by ipfs_path_split_segments
void FreeSegments (char ***s) void ipfs_path_free_segments (char ***s)
{ {
if (*s && **s) { if (*s && **s) {
free(**s); // free string buffer 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>. // ipfs_path_is_just_a_key returns true if the path is of the form <key> or /ipfs/<key>.
int IsJustAKey (char *p) int ipfs_path_is_just_a_key (char *p)
{ {
char **parts; char **parts;
int ret = 0; int ret = 0;
parts = Segments (p); parts = ipfs_path_split_segments (p);
if (parts) { if (parts) {
if (SegmentsLength (parts) == 2 && strcmp (parts[0], "ipfs") == 0) ret++; if (ipfs_path_segments_length (parts) == 2 && strcmp (parts[0], "ipfs") == 0) ret++;
FreeSegments(&parts); ipfs_path_free_segments(&parts);
} }
return ret; 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), // segment, separately. If there is no more to pop (the path is just a key),
// the original path is returned. // 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, '/'); *str = strrchr(p, '/');
if (!*str) return ErrBadPath; // error if (!*str) return ErrBadPath; // error
**str = '\0'; **str = '\0';
@ -117,7 +117,7 @@ int PopLastSegment (char **str, char *p)
return 0; return 0;
} }
char *PathFromSegments(char *prefix, char **seg) char *ipfs_path_from_segments(char *prefix, char **seg)
{ {
int retlen, i; int retlen, i;
char *ret; char *ret;
@ -140,7 +140,7 @@ char *PathFromSegments(char *prefix, char **seg)
return ret; return ret;
} }
int ParseCidToPath (char *dst, char *txt) int ipfs_path_parse_from_cid (char *dst, char *txt)
{ {
struct Cid *c; struct Cid *c;
char *r; char *r;
@ -153,7 +153,7 @@ int ParseCidToPath (char *dst, char *txt)
return ErrCidDecode; return ErrCidDecode;
} }
r = PathFromCid(c); r = ipfs_path_from_cid(c);
if (!r) { if (!r) {
return ErrCidDecode; return ErrCidDecode;
@ -163,7 +163,7 @@ int ParseCidToPath (char *dst, char *txt)
return 0; return 0;
} }
int ParsePath (char *dst, char *txt) int ipfs_path_parse (char *dst, char *txt)
{ {
int err, i; int err, i;
char *c; char *c;
@ -176,10 +176,10 @@ int ParsePath (char *dst, char *txt)
if (*txt == '/') { if (*txt == '/') {
txt++; txt++;
} }
err = ParseCidToPath (dst+plen, txt); err = ipfs_path_parse_from_cid (dst+plen, txt);
if (err == 0) { // only change dst if ParseCidToPath returned success. if (err == 0) { // only change dst if ipfs_path_parse_from_cid returned success.
// Use memcpy instead of strcpy to avoid overwriting // 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); memcpy (dst, prefix, plen);
} }
return err; return err;
@ -194,15 +194,15 @@ int ParsePath (char *dst, char *txt)
strcpy (buf, txt+6); // copy to temp buffer. strcpy (buf, txt+6); // copy to temp buffer.
c = strchr(buf, '/'); c = strchr(buf, '/');
if (c) *c = '\0'; if (c) *c = '\0';
return ParseCidToPath(dst, buf); return ipfs_path_parse_from_cid(dst, buf);
} else if (strcmp (txt, "/ipns/") != 0) { } else if (strcmp (txt, "/ipns/") != 0) {
return ErrBadPath; return ErrBadPath;
} }
return 0; return 0;
} }
int PathIsValid (char *p) int ipfs_path_is_valid (char *p)
{ {
char buf[4096]; char buf[4096];
return ParsePath(buf, p); return ipfs_path_parse(buf, p);
} }

View File

@ -1,20 +1,20 @@
#include "ipfs/cid/cid.h" #include "ipfs/cid/cid.h"
#include "ipfs/path/path.h" #include "ipfs/path/path.h"
Resolver* NewBasicResolver (DAGService *ds) Resolver* ipfs_path_new_basic_resolver (DAGService *ds)
{ {
Resolver *ret = malloc(sizeof(Resolver)); Resolver *ret = malloc(sizeof(Resolver));
if (!ret) return NULL; if (!ret) return NULL;
ret->DAG = ds; ret->DAG = ds;
ret->ResolveOnce = ResolveSingle; ret->ResolveOnce = ipfs_path_resolve_single;
return ret; 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. // 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++; if (strcmp (**parts, "ipfs") == 0) *parts++;
@ -26,9 +26,9 @@ int SplitAbsPath (struct Cid* cid, char ***parts, char *fpath)
return 0; return 0;
} }
// ResolvePath fetches the node for given path. It returns the last item // ipfs_path_resolve_path fetches the node for given path. It returns the last item
// returned by ResolvePathComponents. // returned by ipfs_path_resolve_path_components.
int ResolvePath(Node **nd, Context ctx, Resolver *s, char *fpath) int ipfs_path_resolve_path(Node **nd, Context ctx, Resolver *s, char *fpath)
{ {
int err = IsValid(fpath); int err = IsValid(fpath);
Node **ndd; Node **ndd;
@ -36,7 +36,7 @@ int ResolvePath(Node **nd, Context ctx, Resolver *s, char *fpath)
if (err) { if (err) {
return err; return err;
} }
err = ResolvePathComponents(&ndd, ctx, s, fpath); err = ipfs_path_resolve_path_components(&ndd, ctx, s, fpath);
if (err) { if (err) {
return err; return err;
} }
@ -50,21 +50,21 @@ int ResolvePath(Node **nd, Context ctx, Resolver *s, char *fpath)
return 0; 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 // It uses the first path component as a hash (key) of the first node, then
// resolves all other components walking the links, with ResolveLinks. // resolves all other components walking the links, with ipfs_path_resolve_links.
int ResolvePathComponents(Node ***nd, Context ctx, Resolver *s, char *fpath) int ipfs_path_resolve_path_components(Node ***nd, Context ctx, Resolver *s, char *fpath)
{ {
int err; int err;
struct Cid h; struct Cid h;
char **parts; char **parts;
err = SplitAbsPath(&h, &parts, fpath); err = ipfs_path_split_abs_path(&h, &parts, fpath);
if (err) { if (err) {
return err; return err;
} }
@ -75,27 +75,27 @@ int ResolvePathComponents(Node ***nd, Context ctx, Resolver *s, char *fpath)
// return DAG_ERR_VAL; // 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. // 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 // Returns the list of nodes forming the path, starting with ndd. This list is
// guaranteed never to be empty. // 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 // 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; int err, idx = 0;
NodeLink *lnk; NodeLink *lnk;
Node *nd; Node *nd;
*result = calloc (sizeof(Node*), SegmentsLength(names) + 1); *result = calloc (sizeof(Node*), ipfs_path_segments_length(names) + 1);
if (!*result) { if (!*result) {
return -1; return -1;
} }
memset (*result, NULL, sizeof(Node*) * (SegmentsLength(names)+1)); memset (*result, NULL, sizeof(Node*) * (ipfs_path_segments_length(names)+1));
*result[idx++] = ndd; *result[idx++] = ndd;
nd = ndd; // dup arg workaround nd = ndd; // dup arg workaround
@ -107,7 +107,7 @@ int ResolveLinks(Node ***result, Context ctx, Node *ndd, char **names)
//defer cancel() //defer cancel()
// for each of the path components // for each of the path components
err = ResolveLink(&lnk, *names); err = ipfs_path_resolve_link(&lnk, *names);
if (err) { if (err) {
char msg[51]; char msg[51];
*result[idx] = NULL; *result[idx] = NULL;