Skip to content
Closed
Changes from all 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
16 changes: 15 additions & 1 deletion cortex-cli/src/debug_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,10 @@ async fn run_file(args: FileArgs) -> Result<()> {
None
};

// Check if the current user can actually write to the file
// This is more accurate than just checking permission bits
let readonly = !is_writable_by_current_user(&path);

(
Some(FileMetadata {
size: meta.len(),
Expand All @@ -349,7 +353,7 @@ async fn run_file(args: FileArgs) -> Result<()> {
symlink_target,
modified,
created,
readonly: meta.permissions().readonly(),
readonly,
}),
None,
)
Expand Down Expand Up @@ -506,6 +510,16 @@ fn guess_mime_type(ext: &str) -> String {
.to_string()
}

/// Check if the current user can write to the file.
///
/// This function attempts to open the file for writing to determine
/// if the current user has write access. This is more accurate than
/// checking permission bits, as it accounts for ownership, group
/// membership, and ACLs.
fn is_writable_by_current_user(path: &std::path::Path) -> bool {
std::fs::OpenOptions::new().write(true).open(path).is_ok()
}

/// Detect encoding and binary status.
fn detect_encoding_and_binary(path: &PathBuf) -> (Option<String>, Option<bool>) {
// Read first 8KB to check for binary content
Expand Down
Loading