aboutsummaryrefslogtreecommitdiffstats
path: root/main.c
blob: 2c1be74fbe308b7aa8dafebabcd8eb4ed36106e5 (plain)
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
/**
 * This is just a test program, it does nothing but just 
 * init the cache first then use get_cache_sector to test
 * the LRU algorithm with 12 fake-sector number.
 *
 * And it work well here
 *
 */
#include <stdio.h>
#include "cache.h"
#include "ext2_fs.h"

#include <fcntl.h>
#include <sys/stat.h>



struct ext2_super_block sb;

int fd;

extern struct cache_struct cache[CACHE_ENTRIES + 1];


/**
 * Just print the sector, and according the LRU algorithm, 
 * Left most value is the most least secotr, and Right most 
 * value is the most Recent sector. I see it's a Left Right Used
 * (LRU) algorithm; Just kidding:)
 */
void print_cache(void)
{
        int i = 0;
        struct cache_struct *cs = cache;
        for (; i < CACHE_ENTRIES; i++) {
                cs = cs->next;
                printf("%d ", cs->block);        
        }

        printf("\n");
}


/* test function */
void print_hex(char *data, int size)
{
        int i = 0;
        while ( size --) {
                
                if ( (i != 0) && (i % 16 == 0) )
                        printf("\n");
                i ++;

                if ( (*data < 0x20 ) || (*data > 0x80 ) ) {
                        printf(".");
                        data ++;
                        continue;
                }
                putc(*data, stdout);
                data++;

        }
        printf("\n");
}


void usage(void)
{
        printf("USAGE: a.out ext2fs.img filename\n");
        printf("---- ext2fs.img means a ext2 filesytem image\n");
        printf("---- filename menas the file you wanna open, it can be \n");
        printf("     in one of the two following forms:\n");
        printf("     /file/name/xy.y, this is a full name, or \n");
        printf("     file/name/xy.z, in this case, the program will search\n");
        printf("     from the directory where the extlinux.sys stored\n");

}




/**
 * well, it's just a test program that test if the fs driver 
 * would work on ext2/3(ext4 not added for now) filesystem 
 * well or not. so it's task is simple,too: open the ext2fs, 
 * then read what you want.
 *
 */
int main(int argc, char *argv[])
{

        int bytes_read = 0;
        int total_bytes = 0;
        int have_more;
        char *ext2fs = argv[1];
        char *filename = argv[2];
        char buf[1024];
        struct cache_struct *cs;
        struct open_file_t *file = NULL;
        

        if (argc != 3 ) {
                usage();
                return 0;
        }

        /* init. the cache */
        cache_init();

        
        fd = open(ext2fs, O_RDONLY);
        if ( fd < 0 ) {
                printf("File %s open error....\n", ext2fs);
                return 0;
        }

        init_fs(&sb);

        file = (struct open_file_t *)open_file(filename);
        if ( ! file ) {
                printf("open file error: file %s not found ....\n", filename);
                close(fd);
                return 0;
        }

        /*
         * The following message may be nosiy, but it told we how is it 
         * going well.
         */
        do {
                bytes_read = read_file(file, buf, 1024, &have_more);
                printf("--------read %d bytes-------\n", bytes_read);
                printf("----------------\n");
                print_hex(buf, bytes_read);
                
                total_bytes += bytes_read;
        }while(have_more);
        
        printf("-------------total read %d bytes------\n", total_bytes);

        close(fd);

        return 0;
}