1 /* Copyright (c) 2006-2020. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
9 #include "simgrid/plugins/file_system.h"
10 #include "simgrid/s4u.hpp"
12 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "a sample log category");
16 void show_info(std::vector<simgrid::s4u::Disk*> const& disks) const
18 XBT_INFO("Storage info on %s:", simgrid::s4u::Host::current()->get_cname());
20 for (auto const& d : disks) {
21 // Retrieve disk's information
22 XBT_INFO(" %s (%s) Used: %llu; Free: %llu; Total: %llu.", d->get_cname(), sg_disk_get_mount_point(d),
23 sg_disk_get_size_used(d), sg_disk_get_size_free(d), sg_disk_get_size(d));
27 void operator()() const
29 std::vector<simgrid::s4u::Disk*> const& disks = simgrid::s4u::Host::current()->get_disks();
33 // Open a non-existing file to create it
34 std::string filename = "/scratch/tmp/data.txt";
35 auto* file = new simgrid::s4u::File(filename, nullptr);
37 sg_size_t write = file->write(200000); // Write 200,000 bytes
38 XBT_INFO("Create a %llu bytes file named '%s' on /scratch", write, filename.c_str());
40 // check that sizes have changed
43 // Now retrieve the size of created file and read it completely
44 const sg_size_t file_size = file->size();
46 const sg_size_t read = file->read(file_size);
47 XBT_INFO("Read %llu bytes on %s", read, filename.c_str());
49 // Now write 100,000 bytes in tmp/data.txt
50 write = file->write(100000); // Write 100,000 bytes
51 XBT_INFO("Write %llu bytes on %s", write, filename.c_str());
53 // Now rename file from ./tmp/data.txt to ./tmp/simgrid.readme
54 std::string newpath = "/scratch/tmp/simgrid.readme";
55 XBT_INFO("Move '%s' to '%s'", file->get_path(), newpath.c_str());
58 // Test attaching some user data to the file
59 file->set_data(new std::string("777"));
60 const auto* file_data = static_cast<std::string*>(file->get_data());
61 XBT_INFO("User data attached to the file: %s", file_data->c_str());
69 // Reopen the file and then unlink it
70 file = new simgrid::s4u::File("/scratch/tmp/simgrid.readme", nullptr);
71 XBT_INFO("Unlink file: '%s'", file->get_path());
73 delete file; // Unlinking the file on "disk" does not free the object
79 int main(int argc, char** argv)
81 simgrid::s4u::Engine e(&argc, argv);
82 sg_storage_file_system_init();
83 e.load_platform(argv[1]);
84 simgrid::s4u::Actor::create("host", simgrid::s4u::Host::by_name("bob"), MyHost());