Skip to content

Commit

Permalink
Merge pull request #2876 from jarhodes314/2873-tedge-init-only-create…
Browse files Browse the repository at this point in the history
…-multi-call-binary-symlinks-if-they-do-not-exist

Avoid modifying files if they are up to date in `tedge init`
  • Loading branch information
jarhodes314 authored May 15, 2024
2 parents 7cf3830 + c89b62a commit c4718f5
Show file tree
Hide file tree
Showing 4 changed files with 266 additions and 134 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 21 additions & 11 deletions crates/common/tedge_utils/src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ pub enum FileError {
#[error("Failed to change owner: {name:?}.")]
MetaDataError { name: String, from: std::io::Error },

#[error("Failed to change permissions of file: {name:?}.")]
ChangeModeError { name: String, from: std::io::Error },

#[error("User not found: {user:?}.")]
UserNotFound { user: String },

Expand Down Expand Up @@ -65,13 +68,13 @@ pub enum FileError {

pub fn create_directory<P: AsRef<Path>>(
dir: P,
permissions: PermissionEntry,
permissions: &PermissionEntry,
) -> Result<(), FileError> {
permissions.create_directory(dir.as_ref())
}
/// Create the directory owned by the user running this API with default directory permissions
pub fn create_directory_with_defaults<P: AsRef<Path>>(dir: P) -> Result<(), FileError> {
create_directory(dir, PermissionEntry::default())
create_directory(dir, &PermissionEntry::default())
}

pub fn create_directory_with_user_group(
Expand Down Expand Up @@ -380,7 +383,7 @@ pub fn change_user_and_group(file: &Path, user: &str, group: &str) -> Result<(),
let gid = get_metadata(Path::new(file))?.gid();

// if user and group are same as existing, then do not change
if (ud != uid) && (gd != gid) {
if (ud != uid) || (gd != gid) {
chown(file, Some(Uid::from_raw(ud)), Some(Gid::from_raw(gd))).map_err(|e| {
FileError::MetaDataError {
name: file.display().to_string(),
Expand Down Expand Up @@ -431,15 +434,22 @@ fn change_group(file: &Path, group: &str) -> Result<(), FileError> {
}

fn change_mode(file: &Path, mode: u32) -> Result<(), FileError> {
let mut perm = get_metadata(Path::new(file))?.permissions();
perm.set_mode(mode);

fs::set_permissions(file, perm).map_err(|e| FileError::MetaDataError {
name: file.display().to_string(),
from: e,
})?;
let mut permissions = get_metadata(Path::new(file))?.permissions();

Ok(())
if permissions.mode() & 0o777 != mode {
permissions.set_mode(mode);
debug!("Setting mode of {} to {mode:0o}", file.display());
fs::set_permissions(file, permissions).map_err(|e| FileError::ChangeModeError {
name: file.display().to_string(),
from: e,
})
} else {
debug!(
"Not changing mode of {} as it is already {mode:0o}",
file.display()
);
Ok(())
}
}

/// Return metadata when the given path exists and accessible by user
Expand Down
1 change: 1 addition & 0 deletions crates/core/tedge/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ yansi = { workspace = true }
[dev-dependencies]
assert_cmd = { workspace = true }
assert_matches = { workspace = true }
mockall = { workspace = true }
mockito = { workspace = true }
mqtt_tests = { workspace = true }
pem = { workspace = true }
Expand Down
Loading

0 comments on commit c4718f5

Please sign in to comment.