mirror of
https://github.com/livebook-dev/livebook.git
synced 2025-10-09 21:16:26 +08:00
Make sure write-protected files can only be forked (#182)
* Format file errors into readable messages * Make sure write-protected files can only be forked * Show tooltip when the file is write protected
This commit is contained in:
parent
f31428739f
commit
9e026ee4b9
2 changed files with 19 additions and 6 deletions
|
@ -675,7 +675,8 @@ defmodule Livebook.Session do
|
||||||
handle_operation(state, {:mark_as_not_dirty, self()})
|
handle_operation(state, {:mark_as_not_dirty, self()})
|
||||||
else
|
else
|
||||||
{:error, reason} ->
|
{:error, reason} ->
|
||||||
broadcast_error(state.session_id, "failed to save notebook - #{reason}")
|
message = :file.format_error(reason)
|
||||||
|
broadcast_error(state.session_id, "failed to save notebook - #{message}")
|
||||||
state
|
state
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
|
|
|
@ -57,10 +57,15 @@ defmodule LivebookWeb.HomeLive do
|
||||||
<%= live_patch "Join session", to: Routes.session_path(@socket, :page, session_id_by_path(@path, @session_summaries)),
|
<%= live_patch "Join session", to: Routes.session_path(@socket, :page, session_id_by_path(@path, @session_summaries)),
|
||||||
class: "button button-blue" %>
|
class: "button button-blue" %>
|
||||||
<% else %>
|
<% else %>
|
||||||
<%= content_tag :button, "Open",
|
<%= tag :span, if(File.regular?(@path) and not file_writable?(@path),
|
||||||
class: "button button-blue",
|
do: [class: "tooltip top", aria_label: "This file is write-protected, please fork instead"],
|
||||||
phx_click: "open",
|
else: []
|
||||||
disabled: not path_openable?(@path, @session_summaries) %>
|
) %>
|
||||||
|
<%= content_tag :button, "Open",
|
||||||
|
class: "button button-blue",
|
||||||
|
phx_click: "open",
|
||||||
|
disabled: not path_openable?(@path, @session_summaries) %>
|
||||||
|
</span>
|
||||||
<% end %>
|
<% end %>
|
||||||
</div>
|
</div>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
@ -170,7 +175,7 @@ defmodule LivebookWeb.HomeLive do
|
||||||
end
|
end
|
||||||
|
|
||||||
defp path_openable?(path, session_summaries) do
|
defp path_openable?(path, session_summaries) do
|
||||||
File.regular?(path) and not path_running?(path, session_summaries)
|
File.regular?(path) and not path_running?(path, session_summaries) and file_writable?(path)
|
||||||
end
|
end
|
||||||
|
|
||||||
defp path_running?(path, session_summaries) do
|
defp path_running?(path, session_summaries) do
|
||||||
|
@ -178,6 +183,13 @@ defmodule LivebookWeb.HomeLive do
|
||||||
path in running_paths
|
path in running_paths
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp file_writable?(path) do
|
||||||
|
case File.stat(path) do
|
||||||
|
{:ok, stat} -> stat.access in [:read_write, :write]
|
||||||
|
{:error, _} -> false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
defp create_session(socket, opts \\ []) do
|
defp create_session(socket, opts \\ []) do
|
||||||
case SessionSupervisor.create_session(opts) do
|
case SessionSupervisor.create_session(opts) do
|
||||||
{:ok, id} ->
|
{:ok, id} ->
|
||||||
|
|
Loading…
Add table
Reference in a new issue