test_sbitotal now fills ictable
[fsck.sofs09.git] / fsck_helper.c
blob30b37008b50c143c3f0b3732906cee6f89538c0d
1 /* This module implements functions that allow a controlled access to vital
2 * information of fsck.
4 * No function from this module returns if failure occurs.
5 * When something wrong occurs, the function aborts the program, immediately.
6 */
8 #include <stdio.h>
9 #include <string.h>
10 #include <errno.h>
11 #include <stdbool.h>
12 #include <stdlib.h>
13 #include <assert.h>
15 #include "fsck_helper.h"
16 #include "sofs09.h"
18 static void printError (int errcode, char *cmd_name)
20 fprintf(stderr, "%s: error #%d: %s\n", cmd_name, errcode,\
21 strerror(errcode));
23 #define FABORT(err, fname) \
25 printError(-(err), fname);\
26 exit(EXIT_FAILURE);\
29 /* Fetch current disc superblock.
30 * If the superblock was not read, it is read from disk.
31 * No changes to the superblock are monitored. It is assumed the superblock is
32 * used as read-only.
34 * argument sb is the address of a pointer to a SOSuperBlock
36 * The address to the superblock is placed in (*sb).
37 */
38 void fetch_superblock(SOSuperBlock **sb)
40 assert((*sb) != NULL);
42 static SOSuperBlock localsb;
43 static bool localsb_valid = 0;
44 int fret;
46 if (localsb_valid == false) {
47 fret = soReadRawBlock(0, &localsb);
48 if (fret < 0) FABORT(fret, "fetch_superblock");
49 localsb_valid = true;
52 (*sb) = &localsb;
55 /* Inode control table */
57 static ic_t *ictable;
58 static bool ictable_valid = false;
59 static uint32_t ictable_size = 0;
61 static void ictable_free(void)
63 assert(ictable_valid == true);
65 free(ictable);
66 ictable_valid = false;
69 static void ictable_create(void)
71 assert (ictable_valid == false);
73 int r;
74 SOSuperBlock *sb;
75 fetch_superblock(&sb);
77 ictable = malloc(sizeof(ic_t)*(sb->itotal));
78 if (ictable == NULL) FABORT(errno, "ictable_init");
80 ictable_size = sb->itotal;
81 for (r = 0; r < ictable_size; ++r) {
82 ictable[r] = bah;
85 ictable_valid = true;
87 atexit(ictable_free);
90 void ictable_set(uint32_t index, ic_t value)
92 if (ictable_valid == false) ictable_create();
94 assert(ictable_valid == true);
95 assert(index < ictable_size);
97 ictable[index] = value;
100 void ictable_get(uint32_t index, ic_t *value)
102 assert(value != NULL);
104 if (ictable_valid == false) ictable_create();
106 assert(ictable_valid == true);
107 assert(index < ictable_size);
109 (*value) = ictable[index];
112 /* Cluster control table */
113 static cc_t *cctable;
114 static bool cctable_valid = false;
115 static uint32_t cctable_size = 0;
117 static void cctable_free(void)
119 assert(cctable_valid == true);
121 free(cctable);
122 cctable_valid = false;
125 static void cctable_create(void)
127 assert (cctable_valid == false);
129 int r;
130 SOSuperBlock *sb;
131 fetch_superblock(&sb);
133 cctable = malloc(sizeof(cc_t)*(sb->dzone_size));
134 if (cctable == NULL) FABORT(errno, "cctable_init");
136 ictable_size = sb->dzone_size;
137 for (r = 0; r < cctable_size; ++r) {
138 cctable[r] = bah;
141 cctable_valid = true;
143 atexit(cctable_free);
146 void cctable_set(uint32_t index, cc_t value)
148 if (cctable_valid == false) cctable_create();
150 assert(cctable_valid == true);
151 assert(index < cctable_size);
153 cctable[index] = value;
156 void cctable_get(uint32_t index, cc_t *value)
158 assert(value != NULL);
160 if (cctable_valid == false) cctable_create();
162 assert(cctable_valid == true);
163 assert(index < cctable_size);
165 (*value) = cctable[index];