FAT::FAT(Disk& disk) : disk(disk) { // Allocate the FAT entries entry = new int[disk.num_blocks()]; // Populate the FAT from Disk retrieve(); }
int RPC::dispatch(Disk& disk) { char buf[NDISK_BLOCK_SIZE]; char packet[NDISK_BLOCK_SIZE + 8]; size_t len = NDISK_BLOCK_SIZE + 8; char *msg = packet; if (disk.block_size() != NDISK_BLOCK_SIZE) return DISK_ERROR; // Read the packet (with 8-byte header) do { ssize_t nread = recv(sock, msg, len, 0); if (nread <= 0) return DISK_ERROR; len -= nread; msg += nread; } while (len > 0); // Get the command from the packet's first four bytes char command[5]; memcpy(command, packet, 4); command[4] = '\0'; // Get the block number parameter from the packet int block_num = ston(packet + 4); if (!strcmp(command, "NUMB")) { // Write num blocks into packet data ntos(packet + 8, disk.num_blocks()); } else if (!strcmp(command, "FRMT")) { disk.format(block_num); } else if (!strcmp(command, "READ")) { disk.read(block_num, buf); memcpy(packet + 8, buf, NDISK_BLOCK_SIZE); } else if (!strcmp(command, "WRTE")) { memcpy(buf, packet + 8, NDISK_BLOCK_SIZE); disk.write(block_num, buf); } else { message1("Unknown RPC command %s\n", command); } message2("RPC command %s(%d)\n", command, block_num); len = NDISK_BLOCK_SIZE + 8; msg = packet; // Write the packet (with 8-byte header) do { ssize_t nwritten = send(sock, msg, len, 0); if (nwritten < 0) return DISK_ERROR; len -= nwritten; msg += nwritten; } while (len > 0); return DISK_OK; }