Skip to content

Commit a57d052

Browse files
committed
refactor: repo::create -> create_repo
Also removes read_manifest_signed, as it's pretty legacy and only used by tests
1 parent 8236370 commit a57d052

File tree

7 files changed

+20
-42
lines changed

7 files changed

+20
-42
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,4 @@ nursery = "warn"
7575

7676
[profile.release]
7777
lto = true
78+
debug = "line-tables-only"

src/chunks/network.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ pub async fn install_chunk(
1818
) -> Result<()> {
1919
let chunk_name = get_chunk_filename(&chunk.hash, chunk.permissions);
2020
let chunk_path = chunk_store_path.join(&chunk_name);
21+
let tmp_chunk_path = chunk_store_path.join(format!("{}.tmp", &chunk_name));
2122

2223
if chunk_path.exists() {
2324
return Ok(());
@@ -30,7 +31,8 @@ pub async fn install_chunk(
3031
let hash = hash(hash_kind, &body);
3132

3233
if hash == chunk.hash {
33-
fs::write(&chunk_path, body)?;
34+
fs::write(&tmp_chunk_path, body)?;
35+
fs::rename(&tmp_chunk_path, &chunk_path)?;
3436

3537
Ok(())
3638
} else {

src/commands/repo.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::{fs, os::unix::fs::symlink, path::Path};
66
use crate::RepoCommands;
77
use flintpkg::{
88
crypto::signing::sign,
9-
repo::{self, read_manifest, remove_package, update_manifest},
9+
repo::{create_repo, read_manifest, remove_package, update_manifest},
1010
utils::resolve_repo,
1111
};
1212

@@ -19,7 +19,7 @@ pub async fn repo_commands(
1919
RepoCommands::Create { repo_name } => {
2020
let repo_path = &base_path.join(&repo_name);
2121

22-
repo::create(repo_path, None)?;
22+
create_repo(repo_path, None)?;
2323
symlink(Path::new("../../chunks"), repo_path.join("chunks"))?;
2424
}
2525

src/repo/manifest_io.rs

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ use crate::{
88

99
/// Reads a manifest and verifys it from the EXISTING key. This is best for GENERAL reading.
1010
///
11-
/// # Warning
12-
/// Do NOT run on downloaded manifests before `read_manifest_signed`, or else potentially malicious inputs will be parsed.
13-
///
1411
/// # Errors
1512
///
1613
/// - Filesystem errors (Permissions or doesn't exist)
@@ -37,26 +34,6 @@ fn read_manifest_unsigned(repo_path: &Path) -> Result<RepoManifest> {
3734
Ok(manifest)
3835
}
3936

40-
/// Reads a manifest and verifys it. This is best for WHEN it has been downloaded.
41-
///
42-
/// # Errors
43-
///
44-
/// - Filesystem errors (Permissions or doesn't exist)
45-
/// - Invalid signature
46-
pub fn read_manifest_signed(repo_path: &Path, public_key_serialized: &str) -> Result<RepoManifest> {
47-
let manifest_serialized = fs::read_to_string(repo_path.join("manifest.yml"))?;
48-
let manifest_signature_serialized = fs::read(repo_path.join("manifest.yml.sig"))?;
49-
50-
verify_signature(
51-
&manifest_serialized,
52-
&manifest_signature_serialized,
53-
deserialize_verifying_key(public_key_serialized)?,
54-
)?;
55-
56-
let manifest = serde_yaml::from_str(&manifest_serialized)?;
57-
Ok(manifest)
58-
}
59-
6037
/// Replaces the existing manifest with another one, and verifies that it is correct
6138
///
6239
/// # Errors
@@ -104,7 +81,8 @@ pub fn atomic_replace(base_path: &Path, filename: &str, contents: &[u8]) -> Resu
10481
#[cfg(test)]
10582
mod tests {
10683
use super::*;
107-
use crate::{crypto::signing::sign, repo::create};
84+
use crate::crypto::signing::sign;
85+
use crate::repo::create_repo;
10886
use temp_dir::TempDir;
10987

11088
#[test]
@@ -126,7 +104,7 @@ mod tests {
126104
fn test_update_manifest_valid_and_invalid() -> Result<()> {
127105
let repo = TempDir::new()?;
128106
let repo_path = repo.path();
129-
create(repo_path, Some(repo_path))?;
107+
create_repo(repo_path, Some(repo_path))?;
130108

131109
let old_manifest = read_manifest(repo_path)?;
132110

@@ -156,10 +134,10 @@ mod tests {
156134
fn test_read_signed_manifest() -> Result<()> {
157135
let repo = TempDir::new()?;
158136
let repo_path = repo.path();
159-
create(repo_path, Some(repo_path))?;
137+
create_repo(repo_path, Some(repo_path))?;
160138

161139
let manifest = read_manifest(repo_path)?;
162-
let manifest_signed = read_manifest_signed(repo_path, &manifest.public_key)?;
140+
let manifest_signed = read_manifest(repo_path)?;
163141

164142
assert_eq!(manifest.edition, manifest_signed.edition);
165143

src/repo/mod.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use crate::crypto::signing::sign;
2020
///
2121
/// - File permission errors at `repo_path`
2222
/// - Key generation errors (If you do not already have a key)
23-
pub fn create(repo_path: &Path, config_path: Option<&Path>) -> Result<()> {
23+
pub fn create_repo(repo_path: &Path, config_path: Option<&Path>) -> Result<()> {
2424
if repo_path.join("manifest.yml").exists() {
2525
bail!("Repository Already exists")
2626
}
@@ -131,10 +131,7 @@ pub fn get_package(repo_manifest: &RepoManifest, package_id: &str) -> Result<Pac
131131
}
132132
}
133133

134-
bail!(
135-
"Could not find package '{}' found in Repository.",
136-
package_id
137-
);
134+
bail!("Could not find package '{package_id}' found in Repository.",);
138135
}
139136

140137
/// Gets an installed package manifest from a repository.
@@ -224,7 +221,7 @@ mod tests {
224221
// Create repo
225222
let repo = TempDir::new()?;
226223
let repo_path = repo.path();
227-
create(repo_path, Some(repo_path))?;
224+
create_repo(repo_path, Some(repo_path))?;
228225

229226
// Make sure errors on no package
230227
assert!(get_package(&read_manifest(repo_path)?, "test").is_err());
@@ -260,7 +257,7 @@ mod tests {
260257
let repo_path = tmp.path();
261258

262259
// Create repo
263-
create(repo_path, Some(repo_path)).unwrap();
260+
create_repo(repo_path, Some(repo_path)).unwrap();
264261

265262
// Read unsigned manifest
266263
let manifest = read_manifest(repo_path).unwrap();
@@ -277,7 +274,7 @@ mod tests {
277274
fn test_tampered_manifest_fails() -> Result<()> {
278275
let tmp = TempDir::new()?;
279276
let repo_path = tmp.path();
280-
create(repo_path, Some(repo_path))?;
277+
create_repo(repo_path, Some(repo_path))?;
281278

282279
// Tamper with manifest.yml
283280
let mut contents = fs::read_to_string(repo_path.join("manifest.yml"))?;

src/run/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ pub async fn install_package(
113113
mod tests {
114114
use super::*;
115115
use crate::chunks::save_tree;
116-
use crate::repo::{Metadata, PackageManifest, create, insert_package};
116+
use crate::repo::{Metadata, PackageManifest, create_repo, insert_package};
117117
use std::fs;
118118
use temp_dir::TempDir;
119119

@@ -124,7 +124,7 @@ mod tests {
124124
let chunks_dir = TempDir::new()?;
125125
let chunks_path = chunks_dir.path();
126126

127-
create(repo_path, Some(repo_path))?;
127+
create_repo(repo_path, Some(repo_path))?;
128128

129129
// Create a temp tree
130130
let temp_tree = TempDir::new()?;

tests/full_workflow.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use temp_dir::TempDir;
44

55
use flintpkg::{
66
build::build,
7-
repo::{self, get_installed_package},
7+
repo::{create_repo, get_installed_package},
88
run::{install_package, start},
99
};
1010

@@ -16,7 +16,7 @@ async fn full_workflow_test() -> Result<()> {
1616
let chunks_dir = TempDir::new()?;
1717
let chunks_path = chunks_dir.path();
1818

19-
repo::create(repo_path, None)?;
19+
create_repo(repo_path, None)?;
2020

2121
let build_manifest_path = Path::new("build_manifest.yml");
2222
build(build_manifest_path, repo_path, None, chunks_path).await?;

0 commit comments

Comments
 (0)