|
1 | | -#include "faasm/faasm.h" |
2 | 1 | #include "faasm/files.h" |
3 | | -#include "faasm/input.h" |
4 | 2 |
|
5 | 3 | #include <cstring> |
6 | 4 | #include <errno.h> |
7 | 5 | #include <stdio.h> |
8 | 6 | #include <sys/stat.h> |
9 | 7 |
|
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 | + |
13 | 11 | int main(int argc, char* argv[]) |
14 | 12 | { |
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 | + } |
24 | 32 | } |
25 | 33 |
|
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); |
30 | 38 | return 1; |
31 | 39 | } |
32 | 40 |
|
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 | + } |
36 | 52 |
|
37 | 53 | return 0; |
38 | 54 | } |
0 commit comments