1 /* Copyright (c) 2006-2023. 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");
13 namespace sg4 = simgrid::s4u;
17 void show_info(std::vector<sg4::Disk*> const& disks) const
19 XBT_INFO("Storage info on %s:", sg4::Host::current()->get_cname());
21 for (auto const& d : disks) {
22 // Retrieve disk's information
23 XBT_INFO(" %s (%s) Used: %llu; Free: %llu; Total: %llu.", d->get_cname(), sg_disk_get_mount_point(d),
24 sg_disk_get_size_used(d), sg_disk_get_size_free(d), sg_disk_get_size(d));
28 void operator()() const
30 std::vector<sg4::Disk*> const& disks = sg4::Host::current()->get_disks();
34 // Open a non-existing file to create it
35 std::string filename = "/scratch/tmp/data.txt";
36 auto* file = sg4::File::open(filename, nullptr);
38 sg_size_t write = file->write(200000); // Write 200,000 bytes
39 XBT_INFO("Create a %llu bytes file named '%s' on /scratch", write, filename.c_str());
41 // check that sizes have changed
44 // Now retrieve the size of created file and read it completely
45 const sg_size_t file_size = file->size();
47 const sg_size_t read = file->read(file_size);
48 XBT_INFO("Read %llu bytes on %s", read, filename.c_str());
50 // Now write 100,000 bytes in tmp/data.txt
51 write = file->write(100000); // Write 100,000 bytes
52 XBT_INFO("Write %llu bytes on %s", write, filename.c_str());
54 // Now rename file from ./tmp/data.txt to ./tmp/simgrid.readme
55 std::string newpath = "/scratch/tmp/simgrid.readme";
56 XBT_INFO("Move '%s' to '%s'", file->get_path(), newpath.c_str());
59 // Test attaching some user data to the file
60 file->set_data(new std::string("777"));
61 const auto* file_data = file->get_data<std::string>();
62 XBT_INFO("User data attached to the file: %s", file_data->c_str());
70 // Reopen the file and then unlink it
71 file = sg4::File::open("/scratch/tmp/simgrid.readme", nullptr);
72 XBT_INFO("Unlink file: '%s'", file->get_path());
74 file->close(); // Unlinking the file on "disk" does not close the file and free the object
80 int main(int argc, char** argv)
82 sg4::Engine e(&argc, argv);
83 sg_storage_file_system_init();
84 e.load_platform(argv[1]);
85 sg4::Actor::create("host", e.host_by_name("bob"), MyHost());