Merge pull request #2099 from gitbutlerapp/set-user-permissions-on-unix

Set user permissions on unix
This commit is contained in:
Qix 2023-12-19 11:16:34 +01:00 committed by GitHub
commit 6deff6f4c2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 25 additions and 7 deletions

View File

@ -6,7 +6,7 @@ use std::{
}; };
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
use crate::windows::*; use crate::windows::MetadataShim;
#[cfg(target_family = "unix")] #[cfg(target_family = "unix")]
use std::os::unix::prelude::*; use std::os::unix::prelude::*;

View File

@ -58,6 +58,7 @@ pub enum GetOrCreateError {
Other(#[from] anyhow::Error), Other(#[from] anyhow::Error),
} }
#[cfg(not(target_os = "windows"))]
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::fs; use std::fs;

View File

@ -58,11 +58,15 @@ impl Storage {
} }
fs::write(&file_path, content).map_err(Error::IO)?; fs::write(&file_path, content).map_err(Error::IO)?;
// Set the permissions to be user-only. // Set the permissions to be user-only. We can't actually
// do this on Windows, so we ignore that platform.
#[cfg(target_family = "unix")]
{
let metadata = fs::metadata(file_path.clone())?; let metadata = fs::metadata(file_path.clone())?;
let mut permissions = metadata.permissions(); let mut permissions = metadata.permissions();
permissions.set_mode(0o600); // User read/write permissions.set_mode(0o600); // User read/write
fs::set_permissions(file_path.clone(), permissions)?; fs::set_permissions(file_path.clone(), permissions)?;
}
Ok(()) Ok(())
} }

View File

@ -2312,6 +2312,7 @@ fn test_commit_add_and_delete_files() -> Result<()> {
} }
#[test] #[test]
#[cfg(target_family = "unix")]
fn test_commit_executable_and_symlinks() -> Result<()> { fn test_commit_executable_and_symlinks() -> Result<()> {
let Case { let Case {
project_repository, project_repository,

View File

@ -1905,9 +1905,20 @@ pub fn write_tree_onto_commit(
let mut filemode = git::FileMode::Blob; let mut filemode = git::FileMode::Blob;
// check if full_path file is executable // check if full_path file is executable
if let Ok(metadata) = std::fs::symlink_metadata(&full_path) { if let Ok(metadata) = std::fs::symlink_metadata(&full_path) {
#[cfg(target_family = "unix")]
{
if metadata.permissions().mode() & 0o111 != 0 { if metadata.permissions().mode() & 0o111 != 0 {
filemode = git::FileMode::BlobExecutable; filemode = git::FileMode::BlobExecutable;
} }
}
#[cfg(target_os = "windows")]
{
// TODO(qix-): Pull from `core.filemode` config option to determine
// TODO(qix-): the behavior on windows. For now, we set this to true.
// TODO(qix-): It's not ideal, but it gets us to a windows build faster.
filemode = git::FileMode::BlobExecutable;
}
if metadata.file_type().is_symlink() { if metadata.file_type().is_symlink() {
filemode = git::FileMode::Link; filemode = git::FileMode::Link;
} }

View File

@ -11,6 +11,7 @@ impl MetadataShim for std::fs::Metadata {
fn ino(&self) -> u64 { fn ino(&self) -> u64 {
self.file_index().expect("file metadata constructed based on directory listing instead of a file (see https://doc.rust-lang.org/std/os/windows/fs/trait.MetadataExt.html#tymethod.file_index)") self.file_index().expect("file metadata constructed based on directory listing instead of a file (see https://doc.rust-lang.org/std/os/windows/fs/trait.MetadataExt.html#tymethod.file_index)")
} }
#[allow(clippy::cast_lossless)]
fn dev(&self) -> u64 { fn dev(&self) -> u64 {
self.volume_serial_number().expect("file metadata constructed based on directory listing instead of a file (see https://doc.rust-lang.org/std/os/windows/fs/trait.MetadataExt.html#tymethod.volume_serial_number)") as u64 self.volume_serial_number().expect("file metadata constructed based on directory listing instead of a file (see https://doc.rust-lang.org/std/os/windows/fs/trait.MetadataExt.html#tymethod.volume_serial_number)") as u64
} }