alpm_db/db/error.rs
1//! Error handling for [alpm-db] creation, access and writing.
2//!
3//! [alpm-db]: https://alpm.archlinux.page/specifications/alpm-db.7.html
4
5use std::{fs::TryLockError, path::PathBuf};
6
7use alpm_types::EntryName;
8use fluent_i18n::t;
9use uuid::Uuid;
10
11use crate::db::DIR_MODE;
12
13/// The error that can occur when accessing or writing an [alpm-db].
14///
15/// [alpm-db]: https://alpm.archlinux.page/specifications/alpm-db.7.html
16#[derive(Debug, thiserror::Error)]
17pub enum Error {
18 /// Encountered a non-empty directory when attempting to create a new [alpm-db].
19 ///
20 /// [alpm-db]: https://alpm.archlinux.page/specifications/alpm-db.7.html
21 #[error(
22 "{msg}",
23 msg = t!("error-database-creation-not-empty", {
24 "path" => .path.display().to_string(),
25 })
26 )]
27 DatabaseCreationNotEmpty {
28 /// The path of the database directory, that is not empty.
29 path: PathBuf,
30 },
31
32 /// An [alpm-db] has no transaction.
33 ///
34 /// [alpm-db]: https://alpm.archlinux.page/specifications/alpm-db.7.html
35 #[error(
36 "{msg}",
37 msg = t!("error-database-has-no-transaction", {
38 "path" => .path.display().to_string(),
39 })
40 )]
41 DatabaseHasNoTransaction {
42 /// The path of the database.
43 path: PathBuf,
44 },
45
46 /// An [alpm-db] has a transaction and a write lock already.
47 ///
48 /// [alpm-db]: https://alpm.archlinux.page/specifications/alpm-db.7.html
49 #[error(
50 "{msg}",
51 msg = t!("error-database-has-locked-transaction-already", {
52 "path" => .path.display().to_string(),
53 "lock_file" => .lock_file.display().to_string(),
54 "uuid" => .uuid.to_string(),
55 })
56 )]
57 DatabaseHasLockedTransactionAlready {
58 /// The path of the database.
59 path: PathBuf,
60
61 /// The path of the lock file for the database.
62 lock_file: PathBuf,
63
64 /// The UUID of the transaction.
65 uuid: Uuid,
66 },
67
68 /// An [alpm-db] is locked.
69 ///
70 /// [alpm-db]: https://alpm.archlinux.page/specifications/alpm-db.7.html
71 #[error(
72 "{msg}",
73 msg = t!("error-database-is-locked", {
74 "path" => .path.display().to_string(),
75 "lock_file" => .lock_file.display().to_string(),
76 })
77 )]
78 DatabaseIsLocked {
79 /// The path of the database, that is locked.
80 path: PathBuf,
81
82 /// The path of lock file for the database.
83 lock_file: PathBuf,
84 },
85
86 /// An [alpm-db] root path is not a directory.
87 ///
88 /// [alpm-db]: https://alpm.archlinux.page/specifications/alpm-db.7.html
89 #[error("{msg}", msg = t!("error-database-root-is-not-a-directory", { "path" => .path.display().to_string() }))]
90 DatabaseIsNotADirectory {
91 /// The root path of the database, that is not a directory.
92 path: PathBuf,
93 },
94
95 /// A file lock for the [alpm-db] lock file cannot be created.
96 ///
97 /// [alpm-db]: https://alpm.archlinux.page/specifications/alpm-db.7.html
98 #[error("{msg}", msg = t!("error-database-exclusive-lock-file-failed", { "path" => .path, "source" => .source.to_string() }))]
99 DatabaseFailedExclusiveLockFile {
100 /// The path of the database lock file that cannot be locked.
101 path: PathBuf,
102
103 /// The error source.
104 source: TryLockError,
105 },
106
107 /// The permissions for the root directory of an [alpm-db] differ from the defaults.
108 ///
109 /// [alpm-db]: https://alpm.archlinux.page/specifications/alpm-db.7.html
110 #[error(
111 "{msg}",
112 msg = t!("error-database-wrong-permissions", {
113 "path" => .path.display().to_string(),
114 "defaults" => format!("{:o}", DIR_MODE & 0o777),
115 "permissions" => format!("{:o}", .permissions & 0o777),
116 })
117 )]
118 DatabaseWrongPermissions {
119 /// The path of the database, that has wrong permissions.
120 path: PathBuf,
121
122 /// The permissions, that do not align with the defaults.
123 permissions: u32,
124 },
125
126 /// A component (`desc`, `files`, `install` or `mtree`) in an [alpm-db] entry is not a file.
127 ///
128 /// [alpm-db]: https://alpm.archlinux.page/specifications/alpm-db.7.html
129 #[error("{msg}", msg = t!("error-database-entry-component-is-not-a-file", { "path" => .path }))]
130 EntryComponentIsNotAFile {
131 /// The path of the entry component, that is not a file.
132 path: PathBuf,
133 },
134
135 /// The [`EntryName`] of an [alpm-db] entry and its [alpm-db-desc] component do not match.
136 ///
137 /// [alpm-db]: https://alpm.archlinux.page/specifications/alpm-db.7.html
138 /// [alpm-db]: https://alpm.archlinux.page/specifications/alpm-db-desc.5.html
139 #[error(
140 "{msg}",
141 msg = t!("error-database-entry-component-name-mismatch", {
142 "entry_name" => .entry_name.to_string(),
143 "component_entry_name" => .component_entry_name.to_string(),
144 })
145 )]
146 EntryComponentNameMismatch {
147 /// The entry name of the entry.
148 entry_name: String,
149
150 /// The entry name of the entry component.
151 component_entry_name: String,
152 },
153
154 /// An entry of an [alpm-db] exists already.
155 ///
156 /// [alpm-db]: https://alpm.archlinux.page/specifications/alpm-db.7.html
157 #[error("{msg}", msg = t!("error-database-entry-already-exists", { "name" => .name.to_string() }))]
158 EntryExists {
159 /// The name of the entry, that exists already.
160 name: EntryName,
161 },
162
163 /// An entry of an [alpm-db] does not exist.
164 ///
165 /// [alpm-db]: https://alpm.archlinux.page/specifications/alpm-db.7.html
166 #[error("{msg}", msg = t!("error-database-entry-does-not-exist", { "name" => .name.to_string() }))]
167 EntryDoesNotExist {
168 /// The name of the entry, that does not exist.
169 name: EntryName,
170 },
171
172 /// An [alpm-db] entry path is not a directory.
173 ///
174 /// [alpm-db]: https://alpm.archlinux.page/specifications/alpm-db.7.html
175 #[error("{msg}", msg = t!("error-database-entry-is-not-a-directory", { "path" => .path.display().to_string() }))]
176 EntryIsNotADirectory {
177 /// The path of the database entry, that is not a directory.
178 path: PathBuf,
179 },
180
181 /// An [alpm-db] transaction data directory path is not a directory.
182 ///
183 /// [alpm-db]: https://alpm.archlinux.page/specifications/alpm-db.7.html
184 #[error("{msg}", msg = t!("error-transaction-data-dir-is-not-a-directory", { "path" => .path.display().to_string() }))]
185 TransactionDataDirIsNotADirectory {
186 /// The root path of the database, that is not a directory.
187 path: PathBuf,
188 },
189
190 /// One or more errors occurred while preparing the temporary data for an [alpm-db] transaction.
191 ///
192 /// [alpm-db]: https://alpm.archlinux.page/specifications/alpm-db.7.html
193 #[error(
194 "{msg}",
195 msg = t!("error-transaction-temporary-data-errors",{
196 "path" => .path.display().to_string(),
197 "uuid" => .uuid.to_string(),
198 "errors" => .errors.join("\n\n"),
199 })
200 )]
201 TransactionTemporaryDataErrors {
202 /// The root path of the database.
203 path: PathBuf,
204
205 /// The transaction UUID.
206 uuid: Uuid,
207
208 /// The string representation of the errors, that occurred.
209 errors: Vec<String>,
210 },
211}