Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use std::unique_ptr with std::ifstream.
[simgrid.git] / src / plugins / file_system / s4u_FileSystem.cpp
index 516b680a639b32e0370ed5d09d66a9df24a5b697..73089853485ef35e73be73f671d839282b44cdb7 100644 (file)
@@ -389,7 +389,7 @@ int File::unlink() const
   }
 }
 
-int File::remote_copy(sg_host_t host, const char* fullpath)
+int File::remote_copy(sg_host_t host, const std::string& fullpath)
 {
   /* Find the host where the file is physically located and read it */
   Host* src_host = nullptr;
@@ -432,7 +432,7 @@ int File::remote_copy(sg_host_t host, const char* fullpath)
       /* Mount point found, retrieve the host the storage is attached to */
       dst_host = storage_dest->get_host();
     } else {
-      XBT_WARN("Can't find mount point for '%s' on destination host '%s'", fullpath, host->get_cname());
+      XBT_WARN("Can't find mount point for '%s' on destination host '%s'", fullpath.c_str(), host->get_cname());
       return -1;
     }
   }
@@ -451,7 +451,7 @@ int File::remote_copy(sg_host_t host, const char* fullpath)
     }
 
     if (dst_disk == nullptr) {
-      XBT_WARN("Can't find mount point for '%s' on destination host '%s'", fullpath, host->get_cname());
+      XBT_WARN("Can't find mount point for '%s' on destination host '%s'", fullpath.c_str(), host->get_cname());
       return -1;
     }
   }
@@ -463,19 +463,18 @@ int File::remote_copy(sg_host_t host, const char* fullpath)
   }
 
   /* Create file on remote host, write it and close it */
-  auto* fd = new File(fullpath, dst_host, nullptr);
+  File fd(fullpath, dst_host, nullptr);
   if (local_storage_) {
-    sg_size_t write_size = fd->local_storage_->write(read_size);
-    fd->local_storage_->extension<FileSystemStorageExt>()->incr_used_size(write_size);
-    (*(fd->local_storage_->extension<FileSystemStorageExt>()->get_content()))[path_] = size_;
+    sg_size_t write_size = fd.local_storage_->write(read_size);
+    fd.local_storage_->extension<FileSystemStorageExt>()->incr_used_size(write_size);
+    (*(fd.local_storage_->extension<FileSystemStorageExt>()->get_content()))[path_] = size_;
   }
   if (local_disk_)
-    fd->write(read_size);
-  delete fd;
+    fd.write(read_size);
   return 0;
 }
 
-int File::remote_move(sg_host_t host, const char* fullpath)
+int File::remote_move(sg_host_t host, const std::string& fullpath)
 {
   int res = remote_copy(host, fullpath);
   unlink();
@@ -512,7 +511,7 @@ std::map<std::string, sg_size_t>* FileSystemDiskExt::parse_content(const std::st
 
   auto* parse_content = new std::map<std::string, sg_size_t>();
 
-  std::ifstream* fs = surf_ifsopen(filename);
+  auto fs = std::unique_ptr<std::ifstream>(surf_ifsopen(filename));
   xbt_assert(not fs->fail(), "Cannot open file '%s' (path=%s)", filename.c_str(),
              (boost::join(surf_path, ":")).c_str());
 
@@ -530,7 +529,6 @@ std::map<std::string, sg_size_t>* FileSystemDiskExt::parse_content(const std::st
       parse_content->insert({tokens.front(), size});
     }
   } while (not fs->eof());
-  delete fs;
   return parse_content;
 }
 
@@ -541,7 +539,7 @@ std::map<std::string, sg_size_t>* FileSystemStorageExt::parse_content(const std:
 
   auto* parse_content = new std::map<std::string, sg_size_t>();
 
-  std::ifstream* fs = surf_ifsopen(filename);
+  auto fs = std::unique_ptr<std::ifstream>(surf_ifsopen(filename));
   xbt_assert(not fs->fail(), "Cannot open file '%s' (path=%s)", filename.c_str(),
              (boost::join(surf_path, ":")).c_str());
 
@@ -559,7 +557,6 @@ std::map<std::string, sg_size_t>* FileSystemStorageExt::parse_content(const std:
       parse_content->insert({tokens.front(), size});
     }
   } while (not fs->eof());
-  delete fs;
   return parse_content;
 }
 
@@ -614,7 +611,7 @@ static void on_platform_created()
       simgrid::s4u::Host* remote_host = simgrid::s4u::Host::by_name_or_null(tokens[2]);
       xbt_assert(remote_host, "You're trying to access a host that does not exist. Please check your platform file");
 
-      simgrid::s4u::Disk* disk = nullptr;
+      const simgrid::s4u::Disk* disk = nullptr;
       for (auto const& d : remote_host->get_disks())
         if (d->get_name() == tokens[1]) {
           disk = d;