Skip to content

Commit 478109b

Browse files
nathaniel-danielwez
authored andcommitted
Add OpenFlags for SFTP file open options
1 parent 0baf910 commit 478109b

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

libssh-rs/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ license = "MIT"
1010

1111
[dependencies]
1212
bitflags = "1.3"
13+
libc = "0.2"
1314
libssh-rs-sys = { version = "0.2.2", path = "../libssh-rs-sys" }
1415
thiserror = "1.0"
1516
openssl-sys = "0.9.93"

libssh-rs/src/sftp.rs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,12 +218,12 @@ impl Sftp {
218218
pub fn open(
219219
&self,
220220
filename: &str,
221-
accesstype: c_int,
221+
accesstype: OpenFlags,
222222
mode: sys::mode_t,
223223
) -> SshResult<SftpFile> {
224224
let filename = CString::new(filename)?;
225225
let (_sess, sftp) = self.lock_session();
226-
let res = unsafe { sys::sftp_open(sftp, filename.as_ptr(), accesstype, mode) };
226+
let res = unsafe { sys::sftp_open(sftp, filename.as_ptr(), accesstype.bits(), mode) };
227227
if res.is_null() {
228228
Err(Error::Sftp(SftpError::from_session(sftp)))
229229
} else {
@@ -656,3 +656,29 @@ pub enum FileType {
656656
Directory,
657657
Unknown,
658658
}
659+
660+
bitflags::bitflags! {
661+
/// Bitflags that indicate options for opening a sftp file.
662+
pub struct OpenFlags: c_int {
663+
/// The file should be opened as read-only.
664+
const READ_ONLY = libc::O_RDONLY;
665+
/// The file should be opened as write-only.
666+
const WRITE_ONLY = libc::O_WRONLY;
667+
/// The file should be opened as read and write.
668+
///
669+
/// Note that this is a different value than `READ_ONLY | WRITE_ONLY`, which is a logic error.
670+
const READ_WRITE = libc::O_RDWR;
671+
/// Create the file if it does not exist.
672+
const CREATE = libc::O_CREAT;
673+
/// When used with `CREATE`, this flag ensures that a new file is created.
674+
const EXCLUSIVE = libc::O_EXCL;
675+
/// If the file exists, truncate it.
676+
const TRUNCATE = libc::O_TRUNC;
677+
/// Before each write, the file offset is set to the end of the file.
678+
const APPEND = libc::O_APPEND;
679+
/// Create a new file, failing if it already exists.
680+
///
681+
/// This is an alias for `CREATE | EXCLUSIVE`.
682+
const CREATE_NEW = libc::O_CREAT | libc::O_EXCL;
683+
}
684+
}

0 commit comments

Comments
 (0)