Skip to content

Implement RegistryManager #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ exclude.workspace = true
readme.workspace = true

[dependencies]
bottles-core = { workspace = true }
bottles-core = { path = "../next-core" }
tonic = { workspace = true }
tokio = { workspace = true }

Expand All @@ -27,3 +27,6 @@ features = [
"Win32_UI_WindowsAndMessaging",
"Win32_UI_Shell",
]

[dependencies.windows-registry]
version = "0.5.1"
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,15 @@
# next-winebridge
The source code of the Next version of WineBridge

## Testing
Tests for winebridge are meant to be run inside wine. Generate the test executables using the following command:
```bash
cargo test --no-run --lib
```

This will generate the test executables in the `target/$TARGET/debug/deps` which can be run using wine as follows:
```bash
wine target/$TARGET/debug/deps/next-winebridge-*.exe --test-threads=1
```

The `--test-threads=1` flag is required becase parallel operations on Wine registry can interfere with each other and cause tests to fail. This issue is not specific to this project.
134 changes: 134 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
mod processes;
mod registry;

use bottles_core::proto::{self, wine_bridge_server::WineBridge};
use processes::{manager::ProcessManager, process::ProcessIdentifier};
use registry::manager::{to_proto_reg_val, to_reg_data, KeyExtension, RegistryManager};
use windows::{
core::{s, PCSTR},
Win32::UI::WindowsAndMessaging::{MessageBoxA, MB_OK},
};
use windows_registry::Key;

#[derive(Debug, Default)]
pub struct WineBridgeService;
Expand Down Expand Up @@ -87,4 +90,135 @@ impl WineBridge for WineBridgeService {

Ok(tonic::Response::new(proto::KillProcessResponse {}))
}

async fn create_registry_key(
&self,
request: tonic::Request<proto::CreateRegistryKeyRequest>,
) -> Result<tonic::Response<proto::MessageResponse>, tonic::Status> {
let input = request.get_ref();
let hive = input
.hive
.parse()
.map_err(|e| tonic::Status::invalid_argument(format!("Invalid hive: {:?}", e)))?;
let subkey = std::path::Path::new(input.subkey.as_str());

RegistryManager
.create_key(hive, subkey)
.map(|_| tonic::Response::new(proto::MessageResponse { success: true }))
.map_err(|e| tonic::Status::internal(format!("Failed to create registry key: {:?}", e)))
}

async fn delete_registry_key(
&self,
request: tonic::Request<proto::DeleteRegistryKeyRequest>,
) -> Result<tonic::Response<proto::MessageResponse>, tonic::Status> {
let input = request.get_ref();
let hive = input
.hive
.parse()
.map_err(|e| tonic::Status::invalid_argument(format!("Invalid hive: {:?}", e)))?;
let subkey = std::path::Path::new(input.subkey.as_str());

RegistryManager
.delete_key(hive, subkey)
.map(|_| tonic::Response::new(proto::MessageResponse { success: true }))
.map_err(|e| tonic::Status::internal(format!("Failed to create registry key: {:?}", e)))
}

async fn get_registry_key(
&self,
request: tonic::Request<proto::GetRegistryKeyRequest>,
) -> Result<tonic::Response<proto::RegistryKey>, tonic::Status> {
let input = request.get_ref();
let hive = input
.hive
.parse()
.map_err(|e| tonic::Status::invalid_argument(format!("Invalid hive: {:?}", e)))?;
let subkey = std::path::Path::new(input.subkey.as_str());

let key = RegistryManager
.key(hive, subkey)
.map_err(|e| {
tonic::Status::internal(format!("Failed to create registry key: {:?}", e))
})?
.as_registry_key(hive, subkey);

Ok(tonic::Response::new(key))
}

async fn get_registry_key_value(
&self,
request: tonic::Request<proto::RegistryKeyRequest>,
) -> Result<tonic::Response<proto::RegistryValue>, tonic::Status> {
let input = request.get_ref();
let name = input.name.as_str();
let hive = input
.hive
.parse()
.map_err(|e| tonic::Status::invalid_argument(format!("Invalid hive: {:?}", e)))?;
let subkey = std::path::Path::new(input.subkey.as_str());

let key = RegistryManager.key(hive, subkey).map_err(|e| {
tonic::Status::internal(format!("Failed to create registry key: {:?}", e))
})?;

let value = key.value(name).map_err(|e| {
tonic::Status::internal(format!("Failed to get registry value: {:?}", e))
})?;

Ok(tonic::Response::new(to_proto_reg_val(value)))
}

async fn set_registry_key_value(
&self,
request: tonic::Request<proto::SetRegistryKeyValueRequest>,
) -> Result<tonic::Response<proto::MessageResponse>, tonic::Status> {
let input = request.get_ref();

let (name, key) = input
.key
.as_ref()
.map(|k| {
let hive = k.hive.parse().unwrap();
let subkey = std::path::Path::new(k.subkey.as_str());
(k.name.clone(), RegistryManager.key(hive, subkey).unwrap())
})
.ok_or_else(|| tonic::Status::invalid_argument("Key is required"))?;

let value = input
.value
.as_ref()
.ok_or_else(|| tonic::Status::invalid_argument("Value is required"))?;

key.create_value(
name.as_str(),
to_reg_data(value.r#type(), value.data.clone()),
)
.map(|_| tonic::Response::new(proto::MessageResponse { success: true }))
.map_err(|e| tonic::Status::internal(format!("Failed to set registry value: {:?}", e)))
}

async fn delete_registry_key_value(
&self,
request: tonic::Request<proto::RegistryKeyRequest>,
) -> Result<tonic::Response<proto::MessageResponse>, tonic::Status> {
let input = request.get_ref();
let name = input.name.as_str();
let hive = input
.hive
.parse()
.map_err(|e| tonic::Status::invalid_argument(format!("Invalid hive: {:?}", e)))?;
let subkey = std::path::Path::new(input.subkey.as_str());

RegistryManager
.key(hive, subkey)
.map_err(|e| {
tonic::Status::internal(format!("Failed to create registry key: {:?}", e))
})?
.remove_value(name)
.map(|_| tonic::Response::new(proto::MessageResponse { success: true }))
.map_err(|e| {
tonic::Status::internal(format!("Failed to delete registry value: {:?}", e))
})
}
}
Loading