| 343 | } |
| 344 | |
| 345 | void insertFile(const std::string& filename) |
| 346 | { |
| 347 | auto leafname = getLeafname(filename); |
| 348 | if (leafname.size() > 8) |
| 349 | error("filename too long"); |
| 350 | std::cout << fmt::format("Inserting '{}'\n", leafname); |
| 351 | |
| 352 | std::ifstream inputFile(filename, std::ios::in | std::ios::binary); |
| 353 | if (!inputFile) |
| 354 | error("unable to open input file: {}", strerror(errno)); |
| 355 | |
| 356 | if (directory.find(leafname) != directory.end()) |
| 357 | error("duplicate filename: {}", leafname); |
| 358 | |
| 359 | auto dirent = std::make_unique<Dirent>(); |
| 360 | dirent->filename = leafname; |
| 361 | dirent->type = (leafname.find('*') != std::string::npos); |
| 362 | dirent->startSector = 0xffff; |
| 363 | dirent->sectorCount = 0; |
| 364 | |
| 365 | uint16_t lastSector = 0xffff; |
| 366 | uint16_t checksum = 0; |
| 367 | while (!inputFile.eof()) |
| 368 | { |
| 369 | uint8_t buffer[SECTOR_SIZE] = {}; |
| 370 | inputFile.read((char*)buffer, sizeof(buffer)); |
| 371 | for (int i = 0; i < inputFile.gcount(); i++) |
| 372 | checksum += buffer[i]; |
| 373 | if (inputFile.gcount() == 0) |
| 374 | break; |
| 375 | if (inputFile.bad()) |
| 376 | error("I/O error on read: {}", strerror(errno)); |
| 377 | |
| 378 | uint16_t thisSector = allocateSector(); |
| 379 | if (lastSector == 0xffff) |
| 380 | dirent->startSector = thisSector; |
| 381 | else |
| 382 | allocationTable[lastSector] = thisSector; |
| 383 | dirent->sectorCount++; |
| 384 | |
| 385 | file.seekp((thisSector - 1) * 0x100, std::ifstream::beg); |
| 386 | file.write((char*)buffer, sizeof(buffer)); |
| 387 | if (file.bad()) |
| 388 | error("I/O error on write: {}", strerror(errno)); |
| 389 | |
| 390 | lastSector = thisSector; |
| 391 | } |
| 392 | |
| 393 | if (leafname == "*boot") |
| 394 | { |
| 395 | std::cout << fmt::format( |
| 396 | "Writing boot sector with checksum 0x{:04x}\n", checksum); |
| 397 | writeBootSector(*dirent, checksum); |
| 398 | } |
| 399 | |
| 400 | directory[leafname] = std::move(dirent); |
| 401 | } |
| 402 |
no test coverage detected