forked from rust-lang/docs.rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelete.rs
412 lines (374 loc) · 13.6 KB
/
delete.rs
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
use crate::error::Result;
use crate::storage::{rustdoc_archive_path, source_archive_path, Storage};
use crate::Config;
use anyhow::Context as _;
use fn_error_context::context;
use postgres::Client;
use std::fs;
/// List of directories in docs.rs's underlying storage (either the database or S3) containing a
/// subdirectory named after the crate. Those subdirectories will be deleted.
static LIBRARY_STORAGE_PATHS_TO_DELETE: &[&str] = &["rustdoc", "sources"];
static BINARY_STORAGE_PATHS_TO_DELETE: &[&str] = &["sources"];
#[derive(Debug, thiserror::Error)]
enum CrateDeletionError {
#[error("crate is missing: {0}")]
MissingCrate(String),
}
#[context("error trying to delete crate {name} from database")]
pub fn delete_crate(
conn: &mut Client,
storage: &Storage,
config: &Config,
name: &str,
) -> Result<()> {
let crate_id = get_id(conn, name)?;
let is_library = delete_crate_from_database(conn, name, crate_id)?;
// #899
let paths = if is_library {
LIBRARY_STORAGE_PATHS_TO_DELETE
} else {
BINARY_STORAGE_PATHS_TO_DELETE
};
for prefix in paths {
// delete the whole rustdoc/source folder for this crate.
// it will include existing archives.
let remote_folder = format!("{prefix}/{name}/");
storage.delete_prefix(&remote_folder)?;
// remove existing local archive index files.
let local_index_folder = config.local_archive_cache_path.join(&remote_folder);
if local_index_folder.exists() {
fs::remove_dir_all(&local_index_folder).with_context(|| {
format!(
"error when trying to remove local index: {:?}",
&local_index_folder
)
})?;
}
}
Ok(())
}
#[context("error trying to delete release {name}-{version} from database")]
pub fn delete_version(
conn: &mut Client,
storage: &Storage,
config: &Config,
name: &str,
version: &str,
) -> Result<()> {
let is_library = delete_version_from_database(conn, name, version)?;
let paths = if is_library {
LIBRARY_STORAGE_PATHS_TO_DELETE
} else {
BINARY_STORAGE_PATHS_TO_DELETE
};
for prefix in paths {
storage.delete_prefix(&format!("{prefix}/{name}/{version}/"))?;
}
let local_archive_cache = &config.local_archive_cache_path;
let mut paths = vec![source_archive_path(name, version)];
if is_library {
paths.push(rustdoc_archive_path(name, version));
}
for archive_filename in paths {
// delete remove archive and remote index
storage.delete_prefix(&archive_filename)?;
// delete eventually existing local indexes
let local_index_file = local_archive_cache.join(format!("{archive_filename}.index"));
if local_index_file.exists() {
fs::remove_file(&local_index_file).with_context(|| {
format!("error when trying to remove local index: {local_index_file:?}")
})?;
}
}
Ok(())
}
fn get_id(conn: &mut Client, name: &str) -> Result<i32> {
let crate_id_res = conn.query("SELECT id FROM crates WHERE name = $1", &[&name])?;
if let Some(row) = crate_id_res.into_iter().next() {
Ok(row.get("id"))
} else {
Err(CrateDeletionError::MissingCrate(name.into()).into())
}
}
// metaprogramming!
// WARNING: these must be hard-coded and NEVER user input.
const METADATA: &[(&str, &str)] = &[
("keyword_rels", "rid"),
("builds", "rid"),
("compression_rels", "release"),
("doc_coverage", "release_id"),
];
/// Returns whether this release was a library
fn delete_version_from_database(conn: &mut Client, name: &str, version: &str) -> Result<bool> {
let crate_id = get_id(conn, name)?;
let mut transaction = conn.transaction()?;
for &(table, column) in METADATA {
transaction.execute(
format!("DELETE FROM {table} WHERE {column} IN (SELECT id FROM releases WHERE crate_id = $1 AND version = $2)").as_str(),
&[&crate_id, &version],
)?;
}
let is_library: bool = transaction
.query_one(
"DELETE FROM releases WHERE crate_id = $1 AND version = $2 RETURNING is_library",
&[&crate_id, &version],
)?
.get("is_library");
transaction.execute(
"UPDATE crates SET latest_version_id = (
SELECT id FROM releases WHERE release_time = (
SELECT MAX(release_time) FROM releases WHERE crate_id = $1
)
) WHERE id = $1",
&[&crate_id],
)?;
let paths = if is_library {
LIBRARY_STORAGE_PATHS_TO_DELETE
} else {
BINARY_STORAGE_PATHS_TO_DELETE
};
for prefix in paths {
transaction.execute(
"DELETE FROM files WHERE path LIKE $1;",
&[&format!("{prefix}/{name}/{version}/%")],
)?;
}
transaction.commit()?;
Ok(is_library)
}
/// Returns whether any release in this crate was a library
fn delete_crate_from_database(conn: &mut Client, name: &str, crate_id: i32) -> Result<bool> {
let mut transaction = conn.transaction()?;
transaction.execute(
"DELETE FROM sandbox_overrides WHERE crate_name = $1",
&[&name],
)?;
for &(table, column) in METADATA {
transaction.execute(
format!(
"DELETE FROM {table} WHERE {column} IN (SELECT id FROM releases WHERE crate_id = $1)"
)
.as_str(),
&[&crate_id],
)?;
}
transaction.execute("DELETE FROM owner_rels WHERE cid = $1;", &[&crate_id])?;
let has_library = transaction
.query_one(
"SELECT
BOOL_OR(releases.is_library) AS has_library
FROM releases
WHERE releases.crate_id = $1
",
&[&crate_id],
)?
.get("has_library");
transaction.execute("DELETE FROM releases WHERE crate_id = $1;", &[&crate_id])?;
transaction.execute("DELETE FROM crates WHERE id = $1;", &[&crate_id])?;
// Transactions automatically rollback when not committing, so if any of the previous queries
// fail the whole transaction will be aborted.
transaction.commit()?;
Ok(has_library)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::registry_api::CrateOwner;
use crate::test::{assert_success, wrapper};
use postgres::Client;
use test_case::test_case;
fn crate_exists(conn: &mut Client, name: &str) -> Result<bool> {
Ok(!conn
.query("SELECT * FROM crates WHERE name = $1;", &[&name])?
.is_empty())
}
fn release_exists(conn: &mut Client, id: i32) -> Result<bool> {
Ok(!conn
.query("SELECT * FROM releases WHERE id = $1;", &[&id])?
.is_empty())
}
#[test_case(true)]
#[test_case(false)]
fn test_delete_crate(archive_storage: bool) {
wrapper(|env| {
let db = env.db();
// Create fake packages in the database
let pkg1_v1_id = env
.fake_release()
.name("package-1")
.version("1.0.0")
.archive_storage(archive_storage)
.create()?;
let pkg1_v2_id = env
.fake_release()
.name("package-1")
.version("2.0.0")
.archive_storage(archive_storage)
.create()?;
let pkg2_id = env
.fake_release()
.name("package-2")
.archive_storage(archive_storage)
.create()?;
assert!(crate_exists(&mut db.conn(), "package-1")?);
assert!(crate_exists(&mut db.conn(), "package-2")?);
assert!(release_exists(&mut db.conn(), pkg1_v1_id)?);
assert!(release_exists(&mut db.conn(), pkg1_v2_id)?);
assert!(release_exists(&mut db.conn(), pkg2_id)?);
for (pkg, version) in &[
("package-1", "1.0.0"),
("package-1", "2.0.0"),
("package-2", "1.0.0"),
] {
assert!(env.storage().rustdoc_file_exists(
pkg,
version,
&format!("{pkg}/index.html"),
archive_storage
)?);
}
delete_crate(&mut db.conn(), &env.storage(), &env.config(), "package-1")?;
assert!(!crate_exists(&mut db.conn(), "package-1")?);
assert!(crate_exists(&mut db.conn(), "package-2")?);
assert!(!release_exists(&mut db.conn(), pkg1_v1_id)?);
assert!(!release_exists(&mut db.conn(), pkg1_v2_id)?);
assert!(release_exists(&mut db.conn(), pkg2_id)?);
// files for package 2 still exists
assert!(env.storage().rustdoc_file_exists(
"package-2",
"1.0.0",
"package-2/index.html",
archive_storage
)?);
// files for package 1 are gone
if archive_storage {
assert!(!env
.storage()
.exists(&rustdoc_archive_path("package-1", "1.0.0"))?);
assert!(!env
.storage()
.exists(&rustdoc_archive_path("package-1", "2.0.0"))?);
} else {
assert!(!env.storage().rustdoc_file_exists(
"package-1",
"1.0.0",
"package-1/index.html",
archive_storage
)?);
assert!(!env.storage().rustdoc_file_exists(
"package-1",
"2.0.0",
"package-1/index.html",
archive_storage
)?);
}
Ok(())
});
}
#[test_case(true)]
#[test_case(false)]
fn test_delete_version(archive_storage: bool) {
wrapper(|env| {
fn owners(conn: &mut Client, crate_id: i32) -> Result<Vec<String>> {
Ok(conn
.query(
"SELECT login FROM owners
INNER JOIN owner_rels ON owners.id = owner_rels.oid
WHERE owner_rels.cid = $1",
&[&crate_id],
)?
.into_iter()
.map(|row| row.get(0))
.collect())
}
let db = env.db();
let v1 = env
.fake_release()
.name("a")
.version("1.0.0")
.archive_storage(archive_storage)
.add_owner(CrateOwner {
login: "malicious actor".into(),
avatar: "https://example.org/malicious".into(),
})
.create()?;
assert!(release_exists(&mut db.conn(), v1)?);
assert!(env.storage().rustdoc_file_exists(
"a",
"1.0.0",
"a/index.html",
archive_storage
)?);
let crate_id = db
.conn()
.query("SELECT crate_id FROM releases WHERE id = $1", &[&v1])?
.into_iter()
.next()
.unwrap()
.get(0);
assert_eq!(
owners(&mut db.conn(), crate_id)?,
vec!["malicious actor".to_string()]
);
let v2 = env
.fake_release()
.name("a")
.version("2.0.0")
.archive_storage(archive_storage)
.add_owner(CrateOwner {
login: "Peter Rabbit".into(),
avatar: "https://example.org/peter".into(),
})
.create()?;
assert!(release_exists(&mut db.conn(), v2)?);
assert!(env.storage().rustdoc_file_exists(
"a",
"2.0.0",
"a/index.html",
archive_storage
)?);
assert_eq!(
owners(&mut db.conn(), crate_id)?,
vec!["Peter Rabbit".to_string()]
);
delete_version(&mut db.conn(), &env.storage(), &env.config(), "a", "1.0.0")?;
assert!(!release_exists(&mut db.conn(), v1)?);
if archive_storage {
// for archive storage the archive and index files
// need to be cleaned up.
let rustdoc_archive = rustdoc_archive_path("a", "1.0.0");
assert!(!env.storage().exists(&rustdoc_archive)?);
// local and remote index are gone too
let archive_index = format!("{rustdoc_archive}.index");
assert!(!env.storage().exists(&archive_index)?);
assert!(!env
.config()
.local_archive_cache_path
.join(&archive_index)
.exists());
} else {
assert!(!env.storage().rustdoc_file_exists(
"a",
"1.0.0",
"a/index.html",
archive_storage
)?);
}
assert!(release_exists(&mut db.conn(), v2)?);
assert!(env.storage().rustdoc_file_exists(
"a",
"2.0.0",
"a/index.html",
archive_storage
)?);
assert_eq!(
owners(&mut db.conn(), crate_id)?,
vec!["Peter Rabbit".to_string()]
);
let web = env.frontend();
assert_success("/a/2.0.0/a/", web)?;
assert_eq!(web.get("/a/1.0.0/a/").send()?.status(), 404);
Ok(())
})
}
}