Skip to content

Commit

Permalink
Merge branch 'obhq:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
SuchAFuriousDeath authored Mar 30, 2024
2 parents a665207 + 3eb0522 commit f6e93d8
Show file tree
Hide file tree
Showing 19 changed files with 511 additions and 249 deletions.
5 changes: 2 additions & 3 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# CMake modules
include(ExternalProject)

# External dependencies.
find_package(Qt6 COMPONENTS Widgets REQUIRED)
find_package(Threads REQUIRED)
Expand Down Expand Up @@ -33,6 +30,8 @@ add_executable(obliteration WIN32 MACOSX_BUNDLE
main.cpp
main_window.cpp
path.cpp
pkg_extractor.cpp
pkg_installer.cpp
progress_dialog.cpp
resources.qrc
settings.cpp
Expand Down
14 changes: 12 additions & 2 deletions src/core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
#include <QUtf8StringView>

#include <cstddef>
#include <cstdint>

struct error;
struct param;
struct pkg;

typedef void (*pkg_extract_status_t) (const char *status, std::size_t current, std::size_t total, void *ud);
typedef void (*pkg_extract_status_t) (const char *status, std::size_t bar, std::uint64_t current, std::uint64_t total, void *ud);

extern "C" {
void error_free(error *err);
Expand Down Expand Up @@ -171,7 +172,16 @@ class Pkg final {
public:
Pkg() : m_obj(nullptr) {}
Pkg(const Pkg &) = delete;
~Pkg() { close(); }
Pkg(Pkg &&other)
{
m_obj = other.m_obj;
other.m_obj = nullptr;
}

~Pkg()
{
close();
}

public:
Pkg &operator=(const Pkg &) = delete;
Expand Down
1 change: 1 addition & 0 deletions src/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ crate-type = ["staticlib"]
[dependencies]
error = { path = "../error" }
ftp = { path = "../ftp" }
humansize = "2.1.3"
param = { path = "../param" }
pkg = { path = "../pkg" }
thiserror = "1.0"
98 changes: 92 additions & 6 deletions src/core/src/pkg.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use error::Error;
use humansize::{SizeFormatter, DECIMAL};
use param::Param;
use pkg::Pkg;
use std::ffi::{c_char, c_void, CStr};
use std::ptr::null_mut;
use pkg::{Pkg, PkgProgress};
use std::ffi::{c_char, c_void, CStr, CString};
use std::path::Path;
use std::ptr::{null, null_mut};

#[no_mangle]
pub unsafe extern "C" fn pkg_open(file: *const c_char, error: *mut *mut Error) -> *mut Pkg {
Expand Down Expand Up @@ -40,13 +42,97 @@ pub unsafe extern "C" fn pkg_get_param(pkg: &Pkg, error: *mut *mut Error) -> *mu
pub unsafe extern "C" fn pkg_extract(
pkg: &Pkg,
dir: *const c_char,
status: extern "C" fn(*const c_char, usize, usize, *mut c_void),
status: extern "C" fn(*const c_char, usize, u64, u64, *mut c_void),
ud: *mut c_void,
) -> *mut Error {
let dir = CStr::from_ptr(dir);
let root: &Path = CStr::from_ptr(dir).to_str().unwrap().as_ref();
let progress = ExtractProgress {
status,
ud,
root,
total: 0,
progress: 0,
};

match pkg.extract(dir.to_str().unwrap(), status, ud) {
match pkg.extract(root, progress) {
Ok(_) => null_mut(),
Err(e) => Error::new(e),
}
}

struct ExtractProgress<'a> {
status: extern "C" fn(*const c_char, usize, u64, u64, *mut c_void),
ud: *mut c_void,
root: &'a Path,
total: u64,
progress: u64,
}

impl<'a> PkgProgress for ExtractProgress<'a> {
fn entry_start(&mut self, path: &Path, current: usize, total: usize) {
let path = path.strip_prefix(self.root).unwrap();
let log = format!("Extracting {}", path.display());
let log = CString::new(log).unwrap();

(self.status)(
log.as_ptr(),
0,
current.try_into().unwrap(),
total.try_into().unwrap(),
self.ud,
);
}

fn entries_completed(&mut self, total: usize) {
let total = total.try_into().unwrap();

(self.status)(
c"Entries extraction completed".as_ptr(),
0,
total,
total,
self.ud,
);
}

fn pfs_start(&mut self, files: usize) {
self.total = files.try_into().unwrap();
}

fn pfs_directory(&mut self, path: &Path) {
let path = path.strip_prefix(self.root).unwrap();
let log = format!("Creating {}", path.display());
let log = CString::new(log).unwrap();

(self.status)(log.as_ptr(), 0, self.progress, self.total, self.ud);
(self.status)(null(), 1, 0, 0, self.ud);

self.progress += 1;
}

fn pfs_file(&mut self, path: &Path, len: u64) {
let path = path.strip_prefix(self.root).unwrap();
let size = SizeFormatter::new(len, DECIMAL);
let log = format!("Extracting {} ({})", path.display(), size);
let log = CString::new(log).unwrap();

(self.status)(log.as_ptr(), 0, self.progress, self.total, self.ud);
(self.status)(null(), 1, 0, len, self.ud);

self.progress += 1;
}

fn pfs_write(&mut self, current: u64, len: u64) {
(self.status)(null(), 1, current, len, self.ud);
}

fn pfs_completed(&mut self) {
(self.status)(
c"PFS extraction completed".as_ptr(),
0,
self.total,
self.total,
self.ud,
);
}
}
28 changes: 1 addition & 27 deletions src/kernel/src/fs/dev/vnode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::dirent::Dirent;
use super::{AllocVnodeError, DevFs};
use crate::errno::{Errno, EIO, ENOENT, ENOTDIR, ENXIO};
use crate::fs::{
check_access, Access, IoCmd, OpenFlags, RevokeFlags, VFile, Vnode, VnodeAttrs, VnodeItem,
check_access, Access, IoCmd, OpenFlags, RevokeFlags, VFileType, Vnode, VnodeAttrs, VnodeItem,
VnodeType,
};
use crate::process::VThread;
Expand Down Expand Up @@ -163,32 +163,6 @@ impl crate::fs::VnodeBackend for VnodeBackend {
}
}

fn open(
&self,
vn: &Arc<Vnode>,
td: Option<&VThread>,
mode: OpenFlags,
file: Option<&mut VFile>,
) -> Result<(), Box<dyn Errno>> {
if !vn.is_character() {
return Ok(());
}

// Not sure why FreeBSD check if vnode is VBLK because all of vnode here always be VCHR.
let item = vn.item();
let Some(VnodeItem::Device(dev)) = item.as_ref() else {
unreachable!();
};

// Execute switch handler.
dev.open(mode, 0x2000, td)?;

// Set file OP.
let Some(file) = file else { return Ok(()) };

todo!()
}

fn revoke(&self, vn: &Arc<Vnode>, flags: RevokeFlags) -> Result<(), Box<dyn Errno>> {
// TODO: Implement this.
todo!()
Expand Down
13 changes: 1 addition & 12 deletions src/kernel/src/fs/host/vnode.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::file::HostFile;
use super::{GetVnodeError, HostFs};
use crate::errno::{Errno, EEXIST, EIO, ENOENT, ENOTDIR};
use crate::fs::{Access, IoCmd, Mode, OpenFlags, VFile, Vnode, VnodeAttrs, VnodeType};
use crate::fs::{Access, IoCmd, Mode, OpenFlags, VFileType, Vnode, VnodeAttrs, VnodeType};
use crate::process::VThread;
use crate::ucred::{Gid, Uid};
use macros::Errno;
Expand Down Expand Up @@ -123,17 +123,6 @@ impl crate::fs::VnodeBackend for VnodeBackend {

Ok(vn)
}

#[allow(unused_variables)] // TODO: remove when implementing.
fn open(
&self,
vn: &Arc<Vnode>,
td: Option<&VThread>,
mode: OpenFlags,
file: Option<&mut VFile>,
) -> Result<(), Box<dyn Errno>> {
todo!()
}
}

/// Represents an error when [`getattr()`] fails.
Expand Down
8 changes: 7 additions & 1 deletion src/kernel/src/fs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,13 @@ impl Fs {
.lookup(path, true, td)
.map_err(OpenError::LookupFailed)?;

todo!();
let ty = if let Some(VnodeItem::Device(dev)) = vnode.item().as_ref() {
VFileType::Device(dev.clone())
} else {
VFileType::Vnode(vnode.clone())
};

Ok(VFile::new(ty))
}

pub fn lookup(
Expand Down
18 changes: 1 addition & 17 deletions src/kernel/src/fs/null/vnode.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use crate::{
errno::{Errno, EISDIR, EROFS},
fs::{
null::hash::NULL_HASHTABLE, perm::Access, Mount, MountFlags, OpenFlags, VFile, Vnode,
VnodeAttrs, VnodeType,
null::hash::NULL_HASHTABLE, perm::Access, Mount, MountFlags, Vnode, VnodeAttrs, VnodeType,
},
process::VThread,
};
Expand Down Expand Up @@ -90,21 +89,6 @@ impl crate::fs::VnodeBackend for VnodeBackend {

Ok(vnode)
}

/// This function tries to mimic what calling `null_bypass` would do.
fn open(
&self,
_: &Arc<Vnode>,
td: Option<&VThread>,
mode: OpenFlags,
file: Option<&mut VFile>,
) -> Result<(), Box<dyn Errno>> {
self.lower
.open(td, mode, file)
.map_err(OpenError::OpenFromLowerFailed)?;

Ok(())
}
}

/// See `null_nodeget` on the PS4 for a reference.
Expand Down
16 changes: 2 additions & 14 deletions src/kernel/src/fs/tmp/node.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
use super::{AllocVnodeError, TempFs};
use crate::errno::{Errno, ENOENT, ENOSPC};
use crate::fs::{Access, OpenFlags, VFile, Vnode, VnodeAttrs, VnodeType};
use crate::fs::{Access, OpenFlags, VFileType, Vnode, VnodeAttrs, VnodeType};
use crate::process::VThread;
use gmtx::{Gutex, GutexGroup, GutexWriteGuard};
use macros::Errno;
use std::collections::VecDeque;
use std::sync::{Arc, RwLock};
use thiserror::Error;

use super::{AllocVnodeError, TempFs};

/// A collection of [`Node`].
#[derive(Debug)]
pub struct Nodes {
Expand Down Expand Up @@ -202,17 +201,6 @@ impl crate::fs::VnodeBackend for VnodeBackend {

Ok(vnode)
}

#[allow(unused_variables)] // TODO: remove when implementing
fn open(
&self,
vn: &Arc<Vnode>,
td: Option<&VThread>,
mode: OpenFlags,
#[allow(unused_variables)] file: Option<&mut VFile>,
) -> Result<(), Box<dyn Errno>> {
todo!()
}
}

/// Represents an error when [`Nodes::alloc()`] fails.
Expand Down
22 changes: 1 addition & 21 deletions src/kernel/src/fs/vnode.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::{
unixify_access, Access, CharacterDevice, FileBackend, IoCmd, Mode, Mount, OpenFlags,
RevokeFlags, Stat, TruncateLength, Uio, UioMut, VFile,
RevokeFlags, Stat, TruncateLength, Uio, UioMut, VFile, VFileType,
};
use crate::arnd;
use crate::errno::{Errno, ENOTDIR, ENOTTY, EOPNOTSUPP, EPERM};
Expand Down Expand Up @@ -135,15 +135,6 @@ impl Vnode {
self.backend.mkdir(self, name, mode, td)
}

pub fn open(
self: &Arc<Self>,
td: Option<&VThread>,
mode: OpenFlags,
file: Option<&mut VFile>,
) -> Result<(), Box<dyn Errno>> {
self.backend.open(self, td, mode, file)
}

pub fn revoke(self: &Arc<Self>, flags: RevokeFlags) -> Result<(), Box<dyn Errno>> {
self.backend.revoke(self, flags)
}
Expand Down Expand Up @@ -300,17 +291,6 @@ pub(super) trait VnodeBackend: Debug + Send + Sync + 'static {
Err(Box::new(DefaultError::NotSupported))
}

/// An implementation of `vop_open`.
fn open(
&self,
#[allow(unused_variables)] vn: &Arc<Vnode>,
#[allow(unused_variables)] td: Option<&VThread>,
#[allow(unused_variables)] mode: OpenFlags,
#[allow(unused_variables)] file: Option<&mut VFile>,
) -> Result<(), Box<dyn Errno>> {
Ok(())
}

/// An implementation of `vop_revoke`.
fn revoke(
&self,
Expand Down
11 changes: 7 additions & 4 deletions src/kernel/src/rtld/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -461,13 +461,17 @@ impl RuntimeLinker {

fn sys_dynlib_load_prx(self: &Arc<Self>, td: &VThread, i: &SysIn) -> Result<SysOut, SysErr> {
// Check if application is a dynamic SELF.
let mut bin = td.proc().bin_mut();
let bin = bin.as_mut().ok_or(SysErr::Raw(EPERM))?;
let mut bin_guard = td.proc().bin_mut();
let bin = bin_guard.as_mut().ok_or(SysErr::Raw(EPERM))?;

let sdk_ver = bin.app().sdk_ver();

if bin.app().file_info().is_none() {
return Err(SysErr::Raw(EPERM));
}

drop(bin_guard);

// Not sure what is this. Maybe kernel only flags?
let mut flags: u32 = i.args[1].try_into().unwrap();

Expand Down Expand Up @@ -544,8 +548,7 @@ impl RuntimeLinker {
let resolver = SymbolResolver::new(
&mains,
&globals,
bin.app().sdk_ver() >= 0x5000000
|| self.flags.read().contains(LinkerFlags::HAS_ASAN),
sdk_ver >= 0x5000000 || self.flags.read().contains(LinkerFlags::HAS_ASAN),
);

self.init_dag(&md);
Expand Down
Loading

0 comments on commit f6e93d8

Please sign in to comment.