distri-filesystem 0.3.7

Filesystem and artifact tools for Distri agents
Documentation
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
use crate::{DirectoryEntry, DirectoryListing, FileReadResult, FileSystemConfig, ReadParams};
use anyhow::{anyhow, Context, Result};
use bytes::Bytes;
use futures::TryStreamExt;
use object_store::path::Path;
use object_store::ObjectStore;
use std::sync::Arc;

/// Object-store backed file storage implementation
#[derive(Debug)]
pub struct FileSystemStore {
    store: Arc<dyn ObjectStore>,
    root: Option<Path>,
}

impl FileSystemStore {
    /// Create a new FileSystemStore using the provided configuration
    pub async fn new(config: FileSystemConfig) -> Result<Self> {
        let store = crate::object_store::build_object_store(&config.object_store)?;

        let root = config.root_prefix.as_ref().and_then(|prefix| {
            let trimmed = prefix.trim_matches('/');
            if trimmed.is_empty() {
                None
            } else {
                Some(Path::from(trimmed))
            }
        });

        Ok(Self { store, root })
    }

    pub fn root_prefix(&self) -> Option<String> {
        self.root.as_ref().map(|p| p.to_string())
    }

    pub fn scoped(&self, prefix: Option<&str>) -> Result<Self> {
        let new_root = match prefix {
            Some(extra) => Self::combine_prefixes(&self.root, extra)?,
            None => self.root.clone(),
        };
        Ok(Self {
            store: self.store.clone(),
            root: new_root,
        })
    }

    fn ensure_safe_path(path: &str) -> Result<&str> {
        if path.split('/').any(|segment| segment == "..") {
            return Err(anyhow!("path segments must not contain '..'"));
        }
        Ok(path)
    }

    fn combine_prefixes(base: &Option<Path>, extra: &str) -> Result<Option<Path>> {
        let trimmed = extra.trim_matches('/');
        if trimmed.is_empty() {
            return Ok(base.clone());
        }
        Self::ensure_safe_path(trimmed)?;
        let combined = match base {
            Some(existing) => {
                let mut prefix = existing.to_string();
                if !prefix.ends_with('/') {
                    prefix.push('/');
                }
                prefix.push_str(trimmed);
                Path::from(prefix)
            }
            None => Path::from(trimmed),
        };
        Ok(Some(combined))
    }

    fn sanitize_object_path(&self, path: &str) -> Result<Path> {
        let trimmed = path.trim_matches('/');
        if trimmed.is_empty() {
            return Err(anyhow!("path cannot be empty"));
        }
        Self::ensure_safe_path(trimmed)?;
        let normalized = match &self.root {
            Some(root) if !root.as_ref().is_empty() => {
                // Join the root prefix with the relative path while preserving separators
                Path::from(format!("{}/{}", root, trimmed))
            }
            _ => Path::from(trimmed),
        };
        Ok(normalized)
    }

    fn sanitize_prefix(&self, path: &str) -> Result<Option<Path>> {
        let trimmed = path.trim_matches('/');
        if trimmed.is_empty() {
            return Ok(self.root.clone());
        }
        Self::ensure_safe_path(trimmed)?;
        let prefix = match &self.root {
            Some(root) if !root.as_ref().is_empty() => {
                // Combine the root prefix with the requested path using path separators
                Path::from(format!("{}/{}", root, trimmed))
            }
            _ => Path::from(trimmed),
        };
        Ok(Some(prefix))
    }

    fn prefix_depth(prefix: &Option<Path>) -> usize {
        prefix.as_ref().map(|p| p.parts().count()).unwrap_or(0)
    }

    fn entry_name(prefix: &Option<Path>, entry: &Path) -> Option<String> {
        let depth = Self::prefix_depth(prefix);
        entry
            .parts()
            .nth(depth)
            .map(|component| component.as_ref().to_string())
    }

    fn build_listing_entry(
        prefix: &Option<Path>,
        entry: &Path,
        is_dir: bool,
        size: Option<u64>,
    ) -> Option<DirectoryEntry> {
        Self::entry_name(prefix, entry).map(|name| DirectoryEntry {
            name,
            is_file: !is_dir,
            is_dir,
            size,
        })
    }

    fn read_to_string(bytes: Bytes, path: &str) -> Result<String> {
        String::from_utf8(bytes.to_vec())
            .with_context(|| format!("failed to decode file {} as utf-8", path))
    }

    /// Read file content as raw string without line numbers
    pub async fn read_raw(&self, path: &str) -> Result<String> {
        let object_path = self
            .sanitize_object_path(path)
            .with_context(|| format!("invalid file path: {}", path))?;
        tracing::debug!("object_path: {}", object_path.to_string());
        let get_result = self
            .store
            .get(&object_path)
            .await
            .map_err(|e| {
                tracing::error!("#{e}");
                e
            })
            .with_context(|| format!("failed to fetch object for {path}"))?;
        let bytes = get_result
            .bytes()
            .await
            .with_context(|| format!("failed to read bytes for {path}"))?;
        Self::read_to_string(bytes, path)
    }

    /// Read file content with line numbers and optional line range
    pub async fn read_with_line_numbers(
        &self,
        path: &str,
        params: ReadParams,
    ) -> Result<FileReadResult> {
        let content = self.read_raw(path).await?;

        let lines: Vec<&str> = content.lines().collect();
        let total_lines = lines.len() as u64;

        if total_lines == 0 {
            return Ok(FileReadResult {
                content: String::new(),
                start_line: 0,
                end_line: 0,
                total_lines: 0,
            });
        }

        let start = params.start_line.unwrap_or(1);
        let end = params.end_line.unwrap_or(total_lines);

        if start > total_lines || end > total_lines || start > end || start == 0 {
            return Err(anyhow!(
                "invalid line range: {}-{} for file {} with {} total lines",
                start,
                end,
                path,
                total_lines
            ));
        }

        let start_idx = (start - 1) as usize;
        let end_idx = end as usize;
        let selected_lines = &lines[start_idx..end_idx];
        let content_with_lines = selected_lines
            .iter()
            .enumerate()
            .map(|(i, line)| format!("{:4}→{}", start + i as u64, line))
            .collect::<Vec<_>>()
            .join("\n");

        Ok(FileReadResult {
            content: content_with_lines,
            start_line: start,
            end_line: end,
            total_lines,
        })
    }

    /// Read file with optional line range (backward compatibility - uses read_with_line_numbers)
    pub async fn read(&self, path: &str, params: ReadParams) -> Result<FileReadResult> {
        self.read_with_line_numbers(path, params).await
    }

    pub async fn write(&self, path: &str, content: &str) -> Result<()> {
        let object_path = self
            .sanitize_object_path(path)
            .with_context(|| format!("invalid file path: {}", path))?;
        tracing::debug!("object_path: {}", object_path.to_string());
        self.store
            .put(&object_path, Bytes::from(content.to_owned()))
            .await
            .map_err(|e| {
                tracing::error!("#{e}");
                e
            })
            .with_context(|| format!("failed to write file {}", path))?;
        Ok(())
    }

    pub async fn write_binary(&self, path: &str, content: &[u8]) -> Result<()> {
        let object_path = self
            .sanitize_object_path(path)
            .with_context(|| format!("invalid binary path: {}", path))?;
        tracing::debug!("object_path: {}", object_path.to_string());
        self.store
            .put(&object_path, Bytes::copy_from_slice(content))
            .await
            .with_context(|| format!("failed to write binary file {}", path))?;
        Ok(())
    }

    pub async fn read_binary(&self, path: &str) -> Result<Vec<u8>> {
        let object_path = self
            .sanitize_object_path(path)
            .with_context(|| format!("invalid binary path: {}", path))?;
        let get_result = self
            .store
            .get(&object_path)
            .await
            .map_err(|e| {
                tracing::error!("#{e}");
                e
            })
            .with_context(|| format!("failed to fetch binary object for {}", path))?;
        let bytes = get_result
            .bytes()
            .await
            .map_err(|e| {
                tracing::error!("#{e}");
                e
            })
            .with_context(|| format!("failed to read binary bytes for {}", path))?;
        Ok(bytes.to_vec())
    }

    pub async fn list(&self, path: &str) -> Result<DirectoryListing> {
        let prefix = self.sanitize_prefix(path)?;
        let list_result = match &prefix {
            Some(prefix) => self
                .store
                .list_with_delimiter(Some(prefix))
                .await
                .map_err(|e| {
                    tracing::error!("#{e}");
                    e
                })
                .with_context(|| format!("failed to list directory {}", path))?,
            None => self
                .store
                .list_with_delimiter(None)
                .await
                .map_err(|e| {
                    tracing::error!("#{e}");
                    e
                })
                .context("failed to list root directory")?,
        };

        let mut entries: Vec<DirectoryEntry> = Vec::new();

        for common_prefix in list_result.common_prefixes {
            if let Some(entry) = Self::build_listing_entry(&prefix, &common_prefix, true, None) {
                entries.push(entry);
            }
        }

        for object in list_result.objects {
            if let Some(entry) = Self::build_listing_entry(
                &prefix,
                &object.location,
                false,
                Some(object.size as u64),
            ) {
                entries.push(entry);
            }
        }

        entries.sort_by(|a, b| a.name.cmp(&b.name));
        entries.dedup_by(|a, b| a.name == b.name && a.is_dir == b.is_dir);

        Ok(DirectoryListing {
            path: path.to_string(),
            entries,
        })
    }

    pub async fn delete(&self, path: &str, recursive: bool) -> Result<()> {
        let object_path = self
            .sanitize_object_path(path)
            .with_context(|| format!("invalid delete path: {}", path))?;

        if recursive {
            let prefix = self
                .sanitize_prefix(path)?
                .ok_or_else(|| anyhow!("cannot delete root prefix recursively"))?;

            let mut stream = self.store.list(Some(&prefix));
            while let Some(meta) = stream.try_next().await? {
                self.store.delete(&meta.location).await.with_context(|| {
                    format!(
                        "failed to delete object {} while deleting directory {}",
                        meta.location, path
                    )
                })?;
            }
            return Ok(());
        }

        match self.store.delete(&object_path).await {
            Ok(()) => Ok(()),
            Err(object_store::Error::NotFound { .. }) => {
                let prefix = self.sanitize_prefix(path)?;
                if let Some(prefix) = prefix {
                    let mut stream = self.store.list(Some(&prefix));
                    if stream.try_next().await?.is_none() {
                        return Ok(());
                    }
                }
                Err(anyhow!("path {} not found", path))
            }
            Err(err) => Err(err).with_context(|| format!("failed to delete path {}", path)),
        }
    }

    pub async fn copy(&self, source: &str, destination: &str) -> Result<()> {
        let content = self
            .read(source, ReadParams::default())
            .await
            .with_context(|| format!("failed to read source file {}", source))?;
        self.write(destination, &content.content).await
    }

    pub async fn move_file(&self, source: &str, destination: &str) -> Result<()> {
        self.copy(source, destination).await?;
        self.delete(source, false).await
    }

    pub async fn info(&self, path: &str) -> Result<distri_types::filesystem::FileMetadata> {
        let object_path = self
            .sanitize_object_path(path)
            .with_context(|| format!("invalid info path: {}", path))?;
        let metadata = self
            .store
            .head(&object_path)
            .await
            .with_context(|| format!("failed to fetch metadata for {}", path))?;

        let preview_content = self
            .read(path, ReadParams::default())
            .await
            .ok()
            .map(|result| result.content);

        Ok(distri_types::filesystem::FileMetadata {
            file_id: uuid::Uuid::new_v4().to_string(),
            relative_path: path.trim_start_matches('/').to_string(),
            size: metadata.size as u64,
            content_type: None,
            original_filename: path.split('/').next_back().map(|s| s.to_string()),
            created_at: metadata.last_modified,
            updated_at: metadata.last_modified,
            checksum: metadata.e_tag,
            stats: None,
            preview: preview_content,
        })
    }

    pub async fn tree(&self, path: &str) -> Result<DirectoryListing> {
        self.list(path).await
    }
}