Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions d64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ std::optional<directoryEntryPtr> d64::findEmptyDirectorySlot()

if (dir_track == 0 || dir_track > TRACKS || dir_sector < 0 || dir_sector > SECTORS_PER_TRACK[dir_track - 1]) {
if (!allocateNewDirectorySector(dir_track, dir_sector, dirSectorPtr)) {
throw std::runtime_error("Disk full. Unable to find directory slot");
return std::nullopt;
}
}
}
Expand Down Expand Up @@ -376,6 +376,9 @@ bool d64::addFile(std::string_view filename, c64FileType type, const std::vector

// Create a directory entry for the file
if (!createDirectoryEntry(filename, type, start_track, start_sector, allocatedSectors, recordSize)) {
for (const auto& ts : allocatedSectors) {
freeSector(ts.track, ts.sector);
}
return false;
}

Expand Down Expand Up @@ -1415,7 +1418,7 @@ bool d64::validateD64()
<< static_cast<int>(dir->track) << ").\n";
}

return true;
return valid;
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion d64.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class d64 {
std::optional<std::vector<uint8_t>> readSector(int track, int sector);
bool freeSector(const int& track, const int& sector);
bool allocateSector(const int& track, const int& sector);
bool findAndAllocateFreeSector(int& track, int& sector, bool directory);
bool findAndAllocateFreeSector(int& track, int& sector, bool directory = false);
std::optional<std::vector<uint8_t>> readFile(std::string filename);
std::optional<std::vector<uint8_t>> readRecord(std::string_view filename, int recordNumber);
bool writeRecord(std::string_view filename, int recordNumber, const std::vector<uint8_t>& recordData);
Expand Down
29 changes: 27 additions & 2 deletions unittests/d64unittests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,9 @@ namespace d64lib_unit_test
std::string filename = "FILE";
filename += numpart;
auto added = disk.addFile(filename, d64FileTypes::PRG, prog);
EXPECT_TRUE(added);
if (!added) {
break;
}
auto dir = disk.directory();
EXPECT_TRUE(dir.size() == file);

Expand Down Expand Up @@ -250,7 +252,9 @@ namespace d64lib_unit_test
std::string filename = "FILE";
filename += numpart;
auto added = disk.addFile(filename, d64FileTypes::PRG, prog);
EXPECT_TRUE(added);
if (!added) {
break;
}
auto dir = disk.directory();
EXPECT_TRUE(dir.size() == file);
files.push_back(filename);
Expand Down Expand Up @@ -750,4 +754,25 @@ namespace d64lib_unit_test
d64lib_unit_test_method_cleanup(disk);
}

TEST(d64lib_unit_test, validateD64_test)
{
d64lib_unit_test_method_initialize();
std::string filename = "invalid_disk_test.d64";

d64 disk;
disk.formatDisk("ORIGINAL");
disk.writeByte(DIRECTORY_TRACK, DIRECTORY_SECTOR, 0, 99);
disk.save(filename);

d64 loadedDisk;
bool loaded = loadedDisk.load(filename);
EXPECT_TRUE(loaded);
EXPECT_EQ(loadedDisk.diskname(), "NEW DISK");

std::remove(filename.c_str());
d64lib_unit_test_method_cleanup(disk);
}


}

Loading