Skip to content

Commit e2a1076

Browse files
authored
Update shared files example to use writeable shared files (#87)
* Update shared files example * Formattinge
1 parent 217f20a commit e2a1076

File tree

1 file changed

+37
-21
lines changed

1 file changed

+37
-21
lines changed

func/demo/shared_file.cpp

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,54 @@
1-
#include "faasm/faasm.h"
21
#include "faasm/files.h"
3-
#include "faasm/input.h"
42

53
#include <cstring>
64
#include <errno.h>
75
#include <stdio.h>
86
#include <sys/stat.h>
97

10-
/**
11-
* Test reading a shared file
12-
*/
8+
#define FILE_PATH "faasm://some_dir/test_file.txt"
9+
#define FILE_DIR "faasm://some_dir"
10+
1311
int main(int argc, char* argv[])
1412
{
15-
const char* inputStr = faasm::getStringInput("");
16-
17-
// First stat the file to make sure it exists
18-
struct stat64 res
19-
{};
20-
int statRes = stat64(inputStr, &res);
21-
if (statRes != 0) {
22-
printf("Failed to stat shared file: %s\n", strerror(errno));
23-
return 1;
13+
// Create dir if not exists
14+
struct stat st = { 0 };
15+
if (::stat(FILE_DIR, &st) == -1) {
16+
::mkdir(FILE_DIR, 0700);
17+
}
18+
19+
// Delete file if it exists
20+
FILE* check = fopen(FILE_PATH, "r");
21+
if (check != nullptr) {
22+
::fclose(check);
23+
printf("Shared file at %s exists\n", FILE_PATH);
24+
::remove(FILE_PATH);
25+
26+
// Confirm file doesn't exist
27+
check = ::fopen(FILE_PATH, "r");
28+
if (check != nullptr) {
29+
printf("Shared file at %s exists after deletion\n", FILE_PATH);
30+
return 1;
31+
}
2432
}
2533

26-
// Open the file
27-
char* content = faasm::readFileToString(inputStr);
28-
if (content == nullptr) {
29-
printf("Failed to open file at %s\n", inputStr);
34+
// Open the file for writing
35+
FILE* wp = ::fopen(FILE_PATH, "w");
36+
if (wp == nullptr) {
37+
printf("Could not open shared file for writing at %s\n", FILE_PATH);
3038
return 1;
3139
}
3240

33-
// Write file content as output
34-
long length = faasm::getFileLength(inputStr);
35-
faasmSetOutput(BYTES(content), length);
41+
// Write some data to the file
42+
const char* contents = "This is some dummy content";
43+
::fprintf(wp, "%s", contents);
44+
::fclose(wp);
45+
46+
// Read in the data and check
47+
const char* actual = faasm::readFileToString(FILE_PATH);
48+
if (::strcmp(actual, contents) != 0) {
49+
printf("File contents not as expected: %s != %s\n", actual, contents);
50+
return 1;
51+
}
3652

3753
return 0;
3854
}

0 commit comments

Comments
 (0)