The related code/working directory of this example resides in directory {WAMR_DIR}/samples/file
This sample demonstrates the supported file interaction API of WASI. This sample can also demonstrate the SGX IPFS (Intel Protected File System), enabling an enclave to seal and unseal data at rest.
Preparation
Please install WASI SDK, download the wasi-sdk release and extract the archive to default path /opt/wasi-sdk. For testing with SGX IPFS, follow the instructions in the documentation of SGX for WAMR.
Build the sample
mkdirbuildcdbuildcmake..make
The WebAssembly application is the file located at wasm-app/file.wasm.
Run workload
Either use iwasm-sample for Linux, or enclave-sample for Intel SGX to run the sample, with the argument to allow the file system interaction with the current folder (--dir=.).
The output with Linux and POSIX is like:
Openingafile..[Test] File opening passed.Writingtothefile..[Test] File writing passed.Movingthecursortothestartofthefile..Readingfromthefile,upto1000characters..Textread:Hello,world![Test] Filereadingpassed.Determinewhetherwereachtheendofthefile..Istheendoffile?1[Test] End of file detection passed.Gettingtheplaintextsize..Theplaintextsizeis13.[Test] Retrieving file offset passed.Forceactualwriteofallthecacheddatatothedisk..[Test] Retrieving file offset passed.Writing5charactersatoffset7..Filecurrentoffset:13[Test] Writing at specified offset passed.Reading5charactersatoffset7..Textread:JamesFilecurrentoffset:13[Test] Reading at specified offset passed.Allocatemorespacetothefile..Filecurrentoffset:13Movingtotheend..Filecurrentoffset:23[Test] Allocation or more space passed.Extendthefilesizeof10bytesusingftruncate..Filecurrentoffset:23Movingtotheend..Filecurrentoffset:33[Test] Extension of the file size passed.Closingfromthefile..[Test] Closing file passed.Gettingthesizeofthefileondisk..Thefilesizeis33.Allthetestspassed!
The output with SGX and IPFS is like:
Openingafile..[Test] File opening passed.Writingtothefile..[Test] File writing passed.Movingthecursortothestartofthefile..Readingfromthefile,upto1000characters..Textread:Hello,world![Test] Filereadingpassed.Determinewhetherwereachtheendofthefile..Istheendoffile?1[Test] End of file detection passed.Gettingtheplaintextsize..Theplaintextsizeis13.[Test] Retrieving file offset passed.Forceactualwriteofallthecacheddatatothedisk..[Test] Retrieving file offset passed.Writing5charactersatoffset7..Filecurrentoffset:13[Test] Writing at specified offset passed.Reading5charactersatoffset7..Textread:JamesFilecurrentoffset:13[Test] Reading at specified offset passed.Allocatemorespacetothefile..Filecurrentoffset:23Movingtotheend..Filecurrentoffset:23[Test] Allocation or more space passed.Extendthefilesizeof10bytesusingftruncate..Filecurrentoffset:23Movingtotheend..Filecurrentoffset:33[Test] Extension of the file size passed.Closingfromthefile..[Test] Closing file passed.Gettingthesizeofthefileondisk..Thefilesizeis4096.Allthetestspassed!