1use std::collections::HashMap;
9
10use anyhow::{Context, Result, ensure};
11use log::warn;
12
13use swh_graph::NodeType;
14use swh_graph::graph::*;
15use swh_graph::labels::{EdgeLabel, LabelNameId, Permission};
16use swh_graph::properties;
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 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 resolve_name_by_id(&graph, dir, name_id)
47}
48
49pub fn 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 #[allow(clippy::collapsible_if)]
65 if let EdgeLabel::DirEntry(dentry) = label {
66 if dentry.label_name_id() == name {
67 return Ok(Some(succ));
68 }
69 }
70 }
71 Ok(None)
72}
73
74pub fn resolve_path<G>(graph: &G, dir: NodeId, path: impl AsRef<[u8]>) -> Result<Option<NodeId>>
87where
88 G: SwhLabeledForwardGraph + SwhGraphWithProperties,
89 <G as SwhGraphWithProperties>::LabelNames: properties::LabelNames,
90 <G as SwhGraphWithProperties>::Maps: properties::Maps,
91{
92 let props = graph.properties();
93 let path = path
94 .as_ref()
95 .split(|byte| *byte == b'/')
96 .map(|name| {
97 props
98 .label_name_id(name)
99 .with_context(|| msg_no_label_name_id(name))
100 })
101 .collect::<Result<Vec<LabelNameId>, _>>()?;
102 resolve_path_by_id(&graph, dir, &path)
103}
104
105pub fn resolve_path_by_id<G>(graph: &G, dir: NodeId, path: &[LabelNameId]) -> Result<Option<NodeId>>
109where
110 G: SwhLabeledForwardGraph + SwhGraphWithProperties,
111 <G as SwhGraphWithProperties>::Maps: properties::Maps,
112{
113 let mut cur_entry = dir;
114 for name in path {
115 match resolve_name_by_id(graph, cur_entry, *name)? {
116 None => return Ok(None),
117 Some(entry) => cur_entry = entry,
118 }
119 }
120 Ok(Some(cur_entry))
121}
122
123#[derive(Debug, Default, PartialEq)]
128pub enum FsTree {
129 #[default]
130 Content,
131 Directory(HashMap<Vec<u8>, (FsTree, Option<Permission>)>),
132 Revision(NodeId),
133}
134
135pub fn ls_tree<G>(graph: &G, dir: NodeId) -> Result<FsTree>
143where
144 G: SwhLabeledForwardGraph + SwhGraphWithProperties,
145 <G as SwhGraphWithProperties>::LabelNames: properties::LabelNames,
146 <G as SwhGraphWithProperties>::Maps: properties::Maps,
147{
148 let props = graph.properties();
149 let node_type = props.node_type(dir);
150 ensure!(
151 node_type == NodeType::Directory,
152 "Type of {dir} should be dir, but is {node_type} instead"
153 );
154
155 let mut dir_entries = HashMap::new();
156 for (succ, labels) in graph.labeled_successors(dir) {
157 let node_type = props.node_type(succ);
158 for label in labels {
159 if let EdgeLabel::DirEntry(dentry) = label {
160 let file_name = props.label_name(dentry.label_name_id());
161 let perm = dentry.permission();
162 match node_type {
163 NodeType::Content => {
164 dir_entries.insert(file_name, (FsTree::Content, perm));
165 }
166 NodeType::Directory => {
167 if let Ok(subdir) = ls_tree(graph, succ) {
169 dir_entries.insert(file_name, (subdir, perm));
170 } else {
171 warn!("Cannot list (sub-)directory {succ}, skipping it");
172 }
173 }
174 NodeType::Revision | NodeType::Release => {
175 dir_entries.insert(file_name, (FsTree::Revision(succ), perm));
176 }
177 NodeType::Origin | NodeType::Snapshot => {
178 warn!("Ignoring dir entry with unexpected type {node_type}");
179 }
180 }
181 }
182 }
183 }
184
185 Ok(FsTree::Directory(dir_entries))
186}