smb-server 0.12.0

SMB2/3 file-sharing server library with pluggable storage backends.
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
//! Whole reads and writes on the handles of the inner backend, which may
//! move fewer bytes than asked for.

use std::io;

use super::format::{BlockShift, HEADER_LEN};
use super::names::MAX_SIDE_OBJECT_LEN;
use crate::backend::{Handle, OpenAction, OpenIntent, OpenOptions, ShareBackend};
use crate::error::{SmbError, SmbResult};
use crate::path::SmbPath;

/// Most bytes one inner read or write of stored blocks moves, before it is
/// cut down to whole blocks.
const MAX_CALL: u64 = 4 << 20;

/// Closes a handle that was only read from. A close that fails changes
/// nothing there, so its error is dropped.
pub(super) async fn close_after_reading(handle: Box<dyn Handle>) {
    let _ = handle.close().await;
}

pub(super) fn is_missing(error: &SmbError) -> bool {
    matches!(
        error,
        SmbError::NotFound | SmbError::PathNotFound | SmbError::NotADirectory
    )
}

/// The content of the side object at `path`. `None` when it is missing, a
/// directory, or longer than `MAX_SIDE_OBJECT_LEN`, which is then not read.
pub(super) async fn read_side_object<B: ShareBackend>(
    inner: &B,
    path: &SmbPath,
) -> SmbResult<Option<Vec<u8>>> {
    let opts = OpenOptions {
        non_directory: true,
        ..OpenOptions::default()
    };
    let handle = match inner.open(path, opts).await {
        Ok((handle, _)) => handle,
        Err(error) if is_missing(&error) || matches!(error, SmbError::IsDirectory) => {
            return Ok(None);
        }
        Err(error) => return Err(error),
    };
    let content = side_object_content(handle.as_ref()).await;
    close_after_reading(handle).await;
    content
}

async fn side_object_content(handle: &dyn Handle) -> SmbResult<Option<Vec<u8>>> {
    let info = handle.stat().await?;
    if info.is_directory || info.end_of_file > MAX_SIDE_OBJECT_LEN as u64 {
        return Ok(None);
    }
    let len = usize::try_from(info.end_of_file).expect("a side object is at most 1024 bytes");
    read_full(handle, 0, len).await.map(Some)
}

/// Makes the side object at `path` hold `content`. A new object gets the
/// content. An object that is there already is read and left alone when it
/// holds the same bytes, and rewritten otherwise.
pub(super) async fn write_side_object<B: ShareBackend>(
    inner: &B,
    path: &SmbPath,
    content: &[u8],
) -> SmbResult<()> {
    let opts = OpenOptions {
        write: true,
        non_directory: true,
        intent: OpenIntent::OpenOrCreate,
        ..OpenOptions::default()
    };
    let (handle, action) = inner.open(path, opts).await?;
    match fill_side_object(handle.as_ref(), action, content).await {
        Ok(true) => handle.close().await,
        Ok(false) => {
            close_after_reading(handle).await;
            Ok(())
        }
        Err(error) => {
            let _ = handle.close().await;
            Err(error)
        }
    }
}

/// Writes `content` to the side object on `handle`. `action` says whether the
/// object is new. Returns false, writing nothing, when the object already
/// held `content`.
async fn fill_side_object(
    handle: &dyn Handle,
    action: OpenAction,
    content: &[u8],
) -> SmbResult<bool> {
    if action != OpenAction::Created {
        let held = handle.stat().await?.end_of_file == content.len() as u64
            && read_full(handle, 0, content.len()).await? == content;
        if held {
            return Ok(false);
        }
        handle.truncate(0).await?;
    }
    write_full(handle, 0, content).await?;
    Ok(true)
}

/// Appends `len` bytes read at `offset` to `out`, asking again for what a
/// short read left. A read that returns nothing before all `len` bytes are
/// in fails with `Io`.
async fn read_append(
    handle: &dyn Handle,
    offset: u64,
    out: &mut Vec<u8>,
    len: usize,
) -> SmbResult<()> {
    let start = out.len();
    while out.len() - start < len {
        let done = out.len() - start;
        let missing = len - done;
        let at = offset
            .checked_add(done as u64)
            .ok_or(SmbError::InvalidParameter)?;
        let got = handle
            .read(at, u32::try_from(missing).unwrap_or(u32::MAX))
            .await?;
        if got.is_empty() {
            return Err(SmbError::Io(io::ErrorKind::UnexpectedEof.into()));
        }
        out.extend_from_slice(&got[..got.len().min(missing)]);
    }
    Ok(())
}

/// Reads exactly `len` bytes at `offset`, asking again for what a short read
/// left. A read that returns nothing before all `len` bytes are in fails with
/// `Io`.
pub(super) async fn read_full(handle: &dyn Handle, offset: u64, len: usize) -> SmbResult<Vec<u8>> {
    let mut out = Vec::with_capacity(len);
    read_append(handle, offset, &mut out, len).await?;
    Ok(out)
}

/// The most stored bytes of one inner call: 4 MiB cut down to whole stored
/// blocks, and at least one block.
fn call_len(shift: BlockShift) -> usize {
    let block = shift.stored_block_size();
    let len = (MAX_CALL / block * block).max(block);
    usize::try_from(len).expect("a call of a few MiB fits in memory")
}

/// Reads `len` stored bytes at `offset`, where a block starts, and the run
/// ends where a block ends. Each inner call asks for whole blocks, at most
/// `call_len`, and so ends on a block boundary. Short reads are continued as
/// in `read_full`.
pub(super) async fn read_blocks(
    handle: &dyn Handle,
    shift: BlockShift,
    offset: u64,
    len: usize,
) -> SmbResult<Vec<u8>> {
    let call = call_len(shift);
    let mut out = Vec::with_capacity(len);
    while out.len() < len {
        let at = offset
            .checked_add(out.len() as u64)
            .ok_or(SmbError::InvalidParameter)?;
        let chunk = (len - out.len()).min(call);
        read_append(handle, at, &mut out, chunk).await?;
    }
    Ok(out)
}

/// Writes all of `data` at `offset`, continuing after a short write. A write
/// that takes nothing fails with `Io`.
pub(super) async fn write_full(handle: &dyn Handle, offset: u64, data: &[u8]) -> SmbResult<()> {
    let mut done = 0;
    while done < data.len() {
        let at = offset
            .checked_add(done as u64)
            .ok_or(SmbError::InvalidParameter)?;
        let taken = handle.write(at, &data[done..]).await?;
        if taken == 0 {
            return Err(SmbError::Io(io::ErrorKind::WriteZero.into()));
        }
        done += usize::try_from(taken)
            .unwrap_or(usize::MAX)
            .min(data.len() - done);
    }
    Ok(())
}

/// Writes all of `data` at `offset`, in inner calls of at most `call_len`
/// bytes. Every call but the last ends where a stored block ends. Short
/// writes are continued as in `write_full`.
pub(super) async fn write_blocks(
    handle: &dyn Handle,
    shift: BlockShift,
    offset: u64,
    data: &[u8],
) -> SmbResult<()> {
    let call = call_len(shift) as u64;
    let block = shift.stored_block_size();
    let header = HEADER_LEN as u64;
    let mut done = 0;
    while done < data.len() {
        let at = offset
            .checked_add(done as u64)
            .ok_or(SmbError::InvalidParameter)?;
        let reach = at.checked_add(call).ok_or(SmbError::InvalidParameter)?;
        // A call is a block or more, so the last boundary within reach is
        // past `at`.
        let boundary = header + (reach - header) / block * block;
        let take = usize::try_from(boundary - at)
            .unwrap_or(usize::MAX)
            .min(data.len() - done);
        write_full(handle, at, &data[done..done + take]).await?;
        done += take;
    }
    Ok(())
}

/// Checks the inner calls `calls`, as offset and length: they follow each
/// other from `from` to `to`, hold at most `MAX_CALL` bytes, and each ends at
/// `to` or where a stored block of `block` bytes ends. `context` names the
/// case in a failure.
#[cfg(test)]
#[track_caller]
pub(super) fn assert_calls(calls: &[(u64, usize)], block: u64, from: u64, to: u64, context: &str) {
    let mut next = from;
    for &(at, count) in calls {
        assert_eq!(at, next, "{context}: calls follow each other");
        next = at + count as u64;
        assert!(
            count as u64 <= MAX_CALL,
            "{context}: {count} bytes in one call"
        );
        assert!(
            next == to || (next - HEADER_LEN as u64).is_multiple_of(block),
            "{context}: a call ends at {next}, inside a block"
        );
    }
    assert_eq!(next, to, "{context}");
}

#[cfg(test)]
mod tests {
    use super::super::testing::injected;
    use super::*;
    use crate::backend::{OpenIntent, ShareBackend};
    use crate::tests::faulty::Faulty;
    use crate::tests::memfs::MemFsBackend;
    use crate::tests::util::{opts, path};

    #[tokio::test]
    async fn a_side_object_is_read_up_to_1024_bytes_and_no_further() {
        let files = MemFsBackend::new()
            .with_file("exact", &[7; 1024])
            .with_file("over", &[7; 1025])
            .with_dir("dir");
        let faulty = Faulty::new(files.clone());

        let read = read_side_object(&faulty, &path("exact")).await.unwrap();
        assert_eq!(read, Some(vec![7; 1024]));
        assert_eq!(faulty.reads(), [(0, 1024)]);

        assert_eq!(
            read_side_object(&faulty, &path("over")).await.unwrap(),
            None
        );
        assert_eq!(faulty.reads().len(), 1, "an object over 1024 is not read");
        for none in ["dir", "missing", "exact\\below"] {
            let read = read_side_object(&faulty, &path(none)).await.unwrap();
            assert_eq!(read, None, "{none}");
        }

        faulty.fail_open(1, |_, _| true, injected());
        assert!(matches!(
            read_side_object(&faulty, &path("exact")).await,
            Err(SmbError::Io(_))
        ));
        assert_eq!(files.open_handles(), 0);
    }

    #[test]
    fn a_call_holds_whole_blocks_and_at_most_4_mib() {
        for (bits, blocks) in [(13, 510), (16, 63), (20, 3)] {
            let shift = BlockShift::new(bits).unwrap();
            let block = usize::try_from(shift.stored_block_size()).unwrap();
            assert_eq!(call_len(shift), blocks * block, "shift {bits}");
            assert!(call_len(shift) <= 4 << 20);
            assert!(call_len(shift) + block > 4 << 20);
        }
    }

    #[tokio::test]
    async fn blocks_are_read_in_calls_that_end_on_block_boundaries() {
        let shift = BlockShift::new(13).unwrap();
        let data: Vec<u8> = (0..9_000_024u32).map(|i| (i % 251) as u8).collect();
        let files = MemFsBackend::new().with_file("f", &data);
        let faulty = Faulty::new(files);
        let (handle, _) = faulty
            .open(&path("f"), opts(OpenIntent::Open))
            .await
            .unwrap();

        let got = read_blocks(&*handle, shift, 24, 9_000_000).await.unwrap();
        assert_eq!(got, data[24..]);
        assert_eq!(
            faulty.reads(),
            [
                (24, 4_192_200),
                (24 + 4_192_200, 4_192_200),
                (24 + 8_384_400, 615_600)
            ]
        );
        assert!(
            read_blocks(&*handle, shift, 24, 0)
                .await
                .unwrap()
                .is_empty()
        );
        assert_eq!(faulty.reads().len(), 3);
    }

    #[tokio::test]
    async fn short_reads_of_blocks_continue_inside_their_call() {
        let shift = BlockShift::new(13).unwrap();
        let data: Vec<u8> = (0..20_000u32).map(|i| (i % 251) as u8).collect();
        let files = MemFsBackend::new().with_file("f", &data);
        let faulty = Faulty::new(files);
        faulty.short_reads(Some(7_000));
        let (handle, _) = faulty
            .open(&path("f"), opts(OpenIntent::Open))
            .await
            .unwrap();

        let got = read_blocks(&*handle, shift, 24, 19_976).await.unwrap();
        assert_eq!(got, data[24..]);
        assert_eq!(
            faulty.reads(),
            [(24, 19_976), (7_024, 12_976), (14_024, 5_976)]
        );

        faulty.short_reads(Some(0));
        let Err(SmbError::Io(error)) = read_blocks(&*handle, shift, 24, 100).await else {
            panic!("a read that returns nothing fails");
        };
        assert_eq!(error.kind(), io::ErrorKind::UnexpectedEof);
    }

    #[tokio::test]
    async fn blocks_are_written_in_calls_that_end_on_block_boundaries() {
        let shift = BlockShift::new(13).unwrap();
        let data: Vec<u8> = (0..9_000_024u32).map(|i| (i % 251) as u8).collect();
        let files = MemFsBackend::new();
        let faulty = Faulty::new(files.clone());
        let (handle, _) = faulty
            .open(&path("f"), opts(OpenIntent::Create))
            .await
            .unwrap();

        write_blocks(&*handle, shift, 0, &data).await.unwrap();
        assert_eq!(
            faulty.write_calls(),
            [(0, 4_184_004), (4_184_004, 4_192_200), (8_376_204, 623_820)]
        );
        assert_eq!(files.contents("f").unwrap(), data);

        write_blocks(&*handle, shift, 0, b"").await.unwrap();
        assert_eq!(faulty.write_calls().len(), 3);
    }

    #[tokio::test]
    async fn a_call_of_any_run_ends_on_a_block_boundary_and_holds_at_most_4_mib() {
        for bits in [13, 16, 20] {
            let shift = BlockShift::new(bits).unwrap();
            let block = shift.stored_block_size();
            let len = 9 * (1 << 20) + 17;
            let data = vec![1u8; len];
            let header = HEADER_LEN as u64;
            for offset in [
                0,
                5,
                header,
                header + block - 1,
                header + block,
                header + 3 * block + 9,
            ] {
                let files = MemFsBackend::new();
                let faulty = Faulty::new(files.clone());
                faulty.sink_writes_from(Some(0));
                let (handle, _) = faulty
                    .open(&path("f"), opts(OpenIntent::Create))
                    .await
                    .unwrap();
                write_blocks(&*handle, shift, offset, &data).await.unwrap();

                assert_calls(
                    &faulty.write_calls(),
                    block,
                    offset,
                    offset + len as u64,
                    &format!("shift {bits}, offset {offset}"),
                );
            }
        }
    }

    #[tokio::test]
    async fn short_writes_of_blocks_continue_inside_their_call() {
        let shift = BlockShift::new(13).unwrap();
        let data: Vec<u8> = (0..19_976u32).map(|i| (i % 251) as u8).collect();
        let files = MemFsBackend::new();
        let faulty = Faulty::new(files.clone());
        faulty.short_writes(Some(7_000));
        let (handle, _) = faulty
            .open(&path("f"), opts(OpenIntent::Create))
            .await
            .unwrap();

        write_blocks(&*handle, shift, 24, &data).await.unwrap();
        assert_eq!(
            faulty.write_calls(),
            [(24, 19_976), (7_024, 12_976), (14_024, 5_976)]
        );
        assert_eq!(files.contents("f").unwrap()[24..], data[..]);

        faulty.short_writes(Some(0));
        let Err(SmbError::Io(error)) = write_blocks(&*handle, shift, 24, b"x").await else {
            panic!("a write that takes nothing fails");
        };
        assert_eq!(error.kind(), io::ErrorKind::WriteZero);
    }
}