@@ -218,12 +218,12 @@ impl Sftp {
218
218
pub fn open (
219
219
& self ,
220
220
filename : & str ,
221
- accesstype : c_int ,
221
+ accesstype : OpenFlags ,
222
222
mode : sys:: mode_t ,
223
223
) -> SshResult < SftpFile > {
224
224
let filename = CString :: new ( filename) ?;
225
225
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) } ;
227
227
if res. is_null ( ) {
228
228
Err ( Error :: Sftp ( SftpError :: from_session ( sftp) ) )
229
229
} else {
@@ -656,3 +656,29 @@ pub enum FileType {
656
656
Directory ,
657
657
Unknown ,
658
658
}
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