aboutsummaryrefslogtreecommitdiffstats
path: root/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'main.c')
-rw-r--r--main.c144
1 files changed, 144 insertions, 0 deletions
diff --git a/main.c b/main.c
new file mode 100644
index 0000000..3dbcfa2
--- /dev/null
+++ b/main.c
@@ -0,0 +1,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->sector);
+ }
+
+ 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 *)ext2_open(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 = ext2_read(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;
+}