Last active
October 30, 2020 22:57
-
-
Save mssun/ae086fe3acb010c7fb9183f6a75c78eb to your computer and use it in GitHub Desktop.
teaclave-sgx-sdk-fs-example.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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