give read _and_ write access to data dir capability, so that we can both read and create files inside it. (warning for when we implement delete/rename etc: Write access to a directory shouldn't permit deleting/renaming it, just to delete/rename stuff inside it)

This commit is contained in:
Martin Fouilleul 2023-06-23 17:26:19 +02:00
parent da2b0867a6
commit 1f75f40536
6 changed files with 21 additions and 55 deletions

@ -1 +1 @@
Subproject commit 18c793dbb4fac59facc1877fbb04ea889ef2b895 Subproject commit 28e0a6c88e3fb272d376d458a875300bb1531e23

View File

@ -12,4 +12,4 @@ wasmFlags="--target=wasm32 \
/usr/local/opt/llvm/bin/clang $wasmFlags -o ./module.wasm ../../sdk/orca.c src/main.c /usr/local/opt/llvm/bin/clang $wasmFlags -o ./module.wasm ../../sdk/orca.c src/main.c
python3 ../../scripts/mkapp.py --orca-dir ../.. --name Pong --icon icon.png --data-dir dir1 module.wasm python3 ../../scripts/mkapp.py --orca-dir ../.. --name Pong --icon icon.png --data-file data/ball.png module.wasm

BIN
samples/pong/data/ball.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 490 KiB

View File

@ -32,12 +32,7 @@ bool rightDown = false;
mg_canvas canvas; mg_canvas canvas;
mg_surface surface; mg_surface surface;
#define TEST_IMAGE 1
#ifdef TEST_IMAGE
mg_image image; mg_image image;
#endif
mg_surface mg_surface_main(void); mg_surface mg_surface_main(void);
@ -47,57 +42,32 @@ void OnInit(void)
surface = mg_surface_main(); surface = mg_surface_main();
canvas = mg_canvas_create(); canvas = mg_canvas_create();
#ifdef TEST_IMAGE
// create an image with a checkerboard pattern //NOTE: file test
u8 pixels[11*11*4]; file_handle file = file_open(STR8("/ball.png"), FILE_ACCESS_READ, 0);
for(int i=0; i<11*11/2; i++) if(file_last_error(file) != IO_OK)
{ {
pixels[8*i] = 0; log_error("Couldn't open file ball.png\n");
pixels[8*i+1] = 0;
pixels[8*i+2] = 0;
pixels[8*i+3] = 255;
pixels[8*i+4] = 255;
pixels[8*i+5] = 255;
pixels[8*i+6] = 255;
pixels[8*i+7] = 255;
} }
image = mg_image_create_from_rgba8(surface, 11, 11, pixels);
/*TODO Once we have file io and stb_image:
file_handle file = file_open(STR8("test.png"), FILE_OPEN_READ);
u64 size = file_size(file);
u8* data = mem_arena_alloc_array(mem_scratch(), u8, size);
file_read(file, size, data);
file_close(file);
image = mg_image_create_from_data(surface, size, data); // --> that will call stbi_load_from_memory(), see milepost/src/graphics_common.c
mem_arena_clear(mem_scratch());
*/
#endif // TEST_IMAGE
//NOTE: testing file io
file_handle file = file_open(STR8("/test_write.txt"), FILE_ACCESS_WRITE, FILE_OPEN_CREATE);
if(file_last_error(file) == IO_OK)
{
str8 string = STR8("Hello, file!\n");
file_write(file, string.len, string.ptr);
file_close(file);
}
else
{
log_error("Couldn't open file test_write.txt\n");
}
file = file_open(STR8("/dir1/test_read.txt"), FILE_ACCESS_READ, 0);
u64 size = file_size(file); u64 size = file_size(file);
char* buffer = mem_arena_alloc(mem_scratch(), size); char* buffer = mem_arena_alloc(mem_scratch(), size);
file_read(file, size, buffer); file_read(file, size, buffer);
file_close(file); file_close(file);
log_info("read file: %.*s", (int)size, buffer); file = file_open(STR8("/test.txt"), FILE_ACCESS_WRITE, FILE_OPEN_CREATE);
if(file_last_error(file) != IO_OK)
{
log_error("Couldn't open/create file test.txt\n");
}
str8 test_string = STR8("Hello, world\n");
file_write(file, test_string.len, test_string.ptr);
file_close(file);
/*NOTE: Do this when we can compile stb to wasm
image = mg_image_create_from_data(surface, str8_from_buffer(size, buffer), false);
*/
mem_arena_clear(mem_scratch());
} }
void OnFrameResize(u32 width, u32 height) void OnFrameResize(u32 width, u32 height)
@ -216,12 +186,8 @@ void OnFrameRefresh(void)
mg_set_color(paddleColor); mg_set_color(paddleColor);
mg_rectangle_fill(paddle.x, paddle.y, paddle.w, paddle.h); mg_rectangle_fill(paddle.x, paddle.y, paddle.w, paddle.h);
#ifdef TEST_IMAGE
mg_image_draw(image, ball);
#else
mg_set_color(ballColor); mg_set_color(ballColor);
mg_circle_fill(ball.x+ball.w/2, ball.y + ball.w/2, ball.w/2.); mg_circle_fill(ball.x+ball.w/2, ball.y + ball.w/2, ball.w/2.);
#endif
mg_matrix_pop(); mg_matrix_pop();

View File

@ -441,7 +441,7 @@ void* orca_runloop(void* user)
str8 localRootPath = path_executable_relative(mem_scratch(), STR8("../app/data")); str8 localRootPath = path_executable_relative(mem_scratch(), STR8("../app/data"));
io_req req = {.op = IO_OP_OPEN_AT, io_req req = {.op = IO_OP_OPEN_AT,
.open.rights = FILE_ACCESS_READ, .open.rights = FILE_ACCESS_READ|FILE_ACCESS_WRITE,
.size = localRootPath.len, .size = localRootPath.len,
.buffer = localRootPath.ptr}; .buffer = localRootPath.ptr};
io_cmp cmp = io_wait_single_req_with_table(&req, &app->fileTable); io_cmp cmp = io_wait_single_req_with_table(&req, &app->fileTable);