Skip to content

Instantly share code, notes, and snippets.

@mssun
Last active October 30, 2020 22:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mssun/ae086fe3acb010c7fb9183f6a75c78eb to your computer and use it in GitHub Desktop.
Save mssun/ae086fe3acb010c7fb9183f6a75c78eb to your computer and use it in GitHub Desktop.
teaclave-sgx-sdk-fs-example.rs
use teaclave_sgx_sdk::fs::{RepoOpener, OpenOptions};
use std::io::prelude::*;
use std::io::{Seek, SeekFrom};
use std::path::Path;
// The most core parts of this module are `Repo` and `File`, which provides most
// API for file system operations and file data I/O.
//
// `Repo` provides similar file system manipulation methods to `std::fs`
// `File` provides similar file I/O methods to `std::fs::File`
fn main() {
// Create and open a repository (in-memory FS), return the `teaclave_sgx_sdk::fs::Repo` type
let mut repo = RepoOpener::new()
.create(true)
.open("mem://my_repo")
.unwrap();
// Create and open a file for writing, return the `teaclave_sgx_sdk::fs::File` type
let mut file = OpenOptions::new()
.create(true)
.open(&mut repo, "/my_file.txt")
.unwrap();
// Use std::io::Write trait to write data into it
file.write_all(b"Hello, world!").unwrap();
// Read file content using std::io::Read trait
let mut content = String::new();
file.seek(SeekFrom::Start(0)).unwrap();
file.read_to_string(&mut content).unwrap();
assert_eq!(content, "Hello, world!");
// Directory navigation can use Path and PathBuf.
// The path separator should always be "/".
let path = Path::new("/foo/bar");
repo.create_dir_all(&path).unwrap();
assert!(repo.is_dir(path.parent().unwrap()).is_ok());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment