mirror of
https://github.com/bytecodealliance/wasm-micro-runtime.git
synced 2025-05-14 05:31:13 +00:00
linux-sgx: Use non-destructive modes for opening files using SGX IPFS (#1645)
Refer to https://github.com/bytecodealliance/wasm-micro-runtime/pull/1645
This commit is contained in:
parent
15d645476a
commit
264fdfafed
|
@ -260,22 +260,27 @@ ipfs_fopen(int fd, const char *filename, int flags)
|
||||||
bool write_only = (flags & O_ACCMODE) == O_WRONLY;
|
bool write_only = (flags & O_ACCMODE) == O_WRONLY;
|
||||||
bool read_write = (flags & O_ACCMODE) == O_RDWR;
|
bool read_write = (flags & O_ACCMODE) == O_RDWR;
|
||||||
|
|
||||||
// The mapping of the mode are described in the table in the official
|
// The mapping of the mode is similar to the table in the official
|
||||||
// specifications:
|
// specifications:
|
||||||
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/fopen.html
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/fopen.html
|
||||||
|
// Note that POSIX has obtained a file descriptor beforehand.
|
||||||
|
// If opened with a destructive mode ("w" or "w+"), the truncate operation
|
||||||
|
// already occurred and must not be repeated because this will invalidate
|
||||||
|
// the file descriptor obtained by POSIX. Therefore, we do NOT map to the
|
||||||
|
// modes that truncate the file ("w" and "w+"). Instead, we map to a
|
||||||
|
// non-destructive mode ("r+").
|
||||||
|
|
||||||
if (read_only)
|
if (read_only)
|
||||||
mode = "r";
|
mode = "r";
|
||||||
else if (write_only && must_create && must_truncate)
|
else if (write_only && must_create && must_truncate)
|
||||||
mode = "w";
|
// Rather than "w", we map to a non-destructive mode
|
||||||
|
mode = "r+";
|
||||||
else if (write_only && must_create && must_append)
|
else if (write_only && must_create && must_append)
|
||||||
mode = "a";
|
mode = "a";
|
||||||
else if (read_write && must_create && must_truncate)
|
|
||||||
mode = "w+";
|
|
||||||
else if (read_write && must_create && must_append)
|
else if (read_write && must_create && must_append)
|
||||||
mode = "a+";
|
mode = "a+";
|
||||||
else if (read_write && must_create)
|
|
||||||
mode = "w+";
|
|
||||||
else if (read_write)
|
else if (read_write)
|
||||||
|
// Rather than "w+", we map to a non-destructive mode
|
||||||
mode = "r+";
|
mode = "r+";
|
||||||
else
|
else
|
||||||
mode = NULL;
|
mode = NULL;
|
||||||
|
|
|
@ -26,6 +26,7 @@ main(int argc, char **argv)
|
||||||
const char *text = FILE_TEXT;
|
const char *text = FILE_TEXT;
|
||||||
char buffer[1000];
|
char buffer[1000];
|
||||||
int ret;
|
int ret;
|
||||||
|
long long stat_size;
|
||||||
|
|
||||||
// Test: File opening (fopen)
|
// Test: File opening (fopen)
|
||||||
printf("Opening a file..\n");
|
printf("Opening a file..\n");
|
||||||
|
@ -114,18 +115,26 @@ main(int argc, char **argv)
|
||||||
assert(ftell(file) == strlen(text) + 2 * ADDITIONAL_SPACE);
|
assert(ftell(file) == strlen(text) + 2 * ADDITIONAL_SPACE);
|
||||||
printf("[Test] Extension of the file size passed.\n");
|
printf("[Test] Extension of the file size passed.\n");
|
||||||
|
|
||||||
|
// Display some debug information
|
||||||
|
printf("Getting the size of the file on disk..\n");
|
||||||
|
struct stat st;
|
||||||
|
stat(PATH_TEST_FILE, &st);
|
||||||
|
stat_size = st.st_size;
|
||||||
|
assert(stat_size != 0);
|
||||||
|
|
||||||
|
// Compare with the size from fstat
|
||||||
|
fstat(fileno(file), &st);
|
||||||
|
printf("The file size is: %lld (stat), %lld (fstat).\n", stat_size,
|
||||||
|
st.st_size);
|
||||||
|
assert(stat_size != 0);
|
||||||
|
assert(stat_size == st.st_size);
|
||||||
|
|
||||||
// Test: closing the file (fclose)
|
// Test: closing the file (fclose)
|
||||||
printf("Closing from the file..\n");
|
printf("Closing from the file..\n");
|
||||||
ret = fclose(file);
|
ret = fclose(file);
|
||||||
assert(ret == 0);
|
assert(ret == 0);
|
||||||
printf("[Test] Closing file passed.\n");
|
printf("[Test] Closing file passed.\n");
|
||||||
|
|
||||||
// Display some debug information
|
|
||||||
printf("Getting the size of the file on disk..\n");
|
|
||||||
struct stat st;
|
|
||||||
stat(PATH_TEST_FILE, &st);
|
|
||||||
printf("The file size is %lld.\n", st.st_size);
|
|
||||||
|
|
||||||
printf("All the tests passed!\n");
|
printf("All the tests passed!\n");
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user