|
| 1 | +use crate::error::{Error, Result}; |
| 2 | +use crate::storage::operations::Mkdirer; |
| 3 | +use crate::storage::operations::mkdir::OpenDalMkdirer; |
| 4 | +use opendal::{ErrorKind, Operator}; |
| 5 | + |
| 6 | +/// Trait for touching files in storage (create or truncate) |
| 7 | +pub trait Toucher { |
| 8 | + /// Ensure a file exists. Optionally truncate existing files. |
| 9 | + /// |
| 10 | + /// - When `no_create` is true and the path does not exist, this is a no-op. |
| 11 | + /// - When `truncate` is true and the path exists as a file, it will be truncated to 0 bytes. |
| 12 | + /// - When `parents` is true, try to create parent directories when needed. |
| 13 | + async fn touch(&self, path: &str, no_create: bool, truncate: bool, parents: bool) |
| 14 | + -> Result<()>; |
| 15 | +} |
| 16 | + |
| 17 | +pub struct OpenDalToucher { |
| 18 | + operator: Operator, |
| 19 | +} |
| 20 | + |
| 21 | +impl OpenDalToucher { |
| 22 | + pub fn new(operator: Operator) -> Self { |
| 23 | + Self { operator } |
| 24 | + } |
| 25 | + |
| 26 | + fn parent_dir_of(path: &str) -> Option<String> { |
| 27 | + let trimmed = path.trim_matches('/'); |
| 28 | + if let Some(idx) = trimmed.rfind('/') { |
| 29 | + let (dir, _) = trimmed.split_at(idx); |
| 30 | + if dir.is_empty() { |
| 31 | + Some(String::new()) |
| 32 | + } else { |
| 33 | + Some(format!("{}/", dir)) |
| 34 | + } |
| 35 | + } else { |
| 36 | + None |
| 37 | + } |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +impl Toucher for OpenDalToucher { |
| 42 | + async fn touch( |
| 43 | + &self, |
| 44 | + path: &str, |
| 45 | + no_create: bool, |
| 46 | + truncate: bool, |
| 47 | + parents: bool, |
| 48 | + ) -> Result<()> { |
| 49 | + if path.ends_with('/') { |
| 50 | + return Err(Error::InvalidArgument { |
| 51 | + message: "touch does not support directories; use mkdir".to_string(), |
| 52 | + }); |
| 53 | + } |
| 54 | + |
| 55 | + match self.operator.stat(path).await { |
| 56 | + Ok(meta) => { |
| 57 | + if meta.mode().is_dir() { |
| 58 | + return Err(Error::InvalidArgument { |
| 59 | + message: "Path is a directory; use mkdir".to_string(), |
| 60 | + }); |
| 61 | + } |
| 62 | + |
| 63 | + if truncate { |
| 64 | + let mut writer = self.operator.writer(path).await?; |
| 65 | + writer.close().await?; |
| 66 | + println!("Truncated: {}", path); |
| 67 | + } |
| 68 | + // else: exists and not truncating -> no-op |
| 69 | + Ok(()) |
| 70 | + } |
| 71 | + Err(e) if e.kind() == ErrorKind::NotFound => { |
| 72 | + if no_create { |
| 73 | + // no-op when file is missing |
| 74 | + return Ok(()); |
| 75 | + } |
| 76 | + |
| 77 | + if parents |
| 78 | + && let Some(parent) = Self::parent_dir_of(path) |
| 79 | + && !parent.is_empty() |
| 80 | + { |
| 81 | + let mkdirer = OpenDalMkdirer::new(self.operator.clone()); |
| 82 | + Mkdirer::mkdir(&mkdirer, &parent, true).await?; |
| 83 | + } |
| 84 | + let mut writer = self.operator.writer(path).await?; |
| 85 | + writer.close().await?; |
| 86 | + println!("Created: {}", path); |
| 87 | + Ok(()) |
| 88 | + } |
| 89 | + Err(e) => Err(e.into()), |
| 90 | + } |
| 91 | + } |
| 92 | +} |
0 commit comments