Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ini file duplication and truncation when using cargo-php command #136

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions crates/cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use dialoguer::{Confirm, Select};

use std::{
fs::OpenOptions,
io::{BufRead, BufReader, Write},
io::{BufRead, BufReader, Seek, SeekFrom, Write},
path::PathBuf,
process::{Command, Stdio},
};
Expand Down Expand Up @@ -207,6 +207,8 @@ impl Install {
let line = line.with_context(|| "Failed to read line from `php.ini`")?;
if !line.contains(&ext_line) {
new_lines.push(line);
} else {
bail!("Extension already enabled.");
}
}

Expand All @@ -216,6 +218,8 @@ impl Install {
}

new_lines.push(ext_line);
file.seek(SeekFrom::Start(0))?;
file.set_len(0)?;
file.write(new_lines.join("\n").as_bytes())
.with_context(|| "Failed to update `php.ini`")?;
}
Expand Down Expand Up @@ -318,7 +322,6 @@ impl Remove {
.read(true)
.write(true)
.create(true)
.truncate(true)
.open(php_ini)
.with_context(|| "Failed to open `php.ini`")?;

Expand All @@ -330,6 +333,8 @@ impl Remove {
}
}

file.seek(SeekFrom::Start(0))?;
file.set_len(0)?;
file.write(new_lines.join("\n").as_bytes())
.with_context(|| "Failed to update `php.ini`")?;
}
Expand Down