Commit timespec to file on Timespec.Add; Unlink timespec file on Timespec.Clear; Initialize timespec from file in channel_init()

This commit is contained in:
Pim van Pelt
2018-11-04 19:19:17 +01:00
parent 8c4864b199
commit 61ddb0334e
3 changed files with 26 additions and 2 deletions

View File

@ -274,6 +274,7 @@ bool timespec_write_file(struct mgos_timespec *ts, const char *fn) {
}
close(fd);
LOG(LL_INFO, ("Wrote timespec to %s", fn));
return true;
}
@ -294,13 +295,18 @@ bool timespec_read_file(struct mgos_timespec *ts, const char *fn) {
if (0 != stat(fn, &fp_stat)) {
LOG(LL_ERROR, ("Could not stat %s", fn));
return false;
}
if (fp_stat.st_size > 1024) {
LOG(LL_ERROR, ("File size of %s is larger than 1024 bytes (%u)", fn, (uint32_t)fp_stat.st_size));
return false;
}
buf = malloc(fp_stat.st_size + 1);
if (!buf) {
LOG(LL_ERROR, ("Could not malloc %u bytes for file %s", (uint32_t)fp_stat.st_size, fn));
return false;
}
if (!(fd = open(fn, O_RDONLY))) {
@ -316,7 +322,6 @@ bool timespec_read_file(struct mgos_timespec *ts, const char *fn) {
}
buf[fp_stat.st_size] = '\0';
close(fd);
LOG(LL_INFO, ("buf='%s'", buf));
// Wipe the timespec and parse back
timespec_clear_spec(ts);
@ -328,5 +333,6 @@ bool timespec_read_file(struct mgos_timespec *ts, const char *fn) {
}
}
free(buf);
LOG(LL_INFO, ("Read timespec from %s", fn));
return true;
}