validate JSON before loading

This commit also introduces slurp() which reads a file in its entirety. Using
this function instead of doing IO in the functions in load_layout.c again and
again makes the code cleaner (fixing at least two memory leaks) and avoids
re-reading the same file 3 times.

related to #2755
This commit is contained in:
Michael Stapelberg
2017-09-13 17:14:51 +02:00
parent 72c972a36c
commit c45c002bad
6 changed files with 131 additions and 83 deletions

View File

@ -31,6 +31,12 @@ typedef enum {
* determine whether the file contains workspaces or regular containers, which
* is important to know when deciding where (and how) to append the contents.
* */
json_content_t json_determine_content(const char *filename);
json_content_t json_determine_content(const char *buf, const size_t len);
void tree_append_json(Con *con, const char *filename, char **errormsg);
/**
* Returns true if the provided JSON could be parsed by yajl.
*
*/
bool json_validate(const char *buf, const size_t len);
void tree_append_json(Con *con, const char *buf, const size_t len, char **errormsg);

View File

@ -164,3 +164,11 @@ void kill_nagbar(pid_t *nagbar_pid, bool wait_for_it);
* if the number could be parsed.
*/
bool parse_long(const char *str, long *out, int base);
/**
* Slurp reads path in its entirety into buf, returning the length of the file
* or -1 if the file could not be read. buf is set to a buffer of appropriate
* size, or NULL if -1 is returned.
*
*/
ssize_t slurp(const char *path, char **buf);