diff --git a/fs/internal/ffi/fs_native.mbt b/fs/internal/ffi/fs_native.mbt index 914764b..55cd2c3 100644 --- a/fs/internal/ffi/fs_native.mbt +++ b/fs/internal/ffi/fs_native.mbt @@ -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) } @@ -30,15 +30,15 @@ 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) } ///| @@ -46,7 +46,7 @@ extern "C" fn write_bytes_to_file_ffi(path : Bytes, content : Bytes) = "write_by ///| 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 } ///| @@ -54,7 +54,7 @@ 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) } @@ -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) } @@ -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 } @@ -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 } @@ -91,7 +91,7 @@ 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)) } ///| @@ -99,7 +99,7 @@ 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) } @@ -107,7 +107,7 @@ pub fn remove_file(path : String) -> Unit { 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 @@ -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) } diff --git a/fs/internal/ffi/native_stub.c b/fs/internal/ffi/native_stub.c index 04b83e7..8afd2c0 100644 --- a/fs/internal/ffi/native_stub.c +++ b/fs/internal/ffi/native_stub.c @@ -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; } @@ -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); }