1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
|
/*
/usr/src/ext2ed/inode_com.c
A part of the extended file system 2 disk editor.
Commands relevant to ext2_inode type.
First written on: April 9 1995
Copyright (C) 1995 Gadi Oxman
*/
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "ext2ed.h"
void type_ext2_inode___prev (char *command_line)
{
char *ptr,buffer [80];
long group_num,group_offset,entry_num,block_num,first_entry,last_entry;
long inode_num,mult=1;
struct ext2_group_desc desc;
ptr=parse_word (command_line,buffer);
if (*ptr!=0) {
ptr=parse_word (ptr,buffer);
mult=atol (buffer);
}
block_num=device_offset/file_system_info.block_size;
group_num=inode_offset_to_group_num (device_offset);
group_offset=file_system_info.first_group_desc_offset+group_num*sizeof (struct ext2_group_desc);
low_read ((char *) &desc,sizeof (struct ext2_group_desc),group_offset);
entry_num=(device_offset-desc.bg_inode_table*file_system_info.block_size)/sizeof (struct ext2_inode);
first_entry=0;last_entry=file_system_info.super_block.s_inodes_per_group-1;
inode_num=0;
if (entry_num-mult+1>0) {
device_offset-=sizeof (struct ext2_inode)*mult;
entry_num-=mult;
sprintf (buffer,"setoffset %ld",device_offset);dispatch (buffer);
strcpy (buffer,"show");dispatch (buffer);
}
else {
wprintw (command_win,"Error - Entry out of limits\n");refresh_command_win ();
}
if (entry_num==0) {
wprintw (command_win,"Reached first inode in current group descriptor\n");
refresh_command_win ();
}
}
void type_ext2_inode___next (char *command_line)
{
char *ptr,buffer [80];
long group_num,group_offset,entry_num,block_num,first_entry,last_entry;
long inode_num,mult=1;
struct ext2_group_desc desc;
ptr=parse_word (command_line,buffer);
if (*ptr!=0) {
ptr=parse_word (ptr,buffer);
mult=atol (buffer);
}
block_num=device_offset/file_system_info.block_size;
group_num=inode_offset_to_group_num (device_offset);
group_offset=file_system_info.first_group_desc_offset+group_num*sizeof (struct ext2_group_desc);
low_read ((char *) &desc,sizeof (struct ext2_group_desc),group_offset);
entry_num=(device_offset-desc.bg_inode_table*file_system_info.block_size)/sizeof (struct ext2_inode);
first_entry=0;last_entry=file_system_info.super_block.s_inodes_per_group-1;
inode_num=0;
if (entry_num+mult-1<last_entry) {
device_offset+=sizeof (struct ext2_inode)*mult;
entry_num+=mult;
sprintf (buffer,"setoffset %ld",device_offset);dispatch (buffer);
strcpy (buffer,"show");dispatch (buffer);
}
else {
wprintw (command_win,"Error - Entry out of limits\n");refresh_command_win ();
}
if (entry_num==last_entry) {
wprintw (command_win,"Reached last inode in current group descriptor\n");
refresh_command_win ();
}
}
void type_ext2_inode___show (char *command_line)
{
struct ext2_inode *inode_ptr;
unsigned short temp;
int i;
long group_num,group_offset,entry_num,block_num,first_entry,last_entry,inode_num;
struct ext2_group_desc desc;
block_num=device_offset/file_system_info.block_size;
group_num=inode_offset_to_group_num (device_offset);
group_offset=file_system_info.first_group_desc_offset+group_num*sizeof (struct ext2_group_desc);
low_read ((char *) &desc,sizeof (struct ext2_group_desc),group_offset);
entry_num=(device_offset-desc.bg_inode_table*file_system_info.block_size)/sizeof (struct ext2_inode);
first_entry=0;last_entry=file_system_info.super_block.s_inodes_per_group-1;
inode_num=group_num*file_system_info.super_block.s_inodes_per_group+1;
inode_num+=entry_num;
inode_ptr=&type_data.u.t_ext2_inode;
show (command_line);
wmove (show_pad,0,40);wprintw (show_pad,"octal = %06o ",inode_ptr->i_mode);
for (i=6;i>=0;i-=3) {
temp=inode_ptr->i_mode & 0x1ff;
temp=temp >> i;
if (temp & 4)
wprintw (show_pad,"r");
else
wprintw (show_pad,"-");
if (temp & 2)
wprintw (show_pad,"w");
else
wprintw (show_pad,"-");
if (temp & 1)
wprintw (show_pad,"x");
else
wprintw (show_pad,"-");
}
wmove (show_pad,3,40);wprintw (show_pad,"%s",ctime ((time_t *) &type_data.u.t_ext2_inode.i_atime));
wmove (show_pad,4,40);wprintw (show_pad,"%s",ctime ((time_t *) &type_data.u.t_ext2_inode.i_ctime));
wmove (show_pad,5,40);wprintw (show_pad,"%s",ctime ((time_t *) &type_data.u.t_ext2_inode.i_mtime));
wmove (show_pad,6,40);wprintw (show_pad,"%s",ctime ((time_t *) &type_data.u.t_ext2_inode.i_dtime));
wmove (show_pad,10,40);
temp=inode_ptr->i_flags;
if (temp & EXT2_SECRM_FL)
wprintw (show_pad,"s");
else
wprintw (show_pad,"-");
if (temp & EXT2_UNRM_FL)
wprintw (show_pad,"u");
else
wprintw (show_pad,"-");
if (temp & EXT2_COMPR_FL)
wprintw (show_pad,"c");
else
wprintw (show_pad,"-");
if (temp & EXT2_SYNC_FL)
wprintw (show_pad,"S");
else
wprintw (show_pad,"-");
if (temp & EXT2_IMMUTABLE_FL)
wprintw (show_pad,"i");
else
wprintw (show_pad,"-");
if (temp & EXT2_APPEND_FL)
wprintw (show_pad,"a");
else
wprintw (show_pad,"-");
if (temp & EXT2_NODUMP_FL)
wprintw (show_pad,"d");
else
wprintw (show_pad,"-");
refresh_show_pad ();
wmove (show_win,1,0);
wprintw (show_win,"Inode %ld of %ld. Entry %ld of %ld in group descriptor %ld.\n"
,inode_num,file_system_info.super_block.s_inodes_count,entry_num,last_entry,group_num);
wprintw (show_win,"Inode type: ");
if (inode_num < EXT2_GOOD_OLD_FIRST_INO) {
switch (inode_num) {
case EXT2_BAD_INO:
wprintw (show_win,"Bad blocks inode - ");
break;
case EXT2_ROOT_INO:
wprintw (show_win,"Root inode - ");
break;
case EXT4_USR_QUOTA_INO:
wprintw (show_win,"User quota inode - ");
break;
case EXT4_GRP_QUOTA_INO:
wprintw (show_win,"Group quota inode - ");
break;
case EXT2_BOOT_LOADER_INO:
wprintw (show_win,"Boot loader inode - ");
break;
case EXT2_UNDEL_DIR_INO:
wprintw (show_win,"Undelete directory inode - ");
break;
default:
wprintw (show_win,"Reserved inode - ");
break;
}
}
if (type_data.u.t_ext2_inode.i_mode==0)
wprintw (show_win,"Free. ");
if (S_ISREG (type_data.u.t_ext2_inode.i_mode))
wprintw (show_win,"File. ");
if (S_ISDIR (type_data.u.t_ext2_inode.i_mode))
wprintw (show_win,"Directory. ");
if (S_ISLNK (type_data.u.t_ext2_inode.i_mode)) {
wprintw (show_win,"Symbolic link. ");
wmove (show_pad,12,40);
if (inode_ptr->i_size <= 60)
wprintw (show_pad,"-> %s",(char *) &type_data.u.t_ext2_inode.i_block [0]);
else
wprintw (show_pad,"Slow symbolic link\n");
refresh_show_pad ();
}
if (S_ISCHR (type_data.u.t_ext2_inode.i_mode))
wprintw (show_win,"Character device.");
if (S_ISBLK (type_data.u.t_ext2_inode.i_mode))
wprintw (show_win,"Block device. ");
wprintw (show_win,"\n");refresh_show_win ();
if (entry_num==last_entry) {
wprintw (command_win,"Reached last inode in current group descriptor\n");
refresh_command_win ();
}
if (entry_num==first_entry) {
wprintw (command_win,"Reached first inode in current group descriptor\n");
refresh_command_win ();
}
}
void type_ext2_inode___entry (char *command_line)
{
char *ptr,buffer [80];
long group_num,group_offset,entry_num,block_num,wanted_entry;
struct ext2_group_desc desc;
ptr=parse_word (command_line,buffer);
if (*ptr==0) return;
ptr=parse_word (ptr,buffer);
wanted_entry=atol (buffer);
block_num=device_offset/file_system_info.block_size;
group_num=inode_offset_to_group_num (device_offset);
group_offset=file_system_info.first_group_desc_offset+group_num*sizeof (struct ext2_group_desc);
low_read ((char *) &desc,sizeof (struct ext2_group_desc),group_offset);
entry_num=(device_offset-desc.bg_inode_table*file_system_info.block_size)/sizeof (struct ext2_inode);
if (wanted_entry > entry_num) {
sprintf (buffer,"next %ld",wanted_entry-entry_num);
dispatch (buffer);
}
else if (wanted_entry < entry_num) {
sprintf (buffer,"prev %ld",entry_num-wanted_entry);
dispatch (buffer);
}
}
void type_ext2_inode___group (char *command_line)
{
char buffer [80];
long group_num,group_offset;
group_num=inode_offset_to_group_num (device_offset);
group_offset=file_system_info.first_group_desc_offset+group_num*sizeof (struct ext2_group_desc);
sprintf (buffer,"setoffset %ld",group_offset);dispatch (buffer);
sprintf (buffer,"settype ext2_group_desc");dispatch (buffer);
}
void type_ext2_inode___file (char *command_line)
{
char buffer [80];
if (!S_ISREG (type_data.u.t_ext2_inode.i_mode)) {
wprintw (command_win,"Error - Inode type is not file\n");refresh_command_win ();
return;
}
if (!init_file_info ()) {
wprintw (command_win,"Error - Unable to show file\n");refresh_command_win ();
return;
}
sprintf (buffer,"settype file");dispatch (buffer);
}
void type_ext2_inode___dir (char *command_line)
{
char buffer [80];
if (!S_ISDIR (type_data.u.t_ext2_inode.i_mode)) {
wprintw (command_win,"Error - Inode type is not directory\n");refresh_command_win ();
return;
}
/* It is very important to init first_file_info first, as search_dir_entries relies on it */
if (!init_dir_info (&first_file_info)) {
wprintw (command_win,"Error - Unable to show directory\n");refresh_command_win ();
return;
}
file_info=first_file_info;
sprintf (buffer,"settype dir");dispatch (buffer);
}
long inode_offset_to_group_num (long inode_offset)
{
int found=0;
struct ext2_group_desc desc;
long block_num,group_offset,group_num;
block_num=inode_offset/file_system_info.block_size;
group_offset=file_system_info.first_group_desc_offset;
group_num=(group_offset-file_system_info.first_group_desc_offset)/sizeof (struct ext2_group_desc);
while (!found && group_num>=0 && group_num<file_system_info.groups_count) {
low_read ((char *) &desc,sizeof (struct ext2_group_desc),group_offset);
if (block_num>=desc.bg_inode_table && block_num<desc.bg_inode_table+file_system_info.blocks_per_group)
found=1;
else
group_offset+=sizeof (struct ext2_group_desc);
group_num=(group_offset-file_system_info.first_group_desc_offset)/sizeof (struct ext2_group_desc);
}
if (!found)
return (-1);
return (group_num);
}
long int inode_offset_to_inode_num (long inode_offset)
{
long group_num,group_offset,entry_num,block_num,first_entry,last_entry,inode_num;
struct ext2_group_desc desc;
block_num=inode_offset/file_system_info.block_size;
group_num=inode_offset_to_group_num (inode_offset);
group_offset=file_system_info.first_group_desc_offset+group_num*sizeof (struct ext2_group_desc);
low_read ((char *) &desc,sizeof (struct ext2_group_desc),group_offset);
entry_num=(inode_offset-desc.bg_inode_table*file_system_info.block_size)/sizeof (struct ext2_inode);
first_entry=0;last_entry=file_system_info.super_block.s_inodes_per_group-1;
inode_num=group_num*file_system_info.super_block.s_inodes_per_group+1;
inode_num+=entry_num;
return (inode_num);
}
long int inode_num_to_inode_offset (long inode_num)
{
long group_num,group_offset,inode_offset,inode_entry;
struct ext2_group_desc desc;
inode_num--;
group_num=inode_num/file_system_info.super_block.s_inodes_per_group;
inode_entry=inode_num%file_system_info.super_block.s_inodes_per_group;
group_offset=file_system_info.first_group_desc_offset+group_num*sizeof (struct ext2_group_desc);
low_read ((char *) &desc,sizeof (struct ext2_group_desc),group_offset);
inode_offset=desc.bg_inode_table*file_system_info.block_size+inode_entry*sizeof (struct ext2_inode);
return (inode_offset);
}
|