Skip to content
Closed
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
17 changes: 12 additions & 5 deletions lib/just_bash/commands/chmod.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ defmodule JustBash.Commands.Chmod do
{opts, positional} = parse_args(args)

case positional do
[_mode | paths] when paths != [] ->
check_paths(bash, paths, opts.recursive)
[mode | paths] when paths != [] ->
check_paths(bash, mode, paths, opts.recursive)

_ ->
{Command.error("chmod: missing operand\n"), bash}
Expand All @@ -41,14 +41,21 @@ defmodule JustBash.Commands.Chmod do

defp parse_args([arg | rest], opts, pos), do: parse_args(rest, opts, [arg | pos])

defp check_paths(bash, paths, _recursive) do
defp check_paths(bash, _mode, paths, _recursive) do
{stderr, exit_code, fs} =
Enum.reduce(paths, {"", 0, bash.fs}, fn path, {err, code, fs} ->
resolved = FS.resolve_path(bash.cwd, path)

case FS.stat(fs, resolved) do
{:ok, _, new_fs} ->
{err, code, new_fs}
{:ok, stat, new_fs} ->
case FS.chmod(new_fs, resolved, stat.mode || 0) do
{:ok, newer_fs} ->
{err, code, newer_fs}

{:error, error} ->
{err <> "chmod: changing permissions of '#{path}': #{FS.strerror(error)}\n", 1,
new_fs}
end

{:error, _} ->
{err <> "chmod: cannot access '#{path}': No such file or directory\n", 1, fs}
Expand Down
36 changes: 36 additions & 0 deletions lib/just_bash/fs/fs.ex
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,42 @@ defmodule JustBash.FS do
end
end

@doc """
Read every regular file under `root` into a `%{path => content}` map —
the filesystem as a git-style tree of absolute paths to blobs. The
inverse of `new/1` up to what a tree can express: `FS.new(tree)`
rebuilds an equivalent filesystem, but modes, mtimes, empty
directories, and symlink identity are not captured (symlinks are
materialized as their target's content; dangling symlinks and cyclic
directory symlinks follow `VFS.walk/3` semantics and are omitted).

Walks the whole mount table, so mounted backends contribute their
files. Returns `{:ok, tree, fs}` per vfs read conventions — thread the
returned `fs` forward when the mount table contains lazy backends.

## Examples

iex> fs = JustBash.FS.new(%{"/a.txt" => "alpha", "/notes/b.txt" => "beta"})
iex> {:ok, tree, _fs} = JustBash.FS.to_tree(fs)
iex> tree
%{"/a.txt" => "alpha", "/notes/b.txt" => "beta"}
"""
@spec to_tree(t(), String.t()) :: {:ok, %{String.t() => binary()}, t()} | {:error, Error.t()}
def to_tree(fs, root \\ "/") do
fs
|> walk(normalize_path(root))
|> Enum.reduce_while({:ok, %{}, fs}, fn
{path, %VFS.Stat{type: :regular}}, {:ok, tree, fs} ->
case read_file(fs, path) do
{:ok, content, fs} -> {:cont, {:ok, Map.put(tree, path, content), fs}}
{:error, %Error{} = err} -> {:halt, {:error, err}}
end

{_path, %VFS.Stat{}}, acc ->
{:cont, acc}
end)
end

# ── error formatting ─────────────────────────────────────────────────────

@doc """
Expand Down
Loading
Loading