Nothing to see here, move along
1#![no_std]
2#![no_main]
3
4use lancer_user::fs::{self, FsRights};
5use lancer_user::net;
6use lancer_user::println;
7use lancer_user::syscall;
8
9#[unsafe(no_mangle)]
10pub extern "C" fn lancer_main() -> ! {
11 let (rx, _tx) = match net::init() {
12 Some(pair) => pair,
13 None => {
14 lancer_user::show!(net, error, "netsock init failed");
15 syscall::exit();
16 }
17 };
18
19 let mut args_buf = [0u8; 512];
20 let args_len = net::recv_args(&rx, &mut args_buf);
21 let args = &args_buf[..args_len];
22
23 let (_cmd, rest) = net::next_token(args);
24 let (src_tok, rest2) = net::next_token(rest);
25 let (dst_tok, _) = net::next_token(rest2);
26
27 if src_tok.is_empty() || dst_tok.is_empty() {
28 println!("usage: cp <src> <dst>");
29 syscall::exit();
30 }
31
32 let mut client = unsafe { fs::init() };
33
34 let src_handle = match fs::open_path_with_rights(&mut client, 0, src_tok, fs::FsRights::READ) {
35 Ok(h) => h,
36 Err(e) => {
37 println!("cp: src: {}", e.name());
38 syscall::exit();
39 }
40 };
41
42 let (dst_parent, dst_name) = match fs::open_parent(&mut client, 0, dst_tok) {
43 Ok(pair) => pair,
44 Err(e) => {
45 println!("cp: dst: {}", e.name());
46 let _ = client.close(src_handle);
47 syscall::exit();
48 }
49 };
50
51 let dst_mode = FsRights::from_raw(FsRights::WRITE.raw() | FsRights::CREATE.raw());
52 let dst_handle = match client.open(dst_parent, dst_name, dst_mode) {
53 Ok(h) => h,
54 Err(e) => {
55 println!("cp: dst open: {}", e.name());
56 let _ = client.close(dst_parent);
57 let _ = client.close(src_handle);
58 syscall::exit();
59 }
60 };
61
62 match client.truncate(dst_handle, 0) {
63 Ok(()) => {}
64 Err(e) => {
65 println!("cp: truncate: {}", e.name());
66 let _ = client.close(dst_handle);
67 let _ = client.close(dst_parent);
68 let _ = client.close(src_handle);
69 syscall::exit();
70 }
71 }
72
73 let mut buf = [0u8; 4096];
74 fn copy_chunks(client: &mut fs::FsClient, src: u8, dst: u8, buf: &mut [u8], offset: u64) {
75 match client.read(src, offset, buf) {
76 Ok(0) => {}
77 Ok(n) => match client.write(dst, offset, &buf[..n]) {
78 Ok(_) => copy_chunks(client, src, dst, buf, offset + n as u64),
79 Err(e) => println!("cp: write: {}", e.name()),
80 },
81 Err(e) => println!("cp: read: {}", e.name()),
82 }
83 }
84 copy_chunks(&mut client, src_handle, dst_handle, &mut buf, 0);
85
86 let _ = client.close(dst_handle);
87 let _ = client.close(dst_parent);
88 let _ = client.close(src_handle);
89 syscall::exit()
90}