feat: mount point flags check
[lunaix-os.git] / lunaix-os / kernel / demos / iotest.c
1 #include <lunaix/fctrl.h>
2 #include <lunaix/foptions.h>
3 #include <lunaix/lunistd.h>
4 #include <lunaix/proc.h>
5 #include <lunaix/syslog.h>
6
7 LOG_MODULE("IOTEST")
8
9 #define STDIN 1
10 #define STDOUT 0
11
12 void
13 _iotest_main()
14 {
15     char test_sequence[] = "Once upon a time, in a magical land of Equestria. "
16                            "There were two regal sisters who ruled together "
17                            "and created harmony for all the land.";
18     char read_out[256];
19
20     // 切换工作目录至 /dev
21     int errno = chdir("/dev");
22     if (errno) {
23         write(STDOUT, "fail to chdir", 15);
24         return;
25     }
26
27     if (getcwd(read_out, sizeof(read_out))) {
28         write(STDOUT, "current working dir: ", 22);
29         write(STDOUT, read_out, 256);
30         write(STDOUT, "\n", 2);
31     }
32
33     // sda 设备 - 硬盘
34     //  sda设备属于容积设备(Volumetric Device),
35     //  Lunaix会尽可能缓存任何对此设备的上层读写,并使用延迟写入策略。(FO_DIRECT可用于屏蔽该功能)
36     int fd = open("./sda", 0);
37
38     if (fd < 0) {
39         kprintf(KERROR "fail to open (%d)\n", geterrno());
40         return;
41     }
42
43     // 移动指针至512字节,在大多数情况下,这是第二个逻辑扇区的起始处
44     lseek(fd, 512, FSEEK_SET);
45
46     // 总共写入 64 * 136 字节,会产生3个页作为缓存
47     for (size_t i = 0; i < 64; i++) {
48         write(fd, test_sequence, sizeof(test_sequence));
49     }
50
51     // 随机读写测试
52     lseek(fd, 4 * 4096, FSEEK_SET);
53     write(fd, test_sequence, sizeof(test_sequence));
54
55     write(STDOUT, "input: ", 8);
56     int size = read(STDIN, read_out, 256);
57
58     write(STDOUT, "your input: ", 13);
59     write(STDOUT, read_out, size);
60     write(fd, read_out, size);
61     write(STDOUT, "\n", 1);
62
63     // 读出我们写的内容
64     lseek(fd, 512, FSEEK_SET);
65     read(fd, read_out, sizeof(read_out));
66
67     // 将读出的内容直接写入tty设备
68     write(STDOUT, read_out, sizeof(read_out));
69     write(STDOUT, "\n", 1);
70
71     // 关闭文件,这同时会将页缓存中的数据下发到底层驱动
72     close(fd);
73
74     kprint_hex(read_out, sizeof(read_out));
75 }