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
427
428
429
430
// Copyright 2022 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.

use {
    crate::PackageManifest,
    anyhow::{Context as _, Result},
    camino::Utf8PathBuf,
    fuchsia_merkle::Hash,
    fuchsia_url::RelativePackageUrl,
    serde::{de::Deserializer, Deserialize, Serialize},
    std::io,
};

/// Helper type for reading the build-time information based on the subpackage
/// declarations declared in a build file (such as the `subpackages` list in
/// a `fuchsia_package()` target, in `BUILD.gn`).
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SubpackagesBuildManifest(SubpackagesBuildManifestV0);

impl SubpackagesBuildManifest {
    /// Return the subpackage manifest entries.
    pub fn entries(&self) -> &[SubpackagesBuildManifestEntry] {
        &self.0.entries
    }

    /// Open up each entry in the manifest and return the subpackage url and hash.
    pub fn to_subpackages(&self) -> Result<Vec<(RelativePackageUrl, Hash, Utf8PathBuf)>> {
        let mut entries = Vec::with_capacity(self.0.entries.len());
        for entry in &self.0.entries {
            // Read the merkle from the package manifest.
            let manifest = PackageManifest::try_load_from(&entry.package_manifest_file)
                .with_context(|| format!("reading {}", &entry.package_manifest_file))?;
            let meta_far_blob = manifest
                .blobs()
                .iter()
                .find(|b| b.path == "meta/")
                .with_context(|| format!("finding meta/in {}", &entry.package_manifest_file))?;

            // Read the name from the package manifest, but override with the name if provided.
            let url = match &entry.kind {
                SubpackagesBuildManifestEntryKind::Url(url) => url.clone(),
                SubpackagesBuildManifestEntryKind::Empty
                | SubpackagesBuildManifestEntryKind::MetaPackageFile(_) => {
                    manifest.name().clone().into()
                }
            };

            entries.push((url, meta_far_blob.merkle, entry.package_manifest_file.clone()));
        }
        Ok(entries)
    }

    /// Deserializes a `SubpackagesBuildManifest` from json.
    pub fn deserialize(reader: impl io::BufRead) -> Result<Self> {
        Ok(SubpackagesBuildManifest(serde_json::from_reader(reader)?))
    }

    /// Serializes a `SubpackagesBuildManifest` to json.
    pub fn serialize(&self, writer: impl io::Write) -> Result<()> {
        Ok(serde_json::to_writer(writer, &self.0.entries)?)
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
struct SubpackagesBuildManifestV0 {
    entries: Vec<SubpackagesBuildManifestEntry>,
}

impl From<Vec<SubpackagesBuildManifestEntry>> for SubpackagesBuildManifest {
    fn from(entries: Vec<SubpackagesBuildManifestEntry>) -> Self {
        SubpackagesBuildManifest(SubpackagesBuildManifestV0 { entries })
    }
}

impl<'de> Deserialize<'de> for SubpackagesBuildManifestV0 {
    fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        #[derive(Deserialize)]
        struct Helper {
            #[serde(flatten)]
            helper_kind: Option<HelperKind>,
            package_manifest_file: Utf8PathBuf,
        }

        #[derive(Deserialize)]
        #[serde(untagged)]
        enum HelperKind {
            Name { name: RelativePackageUrl },
            File { meta_package_file: Utf8PathBuf },
        }

        let manifest_entries = Vec::<Helper>::deserialize(deserializer)?;

        let mut entries = vec![];
        for Helper { helper_kind, package_manifest_file } in manifest_entries {
            let kind = match helper_kind {
                Some(HelperKind::Name { name }) => SubpackagesBuildManifestEntryKind::Url(name),
                Some(HelperKind::File { meta_package_file }) => {
                    SubpackagesBuildManifestEntryKind::MetaPackageFile(meta_package_file)
                }
                None => SubpackagesBuildManifestEntryKind::Empty,
            };
            entries.push(SubpackagesBuildManifestEntry { kind, package_manifest_file });
        }

        Ok(SubpackagesBuildManifestV0 { entries })
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SubpackagesBuildManifestEntry {
    /// The subpackages build manifest entry's [EntryKind].
    pub kind: SubpackagesBuildManifestEntryKind,

    /// The package_manifest.json of the subpackage.
    pub package_manifest_file: Utf8PathBuf,
}

impl Serialize for SubpackagesBuildManifestEntry {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        #[derive(Serialize)]
        struct Helper<'a> {
            #[serde(skip_serializing_if = "Option::is_none")]
            name: Option<&'a RelativePackageUrl>,
            #[serde(skip_serializing_if = "Option::is_none")]
            meta_package_file: Option<&'a Utf8PathBuf>,
            package_manifest_file: &'a Utf8PathBuf,
        }
        let mut helper = Helper {
            name: None,
            meta_package_file: None,
            package_manifest_file: &self.package_manifest_file,
        };
        match &self.kind {
            SubpackagesBuildManifestEntryKind::Empty => {}
            SubpackagesBuildManifestEntryKind::Url(url) => helper.name = Some(url),
            SubpackagesBuildManifestEntryKind::MetaPackageFile(path) => {
                helper.meta_package_file = Some(path)
            }
        }
        helper.serialize(serializer)
    }
}

impl SubpackagesBuildManifestEntry {
    /// Construct a new [SubpackagesBuildManifestEntry].
    pub fn new(
        kind: SubpackagesBuildManifestEntryKind,
        package_manifest_file: Utf8PathBuf,
    ) -> Self {
        Self { kind, package_manifest_file }
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum SubpackagesBuildManifestEntryKind {
    Empty,
    Url(RelativePackageUrl),
    MetaPackageFile(Utf8PathBuf),
}

#[cfg(test)]
mod tests {
    use {
        super::*,
        crate::{MetaPackage, PackageBuilder},
        assert_matches::assert_matches,
        camino::Utf8Path,
        fuchsia_url::PackageName,
        serde_json::json,
        std::fs::File,
    };

    const FAKE_ABI_REVISION: version_history::AbiRevision =
        version_history::AbiRevision::from_u64(0xdc6e746980ce30a9);

    #[test]
    fn test_deserialize() {
        let tmp = tempfile::tempdir().unwrap();
        let dir = Utf8Path::from_path(tmp.path()).unwrap();

        // Generate a subpackages build manifest.
        let pkg1_name = PackageName::try_from("pkg1".to_string()).unwrap();
        let pkg1_url = RelativePackageUrl::from(pkg1_name.clone());
        let pkg1_package_manifest_file = dir.join("pkg1-package_manifest.json");
        let pkg1_meta_far_file = dir.join("pkg1-meta.far");

        let pkg2_name = PackageName::try_from("pkg2".to_string()).unwrap();
        let pkg2_url = RelativePackageUrl::from(pkg2_name.clone());
        let pkg2_meta_package_file = dir.join("pkg2-meta-package");
        let pkg2_package_manifest_file = dir.join("pkg2-package_manifest.json");
        let pkg2_meta_far_file = dir.join("pkg2-meta.far");

        // Write out all the files.
        let meta_package = MetaPackage::from_name_and_variant_zero(pkg2_name.clone());
        meta_package.serialize(File::create(&pkg2_meta_package_file).unwrap()).unwrap();

        let mut builder = PackageBuilder::new(&pkg1_name, FAKE_ABI_REVISION);
        builder.manifest_path(&pkg1_package_manifest_file);
        let manifest = builder.build(dir, pkg1_meta_far_file).unwrap();
        let pkg1_hash = manifest.blobs().iter().find(|b| b.path == "meta/").unwrap().merkle;

        let mut builder = PackageBuilder::new(&pkg2_name, FAKE_ABI_REVISION);
        builder.manifest_path(&pkg2_package_manifest_file);
        let manifest = builder.build(dir, pkg2_meta_far_file).unwrap();
        let pkg2_hash = manifest.blobs().iter().find(|b| b.path == "meta/").unwrap().merkle;

        // Make sure we can deserialize from the manifest format.
        let subpackages_build_manifest_path = dir.join("subpackages-build-manifest");
        serde_json::to_writer(
            File::create(&subpackages_build_manifest_path).unwrap(),
            &json!([
                {
                    "package_manifest_file": pkg1_package_manifest_file.to_string(),
                },
                {
                    "name": pkg1_name.to_string(),
                    "package_manifest_file": pkg1_package_manifest_file.to_string(),
                },
                {
                    "meta_package_file": pkg2_meta_package_file.to_string(),
                    "package_manifest_file": pkg2_package_manifest_file.to_string(),
                },
            ]),
        )
        .unwrap();

        let manifest = SubpackagesBuildManifest::deserialize(io::BufReader::new(
            File::open(&subpackages_build_manifest_path).unwrap(),
        ))
        .unwrap();

        assert_eq!(
            manifest.0.entries,
            vec![
                SubpackagesBuildManifestEntry {
                    kind: SubpackagesBuildManifestEntryKind::Empty,
                    package_manifest_file: pkg1_package_manifest_file.clone(),
                },
                SubpackagesBuildManifestEntry {
                    kind: SubpackagesBuildManifestEntryKind::Url(pkg1_url.clone()),
                    package_manifest_file: pkg1_package_manifest_file.clone(),
                },
                SubpackagesBuildManifestEntry {
                    kind: SubpackagesBuildManifestEntryKind::MetaPackageFile(
                        pkg2_meta_package_file
                    ),
                    package_manifest_file: pkg2_package_manifest_file.clone(),
                },
            ]
        );

        // Make sure we can convert the manifest into subpackages.
        assert_eq!(
            manifest.to_subpackages().unwrap(),
            vec![
                (pkg1_url.clone(), pkg1_hash, pkg1_package_manifest_file.clone()),
                (pkg1_url, pkg1_hash, pkg1_package_manifest_file),
                (pkg2_url, pkg2_hash, pkg2_package_manifest_file),
            ]
        );
    }

    #[test]
    fn test_meta_package_not_found() {
        let tmp = tempfile::tempdir().unwrap();
        let dir = Utf8Path::from_path(tmp.path()).unwrap();

        let pkg_meta_package_file = dir.join("pkg-meta-package");
        let pkg_package_manifest_file = dir.join("package_manifest.json");
        let pkg_meta_far_file = dir.join("meta.far");

        let subpackages_build_manifest_path = dir.join("subpackages-build-manifest");
        serde_json::to_writer(
            File::create(&subpackages_build_manifest_path).unwrap(),
            &json!([
                {
                    "meta_package_file": pkg_meta_package_file.to_string(),
                    "package_manifest_file": pkg_package_manifest_file.to_string(),
                },
            ]),
        )
        .unwrap();

        let manifest = SubpackagesBuildManifest::deserialize(io::BufReader::new(
            File::open(&subpackages_build_manifest_path).unwrap(),
        ))
        .unwrap();

        // We should error out if the package manifest file doesn't exist.
        assert_matches!(
            manifest.to_subpackages(),
            Err(err) if err.downcast_ref::<io::Error>().unwrap().kind() == io::ErrorKind::NotFound
        );

        // It should work once we write the files.
        let mut builder = PackageBuilder::new("pkg", FAKE_ABI_REVISION);
        builder.manifest_path(&pkg_package_manifest_file);
        let package_manifest = builder.build(dir, pkg_meta_far_file).unwrap();
        let pkg_hash = package_manifest.blobs().iter().find(|b| b.path == "meta/").unwrap().merkle;

        let pkg_name = PackageName::try_from("pkg".to_string()).unwrap();
        MetaPackage::from_name_and_variant_zero(pkg_name.clone())
            .serialize(File::create(&pkg_meta_package_file).unwrap())
            .unwrap();
        let pkg_url = RelativePackageUrl::from(pkg_name);

        assert_eq!(
            manifest.to_subpackages().unwrap(),
            vec![(pkg_url, pkg_hash, pkg_package_manifest_file)]
        );
    }

    #[test]
    fn test_merkle_file_not_found() {
        let tmp = tempfile::tempdir().unwrap();
        let dir = Utf8Path::from_path(tmp.path()).unwrap();

        let pkg_name = PackageName::try_from("pkg".to_string()).unwrap();
        let pkg_url = RelativePackageUrl::from(pkg_name);
        let pkg_package_manifest_file = dir.join("package_manifest.json");
        let pkg_meta_far_file = dir.join("meta.far");

        let subpackages_build_manifest_path = dir.join("subpackages-build-manifest");
        serde_json::to_writer(
            File::create(&subpackages_build_manifest_path).unwrap(),
            &json!([
                {
                    "name": pkg_url.to_string(),
                    "package_manifest_file": pkg_package_manifest_file.to_string(),
                },
            ]),
        )
        .unwrap();

        let manifest = SubpackagesBuildManifest::deserialize(io::BufReader::new(
            File::open(&subpackages_build_manifest_path).unwrap(),
        ))
        .unwrap();

        // We should error out if the package manifest file doesn't exist.
        assert_matches!(
            manifest.to_subpackages(),
            Err(err) if err.downcast_ref::<io::Error>().unwrap().kind() == io::ErrorKind::NotFound
        );

        // It should work once we write the files.
        let mut builder = PackageBuilder::new("pkg", FAKE_ABI_REVISION);
        builder.manifest_path(&pkg_package_manifest_file);
        let package_manifest = builder.build(dir, pkg_meta_far_file).unwrap();
        let pkg_hash = package_manifest.blobs().iter().find(|b| b.path == "meta/").unwrap().merkle;

        assert_eq!(
            manifest.to_subpackages().unwrap(),
            vec![(pkg_url, pkg_hash, pkg_package_manifest_file)]
        );
    }

    #[test]
    fn test_serialize() {
        let entries = vec![
            SubpackagesBuildManifestEntry::new(
                SubpackagesBuildManifestEntryKind::Empty,
                "package-manifest-path-0".into(),
            ),
            SubpackagesBuildManifestEntry::new(
                SubpackagesBuildManifestEntryKind::Url("subpackage-name".parse().unwrap()),
                "package-manifest-path-0".into(),
            ),
            SubpackagesBuildManifestEntry::new(
                SubpackagesBuildManifestEntryKind::MetaPackageFile("file-path".into()),
                "package-manifest-path-1".into(),
            ),
        ];
        let manifest = SubpackagesBuildManifest::from(entries);

        let mut bytes = vec![];
        let () = manifest.serialize(&mut bytes).unwrap();
        let actual_json: serde_json::Value = serde_json::from_slice(&bytes).unwrap();

        assert_eq!(
            actual_json,
            json!([
                {
                    "package_manifest_file": "package-manifest-path-0"
                },
                {
                    "name": "subpackage-name",
                    "package_manifest_file": "package-manifest-path-0"
                },
                {
                    "meta_package_file": "file-path",
                    "package_manifest_file": "package-manifest-path-1"
                },
            ])
        );
    }

    #[test]
    fn test_serialize_deserialize() {
        let entries = vec![
            SubpackagesBuildManifestEntry::new(
                SubpackagesBuildManifestEntryKind::Empty,
                "package-manifest-path-0".into(),
            ),
            SubpackagesBuildManifestEntry::new(
                SubpackagesBuildManifestEntryKind::Url("subpackage-name".parse().unwrap()),
                "package-manifest-path-0".into(),
            ),
            SubpackagesBuildManifestEntry::new(
                SubpackagesBuildManifestEntryKind::MetaPackageFile("file-path".into()),
                "package-manifest-path-1".into(),
            ),
        ];
        let manifest = SubpackagesBuildManifest::from(entries);

        let mut bytes = vec![];
        let () = manifest.serialize(&mut bytes).unwrap();
        let deserialized =
            SubpackagesBuildManifest::deserialize(io::BufReader::new(bytes.as_slice())).unwrap();

        assert_eq!(deserialized, manifest);
    }
}