Skip to main content

zipatch_rs/chunk/
ddir.rs

1use binrw::BinRead;
2
3use super::util::read_null_trimmed_utf8;
4
5/// `DELD` chunk: remove a directory under the game install root.
6///
7/// When applied, the patcher calls `remove_dir` for `<game_root>/<name>`.
8/// Unlike `ADIR`, only an empty directory can be deleted this way; if the
9/// directory is non-empty the OS returns an error. If
10/// [`crate::ApplySession::ignore_missing`] is set, a missing directory is
11/// silently skipped instead of returning an error.
12///
13/// Like `ADIR`, `DELD` chunks are rare in modern patches.
14///
15/// # Wire format
16///
17/// ```text
18/// [name_len: u32 BE] [name: name_len bytes, NUL-padded]
19/// ```
20///
21/// Identical layout to [`crate::chunk::adir::AddDirectory`]. `name_len`
22/// includes any trailing NUL padding bytes; the parsed field has them stripped.
23///
24/// # Errors
25///
26/// Parsing fails with [`crate::ParseError::Decode`] if:
27/// - the body is too short to contain the `name_len` field or the declared
28///   number of name bytes (truncated input), or
29/// - the name bytes are not valid UTF-8.
30#[derive(BinRead, Debug, Clone, PartialEq, Eq)]
31#[br(big)]
32pub struct DeleteDirectory {
33    /// Directory path relative to the game install root.
34    ///
35    /// Encoded as UTF-8 on the wire, length-prefixed by a `u32` big-endian
36    /// byte count. Trailing NUL bytes used as alignment padding are stripped
37    /// before this field is populated. Example: `"sqpack/ex3"`.
38    #[br(parse_with = read_null_trimmed_utf8)]
39    pub name: String,
40}
41
42pub(crate) fn parse(body: &[u8]) -> crate::ParseResult<DeleteDirectory> {
43    super::util::parse_be(body)
44}
45
46#[cfg(test)]
47mod tests {
48    use super::parse;
49
50    #[test]
51    fn parses_delete_directory() {
52        let mut body = Vec::new();
53        body.extend_from_slice(&4u32.to_be_bytes());
54        body.extend_from_slice(b"data");
55        assert_eq!(parse(&body).unwrap().name, "data");
56    }
57}