Skip to content

Commit

Permalink
Merge pull request #285 from krihal/feature_recursive_search
Browse files Browse the repository at this point in the history
Fix for issue #284, do recursive lookup when reading YANG files from directory
  • Loading branch information
olofhagsand authored Nov 9, 2021
2 parents a1fe080 + 02a5d97 commit 52e63a1
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 78 deletions.
3 changes: 3 additions & 0 deletions lib/clixon/clixon_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@
int clicon_file_dirent(const char *dir, struct dirent **ent,
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);

#endif /* _CLIXON_FILE_H_ */
80 changes: 80 additions & 0 deletions lib/src/clixon_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,86 @@
#include "clixon_log.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)
*/
static int
Expand Down
74 changes: 43 additions & 31 deletions lib/src/clixon_yang_parse_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -903,57 +903,69 @@ yang_parse_find_match(clicon_handle h,
cbuf *fbuf)
{
int retval = -1;
struct dirent *dp = NULL;
char **dp = NULL;
int ndp;
cbuf *regex = NULL;
cxobj *x;
cxobj *xc;
char *dir;
int nent = 0;
int i = 0;

/* get clicon config file in xml form */
if ((x = clicon_conf_xml(h)) == NULL)
goto ok;
goto ok;
if ((regex = cbuf_new()) == NULL){
clicon_err(OE_YANG, errno, "cbuf_new");
goto done;
clicon_err(OE_YANG, errno, "cbuf_new");
goto done;
}
/* RFC 6020: The name of the file SHOULD be of the form:
* module-or-submodule-name ['@' revision-date] ( '.yang' / '.yin' )
* revision-date ::= 4DIGIT "-" 2DIGIT "-" 2DIGIT
*/
if (revision)
cprintf(regex, "^%s@%s(.yang)$", module, revision);
cprintf(regex, "^%s@%s(.yang)$", module, revision);
else
cprintf(regex, "^%s(@[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9])?(.yang)$",
module);
cprintf(regex, "^%s(@[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9])?(.yang)$",
module);
xc = NULL;

while ((xc = xml_child_each(x, xc, CX_ELMNT)) != NULL) {
/* Skip if not yang dir */
if (strcmp(xml_name(xc), "CLICON_YANG_DIR") != 0 &&
strcmp(xml_name(xc), "CLICON_YANG_MAIN_DIR") != 0)
continue;
dir = xml_body(xc);
/* get all matching files in this directory */
if ((ndp = clicon_file_dirent(dir,
&dp,
cbuf_get(regex),
S_IFREG)) < 0)
goto done;
/* Entries are sorted, last entry should be most recent date
*/
if (ndp != 0){
cprintf(fbuf, "%s/%s", dir, dp[ndp-1].d_name);
retval = 1;
goto done;
}
/* Skip if not yang dir */
if (strcmp(xml_name(xc), "CLICON_YANG_DIR") != 0 &&
strcmp(xml_name(xc), "CLICON_YANG_MAIN_DIR") != 0)
continue;
dir = xml_body(xc);

/* get all matching files in this directory */
dp = (char **)malloc(sizeof(char *));
if ((ndp = clicon_files_recursive(dir,
cbuf_get(regex),
dp,
&nent)) < 0) {
goto done;
}

/* Entries are sorted, last entry should be most recent date
*/
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;
done:
done:
if (regex)
cbuf_free(regex);
cbuf_free(regex);
if (dp)
free(dp);
free(dp);
return retval;
}

Expand Down Expand Up @@ -1656,8 +1668,8 @@ yang_spec_load_dir(clicon_handle h,
/* Load all yang files in dir */
for (i = 0; i < ndp; i++) {
/* base = module name [+ @rev ] + .yang */
if (oldbase)
free(oldbase);
if (oldbase)
free(oldbase);
oldbase = base;
base = NULL;
revf = 0;
Expand Down
4 changes: 1 addition & 3 deletions test/test_cli_auto_genmodel.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,12 @@ if [ ! -d "$OPENCONFIG" ]; then
if [ "$s" = $0 ]; then exit 0; else return 0; fi
fi

OCDIR=$OPENCONFIG/release/models

cat <<EOF > $cfg
<clixon-config xmlns="http://clicon.org/config">
<CLICON_CONFIGFILE>$cfg</CLICON_CONFIGFILE>
<CLICON_YANG_DIR>/usr/local/share/clixon</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_BACKEND_DIR>/usr/local/lib/$APPNAME/backend</CLICON_BACKEND_DIR>
<CLICON_CLISPEC_DIR>$clidir</CLICON_CLISPEC_DIR>
Expand Down
38 changes: 2 additions & 36 deletions test/test_openconfig.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,46 +23,12 @@ if [ ! -d "$OPENCONFIG" ]; then
if [ "$s" = $0 ]; then exit 0; else return 0; fi
fi

OCDIR=$OPENCONFIG/release/models

cat <<EOF > $cfg
<clixon-config xmlns="http://clicon.org/config">
<CLICON_CONFIGFILE>$cfg</CLICON_CONFIGFILE>
<CLICON_FEATURE>ietf-netconf:startup</CLICON_FEATURE>
<CLICON_YANG_DIR>/usr/local/share/clixon</CLICON_YANG_DIR>
<CLICON_YANG_DIR>$OCDIR</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_YANG_DIR>$OPENCONFIG</CLICON_YANG_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_MODE>$APPNAME</CLICON_CLI_MODE>
Expand All @@ -73,7 +39,7 @@ cat <<EOF > $cfg
</clixon-config>
EOF

files=$(find $OCDIR -name "*.yang")
files=$(find $OPENCONFIG -name "*.yang")
# Count nr of modules (exclude submodule) Assume "module" or "submodule"
# first word on first line
let ms=0; # Nr of modules
Expand Down
8 changes: 1 addition & 7 deletions test/test_openconfig_interfaces.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,12 @@ if [ ! -d "$OPENCONFIG" ]; then
if [ "$s" = $0 ]; then exit 0; else return 0; fi
fi

OCDIR=$OPENCONFIG/release/models

cat <<EOF > $cfg
<clixon-config xmlns="http://clicon.org/config">
<CLICON_CONFIGFILE>$cfg</CLICON_CONFIGFILE>
<CLICON_FEATURE>ietf-netconf:startup</CLICON_FEATURE>
<CLICON_YANG_DIR>/usr/local/share/clixon</CLICON_YANG_DIR>
<CLICON_YANG_DIR>$OCDIR</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_DIR>$OPENCONFIG</CLICON_YANG_DIR>
<CLICON_YANG_MAIN_FILE>$fyang</CLICON_YANG_MAIN_FILE>
<CLICON_CLISPEC_DIR>/usr/local/lib/$APPNAME/clispec</CLICON_CLISPEC_DIR>
<CLICON_CLI_DIR>/usr/local/lib/$APPNAME/cli</CLICON_CLI_DIR>
Expand Down
2 changes: 1 addition & 1 deletion test/test_upgrade_repair.sh
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ cat <<EOF > $cfg
<CLICON_CONFIGFILE>$cfg</CLICON_CONFIGFILE>
<CLICON_FEATURE>ietf-netconf:startup</CLICON_FEATURE>
<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_MAIN_DIR>$dir</CLICON_YANG_MAIN_DIR>
<CLICON_SOCK>/usr/local/var/$APPNAME/$APPNAME.sock</CLICON_SOCK>
<CLICON_BACKEND_DIR>/usr/local/lib/example/backend</CLICON_BACKEND_DIR>
Expand Down

0 comments on commit 52e63a1

Please sign in to comment.