Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
huguesBouvier committed Oct 14, 2021
1 parent c5d6176 commit bdd0e7b
Showing 1 changed file with 12 additions and 44 deletions.
56 changes: 12 additions & 44 deletions edgelet/edgelet-http/src/unix.rs
Original file line number Diff line number Diff line change
@@ -1,45 +1,38 @@
// Copyright (c) Microsoft. All rights reserved.

use std::fs;
#[cfg(unix)]
use std::os::unix::fs::MetadataExt;
use std::os::unix::prelude::PermissionsExt;
use std::path::Path;

use failure::ResultExt;
use log::{debug, error};
#[cfg(unix)]
use nix::sys::stat::{umask, Mode};
#[cfg(unix)]
use scopeguard::defer;
#[cfg(unix)]
use tokio_uds::UnixListener;

use crate::error::{Error, ErrorKind};
use crate::util::{incoming::Incoming, socket_file_exists};

pub fn listener<P: AsRef<Path>>(path: P, unix_socket_permission: u32) -> Result<Incoming, Error> {
let listener = if socket_file_exists(path.as_ref()) {
// get the previous file's metadata
#[cfg(unix)]
let metadata = get_metadata(path.as_ref())?;

debug!("unlinking {}...", path.as_ref().display());
fs::remove_file(&path)
.with_context(|_| ErrorKind::Path(path.as_ref().display().to_string()))?;
debug!("unlinked {}", path.as_ref().display());

#[cfg(unix)]
let prev = set_umask(&metadata, path.as_ref());
#[cfg(unix)]
defer! {{ umask(prev); }}

debug!("binding {}...", path.as_ref().display());

let listener = UnixListener::bind(&path)
.with_context(|_| ErrorKind::Path(path.as_ref().display().to_string()))?;
debug!("bound {}", path.as_ref().display());

fs::set_permissions(
path.as_ref(),
fs::Permissions::from_mode(unix_socket_permission),
)
.map_err(|err| {
error!("Cannot set directory permissions: {}", err);
ErrorKind::Path(path.as_ref().display().to_string())
})?;

Incoming::Unix(listener)
} else {
// If parent doesn't exist, create it and socket will be created inside.
Expand Down Expand Up @@ -70,36 +63,11 @@ pub fn listener<P: AsRef<Path>>(path: P, unix_socket_permission: u32) -> Result<
Ok(listener)
}

#[cfg(unix)]
fn get_metadata(path: &Path) -> Result<fs::Metadata, Error> {
let metadata =
fs::metadata(path).with_context(|_| ErrorKind::Path(path.display().to_string()))?;
debug!("read metadata {:?} for {}", metadata, path.display());
Ok(metadata)
}

#[cfg(unix)]
fn set_umask(metadata: &fs::Metadata, path: &Path) -> Mode {
#[cfg(target_os = "macos")]
#[allow(clippy::cast_possible_truncation)]
let mode = Mode::from_bits_truncate(metadata.mode() as u16);

#[cfg(not(target_os = "macos"))]
let mode = Mode::from_bits_truncate(metadata.mode());

let mut mask = Mode::all();
mask.toggle(mode);

debug!("settings permissions {:#o} for {}...", mode, path.display());

umask(mask)
}

#[cfg(test)]
#[cfg(unix)]
mod tests {
use super::{listener, MetadataExt};

use super::{listener};
use std::os::unix::fs::MetadataExt;
use std::fs::OpenOptions;
use std::os::unix::fs::OpenOptionsExt;

Expand All @@ -126,7 +94,7 @@ mod tests {
let _srv = listener.for_each(move |(_socket, _addr)| Ok(()));

let file_stat = stat(&path).unwrap();
assert_eq!(0o600, file_stat.st_mode & 0o777);
assert_eq!(0o666, file_stat.st_mode & 0o777);

dir.close().unwrap();
}
Expand Down

0 comments on commit bdd0e7b

Please sign in to comment.