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
extern crate game_kernel_utils;

use std::cell::{Ref, RefCell};
use std::collections::HashMap;
use std::io::{Read, Seek, Write};
use std::ops::{Deref, DerefMut};
use std::sync::{Arc, RwLock, RwLockReadGuard};

use game_kernel_utils::{MapAbstract, MapMap};

mod path;

pub trait OpenVFile: OpenVROFile + Write {}
pub trait OpenVROFile: Read + Seek {}

trait VFile: VROFile {
    fn open_rw(&self) -> Box<dyn OpenVROFile>;
}

trait VROFile: AsVROFile {
    fn open(&mut self) -> Box<dyn OpenVROFile>;
}

trait AsVROFile {
    fn as_ro(&self) -> &VROFile;
}

impl<T: VROFile> AsVROFile for T {
    fn as_ro(&self) -> &VROFile {
        self
    }
}

enum FsObjTypes {
    file(Box<VFile>),
    ro_file(Box<VROFile>),
    directory(Directory),
}

#[derive(Clone)]
pub enum Permissions {
    Read,
    ReadWrite,
}

pub struct FsObject {
    //parent: Option<RefCell<Directory>>,
    obj_type: FsObjTypes,
    name: String,
    acl: HashMap<u32, Permissions>,
}

impl FsObject {
    pub fn get_acl(&self) -> &HashMap<u32, Permissions> {
        &self.acl
    }

    pub fn get_permissions(&self, user: &u32) -> Option<Permissions> {
        self.get_acl().get(user).map(|t| (*t).clone())
    }

    pub fn can_read(&self, user: &u32) -> bool {
        self.get_permissions(user).is_some()
    }

    pub fn can_write(&self, user: &u32) -> bool {
        if let Some(Permissions::ReadWrite) = self.get_permissions(user) {
            true
        } else {
            false
        }
    }
}

pub struct FileMeta {
    name: String,
    ro: bool,
    acl: HashMap<u32, Permissions>,
}

pub struct Directory {
    contents: HashMap<String, Arc<FsObject>>,
    driver: Option<Box<dyn FsDriver>>,
    driver_path: Option<path::Path>,
    read_only: bool,
}

impl Directory {
    pub fn new() -> Self {
        Self {
            contents: HashMap::new(),
            driver: None,
            driver_path: None,
            read_only: false,
        }
    }

    pub fn get_contents(&self) -> HashMap<String, Arc<FsObject>> {
        self.contents.clone()
    }

    pub fn get_contents_mut(&mut self) -> HashMap<String, Arc<FsObject>> {
        self.contents.clone()
    }

    pub fn put_object(&mut self, obj: FsObject) -> Result<(), ()> {
        if self.read_only {
            return Err(());
        }

        if let (Some(ref mut driver), Some(ref d_path)) = (&mut self.driver, &self.driver_path) {
            driver.put_object(&d_path, obj)
        } else {
            self.contents.insert(obj.name.clone(), Arc::new(obj));
            Ok(())
        }
    }

    pub fn new_file(&mut self, meta: FileMeta) -> Result<&FsObject, ()> {
        if self.read_only || self.driver.is_none() {
            return Err(());
        }

        if let (Some(driver), Some(path)) = (&mut self.driver, &self.driver_path) {
            driver.put_new_object(meta, path)
        } else {
            Err(())
        }
    }
}

pub trait FsDriver {
    fn get_root(&self) -> Directory;
    fn put_object(&mut self, path: &path::Path, obj: FsObject) -> Result<(), ()>;
    fn put_new_object<'a>(
        &'a mut self,
        meta: FileMeta,
        path: &path::Path,
    ) -> Result<&'a FsObject, ()>;
}

pub struct Vfs {
    root: Arc<FsObject>,
}

impl Vfs {
    /*pub fn new() -> Self
    {
        Self{
            root: Directory::new()
        }
    }*/

    pub fn get_object_mut(&mut self, path: &path::Path) -> Result<Arc<FsObject>, ()> {
        let mut curr_obj = self.root.clone();
        for obj_name in path.obj_name_iter() {
            match (curr_obj.obj_type) {
                FsObjTypes::directory(ref dir) => {
                    let contents = dir.get_contents();
                    if let Some(obj) = contents.get(obj_name) {
                        curr_obj = obj.clone();
                    } else {
                        return Err(());
                    }
                }

                _ => return Err(()),
            }
        }

        Ok(curr_obj)
    }

    pub fn get_object(&self, path: &path::Path) -> Result<Arc<FsObject>, ()> {
        let mut curr_obj = self.root.clone();
        for obj_name in path.obj_name_iter() {
            match (curr_obj.obj_type) {
                FsObjTypes::directory(ref dir) => {
                    let contents = dir.get_contents();
                    if let Some(obj) = contents.get(obj_name) {
                        curr_obj = obj.clone();
                    } else {
                        return Err(());
                    }
                }

                _ => return Err(()),
            }
        }

        Ok(curr_obj)
    }

    pub fn put_object(&mut self, path: path::Path) -> Result<(), ()> {
        let (path, name) = path.split_at_base();
        let mut curr_obj = self.root.clone();
        for obj_name in path.obj_name_iter() {
            match (curr_obj.obj_type) {
                FsObjTypes::directory(ref dir) => {
                    let contents = dir.get_contents();
                    if let Some(obj) = contents.get(obj_name) {
                        curr_obj = obj.clone();
                    } else {
                        return Err(());
                    }
                }

                _ => return Err(()),
            }
        }

        Ok(())
    }
}

pub fn mount<T: FsDriver>(vfs: &mut Vfs, driver: T, path: path::Path) -> Result<(), ()> {
    let driver_tobject = Box::new(driver);
    let (base, filename) = path.split_at_base();
    let mut b = vfs.get_object_mut(&base)?;
    let dir = match (&mut Arc::get_mut(&mut b).ok_or(())?.obj_type) {
        FsObjTypes::directory(dir) => Ok(dir),
        _ => Err(()),
    }?;
    dir.put_object(FsObject {
        //parent: Some(RefCell::new(*dir)),
        obj_type: FsObjTypes::directory(driver_tobject.get_root()),
        name: filename,
        acl: HashMap::new(),
    });
    Ok(())
}

pub struct VfsHanfle<'a> {
    user: u32,
    vfs: &'a Vfs,
}

impl<'a> VfsHanfle<'a> {
    pub fn new(user: u32, vfs: &'a mut Vfs) -> Self {
        Self { user, vfs }
    }

    fn check_permission<T>(&self, obj: T, perm: Permissions) -> Result<T, ()>
    where
        T: Deref<Target = FsObject>,
    {
        match ((obj.can_read(&self.user), obj.can_write(&self.user), perm)) {
            (true, _, Permissions::Read) => Ok(obj),
            (true, true, Permissions::ReadWrite) => Ok(obj),
            _ => Err(()),
        }
    }

    fn check_permission_mut<T>(&self, obj: T, perm: Permissions) -> Result<T, ()>
    where
        T: DerefMut<Target = FsObject>,
    {
        match ((obj.can_read(&self.user), obj.can_write(&self.user), perm)) {
            (true, _, Permissions::Read) => Ok(obj),
            (true, true, Permissions::ReadWrite) => Ok(obj),
            _ => Err(()),
        }
    }

    fn own_acl(&self) -> HashMap<u32, Permissions> {
        let mut acl = HashMap::new();
        acl.insert(self.user, Permissions::ReadWrite);
        acl
    }

    /*pub fn open(&self, path: &path::Path) -> Result<&'a VROFile, ()>
    {
        let obj = self.vfs.get_object(path)?;
        if let FsObject{obj_type: FsObjTypes::file(file), ..} = self.check_permission(obj.as_ref(), Permissions::Read)?
        {
            Ok(file.as_ref().clone().as_ro())
        }
        else if let FsObject{obj_type: FsObjTypes::ro_file(file), ..} = self.check_permission(obj.as_ref(), Permissions::Read)?
        {
            Ok(file.as_ref().clone())
        }
        else
        {
            Err(())
        }
    }

    pub fn open_rw(&self, path: path::Path) -> Result<&'a VFile, ()>
    {
        let obj = self.vfs.get_object(&path)?;
        /*let obj = obj.unwrap_or({
            let (base, filename) = path.split_at_base();
            if let Ok(FsObject{obj_type: FsObjTypes::directory(dir), ..}) = self.check_permission_mut(self.vfs.get_object_mut(&base)?, Permissions::ReadWrite)
            {
                dir.new_file(FileMeta{name: filename, ro: false, acl: self.own_acl()})?
            }
            else
            {
                return Err(())
            }
        });*/
        if let FsObject{obj_type: FsObjTypes::file(ref file), ..} = *self.check_permission(obj, Permissions::ReadWrite)?
        {
            Ok(file.as_ref())
        }
        else
        {
            Err(())
        }
    }*/
}