summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRina Fujino <18257209+rina23q@users.noreply.github.com>2022-08-08 10:14:39 +0200
committerGitHub <noreply@github.com>2022-08-08 10:14:39 +0200
commit4e2d66722c3b196ccedbd809b7db0464994d8b02 (patch)
tree1a9d2339183daee06fa11e621325220d0fcd4b89
parentfd178124731a39aac6384be65218a33e4e137480 (diff)
parent229259a38f41fb2686031c4bc7748eea324d8ca4 (diff)
Merge pull request #1208 from matthiasbeyer/tedge_util_style_fixes
tedge_util style fixes
-rw-r--r--crates/common/tedge_utils/src/file.rs78
-rw-r--r--crates/core/tedge/src/main.rs23
-rw-r--r--crates/core/tedge_agent/src/agent.rs8
-rw-r--r--crates/core/tedge_mapper/src/az/mapper.rs5
-rw-r--r--crates/core/tedge_mapper/src/c8y/mapper.rs13
-rw-r--r--plugins/c8y_configuration_plugin/src/main.rs26
-rw-r--r--plugins/c8y_log_plugin/src/main.rs44
7 files changed, 111 insertions, 86 deletions
diff --git a/crates/common/tedge_utils/src/file.rs b/crates/common/tedge_utils/src/file.rs
index af7dbbb0..4da9f1bf 100644
--- a/crates/common/tedge_utils/src/file.rs
+++ b/crates/common/tedge_utils/src/file.rs
@@ -34,29 +34,29 @@ pub enum FileError {
}
pub fn create_directory_with_user_group(
- dir: &str,
+ dir: impl AsRef<Path>,
user: &str,
group: &str,
mode: u32,
) -> Result<(), FileError> {
let perm_entry = PermissionEntry::new(Some(user.into()), Some(group.into()), Some(mode));
- perm_entry.create_directory(Path::new(dir))
+ perm_entry.create_directory(dir.as_ref())
}
-pub fn create_directory_with_mode(dir: &str, mode: u32) -> Result<(), FileError> {
+pub fn create_directory_with_mode(dir: impl AsRef<Path>, mode: u32) -> Result<(), FileError> {
let perm_entry = PermissionEntry::new(None, None, Some(mode));
- perm_entry.create_directory(Path::new(dir))
+ perm_entry.create_directory(dir.as_ref())
}
pub fn create_file_with_user_group(
- file: &str,
+ file: impl AsRef<Path>,
user: &str,
group: &str,
mode: u32,
default_content: Option<&str>,
) -> Result<(), FileError> {
let perm_entry = PermissionEntry::new(Some(user.into()), Some(group.into()), Some(mode));
- perm_entry.create_file(Path::new(file), default_content)
+ perm_entry.create_file(file.as_ref(), default_content)
}
#[derive(Debug, PartialEq, Eq, Default, Clone)]
@@ -141,22 +141,18 @@ impl PermissionEntry {
}
pub fn change_user_and_group(file: &Path, user: &str, group: &str) -> Result<(), FileError> {
- let ud = match get_user_by_name(user) {
- Some(user) => user.uid(),
- None => {
- return Err(FileError::UserNotFound { user: user.into() });
- }
- };
+ let ud = get_user_by_name(user)
+ .map(|u| u.uid())
+ .ok_or_else(|| FileError::UserNotFound { user: user.into() })?;
+
let uid = get_metadata(Path::new(file))?.st_uid();
- let gd = match get_group_by_name(group) {
- Some(group) => group.gid(),
- None => {
- return Err(FileError::GroupNotFound {
- group: group.into(),
- });
- }
- };
+ let gd = get_group_by_name(group)
+ .map(|g| g.gid())
+ .ok_or_else(|| FileError::GroupNotFound {
+ group: group.into(),
+ })?;
+
let gid = get_metadata(Path::new(file))?.st_gid();
// if user and group are same as existing, then do not change
@@ -168,12 +164,9 @@ pub fn change_user_and_group(file: &Path, user: &str, group: &str) -> Result<(),
}
fn change_user(file: &Path, user: &str) -> Result<(), FileError> {
- let ud = match get_user_by_name(user) {
- Some(user) => user.uid(),
- None => {
- return Err(FileError::UserNotFound { user: user.into() });
- }
- };
+ let ud = get_user_by_name(user)
+ .map(|u| u.uid())
+ .ok_or_else(|| FileError::UserNotFound { user: user.into() })?;
let uid = get_metadata(Path::new(file))?.st_uid();
@@ -186,14 +179,11 @@ fn change_user(file: &Path, user: &str) -> Result<(), FileError> {
}
fn change_group(file: &Path, group: &str) -> Result<(), FileError> {
- let gd = match get_group_by_name(group) {
- Some(group) => group.gid(),
- None => {
- return Err(FileError::GroupNotFound {
- group: group.into(),
- });
- }
- };
+ let gd = get_group_by_name(group)
+ .map(|g| g.gid())
+ .ok_or_else(|| FileError::GroupNotFound {
+ group: group.into(),
+ })?;
let gid = get_metadata(Path::new(file))?.st_gid();
@@ -243,7 +233,7 @@ mod tests {
let file_path = temp_dir.path().join("file").display().to_string();
let user = whoami::username();
- let _ = create_file_with_user_group(file_path.as_str(), &user, &user, 0o644, None).unwrap();
+ let _ = create_file_with_user_group(&file_path, &user, &user, 0o644, None).unwrap();
assert!(Path::new(file_path.as_str()).exists());
let meta = std::fs::metadata(file_path.as_str()).unwrap();
let perm = meta.permissions();
@@ -264,7 +254,7 @@ mod tests {
// Create a new file with default content
create_file_with_user_group(
- file_path.as_str(),
+ &file_path,
&user,
&user,
0o775,
@@ -282,8 +272,7 @@ mod tests {
let file_path = temp_dir.path().join("file").display().to_string();
let user = whoami::username();
- let err = create_file_with_user_group(file_path.as_str(), "test", &user, 0o775, None)
- .unwrap_err();
+ let err = create_file_with_user_group(&file_path, "test", &user, 0o775, None).unwrap_err();
assert!(err.to_string().contains("User not found"));
}
@@ -294,8 +283,7 @@ mod tests {
let file_path = temp_dir.path().join("file").display().to_string();
let user = whoami::username();
- let err = create_file_with_user_group(file_path.as_str(), &user, "test", 0o775, None)
- .unwrap_err();
+ let err = create_file_with_user_group(&file_path, &user, "test", 0o775, None).unwrap_err();
assert!(err.to_string().contains("Group not found"));
fs::remove_file(file_path.as_str()).unwrap();
@@ -307,7 +295,7 @@ mod tests {
let dir_path = temp_dir.path().join("dir").display().to_string();
let user = whoami::username();
- let _ = create_directory_with_user_group(dir_path.as_str(), &user, &user, 0o775).unwrap();
+ let _ = create_directory_with_user_group(&dir_path, &user, &user, 0o775).unwrap();
assert!(Path::new(dir_path.as_str()).exists());
let meta = fs::metadata(dir_path.as_str()).unwrap();
@@ -323,8 +311,7 @@ mod tests {
let user = whoami::username();
- let err =
- create_directory_with_user_group(dir_path.as_str(), "test", &user, 0o775).unwrap_err();
+ let err = create_directory_with_user_group(dir_path, "test", &user, 0o775).unwrap_err();
assert!(err.to_string().contains("User not found"));
}
@@ -336,8 +323,7 @@ mod tests {
let user = whoami::username();
- let err =
- create_directory_with_user_group(dir_path.as_str(), &user, "test", 0o775).unwrap_err();
+ let err = create_directory_with_user_group(dir_path, &user, "test", 0o775).unwrap_err();
assert!(err.to_string().contains("Group not found"));
}
@@ -348,7 +334,7 @@ mod tests {
let file_path = temp_dir.path().join("file").display().to_string();
let user = whoami::username();
- let _ = create_file_with_user_group(file_path.as_str(), &user, &user, 0o644, None).unwrap();
+ let _ = create_file_with_user_group(&file_path, &user, &user, 0o644, None).unwrap();
assert!(Path::new(file_path.as_str()).exists());
let meta = fs::metadata(file_path.as_str()).unwrap();
diff --git a/crates/core/tedge/src/main.rs b/crates/core/tedge/src/main.rs
index ea5ccd9a..4bf6b5f6 100644
--- a/crates/core/tedge/src/main.rs
+++ b/crates/core/tedge/src/main.rs
@@ -46,20 +46,29 @@ fn main() -> anyhow::Result<()> {
}
}
-fn initialize_tedge(cfg_dir: &Path) -> anyhow::Result<()> {
- let config_dir = cfg_dir.display().to_string();
- create_directory_with_user_group(&config_dir, "tedge", "tedge", 0o775)?;
+fn initialize_tedge(config_dir: &Path) -> anyhow::Result<()> {
+ create_directory_with_user_group(config_dir, "tedge", "tedge", 0o775)?;
create_directory_with_user_group("/var/log/tedge", "tedge", "tedge", 0o775)?;
create_directory_with_user_group(
- &format!("{config_dir}/mosquitto-conf"),
+ format!("{}/mosquitto-conf", config_dir.display()),
"tedge",
"tedge",
0o775,
)?;
- create_directory_with_user_group(&format!("{config_dir}/operations"), "tedge", "tedge", 0o775)?;
- create_directory_with_user_group(&format!("{config_dir}/plugins"), "tedge", "tedge", 0o775)?;
create_directory_with_user_group(
- &format!("{config_dir}/device-certs"),
+ format!("{}/operations", config_dir.display()),
+ "tedge",
+ "tedge",
+ 0o775,
+ )?;
+ create_directory_with_user_group(
+ format!("{}/plugins", config_dir.display()),
+ "tedge",
+ "tedge",
+ 0o775,
+ )?;
+ create_directory_with_user_group(
+ format!("{}/device-certs", config_dir.display()),
"mosquitto",
"mosquitto",
0o775,
diff --git a/crates/core/tedge_agent/src/agent.rs b/crates/core/tedge_agent/src/agent.rs
index 66573692..d4d723c8 100644
--- a/crates/core/tedge_agent/src/agent.rs
+++ b/crates/core/tedge_agent/src/agent.rs
@@ -227,8 +227,12 @@ impl SmAgent {
#[instrument(skip(self), name = "sm-agent")]
pub async fn init(&mut self, config_dir: PathBuf) -> Result<(), anyhow::Error> {
- let cfg_dir = config_dir.as_path().display().to_string();
- create_directory_with_user_group(&format!("{cfg_dir}/.agent"), "tedge", "tedge", 0o775)?;
+ create_directory_with_user_group(
+ format!("{}/.agent", config_dir.display()),
+ "tedge",
+ "tedge",
+ 0o775,
+ )?;
create_directory_with_user_group("/var/log/tedge/agent", "tedge", "tedge", 0o775)?;
info!("Initializing the tedge agent session");
mqtt_channel::init_session(&self.config.mqtt_config).await?;
diff --git a/crates/core/tedge_mapper/src/az/mapper.rs b/crates/core/tedge_mapper/src/az/mapper.rs
index 73ab05a9..57c2ebff 100644
--- a/crates/core/tedge_mapper/src/az/mapper.rs
+++ b/crates/core/tedge_mapper/src/az/mapper.rs
@@ -28,11 +28,10 @@ impl TEdgeComponent for AzureMapper {
AZURE_MAPPER_NAME
}
- async fn init(&self, cfg_dir: &Path) -> Result<(), anyhow::Error> {
+ async fn init(&self, config_dir: &Path) -> Result<(), anyhow::Error> {
info!("Initialize tedge mapper az");
- let config_dir = cfg_dir.display().to_string();
create_directory_with_user_group(
- &format!("{config_dir}/operations/az"),
+ format!("{}/operations/az", config_dir.display()),
"tedge",
"tedge",
0o775,
diff --git a/crates/core/tedge_mapper/src/c8y/mapper.rs b/crates/core/tedge_mapper/src/c8y/mapper.rs
index 24bce5cb..502bbc10 100644
--- a/crates/core/tedge_mapper/src/c8y/mapper.rs
+++ b/crates/core/tedge_mapper/src/c8y/mapper.rs
@@ -51,9 +51,8 @@ impl TEdgeComponent for CumulocityMapper {
async fn init(&self, cfg_dir: &Path) -> Result<(), anyhow::Error> {
info!("Initialize tedge mapper c8y");
- let config_dir = cfg_dir.display().to_string();
- create_directories(&config_dir)?;
- let operations = Operations::try_new(&format!("{config_dir}/operations"), "c8y")?;
+ create_directories(cfg_dir)?;
+ let operations = Operations::try_new(format!("{}/operations", cfg_dir.display()), "c8y")?;
self.init_session(CumulocityMapper::subscriptions(&operations)?)
.await?;
Ok(())
@@ -98,22 +97,22 @@ impl TEdgeComponent for CumulocityMapper {
}
}
-fn create_directories(config_dir: &str) -> Result<(), anyhow::Error> {
+fn create_directories(config_dir: &Path) -> Result<(), anyhow::Error> {
create_directory_with_user_group(
- &format!("{config_dir}/operations/c8y"),
+ format!("{}/operations/c8y", config_dir.display()),
"tedge",
"tedge",
0o775,
)?;
create_file_with_user_group(
- &format!("{config_dir}/operations/c8y/c8y_SoftwareUpdate"),
+ format!("{}/operations/c8y/c8y_SoftwareUpdate", config_dir.display()),
"tedge",
"tedge",
0o644,
None,
)?;
create_file_with_user_group(
- &format!("{config_dir}/operations/c8y/c8y_Restart"),
+ format!("{}/operations/c8y/c8y_Restart", config_dir.display()),
"tedge",
"tedge",
0o644,
diff --git a/plugins/c8y_configuration_plugin/src/main.rs b/plugins/c8y_configuration_plugin/src/main.rs
index 162e9e71..6f9ddf7b 100644
--- a/plugins/c8y_configuration_plugin/src/main.rs
+++ b/plugins/c8y_configuration_plugin/src/main.rs
@@ -250,13 +250,17 @@ async fn process_mqtt_message(
fn init(cfg_dir: PathBuf) -> Result<(), anyhow::Error> {
info!("Creating supported operation files");
- let config_dir = cfg_dir.as_path().display().to_string();
- let () = create_operation_files(config_dir.as_str())?;
+ let () = create_operation_files(&cfg_dir)?;
Ok(())
}
-fn create_operation_files(config_dir: &str) -> Result<(), anyhow::Error> {
- create_directory_with_user_group(&format!("{config_dir}/c8y"), "root", "root", 0o1777)?;
+fn create_operation_files(config_dir: &Path) -> Result<(), anyhow::Error> {
+ create_directory_with_user_group(
+ format!("{}/c8y", config_dir.display()),
+ "root",
+ "root",
+ 0o1777,
+ )?;
let example_config = r#"# Add the configurations to be managed by c8y-configuration-plugin
files = [
@@ -268,7 +272,7 @@ files = [
]"#;
create_file_with_user_group(
- &format!("{config_dir}/c8y/c8y-configuration-plugin.toml"),
+ format!("{}/c8y/c8y-configuration-plugin.toml", config_dir.display()),
"root",
"root",
0o644,
@@ -276,20 +280,26 @@ files = [
)?;
create_directory_with_user_group(
- &format!("{config_dir}/operations/c8y"),
+ format!("{}/operations/c8y", config_dir.display()),
"tedge",
"tedge",
0o775,
)?;
create_file_with_user_group(
- &format!("{config_dir}/operations/c8y/c8y_UploadConfigFile"),
+ format!(
+ "{}/operations/c8y/c8y_UploadConfigFile",
+ config_dir.display()
+ ),
"tedge",
"tedge",
0o644,
None,
)?;
create_file_with_user_group(
- &format!("{config_dir}/operations/c8y/c8y_DownloadConfigFile"),
+ format!(
+ "{}/operations/c8y/c8y_DownloadConfigFile",
+ config_dir.display()
+ ),
"tedge",
"tedge",
0o644,
diff --git a/plugins/c8y_log_plugin/src/main.rs b/plugins/c8y_log_plugin/src/main.rs
index abcfa3a9..b5a7799e 100644
--- a/plugins/c8y_log_plugin/src/main.rs
+++ b/plugins/c8y_log_plugin/src/main.rs
@@ -212,9 +212,7 @@ async fn main() -> Result<(), anyhow::Error> {
fn init(config_dir: &Path, logs_dir: &Path) -> Result<(), anyhow::Error> {
info!("Creating supported operation files");
- let config_dir = config_dir.display().to_string();
- let logs_dir = logs_dir.display().to_string();
- let () = create_init_logs_directories_and_files(config_dir.as_str(), logs_dir.as_str())?;
+ let () = create_init_logs_directories_and_files(config_dir, logs_dir)?;
Ok(())
}
@@ -229,33 +227,53 @@ fn init(config_dir: &Path, logs_dir: &Path) -> Result<(), anyhow::Error> {
/// - CONFIG_DIR/operations/c8y/c8y_LogfileRequest
/// - CONFIG_DIR/c8y/c8y-log-plugin.toml
fn create_init_logs_directories_and_files(
- config_dir: &str,
- logs_dir: &str,
+ config_dir: &Path,
+ logs_dir: &Path,
) -> Result<(), anyhow::Error> {
// creating logs_dir
- create_directory_with_user_group(&format!("{logs_dir}/tedge"), "tedge", "tedge", 0o755)?;
- create_directory_with_user_group(&format!("{logs_dir}/tedge/agent"), "tedge", "tedge", 0o755)?;
+ create_directory_with_user_group(
+ format!("{}/tedge", logs_dir.display()),
+ "tedge",
+ "tedge",
+ 0o755,
+ )?;
+ create_directory_with_user_group(
+ format!("{}/tedge/agent", logs_dir.display()),
+ "tedge",
+ "tedge",
+ 0o755,
+ )?;
// creating /operations/c8y directories
- create_directory_with_user_group(&format!("{config_dir}/operations"), "tedge", "tedge", 0o755)?;
create_directory_with_user_group(
- &format!("{config_dir}/operations/c8y"),
+ format!("{}/operations", config_dir.display()),
+ "tedge",
+ "tedge",
+ 0o755,
+ )?;
+ create_directory_with_user_group(
+ format!("{}/operations/c8y", config_dir.display()),
"tedge",
"tedge",
0o755,
)?;
// creating c8y_LogfileRequest operation file
create_file_with_user_group(
- &format!("{config_dir}/operations/c8y/c8y_LogfileRequest"),
+ format!("{}/operations/c8y/c8y_LogfileRequest", config_dir.display()),
"tedge",
"tedge",
0o644,
None,
)?;
// creating c8y directory
- create_directory_with_user_group(&format!("{config_dir}/c8y"), "root", "root", 0o1777)?;
+ create_directory_with_user_group(
+ format!("{}/c8y", config_dir.display()),
+ "root",
+ "root",
+ 0o1777,
+ )?;
// creating c8y-log-plugin.toml
- let logs_path = format!("{logs_dir}/tedge/agent/software-*");
+ let logs_path = format!("{}/tedge/agent/software-*", logs_dir.display());
let data = format!(
r#"files = [
{{ type = "software-management", path = "{logs_path}" }},
@@ -263,7 +281,7 @@ fn create_init_logs_directories_and_files(
);
create_file_with_user_group(
- &format!("{config_dir}/{DEFAULT_PLUGIN_CONFIG_FILE}"),
+ format!("{}/{DEFAULT_PLUGIN_CONFIG_FILE}", config_dir.display()),
"root",
"root",
0o644,