Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/copy binary files #467

Merged
merged 2 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,23 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [v1.10.4](https://github.com/rash-sh/rash/tree/v1.10.4) - 2024-07-04

### Build

- deps: Update Rust crate serde_json to v1.0.118
- deps: Update Rust crate log to v0.4.22
- deps: Update Rust crate clap to v4.5.8
- deps: Update Rust crate serde_json to v1.0.119
- deps: Update Rust crate serde_with to v3.8.2
- deps: Update Rust crate serde_json to v1.0.120
- deps: Update KSXGitHub/github-actions-deploy-aur action to v2.7.2
- deps: Update Rust crate serde_with to v3.8.3

### Fixed

- module: Copy binary data

## [v1.10.3](https://github.com/rash-sh/rash/tree/v1.10.3) - 2024-06-24

### Build
Expand Down
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ resolver = "2"


[workspace.package]
version = "1.10.3"
version = "1.10.4"
authors = ["Pando85 <[email protected]>"]
rust-version = "1.74"
edition = "2021"
Expand Down
2 changes: 1 addition & 1 deletion mdbook_rash/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ path = "src/bin/mdbook-rash.rs"
doc = false

[dependencies]
rash_core = { path = "../rash_core", features = ["docs"], version = "1.10.3" }
rash_core = { path = "../rash_core", features = ["docs"], version = "1.10.4" }
lazy_static.workspace = true
log.workspace = true
regex.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion rash_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ path = "src/bin/rash.rs"
docs = ["rash_derive/docs", "schemars"]

[dependencies]
rash_derive = { path = "../rash_derive", version = "1.10.3" }
rash_derive = { path = "../rash_derive", version = "1.10.4" }
lazy_static.workspace = true
log.workspace = true
regex.workspace = true
Expand Down
100 changes: 94 additions & 6 deletions rash_core/src/modules/copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ use rash_derive::DocJsonSchema;
use std::fs::{metadata, set_permissions, File, OpenOptions, Permissions};
use std::io::prelude::*;

use std::fmt;
use std::io::Result as IoResult;
use std::io::{BufReader, Write};
use std::os::unix::fs::PermissionsExt;

Expand Down Expand Up @@ -97,6 +99,48 @@ fn change_permissions(
Ok(false)
}

#[derive(Debug, PartialEq)]
enum Content {
Str(String),
Bytes(Vec<u8>),
}

// Implement std::fmt::Display for Content
impl fmt::Display for Content {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Content::Str(s) => write!(f, "{}", s),
Content::Bytes(b) => write!(f, "{:?}", b),
}
}
}

impl Content {
fn as_bytes(&self) -> &[u8] {
match self {
Content::Str(s) => s.as_bytes(),
Content::Bytes(b) => b,
}
}

fn len(&self) -> usize {
match self {
Content::Str(s) => s.len(),
Content::Bytes(b) => b.len(),
}
}
}

fn read_content<R: BufRead + Seek>(buf_reader: &mut R) -> IoResult<Content> {
let mut content = Vec::new();
buf_reader.read_to_end(&mut content)?;

match String::from_utf8(content.clone()) {
Ok(s) => Ok(Content::Str(s)),
Err(_) => Ok(Content::Bytes(content)),
}
}

pub fn copy_file(params: Params, check_mode: bool) -> Result<ModuleResult> {
trace!("params: {:?}", params);
let open_read_file = OpenOptions::new().read(true).clone();
Expand All @@ -114,20 +158,17 @@ pub fn copy_file(params: Params, check_mode: bool) -> Result<ModuleResult> {
}
})?;
let mut buf_reader = BufReader::new(&read_file);
let mut content = String::new();
buf_reader.read_to_string(&mut content)?;
let content = read_content(&mut buf_reader)?;
let dest_metadata = read_file.metadata()?;
let dest_permissions = dest_metadata.permissions();
let mut changed = false;

let desired_content = match params.input.clone() {
Input::Content(s) => s,
Input::Content(s) => Content::Str(s),
Input::Src(src) => {
let file = File::open(src)?;
let mut buf_reader = BufReader::new(file);
let mut contents = String::new();
buf_reader.read_to_string(&mut contents)?;
contents
read_content(&mut buf_reader)?
}
};

Expand Down Expand Up @@ -876,4 +917,51 @@ mod tests {
}
);
}

#[test]
fn test_copy_file_binary() {
let dir = tempdir().unwrap();

let src_path = dir.path().join("image.png");
let file_path = dir.path().join("output_image.png");

let image_data: &[u8] = &[
0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, 0x49, 0x48,
0x44, 0x52, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x05, 0x08, 0x06, 0x00, 0x00,
0x00, 0x8d, 0x6f, 0x26, 0xe5, 0x00, 0x00, 0x00, 0x1c, 0x49, 0x44, 0x41, 0x54, 0x08,
0xd7, 0x63, 0xf8,
];
let mut file = File::create(src_path.clone()).unwrap();
file.write_all(image_data).unwrap();
let output = copy_file(
Params {
input: Input::Src(src_path.into_os_string().into_string().unwrap()),
dest: file_path.to_str().unwrap().to_owned(),
mode: Some("0400".to_owned()),
},
false,
)
.unwrap();

let mut file = File::open(&file_path).unwrap();
let mut contents = Vec::new();
file.read_to_end(&mut contents).unwrap();
assert_eq!(contents, image_data);

let metadata = file.metadata().unwrap();
let permissions = metadata.permissions();
assert_eq!(
format!("{:o}", permissions.mode() & 0o7777),
format!("{:o}", 0o400)
);

assert_eq!(
output,
ModuleResult {
changed: true,
output: Some(file_path.to_str().unwrap().to_owned()),
extra: None,
}
);
}
}
Loading