1use std::collections::HashMap;
9
10use anyhow::{ensure, Context, Result};
11use log::warn;
12
13use swh_graph::graph::*;
14use swh_graph::labels::{EdgeLabel, LabelNameId, Permission};
15use swh_graph::properties;
16use swh_graph::NodeType;
17
18fn msg_no_label_name_id(name: impl AsRef<[u8]>) -> String {
19 format!(
20 "no label_name id found for entry \"{}\"",
21 String::from_utf8_lossy(name.as_ref())
22 )
23}
24
25pub fn fs_resolve_name<G>(graph: &G, dir: NodeId, name: impl AsRef<[u8]>) -> Result<Option<NodeId>>
37where
38 G: SwhLabeledForwardGraph + SwhGraphWithProperties,
39 <G as SwhGraphWithProperties>::LabelNames: properties::LabelNames,
40 <G as SwhGraphWithProperties>::Maps: properties::Maps,
41{
42 let props = graph.properties();
43 let name_id = props
44 .label_name_id(name.as_ref())
45 .with_context(|| msg_no_label_name_id(name))?;
46 fs_resolve_name_by_id(&graph, dir, name_id)
47}
48
49pub fn fs_resolve_name_by_id<G>(graph: &G, dir: NodeId, name: LabelNameId) -> Result<Option<NodeId>>
53where
54 G: SwhLabeledForwardGraph + SwhGraphWithProperties,
55 <G as SwhGraphWithProperties>::Maps: properties::Maps,
56{
57 let node_type = graph.properties().node_type(dir);
58 ensure!(
59 node_type == NodeType::Directory,
60 "Type of {dir} should be dir, but is {node_type} instead"
61 );
62
63 for (succ, label) in graph.labeled_successors(dir).flatten_labels() {
64 if let EdgeLabel::DirEntry(dentry) = label {
65 if dentry.label_name_id() == name {
66 return Ok(Some(succ));
67 }
68 }
69 }
70 Ok(None)
71}
72
73pub fn fs_resolve_path<G>(graph: &G, dir: NodeId, path: impl AsRef<[u8]>) -> Result<Option<NodeId>>
86where
87 G: SwhLabeledForwardGraph + SwhGraphWithProperties,
88 <G as SwhGraphWithProperties>::LabelNames: properties::LabelNames,
89 <G as SwhGraphWithProperties>::Maps: properties::Maps,
90{
91 let props = graph.properties();
92 let path = path
93 .as_ref()
94 .split(|byte| *byte == b'/')
95 .map(|name| {
96 props
97 .label_name_id(name)
98 .with_context(|| msg_no_label_name_id(name))
99 })
100 .collect::<Result<Vec<LabelNameId>, _>>()?;
101 fs_resolve_path_by_id(&graph, dir, &path)
102}
103
104pub fn fs_resolve_path_by_id<G>(
108 graph: &G,
109 dir: NodeId,
110 path: &[LabelNameId],
111) -> Result<Option<NodeId>>
112where
113 G: SwhLabeledForwardGraph + SwhGraphWithProperties,
114 <G as SwhGraphWithProperties>::Maps: properties::Maps,
115{
116 let mut cur_entry = dir;
117 for name in path {
118 match fs_resolve_name_by_id(graph, cur_entry, *name)? {
119 None => return Ok(None),
120 Some(entry) => cur_entry = entry,
121 }
122 }
123 Ok(Some(cur_entry))
124}
125
126#[derive(Debug, Default, PartialEq)]
131pub enum FsTree {
132 #[default]
133 Content,
134 Directory(HashMap<Vec<u8>, (FsTree, Option<Permission>)>),
135 Revision(NodeId),
136}
137
138pub fn fs_ls_tree<G>(graph: &G, dir: NodeId) -> Result<FsTree>
146where
147 G: SwhLabeledForwardGraph + SwhGraphWithProperties,
148 <G as SwhGraphWithProperties>::LabelNames: properties::LabelNames,
149 <G as SwhGraphWithProperties>::Maps: properties::Maps,
150{
151 let props = graph.properties();
152 let node_type = props.node_type(dir);
153 ensure!(
154 node_type == NodeType::Directory,
155 "Type of {dir} should be dir, but is {node_type} instead"
156 );
157
158 let mut dir_entries = HashMap::new();
159 for (succ, labels) in graph.labeled_successors(dir) {
160 let node_type = props.node_type(succ);
161 for label in labels {
162 if let EdgeLabel::DirEntry(dentry) = label {
163 let file_name = props.label_name(dentry.label_name_id());
164 let perm = dentry.permission();
165 match node_type {
166 NodeType::Content => {
167 dir_entries.insert(file_name, (FsTree::Content, perm));
168 }
169 NodeType::Directory => {
170 if let Ok(subdir) = fs_ls_tree(graph, succ) {
172 dir_entries.insert(file_name, (subdir, perm));
173 } else {
174 warn!("Cannot list (sub-)directory {succ}, skipping it");
175 }
176 }
177 NodeType::Revision | NodeType::Release => {
178 dir_entries.insert(file_name, (FsTree::Revision(succ), perm));
179 }
180 NodeType::Origin | NodeType::Snapshot => {
181 warn!("Ignoring dir entry with unexpected type {node_type}");
182 }
183 }
184 }
185 }
186 }
187
188 Ok(FsTree::Directory(dir_entries))
189}