Skip to content

Commit

Permalink
Implement File.copy
Browse files Browse the repository at this point in the history
  • Loading branch information
yorickpeterse committed Aug 31, 2024
1 parent c0fa48c commit 028f23d
Show file tree
Hide file tree
Showing 16 changed files with 526 additions and 177 deletions.
69 changes: 0 additions & 69 deletions rt/src/runtime/fs.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,8 @@
use crate::mem::String as InkoString;
use crate::process::ProcessPointer;
use crate::result::Result as InkoResult;
use crate::state::State;
use std::fs::{self};
use std::path::PathBuf;

#[no_mangle]
pub unsafe extern "system" fn inko_file_copy(
process: ProcessPointer,
from: *const InkoString,
to: *const InkoString,
) -> InkoResult {
process
.blocking(|| fs::copy(InkoString::read(from), InkoString::read(to)))
.map(|size| InkoResult::ok(size as _))
.unwrap_or_else(InkoResult::io_error)
}

#[no_mangle]
pub unsafe extern "system" fn inko_file_remove(
process: ProcessPointer,
path: *const InkoString,
) -> InkoResult {
process
.blocking(|| fs::remove_file(InkoString::read(path)))
.map(|_| InkoResult::none())
.unwrap_or_else(InkoResult::io_error)
}

#[no_mangle]
pub unsafe extern "system" fn inko_path_expand(
state: *const State,
Expand All @@ -43,47 +18,3 @@ pub unsafe extern "system" fn inko_path_expand(
})
.unwrap_or_else(InkoResult::io_error)
}

#[no_mangle]
pub unsafe extern "system" fn inko_directory_create(
process: ProcessPointer,
path: *const InkoString,
) -> InkoResult {
process
.blocking(|| fs::create_dir(InkoString::read(path)))
.map(|_| InkoResult::none())
.unwrap_or_else(InkoResult::io_error)
}

#[no_mangle]
pub unsafe extern "system" fn inko_directory_create_recursive(
process: ProcessPointer,
path: *const InkoString,
) -> InkoResult {
process
.blocking(|| fs::create_dir_all(InkoString::read(path)))
.map(|_| InkoResult::none())
.unwrap_or_else(InkoResult::io_error)
}

#[no_mangle]
pub unsafe extern "system" fn inko_directory_remove(
process: ProcessPointer,
path: *const InkoString,
) -> InkoResult {
process
.blocking(|| fs::remove_dir(InkoString::read(path)))
.map(|_| InkoResult::none())
.unwrap_or_else(InkoResult::io_error)
}

#[no_mangle]
pub unsafe extern "system" fn inko_directory_remove_recursive(
process: ProcessPointer,
path: *const InkoString,
) -> InkoResult {
process
.blocking(|| fs::remove_dir_all(InkoString::read(path)))
.map(|_| InkoResult::none())
.unwrap_or_else(InkoResult::io_error)
}
1 change: 1 addition & 0 deletions std/fixtures/hello.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello
3 changes: 3 additions & 0 deletions std/src/std/fs.inko
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ class pub Metadata {
# The type of the file.
let pub @type: FileType

# The ownership/mode of the file.
let @mode: Int

# The size of the file in bytes.
let pub @size: Int

Expand Down
42 changes: 37 additions & 5 deletions std/src/std/fs/file.inko
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,13 @@ class pub ReadOnlyFile {
# ```
fn pub static new(path: Path) -> Result[ReadOnlyFile, Error] {
match
sys.open_file(path.to_string, read: true, write: false, append: false)
sys.open_file(
path.to_string,
read: true,
write: false,
append: false,
truncate: false,
)
{
case Ok(fd) -> Result.Ok(ReadOnlyFile(fd: fd, path: path))
case Error(e) -> Result.Error(e)
Expand Down Expand Up @@ -113,7 +119,13 @@ class pub WriteOnlyFile {
# ```
fn pub static new(path: Path) -> Result[WriteOnlyFile, Error] {
match
sys.open_file(path.to_string, read: false, write: true, append: false)
sys.open_file(
path.to_string,
read: false,
write: true,
append: false,
truncate: true,
)
{
case Ok(fd) -> Result.Ok(WriteOnlyFile(fd: fd, path: path))
case Error(e) -> Result.Error(e)
Expand All @@ -131,7 +143,13 @@ class pub WriteOnlyFile {
# ```
fn pub static append(path: Path) -> Result[WriteOnlyFile, Error] {
match
sys.open_file(path.to_string, read: false, write: true, append: true)
sys.open_file(
path.to_string,
read: false,
write: true,
append: true,
truncate: false,
)
{
case Ok(fd) -> Result.Ok(WriteOnlyFile(fd: fd, path: path))
case Error(e) -> Result.Error(e)
Expand Down Expand Up @@ -213,7 +231,13 @@ class pub ReadWriteFile {
# ```
fn pub static new(path: Path) -> Result[ReadWriteFile, Error] {
match
sys.open_file(path.to_string, read: true, write: true, append: false)
sys.open_file(
path.to_string,
read: true,
write: true,
append: false,
truncate: false,
)
{
case Ok(fd) -> Result.Ok(ReadWriteFile(fd: fd, path: path))
case Error(e) -> Result.Error(e)
Expand All @@ -230,7 +254,15 @@ class pub ReadWriteFile {
# ReadWriteFile.append('/dev/null'.to_path)
# ```
fn pub static append(path: Path) -> Result[ReadWriteFile, Error] {
match sys.open_file(path.to_string, read: true, write: true, append: true) {
match
sys.open_file(
path.to_string,
read: true,
write: true,
append: true,
truncate: false,
)
{
case Ok(fd) -> Result.Ok(ReadWriteFile(fd: fd, path: path))
case Error(e) -> Result.Error(e)
}
Expand Down
Loading

0 comments on commit 028f23d

Please sign in to comment.