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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// Copyright 2023 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

//! Data structures and functions relevant to `fuchsia.io` name processing.
//!
//! These names may be used to designate the location of a node as it
//! appears in a directory.
//!
//! These should be aligned with the library comments in sdk/fidl/fuchsia.io/io.fidl.

use {
    fidl_fuchsia_io as fio, fuchsia_zircon_status::Status, static_assertions::const_assert_eq,
    std::borrow::Borrow, std::fmt::Display, std::ops::Deref, thiserror::Error,
};

/// The type for the name of a node, i.e. a single path component, e.g. `foo`.
///
/// ## Invariants
///
/// A valid node name must meet the following criteria:
///
/// * It cannot be longer than [MAX_NAME_LENGTH].
/// * It cannot be empty.
/// * It cannot be ".." (dot-dot).
/// * It cannot be "." (single dot).
/// * It cannot contain "/".
/// * It cannot contain embedded NUL.
#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct Name(String);

/// The maximum length, in bytes, of a single filesystem component.
pub const MAX_NAME_LENGTH: usize = fio::MAX_NAME_LENGTH as usize;
const_assert_eq!(MAX_NAME_LENGTH as u64, fio::MAX_NAME_LENGTH);

#[derive(Error, Debug, Clone)]
pub enum ParseNameError {
    #[error("name `{0}` is too long")]
    TooLong(String),

    #[error("name cannot be empty")]
    Empty,

    #[error("name cannot be `.`")]
    Dot,

    #[error("name cannot be `..`")]
    DotDot,

    #[error("name cannot contain `/`")]
    Slash,

    #[error("name cannot contain embedded NUL")]
    EmbeddedNul,
}

impl From<ParseNameError> for Status {
    fn from(value: ParseNameError) -> Self {
        match value {
            ParseNameError::TooLong(_) => Status::BAD_PATH,
            _ => Status::INVALID_ARGS,
        }
    }
}

impl Name {
    pub fn from<S: Into<String>>(s: S) -> Result<Name, ParseNameError> {
        parse_name(s.into())
    }
}

impl Display for Name {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let value = &self.0;
        write!(f, "{value}")
    }
}

/// Parses a string name into a [Name].
pub fn parse_name(name: String) -> Result<Name, ParseNameError> {
    validate_name(&name)?;
    Ok(Name(name))
}

/// Check whether a string name will be a valid input to [Name].
pub fn validate_name(name: &str) -> Result<(), ParseNameError> {
    if name.len() > MAX_NAME_LENGTH {
        return Err(ParseNameError::TooLong(name.to_string()));
    }
    if name.len() == 0 {
        return Err(ParseNameError::Empty);
    }
    if name == "." {
        return Err(ParseNameError::Dot);
    }
    if name == ".." {
        return Err(ParseNameError::DotDot);
    }
    if name.chars().any(|c: char| c == '/') {
        return Err(ParseNameError::Slash);
    }
    if name.chars().any(|c: char| c == '\0') {
        return Err(ParseNameError::EmbeddedNul);
    }
    Ok(())
}

impl From<Name> for String {
    fn from(value: Name) -> Self {
        value.0
    }
}

impl TryFrom<String> for Name {
    type Error = ParseNameError;

    fn try_from(value: String) -> Result<Name, ParseNameError> {
        parse_name(value)
    }
}

impl Deref for Name {
    type Target = str;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl Borrow<str> for Name {
    fn borrow(&self) -> &str {
        &*self
    }
}

#[cfg(test)]
mod tests {
    use {super::*, assert_matches::assert_matches};

    #[test]
    fn test_parse_name() {
        assert_matches!(parse_name("a".repeat(1000)), Err(ParseNameError::TooLong(_)));
        assert_matches!(
            parse_name(
                std::str::from_utf8(&vec![65; fio::MAX_FILENAME as usize + 1]).unwrap().to_string()
            ),
            Err(ParseNameError::TooLong(_))
        );
        assert_matches!(
            parse_name(
                std::str::from_utf8(&vec![65; fio::MAX_FILENAME as usize]).unwrap().to_string()
            ),
            Ok(_)
        );
        assert_matches!(parse_name("".to_string()), Err(ParseNameError::Empty));
        assert_matches!(parse_name(".".to_string()), Err(ParseNameError::Dot));
        assert_matches!(parse_name("..".to_string()), Err(ParseNameError::DotDot));
        assert_matches!(parse_name(".a".to_string()), Ok(Name(name)) if &name == ".a");
        assert_matches!(parse_name("..a".to_string()), Ok(Name(name)) if &name == "..a");
        assert_matches!(parse_name("a/b".to_string()), Err(ParseNameError::Slash));
        assert_matches!(parse_name("a\0b".to_string()), Err(ParseNameError::EmbeddedNul));
        assert_matches!(parse_name("abc".to_string()), Ok(Name(name)) if &name == "abc");
    }

    #[test]
    fn test_validate_name() {
        assert_matches!(validate_name(&"a".repeat(1000)), Err(ParseNameError::TooLong(_)));
        assert_matches!(
            validate_name(std::str::from_utf8(&vec![65; fio::MAX_FILENAME as usize + 1]).unwrap()),
            Err(ParseNameError::TooLong(_))
        );
        assert_matches!(
            validate_name(std::str::from_utf8(&vec![65; fio::MAX_FILENAME as usize]).unwrap()),
            Ok(())
        );
        assert_matches!(validate_name(""), Err(ParseNameError::Empty));
        assert_matches!(validate_name("."), Err(ParseNameError::Dot));
        assert_matches!(validate_name(".."), Err(ParseNameError::DotDot));
        assert_matches!(validate_name(".a"), Ok(()));
        assert_matches!(validate_name("..a"), Ok(()));
        assert_matches!(validate_name("a/b"), Err(ParseNameError::Slash));
        assert_matches!(validate_name("a\0b"), Err(ParseNameError::EmbeddedNul));
        assert_matches!(validate_name("abc"), Ok(()));
    }

    #[test]
    fn test_try_from() {
        assert_matches!(Name::try_from("a".repeat(1000)), Err(ParseNameError::TooLong(_)));
        assert_matches!(Name::try_from("abc".to_string()), Ok(Name(name)) if &name == "abc");
    }

    #[test]
    fn test_into() {
        let name = Name::try_from("a".to_string()).unwrap();
        let name: String = name.into();
        assert_eq!(name, "a".to_string());
    }

    #[test]
    fn test_deref() {
        let name = Name::try_from("a".to_string()).unwrap();
        let name: &str = &name;
        assert_eq!(name, "a");
    }
}