Skip to content

Commit

Permalink
sftp file flags
Browse files Browse the repository at this point in the history
  • Loading branch information
1148118271 committed Jan 29, 2023
1 parent fbbb389 commit 498f62e
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 41 deletions.
4 changes: 4 additions & 0 deletions src/channel/local/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<u8>> {
Expand Down
55 changes: 33 additions & 22 deletions src/channel/local/channel_sftp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<S: Read + Write> {
channel: Channel<S>,
Expand Down Expand Up @@ -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<P: AsRef<OsStr> + ?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<P: AsRef<OsStr> + ?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<S> Deref for ChannelSftp<S>
Expand Down
48 changes: 48 additions & 0 deletions src/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
1 change: 0 additions & 1 deletion src/sftp/mod.rs

This file was deleted.

18 changes: 0 additions & 18 deletions src/sftp/req_id.rs

This file was deleted.

0 comments on commit 498f62e

Please sign in to comment.