this repo has no description
2
fork

Configure Feed

Select the types of activity you want to include in your feed.

Add ls/exists? and fix pread bug

garrison 698c2644 2fb9299f

+83 -1
+78 -1
lib/trinity/sim_file.ex
··· 2 2 alias Trinity.Scheduler.Simulation 3 3 import Trinity.Scheduler, only: [simulation_key: 0, sim_node_key: 0] 4 4 5 + import ExUnit.Assertions, only: [assert: 1] 6 + 5 7 @type file_descriptor :: term 6 8 @type posix :: :file.posix 7 9 ··· 43 45 end 44 46 end 45 47 48 + @spec exists?(String.t) :: boolean 49 + def exists?(path) do 50 + case get_sim() do 51 + nil -> raise "not supported" 52 + _sim -> sim_exists?(path) 53 + end 54 + end 55 + 56 + @spec ls(String.t) :: {:ok, [String.t]} | {:error, posix} 57 + def ls(path) do 58 + case get_sim() do 59 + nil -> raise "not supported" 60 + _sim -> sim_ls(path) 61 + end 62 + end 63 + 46 64 defp next_fd(file_paths), do: :ets.update_counter(file_paths, :next_fd, 1) 47 65 48 66 defp sim_open(path, _modes) do ··· 92 110 93 111 start_block_index = div(loc, @block_size) 94 112 start_pos = rem(loc, @block_size) 113 + start_bin_size = min(bytes, @block_size - start_pos) 95 114 96 115 << 97 116 _prefix::binary-size(start_pos), 98 - bin_acc::binary, 117 + bin_acc::binary-size(start_bin_size), 118 + _::binary, 99 119 >> = read_block(file_data, fd, start_block_index) 100 120 101 121 bytes_remaining = bytes - byte_size(bin_acc) ··· 108 128 end 109 129 110 130 defp do_read_blocks(file_data, fd, block_index, bin_acc, bytes_remaining) when bytes_remaining < @block_size do 131 + assert bytes_remaining > 0 111 132 << 112 133 bin::binary-size(bytes_remaining), 113 134 _rest::binary, ··· 223 244 end 224 245 end 225 246 247 + defp sim_exists?(path) do 248 + %{file_paths: file_paths} = get_sim() 249 + node = get_proc_node() 250 + # TODO: is it actually correct to normalize the path for a file? 251 + # Or should this only happen for a directory? 252 + path = normalize_trailing(path) 253 + 254 + case get_path(file_paths, node, path) do 255 + nil -> false 256 + _ -> true 257 + end 258 + end 259 + 260 + defp sim_ls(path) do 261 + %{file_paths: file_paths} = get_sim() 262 + node = get_proc_node() 263 + path = normalize_trailing(path) 264 + 265 + case get_path(file_paths, node, path) do 266 + :directory -> 267 + prefix = path <> "/" 268 + 269 + children = 270 + scan_prefix(file_paths, node, path) 271 + |> Enum.reduce([], fn ^prefix <> name, acc -> 272 + # If the suffix of the path contains a "/", it's a 273 + # subdirectory and not a direct child of `path` 274 + case String.contains?(name, "/") do 275 + true -> acc 276 + false -> [name | acc] 277 + end 278 + end) 279 + 280 + {:ok, children} 281 + 282 + nil -> {:error, :enoent} 283 + fd when is_integer(fd) -> {:error, :enotdir} 284 + end 285 + end 286 + 226 287 defp normalize_trailing(path), do: String.trim_trailing(path, "/") 227 288 defp split_path("/" <> path), do: String.split(path, "/") 289 + 290 + # Note: returned path list is reversed 291 + defp scan_prefix(file_paths, node, path) do 292 + prev_key = {node, path <> "/"} 293 + end_path = path <> "0" 294 + do_scan_paths(file_paths, node, end_path, prev_key, []) 295 + end 296 + 297 + defp do_scan_paths(file_paths, node, end_path, prev_key, acc) do 298 + case :ets.next(file_paths, prev_key) do 299 + {^node, p} = key when p < end_path -> 300 + do_scan_paths(file_paths, node, end_path, key, [p | acc]) 301 + _ -> 302 + acc 303 + end 304 + end 228 305 229 306 defp get_path(file_paths, node, path) do 230 307 case :ets.lookup(file_paths, {node, path}) do
+5
test/trinity_test.exs
··· 18 18 end 19 19 20 20 def init(%{id: id, initial_count: initial_count}) do 21 + assert SimFile.exists?("/counters/#{id}/") == false 21 22 :ok = SimFile.mkdir_p("/counters/#{id}/") 23 + assert SimFile.exists?("/counters/#{id}") == true 24 + assert SimFile.exists?("/counters/#{id}/") == true 25 + 22 26 {:ok, fd} = SimFile.open("/counters/#{id}/#{id}.count", [:read, :write]) 27 + assert SimFile.ls("/counters/#{id}/") == {:ok, ["#{id}.count"]} 23 28 24 29 SimLogger.debug "Init (id=#{fd}, fd=#{fd}, initial_count=#{initial_count})" 25 30