From c4707badd013c37ff3c1c0847d752bf69f12f86a Mon Sep 17 00:00:00 2001 From: Chirantan Ekbote Date: Mon, 25 May 2020 18:31:51 +0900 Subject: devices: fs: Refactor ioctl handling code Now that the ioctl number method is const we can use a match statement rather than a series of if-else expressions. BUG=b:157189438 TEST=unit tests Change-Id: I9839f2de842ec512811101c07445ca5f99f3fe2f Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2214963 Tested-by: kokoro Commit-Queue: Chirantan Ekbote Reviewed-by: Zach Reizner Reviewed-by: Stephen Barber --- devices/src/virtio/fs/passthrough.rs | 50 +++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 23 deletions(-) (limited to 'devices/src/virtio/fs/passthrough.rs') diff --git a/devices/src/virtio/fs/passthrough.rs b/devices/src/virtio/fs/passthrough.rs index bcc7c5d..189b0c3 100644 --- a/devices/src/virtio/fs/passthrough.rs +++ b/devices/src/virtio/fs/passthrough.rs @@ -1694,34 +1694,38 @@ impl FileSystem for PassthroughFs { out_size: u32, r: R, ) -> io::Result { + const GET_ENCRYPTION_POLICY: u32 = FS_IOC_GET_ENCRYPTION_POLICY() as u32; + const SET_ENCRYPTION_POLICY: u32 = FS_IOC_SET_ENCRYPTION_POLICY() as u32; + // Normally, we wouldn't need to retry the FS_IOC_GET_ENCRYPTION_POLICY and // FS_IOC_SET_ENCRYPTION_POLICY ioctls. Unfortunately, the I/O directions for both of them // are encoded backwards so they can only be handled as unrestricted fuse ioctls. - if cmd == FS_IOC_GET_ENCRYPTION_POLICY() as u32 { - if out_size < size_of::() as u32 { - let input = Vec::new(); - let output = vec![IoctlIovec { - base: arg, - len: size_of::() as u64, - }]; - Ok(IoctlReply::Retry { input, output }) - } else { - self.get_encryption_policy(handle) + match cmd { + GET_ENCRYPTION_POLICY => { + if out_size < size_of::() as u32 { + let input = Vec::new(); + let output = vec![IoctlIovec { + base: arg, + len: size_of::() as u64, + }]; + Ok(IoctlReply::Retry { input, output }) + } else { + self.get_encryption_policy(handle) + } } - } else if cmd == FS_IOC_SET_ENCRYPTION_POLICY() as u32 { - if in_size < size_of::() as u32 { - let input = vec![IoctlIovec { - base: arg, - len: size_of::() as u64, - }]; - let output = Vec::new(); - Ok(IoctlReply::Retry { input, output }) - } else { - self.set_encryption_policy(handle, r) + SET_ENCRYPTION_POLICY => { + if in_size < size_of::() as u32 { + let input = vec![IoctlIovec { + base: arg, + len: size_of::() as u64, + }]; + let output = Vec::new(); + Ok(IoctlReply::Retry { input, output }) + } else { + self.set_encryption_policy(handle, r) + } } - } else { - // Did you know that a file/directory is not a TTY? - Err(io::Error::from_raw_os_error(libc::ENOTTY)) + _ => Err(io::Error::from_raw_os_error(libc::ENOTTY)), } } -- cgit 1.4.1