|
1 | 1 | use std::{ |
2 | 2 | io, |
3 | | - path::{self, Path, PathBuf}, |
| 3 | + path::{Path, PathBuf}, |
4 | 4 | }; |
5 | 5 |
|
6 | 6 | #[derive(Debug, Clone)] |
@@ -31,75 +31,103 @@ impl CopyToContainer { |
31 | 31 | } |
32 | 32 | } |
33 | 33 |
|
34 | | - pub(crate) fn target_directory(&self) -> Result<String, CopyToContaienrError> { |
35 | | - path::Path::new(&self.target) |
36 | | - .parent() |
37 | | - .map(path::Path::display) |
38 | | - .map(|dir| dir.to_string()) |
39 | | - .ok_or_else(|| CopyToContaienrError::PathNameError(self.target.clone())) |
40 | | - } |
41 | | - |
42 | 34 | pub(crate) async fn tar(&self) -> Result<bytes::Bytes, CopyToContaienrError> { |
43 | 35 | self.source.tar(&self.target).await |
44 | 36 | } |
45 | 37 | } |
46 | 38 |
|
| 39 | +impl From<&Path> for CopyDataSource { |
| 40 | + fn from(value: &Path) -> Self { |
| 41 | + CopyDataSource::File(value.to_path_buf()) |
| 42 | + } |
| 43 | +} |
| 44 | +impl From<PathBuf> for CopyDataSource { |
| 45 | + fn from(value: PathBuf) -> Self { |
| 46 | + CopyDataSource::File(value) |
| 47 | + } |
| 48 | +} |
| 49 | +impl From<Vec<u8>> for CopyDataSource { |
| 50 | + fn from(value: Vec<u8>) -> Self { |
| 51 | + CopyDataSource::Data(value) |
| 52 | + } |
| 53 | +} |
| 54 | + |
47 | 55 | impl CopyDataSource { |
48 | 56 | pub(crate) async fn tar( |
49 | 57 | &self, |
50 | 58 | target_path: impl Into<String>, |
51 | 59 | ) -> Result<bytes::Bytes, CopyToContaienrError> { |
52 | 60 | let target_path: String = target_path.into(); |
53 | | - let mut ar = tokio_tar::Builder::new(Vec::new()); |
54 | | - |
55 | | - match self { |
56 | | - CopyDataSource::File(file_path) => { |
57 | | - let f = &mut tokio::fs::File::open(file_path) |
58 | | - .await |
59 | | - .map_err(CopyToContaienrError::IoError)?; |
60 | | - ar.append_file(&target_path, f) |
61 | | - .await |
62 | | - .map_err(CopyToContaienrError::IoError)?; |
63 | | - } |
64 | | - CopyDataSource::Data(data) => { |
65 | | - let path = path::Path::new(&target_path); |
66 | | - let file_name = match path.file_name() { |
67 | | - Some(v) => v, |
68 | | - None => return Err(CopyToContaienrError::PathNameError(target_path)), |
69 | | - }; |
70 | | - |
71 | | - let mut header = tokio_tar::Header::new_gnu(); |
72 | | - header.set_size(data.len() as u64); |
73 | | - header.set_mode(0o0644); |
74 | | - header.set_cksum(); |
75 | | - |
76 | | - ar.append_data(&mut header, file_name, data.as_slice()) |
77 | | - .await |
78 | | - .map_err(CopyToContaienrError::IoError)?; |
79 | | - } |
80 | | - } |
81 | 61 |
|
82 | | - let bytes = ar |
83 | | - .into_inner() |
84 | | - .await |
85 | | - .map_err(CopyToContaienrError::IoError)?; |
| 62 | + let bytes = match self { |
| 63 | + CopyDataSource::File(source_file_path) => { |
| 64 | + tar_file(source_file_path, &target_path).await? |
| 65 | + } |
| 66 | + CopyDataSource::Data(data) => tar_bytes(data, &target_path).await?, |
| 67 | + }; |
86 | 68 |
|
87 | 69 | Ok(bytes::Bytes::copy_from_slice(bytes.as_slice())) |
88 | 70 | } |
89 | 71 | } |
90 | 72 |
|
91 | | -impl From<&Path> for CopyDataSource { |
92 | | - fn from(value: &Path) -> Self { |
93 | | - CopyDataSource::File(value.to_path_buf()) |
94 | | - } |
| 73 | +async fn tar_file( |
| 74 | + source_file_path: &Path, |
| 75 | + target_path: &str, |
| 76 | +) -> Result<Vec<u8>, CopyToContaienrError> { |
| 77 | + let target_path = make_path_relative(&target_path); |
| 78 | + let meta = tokio::fs::metadata(source_file_path) |
| 79 | + .await |
| 80 | + .map_err(CopyToContaienrError::IoError)?; |
| 81 | + |
| 82 | + let mut ar = tokio_tar::Builder::new(Vec::new()); |
| 83 | + if meta.is_dir() { |
| 84 | + ar.append_dir_all(target_path, source_file_path) |
| 85 | + .await |
| 86 | + .map_err(CopyToContaienrError::IoError)?; |
| 87 | + } else { |
| 88 | + let f = &mut tokio::fs::File::open(source_file_path) |
| 89 | + .await |
| 90 | + .map_err(CopyToContaienrError::IoError)?; |
| 91 | + |
| 92 | + ar.append_file(target_path, f) |
| 93 | + .await |
| 94 | + .map_err(CopyToContaienrError::IoError)?; |
| 95 | + }; |
| 96 | + |
| 97 | + let res = ar |
| 98 | + .into_inner() |
| 99 | + .await |
| 100 | + .map_err(CopyToContaienrError::IoError)?; |
| 101 | + |
| 102 | + Ok(res) |
95 | 103 | } |
96 | | -impl From<PathBuf> for CopyDataSource { |
97 | | - fn from(value: PathBuf) -> Self { |
98 | | - CopyDataSource::File(value) |
99 | | - } |
| 104 | + |
| 105 | +async fn tar_bytes(data: &Vec<u8>, target_path: &str) -> Result<Vec<u8>, CopyToContaienrError> { |
| 106 | + let relative_target_path = make_path_relative(&target_path); |
| 107 | + |
| 108 | + let mut header = tokio_tar::Header::new_gnu(); |
| 109 | + header.set_size(data.len() as u64); |
| 110 | + header.set_mode(0o0644); |
| 111 | + header.set_cksum(); |
| 112 | + |
| 113 | + let mut ar = tokio_tar::Builder::new(Vec::new()); |
| 114 | + ar.append_data(&mut header, relative_target_path, data.as_slice()) |
| 115 | + .await |
| 116 | + .map_err(CopyToContaienrError::IoError)?; |
| 117 | + |
| 118 | + let res = ar |
| 119 | + .into_inner() |
| 120 | + .await |
| 121 | + .map_err(CopyToContaienrError::IoError)?; |
| 122 | + |
| 123 | + Ok(res) |
100 | 124 | } |
101 | | -impl From<Vec<u8>> for CopyDataSource { |
102 | | - fn from(value: Vec<u8>) -> Self { |
103 | | - CopyDataSource::Data(value) |
| 125 | + |
| 126 | +fn make_path_relative(path: &str) -> String { |
| 127 | + // TODO support also absolute windows paths like "C:\temp\foo.txt" |
| 128 | + if path.starts_with("/") { |
| 129 | + path.trim_start_matches("/").to_string() |
| 130 | + } else { |
| 131 | + path.to_string() |
104 | 132 | } |
105 | 133 | } |
0 commit comments