Skip to content

Commit

Permalink
Fix INI file truncation and duplication
Browse files Browse the repository at this point in the history
Addresses davidcole1340#134

Also prevents adding the extension include line twice.
  • Loading branch information
roborourke committed May 4, 2022
1 parent f0e13d3 commit 6d29de0
Showing 1 changed file with 7 additions and 2 deletions.
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 @@ -213,6 +213,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 @@ -222,6 +224,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 @@ -324,7 +328,6 @@ impl Remove {
.read(true)
.write(true)
.create(true)
.truncate(true)
.open(php_ini)
.with_context(|| "Failed to open `php.ini`")?;

Expand All @@ -336,6 +339,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

0 comments on commit 6d29de0

Please sign in to comment.