parameters: open export files O_TRUNC to discard previous data

- rcS parameter backup try to directly restore param (FRAM) from backup (in case SD card is removed before successful export)
 - rcS parameter backup logging rearrange to capture more logging output (param_import_fail.txt)
 - posix rcS try to keep param backup and restore roughly in sync with NuttX rcS
 - tinybson fix debug printf format
 - param_export_internal ensure file descriptor positioned at 0 (precaution)
This commit is contained in:
Daniel Agar
2022-08-17 17:45:29 -04:00
parent 296b1704c5
commit 3f3a5f19f0
6 changed files with 69 additions and 28 deletions
+7 -2
View File
@@ -1116,7 +1116,7 @@ int param_save_default()
for (int attempt = 1; attempt <= MAX_ATTEMPTS; attempt++) {
// write parameters to file
int fd = ::open(filename, O_WRONLY | O_CREAT, PX4_O_MODE_666);
int fd = ::open(filename, O_WRONLY | O_CREAT | O_TRUNC, PX4_O_MODE_666);
if (fd > -1) {
perf_begin(param_export_perf);
@@ -1155,7 +1155,7 @@ int param_save_default()
// backup file
if (param_backup_file) {
int fd_backup_file = ::open(param_backup_file, O_WRONLY | O_CREAT, PX4_O_MODE_666);
int fd_backup_file = ::open(param_backup_file, O_WRONLY | O_CREAT | O_TRUNC, PX4_O_MODE_666);
if (fd_backup_file > -1) {
int backup_export_ret = param_export_internal(fd_backup_file, nullptr);
@@ -1374,6 +1374,11 @@ static int param_export_internal(int fd, param_filter_func filter)
bson_encoder_s encoder{};
uint8_t bson_buffer[256];
if (lseek(fd, 0, SEEK_SET) != 0) {
PX4_ERR("export seek failed %d", errno);
return -1;
}
if (bson_encoder_init_buf_file(&encoder, fd, &bson_buffer, sizeof(bson_buffer)) != 0) {
goto out;
}
+2 -2
View File
@@ -147,7 +147,7 @@ bson_decoder_init_buf(bson_decoder_t decoder, void *buf, unsigned bufsize, bson_
if (bufsize == 0) {
decoder->bufsize = *(uint32_t *)buf;
debug("auto-detected %u byte object", decoder->bufsize);
debug("auto-detected %zu byte object", decoder->bufsize);
} else {
decoder->bufsize = bufsize;
@@ -403,7 +403,7 @@ write_x(bson_encoder_t encoder, const void *p, size_t s)
memcpy(encoder->buf + encoder->bufpos, p, s);
encoder->bufpos += s;
debug("appended %d bytes", s);
debug("appended %zu bytes", s);
return 0;
}