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
use std::ffi::{OsStr};
use time::Timespec;
use fuse::{
    FileType,
    Filesystem,
    ReplyAttr,
    ReplyCreate,
    ReplyData, ReplyDirectory, ReplyEmpty,
    ReplyEntry, ReplyOpen, ReplyWrite,
    Request,
};

use std::path;

extern crate file_store;
use file_store::fstore::{FileStore};

use file_node::SingleService;

use libc::{ENOENT, ENOTDIR};

pub struct Fs {
    store: FileStore
}

impl Fs {
    pub fn new(svcs: Vec<Box<dyn SingleService + Send>>) -> Fs {
        let mut fs = Fs {
            store: FileStore::new()
        };

        fs.register_services(svcs);

        fs
    }

    fn register_services(&mut self, svcs: Vec<Box<dyn SingleService + Send>>) {
        self.store.register_services(svcs);
    }
}

impl Filesystem for Fs {
    fn init(&mut self, _req: &Request) -> Result<(), i32> {
        log::info!("up and running");

        Ok(())
    }

    fn access(&mut self, _req: &Request, ino: u64, mask: u32, reply: ReplyEmpty) {
        log::error!("access: {} {}", ino, mask);
                reply.ok();
    }

    fn lookup(&mut self, _req: &Request, parent: u64, name: &OsStr, reply: ReplyEntry) {
        log::error!("called lookup");
        match self.store.lookup_path(&parent, name) {
            Some(file) => {
                log::error!("found file: {:?}", file);
                let _data = &file.data;

                // the generation final arg needs to be the id.
                // seems similar to fh wtf
                reply.entry(&file.ttl, &file.attr, file.id);
            }
            None => {
                log::error!("no file found in lookup: {:?} {:?}", name, parent);
                reply.error(ENOENT);
            }
        }

    }

    fn rmdir(
        &mut self, 
        _req: &Request, 
        parent: u64, 
        name: &OsStr, 
        reply: ReplyEmpty

        ) {
        log::error!("unlink {} {:?}", parent, name);
        self.store.unlink(&parent, name);
        reply.ok();
    }

    fn rename(
        &mut self, 
        _req: &Request, 
        parent: u64, 
        name: &OsStr, 
        newparent: u64, 
        newname: &OsStr, 
        reply: ReplyEmpty
        ) {
        match self.store.rename(&parent, name, newparent, newname) {
            Ok(_) => reply.ok(),
            Err(_) => reply.error(ENOENT)
        }
    }


    fn mkdir(
        &mut self, 
        _req: &Request, 
        parent: u64, 
        name: &OsStr, 
        mode: u32, 
        reply: ReplyEntry
        ) {
        log::info!("creating a dir");

        let node = self.store.create_dir(parent, name, mode);
        let ttl = Timespec::new(1, 0);
        match node {
            Some(dir) => {
                reply.entry(&ttl, &dir.attr, dir.id);
            }
            _ => reply.error(ENOENT)
        }
    }

    fn create(
        &mut self,
        _req: &Request,
        parent: u64,
        name: &OsStr,
        mode: u32,
        flags: u32,
        reply: ReplyCreate,
    ) {
        let _now = time::now().to_timespec();
        log::error!("create: {}, {:?}, {}, {}", parent, name, mode, flags);
        let id = self.store.touch_file(&parent, name);
        match self.store.get(&id) {
            Some(f) => {
                let file = f.attr;
                let ttl = Timespec::new(1, 0);
                log::error!("got through create");
                reply.created(&ttl, &file, id, id, flags);
            }
            None => {
                log::error!("not a valid parent");
                reply.error(ENOTDIR);
            }
        }
    }


    fn read(&mut self, _req: &Request, ino: u64, _fh: u64, offset: i64, _size: u32, reply: ReplyData) {
        match self.store.read_file(&ino) {
            Some(data) => {
                // need to also restrict length of response by size bytes
                let d = &data[offset as usize..];

                reply.data(&d)
            }
            None => reply.error(ENOENT)
        }
    }

    fn write(
        &mut self,
        _req: &Request,
        ino: u64,
        fh: u64,
        offset: i64,
        data: &[u8],
        flags: u32,
        reply: ReplyWrite,
    ) {
        log::error!("write: {} {} {} {:?} {}", ino, fh, offset, data, flags);
        let w_size = std::mem::size_of_val(data) as u32;
        log::error!("write size: {}", w_size as u32);
        let size = self.store.write(ino, data, flags, offset);
        log::error!("size={}", size);
        // must return exact same size as data that was requested to be written
        // or else stupid io invalid arg error or something happens
        // really stupid
        reply.written(w_size)
    }

    fn readdir(
        &mut self,
        _req: &Request,
        ino: u64,
        fh: u64,
        offset: i64,
        mut reply: ReplyDirectory,
        ) {
        log::error!("readdir: {}, {}, {}", ino, fh, offset);
        match self.store.read_dir_children(&ino) {
            Some(children) => {
                let mut idx: u64 = 0;
                let offset = offset as u64;
                if offset > 2 {
                    idx = offset - 2;
                }

                let len = children.len() as u64;
                if offset < len + 1 as u64 {
                    reply.add(1, 0, FileType::Directory, &path::Path::new("."));
                    reply.add(1, 1, FileType::Directory, &path::Path::new(".."));
                    let mut ctr = 2 + offset as i64;
                    for id in children.range(idx..) {
                        match self.store.get(&id) {
                            Some(f) => {
                                reply.add(f.id, ctr, f.attr.kind, &f.path);
                                ctr += 1;
                                log::error!("{:?}", f);
                            }
                            None => log::error!(
                                "dangling child reference: parent={} child={}",
                                &ino,
                                &id
                            ),
                        }
                    }
                }
                reply.ok();

            }
            None => reply.error(ENOENT)

        }
    }

    /*
    fn getlk(
        &mut self, 
        _req: &Request, 
        _ino: u64, 
        _fh: u64, 
        _lock_owner: u64, 
        _start: u64, 
        _end: u64, 
        _typ: u32, 
        _pid: u32, 
        _reply: ReplyLock
        ) {
        log::error!("getlk!");
    }
    */

    fn setattr(
        &mut self,
        _req: &Request,
        ino: u64,
        mode: Option<u32>,
        gid: Option<u32>,
        uid: Option<u32>,
        size: Option<u64>,
        atime: Option<Timespec>,
        mtime: Option<Timespec>,
        fh: Option<u64>,
        crtime: Option<Timespec>,
        chgtime: Option<Timespec>,
        bkuptime: Option<Timespec>,
        flags: Option<u32>,
        reply: ReplyAttr,
    ) {
        log::error!(
            "set attr: ino={} mode={:?} gid={:?} \
             uid={:?} size={:?} atime{:?} \
             mtime={:?} fh={:?} crtime={:?} \
             chgtime={:?} bkuptime={:?} flags={:?}",
            ino,
            mode,
            gid,
            uid,
            size,
            atime,
            mtime,
            fh,
            crtime,
            chgtime,
            bkuptime,
            flags
            );
        let now = Timespec::new(1, 0);
        if let Some(_) = size {
            self.store.clear_file(&ino);
        }
        let file = self.store.get(&ino).unwrap();
        reply.attr(&now, &file.attr);

    }

    fn flush(&mut self, _req: &Request, ino: u64, fh: u64, lock_owner: u64, reply: ReplyEmpty) {
        log::error!("flush: {}, {}, {}", ino, fh, lock_owner);
        reply.ok();

    }

    fn release(
        &mut self,
        _req: &Request,
        ino: u64,
        fh: u64,
        flags: u32,
        lock_owner: u64,
        flush: bool,
        reply: ReplyEmpty,

        ) {
        log::error!("release {} {} {} {} {}", ino, fh, flags, lock_owner, flush);
        reply.ok();
    }

    /*
    fn opendir(&mut self, _req: &Request, ino: u64, flags: u32, reply: ReplyOpen) {
        log::error!("opendir: {}, {}", ino, flags);
    }
    */
    fn open(&mut self, _req: &Request, ino: u64, flags: u32, reply: ReplyOpen) {
        log::error!("open called {:?} {:?}", ino, flags);
        reply.opened(ino, flags);
    }

    fn getattr(&mut self, _req: &Request, ino: u64, reply: ReplyAttr) {
        match self.store.get(&ino) {
            Some(file) => {
                let ttl = Timespec::new(1, 0);
                log::info!("found filez: {:?} {:?}", file.attr, ttl);
                reply.attr(&ttl, &file.attr);
            }
            None => {
                log::error!("none found! {:?}", ino, );
                reply.error(ENOENT);
            }
        }

    }

    fn unlink(&mut self, _req: &Request, parent: u64, name: &OsStr, reply: ReplyEmpty) {
        log::error!("unlink {} {:?}", parent, name);
        self.store.unlink(&parent, name);
        reply.ok();
    }

}