opendal/services/ftp/
backend.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18use std::fmt::Debug;
19use std::fmt::Formatter;
20use std::str;
21use std::str::FromStr;
22use std::sync::Arc;
23
24use super::core::FtpCore;
25use http::Uri;
26use log::debug;
27use services::ftp::core::Manager;
28use suppaftp::list::File;
29use suppaftp::types::Response;
30use suppaftp::FtpError;
31use suppaftp::Status;
32use tokio::sync::OnceCell;
33use uuid::Uuid;
34
35use super::delete::FtpDeleter;
36use super::err::parse_error;
37use super::lister::FtpLister;
38use super::reader::FtpReader;
39use super::writer::FtpWriter;
40use crate::raw::*;
41use crate::services::FtpConfig;
42use crate::*;
43
44impl Configurator for FtpConfig {
45    type Builder = FtpBuilder;
46    fn into_builder(self) -> Self::Builder {
47        FtpBuilder { config: self }
48    }
49}
50
51/// FTP and FTPS services support.
52#[doc = include_str!("docs.md")]
53#[derive(Default)]
54pub struct FtpBuilder {
55    config: FtpConfig,
56}
57
58impl Debug for FtpBuilder {
59    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
60        f.debug_struct("FtpBuilder")
61            .field("config", &self.config)
62            .finish()
63    }
64}
65
66impl FtpBuilder {
67    /// set endpoint for ftp backend.
68    pub fn endpoint(mut self, endpoint: &str) -> Self {
69        self.config.endpoint = if endpoint.is_empty() {
70            None
71        } else {
72            Some(endpoint.to_string())
73        };
74
75        self
76    }
77
78    /// set root path for ftp backend.
79    pub fn root(mut self, root: &str) -> Self {
80        self.config.root = if root.is_empty() {
81            None
82        } else {
83            Some(root.to_string())
84        };
85
86        self
87    }
88
89    /// set user for ftp backend.
90    pub fn user(mut self, user: &str) -> Self {
91        self.config.user = if user.is_empty() {
92            None
93        } else {
94            Some(user.to_string())
95        };
96
97        self
98    }
99
100    /// set password for ftp backend.
101    pub fn password(mut self, password: &str) -> Self {
102        self.config.password = if password.is_empty() {
103            None
104        } else {
105            Some(password.to_string())
106        };
107
108        self
109    }
110}
111
112impl Builder for FtpBuilder {
113    const SCHEME: Scheme = Scheme::Ftp;
114    type Config = FtpConfig;
115
116    fn build(self) -> Result<impl Access> {
117        debug!("ftp backend build started: {:?}", &self);
118        let endpoint = match &self.config.endpoint {
119            None => return Err(Error::new(ErrorKind::ConfigInvalid, "endpoint is empty")),
120            Some(v) => v,
121        };
122
123        let endpoint_uri = match endpoint.parse::<Uri>() {
124            Err(e) => {
125                return Err(Error::new(ErrorKind::ConfigInvalid, "endpoint is invalid")
126                    .with_context("endpoint", endpoint)
127                    .set_source(e));
128            }
129            Ok(uri) => uri,
130        };
131
132        let host = endpoint_uri.host().unwrap_or("127.0.0.1");
133        let port = endpoint_uri.port_u16().unwrap_or(21);
134
135        let endpoint = format!("{host}:{port}");
136
137        let enable_secure = match endpoint_uri.scheme_str() {
138            Some("ftp") => false,
139            // if the user forgot to add a scheme prefix
140            // treat it as using secured scheme
141            Some("ftps") | None => true,
142
143            Some(s) => {
144                return Err(Error::new(
145                    ErrorKind::ConfigInvalid,
146                    "endpoint is unsupported or invalid",
147                )
148                .with_context("endpoint", s));
149            }
150        };
151
152        let root = normalize_root(&self.config.root.unwrap_or_default());
153
154        let user = match &self.config.user {
155            None => "".to_string(),
156            Some(v) => v.clone(),
157        };
158
159        let password = match &self.config.password {
160            None => "".to_string(),
161            Some(v) => v.clone(),
162        };
163
164        let accessor_info = AccessorInfo::default();
165        accessor_info
166            .set_scheme(Scheme::Ftp)
167            .set_root(&root)
168            .set_native_capability(Capability {
169                stat: true,
170                stat_has_content_length: true,
171                stat_has_last_modified: true,
172
173                read: true,
174
175                write: true,
176                write_can_multi: true,
177                write_can_append: true,
178
179                delete: true,
180                create_dir: true,
181
182                list: true,
183                list_has_content_length: true,
184                list_has_last_modified: true,
185
186                shared: true,
187
188                ..Default::default()
189            });
190        let manager = Manager {
191            endpoint: endpoint.clone(),
192            root: root.clone(),
193            user: user.clone(),
194            password: password.clone(),
195            enable_secure,
196        };
197        let core = Arc::new(FtpCore {
198            info: accessor_info.into(),
199            manager,
200            pool: OnceCell::new(),
201        });
202
203        Ok(FtpBackend { core })
204    }
205}
206
207// Backend is used to serve `Accessor` support for ftp.
208#[derive(Clone)]
209pub struct FtpBackend {
210    core: Arc<FtpCore>,
211}
212
213impl Debug for FtpBackend {
214    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
215        f.debug_struct("Backend").finish()
216    }
217}
218
219impl Access for FtpBackend {
220    type Reader = FtpReader;
221    type Writer = FtpWriter;
222    type Lister = FtpLister;
223    type Deleter = oio::OneShotDeleter<FtpDeleter>;
224
225    fn info(&self) -> Arc<AccessorInfo> {
226        self.core.info.clone()
227    }
228
229    async fn create_dir(&self, path: &str, _: OpCreateDir) -> Result<RpCreateDir> {
230        let mut ftp_stream = self.core.ftp_connect(Operation::CreateDir).await?;
231
232        let paths: Vec<&str> = path.split_inclusive('/').collect();
233
234        let mut curr_path = String::new();
235
236        for path in paths {
237            curr_path.push_str(path);
238            match ftp_stream.mkdir(&curr_path).await {
239                // Do nothing if status is FileUnavailable or OK(()) is return.
240                Err(FtpError::UnexpectedResponse(Response {
241                    status: Status::FileUnavailable,
242                    ..
243                }))
244                | Ok(()) => (),
245                Err(e) => {
246                    return Err(parse_error(e));
247                }
248            }
249        }
250
251        Ok(RpCreateDir::default())
252    }
253
254    async fn stat(&self, path: &str, _: OpStat) -> Result<RpStat> {
255        let file = self.ftp_stat(path).await?;
256
257        let mode = if file.is_file() {
258            EntryMode::FILE
259        } else if file.is_directory() {
260            EntryMode::DIR
261        } else {
262            EntryMode::Unknown
263        };
264
265        let mut meta = Metadata::new(mode);
266        meta.set_content_length(file.size() as u64);
267        meta.set_last_modified(file.modified().into());
268
269        Ok(RpStat::new(meta))
270    }
271
272    async fn read(&self, path: &str, args: OpRead) -> Result<(RpRead, Self::Reader)> {
273        let ftp_stream = self.core.ftp_connect(Operation::Read).await?;
274
275        let reader = FtpReader::new(ftp_stream, path.to_string(), args).await?;
276        Ok((RpRead::new(), reader))
277    }
278
279    async fn write(&self, path: &str, op: OpWrite) -> Result<(RpWrite, Self::Writer)> {
280        // Ensure the parent dir exists.
281        let parent = get_parent(path);
282        let paths: Vec<&str> = parent.split('/').collect();
283
284        // TODO: we can optimize this by checking dir existence first.
285        let mut ftp_stream = self.core.ftp_connect(Operation::Write).await?;
286        let mut curr_path = String::new();
287
288        for path in paths {
289            if path.is_empty() {
290                continue;
291            }
292            curr_path.push_str(path);
293            curr_path.push('/');
294            match ftp_stream.mkdir(&curr_path).await {
295                // Do nothing if status is FileUnavailable or OK(()) is return.
296                Err(FtpError::UnexpectedResponse(Response {
297                    status: Status::FileUnavailable,
298                    ..
299                }))
300                | Ok(()) => (),
301                Err(e) => {
302                    return Err(parse_error(e));
303                }
304            }
305        }
306
307        let tmp_path = if op.append() {
308            None
309        } else {
310            let uuid = Uuid::new_v4().to_string();
311            Some(format!("{}.{}", path, uuid))
312        };
313
314        let w = FtpWriter::new(ftp_stream, path.to_string(), tmp_path);
315
316        Ok((RpWrite::new(), w))
317    }
318
319    async fn delete(&self) -> Result<(RpDelete, Self::Deleter)> {
320        Ok((
321            RpDelete::default(),
322            oio::OneShotDeleter::new(FtpDeleter::new(self.core.clone())),
323        ))
324    }
325
326    async fn list(&self, path: &str, _: OpList) -> Result<(RpList, Self::Lister)> {
327        let mut ftp_stream = self.core.ftp_connect(Operation::List).await?;
328
329        let pathname = if path == "/" { None } else { Some(path) };
330        let files = ftp_stream.list(pathname).await.map_err(parse_error)?;
331
332        Ok((
333            RpList::default(),
334            FtpLister::new(if path == "/" { "" } else { path }, files),
335        ))
336    }
337}
338
339impl FtpBackend {
340    pub async fn ftp_stat(&self, path: &str) -> Result<File> {
341        let mut ftp_stream = self.core.ftp_connect(Operation::Stat).await?;
342
343        let (parent, basename) = (get_parent(path), get_basename(path));
344
345        let pathname = if parent == "/" { None } else { Some(parent) };
346
347        let resp = ftp_stream.list(pathname).await.map_err(parse_error)?;
348
349        // Get stat of file.
350        let mut files = resp
351            .into_iter()
352            .filter_map(|file| File::from_str(file.as_str()).ok())
353            .filter(|f| f.name() == basename.trim_end_matches('/'))
354            .collect::<Vec<File>>();
355
356        if files.is_empty() {
357            Err(Error::new(
358                ErrorKind::NotFound,
359                "file is not found during list",
360            ))
361        } else {
362            Ok(files.remove(0))
363        }
364    }
365}
366
367#[cfg(test)]
368mod build_test {
369    use super::FtpBuilder;
370    use crate::*;
371
372    #[test]
373    fn test_build() {
374        // ftps scheme, should suffix with default port 21
375        let b = FtpBuilder::default()
376            .endpoint("ftps://ftp_server.local")
377            .build();
378        assert!(b.is_ok());
379
380        // ftp scheme
381        let b = FtpBuilder::default()
382            .endpoint("ftp://ftp_server.local:1234")
383            .build();
384        assert!(b.is_ok());
385
386        // no scheme
387        let b = FtpBuilder::default()
388            .endpoint("ftp_server.local:8765")
389            .build();
390        assert!(b.is_ok());
391
392        // invalid scheme
393        let b = FtpBuilder::default()
394            .endpoint("invalidscheme://ftp_server.local:8765")
395            .build();
396        assert!(b.is_err());
397        let e = b.unwrap_err();
398        assert_eq!(e.kind(), ErrorKind::ConfigInvalid);
399    }
400}