Skip to content
Snippets Groups Projects
Commit 161267f0 authored by Giovanna Lazzari Miotto's avatar Giovanna Lazzari Miotto :mushroom:
Browse files

improve creat

parent da09e1d8
No related branches found
No related tags found
No related merge requests found
Pipeline #8028389 failed
#include <errno.h>
#include <fcntl.h>
#include <linux/famfs_ioctl.h>
//#include <linux/famfs_ioctl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <unistd.h>
#include <iostream>
......@@ -17,9 +18,7 @@ void generate_random_data(char *buffer, size_t length) {
buffer[length] = '\0';
}
int main(int argc, char* argv[]) {
int main(int argc, char *argv[]) {
if (argc < 4) {
std::cout << "Wrong number of arguments" << std::endl;
return -1;
......@@ -39,32 +38,62 @@ int main(int argc, char* argv[]) {
}
generate_random_data(data, length - 1);
std::cout << "=== Data generated ===" << std::endl;
// open()
std::string open_filepath = filepath + "_open";
std::string open_filepath = filepath + "_write";
std::cout << "Attempting to call open() on FAMFS for filename=" << open_filepath << std::endl;
int fd = open(open_filepath.c_str(), O_CREAT | O_RDWR);
if (fd < 0) {
std::cout << "Failed open() with fd=" << std::to_string(fd)
<< " and errno=" << std::to_string(errno) << std::endl;
}
int write_rc = write(fd, (void *)data, sizeof(data));
if (write_rc != sizeof(data)) {
std::cout << "write() call returned wrong value rc=" << std::to_string(write_rc) << std::endl;
} else {
int write_rc = write(fd, (void *)data, sizeof(data));
if (write_rc != sizeof(data)) {
std::cout << "write() call returned wrong value rc=" << std::to_string(write_rc) << std::endl;
} else {
std::cout << "sys open / write ran without failure" << std::endl;
}
}
// fopen()
std::string fopen_filepath = filepath + "_fopen";
std::string fopen_filepath = filepath + "_fwrite";
std::cout << "Attempting to call fopen() on FAMFS for filename=" << fopen_filepath << std::endl;
FILE *stream_file = fopen(fopen_filepath.c_str(), "rwa+");
if (stream_file == NULL) {
std::cout << "Failed fopen() with errno=" << std::to_string(errno) << std::endl;
} else {
int fwrite_rc = fwrite((void *)data, 1, sizeof(data), stream_file);
if (fwrite_rc != sizeof(data)) {
std::cout << "fwrite() call returned wrong value rc=" << std::to_string(fwrite_rc)
<< std::endl;
} else {
std::cout << "fopen / fwrite ran without failure" << std::endl;
}
}
int fwrite_rc = fwrite((void *)data, 1, sizeof(data), stream_file);
if (fwrite_rc != sizeof(data)) {
std::cout << "write() call returned wrong value rc=" << std::to_string(write_rc) << std::endl;
std::string mmap_filepath = filepath + "_mmap";
std::cout << "[MMAP] Attempting to call open() on FAMFS for filename=" << mmap_filepath
<< std::endl;
int fd_mmap = open(mmap_filepath.c_str(), O_CREAT | O_RDWR);
if (fd_mmap < 0) {
std::cout << "Failed open() with fd=" << std::to_string(fd_mmap)
<< " and errno=" << std::to_string(errno) << std::endl;
} else {
auto buf = mmap(NULL, length, PROT_WRITE, MAP_SHARED, fd_mmap, 0);
if (buf == MAP_FAILED) {
std::cout << "[MMAP] Error! Memory mapping unsuccessful. errno=" << std::to_string(errno) << std::endl;
} else {
memcpy(buf, data, length);
if (munmap(buf, length) < 0) {
std::cout << "[MMAP FAILED] errno=" << std::to_string(errno) << std::endl;
} else {
std::cout << "[MMAP SUCCESS]" << std::endl;
}
}
}
return 0;
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment