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
use anyhow::{bail, Context as AnyhowContext, Result};
use super::Context;
use slos_filesystem::impls::SimpleMemoryFilesystem;
pub fn cmd_path_normalize(_context: &mut Context, args: &[String]) -> Result<()> {
for path in args.iter() {
let npath = slos_filesystem::path::normalize(path);
println!("{}", npath);
}
Ok(())
}
pub fn cmd_path_split(_context: &mut Context, args: &[String]) -> Result<()> {
for path in args.iter() {
let npath = slos_filesystem::path::split(path);
println!("{:?}", npath);
}
Ok(())
}
pub fn cmd_path_join(_context: &mut Context, args: &[String]) -> Result<()> {
let npath = slos_filesystem::path::join(args);
println!("{}", npath);
Ok(())
}
pub fn cmd_mount_list(context: &mut Context, _args: &[String]) -> Result<()> {
for mp in context.fs.mountpoints.as_slice().iter() {
let pathvec = mp
.path_vec()
.iter()
.map(|x| String::from(*x))
.collect::<Vec<String>>();
println!("{} - {:?}", slos_filesystem::path::join(&pathvec), mp.root);
}
Ok(())
}
pub fn cmd_mount_new_memoryfs(context: &mut Context, args: &[String]) -> Result<()> {
let path = if let Some(path) = args.first() {
slos_filesystem::path::split(path)
} else {
bail!("invalid path")
};
let path_split = path.iter().map(|x| x.as_str()).collect::<Vec<&str>>();
context
.fs
.mount(&path_split, Box::new(SimpleMemoryFilesystem::new()))
.with_context(|| "failed to mount new SimpleMemoryFilesystem")?;
Ok(())
}
pub fn cmd_file_read(context: &mut Context, args: &[String]) -> Result<()> {
let path = if let Some(path) = args.first() {
slos_filesystem::path::split(path)
} else {
bail!("invalid path")
};
let path_split = path.iter().map(|x| x.as_str()).collect::<Vec<&str>>();
let node = context
.fs
.node_at_path(&path_split)
.context("couldn't get node")?;
let filenode = node.try_file().context("failed to FsNode.try_file")?;
let filehandle = filenode.open().context("failed to open file")?;
let content = filehandle
.raw_read(0, None)
.context("failed to read file")?;
println!("[bytes] {:?}", content);
if let Ok(s) = String::from_utf8(content.to_vec()) {
println!("[string] {:?}", s);
}
Ok(())
}
pub fn cmd_file_write_test(context: &mut Context, args: &[String]) -> Result<()> {
let path = if let Some(path) = args.first() {
slos_filesystem::path::split(path)
} else {
bail!("invalid path")
};
let mut path_split = path.iter().map(|x| x.as_str()).collect::<Vec<&str>>();
let filename = if let Some(filename) = path_split.pop() {
filename
} else {
bail!("invalid path")
};
let parent = context
.fs
.node_at_path(&path_split)
.context("couldn't get parent")?;
let parentdir = parent
.try_directory()
.context("parent is not a directory")?;
let filenode = parentdir.touch(filename).context("failed to create file")?;
let filenode = filenode.try_file().context("failed to FsNode.try_file")?;
let filehandle = filenode.open().context("failed to open file")?;
filehandle
.raw_write(0, b"hello world!")
.context("failed to write to file")?;
Ok(())
}