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
/*
 * desc.rs: file descriptor data structure and needed tools to operate on single file.
 * Copyright (C) 2019  Oddcoder
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
use plugin::*;
use serde::{Deserialize, Serialize};
use utils::*;
#[derive(Serialize, Deserialize)]
pub struct RIODesc {
    pub(crate) name: String,
    pub(crate) perm: IoMode,
    pub(crate) hndl: u64,
    pub(crate) paddr: u64, //padd is simulated physical address
    pub(crate) size: u64,
    raddr: u64, // raddr is the IO descriptor address, general rule of interaction paddr is high level lie, while raddr is the real thing.
    // Since we are skiping files operation structures .. after deserializing RIO .. we must
    // reopen the files again and make sure that they are in the right place
    #[serde(skip)]
    plugin_operations: Box<dyn RIOPluginOperations>,
}

impl RIODesc {
    pub(crate) fn raddr(&self) -> u64 {
        self.raddr
    }
    pub(crate) fn open(plugin: &mut dyn RIOPlugin, uri: &str, flags: IoMode) -> Result<RIODesc, IoError> {
        let plugin_desc = plugin.open(uri, flags)?;
        let desc = RIODesc {
            hndl: 0,
            name: plugin_desc.name,
            perm: plugin_desc.perm,
            paddr: 0,
            size: plugin_desc.size,
            plugin_operations: plugin_desc.plugin_operations,
            raddr: plugin_desc.raddr,
        };
        return Ok(desc);
    }
    pub(crate) fn reopen(&mut self, plugin: &mut dyn RIOPlugin) -> Result<(), IoError> {
        let plugin_desc = plugin.open(&self.name, self.perm)?;
        self.plugin_operations = plugin_desc.plugin_operations;
        self.raddr = plugin_desc.raddr;
        return Ok(());
    }
    pub(crate) fn read(&mut self, paddr: usize, buffer: &mut [u8]) -> Result<(), IoError> {
        return self.plugin_operations.read(paddr - self.paddr as usize + self.raddr as usize as usize, buffer);
    }
    pub(crate) fn write(&mut self, paddr: usize, buffer: &[u8]) -> Result<(), IoError> {
        return self.plugin_operations.write(paddr - self.paddr as usize + self.raddr as usize, buffer);
    }
    /// Returns URI of current file descriptor.
    pub fn name(&self) -> &str {
        return &self.name;
    }
    /// Returns *true* if paddr exists in this file descriptor and *false* otherwise.
    pub fn has_paddr(&self, paddr: u64) -> bool {
        return paddr >= self.paddr && paddr < self.paddr + self.size as u64;
    }
    /// Returns the base physical address of this file.
    pub fn paddr_base(&self) -> u64 {
        return self.paddr;
    }
    /// Returns size of file on disk.
    pub fn size(&self) -> u64 {
        return self.size;
    }
    /// Returns the permissions which the file was opened with.
    pub fn perm(&self) -> IoMode {
        return self.perm;
    }
    /// Returns the Handle of given file descriptor.
    pub fn hndl(&self) -> u64 {
        return self.hndl;
    }
}

#[cfg(test)]
mod default_plugin_tests {
    use super::*;
    use plugins::defaultplugin;
    use std::io;
    use std::path::Path;
    use test_file::*;
    fn test_desc_read_cb(path: &Path) {
        let mut plugin = defaultplugin::plugin();
        let mut desc = RIODesc::open(&mut *plugin, &path.to_string_lossy(), IoMode::READ).unwrap();
        desc.paddr = 0x40000;
        let mut buffer: &mut [u8] = &mut [0; 8];
        // read at the begining
        desc.read(desc.paddr as usize, &mut buffer).unwrap();
        assert_eq!(buffer, [0x00, 0x01, 0x01, 0x02, 0x03, 0x05, 0x08, 0x0d]);
        // read at the middle
        desc.read((desc.paddr + 0x10) as usize, &mut buffer).unwrap();
        assert_eq!(buffer, [0xdb, 0x3d, 0x18, 0x55, 0x6d, 0xc2, 0x2f, 0xf1]);
        // read at the end
        desc.read((desc.paddr + 97) as usize, &mut buffer).unwrap();
        assert_eq!(buffer, [0x41, 0xc1, 0x02, 0xc3, 0xc5, 0x88, 0x4d, 0xd5]);
    }
    #[test]
    fn test_desc_read() {
        operate_on_file(&test_desc_read_cb, DATA)
    }
    fn test_desc_has_paddr_cb(path: &Path) {
        let mut plugin = defaultplugin::plugin();
        let mut desc = RIODesc::open(&mut *plugin, &path.to_string_lossy(), IoMode::READ).unwrap();
        desc.paddr = 0x40000;
        assert_eq!(desc.has_paddr(0x40000), true);
        assert_eq!(desc.has_paddr(0x5), false);
        assert_eq!(desc.has_paddr(0x40000 + DATA.len() as u64), false);
        assert_eq!(desc.has_paddr(0x40000 + DATA.len() as u64 - 1), true);
    }
    #[test]
    fn test_desc_has_paddr() {
        operate_on_file(&test_desc_has_paddr_cb, DATA);
    }
    fn test_desc_read_errors_cb(path: &Path) {
        let mut plugin = defaultplugin::plugin();
        let mut desc = RIODesc::open(&mut *plugin, &path.to_string_lossy(), IoMode::READ).unwrap();
        desc.paddr = 0x40000;
        let mut buffer: &mut [u8] = &mut [0; 8];
        // read past the end
        let mut e = desc.read((desc.paddr + desc.size) as usize, &mut buffer);
        match e {
            Err(IoError::Parse(io_err)) => assert_eq!(io_err.kind(), io::ErrorKind::UnexpectedEof),
            _ => assert!(true, "UnexpectedEof Error should have been generated"),
        };
        // read at the middle past the the end
        e = desc.read((desc.paddr + desc.size - 5) as usize, &mut buffer);
        match e {
            Err(IoError::Parse(io_err)) => assert_eq!(io_err.kind(), io::ErrorKind::UnexpectedEof),
            _ => assert!(true, "UnexpectedEof Error should have been generated"),
        };

        // read at the start past the end
        let mut v: Vec<u8> = vec![0; (desc.size + 8) as usize];
        buffer = &mut v;
        e = desc.read(desc.paddr as usize, &mut buffer);
        match e {
            Err(IoError::Parse(io_err)) => assert_eq!(io_err.kind(), io::ErrorKind::UnexpectedEof),
            _ => assert!(true, "UnexpectedEof Error should have been generated"),
        };
    }
    #[test]
    fn test_desc_read_errors() {
        operate_on_file(&test_desc_read_errors_cb, DATA);
    }

    fn test_desc_write_cb(path: &Path) {
        let mut plugin = defaultplugin::plugin();
        let mut desc = RIODesc::open(&mut *plugin, &path.to_string_lossy(), IoMode::READ | IoMode::WRITE).unwrap();
        let mut buffer: &mut [u8] = &mut [0; 8];
        desc.paddr = 0x40000;
        // write at the begining
        desc.write(desc.paddr as usize, &buffer).unwrap();
        desc.read(desc.paddr as usize, &mut buffer).unwrap();
        assert_eq!(buffer, [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
        // write at the middle
        desc.write((desc.paddr + 0x10) as usize, &buffer).unwrap();
        desc.read((desc.paddr + 0x10) as usize, &mut buffer).unwrap();
        assert_eq!(buffer, [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
        // write at the end
        desc.write((desc.paddr + 97) as usize, &buffer).unwrap();
        desc.read((desc.paddr + 97) as usize, &mut buffer).unwrap();
        assert_eq!(buffer, [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
    }

    #[test]
    fn test_desc_write() {
        operate_on_file(&test_desc_write_cb, DATA);
    }

    fn test_write_errors_cb(path: &Path) {
        let mut plugin = defaultplugin::plugin();
        let mut desc = RIODesc::open(&mut *plugin, &path.to_string_lossy(), IoMode::READ | IoMode::WRITE).unwrap();
        let mut buffer: &[u8] = &[0; 8];
        desc.paddr = 0x40000;
        // write past the end
        let mut e = desc.write((desc.paddr + desc.size) as usize, &buffer);
        match e {
            Err(IoError::Parse(io_err)) => assert_eq!(io_err.kind(), io::ErrorKind::UnexpectedEof),
            _ => assert!(true, "UnexpectedEof Error should have been generated"),
        };
        // middle at the middle past the the end
        e = desc.write((desc.paddr + desc.size - 5) as usize, &buffer);
        match e {
            Err(IoError::Parse(io_err)) => assert_eq!(io_err.kind(), io::ErrorKind::UnexpectedEof),
            _ => assert!(true, "UnexpectedEof Error should have been generated"),
        };
        // read at the start past the end
        let v: Vec<u8> = vec![0; (desc.size + 8) as usize];
        buffer = &v;
        e = desc.write(desc.paddr as usize, &mut buffer);
        match e {
            Err(IoError::Parse(io_err)) => assert_eq!(io_err.kind(), io::ErrorKind::UnexpectedEof),
            _ => assert!(true, "UnexpectedEof Error should have been generated"),
        };
    }
    #[test]
    fn test_write_errors() {
        operate_on_file(&test_write_errors_cb, DATA);
    }
}