File: cat.rs

package info (click to toggle)
rust-tokio-uring 0.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 828 kB
  • sloc: makefile: 2
file content (48 lines) | stat: -rw-r--r-- 968 bytes parent folder | download
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
use std::{
    io::Write,
    {env, io},
};

use tokio_uring::fs::File;

fn main() {
    // The file to `cat` is passed as a CLI argument
    let args: Vec<_> = env::args().collect();

    if args.len() <= 1 {
        panic!("no path specified");
    }

    let path = &args[1];

    // Lock stdout
    let out = io::stdout();
    let mut out = out.lock();

    tokio_uring::start(async {
        // Open the file without blocking
        let file = File::open(path).await.unwrap();
        let mut buf = vec![0; 16 * 1_024];

        // Track the current position in the file;
        let mut pos = 0;

        loop {
            // Read a chunk
            let (res, b) = file.read_at(buf, pos).await;
            let n = res.unwrap();

            if n == 0 {
                break;
            }

            out.write_all(&b[..n]).unwrap();
            pos += n as u64;

            buf = b;
        }

        // Include a new line
        println!();
    });
}