|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +use crate::config::{FileContent, Encoding}; |
| 5 | +use std::path::Path; |
| 6 | +use std::io; |
| 7 | +use tracing::{debug}; |
| 8 | + |
| 9 | +impl Encoding { |
| 10 | + pub fn to_encoding_rs(&self) -> Option<&'static encoding_rs::Encoding> { |
| 11 | + match self { |
| 12 | + Encoding::Utf8 => Some(encoding_rs::UTF_8), |
| 13 | + Encoding::Utf16 => Some(encoding_rs::UTF_16LE), // or UTF_16BE depending on your needs |
| 14 | + Encoding::Ascii => Some(encoding_rs::WINDOWS_1252), // ASCII is a subset of Windows-1252 |
| 15 | + Encoding::Binary => None, |
| 16 | + } |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +impl FileContent { |
| 21 | + /// Create a new `FileContent`. |
| 22 | + /// |
| 23 | + /// # Arguments |
| 24 | + /// |
| 25 | + /// * `string` - The string for the Path |
| 26 | + #[must_use] |
| 27 | + pub fn new(path: &str) -> FileContent { |
| 28 | + FileContent { |
| 29 | + path: path.to_string(), |
| 30 | + content: None, |
| 31 | + hash: None, |
| 32 | + encoding: Some(Encoding::Utf8), |
| 33 | + exist: None, |
| 34 | + } |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +pub fn get_file_content(filecontent: &FileContent) -> Result<FileContent, Box<dyn std::error::Error>> { |
| 39 | + debug!("In get_file_content"); |
| 40 | + match compare_filecontent_state(filecontent) { |
| 41 | + Ok(f) => { |
| 42 | + Ok(f) |
| 43 | + }, |
| 44 | + Err(e) => { |
| 45 | + Err(e)? |
| 46 | + } |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +pub fn set_file_content(filecontent: &FileContent) -> Result<FileContent, Box<dyn std::error::Error>> { |
| 51 | + // debug!("In set_file_content"); |
| 52 | + // let path = Path::new(&filecontent.path); |
| 53 | + // let content = filecontent.content.as_ref().unwrap_or(&String::new()); |
| 54 | + // let encoding = filecontent.encoding.unwrap_or(Encoding::Utf8).to_encoding_rs().unwrap_or(encoding_rs::UTF_8); |
| 55 | + // let mut file = fsFile::create(path)?; |
| 56 | + // let mut encoder = encoding.new_encoder(); |
| 57 | + // let mut bytes = vec![0; content.len() * encoding.new_encoder().max_buffer_length()]; |
| 58 | + // let (bytes_written, _, _) = encoder.encode_to_slice(content, &mut bytes, true); |
| 59 | + // file.write_all(&bytes[..bytes_written])?; |
| 60 | + // Ok(filecontent.clone()) |
| 61 | + Ok(filecontent.clone()) |
| 62 | +} |
| 63 | + |
| 64 | +pub fn delete_file_content(filecontent: &FileContent) -> Result<FileContent, Box<dyn std::error::Error>> { |
| 65 | + debug!("In delete_file_content"); |
| 66 | + let path = Path::new(&filecontent.path); |
| 67 | + |
| 68 | + let mut filecontent_to_delete = filecontent.clone(); |
| 69 | + |
| 70 | + if path.exists() { |
| 71 | + |
| 72 | + filecontent_to_delete.exist = Some(false); |
| 73 | + filecontent_to_delete.content = None; |
| 74 | + set_file_content(&filecontent)?; |
| 75 | + } |
| 76 | + |
| 77 | + Ok(filecontent_to_delete) |
| 78 | +} |
| 79 | + |
| 80 | +pub fn read_file_with_encoding(path: &Path, encoding: &'static encoding_rs::Encoding) -> io::Result<String> { |
| 81 | + let bytes = std::fs::read(path)?; |
| 82 | + let (decoded_str, _encoding_used, had_errors) = encoding.decode(&bytes); |
| 83 | + |
| 84 | + if had_errors { |
| 85 | + return Err(io::Error::new(io::ErrorKind::InvalidData, "Invalid encoding")); |
| 86 | + } |
| 87 | + |
| 88 | + Ok(decoded_str.to_string()) |
| 89 | +} |
| 90 | + |
| 91 | +pub fn compare_filecontent_state(filecontent: &FileContent) -> Result<FileContent, Box<dyn std::error::Error>> { |
| 92 | + debug!("In compare_filecontent_state"); |
| 93 | + |
| 94 | + let rs_encoding = filecontent.encoding.as_ref().unwrap_or(&Encoding::Utf8).to_encoding_rs().unwrap_or(encoding_rs::UTF_8); |
| 95 | + |
| 96 | + let path = Path::new(&filecontent.path); |
| 97 | + if path.exists() { |
| 98 | + let content = read_file_with_encoding(path, rs_encoding)?; |
| 99 | + let content_hash = sha256::digest(content.as_bytes()); |
| 100 | + let hash = filecontent.hash.as_ref().unwrap_or(&content_hash); |
| 101 | + |
| 102 | + match filecontent.hash.as_ref() { |
| 103 | + Some(h) => { |
| 104 | + if h.to_lowercase() == hash.to_lowercase() { |
| 105 | + let mut updated_file_content = filecontent.clone(); |
| 106 | + updated_file_content.hash = Some(hash.to_string()); |
| 107 | + updated_file_content.content = Some(content); |
| 108 | + updated_file_content.exist = Some(true); |
| 109 | + |
| 110 | + return Ok(updated_file_content) |
| 111 | + } |
| 112 | + else { |
| 113 | + return Err("Hash does not match")?; |
| 114 | + } |
| 115 | + }, |
| 116 | + None => { |
| 117 | + let mut updated_file_content = filecontent.clone(); |
| 118 | + updated_file_content.hash = Some(hash.to_string()); |
| 119 | + updated_file_content.content = Some(content); |
| 120 | + updated_file_content.exist = Some(true); |
| 121 | + |
| 122 | + return Ok(updated_file_content) |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + else { |
| 127 | + match filecontent.exist { |
| 128 | + Some(true) | None => { |
| 129 | + return Err("File does not exist")?; |
| 130 | + }, |
| 131 | + Some(false) => { |
| 132 | + return Ok(filecontent.clone()); |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | +} |
0 commit comments