Skip to content

Commit

Permalink
Fix INI file truncation and duplication (davidcole1340#136)
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 authored Sep 30, 2022
1 parent 1da812c commit 7dac401
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 @@ -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

0 comments on commit 7dac401

Please sign in to comment.