From 64da82c661b2a28c7a258bd30c0084633d0e1896 Mon Sep 17 00:00:00 2001 From: Justin Wernick Date: Fri, 11 Aug 2023 22:04:19 +0200 Subject: Create group directories if they don't already exist - With the correct ownership - With the correct permissions Fix #5 --- src/git.rs | 53 +++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 39 insertions(+), 14 deletions(-) (limited to 'src/git.rs') diff --git a/src/git.rs b/src/git.rs index 9fa3e78..ea55f2c 100644 --- a/src/git.rs +++ b/src/git.rs @@ -1,11 +1,12 @@ use crate::{ parser::{GitReceivePackArgs, GitUploadPackArgs}, - user_info::{get_user_groups, get_username}, + user_info::{get_gid, get_user_groups, get_username}, ShackleError, }; use git2::{ErrorCode, Repository, RepositoryInitMode, RepositoryInitOptions}; use std::{ fs, + os::unix::fs::PermissionsExt, path::{Path, PathBuf}, process::Command, }; @@ -75,24 +76,48 @@ pub fn init( description: &Option, branch: &str, ) -> Result { + if let Some(group) = &group { + if !verify_user_is_in_group(group) { + return Err(ShackleError::InvalidGroup); + } + } + + let git_prefix = git_dir_prefix(); + let collection_dir = match group { + Some(group) => group_git_dir(group), + None => personal_git_dir()?, + }; + let path = collection_dir.join(repo_name).with_extension("git"); + + if !git_prefix.is_dir() { + fs::create_dir(&git_prefix)?; + } + + if !collection_dir.is_dir() { + fs::create_dir(&collection_dir)?; + + if let Some(group) = group { + let gid = get_gid(&group).expect("User is in group but no group ID?"); + nix::unistd::chown(&collection_dir, None, Some(gid))?; + } + + let mut perms = collection_dir.metadata()?.permissions(); + perms.set_mode(match group { + Some(_) => 0o2770, + None => 0o700, + }); + fs::set_permissions(&collection_dir, perms)?; + } + let mut init_opts = RepositoryInitOptions::new(); init_opts .bare(true) - .mkdir(true) + .mkdir(false) .no_reinit(true) .initial_head(branch); - - let path = match group { - Some(group) => { - if !verify_user_is_in_group(group) { - return Err(ShackleError::InvalidGroup); - } - - init_opts.mode(RepositoryInitMode::SHARED_GROUP); - group_git_dir(group).join(repo_name).with_extension("git") - } - None => personal_git_dir()?.join(repo_name).with_extension("git"), - }; + if group.is_some() { + init_opts.mode(RepositoryInitMode::SHARED_GROUP); + } Repository::init_opts(&path, &init_opts)?; if let Some(description) = description { -- cgit v1.2.3