filessh 0.4.4

A fast and convenient TUI file browser for remote servers
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
#![allow(dead_code)]
/// Inspired by the Parallel Walker in the
/// `ignore` crate
use color_eyre::eyre::Result;
use crossbeam::deque::{Stealer, Worker as Deque};
use futures::future::join_all; // add at top of file if not present
use russh_sftp::{
    client::{SftpSession, fs::Metadata},
    protocol::FileType,
};
use std::{
    path::PathBuf,
    sync::{
        Arc,
        atomic::{AtomicBool, AtomicUsize, Ordering},
    },
};
use tokio::task::JoinHandle;

use crate::files::FileEntry;

pub type Filter = Arc<dyn Fn(&FileEntry) -> bool + Send + Sync + 'static>;

#[allow(dead_code)]
pub struct WalkParallel {
    pub filter: Filter,
    pub path: PathBuf,
    pub max_depth: Option<usize>,
    pub min_depth: Option<usize>,
    pub threads: usize,
    pub sftp: Arc<SftpSession>,
}

#[derive(Debug, PartialEq, Eq)]
pub enum WalkState {
    Continue,
    Quit,
}

struct FnBuilder<F> {
    builder: F,
}

pub trait ParallelVisitorBuilder<'s>: Send {
    fn build(&mut self) -> Box<dyn ParallelVisitor + 's>;
}

impl<'s, P: ParallelVisitorBuilder<'s>> ParallelVisitorBuilder<'s> for &mut P {
    fn build(&mut self) -> Box<dyn ParallelVisitor + 's> {
        (**self).build()
    }
}

impl<'s, F: FnMut() -> FnVisitor<'s> + Send> ParallelVisitorBuilder<'s> for FnBuilder<F> {
    fn build(&mut self) -> Box<dyn ParallelVisitor + 's> {
        let visitor = (self.builder)();
        Box::new(FnVisitorImp { visitor })
    }
}

pub trait ParallelVisitor: Send {
    fn visit(&mut self, entry: Result<FileEntry>) -> WalkState;
}

type FnVisitor<'s> = Box<dyn FnMut(Result<FileEntry>) -> WalkState + Send + 's>;

struct FnVisitorImp<'s> {
    visitor: FnVisitor<'s>,
}
impl<'s> ParallelVisitor for FnVisitorImp<'s> {
    fn visit(&mut self, entry: Result<FileEntry>) -> WalkState {
        (self.visitor)(entry)
    }
}

impl WalkParallel {
    pub async fn run<F>(self, mkf: F)
    where
        F: FnMut() -> FnVisitor<'static> + Send,
    {
        self.visit(&mut FnBuilder { builder: mkf }).await
    }

    pub async fn visit(self, builder: &mut dyn ParallelVisitorBuilder<'static>) {
        let threads = self.threads();

        // --- Create the root work item ------------------------------------
        let root_path = self.path.display().to_string();

        // Build a synthetic root FileEntry (a directory)
        let attr = Metadata::empty();
        let root_entry = FileEntry::from_file(root_path.clone(), FileType::Dir, attr);

        // The traversal starts at the root path (cwd = root_path)
        let init: Vec<Message> = vec![Message::Work(Work {
            entry: root_entry,
            cwd: root_path.clone(),
        })];

        // --- Create per-thread work-stealing stacks -----------------------
        let stacks = Stack::new_for_each_thread(threads, init);

        // --- Shared state -------------------------------------------------
        let quit_now = Arc::new(AtomicBool::new(false));
        let active_workers = Arc::new(AtomicUsize::new(threads));

        // --- Build all visitors *before* spawning -------------------------
        let visitors: Vec<Box<dyn ParallelVisitor + 'static>> =
            (0..threads).map(|_| builder.build()).collect();

        // --- Spawn workers ------------------------------------------------
        let mut handles: Vec<JoinHandle<()>> = Vec::with_capacity(threads);

        for (stack, visitor) in stacks.into_iter().zip(visitors.into_iter()) {
            let worker = Woker {
                visitor,
                stack,
                quit_now: Arc::clone(&quit_now),
                active_workers: Arc::clone(&active_workers),
                max_depth: self.max_depth,
                filter: Some(self.filter.clone()),
                sftp: Arc::clone(&self.sftp),
            };

            // Each worker runs concurrently; owns its visitor and stack
            handles.push(tokio::spawn(async move {
                worker.run().await;
            }));
        }

        // --- Wait for all workers to finish -------------------------------
        let _ = join_all(handles).await;
    }

    fn threads(&self) -> usize {
        if self.threads == 0 {
            std::thread::available_parallelism()
                .map_or(1, |n| n.get())
                .min(12)
        } else {
            self.threads
        }
    }
}

enum Message {
    /// A work item corresponds to a directory that should be descended into.
    /// Work items for entries that should be skipped or ignored should not
    /// be produced.
    Work(Work),
    /// This instruction indicates that the worker should quit.
    Quit,
}

pub struct Work {
    entry: FileEntry,
    cwd: String, // full remote path to the *entry*
}

impl Work {
    pub async fn read_dir(&self, sftp: Arc<SftpSession>) -> Result<Vec<Work>> {
        // Determine the directory path we should read.
        // For the root, `cwd` is already the directory path.
        let dir_path = if self.entry.is_dir() {
            if self.cwd.ends_with(self.entry.name()) {
                // If cwd already ends with the entry name, use it as-is
                self.cwd.clone()
            } else {
                // Otherwise, join cwd + entry name
                format!("{}/{}", self.cwd, self.entry.name())
            }
        } else {
            // Not a directory, nothing to read
            return Ok(vec![]);
        };

        // Read entries via SFTP
        let entries = sftp.read_dir(dir_path.clone()).await?;

        let works = entries
            .map(|child| Work {
                entry: FileEntry::from(child),
                cwd: dir_path.clone(), // new base for children
            })
            .collect::<Vec<_>>();

        Ok(works)
    }
}

/// A work-stealing stack.
#[derive(Debug)]
struct Stack {
    /// This thread's index.
    index: usize,
    /// The thread-local stack.
    deque: Deque<Message>,
    /// The work stealers.
    stealers: Arc<[Stealer<Message>]>,
}

impl Stack {
    /// Create a work-stealing stack for each thread. The given messages
    /// correspond to the initial paths to start the search at. They will
    /// be distributed automatically to each stack in a round-robin fashion.
    fn new_for_each_thread(threads: usize, init: Vec<Message>) -> Vec<Stack> {
        // Using new_lifo() ensures each worker operates depth-first, not
        // breadth-first. We do depth-first because a breadth first traversal
        // on wide directories with a lot of gitignores is disastrous (for
        // example, searching a directory tree containing all of crates.io).
        let deques: Vec<Deque<Message>> = std::iter::repeat_with(Deque::new_lifo)
            .take(threads)
            .collect();
        let stealers =
            Arc::<[Stealer<Message>]>::from(deques.iter().map(Deque::stealer).collect::<Vec<_>>());
        let stacks: Vec<Stack> = deques
            .into_iter()
            .enumerate()
            .map(|(index, deque)| Stack {
                index,
                deque,
                stealers: stealers.clone(),
            })
            .collect();
        // Distribute the initial messages, reverse the order to cancel out
        // the other reversal caused by the inherent LIFO processing of the
        // per-thread stacks which are filled here.
        init.into_iter()
            .rev()
            .zip(stacks.iter().cycle())
            .for_each(|(m, s)| s.push(m));
        stacks
    }

    /// Push a message.
    fn push(&self, msg: Message) {
        self.deque.push(msg);
    }

    /// Pop a message.
    fn pop(&self) -> Option<Message> {
        self.deque.pop().or_else(|| self.steal())
    }

    /// Steal a message from another queue.
    fn steal(&self) -> Option<Message> {
        // For fairness, try to steal from index + 1, index + 2, ... len - 1,
        // then wrap around to 0, 1, ... index - 1.
        let (left, right) = self.stealers.split_at(self.index);
        // Don't steal from ourselves
        let right = &right[1..];

        right
            .iter()
            .chain(left.iter())
            .map(|s| s.steal_batch_and_pop(&self.deque))
            .find_map(|s| s.success())
    }
}

pub struct Woker<'a> {
    visitor: Box<dyn ParallelVisitor + 'a>,
    stack: Stack,
    quit_now: Arc<AtomicBool>,
    active_workers: Arc<AtomicUsize>,
    max_depth: Option<usize>,
    filter: Option<Filter>,
    sftp: Arc<SftpSession>,
}

impl WalkState {
    fn is_continue(&self) -> bool {
        *self == WalkState::Continue
    }

    fn is_quit(&self) -> bool {
        *self == WalkState::Quit
    }
}

impl<'a> Woker<'a> {
    pub async fn run(mut self) {
        while let Some(work) = self.get_work() {
            if let WalkState::Quit = self.run_one(work).await {
                self.quit_now();
            }
        }
    }

    pub async fn run_one(&mut self, work: Work) -> WalkState {
        let sftp = Arc::clone(&self.sftp);
        let readdir = work.read_dir(sftp).await;

        // --- Create a new FileEntry with an absolute name -----------------
        let abs_entry = {
            let path = if work.cwd.ends_with(work.entry.name()) {
                work.cwd.clone()
            } else {
                PathBuf::from(&work.cwd)
                    .join(work.entry.name())
                    .to_string_lossy()
                    .to_string()
            };

            // Rebuild FileEntry, preserving its metadata but using abs path as the name
            FileEntry::from_file(path, FileType::Dir, work.entry.attributes.clone())
        };

        // Visit the current file/directory with absolute name
        let state = self.visitor.visit(Ok(abs_entry));
        if !state.is_continue() {
            return state;
        }

        // --- Process directory contents -----------------------------------
        let readdir = match readdir {
            Ok(readdir) => readdir,
            Err(err) => return self.visitor.visit(Err(err)),
        };

        for child_work in readdir {
            let state = self.generate_work(child_work).await;
            if state.is_quit() {
                return state;
            }
        }

        WalkState::Continue
    }

    pub async fn generate_work(&mut self, work: Work) -> WalkState {
        // Push this new work onto the queue
        self.send(work);
        WalkState::Continue
    }

    fn get_work(&mut self) -> Option<Work> {
        let mut value = self.recv();
        loop {
            // Simulate a priority channel: If quit_now flag is set, we can
            // receive only quit messages.
            if self.is_quit_now() {
                value = Some(Message::Quit)
            }
            match value {
                Some(Message::Work(work)) => {
                    return Some(work);
                }
                Some(Message::Quit) => {
                    // Repeat quit message to wake up sleeping threads, if
                    // any. The domino effect will ensure that every thread
                    // will quit.
                    self.send_quit();
                    return None;
                }
                None => {
                    if self.deactivate_worker() == 0 {
                        // If deactivate_worker() returns 0, every worker thread
                        // is currently within the critical section between the
                        // acquire in deactivate_worker() and the release in
                        // activate_worker() below.  For this to happen, every
                        // worker's local deque must be simultaneously empty,
                        // meaning there is no more work left at all.
                        self.send_quit();
                        return None;
                    }
                    // Wait for next `Work` or `Quit` message.
                    loop {
                        if let Some(v) = self.recv() {
                            self.activate_worker();
                            value = Some(v);
                            break;
                        }
                        // Our stack isn't blocking. Instead of burning the
                        // CPU waiting, we let the thread sleep for a bit. In
                        // general, this tends to only occur once the search is
                        // approaching termination.
                        let dur = std::time::Duration::from_millis(1);
                        std::thread::sleep(dur);
                    }
                }
            }
        }
    }

    /// Indicates that all workers should quit immediately.
    fn quit_now(&self) {
        self.quit_now.store(true, Ordering::SeqCst);
    }

    /// Returns true if this worker should quit immediately.
    fn is_quit_now(&self) -> bool {
        self.quit_now.load(Ordering::SeqCst)
    }

    /// Send work.
    fn send(&self, work: Work) {
        self.stack.push(Message::Work(work));
    }

    /// Send a quit message.
    fn send_quit(&self) {
        self.stack.push(Message::Quit);
    }

    /// Receive work.
    fn recv(&self) -> Option<Message> {
        self.stack.pop()
    }

    /// Deactivates a worker and returns the number of currently active workers.
    fn deactivate_worker(&self) -> usize {
        self.active_workers.fetch_sub(1, Ordering::Acquire) - 1
    }

    /// Reactivates a worker.
    fn activate_worker(&self) {
        self.active_workers.fetch_add(1, Ordering::Release);
    }
}