xref: /aosp_15_r20/external/e2fsprogs/ext2ed/blockbitmap_com.c (revision 6a54128f25917bfc36a8a6e9d722c04a0b4641b6)
1*6a54128fSAndroid Build Coastguard Worker /*
2*6a54128fSAndroid Build Coastguard Worker 
3*6a54128fSAndroid Build Coastguard Worker /usr/src/ext2ed/blockbitmap_com.c
4*6a54128fSAndroid Build Coastguard Worker 
5*6a54128fSAndroid Build Coastguard Worker A part of the extended file system 2 disk editor.
6*6a54128fSAndroid Build Coastguard Worker 
7*6a54128fSAndroid Build Coastguard Worker -------------------------
8*6a54128fSAndroid Build Coastguard Worker Handles the block bitmap.
9*6a54128fSAndroid Build Coastguard Worker -------------------------
10*6a54128fSAndroid Build Coastguard Worker 
11*6a54128fSAndroid Build Coastguard Worker This file implements the commands which are specific to the blockbitmap type.
12*6a54128fSAndroid Build Coastguard Worker 
13*6a54128fSAndroid Build Coastguard Worker First written on: July 5 1995
14*6a54128fSAndroid Build Coastguard Worker 
15*6a54128fSAndroid Build Coastguard Worker Copyright (C) 1995 Gadi Oxman
16*6a54128fSAndroid Build Coastguard Worker 
17*6a54128fSAndroid Build Coastguard Worker */
18*6a54128fSAndroid Build Coastguard Worker 
19*6a54128fSAndroid Build Coastguard Worker #include "config.h"
20*6a54128fSAndroid Build Coastguard Worker #include <stdio.h>
21*6a54128fSAndroid Build Coastguard Worker #include <stdlib.h>
22*6a54128fSAndroid Build Coastguard Worker #include <string.h>
23*6a54128fSAndroid Build Coastguard Worker 
24*6a54128fSAndroid Build Coastguard Worker #include "ext2ed.h"
25*6a54128fSAndroid Build Coastguard Worker 
26*6a54128fSAndroid Build Coastguard Worker /*
27*6a54128fSAndroid Build Coastguard Worker 
28*6a54128fSAndroid Build Coastguard Worker The functions in this file use the flobal structure block_bitmap_info. This structure contains the current
29*6a54128fSAndroid Build Coastguard Worker position in the bitmap.
30*6a54128fSAndroid Build Coastguard Worker 
31*6a54128fSAndroid Build Coastguard Worker */
32*6a54128fSAndroid Build Coastguard Worker 
type_ext2_block_bitmap___entry(char * command_line)33*6a54128fSAndroid Build Coastguard Worker void type_ext2_block_bitmap___entry (char *command_line)
34*6a54128fSAndroid Build Coastguard Worker 
35*6a54128fSAndroid Build Coastguard Worker /*
36*6a54128fSAndroid Build Coastguard Worker 
37*6a54128fSAndroid Build Coastguard Worker This function changes the current entry in the bitmap. It just changes the entry_num variable in block_bitmap_info
38*6a54128fSAndroid Build Coastguard Worker and dispatches a show command to show the new entry.
39*6a54128fSAndroid Build Coastguard Worker 
40*6a54128fSAndroid Build Coastguard Worker */
41*6a54128fSAndroid Build Coastguard Worker 
42*6a54128fSAndroid Build Coastguard Worker {
43*6a54128fSAndroid Build Coastguard Worker 	unsigned long entry_num;
44*6a54128fSAndroid Build Coastguard Worker 	char *ptr,buffer [80];
45*6a54128fSAndroid Build Coastguard Worker 
46*6a54128fSAndroid Build Coastguard Worker 
47*6a54128fSAndroid Build Coastguard Worker 
48*6a54128fSAndroid Build Coastguard Worker 	ptr=parse_word (command_line,buffer);					/* Get the requested entry */
49*6a54128fSAndroid Build Coastguard Worker 	if (*ptr==0) {
50*6a54128fSAndroid Build Coastguard Worker 		wprintw (command_win,"Error - No argument specified\n");
51*6a54128fSAndroid Build Coastguard Worker 		refresh_command_win ();	return;
52*6a54128fSAndroid Build Coastguard Worker 	}
53*6a54128fSAndroid Build Coastguard Worker 	ptr=parse_word (ptr,buffer);
54*6a54128fSAndroid Build Coastguard Worker 
55*6a54128fSAndroid Build Coastguard Worker 	entry_num=atol (buffer);
56*6a54128fSAndroid Build Coastguard Worker 
57*6a54128fSAndroid Build Coastguard Worker 
58*6a54128fSAndroid Build Coastguard Worker 	if (entry_num >= file_system_info.super_block.s_blocks_per_group) {	/* Check if it is a valid entry number */
59*6a54128fSAndroid Build Coastguard Worker 
60*6a54128fSAndroid Build Coastguard Worker 		wprintw (command_win,"Error - Entry number out of bounds\n");
61*6a54128fSAndroid Build Coastguard Worker 		refresh_command_win ();return;
62*6a54128fSAndroid Build Coastguard Worker 	}
63*6a54128fSAndroid Build Coastguard Worker 
64*6a54128fSAndroid Build Coastguard Worker 
65*6a54128fSAndroid Build Coastguard Worker 
66*6a54128fSAndroid Build Coastguard Worker 	block_bitmap_info.entry_num=entry_num;					/* If it is, just change entry_num and */
67*6a54128fSAndroid Build Coastguard Worker 	strcpy (buffer,"show");dispatch (buffer);				/* dispatch a show command */
68*6a54128fSAndroid Build Coastguard Worker }
69*6a54128fSAndroid Build Coastguard Worker 
type_ext2_block_bitmap___next(char * command_line)70*6a54128fSAndroid Build Coastguard Worker void type_ext2_block_bitmap___next (char *command_line)
71*6a54128fSAndroid Build Coastguard Worker 
72*6a54128fSAndroid Build Coastguard Worker /*
73*6a54128fSAndroid Build Coastguard Worker 
74*6a54128fSAndroid Build Coastguard Worker This function passes to the next entry in the bitmap. We just call the above entry command.
75*6a54128fSAndroid Build Coastguard Worker 
76*6a54128fSAndroid Build Coastguard Worker */
77*6a54128fSAndroid Build Coastguard Worker 
78*6a54128fSAndroid Build Coastguard Worker {
79*6a54128fSAndroid Build Coastguard Worker 	long entry_offset=1;
80*6a54128fSAndroid Build Coastguard Worker 	char *ptr,buffer [80];
81*6a54128fSAndroid Build Coastguard Worker 
82*6a54128fSAndroid Build Coastguard Worker 	ptr=parse_word (command_line,buffer);
83*6a54128fSAndroid Build Coastguard Worker 	if (*ptr!=0) {
84*6a54128fSAndroid Build Coastguard Worker 		ptr=parse_word (ptr,buffer);
85*6a54128fSAndroid Build Coastguard Worker 		entry_offset=atol (buffer);
86*6a54128fSAndroid Build Coastguard Worker 	}
87*6a54128fSAndroid Build Coastguard Worker 
88*6a54128fSAndroid Build Coastguard Worker 	sprintf (buffer,"entry %ld",block_bitmap_info.entry_num+entry_offset);
89*6a54128fSAndroid Build Coastguard Worker 	dispatch (buffer);
90*6a54128fSAndroid Build Coastguard Worker }
91*6a54128fSAndroid Build Coastguard Worker 
type_ext2_block_bitmap___prev(char * command_line)92*6a54128fSAndroid Build Coastguard Worker void type_ext2_block_bitmap___prev (char *command_line)
93*6a54128fSAndroid Build Coastguard Worker 
94*6a54128fSAndroid Build Coastguard Worker {
95*6a54128fSAndroid Build Coastguard Worker 	long entry_offset=1;
96*6a54128fSAndroid Build Coastguard Worker 	char *ptr,buffer [80];
97*6a54128fSAndroid Build Coastguard Worker 
98*6a54128fSAndroid Build Coastguard Worker 	ptr=parse_word (command_line,buffer);
99*6a54128fSAndroid Build Coastguard Worker 	if (*ptr!=0) {
100*6a54128fSAndroid Build Coastguard Worker 		ptr=parse_word (ptr,buffer);
101*6a54128fSAndroid Build Coastguard Worker 		entry_offset=atol (buffer);
102*6a54128fSAndroid Build Coastguard Worker 	}
103*6a54128fSAndroid Build Coastguard Worker 
104*6a54128fSAndroid Build Coastguard Worker 	sprintf (buffer,"entry %ld",block_bitmap_info.entry_num-entry_offset);
105*6a54128fSAndroid Build Coastguard Worker 	dispatch (buffer);
106*6a54128fSAndroid Build Coastguard Worker }
107*6a54128fSAndroid Build Coastguard Worker 
type_ext2_block_bitmap___allocate(char * command_line)108*6a54128fSAndroid Build Coastguard Worker void type_ext2_block_bitmap___allocate (char *command_line)
109*6a54128fSAndroid Build Coastguard Worker 
110*6a54128fSAndroid Build Coastguard Worker /*
111*6a54128fSAndroid Build Coastguard Worker 
112*6a54128fSAndroid Build Coastguard Worker This function starts allocating block from the current position. Allocating involves setting the correct bits
113*6a54128fSAndroid Build Coastguard Worker in the bitmap. This function is a vector version of allocate_block below - We just run on the blocks that
114*6a54128fSAndroid Build Coastguard Worker we need to allocate, and call allocate_block for each one.
115*6a54128fSAndroid Build Coastguard Worker 
116*6a54128fSAndroid Build Coastguard Worker */
117*6a54128fSAndroid Build Coastguard Worker 
118*6a54128fSAndroid Build Coastguard Worker {
119*6a54128fSAndroid Build Coastguard Worker 	long entry_num,num=1;
120*6a54128fSAndroid Build Coastguard Worker 	char *ptr,buffer [80];
121*6a54128fSAndroid Build Coastguard Worker 
122*6a54128fSAndroid Build Coastguard Worker 	ptr=parse_word (command_line,buffer);					/* Get the number of blocks to allocate */
123*6a54128fSAndroid Build Coastguard Worker 	if (*ptr!=0) {
124*6a54128fSAndroid Build Coastguard Worker 		ptr=parse_word (ptr,buffer);
125*6a54128fSAndroid Build Coastguard Worker 		num=atol (buffer);
126*6a54128fSAndroid Build Coastguard Worker 	}
127*6a54128fSAndroid Build Coastguard Worker 
128*6a54128fSAndroid Build Coastguard Worker 	entry_num=block_bitmap_info.entry_num;
129*6a54128fSAndroid Build Coastguard Worker 										/* Check for limits */
130*6a54128fSAndroid Build Coastguard Worker 	if (num > file_system_info.super_block.s_blocks_per_group-entry_num) {
131*6a54128fSAndroid Build Coastguard Worker 		wprintw (command_win,"Error - There aren't that much blocks in the group\n");
132*6a54128fSAndroid Build Coastguard Worker 		refresh_command_win ();return;
133*6a54128fSAndroid Build Coastguard Worker 	}
134*6a54128fSAndroid Build Coastguard Worker 
135*6a54128fSAndroid Build Coastguard Worker 	while (num) {								/* And call allocate_block */
136*6a54128fSAndroid Build Coastguard Worker 		allocate_block (entry_num);					/* for each block */
137*6a54128fSAndroid Build Coastguard Worker 		num--;entry_num++;
138*6a54128fSAndroid Build Coastguard Worker 	}
139*6a54128fSAndroid Build Coastguard Worker 
140*6a54128fSAndroid Build Coastguard Worker 	dispatch ("show");							/* Show the result */
141*6a54128fSAndroid Build Coastguard Worker }
142*6a54128fSAndroid Build Coastguard Worker 
type_ext2_block_bitmap___deallocate(char * command_line)143*6a54128fSAndroid Build Coastguard Worker void type_ext2_block_bitmap___deallocate (char *command_line)
144*6a54128fSAndroid Build Coastguard Worker 
145*6a54128fSAndroid Build Coastguard Worker /* This is the opposite of the above function - We call deallocate_block instead of allocate_block */
146*6a54128fSAndroid Build Coastguard Worker 
147*6a54128fSAndroid Build Coastguard Worker {
148*6a54128fSAndroid Build Coastguard Worker 	long entry_num,num=1;
149*6a54128fSAndroid Build Coastguard Worker 	char *ptr,buffer [80];
150*6a54128fSAndroid Build Coastguard Worker 
151*6a54128fSAndroid Build Coastguard Worker 	ptr=parse_word (command_line,buffer);
152*6a54128fSAndroid Build Coastguard Worker 	if (*ptr!=0) {
153*6a54128fSAndroid Build Coastguard Worker 		ptr=parse_word (ptr,buffer);
154*6a54128fSAndroid Build Coastguard Worker 		num=atol (buffer);
155*6a54128fSAndroid Build Coastguard Worker 	}
156*6a54128fSAndroid Build Coastguard Worker 
157*6a54128fSAndroid Build Coastguard Worker 	entry_num=block_bitmap_info.entry_num;
158*6a54128fSAndroid Build Coastguard Worker 	if (num > file_system_info.super_block.s_blocks_per_group-entry_num) {
159*6a54128fSAndroid Build Coastguard Worker 		wprintw (command_win,"Error - There aren't that much blocks in the group\n");
160*6a54128fSAndroid Build Coastguard Worker 		refresh_command_win ();return;
161*6a54128fSAndroid Build Coastguard Worker 	}
162*6a54128fSAndroid Build Coastguard Worker 
163*6a54128fSAndroid Build Coastguard Worker 	while (num) {
164*6a54128fSAndroid Build Coastguard Worker 		deallocate_block (entry_num);
165*6a54128fSAndroid Build Coastguard Worker 		num--;entry_num++;
166*6a54128fSAndroid Build Coastguard Worker 	}
167*6a54128fSAndroid Build Coastguard Worker 
168*6a54128fSAndroid Build Coastguard Worker 	dispatch ("show");
169*6a54128fSAndroid Build Coastguard Worker }
170*6a54128fSAndroid Build Coastguard Worker 
171*6a54128fSAndroid Build Coastguard Worker 
allocate_block(long entry_num)172*6a54128fSAndroid Build Coastguard Worker void allocate_block (long entry_num)
173*6a54128fSAndroid Build Coastguard Worker 
174*6a54128fSAndroid Build Coastguard Worker /* In this function we convert the bit number into the right byte and inner bit positions. */
175*6a54128fSAndroid Build Coastguard Worker 
176*6a54128fSAndroid Build Coastguard Worker {
177*6a54128fSAndroid Build Coastguard Worker 	unsigned char bit_mask=1;
178*6a54128fSAndroid Build Coastguard Worker 	int byte_offset,j;
179*6a54128fSAndroid Build Coastguard Worker 
180*6a54128fSAndroid Build Coastguard Worker 	byte_offset=entry_num/8;					/* Find the correct byte - entry_num/8 */
181*6a54128fSAndroid Build Coastguard Worker 									/* The position inside the byte is entry_num %8 */
182*6a54128fSAndroid Build Coastguard Worker 	for (j=0;j<entry_num%8;j++)
183*6a54128fSAndroid Build Coastguard Worker 		bit_mask*=2;						/* Generate the or mask - 1 at the right place */
184*6a54128fSAndroid Build Coastguard Worker 	type_data.u.buffer [byte_offset] |= bit_mask;			/* And apply it */
185*6a54128fSAndroid Build Coastguard Worker }
186*6a54128fSAndroid Build Coastguard Worker 
deallocate_block(long entry_num)187*6a54128fSAndroid Build Coastguard Worker void deallocate_block (long entry_num)
188*6a54128fSAndroid Build Coastguard Worker 
189*6a54128fSAndroid Build Coastguard Worker /* This is the opposite of allocate_block above. We use an and mask instead of an or mask. */
190*6a54128fSAndroid Build Coastguard Worker 
191*6a54128fSAndroid Build Coastguard Worker {
192*6a54128fSAndroid Build Coastguard Worker 	unsigned char bit_mask=1;
193*6a54128fSAndroid Build Coastguard Worker 	int byte_offset,j;
194*6a54128fSAndroid Build Coastguard Worker 
195*6a54128fSAndroid Build Coastguard Worker 	byte_offset=entry_num/8;
196*6a54128fSAndroid Build Coastguard Worker 	for (j=0;j<entry_num%8;j++)
197*6a54128fSAndroid Build Coastguard Worker 		bit_mask*=2;
198*6a54128fSAndroid Build Coastguard Worker 	bit_mask^=0xff;
199*6a54128fSAndroid Build Coastguard Worker 
200*6a54128fSAndroid Build Coastguard Worker 	type_data.u.buffer [byte_offset] &= bit_mask;
201*6a54128fSAndroid Build Coastguard Worker }
202*6a54128fSAndroid Build Coastguard Worker 
type_ext2_block_bitmap___show(char * command_line)203*6a54128fSAndroid Build Coastguard Worker void type_ext2_block_bitmap___show (char *command_line)
204*6a54128fSAndroid Build Coastguard Worker 
205*6a54128fSAndroid Build Coastguard Worker /*
206*6a54128fSAndroid Build Coastguard Worker 
207*6a54128fSAndroid Build Coastguard Worker We show the bitmap as a series of bits, grouped at 8-bit intervals. We display 8 such groups on each line.
208*6a54128fSAndroid Build Coastguard Worker The current position (as known from block_bitmap_info.entry_num) is highlighted.
209*6a54128fSAndroid Build Coastguard Worker 
210*6a54128fSAndroid Build Coastguard Worker */
211*6a54128fSAndroid Build Coastguard Worker 
212*6a54128fSAndroid Build Coastguard Worker {
213*6a54128fSAndroid Build Coastguard Worker 	int i,j;
214*6a54128fSAndroid Build Coastguard Worker 	unsigned char *ptr;
215*6a54128fSAndroid Build Coastguard Worker 	unsigned long block_num,entry_num;
216*6a54128fSAndroid Build Coastguard Worker 
217*6a54128fSAndroid Build Coastguard Worker 	ptr=type_data.u.buffer;
218*6a54128fSAndroid Build Coastguard Worker 	show_pad_info.line=0;show_pad_info.max_line=-1;
219*6a54128fSAndroid Build Coastguard Worker 
220*6a54128fSAndroid Build Coastguard Worker 	wmove (show_pad,0,0);
221*6a54128fSAndroid Build Coastguard Worker 	for (i=0,entry_num=0;i<file_system_info.super_block.s_blocks_per_group/8;i++,ptr++) {
222*6a54128fSAndroid Build Coastguard Worker 		for (j=1;j<=128;j*=2) {						/* j contains the and bit mask */
223*6a54128fSAndroid Build Coastguard Worker 			if (entry_num==block_bitmap_info.entry_num) {		/* Highlight the current entry */
224*6a54128fSAndroid Build Coastguard Worker 				wattrset (show_pad,A_REVERSE);
225*6a54128fSAndroid Build Coastguard Worker 				show_pad_info.line=show_pad_info.max_line-show_pad_info.display_lines/2;
226*6a54128fSAndroid Build Coastguard Worker 			}
227*6a54128fSAndroid Build Coastguard Worker 
228*6a54128fSAndroid Build Coastguard Worker 			if ((*ptr) & j)						/* Apply the mask */
229*6a54128fSAndroid Build Coastguard Worker 				wprintw (show_pad,"1");
230*6a54128fSAndroid Build Coastguard Worker 			else
231*6a54128fSAndroid Build Coastguard Worker 				wprintw (show_pad,"0");
232*6a54128fSAndroid Build Coastguard Worker 
233*6a54128fSAndroid Build Coastguard Worker 			if (entry_num==block_bitmap_info.entry_num)
234*6a54128fSAndroid Build Coastguard Worker 				wattrset (show_pad,A_NORMAL);
235*6a54128fSAndroid Build Coastguard Worker 
236*6a54128fSAndroid Build Coastguard Worker 			entry_num++;						/* Pass to the next entry */
237*6a54128fSAndroid Build Coastguard Worker 		}
238*6a54128fSAndroid Build Coastguard Worker 		wprintw (show_pad," ");
239*6a54128fSAndroid Build Coastguard Worker 		if (i%8==7) {							/* Display 8 groups in a row */
240*6a54128fSAndroid Build Coastguard Worker 			wprintw (show_pad,"\n");
241*6a54128fSAndroid Build Coastguard Worker 			show_pad_info.max_line++;
242*6a54128fSAndroid Build Coastguard Worker 		}
243*6a54128fSAndroid Build Coastguard Worker 	}
244*6a54128fSAndroid Build Coastguard Worker 
245*6a54128fSAndroid Build Coastguard Worker 	refresh_show_pad ();
246*6a54128fSAndroid Build Coastguard Worker 	show_info ();								/* Show the usual information */
247*6a54128fSAndroid Build Coastguard Worker 
248*6a54128fSAndroid Build Coastguard Worker 										/* Show the group number */
249*6a54128fSAndroid Build Coastguard Worker 	wmove (show_win,1,0);
250*6a54128fSAndroid Build Coastguard Worker 	wprintw (show_win,"Block bitmap of block group %ld\n",block_bitmap_info.group_num);
251*6a54128fSAndroid Build Coastguard Worker 										/* Show the block number */
252*6a54128fSAndroid Build Coastguard Worker 
253*6a54128fSAndroid Build Coastguard Worker 	block_num=block_bitmap_info.entry_num+block_bitmap_info.group_num*file_system_info.super_block.s_blocks_per_group;
254*6a54128fSAndroid Build Coastguard Worker 	block_num+=file_system_info.super_block.s_first_data_block;
255*6a54128fSAndroid Build Coastguard Worker 
256*6a54128fSAndroid Build Coastguard Worker 	wprintw (show_win,"Status of block %ld - ",block_num);			/* and the allocation status */
257*6a54128fSAndroid Build Coastguard Worker 	ptr=type_data.u.buffer+block_bitmap_info.entry_num/8;
258*6a54128fSAndroid Build Coastguard Worker 	j=1;
259*6a54128fSAndroid Build Coastguard Worker 	for (i=block_bitmap_info.entry_num % 8;i>0;i--)
260*6a54128fSAndroid Build Coastguard Worker 		j*=2;
261*6a54128fSAndroid Build Coastguard Worker 	if ((*ptr) & j)
262*6a54128fSAndroid Build Coastguard Worker 		wprintw (show_win,"Allocated\n");
263*6a54128fSAndroid Build Coastguard Worker 	else
264*6a54128fSAndroid Build Coastguard Worker 		wprintw (show_win,"Free\n");
265*6a54128fSAndroid Build Coastguard Worker 	refresh_show_win ();
266*6a54128fSAndroid Build Coastguard Worker }
267