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 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 #[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 fs_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 fs_resolve_path_by_id(&graph, dir, &path)
103}
104
105pub fn fs_resolve_path_by_id<G>(
109 graph: &G,
110 dir: NodeId,
111 path: &[LabelNameId],
112) -> Result<Option<NodeId>>
113where
114 G: SwhLabeledForwardGraph + SwhGraphWithProperties,
115 <G as SwhGraphWithProperties>::Maps: properties::Maps,
116{
117 let mut cur_entry = dir;
118 for name in path {
119 match fs_resolve_name_by_id(graph, cur_entry, *name)? {
120 None => return Ok(None),
121 Some(entry) => cur_entry = entry,
122 }
123 }
124 Ok(Some(cur_entry))
125}
126
127#[derive(Debug, Default, PartialEq)]
132pub enum FsTree {
133 #[default]
134 Content,
135 Directory(HashMap<Vec<u8>, (FsTree, Option<Permission>)>),
136 Revision(NodeId),
137}
138
139pub fn fs_ls_tree<G>(graph: &G, dir: NodeId) -> Result<FsTree>
147where
148 G: SwhLabeledForwardGraph + SwhGraphWithProperties,
149 <G as SwhGraphWithProperties>::LabelNames: properties::LabelNames,
150 <G as SwhGraphWithProperties>::Maps: properties::Maps,
151{
152 let props = graph.properties();
153 let node_type = props.node_type(dir);
154 ensure!(
155 node_type == NodeType::Directory,
156 "Type of {dir} should be dir, but is {node_type} instead"
157 );
158
159 let mut dir_entries = HashMap::new();
160 for (succ, labels) in graph.labeled_successors(dir) {
161 let node_type = props.node_type(succ);
162 for label in labels {
163 if let EdgeLabel::DirEntry(dentry) = label {
164 let file_name = props.label_name(dentry.label_name_id());
165 let perm = dentry.permission();
166 match node_type {
167 NodeType::Content => {
168 dir_entries.insert(file_name, (FsTree::Content, perm));
169 }
170 NodeType::Directory => {
171 if let Ok(subdir) = fs_ls_tree(graph, succ) {
173 dir_entries.insert(file_name, (subdir, perm));
174 } else {
175 warn!("Cannot list (sub-)directory {succ}, skipping it");
176 }
177 }
178 NodeType::Revision | NodeType::Release => {
179 dir_entries.insert(file_name, (FsTree::Revision(succ), perm));
180 }
181 NodeType::Origin | NodeType::Snapshot => {
182 warn!("Ignoring dir entry with unexpected type {node_type}");
183 }
184 }
185 }
186 }
187 }
188
189 Ok(FsTree::Directory(dir_entries))
190}