Skip to content

Commit

Permalink
Revert "Make nix.conf changes deterministic (#620)" (#648)
Browse files Browse the repository at this point in the history
* Revert "Make nix.conf changes deterministic (#620)"

This reverts commit 05571a4.

* fmt

* Prod ci
  • Loading branch information
Hoverbear authored Sep 28, 2023
1 parent 3c7297e commit fc6d611
Show file tree
Hide file tree
Showing 12 changed files with 983 additions and 1,241 deletions.
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ uuid = { version = "1.2.2", features = ["serde"] }
os-release = { version = "0.1.0", default-features = false }
is_ci = { version = "1.1.1", default-features = false, optional = true }
strum = { version = "0.25.0", features = ["derive"] }
nix-config-parser = { version = "0.1.2", features = ["serde"] }
which = "4.4.0"
sysctl = "0.5.4"
walkdir = "2.3.3"
Expand Down
15 changes: 7 additions & 8 deletions src/action/base/create_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub struct CreateFile {
group: Option<String>,
mode: Option<u32>,
buf: String,
replace: bool,
force: bool,
}

impl CreateFile {
Expand All @@ -38,7 +38,7 @@ impl CreateFile {
group: impl Into<Option<String>>,
mode: impl Into<Option<u32>>,
buf: String,
replace: bool,
force: bool,
) -> Result<StatefulAction<Self>, ActionError> {
let path = path.as_ref().to_path_buf();
let mode = mode.into();
Expand All @@ -50,7 +50,7 @@ impl CreateFile {
group,
mode,
buf,
replace,
force,
};

if this.path.exists() {
Expand Down Expand Up @@ -128,7 +128,7 @@ impl CreateFile {
.map_err(|e| ActionErrorKind::Read(this.path.clone(), e))
.map_err(Self::error)?;

if discovered_buf != this.buf && !this.replace {
if discovered_buf != this.buf {
return Err(Self::error(ActionErrorKind::DifferentContent(
this.path.clone(),
)));
Expand Down Expand Up @@ -183,8 +183,7 @@ impl Action for CreateFile {
group,
mode,
buf,
// If `replace` was not set, and the file existed, the `plan` step would have errored
replace: _,
force: _,
} = self;

if tracing::enabled!(tracing::Level::TRACE) {
Expand Down Expand Up @@ -248,7 +247,7 @@ impl Action for CreateFile {
group: _,
mode: _,
buf: _,
replace: _,
force: _,
} = &self;

vec![ActionDescription::new(
Expand All @@ -265,7 +264,7 @@ impl Action for CreateFile {
group: _,
mode: _,
buf: _,
replace: _,
force: _,
} = self;
// The user already deleted it
if !path.exists() {
Expand Down
102 changes: 21 additions & 81 deletions src/action/base/create_or_insert_into_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ use tracing::{span, Span};
pub enum Position {
Beginning,
End,
Before {
index: usize,
expected_content: String,
},
}

/** Create a file at the given location with the provided `buf` as
Expand Down Expand Up @@ -144,20 +140,6 @@ impl CreateOrInsertIntoFile {
return Ok(StatefulAction::completed(this));
}

if let Position::Before {
index,
expected_content,
} = &this.position
{
if discovered_buf.lines().nth(*index) != Some(expected_content.as_str()) {
return Err(ActionErrorKind::DifferentLineContent(
this.path.clone(),
this.position.clone(),
))
.map_err(Self::error);
}
}

// If not, we can't skip this, so we still do it
}

Expand Down Expand Up @@ -239,75 +221,33 @@ impl Action for CreateOrInsertIntoFile {
ActionErrorKind::Open(temp_file_path.clone(), e)
}).map_err(Self::error)?;

match position {
Position::End => {
if let Some(ref mut orig_file) = orig_file {
tokio::io::copy(orig_file, &mut temp_file)
.await
.map_err(|e| {
ActionErrorKind::Copy(path.to_owned(), temp_file_path.to_owned(), e)
})
.map_err(Self::error)?;
}

temp_file
.write_all(buf.as_bytes())
if *position == Position::End {
if let Some(ref mut orig_file) = orig_file {
tokio::io::copy(orig_file, &mut temp_file)
.await
.map_err(|e| ActionErrorKind::Write(temp_file_path.clone(), e))
.map_err(|e| {
ActionErrorKind::Copy(path.to_owned(), temp_file_path.to_owned(), e)
})
.map_err(Self::error)?;
},
Position::Beginning => {
temp_file
.write_all(buf.as_bytes())
.await
.map_err(|e| ActionErrorKind::Write(temp_file_path.clone(), e))
.map_err(Self::error)?;

if let Some(ref mut orig_file) = orig_file {
tokio::io::copy(orig_file, &mut temp_file)
.await
.map_err(|e| {
ActionErrorKind::Copy(path.to_owned(), temp_file_path.to_owned(), e)
})
.map_err(Self::error)?;
}
},
Position::Before {
index,
expected_content,
} => {
let mut original_content_buf = Vec::new();
if let Some(ref mut orig_file) = orig_file {
tokio::io::copy(orig_file, &mut original_content_buf)
.await
.map_err(|e| {
ActionErrorKind::Copy(path.to_owned(), temp_file_path.to_owned(), e)
})
.map_err(Self::error)?;
}
let original_content = String::from_utf8(original_content_buf)
.map_err(ActionErrorKind::FromUtf8)
.map_err(Self::error)?;
let mut original_content_lines = original_content.lines().collect::<Vec<_>>();
// The last line should match expected
if original_content_lines.get(*index).copied() != Some(expected_content.as_str()) {
return Err(ActionErrorKind::DifferentLineContent(
path.clone(),
position.clone(),
))
.map_err(Self::error);
}
}
}

original_content_lines.insert(*index, buf);
let new_content = original_content_lines.join("\n");
temp_file
.write_all(buf.as_bytes())
.await
.map_err(|e| ActionErrorKind::Write(temp_file_path.clone(), e))
.map_err(Self::error)?;

temp_file
.write_all(new_content.as_bytes())
if *position == Position::Beginning {
if let Some(ref mut orig_file) = orig_file {
tokio::io::copy(orig_file, &mut temp_file)
.await
.map_err(|e| ActionErrorKind::Write(temp_file_path.clone(), e))
.map_err(|e| {
ActionErrorKind::Copy(path.to_owned(), temp_file_path.to_owned(), e)
})
.map_err(Self::error)?;
},
};
}
}

let gid = if let Some(group) = group {
Some(
Expand Down
Loading

0 comments on commit fc6d611

Please sign in to comment.