Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions crates/flare-git-core/src/worktree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,34 @@ fn remove_worktree_dir(path: &Path, name: &str) -> bool {
return true;
}

// `rmdir`/`remove_dir_all` both fail identically against a genuine
// ACL denial, not just a transient in-use lock -- item #267's actual
// failure mode: cargo's own `target/*/.fingerprint/*` files can end
// up ACL-restricted (not merely open), which no amount of retrying
// or an in-use-lock-only tool like `rmdir` can clear. `icacls /grant
// ... /T` resets ownership access recursively before one final
// delete attempt; a no-op (and thus never destructive) if the real
// problem was actually an in-use lock the retries above already
// would have cleared.
if let Ok(user) = std::env::var("USERNAME") {
let icacls_args: Vec<String> = vec![
path.to_string_lossy().to_string(),
"/grant".to_string(),
format!("{user}:F"),
"/T".to_string(),
"/C".to_string(),
"/Q".to_string(),
];
let _ = std::process::Command::new("icacls")
.args(&icacls_args)
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null())
.status();
if std::fs::remove_dir_all(path).is_ok() {
Comment on lines +665 to +679

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

printf '\n== worktree.rs around cited lines ==\n'
sed -n '600,660p' crates/flare-git-core/src/worktree.rs
printf '\n== worktree.rs test region ==\n'
sed -n '690,740p' crates/flare-git-core/src/worktree.rs
printf '\n== search for remove_worktree_dir and ACL helpers ==\n'
rg -n "remove_worktree_dir|icacls|USERNAME|deny|grant|delete" crates/flare-git-core/src/worktree.rs

Repository: getappz/agentflare

Length of output: 6659


🌐 Web query:

icacls /grant explicit deny ace does not remove deny delete child permission parent delete-child documentation

💡 Result:

The behavior you are observing is a documented limitation of the icacls command-line tool, compounded by how NTFS handles specific permission masks and ACE removal. 1. Lack of granular removal: There is no command in icacls to remove a single, specific ACE (such as Deny Delete Child) [1][2]. The /remove command only operates at the SID (User/Group) level [1][3]. Using /remove:d [User] will remove all Deny ACEs for that user, not just the one you intended to target [1][2]. 2. Precedence and Side Effects: When you use /deny, icacls may include additional implicit permissions like Synchronize (S) in the ACE mask [4][5]. Consequently, simply granting a permission does not automatically "remove" the corresponding Deny ACE; the Deny ACE remains in the Access Control List (ACL) [1]. Because Windows processes the DACL in a specific order—typically evaluating explicit Deny ACEs before explicit Grant ACEs—the Deny ACE will continue to take precedence and block the action, even if you subsequently attempt to grant that same permission [6][7]. 3. Recommended Workarounds: - To effectively manage specific permissions without unintended side effects, avoid using /deny if possible. Instead, structure your permissions using Allow ACEs only [6][7]. - If you must remove a specific Deny ACE created by icacls, you may need to use third-party tools (such as SetACL) that provide granular control over ACE manipulation [5]. - If you must continue using native tools, you may have to remove all permissions for the user using /remove:d and then re-apply the desired Grant ACEs to reconstruct the ACL correctly [1][2]. For further technical context, Microsoft has documented that the icacls program manages permission masks (like Delete vs. Delete Child) in ways that can differ from the GUI, sometimes including Synchronize (S) automatically, which can lead to unexpected access blocks (e.g., inability to traverse a folder) [4][5].

Citations:


🌐 Web query:

Microsoft NTFS delete child explicit deny delete permission child file parent directory access check

💡 Result:

In the Microsoft NTFS file system, the ability to delete a file or directory is governed by a dual-path authorization mechanism [1][2][3]. A user can successfully delete a child object if they satisfy either of the following conditions: 1. They have the DELETE permission explicitly granted on the child object itself [1][2][3]. 2. They have the DELETE_CHILD (also known as "Delete subfolders and files") permission granted on the parent directory [4][5][3]. If either of these conditions is met, the file system will typically allow the delete operation [1][2][3]. Consequently, if a user has DELETE_CHILD access on a parent directory, this permission effectively overrides a specific "Deny Delete" ACE (Access Control Entry) applied to the child file itself [6][2]. Because the system verifies security descriptors for both the parent and the object to determine if the deletion is authorized, an explicit "Deny" on the object does not necessarily block the operation if the parent's "Allow" (DELETE_CHILD) remains in effect [6]. To effectively prevent a user from deleting a file, you must ensure that both paths are restricted [1][2]. This requires: - Denying the DELETE permission on the child file itself [1][2]. - Denying the DELETE_CHILD permission on the parent directory [1][7]. If only one of these paths is restricted, the other will continue to provide the necessary authorization for the deletion to occur [1][2]. Documentation and technical analysis confirm that this behavior is by design, as the "Delete" operation is fundamentally treated as an action performed on the parent directory container, not solely on the child object itself [8][7].

Citations:


🌐 Web query:

site:learn.microsoft.com delete child permission delete file even if file has deny delete Windows ACL

💡 Result:

In Windows, the ability to delete a file is governed by two distinct sets of permissions: those assigned to the file itself (Delete permission) and those assigned to the parent directory (Delete Child permission) [1][2][3]. Because these are independent mechanisms, a user can delete a file if they possess either: 1. Delete permission on the file itself [1][3] 2. Delete Child permission on the parent directory [1][4][3] If a user is explicitly denied "Delete" access on a file, they can still successfully delete that file if they have "Delete Child" permission on the parent directory [4][3]. This occurs because the system evaluates these permissions independently; the "Delete Child" right on the directory grants the user authority over the directory's contents, which bypasses the restrictive ACL placed directly on the file [4][5][3]. To effectively prevent a user from deleting a file, you must ensure that they are denied the necessary permissions at both levels [4][3]: - File level: Explicitly deny "Delete" access [3]. - Directory level: Explicitly deny "Delete Child" (or "Delete subfolders and files") access on the parent directory [4][6][3]. Because "Delete Child" is a powerful privilege that can override file-level protections, it is standard security practice to carefully restrict who holds this permission on directories [4]. Furthermore, because the Windows security system evaluates access based on the first applicable "Deny" or "Allow" rule encountered (based on the Discretionary Access Control List (DACL) order), it is critical that "Deny" entries are properly configured and prioritized [7][8][9]. Note also that users with administrative privileges or those who hold the "Take Ownership" right can modify these permissions to regain access, regardless of existing Deny rules [3].

Citations:


icacls /grant doesn’t clear a deny ACE.
The retry can still fail on a real ACL denial because /grant leaves the explicit deny in place; clear the deny ACE first or rebuild the ACL before retrying.

The test also only denies Delete on locked.txt, which can still allow tree deletion via the parent directory’s delete-child right, so it doesn’t reliably exercise the fallback.

📍 Affects 1 file
  • crates/flare-git-core/src/worktree.rs#L622-L636 (this comment)
  • crates/flare-git-core/src/worktree.rs#L709-L724
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@crates/flare-git-core/src/worktree.rs` around lines 622 - 636, Update the
Windows ACL fallback in the retry block around
std::process::Command::new("icacls") to clear or rebuild deny ACEs before
granting the current user full control, then retry remove_dir_all. In the
related test at crates/flare-git-core/src/worktree.rs:709-724, deny deletion of
the directory tree or the parent’s delete-child permission rather than only
locked.txt’s Delete permission, so the fallback is reliably exercised.

return true;
}
}

for handle_exe in &["handle64.exe", "handle.exe"] {
if let Ok(output) = std::process::Command::new(handle_exe)
.args(["-accepteula", "-nobanner", &path.to_string_lossy()])
Expand Down Expand Up @@ -665,6 +693,38 @@ mod tests {
assert!(!dir.exists());
}

#[cfg(windows)]
#[test]
fn remove_worktree_dir_clears_a_genuine_acl_denial() {
// item #267's actual failure mode: cargo's own target/*/.fingerprint/*
// files can end up ACL-restricted, not just transiently open. Neither
// the retry loop nor `cmd /c rmdir` clears a real ACL deny -- only
// `icacls /grant ... /T` does.
let tmp = TempDir::new().unwrap();
let dir = tmp.path().join("wt");
std::fs::create_dir_all(&dir).unwrap();
let locked_file = dir.join("locked.txt");
std::fs::write(&locked_file, b"hello").unwrap();

let user = std::env::var("USERNAME").unwrap();
let deny_args: Vec<String> = vec![
locked_file.to_string_lossy().to_string(),
"/deny".to_string(),
format!("{user}:(D)"),
];
let status = std::process::Command::new("icacls")
.args(&deny_args)
.status()
.unwrap();
assert!(status.success(), "test setup: icacls /deny must succeed");

assert!(
remove_worktree_dir(&dir, "test"),
"must clear the ACL denial via icacls /grant, not just retry"
);
assert!(!dir.exists());
}

#[test]
fn remove_worktree_dir_retries_past_a_transient_lock() {
// On Windows, an open file handle blocks remove_dir_all with
Expand Down
Loading