]> AND Public Git Repository - simgrid.git/blobdiff - src/s4u/s4u_file.cpp
Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of scm.gforge.inria.fr:/gitroot/simgrid/simgrid
[simgrid.git] / src / s4u / s4u_file.cpp
index 043f6d5d8d12731ca01d3e4b6f5f986bf9f2306a..8c90137942f425ed114f25d482f37d7a23a9d0d5 100644 (file)
@@ -17,19 +17,19 @@ XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_file,"S4U files");
 namespace simgrid {
 namespace s4u {
 
-File::File(const char* fullpath, void* userdata) : File(fullpath, Host::current(), userdata){};
+File::File(std::string fullpath, void* userdata) : File(fullpath, Host::current(), userdata){};
 
-File::File(const char* fullpath, sg_host_t host, void* userdata) : path_(fullpath), userdata_(userdata)
+File::File(std::string fullpath, sg_host_t host, void* userdata) : path_(fullpath), userdata_(userdata)
 {
   // this cannot fail because we get a xbt_die if the mountpoint does not exist
   Storage* st                  = nullptr;
   size_t longest_prefix_length = 0;
   std::string path;
-  XBT_DEBUG("Search for storage name for '%s' on '%s'", fullpath, host->getCname());
+  XBT_DEBUG("Search for storage name for '%s' on '%s'", fullpath.c_str(), host->getCname());
 
-  for (auto mnt : host->getMountedStorages()) {
+  for (auto const& mnt : host->getMountedStorages()) {
     XBT_DEBUG("See '%s'", mnt.first.c_str());
-    mount_point = std::string(fullpath).substr(0, mnt.first.size());
+    mount_point = fullpath.substr(0, mnt.first.length());
 
     if (mount_point == mnt.first && mnt.first.length() > longest_prefix_length) {
       /* The current mount name is found in the full path and is bigger than the previous*/
@@ -38,15 +38,14 @@ File::File(const char* fullpath, sg_host_t host, void* userdata) : path_(fullpat
     }
   }
   if (longest_prefix_length > 0) { /* Mount point found, split fullpath into mount_name and path+filename*/
-    mount_point = std::string(fullpath).substr(0, longest_prefix_length);
-    path        = std::string(fullpath).substr(longest_prefix_length, strlen(fullpath));
+    mount_point = fullpath.substr(0, longest_prefix_length);
+    path        = fullpath.substr(longest_prefix_length, fullpath.length());
   } else
-    xbt_die("Can't find mount point for '%s' on '%s'", fullpath, host->getCname());
+    xbt_die("Can't find mount point for '%s' on '%s'", fullpath.c_str(), host->getCname());
 
   pimpl_ =
       simgrid::simix::kernelImmediate([this, st, path] { return new simgrid::surf::FileImpl(st, path, mount_point); });
-  storage_type = st->getType();
-  storageId    = st->getName();
+  onStorage = st;
 }
 
 File::~File()
@@ -56,12 +55,30 @@ File::~File()
 
 sg_size_t File::read(sg_size_t size)
 {
-  return simcall_file_read(pimpl_, size);
+  XBT_DEBUG("READ %s on disk '%s'", getPath(), onStorage->getCname());
+  // if the current position is close to the end of the file, we may not be able to read the requested size
+  sg_size_t read_size = onStorage->read(std::min(size, this->size() - this->tell()));
+  pimpl_->incrPosition(read_size);
+  return read_size;
 }
 
 sg_size_t File::write(sg_size_t size)
 {
-  return simcall_file_write(pimpl_, size);
+  XBT_DEBUG("WRITE %s on disk '%s'. size '%llu/%llu'", getPath(), onStorage->getCname(), size, this->size());
+  // If the storage is full before even starting to write
+  if (onStorage->getSizeUsed() >= onStorage->getSize())
+    return 0;
+  /* Substract the part of the file that might disappear from the used sized on the storage element */
+  onStorage->decrUsedSize(this->size() - this->tell());
+
+  sg_size_t write_size = onStorage->write(size);
+  pimpl_->incrPosition(write_size);
+  pimpl_->setSize(this->tell());
+
+  onStorage->getContent()->erase(pimpl_->getName());
+  onStorage->getContent()->insert({pimpl_->getName(), this->size()});
+
+  return write_size;
 }
 
 sg_size_t File::size()
@@ -84,7 +101,7 @@ sg_size_t File::tell()
   return simgrid::simix::kernelImmediate([this] { return pimpl_->tell(); });
 }
 
-void File::move(const char* fullpath)
+void File::move(std::string fullpath)
 {
   simgrid::simix::kernelImmediate([this, fullpath] { pimpl_->move(fullpath); });
 }