From 5083e2ba9b5ca0194772b135c5a32657ff7e478b Mon Sep 17 00:00:00 2001 From: Raine Date: Sat, 14 Oct 2023 22:39:39 +0200 Subject: [PATCH] feat: fixed file management --- .idea/pkgr.iml | 6 ++++ bootpkg/Cargo.toml | 3 ++ bootpkg/src/args.rs | 9 +++--- bootpkg/src/main.rs | 44 ++++++++++++++++++++++----- bootpkg/src/prelude.rs | 41 ++++++++++++++++--------- docs/pkgr/flow.md | 3 ++ manifest/src/bin.rs | 3 +- manifest/src/lib.rs | 18 ++++++++++- mock-pkg.py | 22 ++++++++++++++ package.toml | 19 +++++------- pkg/scripts/bootstrap/download_latest | 7 +++++ pkgfile/src/lib.rs | 16 ++++++++-- 12 files changed, 148 insertions(+), 43 deletions(-) create mode 100644 docs/pkgr/flow.md create mode 100644 mock-pkg.py create mode 100644 pkg/scripts/bootstrap/download_latest diff --git a/.idea/pkgr.iml b/.idea/pkgr.iml index c67ef27..c93234c 100644 --- a/.idea/pkgr.iml +++ b/.idea/pkgr.iml @@ -1,5 +1,10 @@ + + + + + @@ -13,5 +18,6 @@ + \ No newline at end of file diff --git a/bootpkg/Cargo.toml b/bootpkg/Cargo.toml index c48277f..8292150 100644 --- a/bootpkg/Cargo.toml +++ b/bootpkg/Cargo.toml @@ -8,5 +8,8 @@ edition = "2021" [dependencies] manifest = { path = "../manifest" } pkgfile = { path = "../pkgfile" } +toml = { version = "0.7.6", features = [ "parse" ] } +serde = { version = "1.0.171", features = ["derive"] } regex = "1.9.1" reqwest = { version = "0.11.18", features = ["blocking"] } +uuid = { version = "1.4.0", features = ["serde", "v4"] } diff --git a/bootpkg/src/args.rs b/bootpkg/src/args.rs index 2f51141..cf061e3 100644 --- a/bootpkg/src/args.rs +++ b/bootpkg/src/args.rs @@ -1,7 +1,7 @@ pub enum Command { Strap, Unpack, - None + None, } impl From for Command { @@ -9,11 +9,10 @@ impl From for Command { match value.to_lowercase().as_str() { "strap" => Command::Strap, "unpack" => Command::Unpack, - _ => Command::None + _ => Command::None, } } } - pub struct Args { pub command: Command, pub args: Vec, @@ -23,7 +22,7 @@ impl From> for Args { fn from(value: Vec) -> Self { Args { command: Command::from(value[0].to_owned()), - args: value[1..].to_owned() + args: value[1..].to_owned(), } } -} \ No newline at end of file +} diff --git a/bootpkg/src/main.rs b/bootpkg/src/main.rs index 8339041..f59cf01 100644 --- a/bootpkg/src/main.rs +++ b/bootpkg/src/main.rs @@ -1,10 +1,15 @@ -use std::env; -use std::process::exit; -use pkgfile::PKGFile; use crate::args::{Args, Command}; +use pkgfile::PKGFile; +use std::env; +use std::env::temp_dir; +use std::fs::{create_dir, create_dir_all, File}; +use std::io::Write; +use std::path::PathBuf; +use std::process::exit; +use uuid::Uuid; -mod prelude; mod args; +mod prelude; fn main() { let args = Args::from(env::args().collect::>()[1..].to_owned()); @@ -33,7 +38,7 @@ fn main() { } }; - let p = match PKGFile::try_from(data) { + let pkg = match PKGFile::try_from(data) { Ok(p) => p, Err(_) => { println!("!! Could not interpret PKGFile..."); @@ -41,8 +46,33 @@ fn main() { } }; - }, - Command::Unpack => todo!(), + let mani = prelude::mani_from_str(&pkg.manifest); + + println!("!! Package: {}", &mani.package.name); + + if !mani.valid() { + println!("!!! Manifest is not valid."); + exit(1); + } + + let mut tmp_dir = temp_dir(); + tmp_dir.push(format!("{}", Uuid::new_v4())); + create_dir_all(&tmp_dir) + .expect("Could not create tmp dir."); + + { + let mut file = File::create(format!("{}/pkgtar", tmp_dir.to_str().unwrap())) + .expect("Could not create tmp pkgtar file"); + + file.write(&pkg.data) + .expect("Could not write pkgtar to tmp file"); + } + + // TODO: untar + // TODO: cd into pkg + // TODO: run bootstrap commands + } + Command::Unpack => {} _ => { println!("Unsupported command, allowed commands: strap/unpack."); exit(1); diff --git a/bootpkg/src/prelude.rs b/bootpkg/src/prelude.rs index 4462619..b62eda1 100644 --- a/bootpkg/src/prelude.rs +++ b/bootpkg/src/prelude.rs @@ -1,22 +1,33 @@ -use std::collections::HashMap; -use manifest::Manifest; +use manifest::{self, Manifest}; +use serde::{Deserialize, Serialize}; +use std::{collections::HashMap, str::FromStr}; +use toml; +#[derive(Serialize, Deserialize)] pub struct PKGR { pub bootstrap: Option, } -pub struct Bootstrap {} +#[derive(Serialize, Deserialize)] +pub struct Bootstrap { + check_installed_commands: Vec, + commands: Vec +} -pub fn def_manifest() { +#[derive(Serialize, Deserialize)] +pub struct BadManifest { + pkgr: Option, +} + +pub fn mani_from_str(s: &str) -> Manifest> { + let mani = Manifest::from_str(s).unwrap(); + let bmani = toml::from_str::(s).unwrap(); Manifest::> { - package: manifest::package::Package::default(), - bin: None, - build: None, - dependencies: HashMap::default(), - fs: manifest::fs::FS { - config: None, - data: None, - }, - pkgr: Some(PKGR { bootstrap: None }), - }; -} \ No newline at end of file + package: mani.package, + dependencies: mani.dependencies, + fs: mani.fs, + bin: mani.bin, + build: mani.build, + pkgr: bmani.pkgr + } +} diff --git a/docs/pkgr/flow.md b/docs/pkgr/flow.md new file mode 100644 index 0000000..d6a9a40 --- /dev/null +++ b/docs/pkgr/flow.md @@ -0,0 +1,3 @@ +# Install flow + +pkgr keeps track of installed files and therefore we have a CTREE. diff --git a/manifest/src/bin.rs b/manifest/src/bin.rs index 10d1ea1..e8b48a3 100644 --- a/manifest/src/bin.rs +++ b/manifest/src/bin.rs @@ -1,7 +1,8 @@ +use std::collections::HashMap; use serde::{Serialize, Deserialize}; #[derive(Serialize, Deserialize)] pub struct Bin { pub root: String, - + pub checksums: HashMap } \ No newline at end of file diff --git a/manifest/src/lib.rs b/manifest/src/lib.rs index fdc8cbc..71efe31 100644 --- a/manifest/src/lib.rs +++ b/manifest/src/lib.rs @@ -1,5 +1,5 @@ use serde::{Deserialize, Serialize}; -use std::collections::HashMap; +use std::{collections::HashMap, str::FromStr}; pub mod package; pub mod fs; @@ -18,6 +18,22 @@ pub struct Manifest

> { pub pkgr: P } +impl Manifest { + pub fn valid(&self) -> bool { + if self.bin.is_none() && self.build.is_none() { + return false + } + true + } +} + +impl FromStr for Manifest { + type Err = toml::de::Error; + fn from_str(s: &str) -> Result { + toml::from_str(s) + } +} + impl Default for Manifest { fn default() -> Self { Manifest { diff --git a/mock-pkg.py b/mock-pkg.py new file mode 100644 index 0000000..57ee008 --- /dev/null +++ b/mock-pkg.py @@ -0,0 +1,22 @@ +import tarfile, os + +with open("./package.toml", mode='r') as mani: + data = mani.read() + with open("./mock.pkg", mode='wb') as pkg: + print("building header...") + pkg.write(bytes([0x01, (len(data) >> 8) & 0xFF, len(data) & 0xFF])) + print("writing manifest into pkg...") + pkg.write(data.encode("utf-8")) + with tarfile.TarFile("/tmp/pkgtar", 'w') as pkgtar: + for root, dirs, files in os.walk("."): + for file in files: + print(f"\33[2Kadd: {file}", end="\r", flush=True) + pkgtar.add(os.path.join(root, file)) + with open("/tmp/pkgtar", 'rb') as pkgtar: + print("appending pkgtar to pkg...") + pkg.write(pkgtar.read()) + print("deleting /tmp/pkgtar...") + os.unlink("/tmp/pkgtar") + print("closing write stream") + pkg.close() + \ No newline at end of file diff --git a/package.toml b/package.toml index fbc4070..54d55b5 100644 --- a/package.toml +++ b/package.toml @@ -18,10 +18,7 @@ version = 1 # this can automatically be incremented when publishing by running ` # previous version will lose this tag # - oldest # assigned to the first version. -tags = [ - "prod", - "pkgr-spec-1" -] +tags = [ "prod", "pkgr-spec-1" ] ## package.type # Supported types: @@ -39,7 +36,7 @@ arch = "x86_64" # this is automatically filled by `pkgr publish ...` # you may use the following syntax # "" or ",,..." [dependencies] -example = { arch = "x86_64", tags = [ "stable" ], version = "v1" } +#example = { arch = "x86_64", tags = [ "stable" ], version = "v1" } exnmbr2 = "v1,stable" ## fs @@ -97,14 +94,12 @@ base = "latest,stable" # selected by default # This exists so that packager is easy to install on anything! # and only 1 release channel for pkgr [pkgr.bootstrap] -# any non-zero = installed -check_installed_commands = [ - "/scripts/check_installed" -] +## any non-zero = installed +check_installed_commands = [ "/scripts/check_installed" ] # any non-zero = fail commands = [ - "/scripts/bootstrap/download_latest @version /tmp/pkgr.pkg", - "/scripts/bootstrap/dirty_install /tmp/pkgr.pkg", - "/scripts/bootstrap/check_install" + "/scripts/bootstrap/download_latest @version /tmp/pkgr.pkg", + "/scripts/bootstrap/dirty_install /tmp/pkgr.pkg", + "/scripts/bootstrap/check_install" ] diff --git a/pkg/scripts/bootstrap/download_latest b/pkg/scripts/bootstrap/download_latest new file mode 100644 index 0000000..229aaa2 --- /dev/null +++ b/pkg/scripts/bootstrap/download_latest @@ -0,0 +1,7 @@ +#!/bin/sh + +fetch_latest_version() { +} + +download_file() { +} \ No newline at end of file diff --git a/pkgfile/src/lib.rs b/pkgfile/src/lib.rs index a6b1d5e..6025cd5 100644 --- a/pkgfile/src/lib.rs +++ b/pkgfile/src/lib.rs @@ -20,17 +20,29 @@ impl TryFrom> for PKGFile { Ok(PKGFile { manifest: match String::from_utf8( value[ - 3..(manifest_size as usize) + 3..(manifest_size as usize + 3) ].to_vec() ) { Ok(s) => s, _ => return Err(()) }, - data: value[(manifest_size as usize)..].to_vec(), + data: value[(manifest_size as usize + 3)..].to_vec(), }) } _ => Err(()) } } } + +impl Into> for PKGFile { + fn into(self) -> Vec { + let mut bytes = vec![0x01]; + let manifest_bytes = self.manifest.into_bytes(); + bytes.push((manifest_bytes.len() >> 8) as u8); + bytes.push((manifest_bytes.len() | 0xFF) as u8); + bytes.extend(manifest_bytes); + bytes.extend(self.data); + bytes + } +} \ No newline at end of file