summaryrefslogtreecommitdiff
path: root/src/file.rs
blob: 2e69cdd7e69441d1e6f0aa2918fd7568455f1f8b (plain)
use std::{
	fs::File,
	path::{Path, PathBuf},
};

use happylock::ThreadKey;
use uuid::Uuid;

use crate::processes::process_id;

fn root() -> &'static Path {
	Path::new("~/.dozos")
}

fn path(root: &Path, program_id: Uuid, file_id: Uuid) -> PathBuf {
	let mut builder = PathBuf::from(root);
	builder.push(program_id.to_string());
	builder.push(file_id.to_string());

	builder
}

pub fn open(key: &mut ThreadKey, file_id: Uuid) -> std::io::Result<File> {
	let path = path(root(), process_id(key), file_id);
	File::options()
		.read(true)
		.write(true)
		.create(true)
		.truncate(false)
		.open(path)
}