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
// Copyright 2019 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 {
    fuchsia_merkle::Hash,
    fuchsia_url::errors::{PackagePathSegmentError, ResourcePathError},
    std::{io, path::PathBuf},
    tempfile::PersistError,
    thiserror::Error,
};

#[derive(Debug, Error)]
pub enum PackageBuildManifestError {
    #[error("manifest contains an invalid resource path '{}'.", path)]
    ResourcePath {
        #[source]
        cause: ResourcePathError,
        path: String,
    },

    #[error("attempted to deserialize creation manifest from malformed json")]
    Json(#[from] serde_json::Error),

    #[error("package external content cannot be in 'meta/' directory: {}", path)]
    ExternalContentInMetaDirectory { path: String },

    #[error("package far content must be in 'meta/' directory: {}", path)]
    FarContentNotInMetaDirectory { path: String },

    #[error("entry has no '=': '{}'", entry)]
    EntryHasNoEqualsSign { entry: String },

    #[error("duplicate resource path: '{}'", path)]
    DuplicateResourcePath { path: String },

    #[error("io error")]
    IoError(#[from] io::Error),

    #[error("error occurred when traverse the contents of a directory")]
    WalkDir(#[from] walkdir::Error),

    #[error("error occurred when strip prefix from the path")]
    StripPrefixError(#[from] std::path::StripPrefixError),

    #[error("the resource path is empty")]
    EmptyResourcePath,

    #[error("manifest contains an invalid file path '{}'.", path.display())]
    InvalidFileType { path: PathBuf },

    #[error("file directory collision at: {:?}", path)]
    FileDirectoryCollision { path: String },
}

#[derive(Debug, Error)]
pub enum PackageManifestError {
    #[error("package contains an invalid blob source path '{source_path:?}'. {merkle}")]
    InvalidBlobPath { merkle: Hash, source_path: PathBuf },

    #[error("package contains an invalid subpackage path '{path:?}'. {merkle}")]
    InvalidSubpackagePath { merkle: Hash, path: PathBuf },

    #[error("io error")]
    IoError(#[from] io::Error),

    #[error("io error {cause}: '{path}'")]
    IoErrorWithPath { cause: io::Error, path: PathBuf },

    #[error("meta contents")]
    MetaContents(#[from] MetaContentsError),

    #[error("meta package")]
    MetaPackage(#[from] MetaPackageError),

    #[error("meta subpackages")]
    MetaSubpackages(#[from] MetaSubpackagesError),

    #[error("archive")]
    Archive(#[from] fuchsia_archive::Error),

    #[error("writing to relative path failed")]
    RelativeWrite(#[source] anyhow::Error),

    #[error("persisting to file failed: '{path}'")]
    Persist {
        #[source]
        cause: PersistError,
        path: PathBuf,
    },

    #[error("decompressing delivery blob failed: '{path}'")]
    DecompressDeliveryBlob {
        #[source]
        cause: delivery_blob::DecompressError,
        path: PathBuf,
    },
}

#[derive(Debug, Error)]
pub enum MetaContentsError {
    #[error("invalid resource path: '{:?}'", path)]
    InvalidResourcePath {
        #[source]
        cause: ResourcePathError,
        path: String,
    },

    #[error("package external content cannot be in 'meta/' directory: '{:?}'", path)]
    ExternalContentInMetaDirectory { path: String },

    #[error("entry has no '=': '{:?}'", entry)]
    EntryHasNoEqualsSign { entry: String },

    #[error("duplicate resource path: '{:?}'", path)]
    DuplicateResourcePath { path: String },

    #[error("io error")]
    IoError(#[from] io::Error),

    #[error("invalid hash")]
    ParseHash(#[from] fuchsia_hash::ParseHashError),

    #[error("collision between a file and a directory at path: '{:?}'", path)]
    FileDirectoryCollision { path: String },
}

#[derive(Debug, Error)]
pub enum MetaPackageError {
    #[error("invalid package name")]
    PackageName(#[source] PackagePathSegmentError),

    #[error("invalid package variant")]
    PackageVariant(#[source] PackagePathSegmentError),

    #[error("attempted to deserialize meta/package from malformed json: {}", _0)]
    Json(#[from] serde_json::Error),

    #[error("meta/package file not found")]
    MetaPackageMissing,
}

#[derive(Debug, Error)]
pub enum MetaSubpackagesError {
    #[error("invalid subpackage name: '{:?}'", name)]
    InvalidSubpackageName {
        #[source]
        cause: PackagePathSegmentError,
        name: String,
    },

    #[error("attempted to deserialize {} from malformed json", crate::MetaSubpackages::PATH)]
    Json(#[from] serde_json::Error),

    #[error("duplicate subpackage name: '{:?}'", name)]
    DuplicateSubpackageName { name: String },

    #[error("io error")]
    IoError(#[from] io::Error),

    #[error("invalid hash")]
    ParseHash(#[from] fuchsia_hash::ParseHashError),
}

#[derive(Debug, Error)]
pub enum BuildError {
    #[error("io: {}", _0)]
    IoError(#[from] io::Error),

    #[error("{cause}: '{path}'")]
    IoErrorWithPath { cause: io::Error, path: PathBuf },

    #[error("meta contents")]
    MetaContents(#[from] MetaContentsError),

    #[error("meta package")]
    MetaPackage(#[from] MetaPackageError),

    #[error("meta subpackages")]
    MetaSubpackages(#[from] MetaSubpackagesError),

    #[error("package name")]
    PackageName(#[source] PackagePathSegmentError),

    #[error("package manifest")]
    PackageManifest(#[from] PackageManifestError),

    #[error(
        "the creation manifest contained a resource path that conflicts with a generated resource path: '{}'",
        conflicting_resource_path
    )]
    ConflictingResource { conflicting_resource_path: String },

    #[error("archive write")]
    ArchiveWrite(#[from] fuchsia_archive::Error),
}

impl From<(io::Error, PathBuf)> for BuildError {
    fn from(pair: (io::Error, PathBuf)) -> Self {
        Self::IoErrorWithPath { cause: pair.0, path: pair.1 }
    }
}

#[derive(Debug, Error, Eq, PartialEq)]
pub enum ParsePackagePathError {
    #[error("package path has more than two segments")]
    TooManySegments,

    #[error("package path has fewer than two segments")]
    TooFewSegments,

    #[error("invalid package name")]
    PackageName(#[source] PackagePathSegmentError),

    #[error("invalid package variant")]
    PackageVariant(#[source] PackagePathSegmentError),
}