Clean up + use 8 bytes for uint64_t

custom
ailurux 7 months ago
parent a169e60cac
commit 1d2153f69d
  1. 56
      src/tangara/audio/playlist.cpp

@ -102,7 +102,6 @@ auto Playlist::skipTo(size_t position) -> void {
skipToLocked(position); skipToLocked(position);
} }
// Serialise the cache to a file to avoid having to rescan // Serialise the cache to a file to avoid having to rescan
// the entire queue when resuming // the entire queue when resuming
auto Playlist::serialiseCache() -> bool { auto Playlist::serialiseCache() -> bool {
@ -122,14 +121,18 @@ auto Playlist::serialiseCache() -> bool {
return false; return false;
} }
uint8_t header[8]; uint8_t header[12];
// First 4 bytes = file size of queue file (for checking this file matches) // First 8 bytes = file size of queue file (for checking this file matches)
uint32_t q_file_size = f_size(&file_); uint64_t q_file_size = f_size(&file_);
header[0] = q_file_size >> 24; header[0] = q_file_size >> 56;
header[1] = q_file_size >> 16; header[1] = q_file_size >> 48;
header[2] = q_file_size >> 8; header[2] = q_file_size >> 40;
header[3] = q_file_size; header[3] = q_file_size >> 32;
header[4] = q_file_size >> 24;
header[5] = q_file_size >> 16;
header[6] = q_file_size >> 8;
header[7] = q_file_size;
// Next 4 bytes = number of tracks in this queue // Next 4 bytes = number of tracks in this queue
header[4] = total_size_ >> 24; header[4] = total_size_ >> 24;
@ -138,8 +141,8 @@ auto Playlist::serialiseCache() -> bool {
header[7] = total_size_; header[7] = total_size_;
UINT bytes_written = 0; UINT bytes_written = 0;
f_write(&file, header, 8, &bytes_written); f_write(&file, header, 12, &bytes_written);
if (bytes_written < 8) { if (bytes_written < 12) {
return false; return false;
} }
@ -182,22 +185,20 @@ auto Playlist::deserialiseCache() -> bool {
uint8_t header[8]; uint8_t header[8];
UINT bytes_read; UINT bytes_read;
f_read(&file, header, 8, &bytes_read); f_read(&file, header, 12, &bytes_read);
uint32_t file_size_check = header[0] << 24 | if (bytes_read != 12) {
header[1] << 16 | return false;
header[2] << 8 | }
header[3]; uint64_t file_size_check =
header[0] << 56 | header[1] << 48 | header[2] << 40 | header[3] << 32 |
// TODO: Handle this conversion safely header[4] << 24 | header[5] << 16 | header[6] << 8 | header[7];
if (file_size_check != (uint32_t)f_size(&file_)) {
ESP_LOGE("DANIEL", "Check didn't match. File size check: %lu, actual size: %llu", file_size_check, f_size(&file_)); if (file_size_check != f_size(&file_)) {
return false; return false;
} }
uint32_t size = header[4] << 24 | uint32_t size =
header[5] << 16 | header[4] << 24 | header[5] << 16 | header[6] << 8 | header[7];
header[6] << 8 |
header[7];
total_size_ = size; total_size_ = size;
// Read in the cache // Read in the cache
@ -208,13 +209,8 @@ auto Playlist::deserialiseCache() -> bool {
if (bytes_read == 0) { if (bytes_read == 0) {
break; break;
} }
uint64_t offset = buf[0] << 56 | uint64_t offset = buf[0] << 56 | buf[1] << 48 | buf[2] << 40 |
buf[1] << 48 | buf[3] << 32 | buf[4] << 24 | buf[5] << 16 | buf[6] << 8 |
buf[2] << 40 |
buf[3] << 32 |
buf[4] << 24 |
buf[5] << 16 |
buf[6] << 8 |
buf[7]; buf[7];
offset_cache_.push_back(offset); offset_cache_.push_back(offset);
} }

Loading…
Cancel
Save