From 498f62e58f8ca674b05e0b3aae6ae7733c93ce7f Mon Sep 17 00:00:00 2001 From: 1148118271 <1148118271@qq.com> Date: Sun, 29 Jan 2023 15:39:59 +0800 Subject: [PATCH] sftp file flags --- src/channel/local/channel.rs | 4 +++ src/channel/local/channel_sftp.rs | 55 ++++++++++++++++++------------- src/constant.rs | 48 +++++++++++++++++++++++++++ src/sftp/mod.rs | 1 - src/sftp/req_id.rs | 18 ---------- 5 files changed, 85 insertions(+), 41 deletions(-) delete mode 100644 src/sftp/mod.rs delete mode 100644 src/sftp/req_id.rs diff --git a/src/channel/local/channel.rs b/src/channel/local/channel.rs index a746a13..ed8c64c 100644 --- a/src/channel/local/channel.rs +++ b/src/channel/local/channel.rs @@ -149,6 +149,10 @@ where Ok(maybe_response) } + pub(crate) fn send_sftp_data(&mut self, ) { + + } + /// this method will receive at least one data packet /// pub(super) fn recv(&mut self) -> SshResult> { diff --git a/src/channel/local/channel_sftp.rs b/src/channel/local/channel_sftp.rs index 3653c0e..513d2fb 100644 --- a/src/channel/local/channel_sftp.rs +++ b/src/channel/local/channel_sftp.rs @@ -5,7 +5,7 @@ use crate::SshResult; use std::ffi::OsStr; use std::io::{Read, Write}; use std::ops::{Deref, DerefMut}; -use std::path::Path; + pub struct ChannelSftp { channel: Channel, @@ -42,36 +42,47 @@ where .put_u8(sftp_msg_code::SSH_FXP_INIT) .put_u32(2); self.send_data(data.to_vec())?; - let vec = self.recv().unwrap(); - - println!("vec {:?} ", vec); - println!("vec len {:?} ", vec.len()); - - let mut rd = Data::from(vec); - rd.get_u32(); - - let t = rd.get_u8(); - let ver = rd.get_u32(); - - println!("type {}, version {} ", t, ver); - + // 暂时不处理拓展功能 + let _ = self.recv()?; Ok(()) } - pub fn open_dir + ?Sized>(&mut self, p: &P) -> SshResult<()> { - let mut data = Data::new(); - data.put_u32(13) - .put_u8(sftp_msg_code::SSH_FXP_OPENDIR) + /// 打开文件 + /// + /// 使用SSH_FXP_OPEN消息打开和创建文件。 + /// + /// byte SSH_FXP_OPEN + /// uint32 request-id + /// string filename [UTF-8] + /// uint32 desired-access + /// uint32 flags + /// ATTRS attrs + /// + pub fn open_file(&mut self, p: &str) -> SshResult<()> { + let x = Data::new() + .put_u8(sftp_msg_code::SSH_FXP_OPEN) .put_u32(self.req_id.next().unwrap()) - .put_str("/opt"); - self.send_data(data.to_vec())?; - let vec = self.recv().unwrap(); + .put_str(p); - println!("vec {:?} ", vec); Ok(()) } + + + // pub fn open_dir + ?Sized>(&mut self, p: &P) -> SshResult<()> { + // let mut data = Data::new(); + // data.put_u32(13) + // .put_u8(sftp_msg_code::SSH_FXP_OPENDIR) + // .put_u32(self.req_id.next().unwrap()) + // .put_str("/opt"); + // self.send_data(data.to_vec())?; + // let vec = self.recv().unwrap(); + // + // println!("vec {:?} ", vec); + // + // Ok(()) + // } } impl Deref for ChannelSftp diff --git a/src/constant.rs b/src/constant.rs index 544e4d8..373bf7f 100644 --- a/src/constant.rs +++ b/src/constant.rs @@ -282,5 +282,53 @@ pub(crate) mod sftp_msg_status_code { pub const SSH_FX_NO_MATCHING_BYTE_RANGE_LOCK: u8 = 31; } +/// file flags +/// +#[allow(dead_code)] +pub(crate) mod sftp_file_flags { + // 读取文件 + /// Open the file for reading. + /// + pub const SSH_FXF_READ: u32 = 0x00000001; + + // 写入文件 + /// Open the file for writing. + /// If both this and SSH_FXF_READ are specified, the file is opened for both reading and writing. + /// + pub const SSH_FXF_WRITE: u32 = 0x00000002; + + // 追加内容到文件 + /// Force all writes to append data at the end of the file. + /// The offset parameter to write will be ignored. + /// + pub const SSH_FXF_APPEND: u32 = 0x00000004; + + // 创建文件 + /// If this flag is specified, then a new file will be created if one + /// does not already exist (if O_TRUNC is specified, the new file will + /// be truncated to zero length if it previously exists). + /// + pub const SSH_FXF_CREAT: u32 = 0x00000008; + + // 截断文件 + /// Forces an existing file with the same name to be truncated to zero + /// length when creating a file by specifying SSH_FXF_CREAT. + /// SSH_FXF_CREAT MUST also be specified if this flag is used. + /// + pub const SSH_FXF_TRUNC: u32 = 0x00000010; + + // 创建文件时,不能有旧文件 + /// Causes the request to fail if the named file already exists. + /// SSH_FXF_CREAT MUST also be specified if this flag is used. + /// + pub const SSH_FXF_EXCL: u32 = 0x00000020; + + // 应将文件视为文本 + /// Indicates that the server should treat the file as text and + /// convert it to the canonical newline convention in use. + /// + pub const SSH_FXF_TEXT: u32 = 0x00000040; +} + /// 密钥交换后进行HASH时候需要的常量值 pub(crate) const ALPHABET: [u8; 6] = [b'A', b'B', b'C', b'D', b'E', b'F']; diff --git a/src/sftp/mod.rs b/src/sftp/mod.rs deleted file mode 100644 index 07f9128..0000000 --- a/src/sftp/mod.rs +++ /dev/null @@ -1 +0,0 @@ -mod req_id; diff --git a/src/sftp/req_id.rs b/src/sftp/req_id.rs deleted file mode 100644 index 94386b9..0000000 --- a/src/sftp/req_id.rs +++ /dev/null @@ -1,18 +0,0 @@ -pub(crate) struct ReqId(u32); - -impl ReqId { - pub(crate) fn new() -> Self { - ReqId(0) - } - - pub(crate) fn get(&mut self) -> u32 { - let id = self.0; - let n_id = id + 1; - if n_id > u32::MAX { - self.0 = 0; - } else { - self.0 = n_id - } - id - } -}