smb 0.11.2

A Pure Rust SMB Client implementation
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
use super::file_util::*;
use super::*;
#[cfg(not(feature = "async"))]
use std::io::prelude::*;
use std::ops::{Deref, DerefMut};

/// An opened file on the server.
///
/// # [std::io] Support
/// The [File] struct also supports the [Read][std::io::Read] and [Write][std::io::Write] traits.
/// Note that both of these traits are blocking, and will block the current thread until the operation is complete.
/// Use [File::read_block] and [File::write_block] for non-blocking operations.
/// The [File] struct also implements the [Seek][std::io::Seek] trait.
/// This allows you to seek to a specific position in the file, combined with the [Read][std::io::Read] and [Write][std::io::Write] traits.
/// Using any of the implemented [std::io] traits mentioned above should have no effect on calling the other, non-blocking methods.
/// Since we would NOT like to call a tokio task from a blocking context, these traits are **NOT** implemented in the async context!
///
/// You may not directly create this struct. Instead, use the [Tree::create][crate::tree::Tree::create] method to gain
/// a proper handle against the server in the shape of a [Resource], that can be then converted to a [File].
pub struct File {
    handle: ResourceHandle,

    #[cfg(not(feature = "async"))]
    pos: u64,
    #[cfg(not(feature = "async"))]
    dirty: bool,

    end_of_file: u64,
}

#[maybe_async(AFIT)]
impl File {
    pub fn new(handle: ResourceHandle, end_of_file: u64) -> Self {
        File {
            handle,
            end_of_file,
            #[cfg(not(feature = "async"))]
            pos: 0,
            #[cfg(not(feature = "async"))]
            dirty: false,
        }
    }

    /// Returns the access mask of the file,
    /// when the file was opened.
    pub fn access(&self) -> FileAccessMask {
        self.access
    }

    /// Read a block of data from an opened file.
    /// # Arguments
    /// * `buf` - The buffer to read the data into. A maximum of `buf.len()` bytes will be read.
    /// * `pos` - The offset in the file to read from.
    /// * `unbuffered` - Whether to try using unbuffered I/O (if supported by the server).
    /// # Returns
    /// The number of bytes read, up to `buf.len()`.
    pub async fn read_block(
        &self,
        buf: &mut [u8],
        pos: u64,
        channel: Option<u32>,
        unbuffered: bool,
    ) -> std::io::Result<usize> {
        if buf.is_empty() {
            return Ok(0);
        }

        if !self.access.file_read_data() {
            return Err(std::io::Error::new(
                std::io::ErrorKind::PermissionDenied,
                "No read permission",
            ));
        }

        // EOF
        if pos >= self.end_of_file {
            return Ok(0);
        }

        log::debug!(
            "Reading up to {} bytes at offset {} from {}",
            buf.len(),
            pos,
            self.handle.name()
        );

        let mut flags = ReadFlags::new();
        if self.handle.conn_info.config.compression_enabled
            && self.handle.conn_info.dialect.supports_compression()
        {
            flags.set_read_compressed(true);
        }

        if unbuffered && self.handle.conn_info.negotiation.dialect_rev >= Dialect::Smb0302 {
            flags.set_read_unbuffered(true);
        }

        let request = OutgoingMessage::new(
            ReadRequest {
                flags,
                length: buf.len() as u32,
                offset: pos,
                file_id: self.handle.file_id().map_err(std::io::Error::other)?,
                minimum_count: 1,
            }
            .into(),
        )
        .with_channel_id(channel);

        let response = self
            .handle
            .sendo_recvo(request, ReceiveOptions::new().with_allow_async(true))
            .await
            .map_err(|e| std::io::Error::other(e.to_string()))?;
        let content = response
            .message
            .content
            .to_read()
            .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))?;
        let actual_read_length = content.buffer.len();
        log::debug!(
            "Read {} bytes from {}.",
            actual_read_length,
            self.handle.name()
        );

        buf[..actual_read_length].copy_from_slice(&content.buffer);

        Ok(actual_read_length)
    }

    /// Write a block of data to an opened file.
    /// # Arguments
    /// * `buf` - The data to write.
    /// * `pos` - The offset in the file to write to.
    /// # Returns
    /// The number of bytes written.
    /// # Note
    /// this method copies the data from `buf` into an internal buffer,
    /// which is then sent to the server.
    /// If you want to avoid this copy, use [`File::write_block_zc`] instead.
    #[maybe_async]
    #[inline]
    pub async fn write_block(
        &self,
        buf: &[u8],
        pos: u64,
        channel: Option<u32>,
    ) -> std::io::Result<usize> {
        self.write_block_zc(buf.into(), pos, channel).await
    }

    /// Write a block of data to an opened file, without copying the data.
    /// # Arguments
    /// * `buf` - The data to write.
    /// * `pos` - The offset in the file to write to.
    /// # Returns
    /// The number of bytes written.
    pub async fn write_block_zc(
        &self,
        buf: Arc<[u8]>,
        pos: u64,
        channel: Option<u32>,
    ) -> std::io::Result<usize> {
        if buf.is_empty() {
            return Ok(0);
        }

        if !self.access.file_write_data() {
            return Err(std::io::Error::new(
                std::io::ErrorKind::PermissionDenied,
                "No write permission",
            ));
        }

        log::debug!(
            "Writing {} bytes at offset {} to {}",
            buf.len(),
            pos,
            self.handle.name()
        );

        // Arc is accepted to provide safety regarding the buffer's lifetime,
        // without forcing an actual copy of the data.
        let outgoing = OutgoingMessage::new(
            WriteRequest::new(
                pos,
                self.handle.file_id().map_err(std::io::Error::other)?,
                WriteFlags::new(),
                buf.len() as u32,
            )
            .into(),
        )
        .with_additional_data(Arc::clone(&buf))
        .with_channel_id(channel);

        let response = self
            .handle
            .sendo_recvo(outgoing, ReceiveOptions::new().with_allow_async(true))
            .await
            .map_err(|e| std::io::Error::other(e.to_string()))?;

        let content = response
            .message
            .content
            .to_write()
            .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))?;
        let actual_written_length = content.count as usize;
        log::debug!(
            "Wrote {} bytes to {}.",
            actual_written_length,
            self.handle.name()
        );
        Ok(actual_written_length)
    }

    /// Sends a flush request to the server to flush the file.
    pub async fn flush(&self) -> std::io::Result<()> {
        let _response = self
            .handle
            .send_recvo(
                FlushRequest {
                    file_id: self.handle.file_id().map_err(std::io::Error::other)?,
                }
                .into(),
                ReceiveOptions::new().with_allow_async(true),
            )
            .await
            .map_err(|e| std::io::Error::other(e.to_string()))?;

        log::debug!("Flushed {}.", self.handle.name());
        Ok(())
    }

    /// Performs a server-side copy from another file on the same server.
    /// # Arguments
    /// * `from` - The file to copy from.
    /// # Notes
    /// * This copy must be performed against a file from the same share (tree) as this file.
    pub async fn srv_copy(&self, from: &File) -> crate::Result<()> {
        if !self.access.file_write_data() {
            return Err(Error::InvalidState(
                "No write permission on destination file".to_string(),
            ));
        }
        if !from.access.file_read_data() {
            return Err(Error::InvalidState(
                "No read permission on source file".to_string(),
            ));
        }

        // Even if we weren't testing it properly, the remote would have returned
        // [Status::ObjectNameNotFound] error for unmatching trees.
        if !self.same_tree(from) {
            return Err(Error::InvalidArgument(
                "Source and destination files must be opened from the same share (tree)"
                    .to_string(),
            ));
        }

        let other_end_of_file = from.get_len().await?;
        self.set_len(other_end_of_file).await?;

        let resume_key_response = from.fsctl(SrvRequestResumeKeyRequest(())).await?;
        let resume_key = resume_key_response.resume_key;

        let chunks = (0..other_end_of_file)
            .step_by(CHUNK_SIZE)
            .map(|start| {
                let len_left = other_end_of_file - start;
                SrvCopychunkItem {
                    source_offset: start,
                    target_offset: start,
                    length: std::cmp::min(CHUNK_SIZE as u32, len_left as u32),
                }
            })
            .collect::<Vec<_>>();

        const CHUNK_SIZE: usize = 1024 * 1024; // 1 MB
        let req = SrvCopychunkCopy {
            source_key: resume_key,
            chunks,
        };
        let copy_response = self.fsctl(req).await?;
        if copy_response.total_bytes_written as u64 != other_end_of_file {
            return Err(Error::InvalidArgument(format!(
                "Expected to write {} bytes, but wrote {} bytes",
                other_end_of_file, copy_response.total_bytes_written
            )));
        }
        Ok(())
    }
}

// Despite being available, seeking means nothing here,
// since it may only be used when calling read/write from the std::io traits.
#[cfg(not(feature = "async"))]
impl Seek for File {
    fn seek(&mut self, pos: std::io::SeekFrom) -> std::io::Result<u64> {
        let next_pos = match pos {
            std::io::SeekFrom::Start(pos) => pos,
            std::io::SeekFrom::End(pos) => {
                let pos = self.end_of_file as i64 + pos;
                if pos < 0 {
                    return Err(std::io::Error::new(
                        std::io::ErrorKind::InvalidInput,
                        "Invalid seek position",
                    ));
                }
                pos.try_into().map_err(|_| {
                    std::io::Error::new(std::io::ErrorKind::InvalidInput, "Invalid seek position")
                })?
            }
            std::io::SeekFrom::Current(pos) => {
                let pos = self.pos as i64 + pos;
                if pos < 0 {
                    return Err(std::io::Error::new(
                        std::io::ErrorKind::InvalidInput,
                        "Invalid seek position",
                    ));
                }
                pos.try_into().map_err(|_| {
                    std::io::Error::new(std::io::ErrorKind::InvalidInput, "Invalid seek position")
                })?
            }
        };
        if next_pos > self.end_of_file {
            return Err(std::io::Error::new(
                std::io::ErrorKind::InvalidInput,
                "Invalid seek position",
            ));
        }
        Ok(self.pos)
    }
}

#[cfg(not(feature = "async"))]
impl Read for File {
    fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
        let read_length = File::read_block(self, buf, self.pos, None, false)
            .map_err(|e| std::io::Error::other(e.to_string()))?;
        self.pos += read_length as u64;
        Ok(read_length)
    }
}

#[cfg(not(feature = "async"))]
impl Write for File {
    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
        let written_length = File::write_block(self, buf, self.pos, None)?;
        self.pos += written_length as u64;
        self.dirty = true;
        Ok(written_length)
    }

    fn flush(&mut self) -> std::io::Result<()> {
        if !self.dirty {
            return Ok(());
        }
        File::flush(self)
    }
}

impl ReadAtChannel for File {
    #[maybe_async]
    async fn read_at_channel(
        &self,
        buf: &mut [u8],
        offset: u64,
        channel: Option<u32>,
    ) -> crate::Result<usize> {
        self.read_block(buf, offset, channel, false)
            .await
            .map_err(crate::Error::IoError)
    }
}

impl WriteAtChannel for File {
    #[maybe_async]
    async fn write_at_channel(
        &self,
        buf: &[u8],
        offset: u64,
        channel: Option<u32>,
    ) -> crate::Result<usize> {
        self.write_block(buf, offset, channel)
            .await
            .map_err(crate::Error::IoError)
    }
}

impl GetLen for File {
    #[maybe_async]
    async fn get_len(&self) -> crate::Result<u64> {
        Ok(self.end_of_file)
    }
}

impl SetLen for File {
    #[maybe_async]
    async fn set_len(&self, len: u64) -> crate::Result<()> {
        self.set_info(FileEndOfFileInformation { end_of_file: len })
            .await
    }
}

impl Deref for File {
    type Target = ResourceHandle;

    fn deref(&self) -> &Self::Target {
        &self.handle
    }
}

impl DerefMut for File {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.handle
    }
}