* Changed first parameter from int fd to FILE *f in the following functions:

* clixon_xml_parse_file(), clixon_json_parse_file(), yang_parse_file()
  * See [Bytewise read() of files is slow #146](https://github.com/clicon/clixon/issues/146)
This commit is contained in:
Olof hagsand 2020-11-04 22:21:42 +01:00
parent 7a0838da3a
commit c31b1c471b
21 changed files with 113 additions and 99 deletions

View file

@ -438,7 +438,7 @@ xmldb_readfile(clicon_handle h,
int retval = -1;
cxobj *x0 = NULL;
char *dbfile = NULL;
int fd = -1;
FILE *fp = NULL;
char *format;
int ret;
@ -453,15 +453,15 @@ xmldb_readfile(clicon_handle h,
goto done;
}
/* Parse file into internal XML tree from different formats */
if ((fd = open(dbfile, O_RDONLY)) < 0) {
if ((fp = fopen(dbfile, "r")) < 0) {
clicon_err(OE_UNIX, errno, "open(%s)", dbfile);
goto done;
}
if (strcmp(format, "json")==0){
if ((ret = clixon_json_parse_file(fd, yb, yspec, &x0, NULL)) < 0) /* XXX: ret == 0*/
if ((ret = clixon_json_parse_file(fp, yb, yspec, &x0, NULL)) < 0) /* XXX: ret == 0*/
goto done;
}
else if ((ret = clixon_xml_parse_file(fd, yb, yspec, "</config>", &x0, NULL)) < 0)
else if ((ret = clixon_xml_parse_file(fp, yb, yspec, "</config>", &x0, NULL)) < 0)
goto done;
#ifdef XMLDB_READFILE_FAIL /* The functions calling this function cannot handle a failed parse yet */
if (ret == 0)
@ -495,8 +495,8 @@ xmldb_readfile(clicon_handle h,
}
retval = 1;
done:
if (fd != -1)
close(fd);
if (fp)
fclose(fp);
if (dbfile)
free(dbfile);
if (x0)

View file

@ -1367,14 +1367,14 @@ clixon_json_parse_string(char *str,
* eg: name="a:b" -> prefix="a", name="b"
* But this is not done if yspec=NULL, and is not part of the JSON spec
*
* @param[in] fd File descriptor to the JSON file (ASCII string)
* @param[in] fp File descriptor to the JSON file (ASCII string)
* @param[in] yspec Yang specification, or NULL
* @param[in,out] xt Pointer to (XML) parse tree. If empty, create.
* @param[out] xerr Reason for invalid returned as netconf err msg
*
* @code
* cxobj *xt = NULL;
* if (clixon_json_parse_file(0, YB_MODULE, yspec, &xt) < 0)
* if (clixon_json_parse_file(stdin, YB_MODULE, yspec, &xt) < 0)
* err;
* xml_free(xt);
* @endcode
@ -1390,7 +1390,7 @@ clixon_json_parse_string(char *str,
* @see RFC7951
*/
int
clixon_json_parse_file(int fd,
clixon_json_parse_file(FILE *fp,
yang_bind yb,
yang_stmt *yspec,
cxobj **xt,
@ -1404,7 +1404,7 @@ clixon_json_parse_file(int fd,
char *ptr;
char ch;
int len = 0;
if (xt==NULL){
clicon_err(OE_XML, EINVAL, "xt is NULL");
return -1;
@ -1416,7 +1416,7 @@ clixon_json_parse_file(int fd,
memset(jsonbuf, 0, jsonbuflen);
ptr = jsonbuf;
while (1){
if ((ret = read(fd, &ch, 1)) < 0){
if ((ret = fread(&ch, 1, 1, fp)) < 0){
clicon_err(OE_XML, errno, "read");
break;
}

View file

@ -204,19 +204,19 @@ parse_configfile_one(const char *filename,
cxobj **xconfig)
{
int retval = -1;
int fd = -1;
FILE *fp = NULL;
cxobj *xt = NULL;
cxobj *xerr = NULL;
cxobj *xa;
cbuf *cbret = NULL;
int ret;
if ((fd = open(filename, O_RDONLY)) < 0){
if ((fp = fopen(filename, "r")) < 0){
clicon_err(OE_UNIX, errno, "open configure file: %s", filename);
return -1;
}
clicon_debug(2, "%s: Reading config file %s", __FUNCTION__, filename);
if ((ret = clixon_xml_parse_file(fd, yspec?YB_MODULE:YB_NONE, yspec, NULL, &xt, &xerr)) < 0)
if ((ret = clixon_xml_parse_file(fp, yspec?YB_MODULE:YB_NONE, yspec, NULL, &xt, &xerr)) < 0)
goto done;
if (ret == 0){
if ((cbret = cbuf_new()) ==NULL){
@ -249,8 +249,8 @@ parse_configfile_one(const char *filename,
done:
if (xt)
xml_free(xt);
if (fd != -1)
close(fd);
if (fp)
fclose(fp);
if (cbret)
cbuf_free(cbret);
if (xerr)

View file

@ -436,7 +436,7 @@ clixon_xml_changelog_init(clicon_handle h)
{
int retval = -1;
char *filename;
int fd = -1;
FILE *fp = NULL;
cxobj *xt = NULL;
yang_stmt *yspec;
int ret;
@ -445,11 +445,11 @@ clixon_xml_changelog_init(clicon_handle h)
yspec = clicon_dbspec_yang(h);
if ((filename = clicon_option_str(h, "CLICON_XML_CHANGELOG_FILE")) != NULL){
if ((fd = open(filename, O_RDONLY)) < 0){
clicon_err(OE_UNIX, errno, "open(%s)", filename);
if ((fp = fopen(filename, "r")) < 0){
clicon_err(OE_UNIX, errno, "fopen(%s)", filename);
goto done;
}
if (clixon_xml_parse_file(fd, YB_MODULE, yspec, NULL, &xt, NULL) < 0)
if (clixon_xml_parse_file(fp, YB_MODULE, yspec, NULL, &xt, NULL) < 0)
goto done;
if (xml_rootchild(xt, 0, &xt) < 0)
goto done;
@ -477,8 +477,8 @@ clixon_xml_changelog_init(clicon_handle h)
cbuf_free(cbret);
if (xret)
xml_free(xret);
if (fd != -1)
close(fd);
if (fp)
fclose(fp);
if (xt)
xml_free(xt);
return retval;

View file

@ -578,9 +578,9 @@ FSM(char *tag,
* @code
* cxobj *xt = NULL;
* cxobj *xerr = NULL;
* int fd;
* fd = open(filename, O_RDONLY);
* if ((ret = clixon_xml_parse_file(fd, YB_MODULE, yspec, "</config>", &xt, &xerr)) < 0)
* FILE *f;
* f = fopen(filename, "r");
* if ((ret = clixon_xml_parse_file(f, YB_MODULE, yspec, "</config>", &xt, &xerr)) < 0)
* err;
* xml_free(xt);
* @endcode
@ -590,7 +590,7 @@ FSM(char *tag,
* @note May block on file I/O
*/
int
clixon_xml_parse_file(int fd,
clixon_xml_parse_file(FILE *fp,
yang_bind yb,
yang_stmt *yspec,
char *endtag,
@ -626,9 +626,8 @@ clixon_xml_parse_file(int fd,
memset(xmlbuf, 0, xmlbuflen);
ptr = xmlbuf;
while (1){
if ((ret = read(fd, &ch, 1)) < 0){
clicon_err(OE_XML, errno, "read: [pid:%d]",
(int)getpid());
if ((ret = fread(&ch, 1, 1, fp)) < 0){
clicon_err(OE_XML, errno, "read");
break;
}
if (ret != 0){

View file

@ -704,7 +704,7 @@ yang_parse_str(char *str,
* @note this function simply parse a yang spec, no dependencies or checks
*/
yang_stmt *
yang_parse_file(int fd,
yang_parse_file(FILE *fp,
const char *name,
yang_stmt *yspec)
{
@ -723,7 +723,7 @@ yang_parse_file(int fd,
memset(buf, 0, len);
i = 0; /* position in buf */
while (1){ /* read the whole file */
if ((ret = read(fd, &c, 1)) < 0){
if ((ret = fread(&c, 1, 1, fp)) < 0){
clicon_err(OE_XML, errno, "read");
break;
}
@ -872,7 +872,7 @@ yang_parse_filename(const char *filename,
yang_stmt *yspec)
{
yang_stmt *ymod = NULL;
int fd = -1;
FILE *fp = NULL;
struct stat st;
clicon_debug(1, "%s %s", __FUNCTION__, filename);
@ -880,15 +880,15 @@ yang_parse_filename(const char *filename,
clicon_err(OE_YANG, errno, "%s not found", filename);
goto done;
}
if ((fd = open(filename, O_RDONLY)) < 0){
clicon_err(OE_YANG, errno, "open(%s)", filename);
if ((fp = fopen(filename, "r")) == NULL){
clicon_err(OE_YANG, errno, "fopen(%s)", filename);
goto done;
}
if ((ymod = yang_parse_file(fd, filename, yspec)) < 0)
if ((ymod = yang_parse_file(fp, filename, yspec)) < 0)
goto done;
done:
if (fd != -1)
close(fd);
if (fp)
fclose(fp);
return ymod; /* top-level (sub)module */
}