Skip to content
Merged
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions crates/fs/src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ pub struct Metadata {
pub len: u64,
pub is_fifo: bool,
pub is_executable: bool,
pub is_writable: bool,
}

/// Filesystem modification time. The purpose of this newtype is to discourage use of operations
Expand Down Expand Up @@ -1025,6 +1026,7 @@ impl Fs for RealFs {
is_dir: metadata.file_type().is_dir(),
is_fifo,
is_executable,
is_writable: !metadata.permissions().readonly(),
}))
}

Expand Down Expand Up @@ -3085,6 +3087,7 @@ impl Fs for FakeFs {
is_symlink,
is_fifo: false,
is_executable: false,
is_writable: true,
},
FakeFsEntry::Dir {
inode, mtime, len, ..
Expand All @@ -3096,6 +3099,7 @@ impl Fs for FakeFs {
is_symlink,
is_fifo: false,
is_executable: false,
is_writable: true,
},
FakeFsEntry::Symlink { .. } => unreachable!(),
}))
Expand Down
9 changes: 7 additions & 2 deletions crates/project/src/buffer_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,12 @@ impl LocalBufferStore {
let path = path.clone();
let buffer = match load_file.await {
Ok(loaded) => {
let is_writable = loaded.is_writable;
let capability = if is_writable {
Capability::ReadWrite
} else {
Capability::Read
};
let reservation = cx.reserve_entity::<Buffer>();
let buffer_id = BufferId::from(reservation.entity_id().as_non_zero_u64());
let text_buffer = cx
Expand All @@ -655,8 +661,7 @@ impl LocalBufferStore {
})
.await;
cx.insert_entity(reservation, |_| {
let mut buffer =
Buffer::build(text_buffer, Some(loaded.file), Capability::ReadWrite);
let mut buffer = Buffer::build(text_buffer, Some(loaded.file), capability);
buffer.set_encoding(loaded.encoding);
buffer.set_has_bom(loaded.has_bom);
buffer
Expand Down
40 changes: 39 additions & 1 deletion crates/project/tests/integration/project_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use buffer_diff::{
};
use collections::{BTreeSet, HashMap, HashSet};
use encoding_rs;
use fs::{FakeFs, PathEventKind};
use fs::{FakeFs, PathEventKind, RealFs};
use futures::{StreamExt, future};
use git::{
GitHostingProviderRegistry,
Expand Down Expand Up @@ -15467,6 +15467,44 @@ async fn test_read_only_files_empty_setting(cx: &mut gpui::TestAppContext) {
});
}

#[gpui::test]
#[cfg(not(windows))]
async fn test_os_read_only_files_open_as_read_only(cx: &mut gpui::TestAppContext) {
init_test(cx);
cx.executor().allow_parking();

let root = TempTree::new(json!({
"project": {
"test.txt": "hello",
},
}));
let file_path = root.path().join("project/test.txt");
let mut permissions = std::fs::metadata(&file_path).unwrap().permissions();
permissions.set_readonly(true);
std::fs::set_permissions(&file_path, permissions).unwrap();

let project = Project::test(
Arc::new(RealFs::new(None, cx.executor())),
[root.path()],
cx,
)
.await;

let buffer = project
.update(cx, |project, cx| {
project.open_local_buffer(file_path.as_path(), cx)
})
.await
.unwrap();

buffer.read_with(cx, |buffer, _| {
assert!(
buffer.read_only(),
"OS read-only files should open as read-only"
);
});
}

#[gpui::test]
async fn test_read_only_files_with_lock_files(cx: &mut gpui::TestAppContext) {
init_test(cx);
Expand Down
14 changes: 8 additions & 6 deletions crates/worktree/src/worktree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ pub struct LoadedFile {
pub text: String,
pub encoding: &'static Encoding,
pub has_bom: bool,
pub is_writable: bool,
}

pub struct LoadedBinaryFile {
Expand Down Expand Up @@ -1594,15 +1595,15 @@ impl LocalWorktree {
// if it is too large
// 5GB seems to be more reasonable, peaking at ~16GB, while 6GB jumps up to >24GB which seems like a
// reasonable limit
const FILE_SIZE_MAX: u64 = 6 * 1024 * 1024 * 1024; // 6GB
let metadata = fs.metadata(&abs_path).await?;
if let Some(metadata) = metadata.as_ref()
&& metadata.len >= FILE_SIZE_MAX
{
const FILE_SIZE_MAX: u64 = 6 * 1024 * 1024 * 1024; // 6GB
if let Ok(Some(metadata)) = fs.metadata(&abs_path).await
&& metadata.len >= FILE_SIZE_MAX
{
anyhow::bail!("File is too large to load");
}
anyhow::bail!("File is too large to load");
}
let (text, encoding, has_bom) = decode_file_text(fs.as_ref(), &abs_path).await?;
let is_writable = metadata.is_some_and(|metadata| metadata.is_writable);

let worktree = this.upgrade().context("worktree was dropped")?;
let file = match entry.await? {
Expand Down Expand Up @@ -1636,6 +1637,7 @@ impl LocalWorktree {
text,
encoding,
has_bom,
is_writable,
})
})
}
Expand Down
Loading