bpt 0.1.6

Bedrock Linux package manager
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
use crate::{constant::*, error::*, io::*, location::*, metadata::*, str::*};
use camino::Utf8PathBuf;
use std::{
    collections::HashSet,
    fs::{File, rename},
    io::{ErrorKind, Write},
    str::FromStr,
};

pub struct World {
    // World file may not exist, in which case we have nothing to lock
    _lock: Option<File>,
    // Store the path so we can update the file without re-requesting the root
    path: Utf8PathBuf,
    // Store the lines of the file, mapped to their package.
    // This allows us to write the file back out without losing whitespace or comments.
    contents: Vec<(String, Option<PartId>)>,
    // The actual list of entries
    entries: HashSet<PartId>,
    // Whether we hold an exclusive lock and can save
    writable: bool,
}

impl World {
    pub fn from_root_path_ro(root: &RootDir) -> Result<Self, Err> {
        Self::new(root, false)
    }

    pub fn from_root_path_rw(root: &RootDir) -> Result<Self, Err> {
        Self::new(root, true)
    }

    fn new(root: &RootDir, writable: bool) -> Result<Self, Err> {
        let path = root.as_path().join(WORLD_PATH);

        if writable {
            let dir_path = path.as_str().strip_filename();
            std::fs::create_dir_all(dir_path)
                .map_err(|e| Err::CreateDir(dir_path.to_string(), e))?;
        }

        let mut file = match if writable {
            File::create_or_open_rw(&path)
        } else {
            File::open_ro(&path)
        } {
            Ok(file) => file,
            Err(Err::Open(_, e)) if e.kind() == ErrorKind::NotFound && !writable => {
                return Ok(Self {
                    _lock: None,
                    path,
                    contents: Vec::new(),
                    entries: HashSet::new(),
                    writable,
                });
            }
            Err(e) => return Err(e),
        };

        if writable {
            file.lock_rw("world file").loc(path.clone())?;
        } else {
            file.lock_ro("world file").loc(path.clone())?;
        }

        let mut contents = Vec::new();
        let mut entries = HashSet::new();

        for line in file.read_small_file_string().loc(&path)?.lines() {
            let precomment = line.strip_comment();

            if precomment.is_empty() {
                contents.push((line.to_owned(), None));
            } else {
                let partid = PartId::from_str(precomment)?;
                entries.insert(partid.clone());
                contents.push((line.to_owned(), Some(partid)));
            }
        }

        Ok(Self {
            _lock: Some(file),
            path,
            contents,
            entries,
            writable,
        })
    }

    pub fn get_match(&self, pkgid: &PkgId) -> Option<&PartId> {
        self.entries.iter().find(|partid| partid.matches(pkgid))
    }

    pub fn contains_match(&self, pkgid: &PkgId) -> bool {
        self.get_match(pkgid).is_some()
    }

    #[cfg(test)]
    pub fn contains_entry(&self, partid: &PartId) -> bool {
        self.entries.contains(partid)
    }

    pub fn entries(&self) -> &HashSet<PartId> {
        &self.entries
    }

    pub fn replace_entries(&mut self, entries: HashSet<PartId>) {
        self.entries = entries;
    }

    // TODO: Remove when we have actual update mechanisms
    #[cfg(test)]
    pub fn entries_mut(&mut self) -> &mut HashSet<PartId> {
        &mut self.entries
    }

    pub fn save(&mut self) -> Result<(), Err> {
        debug_assert!(self.writable, "save() called on read-only World");

        let dir_path = self.path.as_str().strip_filename();
        std::fs::create_dir_all(dir_path).map_err(|e| Err::CreateDir(dir_path.to_string(), e))?;

        let tmp_path = self.path.with_file_name(".world-new");
        let _ = std::fs::remove_file(&tmp_path);
        let mut file = File::create_rw(&tmp_path)?;

        // Remove any lines that are no longer in the entries list
        self.contents.retain(|(_, partid)| {
            if let Some(partid) = partid {
                self.entries.contains(partid)
            } else {
                true
            }
        });

        // Add any new entries to the buffer
        for entry in &self.entries {
            if !self
                .contents
                .iter()
                .filter_map(|(_, partid)| partid.as_ref())
                .any(|p| p == entry)
            {
                self.contents.push((entry.to_string(), Some(entry.clone())));
            }
        }

        // Write out new buffer
        for (line, _) in &self.contents {
            writeln!(file, "{line}").map_err(|e| Err::Write(tmp_path.to_string(), e))?;
        }

        rename(&tmp_path, &self.path)
            .map_err(|e| Err::Rename(tmp_path.clone().into(), self.path.clone().into(), e))?;

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use crate::{constant::*, testutil::unit_test_tmp_dir};
    use std::str::FromStr;

    use super::*;

    fn test_root(name: &str) -> (Utf8PathBuf, RootDir) {
        let tmp = unit_test_tmp_dir("world", name);
        let root = RootDir::from_path(&tmp);
        (tmp, root)
    }

    fn write_world(root: &RootDir, content: &str) {
        let world_dir = root.as_path().join("etc/bpt");
        std::fs::create_dir_all(&world_dir).unwrap();
        std::fs::write(root.as_path().join(WORLD_PATH), content).unwrap();
    }

    #[test]
    fn ro_missing_file_returns_empty() {
        let (_tmp, root) = test_root("ro_missing_file_returns_empty");
        let world = World::from_root_path_ro(&root).unwrap();
        assert!(world.entries().is_empty());
    }

    #[test]
    fn ro_empty_file() {
        let (_tmp, root) = test_root("ro_empty_file");
        write_world(&root, "");
        let world = World::from_root_path_ro(&root).unwrap();
        assert!(world.entries().is_empty());
    }

    #[test]
    fn ro_parses_entries() {
        let (_tmp, root) = test_root("ro_parses_entries");
        write_world(&root, "bpt@1.0.0:x86_64\ngcc@11.2.0:aarch64\n");
        let world = World::from_root_path_ro(&root).unwrap();
        assert_eq!(world.entries().len(), 2);
        assert!(world.contains_entry(&PartId::from_str("bpt@1.0.0:x86_64").unwrap()));
        assert!(world.contains_entry(&PartId::from_str("gcc@11.2.0:aarch64").unwrap()));
    }

    #[test]
    fn ro_preserves_comments_and_blanks() {
        let (_tmp, root) = test_root("ro_preserves_comments_and_blanks");
        write_world(&root, "# header comment\n\nbpt@1.0.0:x86_64\n");
        let world = World::from_root_path_ro(&root).unwrap();
        assert_eq!(world.entries().len(), 1);
        assert_eq!(world.contents.len(), 3);
        assert!(world.contents[0].1.is_none());
        assert!(world.contents[1].1.is_none());
        assert!(world.contents[2].1.is_some());
    }

    #[test]
    fn ro_strips_inline_comments() {
        let (_tmp, root) = test_root("ro_strips_inline_comments");
        write_world(&root, "bpt # explicitly requested\n");
        let world = World::from_root_path_ro(&root).unwrap();
        assert_eq!(world.entries().len(), 1);
        assert!(world.contains_entry(&PartId::from_str("bpt").unwrap()));
    }

    #[test]
    fn ro_name_only_partid() {
        let (_tmp, root) = test_root("ro_name_only_partid");
        write_world(&root, "bpt\n");
        let world = World::from_root_path_ro(&root).unwrap();
        let partid = PartId::from_str("bpt").unwrap();
        assert!(world.contains_entry(&partid));
    }

    #[test]
    fn contains_match_name_only_matches_any_version_arch() {
        let (_tmp, root) = test_root("contains_match_name_only_matches_any_version_arch");
        write_world(&root, "bpt\n");
        let world = World::from_root_path_ro(&root).unwrap();
        let pkgid = PkgId::new(
            PkgName::try_from("bpt").unwrap(),
            PkgVer::try_from("2.0.0").unwrap(),
            Arch::from_str("aarch64").unwrap(),
        );
        assert!(world.contains_match(&pkgid));
    }

    #[test]
    fn contains_match_full_partid_requires_exact() {
        let (_tmp, root) = test_root("contains_match_full_partid_requires_exact");
        write_world(&root, "bpt@1.0.0:x86_64\n");
        let world = World::from_root_path_ro(&root).unwrap();

        let matching = PkgId::new(
            PkgName::try_from("bpt").unwrap(),
            PkgVer::try_from("1.0.0").unwrap(),
            Arch::from_str("x86_64").unwrap(),
        );
        assert!(world.contains_match(&matching));

        let wrong_ver = PkgId::new(
            PkgName::try_from("bpt").unwrap(),
            PkgVer::try_from("2.0.0").unwrap(),
            Arch::from_str("x86_64").unwrap(),
        );
        assert!(!world.contains_match(&wrong_ver));

        let wrong_arch = PkgId::new(
            PkgName::try_from("bpt").unwrap(),
            PkgVer::try_from("1.0.0").unwrap(),
            Arch::from_str("aarch64").unwrap(),
        );
        assert!(!world.contains_match(&wrong_arch));
    }

    #[test]
    fn contains_match_no_match_for_different_pkgname() {
        let (_tmp, root) = test_root("contains_match_no_match_for_different_pkgname");
        write_world(&root, "bpt\n");
        let world = World::from_root_path_ro(&root).unwrap();
        let pkgid = PkgId::new(
            PkgName::try_from("gcc").unwrap(),
            PkgVer::try_from("1.0.0").unwrap(),
            Arch::from_str("x86_64").unwrap(),
        );
        assert!(!world.contains_match(&pkgid));
    }

    #[test]
    fn get_match_returns_matching_partid() {
        let (_tmp, root) = test_root("get_match_returns_matching_partid");
        write_world(&root, "bpt@1.0.0:x86_64\n");
        let world = World::from_root_path_ro(&root).unwrap();
        let pkgid = PkgId::new(
            PkgName::try_from("bpt").unwrap(),
            PkgVer::try_from("1.0.0").unwrap(),
            Arch::from_str("x86_64").unwrap(),
        );
        let matched = world.get_match(&pkgid).unwrap();
        assert_eq!(*matched, PartId::from_str("bpt@1.0.0:x86_64").unwrap());
    }

    #[test]
    fn get_match_returns_none_when_absent() {
        let (_tmp, root) = test_root("get_match_returns_none_when_absent");
        write_world(&root, "bpt\n");
        let world = World::from_root_path_ro(&root).unwrap();
        let pkgid = PkgId::new(
            PkgName::try_from("gcc").unwrap(),
            PkgVer::try_from("1.0.0").unwrap(),
            Arch::from_str("x86_64").unwrap(),
        );
        assert!(world.get_match(&pkgid).is_none());
    }

    #[test]
    fn rw_creates_file_if_missing() {
        let (_tmp, root) = test_root("rw_creates_file_if_missing");
        let world_dir = root.as_path().join("etc/bpt");
        std::fs::create_dir_all(&world_dir).unwrap();
        let world = World::from_root_path_rw(&root).unwrap();
        assert!(world.entries().is_empty());
        assert!(world.writable);
    }

    #[test]
    fn rw_missing_parent_dirs_returns_empty() {
        let (_tmp, root) = test_root("rw_missing_parent_dirs_returns_empty");
        let world = World::from_root_path_rw(&root).unwrap();
        assert!(world.entries().is_empty());
        assert!(world.writable);
        assert!(root.as_path().join("etc/bpt").is_dir());
    }

    #[test]
    fn rw_reads_existing_entries() {
        let (_tmp, root) = test_root("rw_reads_existing_entries");
        write_world(&root, "bpt@1.0.0:x86_64\n");
        let world = World::from_root_path_rw(&root).unwrap();
        assert_eq!(world.entries().len(), 1);
        assert!(world.contains_entry(&PartId::from_str("bpt@1.0.0:x86_64").unwrap()));
    }

    #[test]
    fn save_writes_entries_to_disk() {
        let (_tmp, root) = test_root("save_writes_entries_to_disk");
        write_world(&root, "");
        let mut world = World::from_root_path_rw(&root).unwrap();
        world
            .entries_mut()
            .insert(PartId::from_str("bpt@1.0.0:x86_64").unwrap());
        world.save().unwrap();

        let reloaded = World::from_root_path_ro(&root).unwrap();
        assert!(reloaded.contains_entry(&PartId::from_str("bpt@1.0.0:x86_64").unwrap()));
    }

    #[test]
    fn save_creates_world_file_if_missing() {
        let (_tmp, root) = test_root("save_creates_world_file_if_missing");
        let mut world = World::from_root_path_rw(&root).unwrap();
        world
            .entries_mut()
            .insert(PartId::from_str("bpt@1.0.0:x86_64").unwrap());
        world.save().unwrap();

        let world_path = root.as_path().join(WORLD_PATH);
        assert!(world_path.exists());

        let reloaded = World::from_root_path_ro(&root).unwrap();
        assert!(reloaded.contains_entry(&PartId::from_str("bpt@1.0.0:x86_64").unwrap()));
    }

    #[test]
    fn save_removes_deleted_entries() {
        let (_tmp, root) = test_root("save_removes_deleted_entries");
        write_world(&root, "bpt@1.0.0:x86_64\ngcc@11.2.0:aarch64\n");
        let mut world = World::from_root_path_rw(&root).unwrap();
        world
            .entries_mut()
            .remove(&PartId::from_str("gcc@11.2.0:aarch64").unwrap());
        world.save().unwrap();

        let reloaded = World::from_root_path_ro(&root).unwrap();
        assert_eq!(reloaded.entries().len(), 1);
        assert!(reloaded.contains_entry(&PartId::from_str("bpt@1.0.0:x86_64").unwrap()));
        assert!(!reloaded.contains_entry(&PartId::from_str("gcc@11.2.0:aarch64").unwrap()));
    }

    #[test]
    fn save_preserves_comments() {
        let (_tmp, root) = test_root("save_preserves_comments");
        write_world(&root, "# keep this\nbpt@1.0.0:x86_64\n");
        let mut world = World::from_root_path_rw(&root).unwrap();
        world.save().unwrap();

        let content = std::fs::read_to_string(root.as_path().join(WORLD_PATH)).unwrap();
        assert!(content.contains("# keep this"));
        assert!(content.contains("bpt@1.0.0:x86_64"));
    }

    #[test]
    fn save_roundtrip_preserves_inline_comments() {
        let (_tmp, root) = test_root("save_roundtrip_preserves_inline_comments");
        write_world(&root, "bpt # explicitly requested\n");
        let mut world = World::from_root_path_rw(&root).unwrap();
        world.save().unwrap();

        let content = std::fs::read_to_string(root.as_path().join(WORLD_PATH)).unwrap();
        assert!(content.contains("bpt # explicitly requested"));
    }

    #[test]
    fn ro_rejects_invalid_partid() {
        let (_tmp, root) = test_root("ro_rejects_invalid_partid");
        write_world(&root, "bpt@1.0.0:badarch\n");
        assert!(World::from_root_path_ro(&root).is_err());
    }

    #[test]
    fn ro_deduplicates_entries() {
        let (_tmp, root) = test_root("ro_deduplicates_entries");
        write_world(&root, "bpt@1.0.0:x86_64\nbpt@1.0.0:x86_64\n");
        let world = World::from_root_path_ro(&root).unwrap();
        assert_eq!(world.entries().len(), 1);
    }
}