Skip to content

Commit

Permalink
Merge branch with list_mounts.
Browse files Browse the repository at this point in the history
  • Loading branch information
panhania committed Oct 20, 2023
2 parents 99c169f + c664a27 commit 64180f8
Show file tree
Hide file tree
Showing 14 changed files with 137 additions and 31 deletions.
8 changes: 4 additions & 4 deletions crates/ospect/src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,9 @@ where
#[derive(Debug)]
pub struct Mount {
/// Name of the mounted device.
pub source: String,
pub name: String,
/// Mount point, i.e., where the mounted filesystem is available.
pub target: std::path::PathBuf,
pub path: std::path::PathBuf,
/// Type of the mounted filesystem (e.g. `ext4`, `ramfs`, `proc`).
pub fs_type: String,
}
Expand Down Expand Up @@ -356,7 +356,7 @@ mod tests {
.unwrap()
.map(Result::unwrap);

assert!(mounts.find(|mount| mount.target == Path::new("/")).is_some());
assert!(mounts.find(|mount| mount.path == Path::new("/")).is_some());
}

#[cfg(target_family = "windows")]
Expand All @@ -374,7 +374,7 @@ mod tests {
system_drive_path.push(std::path::MAIN_SEPARATOR_STR);

assert! {
mounts.find(|mount| &mount.target == &system_drive_path).is_some()
mounts.find(|mount| &mount.path == &system_drive_path).is_some()
};
}
}
24 changes: 12 additions & 12 deletions crates/ospect/src/fs/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,16 +222,16 @@ impl<R: std::io::Read> Mounts<R> {

// There is more data in the file but we don't care for the time being
// and only "parse" the first three columns.
let source = cols.next()
let name = cols.next()
.ok_or_else(|| std::io::ErrorKind::InvalidData)?;
let target = cols.next()
let path = cols.next()
.ok_or_else(|| std::io::ErrorKind::InvalidData)?;
let fs_type = cols.next()
.ok_or_else(|| std::io::ErrorKind::InvalidData)?;

Ok(Mount {
source: source.into(),
target: target.into(),
name: name.into(),
path: path.into(),
fs_type: fs_type.into(),
})
}
Expand Down Expand Up @@ -414,23 +414,23 @@ proc /proc proc rw,nosuid,nodev,noexec,relatime 0 0
let mut mounts = Mounts::new(MTAB.as_bytes());

let mount = mounts.next().unwrap().unwrap();
assert_eq!(mount.source, "sysfs");
assert_eq!(mount.target, Path::new("/sys"));
assert_eq!(mount.name, "sysfs");
assert_eq!(mount.path, Path::new("/sys"));
assert_eq!(mount.fs_type, "sysfs");

let mount = mounts.next().unwrap().unwrap();
assert_eq!(mount.source, "proc");
assert_eq!(mount.target, Path::new("/proc"));
assert_eq!(mount.name, "proc");
assert_eq!(mount.path, Path::new("/proc"));
assert_eq!(mount.fs_type, "proc");

let mount = mounts.next().unwrap().unwrap();
assert_eq!(mount.source, "/dev/foobar");
assert_eq!(mount.target, Path::new("/"));
assert_eq!(mount.name, "/dev/foobar");
assert_eq!(mount.path, Path::new("/"));
assert_eq!(mount.fs_type, "ext4");

let mount = mounts.next().unwrap().unwrap();
assert_eq!(mount.source, "/dev/quux");
assert_eq!(mount.target, Path::new("/usr/quux"));
assert_eq!(mount.name, "/dev/quux");
assert_eq!(mount.path, Path::new("/usr/quux"));
assert_eq!(mount.fs_type, "ext4");

assert!(mounts.next().is_none());
Expand Down
8 changes: 4 additions & 4 deletions crates/ospect/src/fs/macos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,11 +244,11 @@ pub fn mounts() -> std::io::Result<impl Iterator<Item = std::io::Result<Mount>>>
// safety invariants required by the `from_ptr` call are met. Note that
// `statfs` is now on the stack, so the lifetime of this reference is
// valid until the end of this scope.
let source = unsafe {
let name = unsafe {
std::ffi::CStr::from_ptr(statfs.f_mntfromname.as_ptr())
}.to_string_lossy();
// SAFETY: Same as above.
let target = unsafe {
let path = unsafe {
std::ffi::CStr::from_ptr(statfs.f_mntonname.as_ptr())
}.to_bytes();
// SAFETY: Same as above.
Expand All @@ -259,8 +259,8 @@ pub fn mounts() -> std::io::Result<impl Iterator<Item = std::io::Result<Mount>>>
use std::os::unix::ffi::OsStrExt as _;

mounts.push(Mount {
source: source.into_owned(),
target: PathBuf::from(OsStr::from_bytes(target)),
name: name.into_owned(),
path: PathBuf::from(OsStr::from_bytes(path)),
fs_type: fs_type.into_owned(),
});
}
Expand Down
4 changes: 2 additions & 2 deletions crates/ospect/src/fs/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ pub fn mounts() -> std::io::Result<impl Iterator<Item = std::io::Result<Mount>>>

for mount_point in VolumeMountPoints::new(&name_buf)? {
results.push(Ok(Mount {
source: OsString::from_wide(&name_buf[0..name_len])
name: OsString::from_wide(&name_buf[0..name_len])
.to_string_lossy().into_owned(),
target: mount_point,
path: mount_point,
fs_type: OsString::from_wide(&fs_type_buf[0..fs_type_len])
.to_string_lossy().into_owned(),
}));
Expand Down
1 change: 1 addition & 0 deletions crates/rrg-proto/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const PROTOS_V2: &'static [&'static str] = &[
"../../proto/rrg/action/get_system_metadata.proto",
"../../proto/rrg/action/list_connections.proto",
"../../proto/rrg/action/list_interfaces.proto",
"../../proto/rrg/action/list_mounts.proto",
];

fn main() {
Expand Down
12 changes: 12 additions & 0 deletions crates/rrg-proto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,18 @@ pub mod v2 {
}
}

impl From<ospect::fs::Mount> for fs::Mount {

fn from(mount: ospect::fs::Mount) -> fs::Mount {
let mut proto = fs::Mount::default();
proto.set_name(mount.name);
proto.set_path(mount.path.into());
proto.set_fs_type(mount.fs_type);

proto
}
}

impl From<std::net::Ipv4Addr> for net::IpAddress {

fn from(addr: std::net::Ipv4Addr) -> net::IpAddress {
Expand Down
2 changes: 2 additions & 0 deletions crates/rrg/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ default = [
"action-get_filesystem_timeline",
"action-list_connections",
"action-list_interfaces",
"action-list_mounts",

# These actions are deprecated (awaiting migration to the new protocol).
"action-insttime",
Expand All @@ -31,6 +32,7 @@ action-get_file_contents = ["dep:sha2"]
action-get_filesystem_timeline = ["dep:flate2", "dep:sha2"]
action-list_connections = []
action-list_interfaces = []
action-list_mounts = []

# These actions are deprecated (awaiting migration to the new protocol).
action-insttime = []
Expand Down
7 changes: 7 additions & 0 deletions crates/rrg/src/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ pub mod list_connections;
#[cfg(feature = "action-list_interfaces")]
pub mod list_interfaces;

#[cfg(feature = "action-list_mounts")]
pub mod list_mounts;

use log::info;

/// Dispatches the given `request` to an appropriate action handler.
Expand Down Expand Up @@ -86,6 +89,10 @@ where
ListInterfaces => {
handle(session, request, self::list_interfaces::handle)
}
#[cfg(feature = "action-list_mounts")]
ListMounts => {
handle(session, request, self::list_mounts::handle)
}
// We allow `unreachable_patterns` because otherwise we get a warning if
// we compile with all the actions enabled.
#[allow(unreachable_patterns)]
Expand Down
11 changes: 2 additions & 9 deletions crates/rrg/src/action/deprecated/filesystems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ impl crate::response::Item for Response {
// when `mount_point` and `device` fields of `Filesystem` message
// will have `bytes` type instead of `string`.
let mut proto = rrg_proto::sysinfo::Filesystem::new();
proto.set_device(self.mount_info.source);
proto.set_mount_point(self.mount_info.target.to_string_lossy().into_owned());
proto.set_device(self.mount_info.name);
proto.set_mount_point(self.mount_info.path.to_string_lossy().into_owned());
proto.set_field_type(self.mount_info.fs_type);

proto
Expand All @@ -103,13 +103,6 @@ mod tests {

use super::*;

#[test]
fn test_if_any_filesystem_exists() {
let mut session = session::FakeSession::new();
assert!(handle(&mut session, ()).is_ok());
assert_ne!(session.reply_count(), 0);
}

#[cfg(feature = "test-fuse")]
#[test]
fn test_fuse_filesystem() {
Expand Down
61 changes: 61 additions & 0 deletions crates/rrg/src/action/list_mounts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright 2023 Google LLC
//
// Use of this source code is governed by an MIT-style license that can be found
// in the LICENSE file or at https://opensource.org/licenses/MIT.

/// A result of the `list_mounts` action.
struct Item {
// Information about the individual filesystem mount.
mount: ospect::fs::Mount,
}

// Handles invocations of the `list_mounts` action.
pub fn handle<S>(session: &mut S, _: ()) -> crate::session::Result<()>
where
S: crate::session::Session,
{
let mounts = ospect::fs::mounts()
.map_err(crate::session::Error::action)?;

for mount in mounts {
let mount = match mount {
Ok(mount) => mount,
Err(error) => {
log::warn!("failed to obtain mount information: {}", error);
continue;
}
};

session.reply(Item {
mount,
})?;
}

Ok(())
}

impl crate::response::Item for Item {

type Proto = rrg_proto::v2::list_mounts::Result;

fn into_proto(self) -> rrg_proto::v2::list_mounts::Result {
let mut proto = rrg_proto::v2::list_mounts::Result::default();
proto.set_mount(self.mount.into());

proto
}
}

#[cfg(test)]
mod tests {

use super::*;

#[test]
fn handle_some_mount() {
let mut session = crate::session::FakeSession::new();
assert!(handle(&mut session, ()).is_ok());

assert!(session.reply_count() > 0);
}
}
4 changes: 4 additions & 0 deletions crates/rrg/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ pub enum Action {
ListNamedPipes,
/// List network interfaces available on the system.
ListInterfaces,
/// List filesystem mounts available on the system.
ListMounts,
/// List users available on the system.
ListUsers,
/// Get the snapshot of the entire filesystem.
Expand All @@ -52,6 +54,7 @@ impl std::fmt::Display for Action {
Action::ListConnections => write!(fmt, "list_connections"),
Action::ListNamedPipes => write!(fmt, "list_named_pipes"),
Action::ListInterfaces => write!(fmt, "list_interfaces"),
Action::ListMounts => write!(fmt, "list_mounts"),
Action::ListUsers => write!(fmt, "list_users"),
Action::GetFilesystemTimeline => write!(fmt, "get_filesystem_timeline"),
}
Expand Down Expand Up @@ -95,6 +98,7 @@ impl TryFrom<rrg_proto::v2::rrg::Action> for Action {
LIST_CONNECTIONS => Ok(Action::ListConnections),
LIST_NAMED_PIPES => Ok(Action::ListNamedPipes),
LIST_INTERFACES => Ok(Action::ListInterfaces),
LIST_MOUNTS => Ok(Action::ListMounts),
LIST_USERS => Ok(Action::ListUsers),
GET_FILESYSTEM_TIMELINE => Ok(Action::GetFilesystemTimeline),
_ => {
Expand Down
2 changes: 2 additions & 0 deletions proto/rrg.proto
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ enum Action {
GET_FILESYSTEM_TIMELINE = 10;
// List network interfaces available on the system.
LIST_INTERFACES = 11;
// List filesystem mounts available on the system.
LIST_MOUNTS = 12;

// TODO: Define more actions that should be supported.

Expand Down
14 changes: 14 additions & 0 deletions proto/rrg/action/list_mounts.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright 2023 Google LLC
//
// Use of this source code is governed by an MIT-style license that can be found
// in the LICENSE file or at https://opensource.org/licenses/MIT.
syntax = "proto3";

package rrg.action.list_mounts;

import "rrg/fs.proto";

message Result {
// Information about the individual filesystem mount.
rrg.fs.Mount mount = 1;
}
10 changes: 10 additions & 0 deletions proto/rrg/fs.proto
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,13 @@ message FileExtAttr {
// This can be an arbitrary sequence of bytes both on macOS and Linux.
bytes value = 2;
}

// Information about a mounted filesystem.
message Mount {
// Name or other identifier of the mounted device.
string name = 1;
// Path at which the mounted filesystem is available (a mount point).
Path path = 2;
// Type of the mounted filesystem (e.g. `ext4`, `ramfs`, `NTFS`).
string fs_type = 3;
}

0 comments on commit 64180f8

Please sign in to comment.