Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

internal: tweak fs test to adapt windows #80

Merged
merged 1 commit into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 17 additions & 15 deletions fs/internal/ffi/fs_native.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@

///|
pub fn read_file_to_string(path : String) -> String {
let path_bytes = mbt_string_to_utf8_bytes(path)
let path_bytes = mbt_string_to_utf8_bytes(path, true)
let res = read_file_to_bytes_ffi(path_bytes)
utf8_bytes_to_mbt_string(res)
}

///|
pub fn read_file_to_bytes(path : String) -> Bytes {
let path_bytes = mbt_string_to_utf8_bytes(path)
let path_bytes = mbt_string_to_utf8_bytes(path, true)
read_file_to_bytes_ffi(path_bytes)
}

Expand All @@ -30,31 +30,31 @@ extern "C" fn read_file_to_bytes_ffi(path : Bytes) -> Bytes = "read_file_to_byte

///|
pub fn write_string_to_file(path : String, content : String) -> Unit {
let path_bytes = mbt_string_to_utf8_bytes(path)
let content_bytes = mbt_string_to_utf8_bytes(content)
let path_bytes = mbt_string_to_utf8_bytes(path, true)
let content_bytes = mbt_string_to_utf8_bytes(content, false)
write_bytes_to_file_ffi(path_bytes, content_bytes)
}

///|
pub fn write_bytes_to_file(path : String, content : Bytes) -> Unit {
let path = mbt_string_to_utf8_bytes(path)
write_bytes_to_file_ffi(path, content)
let path_bytes = mbt_string_to_utf8_bytes(path, true)
write_bytes_to_file_ffi(path_bytes, content)
}

///|
extern "C" fn write_bytes_to_file_ffi(path : Bytes, content : Bytes) = "write_bytes_to_file"

///|
pub fn path_exists(path : String) -> Bool {
path_exists_ffi(mbt_string_to_utf8_bytes(path)) == 0
path_exists_ffi(mbt_string_to_utf8_bytes(path, true)) == 0
}

///|
extern "C" fn path_exists_ffi(path : Bytes) -> Int = "path_exists"

///|
pub fn read_dir(path : String) -> Array[String] {
let path_bytes = mbt_string_to_utf8_bytes(path)
let path_bytes = mbt_string_to_utf8_bytes(path, true)
let res = read_dir_ffi(path_bytes).map(utf8_bytes_to_mbt_string)
Array::from_fixed_array(res)
}
Expand All @@ -64,7 +64,7 @@ extern "C" fn read_dir_ffi(path : Bytes) -> FixedArray[Bytes] = "read_dir"

///|
pub fn create_dir(path : String) -> Unit {
let path_bytes = mbt_string_to_utf8_bytes(path)
let path_bytes = mbt_string_to_utf8_bytes(path, true)
create_dir_ffi(path_bytes)
}

Expand All @@ -73,7 +73,7 @@ extern "C" fn create_dir_ffi(path : Bytes) = "create_dir"

///|
pub fn is_dir(path : String) -> Bool {
let path_bytes = mbt_string_to_utf8_bytes(path)
let path_bytes = mbt_string_to_utf8_bytes(path, true)
is_dir_ffi(path_bytes) == 0
}

Expand All @@ -82,7 +82,7 @@ extern "C" fn is_dir_ffi(path : Bytes) -> Int = "is_dir"

///|
pub fn is_file(path : String) -> Bool {
let path_bytes = mbt_string_to_utf8_bytes(path)
let path_bytes = mbt_string_to_utf8_bytes(path, true)
is_file_ffi(path_bytes) == 0
}

Expand All @@ -91,23 +91,23 @@ extern "C" fn is_file_ffi(path : Bytes) -> Int = "is_file"

///|
pub fn remove_dir(path : String) -> Unit {
remove_dir_ffi(mbt_string_to_utf8_bytes(path))
remove_dir_ffi(mbt_string_to_utf8_bytes(path, true))
}

///|
extern "C" fn remove_dir_ffi(path : Bytes) = "remove_dir"

///|
pub fn remove_file(path : String) -> Unit {
let path_bytes = mbt_string_to_utf8_bytes(path)
let path_bytes = mbt_string_to_utf8_bytes(path, true)
remove_file_ffi(path_bytes)
}

///|
extern "C" fn remove_file_ffi(path : Bytes) = "remove_file"

///|
fn mbt_string_to_utf8_bytes(str : String) -> Bytes {
fn mbt_string_to_utf8_bytes(str : String, is_filename : Bool) -> Bytes {
let res : Array[Byte] = []
let len = str.length()
let mut i = 0
Expand Down Expand Up @@ -138,7 +138,9 @@ fn mbt_string_to_utf8_bytes(str : String) -> Bytes {
}
i = i + 1
}
res.push((0).to_byte())
if is_filename {
res.push((0).to_byte())
}
Bytes::from_array(res)
}

Expand Down
23 changes: 6 additions & 17 deletions fs/internal/ffi/native_stub.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,34 +49,22 @@ struct moonbit_bytes* read_file_to_bytes(struct moonbit_bytes *filename) {
return NULL;
}

struct moonbit_bytes* bytes = moonbit_make_bytes(size, 0);

// allocate memory to store the file content
char *buffer = (char *)malloc((size_t)size);
if (buffer == NULL) {
perror("malloc");
fclose((FILE *)(filename->data));
return NULL;
}

// read the file content into the buffer
size_t bytes_read = fread(buffer, 1, (size_t)size, file);
// read the file content into the bytes->data
size_t bytes_read = fread(bytes->data, 1, (size_t)size, file);
if (bytes_read != (size_t)size) {
perror("fread");
free(buffer);
fclose((FILE *)(filename->data));
fclose(file);
return NULL;
}

// close the file
if (fclose(file) != 0) {
perror("fclose");
free(buffer);
return NULL;
}

struct moonbit_bytes* bytes = moonbit_make_bytes(size, 0);
memcpy(bytes->data, buffer, size);

return bytes;
}

Expand Down Expand Up @@ -235,7 +223,8 @@ void remove_file(struct moonbit_bytes* path) {

void write_bytes_to_file(struct moonbit_bytes* path, struct moonbit_bytes* content) {
FILE *file = fopen((const char *)(path->data), "wb");
size_t content_size = strlen((const char *)(content->data));
size_t content_size = content->header.arr.len;
fwrite(content->data, 1, content_size, file);
fflush(file);
fclose(file);
}
Loading