Skip to content

Commit 49b6d9e

Browse files
committed
Auto merge of #12247 - weihanglo:source-doc, r=epage
docs: doc comments for all registry kinds
2 parents 7a0b24b + 2c48599 commit 49b6d9e

File tree

6 files changed

+205
-34
lines changed

6 files changed

+205
-34
lines changed

src/cargo/sources/registry/download.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
//! Shared download logic between [`HttpRegistry`] and [`RemoteRegistry`].
2+
//!
3+
//! [`HttpRegistry`]: super::http_remote::HttpRegistry
4+
//! [`RemoteRegistry`]: super::remote::RemoteRegistry
5+
16
use anyhow::Context;
27
use cargo_util::Sha256;
38

@@ -20,10 +25,15 @@ const PREFIX_TEMPLATE: &str = "{prefix}";
2025
const LOWER_PREFIX_TEMPLATE: &str = "{lowerprefix}";
2126
const CHECKSUM_TEMPLATE: &str = "{sha256-checksum}";
2227

28+
/// Filename of the `.crate` tarball, e.g., `once_cell-1.18.0.crate`.
2329
pub(super) fn filename(pkg: PackageId) -> String {
2430
format!("{}-{}.crate", pkg.name(), pkg.version())
2531
}
2632

33+
/// Checks if `pkg` is downloaded and ready under the directory at `cache_path`.
34+
/// If not, returns a URL to download it from.
35+
///
36+
/// This is primarily called by [`RegistryData::download`](super::RegistryData::download).
2737
pub(super) fn download(
2838
cache_path: &Filesystem,
2939
config: &Config,
@@ -86,6 +96,10 @@ pub(super) fn download(
8696
})
8797
}
8898

99+
/// Verifies the integrity of `data` with `checksum` and persists it under the
100+
/// directory at `cache_path`.
101+
///
102+
/// This is primarily called by [`RegistryData::finish_download`](super::RegistryData::finish_download).
89103
pub(super) fn finish_download(
90104
cache_path: &Filesystem,
91105
config: &Config,
@@ -119,6 +133,10 @@ pub(super) fn finish_download(
119133
Ok(dst)
120134
}
121135

136+
/// Checks if a tarball of `pkg` has been already downloaded under the
137+
/// directory at `cache_path`.
138+
///
139+
/// This is primarily called by [`RegistryData::is_crate_downloaded`](super::RegistryData::is_crate_downloaded).
122140
pub(super) fn is_crate_downloaded(
123141
cache_path: &Filesystem,
124142
config: &Config,

src/cargo/sources/registry/http_remote.rs

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
//! Access to a HTTP-based crate registry.
2-
//!
3-
//! See [`HttpRegistry`] for details.
1+
//! Access to a HTTP-based crate registry. See [`HttpRegistry`] for details.
42
53
use crate::core::{PackageId, SourceId};
64
use crate::ops::{self};
@@ -52,8 +50,15 @@ const UNKNOWN: &'static str = "Unknown";
5250
///
5351
/// [RFC 2789]: https://github.com/rust-lang/rfcs/pull/2789
5452
pub struct HttpRegistry<'cfg> {
53+
/// Path to the registry index (`$CARGO_HOME/registry/index/$REG-HASH`).
54+
///
55+
/// To be fair, `HttpRegistry` doesn't store the registry index it
56+
/// downloads on the file system, but other cached data like registry
57+
/// configuration could be stored here.
5558
index_path: Filesystem,
59+
/// Path to the cache of `.crate` files (`$CARGO_HOME/registry/cache/$REG-HASH`).
5660
cache_path: Filesystem,
61+
/// The unique identifier of this registry source.
5762
source_id: SourceId,
5863
config: &'cfg Config,
5964

@@ -95,20 +100,20 @@ pub struct HttpRegistry<'cfg> {
95100
quiet: bool,
96101
}
97102

98-
/// Helper for downloading crates.
103+
/// State for currently pending index file downloads.
99104
struct Downloads<'cfg> {
100105
/// When a download is started, it is added to this map. The key is a
101-
/// "token" (see `Download::token`). It is removed once the download is
106+
/// "token" (see [`Download::token`]). It is removed once the download is
102107
/// finished.
103108
pending: HashMap<usize, (Download<'cfg>, EasyHandle)>,
104109
/// Set of paths currently being downloaded.
105-
/// This should stay in sync with `pending`.
110+
/// This should stay in sync with the `pending` field.
106111
pending_paths: HashSet<PathBuf>,
107112
/// Downloads that have failed and are waiting to retry again later.
108113
sleeping: SleepTracker<(Download<'cfg>, Easy)>,
109114
/// The final result of each download.
110115
results: HashMap<PathBuf, CargoResult<CompletedDownload>>,
111-
/// The next ID to use for creating a token (see `Download::token`).
116+
/// The next ID to use for creating a token (see [`Download::token`]).
112117
next: usize,
113118
/// Progress bar.
114119
progress: RefCell<Option<Progress<'cfg>>>,
@@ -119,9 +124,10 @@ struct Downloads<'cfg> {
119124
blocking_calls: usize,
120125
}
121126

127+
/// Represents a single index file download, including its progress and retry.
122128
struct Download<'cfg> {
123-
/// The token for this download, used as the key of the `Downloads::pending` map
124-
/// and stored in `EasyHandle` as well.
129+
/// The token for this download, used as the key of the
130+
/// [`Downloads::pending`] map and stored in [`EasyHandle`] as well.
125131
token: usize,
126132

127133
/// The path of the package that we're downloading.
@@ -137,28 +143,39 @@ struct Download<'cfg> {
137143
retry: Retry<'cfg>,
138144
}
139145

146+
/// HTTPS headers [`HttpRegistry`] cares about.
140147
#[derive(Default)]
141148
struct Headers {
142149
last_modified: Option<String>,
143150
etag: Option<String>,
144151
www_authenticate: Vec<String>,
152+
/// We don't care about these headers. Put them here for debugging purpose.
145153
others: Vec<String>,
146154
}
147155

156+
/// HTTP status code [`HttpRegistry`] cares about.
148157
enum StatusCode {
149158
Success,
150159
NotModified,
151160
NotFound,
152161
Unauthorized,
153162
}
154163

164+
/// Represents a complete [`Download`] from an HTTP request.
165+
///
166+
/// Usually it is constructed in [`HttpRegistry::handle_completed_downloads`],
167+
/// and then returns to the caller of [`HttpRegistry::load()`].
155168
struct CompletedDownload {
156169
response_code: StatusCode,
157170
data: Vec<u8>,
158171
header_map: Headers,
159172
}
160173

161174
impl<'cfg> HttpRegistry<'cfg> {
175+
/// Creates a HTTP-rebased remote registry for `source_id`.
176+
///
177+
/// * `name` --- Name of a path segment where `.crate` tarballs and the
178+
/// registry index are stored. Expect to be unique.
162179
pub fn new(
163180
source_id: SourceId,
164181
config: &'cfg Config,
@@ -208,6 +225,7 @@ impl<'cfg> HttpRegistry<'cfg> {
208225
})
209226
}
210227

228+
/// Splits HTTP `HEADER: VALUE` to a tuple.
211229
fn handle_http_header(buf: &[u8]) -> Option<(&str, &str)> {
212230
if buf.is_empty() {
213231
return None;
@@ -222,6 +240,9 @@ impl<'cfg> HttpRegistry<'cfg> {
222240
Some((tag, value))
223241
}
224242

243+
/// Setup the necessary works before the first fetch gets started.
244+
///
245+
/// This is a no-op if called more than one time.
225246
fn start_fetch(&mut self) -> CargoResult<()> {
226247
if self.fetch_started {
227248
// We only need to run the setup code once.
@@ -249,6 +270,8 @@ impl<'cfg> HttpRegistry<'cfg> {
249270
Ok(())
250271
}
251272

273+
/// Checks the results inside the [`HttpRegistry::multi`] handle, and
274+
/// updates relevant state in [`HttpRegistry::downloads`] accordingly.
252275
fn handle_completed_downloads(&mut self) -> CargoResult<()> {
253276
assert_eq!(
254277
self.downloads.pending.len(),
@@ -322,11 +345,15 @@ impl<'cfg> HttpRegistry<'cfg> {
322345
Ok(())
323346
}
324347

348+
/// Constructs the full URL to download a index file.
325349
fn full_url(&self, path: &Path) -> String {
326350
// self.url always ends with a slash.
327351
format!("{}{}", self.url, path.display())
328352
}
329353

354+
/// Check if an index file of `path` is up-to-date.
355+
///
356+
/// The `path` argument is the same as in [`RegistryData::load`].
330357
fn is_fresh(&self, path: &Path) -> bool {
331358
if !self.requested_update {
332359
trace!(
@@ -373,7 +400,7 @@ impl<'cfg> HttpRegistry<'cfg> {
373400
Ok(self.registry_config.as_ref())
374401
}
375402

376-
/// Get the registry configuration.
403+
/// Get the registry configuration from either cache or remote.
377404
fn config(&mut self) -> Poll<CargoResult<&RegistryConfig>> {
378405
debug!("loading config");
379406
let index_path = self.assert_index_locked(&self.index_path);
@@ -405,6 +432,7 @@ impl<'cfg> HttpRegistry<'cfg> {
405432
}
406433
}
407434

435+
/// Moves failed [`Download`]s that are ready to retry to the pending queue.
408436
fn add_sleepers(&mut self) -> CargoResult<()> {
409437
for (dl, handle) in self.downloads.sleeping.to_retry() {
410438
let mut handle = self.multi.add(handle)?;
@@ -790,6 +818,7 @@ impl<'cfg> RegistryData for HttpRegistry<'cfg> {
790818
}
791819

792820
impl<'cfg> Downloads<'cfg> {
821+
/// Updates the state of the progress bar for downloads.
793822
fn tick(&self) -> CargoResult<()> {
794823
let mut progress = self.progress.borrow_mut();
795824
let Some(progress) = progress.as_mut() else { return Ok(()); };

src/cargo/sources/registry/local.rs

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! Access to a regstiry on the local filesystem. See [`LocalRegistry`] for more.
2+
13
use crate::core::PackageId;
24
use crate::sources::registry::{LoadResponse, MaybeLock, RegistryConfig, RegistryData};
35
use crate::util::errors::CargoResult;
@@ -10,18 +12,68 @@ use std::path::Path;
1012
use std::task::Poll;
1113

1214
/// A local registry is a registry that lives on the filesystem as a set of
13-
/// `.crate` files with an `index` directory in the same format as a remote
15+
/// `.crate` files with an `index` directory in the [same format] as a remote
1416
/// registry.
17+
///
18+
/// This type is primarily accessed through the [`RegistryData`] trait.
19+
///
20+
/// When a local registry is requested for a package, it simply looks into what
21+
/// its index has under the `index` directory. When [`LocalRegistry::download`]
22+
/// is called, a local registry verifies the checksum of the requested `.crate`
23+
/// tarball and then unpacks it to `$CARGO_HOME/.registry/src`.
24+
///
25+
/// > Note that there is a third-party subcommand [`cargo-local-registry`],
26+
/// > which happened to be developed by a former Cargo team member when local
27+
/// > registry was introduced. The tool is to ease the burden of maintaining
28+
/// > local registries. However, in general the Cargo team avoids recommending
29+
/// > any specific third-party crate. Just FYI.
30+
///
31+
/// [same format]: super#the-format-of-the-index
32+
/// [`cargo-local-registry`]: https://crates.io/crates/cargo-local-registry
33+
///
34+
/// # Filesystem hierarchy
35+
///
36+
/// Here is an example layout of a local registry on a local filesystem:
37+
///
38+
/// ```text
39+
/// [registry root]/
40+
/// ├── index/ # registry index
41+
/// │ ├── an/
42+
/// │ │ └── yh/
43+
/// │ │ └── anyhow
44+
/// │ ├── ru/
45+
/// │ │ └── st/
46+
/// │ │ ├── rustls
47+
/// │ │ └── rustls-ffi
48+
/// │ └── se/
49+
/// │ └── mv/
50+
/// │ └── semver
51+
/// ├── anyhow-1.0.71.crate # pre-downloaded crate tarballs
52+
/// ├── rustls-0.20.8.crate
53+
/// ├── rustls-ffi-0.8.2.crate
54+
/// └── semver-1.0.17.crate
55+
/// ```
56+
///
57+
/// For general concepts of registries, see the [module-level documentation](crate::sources::registry).
1558
pub struct LocalRegistry<'cfg> {
59+
/// Path to the registry index.
1660
index_path: Filesystem,
61+
/// Root path of this local registry.
1762
root: Filesystem,
63+
/// Path where this local registry extract `.crate` tarballs to.
1864
src_path: Filesystem,
1965
config: &'cfg Config,
66+
/// Whether this source has updated all package informations it may contain.
2067
updated: bool,
68+
/// Disables status messages.
2169
quiet: bool,
2270
}
2371

2472
impl<'cfg> LocalRegistry<'cfg> {
73+
/// Creates a local registry at `root`.
74+
///
75+
/// * `name` --- Name of a path segment where `.crate` tarballs are stored.
76+
/// Expect to be unique.
2577
pub fn new(root: &Path, config: &'cfg Config, name: &str) -> LocalRegistry<'cfg> {
2678
LocalRegistry {
2779
src_path: config.registry_source_path().join(name),

src/cargo/sources/registry/mod.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,9 @@ pub struct RegistryConfig {
277277
/// will be extended with `/{crate}/{version}/download` to
278278
/// support registries like crates.io which were created before the
279279
/// templating setup was created.
280+
///
281+
/// For more on the template of the download URL, see [Index Configuration](
282+
/// https://doc.rust-lang.org/nightly/cargo/reference/registry-index.html#index-configuration).
280283
pub dl: String,
281284

282285
/// API endpoint for the registry. This is what's actually hit to perform
@@ -485,8 +488,10 @@ impl<'cfg> RegistrySource<'cfg> {
485488

486489
/// Creates a source of a registry. This is a inner helper function.
487490
///
488-
/// * `name` --- Unique name for this source to store source files (`.crate` tarballs) are stored.
491+
/// * `name` --- Name of a path segment which may affect where `.crate`
492+
/// tarballs, the registry index and cache are stored. Expect to be unique.
489493
/// * `ops` --- The underlying [`RegistryData`] type.
494+
/// * `yanked_whitelist` --- Packages allowed to be used, even if they are yanked.
490495
fn new(
491496
source_id: SourceId,
492497
config: &'cfg Config,

0 commit comments

Comments
 (0)