Support C++ fstreams
System information
| Type | Version/Name |
|---|---|
| Operating System | Linux |
| OS Version | Ubuntu 20.04 LTS |
| Architecture | x86_64 |
| UnifyFS Version | current git HEAD |
Describe the problem you're observing
It appears C++ fstreams are not handled properly by UnifyFS.
Describe how to reproduce the problem
It looks like pretty much any use of std::ifstream or std::ofstream will fail. I'll post some sample code below.
Include any warning or errors or releveant debugging data
For both std::ofstream and std::ifstream, opening the stream fails. For output streams, the file should have been created automatically, but it isn't. And for input streams, attempting to open a file that is known to exist still fails. The message from strerror(errno) in both cases is "No such file or directory".
GitHub won't let me attach a .cpp file, so I'm posting a simple bit of test code in this comment. Note that since the streams fail to open, this code never even gets to try reading or writing.
// Test UnifyFS using C++ fstreams
#include <unifyfs.h>
#include <unifyfs_rc.h>
#include <errno.h>
#include <string.h>
#include <iostream>
#include <fstream>
#include <string>
// The file we're going to read/write
//#define FNAME "/tmp/simple_test.txt"
#define FNAME "/unifyfs/simple_test.txt"
using namespace std;
int main( int argc, char **argv)
{
int rc = unifyfs_mount("/unifyfs", 0, 1, 0);
if (rc != UNIFYFS_SUCCESS) {
cerr << "unifyfs_mount() failed. Error code: " << rc << " - " << unifyfs_rc_enum_str((unifyfs_rc)rc) << endl;
cerr << "Aborting." << endl;
exit(-1);
}
// Try writing with C++ fstreams
cout << "------- Writing using ofstream ------" << endl;
ofstream outfile(FNAME);
if (!outfile.good()) {
string err(strerror(errno));
cerr << "Failed to open ofstream. Err msg: " << err << endl;
} else {
outfile << "This is a little bit of data written using std::ofstream." << endl;
outfile.close();
}
cout << "-------------------------------------" << endl;
// Try reading with C++ fstreams
cout << "------- Reading using ifstream ------" << endl;
ifstream infile(FNAME);
if (! infile.good()) {
string err(strerror(errno));
cerr << "Failed to open ifstream. Err msg: " << err << endl;
} else {
cout << "Contents of " << FNAME << ":" << endl;
while (! infile.eof()) {
string s;
getline(infile, s);
cout << s << endl;
}
}
infile.close();
cout << "-------------------------------------" << endl;
rc = unifyfs_unmount();
if (rc != UNIFYFS_SUCCESS) {
cerr << "unifyfs_unmount() failed. Error code: " << rc << endl;
}
return 0;
}