Merge pull request #285 from krihal/feature_recursive_search
Fix for issue #284, do recursive lookup when reading YANG files from directory
This commit is contained in:
commit
52e63a14d0
7 changed files with 131 additions and 78 deletions
|
|
@ -43,6 +43,9 @@
|
||||||
int clicon_file_dirent(const char *dir, struct dirent **ent,
|
int clicon_file_dirent(const char *dir, struct dirent **ent,
|
||||||
const char *regexp, mode_t type);
|
const char *regexp, mode_t type);
|
||||||
|
|
||||||
|
int clicon_files_recursive(const char *dir, const char *regexp,
|
||||||
|
char **dp, int *nent);
|
||||||
|
|
||||||
int clicon_file_copy(char *src, char *target);
|
int clicon_file_copy(char *src, char *target);
|
||||||
|
|
||||||
#endif /* _CLIXON_FILE_H_ */
|
#endif /* _CLIXON_FILE_H_ */
|
||||||
|
|
|
||||||
|
|
@ -62,6 +62,86 @@
|
||||||
#include "clixon_log.h"
|
#include "clixon_log.h"
|
||||||
#include "clixon_file.h"
|
#include "clixon_file.h"
|
||||||
|
|
||||||
|
static int
|
||||||
|
clicon_file_string_sort(const void *arg1,
|
||||||
|
const void *arg2)
|
||||||
|
{
|
||||||
|
char *str1 = *(char **)arg1;
|
||||||
|
char *str2 = *(char **)arg2;
|
||||||
|
|
||||||
|
return strcmp(str1, str2);
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
clicon_files_recursive(const char *dir,
|
||||||
|
const char *regexp,
|
||||||
|
char **dp,
|
||||||
|
int *nent)
|
||||||
|
{
|
||||||
|
struct dirent *dent = NULL;
|
||||||
|
char path[MAXPATHLEN];
|
||||||
|
DIR *dirp = NULL;
|
||||||
|
regex_t re;
|
||||||
|
int res;
|
||||||
|
char errbuf[128];
|
||||||
|
|
||||||
|
if (regexp && (res = regcomp(&re, regexp, REG_EXTENDED)) != 0) {
|
||||||
|
regerror(res, &re, errbuf, sizeof(errbuf));
|
||||||
|
clicon_err(OE_DB, 0, "regcomp: %s", errbuf);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!(dirp = opendir(dir))) {
|
||||||
|
return *nent;
|
||||||
|
}
|
||||||
|
|
||||||
|
while ((dent = readdir(dirp)) != NULL) {
|
||||||
|
if (dent->d_type == DT_DIR) {
|
||||||
|
/* If we find a directory we might want to enter it, unless it
|
||||||
|
is the current directory (.) or parent (..) */
|
||||||
|
if (strcmp(dent->d_name, ".") == 0 || strcmp(dent->d_name, "..") == 0) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Build the new directory and enter it. */
|
||||||
|
sprintf(path, "%s/%s", dir, dent->d_name);
|
||||||
|
*nent = clicon_files_recursive(path, regexp, dp, nent);
|
||||||
|
} else if (dent->d_type == DT_REG) {
|
||||||
|
/* If we encounter a file, match it against the regexp and
|
||||||
|
add it to the list of found files.*/
|
||||||
|
if (regexp) {
|
||||||
|
if (regexec(&re, dent->d_name, (size_t)0, NULL, *nent) != 0)
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Add room for the new file. */
|
||||||
|
if ((dp = (char **)realloc(dp, sizeof(char *) * (*nent + 1))) == NULL) {
|
||||||
|
clicon_err(OE_UNIX, errno, "realloc");
|
||||||
|
*nent = -1;
|
||||||
|
goto quit;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((dp[*nent] = (char *)malloc(1 + sizeof(char) * strlen(dir) +
|
||||||
|
strlen(dent->d_name))) == NULL) {
|
||||||
|
clicon_err(OE_UNIX, errno, "malloc");
|
||||||
|
*nent = -1;
|
||||||
|
goto quit;
|
||||||
|
}
|
||||||
|
|
||||||
|
sprintf(dp[*nent], "%s/%s", dir, dent->d_name);
|
||||||
|
(*nent)++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
qsort(dp, *nent, sizeof(char *), clicon_file_string_sort);
|
||||||
|
|
||||||
|
quit:
|
||||||
|
if (dirp)
|
||||||
|
closedir(dirp);
|
||||||
|
|
||||||
|
return *nent;
|
||||||
|
}
|
||||||
|
|
||||||
/*! qsort "compar" for directory alphabetically sorting, see qsort(3)
|
/*! qsort "compar" for directory alphabetically sorting, see qsort(3)
|
||||||
*/
|
*/
|
||||||
static int
|
static int
|
||||||
|
|
|
||||||
|
|
@ -903,57 +903,69 @@ yang_parse_find_match(clicon_handle h,
|
||||||
cbuf *fbuf)
|
cbuf *fbuf)
|
||||||
{
|
{
|
||||||
int retval = -1;
|
int retval = -1;
|
||||||
struct dirent *dp = NULL;
|
char **dp = NULL;
|
||||||
int ndp;
|
int ndp;
|
||||||
cbuf *regex = NULL;
|
cbuf *regex = NULL;
|
||||||
cxobj *x;
|
cxobj *x;
|
||||||
cxobj *xc;
|
cxobj *xc;
|
||||||
char *dir;
|
char *dir;
|
||||||
|
int nent = 0;
|
||||||
|
int i = 0;
|
||||||
|
|
||||||
/* get clicon config file in xml form */
|
/* get clicon config file in xml form */
|
||||||
if ((x = clicon_conf_xml(h)) == NULL)
|
if ((x = clicon_conf_xml(h)) == NULL)
|
||||||
goto ok;
|
goto ok;
|
||||||
if ((regex = cbuf_new()) == NULL){
|
if ((regex = cbuf_new()) == NULL){
|
||||||
clicon_err(OE_YANG, errno, "cbuf_new");
|
clicon_err(OE_YANG, errno, "cbuf_new");
|
||||||
goto done;
|
goto done;
|
||||||
}
|
}
|
||||||
/* RFC 6020: The name of the file SHOULD be of the form:
|
/* RFC 6020: The name of the file SHOULD be of the form:
|
||||||
* module-or-submodule-name ['@' revision-date] ( '.yang' / '.yin' )
|
* module-or-submodule-name ['@' revision-date] ( '.yang' / '.yin' )
|
||||||
* revision-date ::= 4DIGIT "-" 2DIGIT "-" 2DIGIT
|
* revision-date ::= 4DIGIT "-" 2DIGIT "-" 2DIGIT
|
||||||
*/
|
*/
|
||||||
if (revision)
|
if (revision)
|
||||||
cprintf(regex, "^%s@%s(.yang)$", module, revision);
|
cprintf(regex, "^%s@%s(.yang)$", module, revision);
|
||||||
else
|
else
|
||||||
cprintf(regex, "^%s(@[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9])?(.yang)$",
|
cprintf(regex, "^%s(@[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9])?(.yang)$",
|
||||||
module);
|
module);
|
||||||
xc = NULL;
|
xc = NULL;
|
||||||
|
|
||||||
while ((xc = xml_child_each(x, xc, CX_ELMNT)) != NULL) {
|
while ((xc = xml_child_each(x, xc, CX_ELMNT)) != NULL) {
|
||||||
/* Skip if not yang dir */
|
/* Skip if not yang dir */
|
||||||
if (strcmp(xml_name(xc), "CLICON_YANG_DIR") != 0 &&
|
if (strcmp(xml_name(xc), "CLICON_YANG_DIR") != 0 &&
|
||||||
strcmp(xml_name(xc), "CLICON_YANG_MAIN_DIR") != 0)
|
strcmp(xml_name(xc), "CLICON_YANG_MAIN_DIR") != 0)
|
||||||
continue;
|
continue;
|
||||||
dir = xml_body(xc);
|
dir = xml_body(xc);
|
||||||
/* get all matching files in this directory */
|
|
||||||
if ((ndp = clicon_file_dirent(dir,
|
/* get all matching files in this directory */
|
||||||
&dp,
|
dp = (char **)malloc(sizeof(char *));
|
||||||
cbuf_get(regex),
|
if ((ndp = clicon_files_recursive(dir,
|
||||||
S_IFREG)) < 0)
|
cbuf_get(regex),
|
||||||
goto done;
|
dp,
|
||||||
/* Entries are sorted, last entry should be most recent date
|
&nent)) < 0) {
|
||||||
*/
|
goto done;
|
||||||
if (ndp != 0){
|
}
|
||||||
cprintf(fbuf, "%s/%s", dir, dp[ndp-1].d_name);
|
|
||||||
retval = 1;
|
/* Entries are sorted, last entry should be most recent date
|
||||||
goto done;
|
*/
|
||||||
}
|
if (ndp != 0){
|
||||||
|
cprintf(fbuf, "%s", dp[ndp-1]);
|
||||||
|
|
||||||
|
for (i = 0; i < nent; i++)
|
||||||
|
if (dp[i])
|
||||||
|
free(dp[i]);
|
||||||
|
|
||||||
|
retval = 1;
|
||||||
|
goto done;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
ok:
|
ok:
|
||||||
retval = 0;
|
retval = 0;
|
||||||
done:
|
done:
|
||||||
if (regex)
|
if (regex)
|
||||||
cbuf_free(regex);
|
cbuf_free(regex);
|
||||||
if (dp)
|
if (dp)
|
||||||
free(dp);
|
free(dp);
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -1656,8 +1668,8 @@ yang_spec_load_dir(clicon_handle h,
|
||||||
/* Load all yang files in dir */
|
/* Load all yang files in dir */
|
||||||
for (i = 0; i < ndp; i++) {
|
for (i = 0; i < ndp; i++) {
|
||||||
/* base = module name [+ @rev ] + .yang */
|
/* base = module name [+ @rev ] + .yang */
|
||||||
if (oldbase)
|
if (oldbase)
|
||||||
free(oldbase);
|
free(oldbase);
|
||||||
oldbase = base;
|
oldbase = base;
|
||||||
base = NULL;
|
base = NULL;
|
||||||
revf = 0;
|
revf = 0;
|
||||||
|
|
|
||||||
|
|
@ -33,14 +33,12 @@ if [ ! -d "$OPENCONFIG" ]; then
|
||||||
if [ "$s" = $0 ]; then exit 0; else return 0; fi
|
if [ "$s" = $0 ]; then exit 0; else return 0; fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
OCDIR=$OPENCONFIG/release/models
|
|
||||||
|
|
||||||
cat <<EOF > $cfg
|
cat <<EOF > $cfg
|
||||||
<clixon-config xmlns="http://clicon.org/config">
|
<clixon-config xmlns="http://clicon.org/config">
|
||||||
<CLICON_CONFIGFILE>$cfg</CLICON_CONFIGFILE>
|
<CLICON_CONFIGFILE>$cfg</CLICON_CONFIGFILE>
|
||||||
<CLICON_YANG_DIR>/usr/local/share/clixon</CLICON_YANG_DIR>
|
<CLICON_YANG_DIR>/usr/local/share/clixon</CLICON_YANG_DIR>
|
||||||
<CLICON_YANG_DIR>$dir</CLICON_YANG_DIR>
|
<CLICON_YANG_DIR>$dir</CLICON_YANG_DIR>
|
||||||
<CLICON_YANG_DIR>$OCDIR/</CLICON_YANG_DIR>
|
<CLICON_YANG_DIR>$OPENCONFIG/</CLICON_YANG_DIR>
|
||||||
<CLICON_YANG_MAIN_DIR>$dir</CLICON_YANG_MAIN_DIR>
|
<CLICON_YANG_MAIN_DIR>$dir</CLICON_YANG_MAIN_DIR>
|
||||||
<CLICON_BACKEND_DIR>/usr/local/lib/$APPNAME/backend</CLICON_BACKEND_DIR>
|
<CLICON_BACKEND_DIR>/usr/local/lib/$APPNAME/backend</CLICON_BACKEND_DIR>
|
||||||
<CLICON_CLISPEC_DIR>$clidir</CLICON_CLISPEC_DIR>
|
<CLICON_CLISPEC_DIR>$clidir</CLICON_CLISPEC_DIR>
|
||||||
|
|
|
||||||
|
|
@ -23,46 +23,12 @@ if [ ! -d "$OPENCONFIG" ]; then
|
||||||
if [ "$s" = $0 ]; then exit 0; else return 0; fi
|
if [ "$s" = $0 ]; then exit 0; else return 0; fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
OCDIR=$OPENCONFIG/release/models
|
|
||||||
|
|
||||||
cat <<EOF > $cfg
|
cat <<EOF > $cfg
|
||||||
<clixon-config xmlns="http://clicon.org/config">
|
<clixon-config xmlns="http://clicon.org/config">
|
||||||
<CLICON_CONFIGFILE>$cfg</CLICON_CONFIGFILE>
|
<CLICON_CONFIGFILE>$cfg</CLICON_CONFIGFILE>
|
||||||
<CLICON_FEATURE>ietf-netconf:startup</CLICON_FEATURE>
|
<CLICON_FEATURE>ietf-netconf:startup</CLICON_FEATURE>
|
||||||
<CLICON_YANG_DIR>/usr/local/share/clixon</CLICON_YANG_DIR>
|
<CLICON_YANG_DIR>/usr/local/share/clixon</CLICON_YANG_DIR>
|
||||||
<CLICON_YANG_DIR>$OCDIR</CLICON_YANG_DIR>
|
<CLICON_YANG_DIR>$OPENCONFIG</CLICON_YANG_DIR>
|
||||||
<CLICON_YANG_DIR>$OCDIR/acl</CLICON_YANG_DIR>
|
|
||||||
<CLICON_YANG_DIR>$OCDIR/aft</CLICON_YANG_DIR>
|
|
||||||
<CLICON_YANG_DIR>$OCDIR/bfd</CLICON_YANG_DIR>
|
|
||||||
<CLICON_YANG_DIR>$OCDIR/bgp</CLICON_YANG_DIR>
|
|
||||||
<CLICON_YANG_DIR>$OCDIR/catalog</CLICON_YANG_DIR>
|
|
||||||
<CLICON_YANG_DIR>$OCDIR/firewall</CLICON_YANG_DIR>
|
|
||||||
<CLICON_YANG_DIR>$OCDIR/interfaces</CLICON_YANG_DIR>
|
|
||||||
<CLICON_YANG_DIR>$OCDIR/isis</CLICON_YANG_DIR>
|
|
||||||
<CLICON_YANG_DIR>$OCDIR/lacp</CLICON_YANG_DIR>
|
|
||||||
<CLICON_YANG_DIR>$OCDIR/lldp</CLICON_YANG_DIR>
|
|
||||||
<CLICON_YANG_DIR>$OCDIR/local-routing</CLICON_YANG_DIR>
|
|
||||||
<CLICON_YANG_DIR>$OCDIR/macsec</CLICON_YANG_DIR>
|
|
||||||
<CLICON_YANG_DIR>$OCDIR/mpls</CLICON_YANG_DIR>
|
|
||||||
<CLICON_YANG_DIR>$OCDIR/multicast</CLICON_YANG_DIR>
|
|
||||||
<CLICON_YANG_DIR>$OCDIR/network-instance</CLICON_YANG_DIR>
|
|
||||||
<CLICON_YANG_DIR>$OCDIR/openflow</CLICON_YANG_DIR>
|
|
||||||
<CLICON_YANG_DIR>$OCDIR/optical-transport</CLICON_YANG_DIR>
|
|
||||||
<CLICON_YANG_DIR>$OCDIR/ospf</CLICON_YANG_DIR>
|
|
||||||
<CLICON_YANG_DIR>$OCDIR/platform</CLICON_YANG_DIR>
|
|
||||||
<CLICON_YANG_DIR>$OCDIR/policy</CLICON_YANG_DIR>
|
|
||||||
<CLICON_YANG_DIR>$OCDIR/policy-forwarding</CLICON_YANG_DIR>
|
|
||||||
<CLICON_YANG_DIR>$OCDIR/probes</CLICON_YANG_DIR>
|
|
||||||
<CLICON_YANG_DIR>$OCDIR/qos</CLICON_YANG_DIR>
|
|
||||||
<CLICON_YANG_DIR>$OCDIR/relay-agent</CLICON_YANG_DIR>
|
|
||||||
<CLICON_YANG_DIR>$OCDIR/rib</CLICON_YANG_DIR>
|
|
||||||
<CLICON_YANG_DIR>$OCDIR/segment-routing</CLICON_YANG_DIR>
|
|
||||||
<CLICON_YANG_DIR>$OCDIR/stp</CLICON_YANG_DIR>
|
|
||||||
<CLICON_YANG_DIR>$OCDIR/system</CLICON_YANG_DIR>
|
|
||||||
<CLICON_YANG_DIR>$OCDIR/telemetry</CLICON_YANG_DIR>
|
|
||||||
<CLICON_YANG_DIR>$OCDIR/types</CLICON_YANG_DIR>
|
|
||||||
<CLICON_YANG_DIR>$OCDIR/vlan</CLICON_YANG_DIR>
|
|
||||||
<CLICON_YANG_DIR>$OCDIR/wifi</CLICON_YANG_DIR>
|
|
||||||
<CLICON_CLISPEC_DIR>/usr/local/lib/$APPNAME/clispec</CLICON_CLISPEC_DIR>
|
<CLICON_CLISPEC_DIR>/usr/local/lib/$APPNAME/clispec</CLICON_CLISPEC_DIR>
|
||||||
<CLICON_CLI_DIR>/usr/local/lib/$APPNAME/cli</CLICON_CLI_DIR>
|
<CLICON_CLI_DIR>/usr/local/lib/$APPNAME/cli</CLICON_CLI_DIR>
|
||||||
<CLICON_CLI_MODE>$APPNAME</CLICON_CLI_MODE>
|
<CLICON_CLI_MODE>$APPNAME</CLICON_CLI_MODE>
|
||||||
|
|
@ -73,7 +39,7 @@ cat <<EOF > $cfg
|
||||||
</clixon-config>
|
</clixon-config>
|
||||||
EOF
|
EOF
|
||||||
|
|
||||||
files=$(find $OCDIR -name "*.yang")
|
files=$(find $OPENCONFIG -name "*.yang")
|
||||||
# Count nr of modules (exclude submodule) Assume "module" or "submodule"
|
# Count nr of modules (exclude submodule) Assume "module" or "submodule"
|
||||||
# first word on first line
|
# first word on first line
|
||||||
let ms=0; # Nr of modules
|
let ms=0; # Nr of modules
|
||||||
|
|
|
||||||
|
|
@ -17,18 +17,12 @@ if [ ! -d "$OPENCONFIG" ]; then
|
||||||
if [ "$s" = $0 ]; then exit 0; else return 0; fi
|
if [ "$s" = $0 ]; then exit 0; else return 0; fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
OCDIR=$OPENCONFIG/release/models
|
|
||||||
|
|
||||||
cat <<EOF > $cfg
|
cat <<EOF > $cfg
|
||||||
<clixon-config xmlns="http://clicon.org/config">
|
<clixon-config xmlns="http://clicon.org/config">
|
||||||
<CLICON_CONFIGFILE>$cfg</CLICON_CONFIGFILE>
|
<CLICON_CONFIGFILE>$cfg</CLICON_CONFIGFILE>
|
||||||
<CLICON_FEATURE>ietf-netconf:startup</CLICON_FEATURE>
|
<CLICON_FEATURE>ietf-netconf:startup</CLICON_FEATURE>
|
||||||
<CLICON_YANG_DIR>/usr/local/share/clixon</CLICON_YANG_DIR>
|
<CLICON_YANG_DIR>/usr/local/share/clixon</CLICON_YANG_DIR>
|
||||||
<CLICON_YANG_DIR>$OCDIR</CLICON_YANG_DIR>
|
<CLICON_YANG_DIR>$OPENCONFIG</CLICON_YANG_DIR>
|
||||||
<CLICON_YANG_DIR>$OCDIR/interfaces</CLICON_YANG_DIR>
|
|
||||||
<CLICON_YANG_DIR>$OCDIR/types</CLICON_YANG_DIR>
|
|
||||||
<CLICON_YANG_DIR>$OCDIR/vlan</CLICON_YANG_DIR>
|
|
||||||
<CLICON_YANG_DIR>$OCDIR/platform</CLICON_YANG_DIR>
|
|
||||||
<CLICON_YANG_MAIN_FILE>$fyang</CLICON_YANG_MAIN_FILE>
|
<CLICON_YANG_MAIN_FILE>$fyang</CLICON_YANG_MAIN_FILE>
|
||||||
<CLICON_CLISPEC_DIR>/usr/local/lib/$APPNAME/clispec</CLICON_CLISPEC_DIR>
|
<CLICON_CLISPEC_DIR>/usr/local/lib/$APPNAME/clispec</CLICON_CLISPEC_DIR>
|
||||||
<CLICON_CLI_DIR>/usr/local/lib/$APPNAME/cli</CLICON_CLI_DIR>
|
<CLICON_CLI_DIR>/usr/local/lib/$APPNAME/cli</CLICON_CLI_DIR>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue