3 /usr/src/ext2ed/file_com.c
5 A part of the extended file system 2 disk editor.
7 ----------------------------
8 Commands which handle a file
9 ----------------------------
11 First written on: April 18 1995
13 Copyright (C) 1995 Gadi Oxman
24 int init_file_info (void)
27 struct ext2_inode *ptr;
29 ptr=&type_data.u.t_ext2_inode;
31 file_info.inode_ptr=ptr;
32 file_info.inode_offset=device_offset;
34 file_info.global_block_num=ptr->i_block [0];
35 file_info.global_block_offset=ptr->i_block [0]*file_system_info.block_size;
36 file_info.block_num=0;
37 file_info.blocks_count=(ptr->i_size+file_system_info.block_size-1)/file_system_info.block_size;
38 file_info.file_offset=0;
39 file_info.file_length=ptr->i_size;
41 file_info.offset_in_block=0;
43 file_info.display=HEX;
45 low_read (file_info.buffer,file_system_info.block_size,file_info.global_block_offset);
51 void type_file___inode (char *command_line)
54 dispatch ("settype ext2_inode");
57 void type_file___show (char *command_line)
60 if (file_info.display==HEX)
62 if (file_info.display==TEXT)
66 void type_file___nextblock (char *command_line)
70 char *ptr,buffer [80];
72 ptr=parse_word (command_line,buffer);
75 ptr=parse_word (ptr,buffer);
76 block_offset*=atol (buffer);
79 if (file_info.block_num+block_offset >= file_info.blocks_count) {
80 wprintw (command_win,"Error - Block offset out of range\n");wrefresh (command_win);
84 file_info.block_num+=block_offset;
85 file_info.global_block_num=file_block_to_global_block (file_info.block_num,&file_info);
86 file_info.global_block_offset=file_info.global_block_num*file_system_info.block_size;
87 file_info.file_offset=file_info.block_num*file_system_info.block_size;
89 low_read (file_info.buffer,file_system_info.block_size,file_info.global_block_offset);
91 strcpy (buffer,"show");dispatch (buffer);
94 void type_file___next (char *command_line)
98 char *ptr,buffer [80];
100 ptr=parse_word (command_line,buffer);
103 ptr=parse_word (ptr,buffer);
104 offset*=atol (buffer);
107 if (file_info.offset_in_block+offset < file_system_info.block_size) {
108 file_info.offset_in_block+=offset;
109 sprintf (buffer,"show");dispatch (buffer);
113 wprintw (command_win,"Error - Offset out of block\n");refresh_command_win ();
117 void type_file___offset (char *command_line)
120 unsigned long offset;
121 char *ptr,buffer [80];
123 ptr=parse_word (command_line,buffer);
126 ptr=parse_word (ptr,buffer);
127 offset=atol (buffer);
130 wprintw (command_win,"Error - Argument not specified\n");refresh_command_win ();
134 if (offset < file_system_info.block_size) {
135 file_info.offset_in_block=offset;
136 sprintf (buffer,"show");dispatch (buffer);
140 wprintw (command_win,"Error - Offset out of block\n");refresh_command_win ();
144 void type_file___prev (char *command_line)
148 char *ptr,buffer [80];
150 ptr=parse_word (command_line,buffer);
153 ptr=parse_word (ptr,buffer);
154 offset*=atol (buffer);
157 if (file_info.offset_in_block-offset >= 0) {
158 file_info.offset_in_block-=offset;
159 sprintf (buffer,"show");dispatch (buffer);
163 wprintw (command_win,"Error - Offset out of block\n");refresh_command_win ();
167 void type_file___prevblock (char *command_line)
171 char *ptr,buffer [80];
173 ptr=parse_word (command_line,buffer);
176 ptr=parse_word (ptr,buffer);
177 block_offset*=atol (buffer);
180 if (file_info.block_num-block_offset < 0) {
181 wprintw (command_win,"Error - Block offset out of range\n");wrefresh (command_win);
185 file_info.block_num-=block_offset;
186 file_info.global_block_num=file_block_to_global_block (file_info.block_num,&file_info);
187 file_info.global_block_offset=file_info.global_block_num*file_system_info.block_size;
188 file_info.file_offset=file_info.block_num*file_system_info.block_size;
190 low_read (file_info.buffer,file_system_info.block_size,file_info.global_block_offset);
192 strcpy (buffer,"show");dispatch (buffer);
195 void type_file___block (char *command_line)
199 char *ptr,buffer [80];
201 ptr=parse_word (command_line,buffer);
204 wprintw (command_win,"Error - Invalid arguments\n");wrefresh (command_win);
208 ptr=parse_word (ptr,buffer);
209 block_offset=atol (buffer);
211 if (block_offset < 0 || block_offset >= file_info.blocks_count) {
212 wprintw (command_win,"Error - Block offset out of range\n");wrefresh (command_win);
216 file_info.block_num=block_offset;
217 file_info.global_block_num=file_block_to_global_block (file_info.block_num,&file_info);
218 file_info.global_block_offset=file_info.global_block_num*file_system_info.block_size;
219 file_info.file_offset=file_info.block_num*file_system_info.block_size;
221 low_read (file_info.buffer,file_system_info.block_size,file_info.global_block_offset);
223 strcpy (buffer,"show");dispatch (buffer);
226 void type_file___display (char *command_line)
229 char *ptr,buffer [80];
231 ptr=parse_word (command_line,buffer);
233 strcpy (buffer,"hex");
235 ptr=parse_word (ptr,buffer);
237 if (strcasecmp (buffer,"hex")==0) {
238 wprintw (command_win,"Display set to hex\n");wrefresh (command_win);
239 file_info.display=HEX;
240 sprintf (buffer,"show");dispatch (buffer);
243 else if (strcasecmp (buffer,"text")==0) {
244 wprintw (command_win,"Display set to text\n");wrefresh (command_win);
245 file_info.display=TEXT;
246 sprintf (buffer,"show");dispatch (buffer);
250 wprintw (command_win,"Error - Invalid arguments\n");wrefresh (command_win);
254 void file_show_hex (void)
258 unsigned char *ch_ptr;
260 /* device_offset and type_data points to the inode */
262 show_pad_info.line=0;
264 wmove (show_pad,0,0);
265 ch_ptr=file_info.buffer;
266 for (l=0;l<file_system_info.block_size/16;l++) {
267 if (file_info.file_offset+offset>file_info.file_length-1) break;
268 wprintw (show_pad,"%08ld : ",offset);
271 if (file_info.file_offset+offset+i>file_info.file_length-1) {
272 wprintw (show_pad," ");
276 if (file_info.offset_in_block==offset+i)
277 wattrset (show_pad,A_REVERSE);
279 if (ch_ptr [i]>=' ' && ch_ptr [i]<='z')
280 wprintw (show_pad,"%c",ch_ptr [i]);
282 wprintw (show_pad,".");
284 if (file_info.offset_in_block==offset+i)
285 wattrset (show_pad,A_NORMAL);
289 wprintw (show_pad," ");
291 if (file_info.file_offset+offset+i>file_info.file_length-1) break;
292 if (file_info.offset_in_block==offset+i)
293 wattrset (show_pad,A_REVERSE);
295 wprintw (show_pad,"%02x",ch_ptr [i]);
297 if (file_info.offset_in_block==offset+i) {
298 wattrset (show_pad,A_NORMAL);
299 show_pad_info.line=l-l % show_pad_info.display_lines;
302 wprintw (show_pad," ");
306 wprintw (show_pad,"\n");
311 show_pad_info.max_line=l-1;
318 void file_show_text (void)
321 long offset=0,last_offset,l=0,cols=0;
322 unsigned char *ch_ptr;
324 /* device_offset and type_data points to the inode */
326 show_pad_info.line=0;
327 wmove (show_pad,0,0);
328 ch_ptr=file_info.buffer;
330 last_offset=file_system_info.block_size-1;
332 if (file_info.file_offset+last_offset > file_info.file_length-1)
333 last_offset=file_info.file_length-1-file_info.file_offset;
335 while ( (offset <= last_offset) && l<SHOW_PAD_LINES) {
337 if (cols==SHOW_PAD_COLS-1) {
338 wprintw (show_pad,"\n");
343 if (file_info.offset_in_block==offset)
344 wattrset (show_pad,A_REVERSE);
346 if (*ch_ptr >= ' ' && *ch_ptr <= 'z')
347 wprintw (show_pad,"%c",*ch_ptr);
351 if (*ch_ptr == 0xa) {
352 wprintw (show_pad,"\n");
356 else if (*ch_ptr == 0x9)
357 wprintw (show_pad," ");
360 wprintw (show_pad,".");
363 if (file_info.offset_in_block==offset) {
364 wattrset (show_pad,A_NORMAL);
365 show_pad_info.line=l-l % show_pad_info.display_lines;
369 offset++;cols++;ch_ptr++;
372 wprintw (show_pad,"\n");
373 show_pad_info.max_line=l;
380 void show_status (void)
385 werase (show_win);wmove (show_win,0,0);
386 wprintw (show_win,"File contents. Block %ld. ",file_info.global_block_num);
387 wprintw (show_win,"File block %ld of %ld. ",file_info.block_num,file_info.blocks_count-1);
388 wprintw (show_win,"File Offset %ld of %ld.",file_info.file_offset,file_info.file_length-1);
390 wmove (show_win,1,0);
391 inode_num=inode_offset_to_inode_num (file_info.inode_offset);
392 wprintw (show_win,"File inode %ld. Indirection level %ld.",inode_num,file_info.level);
397 void type_file___remember (char *command_line)
402 char *ptr,buffer [80];
403 struct struct_descriptor *descriptor_ptr;
405 ptr=parse_word (command_line,buffer);
408 wprintw (command_win,"Error - Argument not specified\n");wrefresh (command_win);
412 ptr=parse_word (ptr,buffer);
414 entry_num=remember_lifo.entries_count++;
415 if (entry_num>REMEMBER_COUNT-1) {
417 remember_lifo.entries_count--;
420 descriptor_ptr=first_type;
421 while (descriptor_ptr!=NULL && !found) {
422 if (strcmp (descriptor_ptr->name,"ext2_inode")==0)
425 descriptor_ptr=descriptor_ptr->next;
429 remember_lifo.offset [entry_num]=device_offset;
430 remember_lifo.type [entry_num]=descriptor_ptr;
431 strcpy (remember_lifo.name [entry_num],buffer);
433 wprintw (command_win,"Object %s in Offset %ld remembered as %s\n",descriptor_ptr->name,device_offset,buffer);
434 wrefresh (command_win);
437 void type_file___set (char *command_line)
441 char *ptr,buffer [80],*ch_ptr;
444 ptr=parse_word (command_line,buffer);
446 wprintw (command_win,"Error - Argument not specified\n");refresh_command_win ();return;
449 ptr=parse_word (ptr,buffer);
451 if (strcasecmp (buffer,"text")==0) {
456 else if (strcasecmp (buffer,"hex")==0) {
458 ptr=parse_word (ptr,buffer);
462 wprintw (command_win,"Error - Data not specified\n");refresh_command_win ();return;
467 tmp=(unsigned char) strtol (buffer,NULL,16);
468 file_info.buffer [file_info.offset_in_block]=tmp;
469 file_info.offset_in_block++;
470 ptr=parse_word (ptr,buffer);
471 if (file_info.offset_in_block==file_system_info.block_size) {
473 wprintw (command_win,"Error - Ending offset outside block, only partial string changed\n");
474 refresh_command_win ();
476 file_info.offset_in_block--;
484 tmp=(unsigned char) *ch_ptr++;
485 file_info.buffer [file_info.offset_in_block]=tmp;
486 file_info.offset_in_block++;
487 if (file_info.offset_in_block==file_system_info.block_size) {
489 wprintw (command_win,"Error - Ending offset outside block, only partial string changed\n");
490 refresh_command_win ();
492 file_info.offset_in_block--;
497 strcpy (buffer,"show");dispatch (buffer);
500 void type_file___writedata (char *command_line)
503 low_write (file_info.buffer,file_system_info.block_size,file_info.global_block_offset);
507 long file_block_to_global_block (long file_block,struct struct_file_info *file_info_ptr)
510 long last_direct,last_indirect,last_dindirect;
512 last_direct=EXT2_NDIR_BLOCKS-1;
513 last_indirect=last_direct+file_system_info.block_size/4;
514 last_dindirect=last_indirect+(file_system_info.block_size/4)*(file_system_info.block_size/4);
516 if (file_block <= last_direct) {
517 file_info_ptr->level=0;
518 return (file_info_ptr->inode_ptr->i_block [file_block]);
521 if (file_block <= last_indirect) {
522 file_info_ptr->level=1;
523 file_block=file_block-last_direct-1;
524 return (return_indirect (file_info_ptr->inode_ptr->i_block [EXT2_IND_BLOCK],file_block));
527 if (file_block <= last_dindirect) {
528 file_info_ptr->level=2;
529 file_block=file_block-last_indirect-1;
530 return (return_dindirect (file_info_ptr->inode_ptr->i_block [EXT2_DIND_BLOCK],file_block));
533 file_info_ptr->level=3;
534 file_block=file_block-last_dindirect-1;
535 return (return_tindirect (file_info_ptr->inode_ptr->i_block [EXT2_TIND_BLOCK],file_block));
538 long return_indirect (long table_block,long block_num)
541 long block_table [EXT2_MAX_BLOCK_SIZE/4];
543 low_read ((char *) block_table,file_system_info.block_size,table_block*file_system_info.block_size);
544 return (block_table [block_num]);
547 long return_dindirect (long table_block,long block_num)
552 f_indirect=block_num/(file_system_info.block_size/4);
553 f_indirect=return_indirect (table_block,f_indirect);
554 return (return_indirect (f_indirect,block_num%(file_system_info.block_size/4)));
557 long return_tindirect (long table_block,long block_num)
562 s_indirect=block_num/((file_system_info.block_size/4)*(file_system_info.block_size/4));
563 s_indirect=return_indirect (table_block,s_indirect);
564 return (return_dindirect (s_indirect,block_num%((file_system_info.block_size/4)*(file_system_info.block_size/4))));