Skip to content

Commit

Permalink
AP_Filesystem: add crc 32 method
Browse files Browse the repository at this point in the history
  • Loading branch information
IamPete1 committed Nov 25, 2023
1 parent ea23772 commit ecb2718
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
32 changes: 32 additions & 0 deletions libraries/AP_Filesystem/AP_Filesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "AP_Filesystem_config.h"
#include <AP_HAL/HAL.h>
#include <AP_HAL/Util.h>
#include <AP_Math/AP_Math.h>

static AP_Filesystem fs;

Expand Down Expand Up @@ -286,6 +287,37 @@ bool AP_Filesystem::fgets(char *buf, uint8_t buflen, int fd)
return true;
}

// run crc32 over file with given name, returns true if successful
bool AP_Filesystem::crc32(const char *fname, uint32_t& checksum)
{
// Open file in readonly mode
int fd = open(fname, O_RDONLY);
if (fd == -1) {
return false;
}

// Buffer to store data temporarily
const ssize_t buff_len = 64;
uint8_t buf[buff_len];

// Read into buffer and run crc
ssize_t read_size;
do {
read_size = read(fd, buf, buff_len);
if (read_size == -1) {
// Read error, note that we have changed the checksum value in this case
close(fd);
return false;
}
checksum = crc_crc32(checksum, buf, MIN(read_size, buff_len));
} while (read_size > 0);

close(fd);

return true;
}


#if AP_FILESYSTEM_FORMAT_ENABLED
// format filesystem
bool AP_Filesystem::format(void)
Expand Down
3 changes: 3 additions & 0 deletions libraries/AP_Filesystem/AP_Filesystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ class AP_Filesystem {
// returns null-terminated string; cr or lf terminates line
bool fgets(char *buf, uint8_t buflen, int fd);

// run crc32 over file with given name, returns true if successful
bool crc32(const char *fname, uint32_t& checksum) WARN_IF_UNUSED;

// format filesystem. This is async, monitor get_format_status for progress
bool format(void);

Expand Down

0 comments on commit ecb2718

Please sign in to comment.