Giter Club home page Giter Club logo

configfs's Introduction

ConfigFS: Everything Should Be A File

ConfigFS is a library for creating filesystem's that allow users to view and modify structured data and settings. It is inspired by another filesystem of the same name, ConfigFS, a ram based filesystem on linux that allows users to configure kernel objects.

ConfigFS relies on fuse3. It can be installed via your favorite package manager.

You can view the docs here

How Does It Work?

struct FavNumberConfig {
    fav_number: i64,
    output: String,
    modified: bool,
}

impl FavNumberConfig {
    pub fn new(fav_number: i64, output: String) -> Configuration {
        Configuration::Basic(Arc::new(RwLock::new(FavNumberConfig{fav_number, output, modified: false})))
    }
}

#[async_trait]
impl BasicConfigHook for FavNumberConfig {
    async fn fetch(&mut self) -> Result<Vec<u8>> {
        Ok(format!("{}", self.fav_number).as_bytes().to_vec())
    }

    async fn size(&mut self) -> Result<u64> {
        Ok(format!("{}", self.fav_number).as_bytes().len() as u64)
    }

    async fn update(&mut self, data: Vec<u8>) -> Result<()> {
        let input = String::from_utf8(data).map_err(|_| Errno::from(libc::EIO))?;
        let number = i64::from_str_radix(&input, 10).map_err(|_| Errno::from(libc::EIO))?;

        self.fav_number = number;
        println!("My new favourite number is {}", number);
        self.modified = true;
        Ok(())
    }
    
    async fn tick(&mut self) {
        if !self.modified {
            return
        }

        self.modified = false;
        let mut Ok(file) = fs::File::create(&self.output).await else {
            return
        };
        file.write_all(format!("{}", self.fav_number).as_bytes()).await.ok();
    }

    fn tick_interval(&self) -> Duration {
        Duration::from_secs(1)
    }
}

First you create your config interface by implementing the config type's respective trait. Basic configs function as files. Complex configs function as directories.

let mount = Mount::new();
{
    let mut mnt = mount.write().await;
    mnt.mount("/", FavNumberConfig::new(42));
}

Then you define how your configs should be mounted into the config filesystem. This allows you to mount multiple config interfaces into a single filesystem.

FS::mount("Fav Number FS", "/mnt/fav_number", mount)
    .await
    .unwrap()
    .await
    .unwrap()
    .unwrap()

Finally you mount your config filesystem.

License

This software is provided under the MIT license. Click here to view.

configfs's People

Contributors

ellabellla avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.