mirror of
https://github.com/veeso/termscp.git
synced 2025-12-07 09:36:00 -08:00
Replaced sha256 sum with last modification time check, to verify if a file has been changed in the text editor
This commit is contained in:
@@ -29,7 +29,6 @@ extern crate tempfile;
|
||||
use super::{FileTransferActivity, InputMode, LogLevel, PopupType};
|
||||
use crate::fs::{FsEntry, FsFile};
|
||||
use crate::utils::fmt::fmt_millis;
|
||||
use crate::utils::hash::hash_sha256_file;
|
||||
|
||||
// Ext
|
||||
use bytesize::ByteSize;
|
||||
@@ -37,7 +36,7 @@ use crossterm::terminal::{disable_raw_mode, enable_raw_mode};
|
||||
use std::fs::OpenOptions;
|
||||
use std::io::{Read, Seek, Write};
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::time::Instant;
|
||||
use std::time::{Instant, SystemTime};
|
||||
use tui::style::Color;
|
||||
|
||||
impl FileTransferActivity {
|
||||
@@ -781,13 +780,14 @@ impl FileTransferActivity {
|
||||
if let Err(err) = self.filetransfer_recv_file(tmpfile.path(), file) {
|
||||
return Err(err);
|
||||
}
|
||||
// Get current file hash
|
||||
let prev_hash: String = match hash_sha256_file(tmpfile.path()) {
|
||||
Ok(s) => s,
|
||||
// Get current file modification time
|
||||
let prev_mtime: SystemTime = match self.context.as_ref().unwrap().local.stat(tmpfile.path())
|
||||
{
|
||||
Ok(e) => e.get_last_change_time(),
|
||||
Err(err) => {
|
||||
return Err(format!(
|
||||
"Could not get sha256 for \"{}\": {}",
|
||||
file.abs_path.display(),
|
||||
"Could not stat \"{}\": {}",
|
||||
tmpfile.path().display(),
|
||||
err
|
||||
))
|
||||
}
|
||||
@@ -796,19 +796,20 @@ impl FileTransferActivity {
|
||||
if let Err(err) = self.edit_local_file(tmpfile.path()) {
|
||||
return Err(err);
|
||||
}
|
||||
// Check if file has changed
|
||||
let new_hash: String = match hash_sha256_file(tmpfile.path()) {
|
||||
Ok(s) => s,
|
||||
// Get local fs entry
|
||||
let tmpfile_entry: FsEntry = match self.context.as_ref().unwrap().local.stat(tmpfile.path())
|
||||
{
|
||||
Ok(e) => e,
|
||||
Err(err) => {
|
||||
return Err(format!(
|
||||
"Could not get sha256 for \"{}\": {}",
|
||||
file.abs_path.display(),
|
||||
"Could not stat \"{}\": {}",
|
||||
tmpfile.path().display(),
|
||||
err
|
||||
))
|
||||
}
|
||||
};
|
||||
// If hash is different, write changes
|
||||
match new_hash != prev_hash {
|
||||
// Check if file has changed
|
||||
match prev_mtime != tmpfile_entry.get_last_change_time() {
|
||||
true => {
|
||||
self.log(
|
||||
LogLevel::Info,
|
||||
|
||||
@@ -1,75 +0,0 @@
|
||||
//! ## Hash
|
||||
//!
|
||||
//! `hash` is the module which provides utilities for calculating digests
|
||||
|
||||
/*
|
||||
*
|
||||
* Copyright (C) 2020 Christian Visintin - christian.visintin1997@gmail.com
|
||||
*
|
||||
* This file is part of "TermSCP"
|
||||
*
|
||||
* TermSCP is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* TermSCP is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with TermSCP. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
extern crate data_encoding;
|
||||
extern crate ring;
|
||||
|
||||
use data_encoding::HEXLOWER;
|
||||
use ring::digest::{Context, Digest, SHA256};
|
||||
use std::fs::File;
|
||||
use std::io::Read;
|
||||
use std::path::Path;
|
||||
|
||||
/// ### hash_sha256_file
|
||||
///
|
||||
/// Get SHA256 of provided path
|
||||
pub fn hash_sha256_file(file: &Path) -> Result<String, std::io::Error> {
|
||||
// Open file
|
||||
let mut reader: File = File::open(file)?;
|
||||
let mut context = Context::new(&SHA256);
|
||||
let mut buffer = [0; 8192];
|
||||
loop {
|
||||
let count = reader.read(&mut buffer)?;
|
||||
if count == 0 {
|
||||
break;
|
||||
}
|
||||
context.update(&buffer[..count]);
|
||||
}
|
||||
// Finish context
|
||||
let digest: Digest = context.finish();
|
||||
Ok(HEXLOWER.encode(digest.as_ref()))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
|
||||
use super::*;
|
||||
|
||||
use std::io::Write;
|
||||
|
||||
#[test]
|
||||
fn test_utils_hash_sha256() {
|
||||
let tmp: tempfile::NamedTempFile = tempfile::NamedTempFile::new().unwrap();
|
||||
// Write
|
||||
let mut fhnd: File = File::create(tmp.path()).unwrap();
|
||||
assert!(fhnd.write_all(b"Hello world!\n").is_ok());
|
||||
assert_eq!(
|
||||
*hash_sha256_file(tmp.path()).ok().as_ref().unwrap(),
|
||||
String::from("0ba904eae8773b70c75333db4de2f3ac45a8ad4ddba1b242f0b3cfc199391dd8")
|
||||
);
|
||||
// Bad file
|
||||
assert!(hash_sha256_file(Path::new("/tmp/oiojjt5ig/aiehgoiwg")).is_err());
|
||||
}
|
||||
}
|
||||
@@ -25,5 +25,4 @@
|
||||
|
||||
// modules
|
||||
pub mod fmt;
|
||||
pub mod hash;
|
||||
pub mod parser;
|
||||
|
||||
Reference in New Issue
Block a user