static void host(std::vector<std::string> args)
{
- simgrid::s4u::File file(args[1], nullptr);
- const char* filename = file.get_path();
+ simgrid::s4u::File* file = simgrid::s4u::File::open(args[1], nullptr);
+ const char* filename = file->get_path();
XBT_INFO("Opened file '%s'", filename);
- file.dump();
- XBT_INFO("Try to write %llu MiB to '%s'", file.size() / 1024, filename);
- sg_size_t write = file.write(file.size() * 1024);
+ file->dump();
+ XBT_INFO("Try to write %llu MiB to '%s'", file->size() / 1024, filename);
+ sg_size_t write = file->write(file->size() * 1024);
XBT_INFO("Have written %llu MiB to '%s'.", write / (1024 * 1024), filename);
if (args.size() > 4) {
if (std::stoi(args[4]) != 0) {
- XBT_INFO("Move '%s' (of size %llu) from '%s' to '%s'", filename, file.size(),
+ XBT_INFO("Move '%s' (of size %llu) from '%s' to '%s'", filename, file->size(),
simgrid::s4u::Host::current()->get_cname(), args[2].c_str());
- file.remote_move(simgrid::s4u::Host::by_name(args[2]), args[3]);
+ file->remote_move(simgrid::s4u::Host::by_name(args[2]), args[3]);
} else {
- XBT_INFO("Copy '%s' (of size %llu) from '%s' to '%s'", filename, file.size(),
+ XBT_INFO("Copy '%s' (of size %llu) from '%s' to '%s'", filename, file->size(),
simgrid::s4u::Host::current()->get_cname(), args[2].c_str());
- file.remote_copy(simgrid::s4u::Host::by_name(args[2]), args[3]);
+ file->remote_copy(simgrid::s4u::Host::by_name(args[2]), args[3]);
}
}
+ file->close();
}
int main(int argc, char** argv)
// Open a non-existing file to create it
std::string filename = "/scratch/tmp/data.txt";
- auto* file = new simgrid::s4u::File(filename, nullptr);
+ auto* file = simgrid::s4u::File::open(filename, nullptr);
sg_size_t write = file->write(200000); // Write 200,000 bytes
XBT_INFO("Create a %llu bytes file named '%s' on /scratch", write, filename.c_str());
delete file_data;
// Close the file
- delete file;
+ file->close();
show_info(disks);
// Reopen the file and then unlink it
- file = new simgrid::s4u::File("/scratch/tmp/simgrid.readme", nullptr);
+ file = simgrid::s4u::File::open("/scratch/tmp/simgrid.readme", nullptr);
XBT_INFO("Unlink file: '%s'", file->get_path());
file->unlink();
- delete file; // Unlinking the file on "disk" does not free the object
+ file->close(); // Unlinking the file on "disk" does not close the file and free the object
show_info(disks);
}
XBT_PUBLIC sg_file_t sg_file_open(const char* fullpath, void* data);
XBT_PUBLIC sg_size_t sg_file_read(sg_file_t fd, sg_size_t size);
XBT_PUBLIC sg_size_t sg_file_write(sg_file_t fd, sg_size_t size);
-XBT_PUBLIC void sg_file_close(const_sg_file_t fd);
+XBT_PUBLIC void sg_file_close(sg_file_t fd);
XBT_PUBLIC const char* sg_file_get_name(const_sg_file_t fd);
XBT_PUBLIC sg_size_t sg_file_get_size(const_sg_file_t fd);
File& operator=(const File&) = delete;
~File();
+ static File* open(const std::string& fullpath, void* userdata);
+ void close() { delete this; };
+
/** Retrieves the path to the file */
const char* get_path() const { return fullpath_.c_str(); }
kernel::actor::simcall([this, desc_table] { desc_table->push_back(this->desc_id); });
}
+File* File::open(const std::string& fullpath, void* userdata)
+{
+ return new File(fullpath, userdata);
+}
+
void File::dump() const
{
XBT_INFO("File Descriptor information:\n"
sg_file_t sg_file_open(const char* fullpath, void* data)
{
- return new simgrid::s4u::File(fullpath, data);
+ return simgrid::s4u::File::open(fullpath, data);
}
sg_size_t sg_file_read(sg_file_t fd, sg_size_t size)
return fd->write(size);
}
-void sg_file_close(const_sg_file_t fd)
+void sg_file_close(sg_file_t fd)
{
- delete fd;
+ fd->close();
}
/** Retrieves the path to the file
}
}
- file_= new simgrid::s4u::File(fullname, nullptr);
+ file_ = simgrid::s4u::File::open(fullname, nullptr);
list_=nullptr;
if (comm_->rank() == 0) {
int size= comm_->size() + FP_SIZE;
delete[] list_;
}
delete win_;
- delete file_;
+ file_->close();
F2C::free_f(this->f2c_id());
if (info_ != MPI_INFO_NULL)
simgrid::smpi::Info::unref(info_);