aboutsummaryrefslogtreecommitdiff
path: root/shard/lib
diff options
context:
space:
mode:
Diffstat (limited to 'shard/lib')
-rw-r--r--shard/lib/app/directory.ex45
1 files changed, 42 insertions, 3 deletions
diff --git a/shard/lib/app/directory.ex b/shard/lib/app/directory.ex
index f7d871a..55413c4 100644
--- a/shard/lib/app/directory.ex
+++ b/shard/lib/app/directory.ex
@@ -42,10 +42,12 @@ defmodule SApp.Directory do
end
SNet.Group.init_lookup(netgroup, self())
+ revfiles = for {n, m} <- SData.SignRev.get(files), into: %{}, do: {m, n}
+
{:ok, %{
owner: owner, public: public, name: name,
manifest: manifest, id: id, netgroup: netgroup,
- files: files}}
+ files: files, revfiles: revfiles}}
end
def handle_call(:manifest, _from, state) do
@@ -66,7 +68,10 @@ defmodule SApp.Directory do
GenServer.cast(Shard.Manager, {:dep_list, state.id, Map.values(dict)})
{:ok, st2} = SData.SignRev.set(state.files, dict, state.owner)
Shard.Manager.save_state(state.id, st2)
- state = put_in(state.files, st2)
+ state = %{state |
+ files: st2,
+ revfiles: Map.put(state.revfiles, manifest, name)
+ }
bcast_state(state)
{:reply, :ok, state}
end
@@ -81,6 +86,7 @@ defmodule SApp.Directory do
if dict[name] == nil do
{:reply, :not_found, state}
else
+ state = put_in(state.revfiles, Map.delete(state.revfiles, dict[name]))
dict = Map.delete(dict, name)
GenServer.cast(Shard.Manager, {:dep_list, state.id, Map.values(dict)})
{:ok, st2} = SData.SignRev.set(state.files, dict, state.owner)
@@ -94,6 +100,15 @@ defmodule SApp.Directory do
end
end
+ def handle_call({:read, name}, _from, state) do
+ dict = SData.SignRev.get(state.files)
+ {:reply, dict[name], state}
+ end
+
+ def handle_call({:find, manifest}, _from, state) do
+ {:reply, state.revfiles[manifest], state}
+ end
+
def handle_cast(:send_deps, state) do
dict = SData.SignRev.get(state.files)
GenServer.cast(Shard.Manager, {:dep_list, state.id, Map.values(dict)})
@@ -144,13 +159,29 @@ defmodule SApp.Directory do
@doc"""
Return list of files stored in this directory.
- Returns a list of {name, manifests}.
+ Returns a dictionnary of %{name => manifest}.
"""
def get_files(pid) do
GenServer.call(pid, :get_files)
end
@doc"""
+ Return the manifest of file with a given name in directory, or nil if not found.
+
+ Equivalent to get_files(pid)[name] but better.
+ """
+ def read(pid, name) do
+ GenServer.call(pid, {:read, name})
+ end
+
+ @doc"""
+ Find a file in the directory by its manifest. Returns name if found or nil if not found.
+ """
+ def find(pid, manifest) do
+ GenServer.call(pid, {:find, manifest})
+ end
+
+ @doc"""
Add a file to this directory. A file is a name for a shard manifest.
A file added to a directory becomes a dependency of the directory, i.e.
if the directory is pinned then all files inside are pinned as well.
@@ -165,4 +196,12 @@ defmodule SApp.Directory do
def rm_file(pid, name) do
GenServer.call(pid, {:rm_file, name})
end
+
+ @doc"""
+ Returns the friends directory of a user
+ """
+ def friends_dir(pk) do
+ manifest = %Manifest{name: "friends", owner: pk, public: false}
+ Shard.Manager.find_or_start manifest
+ end
end