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
use std::io::{Read, Seek};

use crate::utils::calculate_seek;

/// The `File` struct represents an individual file within the multi-file context.
pub struct File<R> {
    /// The inner reader for the file.
    pub file: R,
    /// The size of the file in bytes.
    pub size: usize,
    /// The name of the file.
    pub filename: String,
}

impl<R: Read> Read for File<R> {
    #[inline]
    fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
        self.file.read(buf)
    }
}

impl<R: Seek> Seek for File<R> {
    #[inline]
    fn seek(&mut self, pos: std::io::SeekFrom) -> std::io::Result<u64> {
        self.file.seek(pos)
    }
}

/// The `MultiFile` struct combines multiple files into a unified stream,
/// allowing sequential reading as if all files are concatenated.
pub struct MultiFile<R> {
    /// The list of files
    files: Vec<File<R>>,

    /// The cumulative offset to the current file within the multi-file context.
    /// (without the in-file offset)
    cumul_offset: usize,
    /// The offset within the current file.
    infile_offset: usize,

    /// The total size of the combined multi-file stream.
    total_len: usize,
    /// The index of the current file being read from.
    current_file_idx: usize,
}

impl<R> MultiFile<R> {
    /// Creates a new `MultiFile` instance with the provided list of files.
    pub fn new(files: Vec<File<R>>) -> Self {
        let total_len = files.iter().map(|f| f.size).sum();
        Self {
            current_file_idx: 0,
            infile_offset: 0,
            cumul_offset: 0,
            files,
            total_len,
        }
    }

    /// Converts the given position within the combined multi-file stream
    /// to the index of the corresponding file within the `files`.
    #[inline]
    fn needle_to_file(&self, needle: usize) -> Option<usize> {
        if needle > self.total_len {
            return None;
        }

        if self.cumul_offset >= needle {
            let mut res = 0;
            for (idx, file) in self.files.iter().enumerate().take(self.current_file_idx) {
                if res + file.size >= needle {
                    return Some(idx);
                }
                res += file.size;
            }
        } else {
            let mut res = self.cumul_offset;
            for (idx, file) in self.files.iter().enumerate().skip(self.current_file_idx) {
                if res + file.size > needle {
                    return Some(idx);
                }
                res += file.size;
            }
        }

        unreachable!()
    }

    /// Calculates the physical offset within the combined multi-file stream.
    #[inline]
    fn physical_offset(&self) -> usize {
        self.cumul_offset + self.infile_offset
    }

    /// The total size of the multi-file stream in bytes.
    pub fn size(&self) -> usize {
        self.total_len
    }
}

impl<R: Read> Read for MultiFile<R> {
    fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
        let tail_idx;
        let mut infile = 0;

        let expected = buf.len();
        let mut taken = 0;

        'find: {
            for (idx, file) in self.files[self.current_file_idx..].iter_mut().enumerate() {
                infile = file.read(&mut buf[taken..])?;
                taken += infile;
                if taken == expected {
                    tail_idx = self.current_file_idx + idx;
                    break 'find;
                }
            }
            tail_idx = self.files.len() - 1;
        }
        let _cumul_offset: usize = self.files[self.current_file_idx..tail_idx]
            .iter()
            .map(|f| f.size)
            .sum();

        self.cumul_offset += _cumul_offset;
        self.current_file_idx = tail_idx;
        self.infile_offset = infile;

        Ok(taken)
    }
}

impl<R: Read + Seek> Seek for MultiFile<R> {
    fn seek(&mut self, pos: std::io::SeekFrom) -> std::io::Result<u64> {
        let calculated_seek = calculate_seek(self.total_len, self.physical_offset(), pos)? as usize;
        let calculated_idx = self.needle_to_file(calculated_seek).unwrap();

        let new_cum = self.files[..calculated_idx]
            .iter()
            .map(|f| f.size)
            .sum::<usize>();

        let seek_to = calculated_seek - new_cum;

        match calculated_idx.cmp(&self.current_file_idx) {
            std::cmp::Ordering::Greater => {
                for file in self.files[..calculated_idx].iter_mut() {
                    let _ = file.seek(std::io::SeekFrom::End(0))?;
                }
            }
            std::cmp::Ordering::Less => {
                for file in self.files[calculated_idx + 1..=self.current_file_idx].iter_mut() {
                    let _ = file.seek(std::io::SeekFrom::Start(0))?;
                }
            }
            std::cmp::Ordering::Equal => {}
        }

        let res =
            self.files[calculated_idx].seek(std::io::SeekFrom::Start(seek_to as u64))? as usize;

        self.current_file_idx = calculated_idx;
        self.cumul_offset = new_cum;
        self.infile_offset = res;

        Ok((new_cum + res) as u64)
    }

    fn stream_position(&mut self) -> std::io::Result<u64> {
        Ok(self.physical_offset() as u64)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Cursor;

    impl From<Cursor<Vec<u8>>> for File<Cursor<Vec<u8>>> {
        fn from(value: Cursor<Vec<u8>>) -> Self {
            let len = value.get_ref().len();
            Self {
                file: value,
                size: len,
                filename: "cursor".to_string(),
            }
        }
    }

    fn new_file() -> MultiFile<Cursor<Vec<u8>>> {
        let a = Cursor::new(vec![1u8, 2, 3]);
        let b = Cursor::new(vec![4u8, 5, 6]);

        MultiFile::new(vec![a.into(), b.into()])
    }
    #[test]
    fn test_read1() {
        let a = Cursor::new(vec![1u8, 2, 3]);
        let mut file = MultiFile::new(vec![a.into()]);

        {
            let mut buf = [0u8; 2];
            let _ = file.seek(std::io::SeekFrom::End(-2));

            let _ = file.read(&mut buf).unwrap();
            assert_eq!(buf, [2, 3])
        }
    }
    #[test]
    fn test_read2() {
        let mut file = new_file();

        {
            let mut buf = [0u8; 3];
            file.read(&mut buf).unwrap();
            assert_eq!(buf, [1, 2, 3])
        }

        {
            let mut buf = [0u8; 1];
            file.read(&mut buf).unwrap();
            assert_eq!(buf, [4])
        }

        {
            let mut buf = [0u8; 5];
            file.read(&mut buf).unwrap();
            assert_eq!(buf, [5, 6, 0, 0, 0])
        }
    }

    #[test]
    fn test_seek() {
        let mut file = new_file();

        {
            let mut buf = [0u8; 1];

            let _ = file.seek(std::io::SeekFrom::Start(3));

            let _ = file.read(&mut buf).unwrap();
            assert_eq!(buf, [4])
        }

        {
            let mut buf = [0u8; 2];

            let _ = file.seek(std::io::SeekFrom::Current(-1));

            let _ = file.read(&mut buf).unwrap();
            assert_eq!(buf, [4, 5])
        }

        {
            let mut buf = [0u8; 5];

            let _ = file.seek(std::io::SeekFrom::Start(0));

            let _ = file.read(&mut buf).unwrap();
            assert_eq!(buf, [1, 2, 3, 4, 5])
        }
    }
}