From 415b5ba56f08923560d2bf9c874e70de719576e2 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Fri, 14 Dec 2018 16:51:11 +0100 Subject: Use bitflags 1.X (#97) Also removes the reexported flags from the crate root, since they're now assoc. consts and it would be a bit weird to reexport those. (breaking change) --- src/lib.rs | 5 ++--- src/sftp.rs | 75 +++++++++++++++++++++++++++++++------------------------------ 2 files changed, 40 insertions(+), 40 deletions(-) (limited to 'src') diff --git a/src/lib.rs b/src/lib.rs index b08915c..1401a7b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -141,9 +141,8 @@ pub use error::Error; pub use knownhosts::{KnownHosts, Hosts, Host}; pub use listener::Listener; pub use session::{Session, ScpFileStat}; -pub use sftp::{Sftp, OpenFlags, READ, WRITE, APPEND, CREATE, TRUNCATE}; -pub use sftp::{EXCLUSIVE, OpenType, File, FileStat, FileType}; -pub use sftp::{RenameFlags, ATOMIC, OVERWRITE, NATIVE}; +pub use sftp::{Sftp, OpenFlags, RenameFlags}; +pub use sftp::{OpenType, File, FileStat, FileType}; pub use DisconnectCode::{HostNotAllowedToConnect, ProtocolError}; pub use DisconnectCode::{KeyExchangeFailed, Reserved, MacError, CompressionError}; pub use DisconnectCode::{ServiceNotAvailable, ProtocolVersionNotSupported}; diff --git a/src/sftp.rs b/src/sftp.rs index 0ef291d..4e1eb56 100644 --- a/src/sftp.rs +++ b/src/sftp.rs @@ -54,43 +54,42 @@ pub struct FileType { } bitflags! { - #[doc = "Options that can be used to configure how a file is opened"] - pub flags OpenFlags: c_ulong { - #[doc = "Open the file for reading."] - const READ = raw::LIBSSH2_FXF_READ, - #[doc = "Open the file for writing. If both this and Read are \ - specified, the file is opened for both reading and writing"] - const WRITE = raw::LIBSSH2_FXF_WRITE, - #[doc = "Force all writes to append data at the end of the file."] - const APPEND = raw::LIBSSH2_FXF_APPEND, - #[doc = "If this flag is specified, then a new file will be created if \ - one does not already exist (if Truncate is specified, the new \ - file will be truncated to zero length if it previously \ - exists) "] - const CREATE = raw::LIBSSH2_FXF_CREAT, - #[doc = "Forces an existing file with the same name to be truncated to \ - zero length when creating a file by specifying `Create`. \ - Using this flag implies the `Create` flag."] - const TRUNCATE = raw::LIBSSH2_FXF_TRUNC | CREATE.bits, - #[doc = "Causes the request to fail if the named file already exists. \ - Using this flag implies the `Create` flag."] - const EXCLUSIVE = raw::LIBSSH2_FXF_EXCL | CREATE.bits + /// Options that can be used to configure how a file is opened + pub struct OpenFlags: c_ulong { + /// Open the file for reading. + const READ = raw::LIBSSH2_FXF_READ; + /// Open the file for writing. If both this and `Read` are specified, + /// the file is opened for both reading and writing. + const WRITE = raw::LIBSSH2_FXF_WRITE; + /// Force all writes to append data at the end of the file. + const APPEND = raw::LIBSSH2_FXF_APPEND; + /// If this flag is specified, then a new file will be created if one + /// does not already exist (if `Truncate` is specified, the new file + /// will be truncated to zero length if it previously exists). + const CREATE = raw::LIBSSH2_FXF_CREAT; + /// Forces an existing file with the same name to be truncated to zero + /// length when creating a file by specifying `Create`. Using this flag + /// implies the `Create` flag. + const TRUNCATE = raw::LIBSSH2_FXF_TRUNC | Self::CREATE.bits; + /// Causes the request to fail if the named file already exists. Using + /// this flag implies the `Create` flag. + const EXCLUSIVE = raw::LIBSSH2_FXF_EXCL | Self::CREATE.bits; } } bitflags! { - #[doc = "Options to `Sftp::rename`"] - pub flags RenameFlags: c_long { - #[doc = "In a rename operation, overwrite the destination if it \ - already exists. If this flag is not present then it is an \ - error if the destination already exists"] - const OVERWRITE = raw::LIBSSH2_SFTP_RENAME_OVERWRITE, - #[doc = "Inform the remote that an atomic rename operation is \ - desired if available"] - const ATOMIC = raw::LIBSSH2_SFTP_RENAME_ATOMIC, - #[doc = "Inform the remote end that the native system calls for \ - renaming should be used"] - const NATIVE = raw::LIBSSH2_SFTP_RENAME_NATIVE + /// Options to `Sftp::rename`. + pub struct RenameFlags: c_long { + /// In a rename operation, overwrite the destination if it already + /// exists. If this flag is not present then it is an error if the + /// destination already exists. + const OVERWRITE = raw::LIBSSH2_SFTP_RENAME_OVERWRITE; + /// Inform the remote that an atomic rename operation is desired if + /// available. + const ATOMIC = raw::LIBSSH2_SFTP_RENAME_ATOMIC; + /// Inform the remote end that the native system calls for renaming + /// should be used. + const NATIVE = raw::LIBSSH2_SFTP_RENAME_NATIVE; } } @@ -125,17 +124,17 @@ impl<'sess> Sftp<'sess> { /// Helper to open a file in the `Read` mode. pub fn open(&self, filename: &Path) -> Result { - self.open_mode(filename, READ, 0o644, OpenType::File) + self.open_mode(filename, OpenFlags::READ, 0o644, OpenType::File) } /// Helper to create a file in write-only mode with truncation. pub fn create(&self, filename: &Path) -> Result { - self.open_mode(filename, WRITE | TRUNCATE, 0o644, OpenType::File) + self.open_mode(filename, OpenFlags::WRITE | OpenFlags::TRUNCATE, 0o644, OpenType::File) } /// Helper to open a directory for reading its contents. pub fn opendir(&self, dirname: &Path) -> Result { - self.open_mode(dirname, READ, 0, OpenType::Dir) + self.open_mode(dirname, OpenFlags::READ, 0, OpenType::Dir) } /// Convenience function to read the files in a directory. @@ -292,7 +291,9 @@ impl<'sess> Sftp<'sess> { /// If no flags are specified then all flags are used. pub fn rename(&self, src: &Path, dst: &Path, flags: Option) -> Result<(), Error> { - let flags = flags.unwrap_or(ATOMIC | OVERWRITE | NATIVE); + let flags = flags.unwrap_or( + RenameFlags::ATOMIC | RenameFlags::OVERWRITE | RenameFlags::NATIVE + ); let src = try!(util::path2bytes(src)); let dst = try!(util::path2bytes(dst)); self.rc(unsafe { -- cgit v1.2.3