Table printing changed. directory parsing corrected.
[fsck.sofs09.git] / fsck_helper.c
blobe8bfc7d445fd273515fd8892c5fdabeafd59e270
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 void printError (int errcode, char *cmd_name)
20 fprintf(stderr, "%s: error #%d: %s\n", cmd_name, errcode,\
21 strerror(errcode));
24 static const int NCOLS = 80;
26 /* Fetch current disc superblock.
27 * If the superblock was not read, it is read from disk.
28 * No changes to the superblock are monitored. It is assumed the superblock is
29 * used as read-only.
31 * argument sb is the address of a pointer to a SOSuperBlock
33 * The address to the superblock is placed in (*sb).
34 */
35 void fetch_superblock(SOSuperBlock **sb)
37 assert(&(*sb) != NULL);
39 static SOSuperBlock localsb;
40 static bool localsb_valid = 0;
41 int fret;
43 if (localsb_valid == false) {
44 fret = soReadRawBlock(0, &localsb);
45 if (fret < 0) FABORT(fret, "fetch_superblock");
46 localsb_valid = true;
49 (*sb) = &localsb;
52 /* Inode control table */
54 static ic_t *ictable;
55 static bool ictable_valid = false;
56 static uint32_t ictable_size = 0;
58 void ictable_free(void)
60 assert(ictable_valid == true);
62 free(ictable);
63 ictable_valid = false;
66 void ictable_create(void)
68 assert (ictable_valid == false);
70 int r;
71 SOSuperBlock *sb;
72 fetch_superblock(&sb);
74 ictable = malloc(sizeof(ic_t)*(sb->itotal));
75 if (ictable == NULL) FABORT(errno, "ictable_init");
77 ictable_size = sb->itotal;
78 for (r = 0; r < ictable_size; ++r) {
79 ictable[r] = bah;
82 ictable_valid = true;
85 void ictable_set(uint32_t index, ic_t value)
87 assert(ictable_valid == true);
88 assert(index < ictable_size);
90 ictable[index] = value;
93 void ictable_get(uint32_t index, ic_t *value)
95 assert(value != NULL);
96 assert(ictable_valid == true);
97 assert(index < ictable_size);
99 (*value) = ictable[index];
102 /* Cluster control table */
103 static cc_t *cctable;
104 static bool cctable_valid = false;
105 static uint32_t cctable_size = 0;
107 void cctable_free(void)
109 assert(cctable_valid == true);
111 free(cctable);
112 cctable_valid = false;
115 void cctable_create(void)
117 assert (cctable_valid == false);
119 int r;
120 SOSuperBlock *sb;
121 fetch_superblock(&sb);
123 cctable = malloc(sizeof(cc_t)*(sb->dzone_size));
124 if (cctable == NULL) FABORT(errno, "cctable_init");
126 cctable_size = sb->dzone_size;
127 for (r = 0; r < cctable_size; ++r) {
128 cctable[r] = bah;
131 cctable_valid = true;
134 void cctable_set(uint32_t index, cc_t value)
136 assert(cctable_valid == true);
137 assert(index < cctable_size);
139 cctable[index] = value;
142 void cctable_get(uint32_t index, cc_t *value)
144 assert(value != NULL);
145 assert(cctable_valid == true);
146 assert(index < cctable_size);
148 (*value) = cctable[index];
151 void cctable_print(void)
153 assert(cctable_valid == true);
155 int ncols;
156 int r;
157 cc_t ccstat;
159 ncols = NCOLS;
161 printf("\nCluster control table contents (\e[42mfree\e[m \e[44mbusy\e[m "
162 "\e[41munknown\e[m)\n");
163 for (r = 0; r < cctable_size; ++r) {
164 cctable_get(r, &ccstat);
165 switch (ccstat) {
166 case idle:
167 printf("\e[42m ");
168 break;
169 case busy:
170 printf("\e[44m ");
171 break;
172 case bah:
173 printf("\e[41m ");
174 break;
176 if (((r+1)%ncols) == 0) printf("\e[m\n");
178 printf("\e[m\n");
181 /* inode refcount table */
183 static uint32_t *irtable;
184 static bool irtable_valid = false;
185 static uint32_t irtable_size = 0;
187 void irtable_free(void)
189 assert(irtable_valid == true);
191 free(irtable);
192 irtable_valid = false;
195 void irtable_create(void)
197 assert (irtable_valid == false);
199 int r;
200 SOSuperBlock *sb;
201 fetch_superblock(&sb);
203 irtable = malloc(sizeof(uint32_t)*(sb->itotal));
204 if (irtable == NULL) FABORT(errno, "irtable_init");
206 irtable_size = sb->itotal;
207 for (r = 0; r < irtable_size; ++r) {
208 irtable[r] = 0;
211 irtable_valid = true;
214 void irtable_inc(uint32_t index)
216 assert(irtable_valid == true);
217 assert(index < irtable_size);
219 irtable[index]++;
222 void irtable_get(uint32_t index, uint32_t *value)
224 assert(value != NULL);
225 assert(irtable_valid == true);
226 assert(index < irtable_size);
228 (*value) = irtable[index];
231 void itable_print(void)
233 assert(ictable_valid == true);
235 int ncols;
236 int r;
237 ic_t icstat;
238 uint32_t refcount;
240 ncols = NCOLS;
242 printf("\nInode control table contents (\e[42mfree\e[m \e[44mbusy\e[m "
243 "\e[41munknown\e[m)\n");
244 for (r = 0; r < ictable_size; ++r) {
245 ictable_get(r, &icstat);
246 irtable_get(r, &refcount);
247 switch (icstat) {
248 case idle:
249 printf("\e[42m%d", refcount);
250 break;
251 case busy:
252 printf("\e[44m%d", refcount);
253 break;
254 case bah:
255 printf("\e[41m%d", refcount);
256 break;
257 default:
258 printf("\e[m\nBUM!\n");
260 if (((r+1)%ncols) == 0) printf("\e[m\n");
262 printf("\e[m\n");