swh_graph_stdlib/
fs.rs

1// Copyright (C) 2024  The Software Heritage developers
2// See the AUTHORS file at the top-level directory of this distribution
3// License: GNU General Public License version 3, or any later version
4// See top-level LICENSE file for more information
5
6//! Filesystem manipulation functions.
7
8use 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
25/// Given a graph and a directory node, return the node id of a named directory
26/// entry located (not recursively) in that directory, if it exists.
27///
28/// See [fs_resolve_path] for a version of this function that traverses
29/// sub-directories recursively.
30///
31/// ```ignore
32/// if let Ok(Some(node)) == fs_resolve_name(&graph, 42, "README.md") {
33///     // do something with node
34/// }
35/// ```
36pub 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
49/// Same as [fs_resolve_name], but using a pre-resolved [LabelNameId] as entry
50/// name. Using this function is more efficient in case the same name (e.g.,
51/// "README.md") is to be looked up in many directories.
52pub 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
73/// Given a graph and a directory node, return the node id of a directory entry
74/// located at a given path within that directory, if it exists.
75///
76/// Slashes (`/`) contained in `path` are interpreted as path separators.
77///
78/// See [fs_resolve_name] for a non-recursive version of this function.
79///
80/// ```ignore
81/// if let Ok(Some(node)) == fs_resolve_path(&graph, 42, "src/main.c") {
82///     // do something with node
83/// }
84/// ```
85pub 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
104/// Same as [fs_resolve_path], but using as path a sequence of pre-resolved
105/// [LabelNameId]-s. Using this function is more efficient in case the same path
106/// (e.g., "src/main.c") is to be looked up in many directories.
107pub 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/// Recursive representation of a directory tree, ignoring sharing.
127///
128/// Note that a `Revision` variant can in fact point to either revision or
129/// release nodes.
130#[derive(Debug, Default, PartialEq)]
131pub enum FsTree {
132    #[default]
133    Content,
134    Directory(HashMap<Vec<u8>, (FsTree, Option<Permission>)>),
135    Revision(NodeId),
136}
137
138/// Given a graph and a directory node in it (usually, but not necessarily, the
139/// *root* directory of a repository), return a recursive list of the contained
140/// files and directories.
141///
142/// Note that symlinks are not followed during listing and are reported as
143/// files in the returned tree. To recognize them as links, check the
144/// permissions of the associated directory entries.
145pub 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                        // recurse into subdir
171                        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}