Skip to content
This repository has been archived by the owner on Jul 23, 2019. It is now read-only.

Commit

Permalink
Truncate existing file on save
Browse files Browse the repository at this point in the history
  • Loading branch information
Antonio Scandurra committed May 15, 2018
1 parent 406cfb9 commit ea2c335
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
3 changes: 2 additions & 1 deletion xray_core/src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1661,7 +1661,8 @@ impl BufferSnapshot {
pub fn iter<'a>(&'a self) -> impl 'a + Iterator<Item = &'a [u16]> {
self.fragments.iter().filter_map(|fragment| {
if fragment.is_visible() {
Some(fragment.insertion.text.code_units.as_ref())
let range = fragment.start_offset..fragment.end_offset;
Some(&fragment.insertion.text.code_units[range])
} else {
None
}
Expand Down
34 changes: 20 additions & 14 deletions xray_server/src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use parking_lot::Mutex;
use std::char::decode_utf16;
use std::ffi::OsString;
use std::fs;
use std::io::{self, Read, Write};
use std::io::{self, Read, Seek, SeekFrom, Write};
use std::os::unix::fs::MetadataExt;
use std::path::PathBuf;
use std::sync::Arc;
Expand Down Expand Up @@ -196,21 +196,27 @@ impl xray_fs::File for File {
let file = self.file.clone();
thread::spawn(move || {
fn write(file: &mut fs::File, snapshot: BufferSnapshot) -> Result<(), io::Error> {
let mut buf_writer = io::BufWriter::new(file);
let mut encode_buf = [0_u8; 4];
for character in snapshot
.iter()
.flat_map(|c| decode_utf16(c.iter().cloned()))
let mut size = 0_u64;
{
let character = character.map_err(|_| {
io::Error::new(
io::ErrorKind::InvalidData,
"buffer did not contain valid UTF-8",
)
})?;
let encoded_char = character.encode_utf8(&mut encode_buf);
buf_writer.write(encoded_char.as_bytes())?;
let mut buf_writer = io::BufWriter::new(&mut *file);
buf_writer.seek(SeekFrom::Start(0))?;
for character in snapshot
.iter()
.flat_map(|c| decode_utf16(c.iter().cloned()))
{
let character = character.map_err(|_| {
io::Error::new(
io::ErrorKind::InvalidData,
"buffer did not contain valid UTF-8",
)
})?;
let mut encode_buf = [0_u8; 4];
let encoded_char = character.encode_utf8(&mut encode_buf);
buf_writer.write(encoded_char.as_bytes())?;
size += encoded_char.len() as u64;
}
}
file.set_len(size)?;
Ok(())
}

Expand Down

0 comments on commit ea2c335

Please sign in to comment.