-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathshared_file.cpp
54 lines (45 loc) · 1.38 KB
/
shared_file.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include "faasm/files.h"
#include <cstring>
#include <errno.h>
#include <stdio.h>
#include <sys/stat.h>
#define FILE_PATH "faasm://some_dir/test_file.txt"
#define FILE_DIR "faasm://some_dir"
int main(int argc, char* argv[])
{
// Create dir if not exists
struct stat st = { 0 };
if (::stat(FILE_DIR, &st) == -1) {
::mkdir(FILE_DIR, 0700);
}
// Delete file if it exists
FILE* check = fopen(FILE_PATH, "r");
if (check != nullptr) {
::fclose(check);
printf("Shared file at %s exists\n", FILE_PATH);
::remove(FILE_PATH);
// Confirm file doesn't exist
check = ::fopen(FILE_PATH, "r");
if (check != nullptr) {
printf("Shared file at %s exists after deletion\n", FILE_PATH);
return 1;
}
}
// Open the file for writing
FILE* wp = ::fopen(FILE_PATH, "w");
if (wp == nullptr) {
printf("Could not open shared file for writing at %s\n", FILE_PATH);
return 1;
}
// Write some data to the file
const char* contents = "This is some dummy content";
::fprintf(wp, "%s", contents);
::fclose(wp);
// Read in the data and check
const char* actual = faasm::readFileToString(FILE_PATH);
if (::strcmp(actual, contents) != 0) {
printf("File contents not as expected: %s != %s\n", actual, contents);
return 1;
}
return 0;
}