Replaced u8 pex with UnixPex struct

This commit is contained in:
veeso
2021-08-23 17:52:55 +02:00
parent 32ab0267fb
commit 92b081c076
10 changed files with 296 additions and 219 deletions

View File

@@ -26,7 +26,7 @@
* SOFTWARE.
*/
use super::{FileTransfer, FileTransferError, FileTransferErrorType};
use crate::fs::{FsDirectory, FsEntry, FsFile};
use crate::fs::{FsDirectory, FsEntry, FsFile, UnixPex};
use crate::utils::fmt::shadow_password;
// Includes
@@ -158,32 +158,25 @@ impl FtpFileTransfer {
/// ### query_unix_pex
///
/// Returns unix pex in tuple of values
fn query_unix_pex(f: &File) -> (u8, u8, u8) {
fn query_unix_pex(f: &File) -> (UnixPex, UnixPex, UnixPex) {
(
Self::pex_to_byte(
UnixPex::new(
f.can_read(PosixPexQuery::Owner),
f.can_write(PosixPexQuery::Owner),
f.can_execute(PosixPexQuery::Owner),
),
Self::pex_to_byte(
UnixPex::new(
f.can_read(PosixPexQuery::Group),
f.can_write(PosixPexQuery::Group),
f.can_execute(PosixPexQuery::Group),
),
Self::pex_to_byte(
UnixPex::new(
f.can_read(PosixPexQuery::Others),
f.can_write(PosixPexQuery::Others),
f.can_execute(PosixPexQuery::Others),
),
)
}
/// ### pex_to_byte
///
/// Convert unix permissions to byte value
fn pex_to_byte(read: bool, write: bool, exec: bool) -> u8 {
((read as u8) << 2) + ((write as u8) << 1) + (exec as u8)
}
}
impl FileTransfer for FtpFileTransfer {
@@ -775,7 +768,7 @@ mod tests {
symlink: None, // UNIX only
user: Some(0), // UNIX only
group: Some(0), // UNIX only
unix_pex: Some((6, 4, 4)), // UNIX only
unix_pex: Some((UnixPex::from(6), UnixPex::from(4), UnixPex::from(4))), // UNIX only
});
assert!(ftp
.rename(&dummy, PathBuf::from("/a/b/c").as_path())
@@ -874,7 +867,10 @@ mod tests {
assert!(file.symlink.is_none());
assert_eq!(file.user, None);
assert_eq!(file.group, None);
assert_eq!(file.unix_pex.unwrap(), (6, 6, 4));
assert_eq!(
file.unix_pex.unwrap(),
(UnixPex::from(6), UnixPex::from(6), UnixPex::from(4))
);
assert_eq!(
file.last_access_time
.duration_since(UNIX_EPOCH)
@@ -930,7 +926,7 @@ mod tests {
symlink: None, // UNIX only
user: Some(0), // UNIX only
group: Some(0), // UNIX only
unix_pex: Some((6, 4, 4)), // UNIX only
unix_pex: Some((UnixPex::from(6), UnixPex::from(4), UnixPex::from(4))), // UNIX only
};
let mut ftp: FtpFileTransfer = FtpFileTransfer::new(false);
assert!(ftp.change_dir(Path::new("/tmp")).is_err());

View File

@@ -27,7 +27,7 @@
*/
// Locals
use super::{FileTransfer, FileTransferError, FileTransferErrorType};
use crate::fs::{FsDirectory, FsEntry, FsFile};
use crate::fs::{FsDirectory, FsEntry, FsFile, UnixPex};
use crate::system::sshkey_storage::SshKeyStorage;
use crate::utils::fmt::{fmt_time, shadow_password};
use crate::utils::parser::parse_lstime;
@@ -128,7 +128,11 @@ impl ScpFileTransfer {
};
// Get unix pex
let unix_pex = (pex(0..3), pex(3..6), pex(6..9));
let unix_pex = (
UnixPex::from(pex(0..3)),
UnixPex::from(pex(3..6)),
UnixPex::from(pex(6..9)),
);
// Parse mtime and convert to SystemTime
let mtime: SystemTime = match parse_lstime(
@@ -873,7 +877,11 @@ impl FileTransfer for ScpFileTransfer {
// Calculate file mode
let mode: i32 = match local.unix_pex {
None => 0o644,
Some((u, g, o)) => ((u as i32) << 6) + ((g as i32) << 3) + (o as i32),
Some((u, g, o)) => {
((u.as_byte() as i32) << 6)
+ ((g.as_byte() as i32) << 3)
+ (o.as_byte() as i32)
}
};
// Calculate mtime, atime
let times: (u64, u64) = {
@@ -1126,7 +1134,7 @@ mod tests {
symlink: None, // UNIX only
user: Some(0), // UNIX only
group: Some(0), // UNIX only
unix_pex: Some((6, 4, 4)), // UNIX only
unix_pex: Some((UnixPex::from(6), UnixPex::from(4), UnixPex::from(4))), // UNIX only
});
assert!(client
.rename(&dummy, PathBuf::from("/a/b/c").as_path())
@@ -1239,7 +1247,10 @@ mod tests {
.unwrap_file();
assert_eq!(entry.name.as_str(), "Cargo.toml");
assert_eq!(entry.abs_path, PathBuf::from("/tmp/Cargo.toml"));
assert_eq!(entry.unix_pex.unwrap(), (6, 4, 4));
assert_eq!(
entry.unix_pex.unwrap(),
(UnixPex::from(6), UnixPex::from(4), UnixPex::from(4))
);
assert_eq!(entry.size, 2056);
assert_eq!(entry.ftype.unwrap().as_str(), "toml");
assert!(entry.symlink.is_none());
@@ -1254,7 +1265,10 @@ mod tests {
.unwrap_file();
assert_eq!(entry.name.as_str(), "CODE_OF_CONDUCT.md");
assert_eq!(entry.abs_path, PathBuf::from("/tmp/CODE_OF_CONDUCT.md"));
assert_eq!(entry.unix_pex.unwrap(), (6, 6, 6));
assert_eq!(
entry.unix_pex.unwrap(),
(UnixPex::from(6), UnixPex::from(6), UnixPex::from(6))
);
assert_eq!(entry.size, 3368);
assert_eq!(entry.ftype.unwrap().as_str(), "md");
assert!(entry.symlink.is_none());
@@ -1269,7 +1283,10 @@ mod tests {
.unwrap_dir();
assert_eq!(entry.name.as_str(), "docs");
assert_eq!(entry.abs_path, PathBuf::from("/tmp/docs"));
assert_eq!(entry.unix_pex.unwrap(), (7, 5, 5));
assert_eq!(
entry.unix_pex.unwrap(),
(UnixPex::from(7), UnixPex::from(5), UnixPex::from(5))
);
assert!(entry.symlink.is_none());
// Short metadata
assert!(client
@@ -1320,7 +1337,7 @@ mod tests {
symlink: None, // UNIX only
user: Some(0), // UNIX only
group: Some(0), // UNIX only
unix_pex: Some((6, 4, 4)), // UNIX only
unix_pex: Some((UnixPex::from(6), UnixPex::from(4), UnixPex::from(4))), // UNIX only
};
let mut scp: ScpFileTransfer = ScpFileTransfer::new(SshKeyStorage::empty());
assert!(scp.change_dir(Path::new("/tmp")).is_err());

View File

@@ -27,7 +27,7 @@
*/
// Locals
use super::{FileTransfer, FileTransferError, FileTransferErrorType};
use crate::fs::{FsDirectory, FsEntry, FsFile};
use crate::fs::{FsDirectory, FsEntry, FsFile, UnixPex};
use crate::system::sshkey_storage::SshKeyStorage;
use crate::utils::fmt::{fmt_time, shadow_password};
@@ -126,11 +126,11 @@ impl SftpFileTransfer {
.map(|ext| String::from(ext.to_str().unwrap_or("")));
let uid: Option<u32> = metadata.uid;
let gid: Option<u32> = metadata.gid;
let pex: Option<(u8, u8, u8)> = metadata.perm.map(|x| {
let pex: Option<(UnixPex, UnixPex, UnixPex)> = metadata.perm.map(|x| {
(
((x >> 6) & 0x7) as u8,
((x >> 3) & 0x7) as u8,
(x & 0x7) as u8,
UnixPex::from(((x >> 6) & 0x7) as u8),
UnixPex::from(((x >> 3) & 0x7) as u8),
UnixPex::from((x & 0x7) as u8),
)
});
let size: u64 = metadata.size.unwrap_or(0);
@@ -720,7 +720,7 @@ impl FileTransfer for SftpFileTransfer {
// Calculate file mode
let mode: i32 = match local.unix_pex {
None => 0o644,
Some((u, g, o)) => ((u as i32) << 6) + ((g as i32) << 3) + (o as i32),
Some((u, g, o)) => ((u.as_byte() as i32) << 6) + ((g.as_byte() as i32) << 3) + (o.as_byte() as i32),
};
debug!("File mode {:?}", mode);
match sftp.open_mode(
@@ -924,7 +924,7 @@ mod tests {
symlink: None, // UNIX only
user: Some(0), // UNIX only
group: Some(0), // UNIX only
unix_pex: Some((6, 4, 4)), // UNIX only
unix_pex: Some((UnixPex::from(6), UnixPex::from(4), UnixPex::from(4))), // UNIX only
});
assert!(client
.rename(&dummy, PathBuf::from("/a/b/c").as_path())
@@ -1072,7 +1072,7 @@ mod tests {
symlink: None, // UNIX only
user: Some(0), // UNIX only
group: Some(0), // UNIX only
unix_pex: Some((6, 4, 4)), // UNIX only
unix_pex: Some((UnixPex::from(6), UnixPex::from(4), UnixPex::from(4))), // UNIX only
};
let mut sftp: SftpFileTransfer = SftpFileTransfer::new(SshKeyStorage::empty());
assert!(sftp.change_dir(Path::new("/tmp")).is_err());