aboutsummaryrefslogtreecommitdiffstats
path: root/iso_main.c
blob: c5b44f40411f70250da3c07a6b6cf627574cb4bc (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
145
146
147
148
149
150
151
152
153
154
/**
 * well, it's just for isolinux, 'cause the isolinux is kind of 
 * different, that don't use cache, and the block size is 2k but 
 * not 512 B
 *
 * so we make a iso_main for isolinux.
 */


/**
 * 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 <string.h>
#include <fcntl.h>
#include <sys/stat.h>


int fd;



/* 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 fs.img filename\n");
        printf("---- fs.img means a filesytem image,it can be EXT, FAT and ISO9660 fs for now\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 *fs = argv[1];
        char *filename = argv[2];
        char buf[2048];
        struct cache_struct *cs;
        struct open_file_t *file = NULL;

        int fd2;
        char save_file[256];
        char *p = NULL;

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

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


        strcpy(save_file, "test_out/");
        if ( p = strchr(fs, '.') )
                strncat(save_file, fs, p - fs);
        else
                strcat(save_file, fs);
                        
        if ( p = strrchr(filename, '/') )
                p ++;
        else
                p = filename;
        
        strcat(save_file, "_");
        strcat(save_file, p);
        fd2 = open(save_file, O_RDWR | O_CREAT, 0644);
        if ( fd < 0 ) {
                printf("create file %s error....\n", save_file);
                return 0;
        }



        init_fs();

        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);
                total_bytes += bytes_read;
                write(fd2, buf, bytes_read);
                                
#if 0
                printf("--------read %d bytes-------\n", bytes_read);
                printf("----------------\n");
                print_hex(buf, bytes_read);
#endif           
        }while(have_more);

        
        printf("-------------total read %d bytes------\n", total_bytes);

        close(fd);

        return 0;
}