-
Notifications
You must be signed in to change notification settings - Fork 6
/
ext-shell.c
346 lines (274 loc) · 8.18 KB
/
ext-shell.c
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
/* =============
* ext shell
* AUTHOR : CVS
* =============
*/
#include <stdio.h>
#include <assert.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include "inc/types.h"
#include "inc/superblock.h"
#include "inc/blockgroup_descriptor.h"
#include "inc/inode.h"
#include "inc/directoryentry.h"
#define DEBUG 0
#define debug(...) \
do { if (DEBUG) printf("<debug> " __VA_ARGS__); } while (0)
struct os_superblock_t *superblock;
struct os_blockgroup_descriptor_t *blockgroup;
struct os_inode_t *inodes;
void read_superblock(int fd)
{
superblock = malloc(sizeof(struct os_superblock_t));
assert(superblock != NULL);
assert(lseek(fd, (off_t)1024, SEEK_SET) == (off_t)1024);
assert(read(fd, (void *)superblock, sizeof(struct os_superblock_t)) == sizeof(struct os_superblock_t));
}
void read_blockgroup(int fd)
{
blockgroup = malloc(sizeof(struct os_blockgroup_descriptor_t));
assert(blockgroup != NULL);
assert(lseek(fd, (off_t)2048, SEEK_SET) == (off_t)2048);
assert(read(fd, (void *)blockgroup, sizeof(struct os_blockgroup_descriptor_t)) == sizeof(struct os_blockgroup_descriptor_t));
}
void read_inodeTable(int fd)
{
#if 0
int i;
#endif
// preparing to cache inode table in inodes
inodes = (struct os_inode_t*)malloc(superblock->s_inodes_count*superblock->s_inode_size);
assert(inodes != NULL);
// seek to start of inode_table
assert(lseek(fd, (off_t)(blockgroup->bg_inode_table*1024), SEEK_SET) == (off_t)(blockgroup->bg_inode_table*1024));
#if 0
// read-in every inode into cache
for(i=0; i<superblock->s_inodes_count;i++) {
printf("%lld\n", lseek(fd, (off_t)superblock->s_inode_size , SEEK_CUR));
assert(read(fd, (void *)&inodes[i], sizeof(struct os_inode_t)) == sizeof(struct os_inode_t));
//assert(lseek(fd, (off_t)superblock->s_inode_size , SEEK_CUR) == (off_t)superblock->s_inode_size);
}
#else
assert(read(fd, (void *)inodes, 0x40000) == 0x40000);
#endif
}
void printInodeType(int inode_type)
{
switch(inode_type)
{
case 1:
printf("-");
break;
case 2:
printf("d");
break;
case 3:
printf("c");
break;
case 4:
printf("b");
break;
case 5:
printf("B");
break;
case 6:
printf("S");
break;
case 7:
printf("l");
break;
default:
printf("X");
break;
}
}
void printInodePerm(int fd, int inode_num)
{
//int curr_pos = lseek(fd, 0, SEEK_CUR);
short int mode = inodes[inode_num-1].i_mode;
mode & EXT2_S_IRUSR ? printf("r") : printf("-");
mode & EXT2_S_IWUSR ? printf("w") : printf("-");
mode & EXT2_S_IXUSR ? printf("x") : printf("-");
mode & EXT2_S_IRGRP ? printf("r") : printf("-");
mode & EXT2_S_IWGRP ? printf("w") : printf("-");
mode & EXT2_S_IXGRP ? printf("x") : printf("-");
mode & EXT2_S_IROTH ? printf("r") : printf("-");
mode & EXT2_S_IWOTH ? printf("w") : printf("-");
mode & EXT2_S_IXOTH ? printf("x") : printf("-");
printf("\t");
//lseek(fd, curr_pos, SEEK_SET);
}
/* findInodeByName
*
* Params:
* int fd fd to img file
* char* filename name of file to find
* int filetype filetype os_direntry_t->file_type
*
* Returns:
* int valid inode-num if found, else -1.
*/
int findInodeByName(int fd, int base_inode_num, char* filename, int filetype)
{
char* name;
int curr_inode_num;
int curr_inode_type;
debug("data block addr\t= 0x%x\n", inodes[base_inode_num-1].i_block[0]);
struct os_direntry_t* dirEntry = malloc(sizeof(struct os_direntry_t));
assert (dirEntry != NULL);
assert(lseek(fd, (off_t)(inodes[base_inode_num-1].i_block[0]*1024), SEEK_SET) == (off_t)(inodes[base_inode_num-1].i_block[0]*1024));
assert(read(fd, (void *)dirEntry, sizeof(struct os_direntry_t)) == sizeof(struct os_direntry_t));
while (dirEntry->inode) {
name = (char*)malloc(dirEntry->name_len+1);
memcpy(name, dirEntry->file_name, dirEntry->name_len);
name[dirEntry->name_len+1] = '\0';
curr_inode_num = dirEntry->inode;
curr_inode_type = dirEntry->file_type;
if (filetype == curr_inode_type) {
if(!strcmp(name, filename)) {
return(curr_inode_num);
}
}
lseek(fd, (dirEntry->rec_len - sizeof(struct os_direntry_t)), SEEK_CUR);
assert(read(fd, (void *)dirEntry, sizeof(struct os_direntry_t)) == sizeof(struct os_direntry_t));
}
return(-1);
}
void saveInode(int fd, int inode_num, char* filename)
{
int wfd = open(filename, O_RDWR | O_CREAT);
if (wfd == -1) {
printf("Could NOT open file \"%s\"\n", filename);
}
char* buffer = malloc(1024);
assert (buffer != NULL);
assert(lseek(fd, (off_t)(inodes[inode_num-1].i_block[0]*1024), SEEK_SET) == (off_t)(inodes[inode_num-1].i_block[0]*1024));
assert(read(fd, (void *)buffer, 1024) == 1024);
write(wfd, buffer, inodes[inode_num-1].i_size);
close(wfd);
}
void ls(int fd, int base_inode_num)
{
char* name;
int curr_inode_num;
int curr_inode_type;
debug("data block addr\t= 0x%x\n", inodes[base_inode_num-1].i_block[0]);
struct os_direntry_t* dirEntry = malloc(sizeof(struct os_direntry_t));
assert (dirEntry != NULL);
assert(lseek(fd, (off_t)(inodes[base_inode_num-1].i_block[0]*1024), SEEK_SET) == (off_t)(inodes[base_inode_num-1].i_block[0]*1024));
assert(read(fd, (void *)dirEntry, sizeof(struct os_direntry_t)) == sizeof(struct os_direntry_t));
while (dirEntry->inode) {
name = (char*)malloc(dirEntry->name_len+1);
memcpy(name, dirEntry->file_name, dirEntry->name_len);
name[dirEntry->name_len+1] = '\0';
curr_inode_num = dirEntry->inode;
curr_inode_type = dirEntry->file_type;
lseek(fd, (dirEntry->rec_len - sizeof(struct os_direntry_t)), SEEK_CUR);
assert(read(fd, (void *)dirEntry, sizeof(struct os_direntry_t)) == sizeof(struct os_direntry_t));
if (name[0] == '.') {
if ( name[1]=='.' || name[1]=='\0')
continue;
} else {
debug("rec_len\t\t= %d\n", dirEntry->rec_len);
debug("dirEntry->inode\t= %d\n",dirEntry->inode);
printInodeType(curr_inode_type);
printInodePerm(fd, curr_inode_num);
printf("%d\t", curr_inode_num);
printf("%s\t", name);
printf("\n");
}
}
return;
}
void cp(int fd, int base_inode_num)
{
char filename[255];
int ret;
//printf("Enter filename:");
scanf("%s", filename);
ret = findInodeByName(fd, base_inode_num, filename, EXT2_FT_REG_FILE);
debug("findInodeByName=%d\n", ret);
if(ret==-1) {
printf("File %s does not exist\n", filename);
} else {
printf("Saving file %s\n", filename);
saveInode(fd, ret, filename);
}
}
int cd(int fd, int base_inode_num)
{
char dirname[255];
int ret;
//printf("Enter directory name:");
scanf("%s", dirname);
ret = findInodeByName(fd, base_inode_num, dirname, EXT2_FT_DIR);
debug("findInodeByName=%d\n", ret);
if(ret==-1) {
printf("Directory %s does not exist\n", dirname);
return(base_inode_num);
} else {
printf("Now in directory %s\n", dirname);
return(ret);
}
}
int extShell(int fd )
{
char cmd[4];
static int pwd_inode = 2;
printf("ext-shell$ ");
scanf("%s", cmd);
debug("cmd=%s\n", cmd);
if(!strcmp(cmd, "q")) {
return(-1);
} else if(!strcmp(cmd, "ls")) {
ls(fd, pwd_inode);
} else if(!strcmp(cmd, "cd")) {
pwd_inode = cd(fd, pwd_inode);
} else if(!strcmp(cmd, "cp")) {
cp(fd, pwd_inode);
} else {
printf("Unknown command: %s\n", cmd);
return(-EINVAL);
}
return(0);
}
int main(int argc, char **argv)
{
// open up the disk file
if (argc !=2) {
printf("usage: ext-shell <file.img>\n");
return -1;
}
int fd = open(argv[1], O_RDONLY|O_SYNC);
if (fd == -1) {
printf("Could NOT open file \"%s\"\n", argv[1]);
return -1;
}
// reading superblock
read_superblock(fd);
printf("block size \t\t= %d bytes\n", 1<<(10 + superblock->s_log_block_size));
printf("inode count \t\t= 0x%x\n", superblock->s_inodes_count);
printf("inode size \t\t= 0x%x\n", superblock->s_inode_size);
// reading blockgroup
read_blockgroup(fd);
printf("inode table address \t= 0x%x\n", blockgroup->bg_inode_table);
printf("inode table size \t= %dKB\n", (superblock->s_inodes_count*superblock->s_inode_size)>>10);
// reading inode table
read_inodeTable(fd);
while(1) {
// extShell waits for one cmd and executes it.
// returns 0 on successfull execution of command,
// returns -EINVAL on unknown command
// returns -1 if cmd="q" i.e. quit.
if ( extShell(fd)==-1 )
break;
}
printf("\n\nQuitting ext-shell.\n\n");
return(0);
}