rusftp 0.2.1

SFTP library based on russh
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
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
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
// This file is part of the rusftp project
//
// Copyright (C) ANEO, 2024-2024. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License")
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use bytes::Bytes;
use futures::Future;

use crate::client::{Dir, Error, File, SftpClient, SftpFuture, SftpReply, SftpRequest, StatusCode};
use crate::message::{
    Attrs, Close, Data, Extended, ExtendedReply, FSetStat, FStat, Handle, LStat, Message, MkDir,
    Name, Open, OpenDir, PFlags, Path, Read, ReadDir, ReadLink, RealPath, Remove, Rename, RmDir,
    SetStat, Stat, Status, Symlink, Write,
};
use crate::utils::IntoBytes;

impl SftpClient {
    /// Close an opened file or directory.
    ///
    /// Equivalent to:
    ///
    /// ```ignore
    /// async fn close(&self, handle: Handle) -> Result<(), Error>;
    /// ```
    ///
    /// # Arguments
    ///
    /// * `handle` - Handle of the file or the directory
    ///
    /// # Cancel safety
    ///
    /// It is safe to cancel the future.
    /// However, the request is actually sent before the future is returned.
    pub fn close(&self, handle: Handle) -> SftpFuture {
        self.request(Close { handle })
    }

    /// Send an extended request.
    ///
    /// Equivalent to:
    ///
    /// ```ignore
    /// async fn extended(&self, request: impl Into<Bytes>, data: impl Into<Bytes>) -> Result<Bytes, Error>;
    /// ```
    ///
    /// # Arguments
    ///
    /// * `request` - Extended-request name (format: `name@domain`)
    /// * `data` - Specific data needed by the extension to intrepret the request
    ///
    /// # Cancel safety
    ///
    /// It is safe to cancel the future.
    /// However, the request is actually sent before the future is returned.
    pub fn extended(&self, request: impl IntoBytes, data: impl IntoBytes) -> SftpFuture<Bytes> {
        self.request_with(
            Extended {
                request: request.into_bytes(),
                data: data.into_bytes(),
            }
            .to_request_message(),
            (),
            |_, msg| Ok(ExtendedReply::from_reply_message(msg)?.data),
        )
    }

    /// Change the attributes (metadata) of an open file or directory.
    ///
    /// Equivalent to:
    ///
    /// ```ignore
    /// async fn fsetstat(&self, handle: Handle, attrs: Attrs) -> Result<(), Error>;
    /// ```
    ///
    /// This operation is used for operations such as changing the ownership,
    /// permissions or access times, as well as for truncating a file.
    ///
    /// An error will be returned if the specified file system object does not exist
    /// or the user does not have sufficient rights to modify the specified attributes.
    ///
    /// # Arguments
    ///
    /// * `handle` - Handle of the file or directory to change the attributes
    /// * `attrs` - New attributes to apply
    ///
    /// # Cancel safety
    ///
    /// It is safe to cancel the future.
    /// However, the request is actually sent before the future is returned.
    pub fn fsetstat(&self, handle: Handle, attrs: Attrs) -> SftpFuture {
        self.request(FSetStat { handle, attrs })
    }

    /// Read the attributes (metadata) of an open file.
    ///
    /// Equivalent to:
    ///
    /// ```ignore
    /// async fn fstat(&self, handle: Handle) -> Result<Attrs, Error>;
    /// ```
    ///
    /// # Arguments
    ///
    /// * `handle` - Handle of the open file
    ///
    /// # Cancel safety
    ///
    /// It is safe to cancel the future.
    /// However, the request is actually sent before the future is returned.
    pub fn fstat(&self, handle: Handle) -> SftpFuture<Attrs> {
        self.request(FStat { handle })
    }

    /// Read the attributes (metadata) of a file or directory.
    ///
    /// Equivalent to:
    ///
    /// ```ignore
    /// async fn lstat(&self, path: impl Into<Path>) -> Result<Attrs, Error>;
    /// ```
    ///
    /// Symbolic links are followed.
    ///
    /// # Arguments
    ///
    /// * `path` - Path of the file, directory, or symbolic link
    ///
    /// # Cancel safety
    ///
    /// It is safe to cancel the future.
    /// However, the request is actually sent before the future is returned.
    pub fn lstat(&self, path: impl Into<Path>) -> SftpFuture<Attrs> {
        self.request(LStat { path: path.into() })
    }

    /// Create a new directory.
    ///
    /// Equivalent to:
    ///
    /// ```ignore
    /// async fn mkdir(&self, path: impl Into<Path>) -> Result<(), Error>;
    /// ```
    ///
    /// An error will be returned if a file or directory with the specified path already exists.
    ///
    /// # Arguments
    ///
    /// * `path` - Path where the new directory will be located
    ///
    /// # Cancel safety
    ///
    /// It is safe to cancel the future.
    /// However, the request is actually sent before the future is returned.
    pub fn mkdir(&self, path: impl Into<Path>) -> SftpFuture {
        self.mkdir_with_attrs(path, Attrs::default())
    }

    /// Create a new directory.
    ///
    /// Equivalent to:
    ///
    /// ```ignore
    /// async fn mkdir_with_attrs(&self, path: impl Into<Path>, attrs: Attrs) -> Result<(), Error>;
    /// ```
    ///
    /// An error will be returned if a file or directory with the specified path already exists.
    ///
    /// # Arguments
    ///
    /// * `path` - Path where the new directory will be located
    /// * `attrs` - Default attributes to apply to the newly created directory
    ///
    /// # Cancel safety
    ///
    /// It is safe to cancel the future.
    /// However, the request is actually sent before the future is returned.
    pub fn mkdir_with_attrs(&self, path: impl Into<Path>, attrs: Attrs) -> SftpFuture {
        self.request(MkDir {
            path: path.into(),
            attrs,
        })
    }

    /// Open a file for reading or writing.
    ///
    /// Equivalent to:
    ///
    /// ```ignore
    /// async fn open_handle(&self, filename: impl Into<Path>, pflags: PFlags, attrs: Attrs) -> Result<Handle, Error>;
    /// ```
    ///
    /// Returns an [`Handle`] for the file specified.
    ///
    /// # Arguments
    ///
    /// * `path` - Path of the file to open
    /// * `pflags` - Flags for the file opening
    /// * `attrs` - Default file attributes to use upon file creation
    ///
    /// # Cancel safety
    ///
    /// It is safe to cancel the future.
    /// However, the request is actually sent before the future is returned.
    pub fn open_handle(
        &self,
        filename: impl Into<Path>,
        pflags: PFlags,
        attrs: Attrs,
    ) -> SftpFuture<Handle> {
        self.request(Open {
            filename: filename.into(),
            pflags,
            attrs,
        })
    }

    /// Open a file for reading or writing.
    ///
    /// Equivalent to:
    ///
    /// ```ignore
    /// async fn open_with_flags_attrs(&self, filename: impl Into<Path>, pflags: PFlags, attrs: Attrs) -> Result<File, Error>;
    /// ```
    ///
    /// Returns a [`File`] object that is compatible with [`tokio::io`].
    ///
    /// # Arguments
    ///
    /// * `path` - Path of the file to open
    /// * `pflags` - Flags for the file opening
    /// * `attrs` - Default file attributes to use upon file creation
    ///
    /// # Cancel safety
    ///
    /// It is safe to cancel the future.
    /// However, the request is actually sent before the future is returned.
    pub fn open_with_flags_attrs(
        &self,
        filename: impl Into<Path>,
        pflags: PFlags,
        attrs: Attrs,
    ) -> SftpFuture<File, SftpClient> {
        self.request_with(
            Open {
                filename: filename.into(),
                pflags,
                attrs,
            }
            .to_request_message(),
            self.clone(),
            |client, msg| Ok(File::new(client, Handle::from_reply_message(msg)?)),
        )
    }

    /// Open a file for reading or writing.
    ///
    /// Equivalent to:
    ///
    /// ```ignore
    /// async fn open_with_flags(&self, filename: impl Into<Path>, pflags: PFlags) -> Result<File, Error>;
    /// ```
    ///
    /// Returns a [`File`] object that is compatible with [`tokio::io`].
    ///
    /// # Arguments
    ///
    /// * `path` - Path of the file to open
    /// * `pflags` - Flags for the file opening
    ///
    /// # Cancel safety
    ///
    /// It is safe to cancel the future.
    /// However, the request is actually sent before the future is returned.
    pub fn open_with_flags(
        &self,
        filename: impl Into<Path>,
        pflags: PFlags,
    ) -> SftpFuture<File, SftpClient> {
        self.open_with_flags_attrs(filename, pflags, Attrs::default())
    }

    /// Open a file for reading or writing.
    ///
    /// Equivalent to:
    ///
    /// ```ignore
    /// async fn open_with_attrs(&self, filename: impl Into<Path>, attrs: Attrs) -> Result<File, Error>;
    /// ```
    ///
    /// Returns a [`File`] object that is compatible with [`tokio::io`].
    ///
    /// # Arguments
    ///
    /// * `path` - Path of the file to open
    /// * `attrs` - Default file attributes to use upon file creation
    ///
    /// # Cancel safety
    ///
    /// It is safe to cancel the future.
    /// However, the request is actually sent before the future is returned.
    pub fn open_with_attrs(
        &self,
        filename: impl Into<Path>,
        attrs: Attrs,
    ) -> SftpFuture<File, SftpClient> {
        self.open_with_flags_attrs(filename, PFlags::default(), attrs)
    }

    /// Open a file for reading or writing.
    ///
    /// Equivalent to:
    ///
    /// ```ignore
    /// async fn open(&self, filename: impl Into<Path>) -> Result<File, Error>;
    /// ```
    ///
    /// Returns a [`File`] object that is compatible with [`tokio::io`].
    ///
    /// # Arguments
    ///
    /// * `path` - Path of the file to open
    ///
    /// # Cancel safety
    ///
    /// It is safe to cancel the future.
    /// However, the request is actually sent before the future is returned.
    pub fn open(&self, filename: impl Into<Path>) -> SftpFuture<File, SftpClient> {
        self.open_with_flags_attrs(filename, PFlags::default(), Attrs::default())
    }

    /// Open a directory for listing.
    ///
    /// Equivalent to:
    ///
    /// ```ignore
    /// async fn opendir_handle(&self, path: impl Into<Path>) -> Result<Handle, Error>;
    /// ```
    ///
    /// Once the directory has been successfully opened, files (and directories)
    /// contained in it can be listed using `readdir_handle`.
    ///
    /// Returns an [`Handle`] for the directory specified.
    ///
    /// # Arguments
    ///
    /// * `path` - Path of the directory to open
    ///
    /// # Cancel safety
    ///
    /// It is safe to cancel the future.
    /// However, the request is actually sent before the future is returned.
    pub fn opendir_handle(&self, path: impl Into<Path>) -> SftpFuture<Handle> {
        self.request(OpenDir { path: path.into() })
    }

    /// Open a directory for listing.
    ///
    /// Equivalent to:
    ///
    /// ```ignore
    /// async fn opendir(&self, path: impl Into<Path>) -> Result<Dir, Error>;
    /// ```
    ///
    /// Returns a [`Dir`] for the directory specified.
    /// It implements [`Stream<Item = Result<NameEntry, ...>>`](futures::stream::Stream).
    ///
    /// # Arguments
    ///
    /// * `path` - Path of the directory to open
    ///
    /// # Cancel safety
    ///
    /// It is safe to cancel the future.
    /// However, the request is actually sent before the future is returned.
    pub fn opendir(&self, path: impl Into<Path>) -> SftpFuture<Dir, SftpClient> {
        self.request_with(
            OpenDir { path: path.into() }.to_request_message(),
            self.clone(),
            |client, msg| Ok(Dir::new(client, Handle::from_reply_message(msg)?)),
        )
    }

    /// Read a portion of an opened file.
    ///
    /// Equivalent to:
    ///
    /// ```ignore
    /// async fn read(&self, handle: Handle, offset: u64, length: u32) -> Result<Bytes, Error>;
    /// ```
    ///
    /// # Arguments
    ///
    /// * `handle`: Handle of the file to read from
    /// * `offset`: Byte offset where the read should start
    /// * `length`: Number of bytes to read
    ///
    /// # Cancel safety
    ///
    /// It is safe to cancel the future.
    /// However, the request is actually sent before the future is returned.
    pub fn read(&self, handle: Handle, offset: u64, length: u32) -> SftpFuture<Bytes> {
        self.request_with(
            Read {
                handle,
                offset,
                length,
            }
            .to_request_message(),
            (),
            |_, msg| Ok(Handle::from_reply_message(msg)?.0),
        )
    }

    /// Read a directory listing.
    ///
    /// Equivalent to:
    ///
    /// ```ignore
    /// async fn readdir_handle(&self, handle: Handle) -> Result<Name, Error>;
    /// ```
    ///
    /// Each `readdir_handle` returns one or more file names with full file attributes for each file.
    /// The client should call `readdir_handle` repeatedly until it has found the file it is looking for
    /// or until the server responds with a [`Status`] message indicating an error
    /// (normally `EOF` if there are no more files in the directory).
    /// The client should then close the handle using `close`.
    ///
    /// # Arguments
    ///
    /// * `handle`: Handle of the open directory
    ///
    /// # Cancel safety
    ///
    /// It is safe to cancel the future.
    /// However, the request is actually sent before the future is returned.
    pub fn readdir_handle(&self, handle: Handle) -> SftpFuture<Name> {
        self.request(ReadDir { handle })
    }

    /// Read a directory listing.
    ///
    /// Equivalent to:
    ///
    /// ```ignore
    /// async fn readdir(&self, path: impl Into<Path>) -> Result<Name, Error>;
    /// ```
    ///
    /// If you need an asynchronous [`Stream`](futures::stream::Stream), you can use `opendir()` instead
    ///
    /// # Arguments
    ///
    /// * `path`: Path of the directory to list
    ///
    /// # Cancel safety
    ///
    /// It is safe to cancel the future.
    /// However, the request is actually sent before the future is returned.
    pub fn readdir(
        &self,
        path: impl Into<Path>,
    ) -> impl Future<Output = Result<Name, Error>> + Send + Sync + 'static {
        let dir = self.request(OpenDir { path: path.into() });
        let client = self.clone();
        let mut entries = Name::default();

        async move {
            let handle = dir.await?;

            loop {
                match client.readdir_handle(handle.clone()).await {
                    Ok(mut chunk) => entries.0.append(&mut chunk.0),
                    Err(Error::Sftp(Status {
                        code: StatusCode::Eof,
                        ..
                    })) => break,
                    Err(err) => {
                        _ = client.close(handle).await;
                        return Err(err);
                    }
                }
            }

            client.close(handle).await?;
            Ok(entries)
        }
    }

    /// Read the target of a symbolic link.
    ///
    /// Equivalent to:
    ///
    /// ```ignore
    /// async fn readlink(&self, path: impl Into<Path>) -> Result<Path, Error>;
    /// ```
    ///
    /// # Arguments
    ///
    /// * `path`: Path of the symbolic link to read
    ///
    /// # Cancel safety
    ///
    /// It is safe to cancel the future.
    /// However, the request is actually sent before the future is returned.
    pub fn readlink(&self, path: impl Into<Path>) -> SftpFuture<Path> {
        self.request_with(
            ReadLink { path: path.into() }.to_request_message(),
            (),
            extract_path_from_name_message,
        )
    }

    /// Canonicalize a path.
    ///
    /// Equivalent to:
    ///
    /// ```ignore
    /// async fn realpath(&self, path: impl Into<Path>) -> Result<Path, Error>;
    /// ```
    ///
    /// # Arguments
    ///
    /// * `path`: Path to canonicalize
    ///
    /// # Cancel safety
    ///
    /// It is safe to cancel the future.
    /// However, the request is actually sent before the future is returned.
    pub fn realpath(&self, path: impl Into<Path>) -> SftpFuture<Path> {
        self.request_with(
            RealPath { path: path.into() }.to_request_message(),
            (),
            extract_path_from_name_message,
        )
    }

    /// Remove a file.
    ///
    /// Equivalent to:
    ///
    /// ```ignore
    /// async fn remove(&self, path: impl Into<Path>) -> Result<(), Error>;
    /// ```
    ///
    /// # Arguments
    ///
    /// * `path`: Path of the file to remove
    ///
    /// # Cancel safety
    ///
    /// It is safe to cancel the future.
    /// However, the request is actually sent before the future is returned.
    pub fn remove(&self, path: impl Into<Path>) -> SftpFuture {
        self.request(Remove { path: path.into() })
    }

    /// Rename/move a file or a directory.
    ///
    /// Equivalent to:
    ///
    /// ```ignore
    /// async fn rename(&self, old_path: impl Into<Path>, new_path: impl Into<Path>) -> Result<(), Error>;
    /// ```
    ///
    /// # Arguments
    ///
    /// * `old_path`: Current path of the file or directory to rename/move
    /// * `new_path`: New path where the file or directory will be moved to
    ///
    /// # Cancel safety
    ///
    /// It is safe to cancel the future.
    /// However, the request is actually sent before the future is returned.
    pub fn rename(&self, old_path: impl Into<Path>, new_path: impl Into<Path>) -> SftpFuture {
        self.request(Rename {
            old_path: old_path.into(),
            new_path: new_path.into(),
        })
    }

    /// Remove an existing directory.
    ///
    /// Equivalent to:
    ///
    /// ```ignore
    /// async fn rmdir(&self, path: impl Into<Path>) -> Result<(), Error>;
    /// ```
    ///
    /// An error will be returned if no directory with the specified path exists,
    /// or if the specified directory is not empty, or if the path specified
    /// a file system object other than a directory.
    ///
    /// # Arguments
    ///
    /// * `path`: Path of the directory to remove
    ///
    /// # Cancel safety
    ///
    /// It is safe to cancel the future.
    /// However, the request is actually sent before the future is returned.
    pub fn rmdir(&self, path: impl Into<Path>) -> SftpFuture {
        self.request(RmDir { path: path.into() })
    }

    /// Change the attributes (metadata) of a file or directory.
    ///
    /// Equivalent to:
    ///
    /// ```ignore
    /// async fn setstat(&self, path: impl Into<Path>, attrs: Attrs) -> Result<(), Error>;
    /// ```
    ///
    /// This request is used for operations such as changing the ownership,
    /// permissions or access times, as well as for truncating a file.
    ///
    /// An error will be returned if the specified file system object does not exist
    /// or the user does not have sufficient rights to modify the specified attributes.
    ///
    /// # Arguments
    ///
    /// * `path`: Path of the file or directory to change the attributes
    /// * `attrs`: New attributes to apply
    ///
    /// # Cancel safety
    ///
    /// It is safe to cancel the future.
    /// However, the request is actually sent before the future is returned.
    pub fn setstat(&self, path: impl Into<Path>, attrs: Attrs) -> SftpFuture {
        self.request(SetStat {
            path: path.into(),
            attrs,
        })
    }

    /// Read the attributes (metadata) of a file or directory.
    ///
    /// Equivalent to:
    ///
    /// ```ignore
    /// async fn stat(&self, path: impl Into<Path>) -> Result<Attrs, Error>;
    /// ```
    ///
    /// Symbolic links *are not* followed.
    ///
    /// # Arguments
    ///
    /// * `path`: Path of the file or directory
    ///
    /// # Cancel safety
    ///
    /// It is safe to cancel the future.
    /// However, the request is actually sent before the future is returned.
    pub fn stat(&self, path: impl Into<Path>) -> SftpFuture<Attrs> {
        self.request(Stat { path: path.into() })
    }

    /// Create a symbolic link.
    ///
    /// Equivalent to:
    ///
    /// ```ignore
    /// async fn symlink(&self, link_path: impl Into<Path>, target_path: impl Into<Path>) -> Result<(), Error>;
    /// ```
    ///
    /// # Arguments
    ///
    /// * `link_path`: Path name of the symbolic link to be created
    /// * `target_path`: Target of the symbolic link
    ///
    /// # Cancel safety
    ///
    /// It is safe to cancel the future.
    /// However, the request is actually sent before the future is returned.
    pub fn symlink(&self, link_path: impl Into<Path>, target_path: impl Into<Path>) -> SftpFuture {
        self.request(Symlink {
            link_path: link_path.into(),
            target_path: target_path.into(),
        })
    }

    /// Write to a portion of an opened file.
    ///
    /// Equivalent to:
    ///
    /// ```ignore
    /// async fn write(&self, handle: Handle, offset: u64, data: impl Into<Data>,) -> Result<(), Error>;
    /// ```
    ///
    /// # Arguments
    ///
    /// * `handle`: Handle of the file to write to
    /// * `offset`: Byte offset where the write should start
    /// * `data`: Bytes to be written to the file
    ///
    /// # Cancel safety
    ///
    /// It is safe to cancel the future.
    /// However, the request is actually sent before the future is returned.
    pub fn write(&self, handle: Handle, offset: u64, data: impl Into<Data>) -> SftpFuture {
        self.request(Write {
            handle,
            offset,
            data: data.into(),
        })
    }
}

/// Convert a SFTP message into [`Name`], and extract its only entry.
/// It fails if the message is not a [`Name`], or if it has not exactly one entry.
fn extract_path_from_name_message(_: (), msg: Message) -> Result<Path, Error> {
    match Name::from_reply_message(msg)?.as_mut() {
        [] => Err(Error::Sftp(StatusCode::BadMessage.to_status("No entry"))),
        [entry] => Ok(std::mem::take(entry).filename),
        _ => Err(Error::Sftp(
            StatusCode::BadMessage.to_status("Multiple entries"),
        )),
    }
}