forked from rust-lang/crates.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkrate.rs
537 lines (473 loc) · 18.5 KB
/
krate.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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
use std::collections::BTreeMap;
use chrono::NaiveDateTime;
use diesel::associations::Identifiable;
use diesel::pg::Pg;
use diesel::prelude::*;
use diesel::sql_types::{Bool, Text};
use crate::app::App;
use crate::controllers::helpers::pagination::*;
use crate::models::version::TopVersions;
use crate::models::{
CrateOwner, CrateOwnerInvitation, Dependency, NewCrateOwnerInvitationOutcome, Owner, OwnerKind,
ReverseDependency, User, Version,
};
use crate::util::errors::{cargo_err, AppResult};
use crate::models::helpers::with_count::*;
use crate::schema::*;
use crate::sql::canon_crate_name;
#[derive(Debug, Queryable, Identifiable, Associations, Clone, Copy)]
#[diesel(
table_name = recent_crate_downloads,
check_for_backend(diesel::pg::Pg),
primary_key(crate_id),
belongs_to(Crate),
)]
pub struct RecentCrateDownloads {
pub crate_id: i32,
pub downloads: i32,
}
#[derive(Debug, Clone, Queryable, Identifiable, AsChangeset, QueryableByName, Selectable)]
#[diesel(table_name = crates, check_for_backend(diesel::pg::Pg))]
pub struct Crate {
pub id: i32,
pub name: String,
pub updated_at: NaiveDateTime,
pub created_at: NaiveDateTime,
pub downloads: i32,
pub description: Option<String>,
pub homepage: Option<String>,
pub documentation: Option<String>,
pub repository: Option<String>,
pub max_upload_size: Option<i32>,
pub max_features: Option<i16>,
}
/// We literally never want to select `textsearchable_index_col`
/// so we provide this type and constant to pass to `.select`
type AllColumns = (
crates::id,
crates::name,
crates::updated_at,
crates::created_at,
crates::downloads,
crates::description,
crates::homepage,
crates::documentation,
crates::repository,
crates::max_upload_size,
crates::max_features,
);
pub const ALL_COLUMNS: AllColumns = (
crates::id,
crates::name,
crates::updated_at,
crates::created_at,
crates::downloads,
crates::description,
crates::homepage,
crates::documentation,
crates::repository,
crates::max_upload_size,
crates::max_features,
);
pub const MAX_NAME_LENGTH: usize = 64;
type CanonCrateName<T> = canon_crate_name::HelperType<T>;
type All = diesel::dsl::Select<crates::table, diesel::dsl::AsSelect<Crate, diesel::pg::Pg>>;
type WithName<'a> = diesel::dsl::Eq<CanonCrateName<crates::name>, CanonCrateName<&'a str>>;
type ByName<'a> = diesel::dsl::Filter<All, WithName<'a>>;
type ByExactName<'a> = diesel::dsl::Filter<All, diesel::dsl::Eq<crates::name, &'a str>>;
#[derive(Insertable, AsChangeset, Default, Debug)]
#[diesel(
table_name = crates,
check_for_backend(diesel::pg::Pg),
// This is actually just to skip updating them
primary_key(name, max_upload_size),
treat_none_as_null = true,
)]
pub struct NewCrate<'a> {
pub name: &'a str,
pub description: Option<&'a str>,
pub homepage: Option<&'a str>,
pub documentation: Option<&'a str>,
pub readme: Option<&'a str>,
pub repository: Option<&'a str>,
pub max_upload_size: Option<i32>,
pub max_features: Option<i16>,
}
impl<'a> NewCrate<'a> {
pub fn update(&self, conn: &mut PgConnection) -> QueryResult<Crate> {
use diesel::update;
update(crates::table)
.filter(canon_crate_name(crates::name).eq(canon_crate_name(self.name)))
.set(self)
.returning(Crate::as_returning())
.get_result(conn)
}
pub fn create(&self, conn: &mut PgConnection, user_id: i32) -> QueryResult<Crate> {
use crate::schema::crates::dsl::*;
conn.transaction(|conn| {
let krate: Crate = diesel::insert_into(crates)
.values(self)
.on_conflict_do_nothing()
.returning(Crate::as_returning())
.get_result(conn)?;
let owner = CrateOwner {
crate_id: krate.id,
owner_id: user_id,
created_by: user_id,
owner_kind: OwnerKind::User,
email_notifications: true,
};
diesel::insert_into(crate_owners::table)
.values(&owner)
.execute(conn)?;
Ok(krate)
})
}
}
impl Crate {
/// SQL filter based on whether the crate's name loosely matches the given
/// string.
///
/// The operator used varies based on the input.
pub fn loosly_matches_name<QS>(
name: &str,
) -> Box<dyn BoxableExpression<QS, Pg, SqlType = Bool> + '_>
where
crates::name: SelectableExpression<QS>,
{
if name.len() > 2 {
let wildcard_name = format!("%{name}%");
Box::new(canon_crate_name(crates::name).like(canon_crate_name(wildcard_name)))
} else {
diesel::infix_operator!(MatchesWord, "%>");
Box::new(MatchesWord::new(
canon_crate_name(crates::name),
name.into_sql::<Text>(),
))
}
}
/// SQL filter with the = binary operator
pub fn with_name(name: &str) -> WithName<'_> {
canon_crate_name(crates::name).eq(canon_crate_name(name))
}
pub fn by_name(name: &str) -> ByName<'_> {
Crate::all().filter(Self::with_name(name))
}
pub fn by_exact_name(name: &str) -> ByExactName<'_> {
Crate::all().filter(crates::name.eq(name))
}
pub fn all() -> All {
crates::table.select(Self::as_select())
}
pub fn find_version(&self, conn: &mut PgConnection, version: &str) -> AppResult<Version> {
self.all_versions()
.filter(versions::num.eq(version))
.first(conn)
.map_err(|_| {
cargo_err(&format_args!(
"crate `{}` does not have a version `{}`",
self.name, version
))
})
}
pub fn valid_name(name: &str) -> bool {
let under_max_length = name.chars().take(MAX_NAME_LENGTH + 1).count() <= MAX_NAME_LENGTH;
Crate::valid_ident(name) && under_max_length
}
fn valid_ident(name: &str) -> bool {
Self::valid_feature_prefix(name)
&& name
.chars()
.next()
.map(char::is_alphabetic)
.unwrap_or(false)
}
pub fn valid_dependency_name(name: &str) -> bool {
let under_max_length = name.chars().take(MAX_NAME_LENGTH + 1).count() <= MAX_NAME_LENGTH;
Crate::valid_dependency_ident(name) && under_max_length
}
fn valid_dependency_ident(name: &str) -> bool {
Self::valid_feature_prefix(name)
&& name
.chars()
.next()
.map(|n| n.is_alphabetic() || n == '_')
.unwrap_or(false)
}
/// Validates the THIS parts of `features = ["THIS", "and/THIS"]`.
pub fn valid_feature_name(name: &str) -> bool {
!name.is_empty()
&& name
.chars()
.all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '-' || c == '+')
}
/// Validates the prefix in front of the slash: `features = ["THIS/feature"]`.
/// Normally this corresponds to the crate name of a dependency.
fn valid_feature_prefix(name: &str) -> bool {
!name.is_empty()
&& name
.chars()
.all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '-')
}
/// Validates a whole feature string, `features = ["THIS", "ALL/THIS"]`.
pub fn valid_feature(name: &str) -> bool {
match name.split_once('/') {
Some((dep, dep_feat)) => {
let dep = dep.strip_suffix('?').unwrap_or(dep);
Crate::valid_feature_prefix(dep) && Crate::valid_feature_name(dep_feat)
}
None => Crate::valid_feature_name(name.strip_prefix("dep:").unwrap_or(name)),
}
}
/// Return both the newest (most recently updated) and
/// highest version (in semver order) for the current crate.
pub fn top_versions(&self, conn: &mut PgConnection) -> QueryResult<TopVersions> {
use crate::schema::versions::dsl::*;
Ok(TopVersions::from_date_version_pairs(
self.versions().select((created_at, num)).load(conn)?,
))
}
pub fn owners(&self, conn: &mut PgConnection) -> QueryResult<Vec<Owner>> {
let users = CrateOwner::by_owner_kind(OwnerKind::User)
.filter(crate_owners::crate_id.eq(self.id))
.inner_join(users::table)
.select(users::all_columns)
.load(conn)?
.into_iter()
.map(Owner::User);
let teams = CrateOwner::by_owner_kind(OwnerKind::Team)
.filter(crate_owners::crate_id.eq(self.id))
.inner_join(teams::table)
.select(teams::all_columns)
.load(conn)?
.into_iter()
.map(Owner::Team);
Ok(users.chain(teams).collect())
}
pub fn owner_add(
&self,
app: &App,
conn: &mut PgConnection,
req_user: &User,
login: &str,
) -> AppResult<String> {
use diesel::insert_into;
let owner = Owner::find_or_create_by_login(app, conn, req_user, login)?;
match owner {
// Users are invited and must accept before being added
Owner::User(user) => {
let config = &app.config;
match CrateOwnerInvitation::create(user.id, req_user.id, self.id, conn, config)? {
NewCrateOwnerInvitationOutcome::InviteCreated { plaintext_token } => {
if let Ok(Some(email)) = user.verified_email(conn) {
// Swallow any error. Whether or not the email is sent, the invitation
// entry will be created in the database and the user will see the
// invitation when they visit https://crates.io/me/pending-invites/.
let _ = app.emails.send_owner_invite(
&email,
&req_user.gh_login,
&self.name,
&plaintext_token,
);
}
Ok(format!(
"user {} has been invited to be an owner of crate {}",
user.gh_login, self.name
))
}
NewCrateOwnerInvitationOutcome::AlreadyExists => Ok(format!(
"user {} already has a pending invitation to be an owner of crate {}",
user.gh_login, self.name
)),
}
}
// Teams are added as owners immediately
owner @ Owner::Team(_) => {
insert_into(crate_owners::table)
.values(&CrateOwner {
crate_id: self.id,
owner_id: owner.id(),
created_by: req_user.id,
owner_kind: OwnerKind::Team,
email_notifications: true,
})
.on_conflict(crate_owners::table.primary_key())
.do_update()
.set(crate_owners::deleted.eq(false))
.execute(conn)?;
Ok(format!(
"team {} has been added as an owner of crate {}",
owner.login(),
self.name
))
}
}
}
pub fn owner_remove(&self, conn: &mut PgConnection, login: &str) -> AppResult<()> {
let owner = Owner::find_by_login(conn, login)?;
let target = crate_owners::table.find((self.id(), owner.id(), owner.kind()));
diesel::update(target)
.set(crate_owners::deleted.eq(true))
.execute(conn)?;
Ok(())
}
/// Returns (dependency, dependent crate name, dependent crate downloads)
#[instrument(skip_all, fields(krate.name = %self.name))]
pub(crate) fn reverse_dependencies(
&self,
conn: &mut PgConnection,
options: PaginationOptions,
) -> AppResult<(Vec<ReverseDependency>, i64)> {
use diesel::sql_query;
use diesel::sql_types::{BigInt, Integer};
let offset = options.offset().unwrap_or_default();
let rows: Vec<WithCount<ReverseDependency>> =
sql_query(include_str!("krate_reverse_dependencies.sql"))
.bind::<Integer, _>(self.id)
.bind::<BigInt, _>(offset)
.bind::<BigInt, _>(options.per_page)
.load(conn)?;
Ok(rows.records_and_total())
}
/// Gather all the necessary data to write an index metadata file
pub fn index_metadata(
&self,
conn: &mut PgConnection,
) -> QueryResult<Vec<crates_io_index::Crate>> {
let mut versions: Vec<Version> = self.all_versions().load(conn)?;
// We sort by `created_at` by default, but since tests run within a
// single database transaction the versions will all have the same
// `created_at` timestamp, so we sort by semver as a secondary key.
versions.sort_by_cached_key(|k| (k.created_at, semver::Version::parse(&k.num).ok()));
let deps: Vec<(Dependency, String)> = Dependency::belonging_to(&versions)
.inner_join(crates::table)
.select((dependencies::all_columns, crates::name))
.load(conn)?;
let deps = deps.grouped_by(&versions);
versions
.into_iter()
.zip(deps)
.map(|(version, deps)| {
let mut deps = deps
.into_iter()
.map(|(dep, name)| {
// If this dependency has an explicit name in `Cargo.toml` that
// means that the `name` we have listed is actually the package name
// that we're depending on. The `name` listed in the index is the
// Cargo.toml-written-name which is what cargo uses for
// `--extern foo=...`
let (name, package) = match dep.explicit_name {
Some(explicit_name) => (explicit_name, Some(name)),
None => (name, None),
};
crates_io_index::Dependency {
name,
req: dep.req,
features: dep.features,
optional: dep.optional,
default_features: dep.default_features,
kind: Some(dep.kind.into()),
package,
target: dep.target,
}
})
.collect::<Vec<_>>();
deps.sort();
let features: BTreeMap<String, Vec<String>> =
serde_json::from_value(version.features).unwrap_or_default();
let (features, features2): (BTreeMap<_, _>, BTreeMap<_, _>) =
features.into_iter().partition(|(_k, vals)| {
!vals
.iter()
.any(|v| v.starts_with("dep:") || v.contains("?/"))
});
let (features2, v) = if features2.is_empty() {
(None, None)
} else {
(Some(features2), Some(2))
};
let krate = crates_io_index::Crate {
name: self.name.clone(),
vers: version.num.to_string(),
cksum: version.checksum,
yanked: Some(version.yanked),
deps,
features,
links: version.links,
rust_version: version.rust_version,
features2,
v,
};
Ok(krate)
})
.collect()
}
}
#[cfg(test)]
mod tests {
use crate::models::Crate;
#[test]
fn valid_name() {
assert!(Crate::valid_name("foo"));
assert!(!Crate::valid_name("京"));
assert!(!Crate::valid_name(""));
assert!(!Crate::valid_name("💝"));
assert!(Crate::valid_name("foo_underscore"));
assert!(Crate::valid_name("foo-dash"));
assert!(!Crate::valid_name("foo+plus"));
// Starting with an underscore is an invalid crate name.
assert!(!Crate::valid_name("_foo"));
assert!(!Crate::valid_name("-foo"));
}
#[test]
fn valid_dependency_name() {
assert!(Crate::valid_dependency_name("foo"));
assert!(!Crate::valid_dependency_name("京"));
assert!(!Crate::valid_dependency_name(""));
assert!(!Crate::valid_dependency_name("💝"));
assert!(Crate::valid_dependency_name("foo_underscore"));
assert!(Crate::valid_dependency_name("foo-dash"));
assert!(!Crate::valid_dependency_name("foo+plus"));
// Starting with an underscore is a valid dependency name.
assert!(Crate::valid_dependency_name("_foo"));
assert!(!Crate::valid_dependency_name("-foo"));
}
#[test]
fn valid_feature_names() {
assert!(Crate::valid_feature("foo"));
assert!(!Crate::valid_feature(""));
assert!(!Crate::valid_feature("/"));
assert!(!Crate::valid_feature("%/%"));
assert!(Crate::valid_feature("a/a"));
assert!(Crate::valid_feature("32-column-tables"));
assert!(Crate::valid_feature("c++20"));
assert!(Crate::valid_feature("krate/c++20"));
assert!(!Crate::valid_feature("c++20/wow"));
assert!(Crate::valid_feature("foo?/bar"));
assert!(Crate::valid_feature("dep:foo"));
assert!(!Crate::valid_feature("dep:foo?/bar"));
assert!(!Crate::valid_feature("foo/?bar"));
assert!(!Crate::valid_feature("foo?bar"));
}
}
pub trait CrateVersions {
fn versions(&self) -> versions::BoxedQuery<'_, Pg> {
self.all_versions().filter(versions::yanked.eq(false))
}
fn all_versions(&self) -> versions::BoxedQuery<'_, Pg>;
}
impl CrateVersions for Crate {
fn all_versions(&self) -> versions::BoxedQuery<'_, Pg> {
Version::belonging_to(self).into_boxed()
}
}
impl CrateVersions for Vec<Crate> {
fn all_versions(&self) -> versions::BoxedQuery<'_, Pg> {
self.as_slice().all_versions()
}
}
impl CrateVersions for [Crate] {
fn all_versions(&self) -> versions::BoxedQuery<'_, Pg> {
Version::belonging_to(self).into_boxed()
}
}