Skip to content

Commit

Permalink
Add OpenFlags for SFTP file open options
Browse files Browse the repository at this point in the history
  • Loading branch information
nathaniel-daniel committed Nov 28, 2023
1 parent 0baf910 commit 7e45d8b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
1 change: 1 addition & 0 deletions libssh-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ license = "MIT"

[dependencies]
bitflags = "1.3"
libc = "0.2"
libssh-rs-sys = { version = "0.2.2", path = "../libssh-rs-sys" }
thiserror = "1.0"
openssl-sys = "0.9.93"
Expand Down
30 changes: 28 additions & 2 deletions libssh-rs/src/sftp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,12 +218,12 @@ impl Sftp {
pub fn open(
&self,
filename: &str,
accesstype: c_int,
accesstype: OpenFlags,
mode: sys::mode_t,
) -> SshResult<SftpFile> {
let filename = CString::new(filename)?;
let (_sess, sftp) = self.lock_session();
let res = unsafe { sys::sftp_open(sftp, filename.as_ptr(), accesstype, mode) };
let res = unsafe { sys::sftp_open(sftp, filename.as_ptr(), accesstype.bits(), mode) };
if res.is_null() {
Err(Error::Sftp(SftpError::from_session(sftp)))
} else {
Expand Down Expand Up @@ -656,3 +656,29 @@ pub enum FileType {
Directory,
Unknown,
}

bitflags::bitflags! {
/// Bitflags that indicate options for opening a sftp file.
pub struct OpenFlags: c_int {
/// The file should be opened as read-only.
const READ_ONLY = libc::O_RDONLY;
/// The file should be opened as write-only.
const WRITE_ONLY = libc::O_WRONLY;
/// The file should be opened as read and write.
///
/// Note that this is a different value than `READ_ONLY | WRITE_ONLY`, which is a logic error.
const READ_WRITE = libc::O_RDWR;
/// Create the file if it does not exist.
const CREATE = libc::O_CREAT;
/// When used with `CREATE`, this flag ensures that a new file is created.
const EXCLUSIVE = libc::O_EXCL;
/// If the file exists, truncate it.
const TRUNCATE = libc::O_TRUNC;
/// Before each write, the file offset is set to the end of the file.
const APPEND = libc::O_APPEND;
/// Create a new file, failing if it already exists.
///
/// This is an alias for `CREATE | EXCLUSIVE`.
const CREATE_NEW = libc::O_CREAT | libc::O_EXCL;
}
}

0 comments on commit 7e45d8b

Please sign in to comment.