From 161267f020da228ca9e2ded13a712f989fc7e22e Mon Sep 17 00:00:00 2001
From: Giovanna Lazzari Miotto <giovanna.lazzari.miotto@cern.ch>
Date: Fri, 30 Aug 2024 10:44:13 +0200
Subject: [PATCH] improve creat

---
 test/testing_scripts/creat_test.cpp | 57 ++++++++++++++++++++++-------
 1 file changed, 43 insertions(+), 14 deletions(-)

diff --git a/test/testing_scripts/creat_test.cpp b/test/testing_scripts/creat_test.cpp
index d884083d..c597efde 100644
--- a/test/testing_scripts/creat_test.cpp
+++ b/test/testing_scripts/creat_test.cpp
@@ -1,8 +1,9 @@
 #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
-- 
GitLab