1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
//! Daemon-side directory inspection for the UI's project picker (UI Phase-1).
//!
//! Why: the UI is a THIN CLIENT — "the UI communicates with the daemon; all UI
//! services talk to the daemon, the daemon provides all functionality". No UI
//! target touches the local filesystem directly, **including Tauri** (which
//! technically could, but must not: doing so would make the Tauri build capable
//! of something the web build isn't, forking the UI into two behaviours). So
//! "browse my disk to pick a project" has to be a daemon API, and this module
//! is it. One call serves both jobs the picker needs: rendering the folder list
//! (screen 7a — breadcrumb + a `git` badge per entry) AND feeding tcode's
//! three-state project binding model (projectless → non-git dir → git repo),
//! because [`DirEntryInfo::is_git_repo`] is exactly the discriminator that model
//! keys on. A non-git directory is a FIRST-CLASS, supported state here, never an
//! error — #2728 removed the git-repo short-circuit precisely so tcode indexes
//! non-git working dirs, and #2747 extended that to temp dirs.
//!
//! What: [`list_dir`] resolves a (possibly `~`-relative) path, then returns a
//! [`DirListing`] — the resolved path, a tilde-collapsed `display_path` for the
//! breadcrumb, the `parent` for up-navigation, and the sorted `entries`, each
//! carrying `name`/`path`/`is_dir`/`is_git_repo`. Failures are the typed
//! [`ListDirError`], distinguishable rather than stringly.
//!
//! ## No path guard here, deliberately — do not "fix" this
//!
//! There is no denylist, allowlist, sandbox root, or permission layer on the
//! path argument, and that is a decision, not an oversight. tcode is a LOCAL
//! app: the daemon runs as the user, with the user's own OS entitlements, so a
//! directory listing discloses nothing the user could not already get from `ls`
//! in their own shell. Decisively: the same API already exposes `task.run`,
//! which executes ARBITRARY CODE as the user. `list_dir` is strictly less
//! powerful than a capability sitting right beside it — guarding browse while
//! `task.run` is one method away is ceremony, not security. There is likewise no
//! macOS TCC permission-state machine: the app inherits whatever entitlements it
//! is granted, and an OS-level refusal surfaces as an ordinary typed error
//! ([`ListDirError::PermissionDenied`]) rather than a bespoke permission model.
//!
//! Note for anyone reaching for a precedent: trusty-search's
//! `allow_sensitive_path` (#2747) is NOT one. That guards INDEXING — file
//! CONTENT leaving the machine for an embedder — which is a categorically
//! different act from listing path names locally.
//!
//! Test: `fs_browse::tests::*` (resolution, git-ness incl. the linked-worktree
//! `.git`-as-file shape, error distinguishability, 7a response shape).
//!
//! [`DirEntryInfo::is_git_repo`]: crate::fs_browse::DirEntryInfo::is_git_repo
//! [`list_dir`]: crate::fs_browse::list_dir
//! [`DirListing`]: crate::fs_browse::DirListing
//! [`ListDirError`]: crate::fs_browse::ListDirError
//! [`ListDirError::PermissionDenied`]: crate::fs_browse::ListDirError::PermissionDenied
use Read as _;
use ;
use Serialize;
/// A single entry in a [`DirListing`] — one row of screen 7a's picker.
///
/// Why: carries exactly the four fields 7a renders and the binding model
/// consumes: the label (`name`), the value to navigate/bind to (`path`),
/// whether it can be descended into (`is_dir`), and whether it draws the `git`
/// badge vs. the `—` placeholder (`is_git_repo`).
/// What: `path` is absolute and already resolved; `is_git_repo` is always
/// `false` for a non-directory (a file cannot be a repo).
/// Test: `tests::lists_entries_with_names_and_paths`,
/// `tests::non_git_dir_is_reported_as_non_git`.
/// The result of a successful [`list_dir`] — everything screen 7a needs to
/// render one view of the picker.
///
/// Why: a bare `Vec<DirEntryInfo>` would force the client to re-derive
/// navigation state it cannot compute without filesystem access it does not
/// have. The listed directory has to describe itself: where it resolved to
/// (`path` — `~`/`..` are resolved server-side), how to caption it (`display_path`),
/// and where "up" goes (`parent`).
/// What: `path` is the canonical absolute directory; `display_path` is `path`
/// with the home prefix collapsed back to `~` for the breadcrumb (7a shows
/// `~/code / acme-api /`, which the client renders by splitting this on `/`);
/// `parent` is `None` only at the filesystem root, which is precisely when the
/// client must disable its up-affordance.
/// Test: `tests::listing_shape_supports_breadcrumb_and_badges`,
/// `tests::parent_is_none_at_filesystem_root`.
/// Why a [`list_dir`] call failed, in distinguishable variants.
///
/// Why: the picker reacts differently per cause — a typo'd path is a
/// recoverable inline message, a permission refusal is an OS-level prompt, an
/// unexpected IO fault is a bug report. A stringly error would force the client
/// to substring-match to tell those apart. Each variant maps to a distinct
/// JSON-RPC code in `protocol::to_rpc_error`.
/// What: `NotFound`/`NotADirectory`/`PermissionDenied` are the three
/// caller-actionable cases; `Io` is the residual OS fault; `NoHome` covers a
/// `~` path on a system with no resolvable home directory.
/// Test: `tests::nonexistent_path_is_not_found`,
/// `tests::file_path_is_not_a_directory`,
/// `tests::unreadable_dir_is_permission_denied`.
/// Expand a leading `~` against the user's home directory.
///
/// Why: the picker's breadcrumb speaks in `~/code`, and an operator typing a
/// path into it expects shell semantics. This mirrors the expansion the rest of
/// the workspace already does (`trusty-mpm`'s `managed_root::expand_tilde`,
/// `trusty-agents`' `repl::commands::connect::expand_tilde`) so tcode is
/// consistent with its siblings rather than inventing a third convention.
/// What: `~` and `~/...` expand against `dirs::home_dir`; `~user` is NOT
/// supported (no workspace precedent does it either) and is left verbatim, as
/// is every non-`~` path. Returns `NoHome` only when expansion was actually
/// required and home is unresolvable.
/// Test: `tests::tilde_expands_to_home`, `tests::tilde_bare_expands_to_home`,
/// `tests::absolute_path_is_left_alone`.
/// Collapse a leading home-directory prefix back to `~` for display.
///
/// Why: the inverse of [`expand_tilde`], so the breadcrumb reads `~/code`
/// rather than `/Users/masa/code` — screen 7a's caption. Purely cosmetic: the
/// client always navigates with the absolute `path`, never with this string.
/// What: returns `~` / `~/rest` when `path` is under home, else `path`
/// unchanged (including when home cannot be resolved at all).
/// Test: `tests::display_path_collapses_home_to_tilde`.
/// Whether `dir` is a git repository.
///
/// Why: this is 7a's `git` badge and the binding model's non-git → git
/// discriminator, so it has to be right on the shapes this repo actually
/// contains — and this workspace lives in linked worktrees. `pub(crate)` (not
/// private) since [`roster::list_projects`] (issue #3365's project-picker
/// roster) reuses the exact same discriminator to filter candidate
/// directories down to git repos — one implementation, not a second copy.
///
/// **The subtlety:** in a linked git worktree (and in a submodule), `.git` is a
/// FILE containing a `gitdir: <path>` pointer, NOT a directory. Testing
/// `.git.is_dir()` therefore reports every worktree as non-git — exactly the bug
/// PR #2839 hit in `build.rs`, and the same latent bug still sitting in
/// `trusty_common::project_discovery::inspect_project_dir`. This function
/// handles BOTH shapes.
///
/// What: `.git` as a directory → repo. `.git` as a file → repo only if it
/// actually carries git's documented `gitdir:` pointer, so a coincidental file
/// named `.git` does not earn a badge. The pointer check reads only the first
/// 64 bytes of the file rather than the whole thing — `gitdir: <path>\n` is
/// always well under that, and this runs on the picker's hot path across every
/// entry, so an unbounded `read_to_string` on a `.git` that happens to be a
/// large coincidental file (not a real pointer) would be an avoidable stall.
/// Anything else (including a metadata error, e.g. a permission refusal on the
/// probe) → `false`.
///
/// **Why not shell out to `git rev-parse`** (as `run_task::diff::is_git_repo`
/// does): that costs one subprocess PER ENTRY, and this runs across every entry
/// of a directory on an interactive picker's hot path — a 200-entry `~/code` at
/// ~5ms a spawn is a second of latency for a badge. Two `stat`s and, only for
/// the rare `.git`-as-file, one small bounded read gets the same answer.
/// `rev-parse` stays the right tool in `diff.rs`, which asks once per run about
/// one root.
///
/// Note this deliberately answers "is `dir` a repo ROOT", not "is `dir` inside a
/// work tree" — the picker badges repos, and a plain subdirectory of a repo is
/// not itself one to bind to.
/// Test: `tests::git_dir_is_detected`, `tests::linked_worktree_gitfile_is_detected`,
/// `tests::bogus_dot_git_file_is_not_a_repo`, `tests::non_git_dir_is_reported_as_non_git`,
/// `tests::large_dot_git_file_is_not_a_repo`, `tests::gitdir_pointer_at_exactly_the_read_bound`.
pub
/// Map an OS IO error onto the typed [`ListDirError`] for `path`.
///
/// Why: keeps the `NotFound`/`PermissionDenied`/`Io` triage in one place so
/// `list_dir`'s two fallible syscalls classify failures identically.
/// What: matches `ErrorKind::NotFound`/`PermissionDenied`, else `Io`.
/// Test: exercised via `tests::nonexistent_path_is_not_found` and
/// `tests::unreadable_dir_is_permission_denied`.
/// List `path`, returning everything the picker needs to render it.
///
/// Why: the single daemon-side primitive behind screen 7a — see module docs for
/// why this is an API at all rather than UI-local filesystem access, and for why
/// it carries no path guard.
/// What: expands `~`, canonicalizes (resolving `..`, symlinks, and relative
/// segments — so a client can navigate up by passing `parent`, or by passing
/// `<path>/..`), rejects a non-directory, then reads the entries. `include_hidden`
/// gates dot-entries: `false` (the picker default) hides them, since 7a lists
/// project folders and a user's home is otherwise mostly dotfile noise; `true`
/// is the escape hatch for reaching a genuinely hidden project directory.
/// Entries are sorted directories-first then case-insensitively by name, so the
/// picker's order is stable and does not depend on the OS's readdir order.
/// An entry whose metadata cannot be read is included with `is_dir: false`
/// rather than dropped — a listing that silently omits rows is worse than one
/// that shows an unbadged entry. Likewise, an entry that `read_dir`'s iterator
/// itself fails to produce (a per-entry OS fault mid-enumeration) is skipped
/// with a `tracing::warn!` rather than failing the whole listing — the same
/// best-effort philosophy, just one layer up. This skip path has no dedicated
/// test: triggering a per-entry `read_dir` iteration error deterministically
/// requires a TOCTOU race (e.g. another process removing an entry between
/// `readdir` returning its name and the kernel stat'ing it) that cannot be
/// reproduced portably in a unit test without faking the OS boundary; the
/// existing coverage for `list_dir`'s other per-entry fallback (metadata read
/// failure → `is_dir: false`, see `tests::*` above) exercises the same
/// best-effort code shape.
/// Test: `tests::*`.