-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathfs_read.rs
679 lines (613 loc) · 21.8 KB
/
fs_read.rs
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
use std::collections::VecDeque;
use std::fs::Metadata;
use std::io::Write;
use std::os::unix::fs::PermissionsExt;
use crossterm::queue;
use crossterm::style::{
self,
Color,
};
use eyre::{
Result,
bail,
};
use fig_os_shim::Context;
use serde::{
Deserialize,
Serialize,
};
use syntect::util::LinesWithEndings;
use tracing::{
debug,
warn,
};
use super::{
InvokeOutput,
MAX_TOOL_RESPONSE_SIZE,
OutputKind,
format_path,
sanitize_path_tool_arg,
};
#[derive(Debug, Clone, Deserialize)]
#[serde(tag = "mode")]
pub enum FsRead {
Line(FsLine),
Directory(FsDirectory),
Search(FsSearch),
}
impl FsRead {
pub async fn validate(&mut self, ctx: &Context) -> Result<()> {
match self {
FsRead::Line(fs_line) => fs_line.validate(ctx).await,
FsRead::Directory(fs_directory) => fs_directory.validate(ctx).await,
FsRead::Search(fs_search) => fs_search.validate(ctx).await,
}
}
pub async fn queue_description(&self, ctx: &Context, updates: &mut impl Write) -> Result<()> {
match self {
FsRead::Line(fs_line) => fs_line.queue_description(ctx, updates).await,
FsRead::Directory(fs_directory) => fs_directory.queue_description(updates),
FsRead::Search(fs_search) => fs_search.queue_description(updates),
}
}
pub async fn invoke(&self, ctx: &Context, updates: &mut impl Write) -> Result<InvokeOutput> {
match self {
FsRead::Line(fs_line) => fs_line.invoke(ctx, updates).await,
FsRead::Directory(fs_directory) => fs_directory.invoke(ctx, updates).await,
FsRead::Search(fs_search) => fs_search.invoke(ctx, updates).await,
}
}
}
/// Read lines from a file.
#[derive(Debug, Clone, Deserialize)]
pub struct FsLine {
pub path: String,
pub start_line: Option<i32>,
pub end_line: Option<i32>,
}
impl FsLine {
const DEFAULT_END_LINE: i32 = -1;
const DEFAULT_START_LINE: i32 = 1;
pub async fn validate(&mut self, ctx: &Context) -> Result<()> {
let path = sanitize_path_tool_arg(ctx, &self.path);
if !path.exists() {
bail!("'{}' does not exist", self.path);
}
let is_file = ctx.fs().symlink_metadata(&path).await?.is_file();
if !is_file {
bail!("'{}' is not a file", self.path);
}
Ok(())
}
pub async fn queue_description(&self, ctx: &Context, updates: &mut impl Write) -> Result<()> {
let path = sanitize_path_tool_arg(ctx, &self.path);
let line_count = ctx.fs().read_to_string(&path).await?.lines().count();
queue!(
updates,
style::Print("Reading file: "),
style::SetForegroundColor(Color::Green),
style::Print(&self.path),
style::ResetColor,
style::Print(", "),
)?;
let start = convert_negative_index(line_count, self.start_line()) + 1;
let end = convert_negative_index(line_count, self.end_line()) + 1;
match (start, end) {
_ if start == 1 && end == line_count => Ok(queue!(updates, style::Print("all lines".to_string()))?),
_ if end == line_count => Ok(queue!(
updates,
style::Print("from line "),
style::SetForegroundColor(Color::Green),
style::Print(start),
style::ResetColor,
style::Print(" to end of file"),
)?),
_ => Ok(queue!(
updates,
style::Print("from line "),
style::SetForegroundColor(Color::Green),
style::Print(start),
style::ResetColor,
style::Print(" to "),
style::SetForegroundColor(Color::Green),
style::Print(end),
style::ResetColor,
)?),
}
}
pub async fn invoke(&self, ctx: &Context, updates: &mut impl Write) -> Result<InvokeOutput> {
let path = sanitize_path_tool_arg(ctx, &self.path);
let relative_path = format_path(ctx.env().current_dir()?, &path);
debug!(?path, "Reading");
let file = ctx.fs().read_to_string(&path).await?;
let line_count = file.lines().count();
let (start, end) = (
convert_negative_index(line_count, self.start_line()),
convert_negative_index(line_count, self.end_line()),
);
// safety check to ensure end is always greater than start
let end = end.max(start);
if start >= line_count {
bail!(
"starting index: {} is outside of the allowed range: ({}, {})",
self.start_line(),
-(line_count as i64),
line_count
);
}
// The range should be inclusive on both ends.
let file_contents = file
.lines()
.skip(start)
.take(end - start + 1)
.collect::<Vec<_>>()
.join("\n");
queue!(
updates,
style::Print("Reading: "),
style::SetForegroundColor(Color::Green),
style::Print(relative_path),
style::ResetColor,
style::Print("\n"),
)?;
let byte_count = file_contents.len();
if byte_count > MAX_TOOL_RESPONSE_SIZE {
bail!(
"This tool only supports reading {MAX_TOOL_RESPONSE_SIZE} bytes at a
time. You tried to read {byte_count} bytes. Try executing with fewer lines specified."
);
}
Ok(InvokeOutput {
output: OutputKind::Text(file_contents),
})
}
fn start_line(&self) -> i32 {
self.start_line.unwrap_or(Self::DEFAULT_START_LINE)
}
fn end_line(&self) -> i32 {
self.end_line.unwrap_or(Self::DEFAULT_END_LINE)
}
}
/// Search in a file.
#[derive(Debug, Clone, Deserialize)]
pub struct FsSearch {
pub path: String,
pub pattern: String,
pub context_lines: Option<usize>,
}
impl FsSearch {
const CONTEXT_LINE_PREFIX: &str = " ";
const DEFAULT_CONTEXT_LINES: usize = 2;
const MATCHING_LINE_PREFIX: &str = "→ ";
pub async fn validate(&mut self, ctx: &Context) -> Result<()> {
let path = sanitize_path_tool_arg(ctx, &self.path);
let relative_path = format_path(ctx.env().current_dir()?, &path);
if !path.exists() {
bail!("File not found: {}", relative_path);
}
if !ctx.fs().symlink_metadata(path).await?.is_file() {
bail!("Path is not a file: {}", relative_path);
}
if self.pattern.is_empty() {
bail!("Search pattern cannot be empty");
}
Ok(())
}
pub fn queue_description(&self, updates: &mut impl Write) -> Result<()> {
queue!(
updates,
style::Print("Searching: "),
style::SetForegroundColor(Color::Green),
style::Print(&self.path),
style::ResetColor,
style::Print(" for pattern: "),
style::SetForegroundColor(Color::Green),
style::Print(&self.pattern.to_lowercase()),
style::ResetColor,
)?;
Ok(())
}
pub async fn invoke(&self, ctx: &Context, updates: &mut impl Write) -> Result<InvokeOutput> {
let file_path = sanitize_path_tool_arg(ctx, &self.path);
let pattern = &self.pattern;
let relative_path = format_path(ctx.env().current_dir()?, &file_path);
let file_content = ctx.fs().read_to_string(&file_path).await?;
let lines: Vec<&str> = LinesWithEndings::from(&file_content).collect();
let mut results = Vec::new();
let mut total_matches = 0;
// Case insensitive search
let pattern_lower = pattern.to_lowercase();
for (line_num, line) in lines.iter().enumerate() {
if line.to_lowercase().contains(&pattern_lower) {
total_matches += 1;
let start = line_num.saturating_sub(self.context_lines());
let end = lines.len().min(line_num + self.context_lines() + 1);
let mut context_text = Vec::new();
(start..end).for_each(|i| {
let prefix = if i == line_num {
Self::MATCHING_LINE_PREFIX
} else {
Self::CONTEXT_LINE_PREFIX
};
let line_text = lines[i].to_string();
context_text.push(format!("{}{}: {}", prefix, i + 1, line_text));
});
let match_text = context_text.join("");
results.push(SearchMatch {
line_number: line_num + 1,
context: match_text,
});
}
}
queue!(
updates,
style::SetForegroundColor(Color::Yellow),
style::ResetColor,
style::Print(format!(
"Found {} matches for pattern '{}' in {}\n",
total_matches, pattern, relative_path
)),
style::Print("\n"),
style::ResetColor,
)?;
Ok(InvokeOutput {
output: OutputKind::Text(serde_json::to_string(&results)?),
})
}
fn context_lines(&self) -> usize {
self.context_lines.unwrap_or(Self::DEFAULT_CONTEXT_LINES)
}
}
/// List directory contents.
#[derive(Debug, Clone, Deserialize)]
pub struct FsDirectory {
pub path: String,
pub depth: Option<usize>,
}
impl FsDirectory {
const DEFAULT_DEPTH: usize = 0;
pub async fn validate(&mut self, ctx: &Context) -> Result<()> {
let path = sanitize_path_tool_arg(ctx, &self.path);
let relative_path = format_path(ctx.env().current_dir()?, &path);
if !path.exists() {
bail!("Directory not found: {}", relative_path);
}
if !ctx.fs().symlink_metadata(path).await?.is_dir() {
bail!("Path is not a directory: {}", relative_path);
}
Ok(())
}
pub fn queue_description(&self, updates: &mut impl Write) -> Result<()> {
queue!(
updates,
style::Print("Reading directory: "),
style::SetForegroundColor(Color::Green),
style::Print(&self.path),
style::ResetColor,
style::Print(" "),
)?;
let depth = self.depth.unwrap_or_default();
Ok(queue!(
updates,
style::Print(format!("with maximum depth of {}", depth))
)?)
}
pub async fn invoke(&self, ctx: &Context, updates: &mut impl Write) -> Result<InvokeOutput> {
let path = sanitize_path_tool_arg(ctx, &self.path);
let cwd = ctx.env().current_dir()?;
let max_depth = self.depth();
debug!(?path, max_depth, "Reading directory at path with depth");
let mut result = Vec::new();
let mut dir_queue = VecDeque::new();
dir_queue.push_back((path, 0));
while let Some((path, depth)) = dir_queue.pop_front() {
if depth > max_depth {
break;
}
let relative_path = format_path(&cwd, &path);
if !relative_path.is_empty() {
queue!(
updates,
style::Print("Reading: "),
style::SetForegroundColor(Color::Green),
style::Print(&relative_path),
style::ResetColor,
style::Print("\n"),
)?;
}
let mut read_dir = ctx.fs().read_dir(path).await?;
while let Some(ent) = read_dir.next_entry().await? {
use std::os::unix::fs::MetadataExt;
let md = ent.metadata().await?;
let formatted_mode = format_mode(md.permissions().mode()).into_iter().collect::<String>();
let modified_timestamp = md.modified()?.duration_since(std::time::UNIX_EPOCH)?.as_secs();
let datetime = time::OffsetDateTime::from_unix_timestamp(modified_timestamp as i64).unwrap();
let formatted_date = datetime
.format(time::macros::format_description!(
"[month repr:short] [day] [hour]:[minute]"
))
.unwrap();
// Mostly copying "The Long Format" from `man ls`.
// TODO: query user/group database to convert uid/gid to names?
result.push(format!(
"{}{} {} {} {} {} {} {}",
format_ftype(&md),
formatted_mode,
md.nlink(),
md.uid(),
md.gid(),
md.size(),
formatted_date,
ent.path().to_string_lossy()
));
if md.is_dir() {
dir_queue.push_back((ent.path(), depth + 1));
}
}
}
let file_count = result.len();
let result = result.join("\n");
let byte_count = result.len();
if byte_count > MAX_TOOL_RESPONSE_SIZE {
bail!(
"This tool only supports reading up to {MAX_TOOL_RESPONSE_SIZE} bytes at a time. You tried to read {byte_count} bytes ({file_count} files). Try executing with fewer lines specified."
);
}
Ok(InvokeOutput {
output: OutputKind::Text(result),
})
}
fn depth(&self) -> usize {
self.depth.unwrap_or(Self::DEFAULT_DEPTH)
}
}
/// Converts negative 1-based indices to positive 0-based indices.
fn convert_negative_index(line_count: usize, i: i32) -> usize {
if i <= 0 {
(line_count as i32 + i).max(0) as usize
} else {
i as usize - 1
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
struct SearchMatch {
line_number: usize,
context: String,
}
fn format_ftype(md: &Metadata) -> char {
if md.is_symlink() {
'l'
} else if md.is_file() {
'-'
} else if md.is_dir() {
'd'
} else {
warn!("unknown file metadata: {:?}", md);
'-'
}
}
/// Formats a permissions mode into the form used by `ls`, e.g. `0o644` to `rw-r--r--`
fn format_mode(mode: u32) -> [char; 9] {
let mut mode = mode & 0o777;
let mut res = ['-'; 9];
fn octal_to_chars(val: u32) -> [char; 3] {
match val {
1 => ['-', '-', 'x'],
2 => ['-', 'w', '-'],
3 => ['-', 'w', 'x'],
4 => ['r', '-', '-'],
5 => ['r', '-', 'x'],
6 => ['r', 'w', '-'],
7 => ['r', 'w', 'x'],
_ => ['-', '-', '-'],
}
}
for c in res.rchunks_exact_mut(3) {
c.copy_from_slice(&octal_to_chars(mode & 0o7));
mode /= 0o10;
}
res
}
#[cfg(test)]
mod tests {
use std::sync::Arc;
use super::*;
const TEST_FILE_CONTENTS: &str = "\
1: Hello world!
2: This is line 2
3: asdf
4: Hello world!
";
const TEST_FILE_PATH: &str = "/test_file.txt";
const TEST_HIDDEN_FILE_PATH: &str = "/aaaa2/.hidden";
/// Sets up the following filesystem structure:
/// ```text
/// test_file.txt
/// /home/testuser/
/// /aaaa1/
/// /bbbb1/
/// /cccc1/
/// /aaaa2/
/// .hidden
/// ```
async fn setup_test_directory() -> Arc<Context> {
let ctx = Context::builder().with_test_home().await.unwrap().build_fake();
let fs = ctx.fs();
fs.write(TEST_FILE_PATH, TEST_FILE_CONTENTS).await.unwrap();
fs.create_dir_all("/aaaa1/bbbb1/cccc1").await.unwrap();
fs.create_dir_all("/aaaa2").await.unwrap();
fs.write(TEST_HIDDEN_FILE_PATH, "this is a hidden file").await.unwrap();
ctx
}
#[test]
fn test_negative_index_conversion() {
assert_eq!(convert_negative_index(5, -100), 0);
assert_eq!(convert_negative_index(5, -1), 4);
}
#[test]
fn test_fs_read_deser() {
serde_json::from_value::<FsRead>(serde_json::json!({ "path": "/test_file.txt", "mode": "Line" })).unwrap();
serde_json::from_value::<FsRead>(
serde_json::json!({ "path": "/test_file.txt", "mode": "Line", "end_line": 5 }),
)
.unwrap();
serde_json::from_value::<FsRead>(
serde_json::json!({ "path": "/test_file.txt", "mode": "Line", "start_line": -1 }),
)
.unwrap();
serde_json::from_value::<FsRead>(
serde_json::json!({ "path": "/test_file.txt", "mode": "Line", "start_line": None::<usize> }),
)
.unwrap();
serde_json::from_value::<FsRead>(serde_json::json!({ "path": "/", "mode": "Directory" })).unwrap();
serde_json::from_value::<FsRead>(
serde_json::json!({ "path": "/test_file.txt", "mode": "Directory", "depth": 2 }),
)
.unwrap();
serde_json::from_value::<FsRead>(
serde_json::json!({ "path": "/test_file.txt", "mode": "Search", "pattern": "hello" }),
)
.unwrap();
}
#[tokio::test]
async fn test_fs_read_line_invoke() {
let ctx = setup_test_directory().await;
let lines = TEST_FILE_CONTENTS.lines().collect::<Vec<_>>();
let mut stdout = std::io::stdout();
macro_rules! assert_lines {
($start_line:expr, $end_line:expr, $expected:expr) => {
let v = serde_json::json!({
"path": TEST_FILE_PATH,
"mode": "Line",
"start_line": $start_line,
"end_line": $end_line,
});
let output = serde_json::from_value::<FsRead>(v)
.unwrap()
.invoke(&ctx, &mut stdout)
.await
.unwrap();
if let OutputKind::Text(text) = output.output {
assert_eq!(text, $expected.join("\n"), "actual(left) does not equal
expected(right) for (start_line, end_line): ({:?}, {:?})", $start_line, $end_line);
} else {
panic!("expected text output");
}
}
}
assert_lines!(None::<i32>, None::<i32>, lines[..]);
assert_lines!(1, 2, lines[..=1]);
assert_lines!(1, -1, lines[..]);
assert_lines!(2, 1, lines[1..=1]);
assert_lines!(-2, -1, lines[2..]);
assert_lines!(-2, None::<i32>, lines[2..]);
assert_lines!(2, None::<i32>, lines[1..]);
}
#[tokio::test]
async fn test_fs_read_line_past_eof() {
let ctx = setup_test_directory().await;
let mut stdout = std::io::stdout();
let v = serde_json::json!({
"path": TEST_FILE_PATH,
"mode": "Line",
"start_line": 100,
"end_line": None::<i32>,
});
assert!(
serde_json::from_value::<FsRead>(v)
.unwrap()
.invoke(&ctx, &mut stdout)
.await
.is_err()
);
}
#[test]
fn test_format_mode() {
macro_rules! assert_mode {
($actual:expr, $expected:expr) => {
assert_eq!(format_mode($actual).iter().collect::<String>(), $expected);
};
}
assert_mode!(0o000, "---------");
assert_mode!(0o700, "rwx------");
assert_mode!(0o744, "rwxr--r--");
assert_mode!(0o641, "rw-r----x");
}
#[tokio::test]
async fn test_fs_read_directory_invoke() {
let ctx = setup_test_directory().await;
let mut stdout = std::io::stdout();
// Testing without depth
let v = serde_json::json!({
"mode": "Directory",
"path": "/",
});
let output = serde_json::from_value::<FsRead>(v)
.unwrap()
.invoke(&ctx, &mut stdout)
.await
.unwrap();
if let OutputKind::Text(text) = output.output {
assert_eq!(text.lines().collect::<Vec<_>>().len(), 4);
} else {
panic!("expected text output");
}
// Testing with depth level 1
let v = serde_json::json!({
"mode": "Directory",
"path": "/",
"depth": 1,
});
let output = serde_json::from_value::<FsRead>(v)
.unwrap()
.invoke(&ctx, &mut stdout)
.await
.unwrap();
if let OutputKind::Text(text) = output.output {
let lines = text.lines().collect::<Vec<_>>();
assert_eq!(lines.len(), 7);
assert!(
!lines.iter().any(|l| l.contains("cccc1")),
"directory at depth level 2 should not be included in output"
);
} else {
panic!("expected text output");
}
}
#[tokio::test]
async fn test_fs_read_search_invoke() {
let ctx = setup_test_directory().await;
let mut stdout = std::io::stdout();
macro_rules! invoke_search {
($value:tt) => {{
let v = serde_json::json!($value);
let output = serde_json::from_value::<FsRead>(v)
.unwrap()
.invoke(&ctx, &mut stdout)
.await
.unwrap();
if let OutputKind::Text(value) = output.output {
serde_json::from_str::<Vec<SearchMatch>>(&value).unwrap()
} else {
panic!("expected Text output")
}
}};
}
let matches = invoke_search!({
"mode": "Search",
"path": TEST_FILE_PATH,
"pattern": "hello",
});
assert_eq!(matches.len(), 2);
assert_eq!(matches[0].line_number, 1);
assert_eq!(
matches[0].context,
format!(
"{}1: 1: Hello world!\n{}2: 2: This is line 2\n{}3: 3: asdf\n",
FsSearch::MATCHING_LINE_PREFIX,
FsSearch::CONTEXT_LINE_PREFIX,
FsSearch::CONTEXT_LINE_PREFIX
)
);
}
}