diff options
| author | 2026-11-07 07:26:34 +0000 | |
|---|---|---|
| committer | 2026-11-07 08:38:33 +0000 | |
| commit | 7fe23e4a8fa6fce19fb3d5e3a8a1f5472496c928 (patch) | |
| tree | 64c163a368fa600d6054796e05a4d8847ed1cd53 /src/raid-4-xor-10b-6d.c | |
| download | lab-master.tar.gz | |
Diffstat (limited to 'src/raid-4-xor-10b-6d.c')
| -rw-r--r-- | src/raid-4-xor-10b-6d.c | 279 |
1 files changed, 279 insertions, 0 deletions
diff --git a/src/raid-4-xor-10b-6d.c b/src/raid-4-xor-10b-6d.c new file mode 100644 index 0000000..621625a --- /dev/null +++ b/src/raid-4-xor-10b-6d.c | |||
| @@ -0,0 +1,279 @@ | |||
| 1 | // Darya | ||
| 2 | // M1M2 M3M4 M5M6 M7M8 M9MA P1P2 | ||
| 3 | #include <stdlib.h> | ||
| 4 | #include <stdio.h> | ||
| 5 | #include <stdint.h> | ||
| 6 | #include <stdbool.h> | ||
| 7 | #include <string.h> | ||
| 8 | #include <ctype.h> | ||
| 9 | |||
| 10 | #define DISK_COUNT 6 | ||
| 11 | #define MESSAGE_SIZE 10 | ||
| 12 | #define PARITY_SIZE 2 | ||
| 13 | #define BLOCK_SIZE 2 | ||
| 14 | |||
| 15 | #define DISK_SIZE 0x40 | ||
| 16 | |||
| 17 | #define POPCOUNT __builtin_popcount | ||
| 18 | #define CTZ __builtin_ctz | ||
| 19 | |||
| 20 | enum command { | ||
| 21 | COMMAND_NONE, | ||
| 22 | COMMAND_WRITE, | ||
| 23 | COMMAND_READ, | ||
| 24 | }; | ||
| 25 | |||
| 26 | FILE *disks[DISK_COUNT]; | ||
| 27 | |||
| 28 | #ifndef WIN32 | ||
| 29 | #define LINE_SIZE 5 | ||
| 30 | #else | ||
| 31 | #define LINE_SIZE 6 | ||
| 32 | static ssize_t | ||
| 33 | getline (char **restrict lineptr, size_t *restrict n, FILE *restrict stream) { | ||
| 34 | register char c; | ||
| 35 | register char *cs = NULL; | ||
| 36 | register int length = 0; | ||
| 37 | while ((c = getc (stream)) != EOF) { | ||
| 38 | cs = (char *) realloc (cs, ++length + 1); | ||
| 39 | if ((*(cs + length - 1) = c) == '\n') { | ||
| 40 | *(cs + length) = '\0'; | ||
| 41 | break; | ||
| 42 | } | ||
| 43 | } | ||
| 44 | |||
| 45 | // return the allocated memory if lineptr is null | ||
| 46 | if ((*lineptr) == NULL) { | ||
| 47 | *lineptr = cs; | ||
| 48 | } else { | ||
| 49 | // check if enough memory is allocated | ||
| 50 | if ((length + 1) < *n) | ||
| 51 | *lineptr = (char *) realloc (*lineptr, length + 1); | ||
| 52 | memcpy (*lineptr, cs, length); | ||
| 53 | free (cs); | ||
| 54 | } | ||
| 55 | |||
| 56 | return (ssize_t) (*n = strlen (*lineptr)); | ||
| 57 | } | ||
| 58 | #endif | ||
| 59 | |||
| 60 | static void | ||
| 61 | open_disks (uint8_t address) | ||
| 62 | { | ||
| 63 | uint16_t bad_disks = 0; | ||
| 64 | for (int i = 0; i < DISK_COUNT; ++i) { | ||
| 65 | char filename[] = "disk0"; | ||
| 66 | filename[strlen (filename) - 1] += i; | ||
| 67 | disks[i] = fopen (filename, "r+"); | ||
| 68 | if (!disks[i]) { // disk file was deleted | ||
| 69 | disks[i] = fopen (filename, "w+"); | ||
| 70 | if (!disks[i]) { | ||
| 71 | fprintf (stderr, "ERROR: the file could not be opened: %s\n", filename); | ||
| 72 | exit (EXIT_FAILURE); | ||
| 73 | } | ||
| 74 | |||
| 75 | fprintf (stderr, "WARNING: an inaccessible disk has been detected %s\n", filename); | ||
| 76 | bad_disks |= 1 << i; | ||
| 77 | } else { // disk file wasn't deleted | ||
| 78 | fseek (disks[i], 0, SEEK_END); | ||
| 79 | if (ftell (disks[i]) != DISK_SIZE*LINE_SIZE) { // disk file has bad format | ||
| 80 | fprintf (stderr, "WARNING: a corrupted disk has been detected: %s\n", filename); | ||
| 81 | bad_disks |= 1 << i; | ||
| 82 | } rewind (disks[i]); | ||
| 83 | } | ||
| 84 | } | ||
| 85 | |||
| 86 | // there were bad disks | ||
| 87 | int bad_disk_count = POPCOUNT (bad_disks); | ||
| 88 | if (bad_disk_count == 1) { // recompute lost disk | ||
| 89 | fprintf (stderr, "INFORMATION: there is only 1 inaccessible disk, a re-calculation is being performed.\n"); | ||
| 90 | int bad_disk = CTZ (bad_disks); | ||
| 91 | |||
| 92 | for (int j = 0; j < DISK_SIZE; ++j) { | ||
| 93 | uint8_t block_bytes[BLOCK_SIZE*DISK_COUNT]; | ||
| 94 | |||
| 95 | // read blocks from good disks | ||
| 96 | bool empty = false; | ||
| 97 | for (int k = 0; k < DISK_COUNT; ++k) { | ||
| 98 | if (k == bad_disk) continue; | ||
| 99 | int c = fgetc (disks[k]); | ||
| 100 | if (c == ' ') { | ||
| 101 | fgetc (disks[k]); | ||
| 102 | |||
| 103 | fgetc (disks[k]); | ||
| 104 | fgetc (disks[k]); | ||
| 105 | empty = true; | ||
| 106 | } else { | ||
| 107 | ungetc (c, disks[k]); | ||
| 108 | fscanf (disks[k], "%02hhX%02hhX", | ||
| 109 | &block_bytes[k*BLOCK_SIZE + 0], | ||
| 110 | &block_bytes[k*BLOCK_SIZE + 1]); | ||
| 111 | } fgetc (disks[k]); | ||
| 112 | } | ||
| 113 | |||
| 114 | if (empty) { // address empty | ||
| 115 | fprintf (disks[bad_disk], " \n"); | ||
| 116 | continue; | ||
| 117 | } | ||
| 118 | |||
| 119 | // compute lost block | ||
| 120 | block_bytes[bad_disk*BLOCK_SIZE + 0] = 0x00; | ||
| 121 | block_bytes[bad_disk*BLOCK_SIZE + 1] = 0x00; | ||
| 122 | for (int k = 0; k < DISK_COUNT; ++k) { | ||
| 123 | if (k == bad_disk) continue; | ||
| 124 | block_bytes[bad_disk*BLOCK_SIZE + 0] ^= block_bytes[k*2 + 0]; | ||
| 125 | block_bytes[bad_disk*BLOCK_SIZE + 1] ^= block_bytes[k*2 + 1]; | ||
| 126 | } | ||
| 127 | |||
| 128 | // write lost block | ||
| 129 | fprintf (disks[bad_disk], "%02hhX%02hhX\n", | ||
| 130 | block_bytes[bad_disk*BLOCK_SIZE + 0], | ||
| 131 | block_bytes[bad_disk*BLOCK_SIZE + 1]); | ||
| 132 | } | ||
| 133 | } else if (bad_disk_count > 1) { // replace all disks with empty ones, all data is lost | ||
| 134 | fprintf (stderr, "ERROR: there are too many inaccessible disks, the array is being initialized with new disks.\n"); | ||
| 135 | |||
| 136 | for (int i = 0; i < DISK_COUNT; ++i) { | ||
| 137 | for (int j = 0; j < DISK_SIZE; ++j) | ||
| 138 | fputs (" \n", disks[i]); | ||
| 139 | } | ||
| 140 | } | ||
| 141 | |||
| 142 | // position head for writing/reading message | ||
| 143 | for (int i = 0; i < DISK_COUNT; ++i) | ||
| 144 | fseek (disks[i], address*LINE_SIZE, SEEK_SET); | ||
| 145 | } | ||
| 146 | |||
| 147 | static void | ||
| 148 | close_disks (void) | ||
| 149 | { | ||
| 150 | for (int i = 0; i < DISK_COUNT; ++i) | ||
| 151 | fclose (disks[i]); | ||
| 152 | } | ||
| 153 | |||
| 154 | int | ||
| 155 | main (void) | ||
| 156 | { | ||
| 157 | fprintf (stderr, "INFORMATION: initialization of RAID 4 on 6 disks with messages of 10 bytes in length. \n"); | ||
| 158 | // check disk integrity on startup | ||
| 159 | open_disks (0); | ||
| 160 | close_disks ( ); | ||
| 161 | |||
| 162 | for (;;) { | ||
| 163 | char *line = NULL; | ||
| 164 | size_t line_size = 0; | ||
| 165 | const char *head; | ||
| 166 | int assigned; | ||
| 167 | int scanned; | ||
| 168 | |||
| 169 | // read whole line | ||
| 170 | printf ("> "); | ||
| 171 | scanned = getline (&line, &line_size, stdin); | ||
| 172 | if (scanned == -1) | ||
| 173 | exit (EXIT_SUCCESS); | ||
| 174 | else if (scanned == 1) | ||
| 175 | continue; | ||
| 176 | head = line; | ||
| 177 | |||
| 178 | uint8_t address; { // scan address | ||
| 179 | assigned = sscanf (head, "%hhx%n", &address, &scanned); | ||
| 180 | if (assigned != 1 || address >= DISK_SIZE) { | ||
| 181 | fprintf(stderr, "ERROR: the address is not in the range [0x00; 0x40)\n"); | ||
| 182 | continue; | ||
| 183 | } head += scanned; | ||
| 184 | } | ||
| 185 | |||
| 186 | enum command command; { // scan command | ||
| 187 | while (*head == ' ') head += 1; | ||
| 188 | if (!strncmp (head, "read", 4)) { | ||
| 189 | head += 4; | ||
| 190 | command = COMMAND_READ; | ||
| 191 | } else if (!strncmp (head, "write", 5)) { | ||
| 192 | head += 5; | ||
| 193 | command = COMMAND_WRITE; | ||
| 194 | } | ||
| 195 | if (!isspace(*head)) | ||
| 196 | command = COMMAND_NONE; | ||
| 197 | } | ||
| 198 | |||
| 199 | switch (command) { | ||
| 200 | case COMMAND_NONE: { | ||
| 201 | fprintf(stderr, "ERROR: invalid command, must be on of (read, write)\n"); | ||
| 202 | continue; | ||
| 203 | } break; | ||
| 204 | case COMMAND_READ: { | ||
| 205 | uint8_t message[MESSAGE_SIZE]; | ||
| 206 | uint8_t parity [ PARITY_SIZE]; | ||
| 207 | |||
| 208 | // parse out message | ||
| 209 | open_disks (address); | ||
| 210 | int c = fgetc (disks[0]); | ||
| 211 | if (c == ' ') { | ||
| 212 | close_disks (); | ||
| 213 | printf ("EMPTY!\n"); | ||
| 214 | break; | ||
| 215 | } ungetc (c, disks[0]); | ||
| 216 | fscanf (disks[0], "%02hhX%02hhX\n", &message[0x0], &message[0x1]); | ||
| 217 | fscanf (disks[1], "%02hhX%02hhX\n", &message[0x2], &message[0x3]); | ||
| 218 | fscanf (disks[2], "%02hhX%02hhX\n", &message[0x4], &message[0x5]); | ||
| 219 | fscanf (disks[3], "%02hhX%02hhX\n", &message[0x6], &message[0x7]); | ||
| 220 | fscanf (disks[4], "%02hhX%02hhX\n", &message[0x8], &message[0x9]); | ||
| 221 | fscanf (disks[5], "%02hhX%02hhX\n", &parity [0x0], &parity [0x1]); | ||
| 222 | close_disks (); | ||
| 223 | |||
| 224 | printf ("%02hhX%02hhX %02hhX%02hhX %02hhX%02hhX %02hhX%02hhX %02hhX%02hhX\n", | ||
| 225 | message[0x0], message[0x1], | ||
| 226 | message[0x2], message[0x3], | ||
| 227 | message[0x4], message[0x5], | ||
| 228 | message[0x6], message[0x7], | ||
| 229 | message[0x8], message[0x9]); | ||
| 230 | } break; | ||
| 231 | case COMMAND_WRITE: { | ||
| 232 | uint8_t message[MESSAGE_SIZE] = {0}; { // scan message | ||
| 233 | int message_size = 0; | ||
| 234 | |||
| 235 | while (*head == ' ' || | ||
| 236 | *head == '\t') ++head; | ||
| 237 | const char *invalid = NULL; | ||
| 238 | size_t len = 0; | ||
| 239 | for (const char *c = head; *c && *c != '\r' && *c != '\n'; ++c) { | ||
| 240 | if (!strchr ("0123456789abcdefABCDEF", *c)) { | ||
| 241 | invalid = c; | ||
| 242 | break; | ||
| 243 | } len += 1; | ||
| 244 | } if (invalid) { | ||
| 245 | fprintf (stderr, "ERROR: message contains invalid character `%c'\n", *invalid); | ||
| 246 | break; | ||
| 247 | } if (len != 2*MESSAGE_SIZE) { | ||
| 248 | fprintf (stderr, "ERROR: expected %d hex digits for message, received %zu\n", 2*MESSAGE_SIZE, len); | ||
| 249 | break; | ||
| 250 | } | ||
| 251 | |||
| 252 | while (sscanf (head, "%2hhx%n", &message[message_size], &scanned) == 1 && | ||
| 253 | message_size < MESSAGE_SIZE) { | ||
| 254 | head += scanned; | ||
| 255 | message_size += 1; | ||
| 256 | } | ||
| 257 | } | ||
| 258 | |||
| 259 | // compute parity | ||
| 260 | uint8_t parity[PARITY_SIZE] = { | ||
| 261 | message[0x0] ^ message[0x2] ^ message[0x4] ^ message[0x6] ^ message[0x8], | ||
| 262 | message[0x1] ^ message[0x3] ^ message[0x5] ^ message[0x7] ^ message[0x9], | ||
| 263 | }; | ||
| 264 | |||
| 265 | // write blocks | ||
| 266 | open_disks (address); // open disk files, ensuring their integrity | ||
| 267 | fprintf (disks[0], "%02hhX%02hhX\n", message[0x0], message[0x1]); | ||
| 268 | fprintf (disks[1], "%02hhX%02hhX\n", message[0x2], message[0x3]); | ||
| 269 | fprintf (disks[2], "%02hhX%02hhX\n", message[0x4], message[0x5]); | ||
| 270 | fprintf (disks[3], "%02hhX%02hhX\n", message[0x6], message[0x7]); | ||
| 271 | fprintf (disks[4], "%02hhX%02hhX\n", message[0x8], message[0x9]); | ||
| 272 | fprintf (disks[5], "%02hhX%02hhX\n", parity [0x0], parity [0x1]); | ||
| 273 | close_disks (); | ||
| 274 | } break; | ||
| 275 | } free (line); | ||
| 276 | } | ||
| 277 | |||
| 278 | return EXIT_SUCCESS; | ||
| 279 | } | ||
