fixing code validation, and also added rename instance
This commit is contained in:
@@ -114,7 +114,7 @@ fn refresh(agent: &Agent, refresh: &str) -> anyhow::Result<(String, String)> {
|
||||
}
|
||||
fn poll(agent: &Agent, device: &Device) -> anyhow::Result<(String, String)> {
|
||||
println!(
|
||||
"Go to {} and enter code: {}",
|
||||
"Go to {} and enter code: {}.",
|
||||
device.verification_uri, device.user_code
|
||||
);
|
||||
loop {
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
use anyhow::Ok;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::{
|
||||
fs::create_dir_all,
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
|
||||
use crate::util::is_valid_name;
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct Instance {
|
||||
pub version: String,
|
||||
@@ -18,8 +19,8 @@ pub struct Loader {
|
||||
}
|
||||
|
||||
pub fn create(instances_dir: &Path, name: &str, version: &str) -> anyhow::Result<PathBuf> {
|
||||
if name.contains('/') {
|
||||
anyhow::bail!("unusable characters in path.");
|
||||
if !is_valid_name(name) {
|
||||
anyhow::bail!("invalid instance name: {name:?}");
|
||||
}
|
||||
let directory = instances_dir.join(name);
|
||||
if directory.join("instance.json").exists() {
|
||||
@@ -63,6 +64,9 @@ pub fn list(root: &Path) -> anyhow::Result<Vec<String>> {
|
||||
Ok(out)
|
||||
}
|
||||
pub fn remove(instances_dir: &Path, name: &str, yes: bool) -> anyhow::Result<()> {
|
||||
if !is_valid_name(name) {
|
||||
anyhow::bail!("invalid instance name: {name:?}");
|
||||
}
|
||||
let dir = instances_dir.join(&name);
|
||||
if !dir.exists() {
|
||||
anyhow::bail!("no instance named {name}");
|
||||
@@ -77,7 +81,25 @@ pub fn remove(instances_dir: &Path, name: &str, yes: bool) -> anyhow::Result<()>
|
||||
}
|
||||
}
|
||||
std::fs::remove_dir_all(&dir)?;
|
||||
println!("Removed {name}.");
|
||||
Ok(())
|
||||
}
|
||||
pub fn rename(instances_dir: &Path, old_name: &str, new_name: &str) -> anyhow::Result<()> {
|
||||
if !is_valid_name(old_name) {
|
||||
anyhow::bail!("invalid original instance name: {old_name:?}");
|
||||
}
|
||||
if !is_valid_name(new_name) {
|
||||
anyhow::bail!("invalid new instance name: {new_name:?}");
|
||||
}
|
||||
let instance_dir = instances_dir.join(&old_name);
|
||||
let new_dir = instances_dir.join(&new_name);
|
||||
if !instance_dir.exists() {
|
||||
anyhow::bail!("original instance name doesn't exist");
|
||||
}
|
||||
if new_dir.exists() {
|
||||
anyhow::bail!("new instance name already exists");
|
||||
}
|
||||
std::fs::rename(instance_dir, new_dir)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
pub fn sanitize(name: &str) -> String {
|
||||
|
||||
20
src/main.rs
20
src/main.rs
@@ -6,13 +6,18 @@ mod launch;
|
||||
mod meta;
|
||||
mod mrpack;
|
||||
mod prism;
|
||||
use crate::{launch::run, meta::fetch_version};
|
||||
mod util;
|
||||
use clap::{Parser, Subcommand};
|
||||
use launch::run;
|
||||
use meta::fetch_version;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
/// Untitled Minecraft Launcher
|
||||
#[derive(Parser)]
|
||||
#[command(name = "uml")]
|
||||
#[command(
|
||||
after_help = "For any instance names with spaces, other than the launch command, enclose it in double quotes."
|
||||
)]
|
||||
struct Cli {
|
||||
#[command(subcommand)]
|
||||
command: Command,
|
||||
@@ -47,6 +52,11 @@ enum InstanceCmd {
|
||||
#[arg(long)]
|
||||
version: String,
|
||||
},
|
||||
/// Rename an instance
|
||||
Rename {
|
||||
current_name: String,
|
||||
new_name: String,
|
||||
},
|
||||
/// Import a Modrinth modpack
|
||||
Import {
|
||||
pack: PathBuf,
|
||||
@@ -104,6 +114,13 @@ fn main() -> anyhow::Result<()> {
|
||||
}
|
||||
instance::create(&instances_dir, &name, &version)?;
|
||||
}
|
||||
InstanceCmd::Rename {
|
||||
current_name,
|
||||
new_name,
|
||||
} => {
|
||||
instance::rename(&instances_dir, ¤t_name, &new_name)?;
|
||||
println!("Renamed {} to {}.", current_name, new_name);
|
||||
}
|
||||
InstanceCmd::Import { pack, name, yes } => {
|
||||
mrpack::import(&pack, &instances_dir, name.as_deref(), yes)?;
|
||||
}
|
||||
@@ -112,6 +129,7 @@ fn main() -> anyhow::Result<()> {
|
||||
}
|
||||
InstanceCmd::Remove { name, yes } => {
|
||||
instance::remove(&instances_dir, &name, yes)?;
|
||||
println!("Removed {name}.");
|
||||
}
|
||||
InstanceCmd::List => {
|
||||
for n in instance::list(&instances_dir)? {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use crate::{
|
||||
download::download,
|
||||
download::verify_sha512,
|
||||
download::{download, verify_sha512},
|
||||
instance::{Loader, create, load, save},
|
||||
util::is_safe_relative_path,
|
||||
};
|
||||
use indicatif::{ProgressBar, ProgressStyle};
|
||||
use serde::Deserialize;
|
||||
@@ -51,10 +51,9 @@ fn extract_overrides(
|
||||
if rel.is_empty() || entry.is_dir() {
|
||||
continue;
|
||||
}
|
||||
if rel.contains("..") || rel.starts_with('/') {
|
||||
if !is_safe_relative_path(rel) {
|
||||
anyhow::bail!("suspicious path in pack: {name}");
|
||||
}
|
||||
|
||||
let dest = instance_dir.join(rel);
|
||||
if let Some(parent) = dest.parent() {
|
||||
std::fs::create_dir_all(parent)?;
|
||||
@@ -146,7 +145,7 @@ pub fn import(
|
||||
pb.set_style(ProgressStyle::with_template("{bar:40} {pos}/{len} {msg}").unwrap());
|
||||
|
||||
for f in &wanted {
|
||||
if f.path.contains("..") || f.path.starts_with('/') {
|
||||
if !is_safe_relative_path(&f.path) {
|
||||
anyhow::bail!("suspicious path in pack: {}", f.path);
|
||||
}
|
||||
pb.set_message(f.path.clone());
|
||||
|
||||
15
src/util.rs
Normal file
15
src/util.rs
Normal file
@@ -0,0 +1,15 @@
|
||||
use std::path::Path;
|
||||
|
||||
pub fn is_valid_name(name: &str) -> bool {
|
||||
let mut components = Path::new(name).components();
|
||||
matches!(
|
||||
(components.next(), components.next()),
|
||||
(Some(std::path::Component::Normal(_)), None)
|
||||
)
|
||||
}
|
||||
pub fn is_safe_relative_path(path: &str) -> bool {
|
||||
let p = Path::new(path);
|
||||
!p.as_os_str().is_empty()
|
||||
&& p.components()
|
||||
.all(|c| matches!(c, std::path::Component::Normal(_)))
|
||||
}
|
||||
Reference in New Issue
Block a user