1 /* Copyright (c) 2006-2019. 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)
18 XBT_INFO("Storage info on %s:", simgrid::s4u::Host::current()->get_cname());
20 for (auto const& d : disks) {
21 const char* mountpoint = d->get_property("mount");
23 // Retrieve disk's information
24 XBT_INFO(" %s (%s) Used: %llu; Free: %llu; Total: %llu.", d->get_cname(), mountpoint, sg_disk_get_size_used(d),
25 sg_disk_get_size_free(d), sg_disk_get_size(d));
31 std::vector<simgrid::s4u::Disk*> const& disks = simgrid::s4u::Host::current()->get_disks();
35 // Open an non-existing file to create it
36 std::string filename = "/scratch/tmp/data.txt";
37 simgrid::s4u::File* file = new simgrid::s4u::File(filename, nullptr);
39 sg_size_t write = file->write(200000); // Write 200,000 bytes
40 XBT_INFO("Create a %llu bytes file named '%s' on /scratch", write, filename.c_str());
42 // check that sizes have changed
45 // Now retrieve the size of created file and read it completely
46 const sg_size_t file_size = file->size();
48 const sg_size_t read = file->read(file_size);
49 XBT_INFO("Read %llu bytes on %s", read, filename.c_str());
51 // Now write 100,000 bytes in tmp/data.txt
52 write = file->write(100000); // Write 100,000 bytes
53 XBT_INFO("Write %llu bytes on %s", write, filename.c_str());
55 // Now rename file from ./tmp/data.txt to ./tmp/simgrid.readme
56 std::string newpath = "/scratch/tmp/simgrid.readme";
57 XBT_INFO("Move '%s' to '%s'", file->get_path(), newpath.c_str());
60 // Test attaching some user data to the file
61 file->set_userdata(new std::string("777"));
62 std::string* file_data = static_cast<std::string*>(file->get_userdata());
63 XBT_INFO("User data attached to the file: %s", file_data->c_str());
71 // Reopen the file and then unlink it
72 file = new simgrid::s4u::File("/scratch/tmp/simgrid.readme", nullptr);
73 XBT_INFO("Unlink file: '%s'", file->get_path());
75 delete file; // Unlinking the file on "disk" does not free the object
81 int main(int argc, char** argv)
83 simgrid::s4u::Engine e(&argc, argv);
84 sg_storage_file_system_init();
85 e.load_platform(argv[1]);
86 simgrid::s4u::Actor::create("host", simgrid::s4u::Host::by_name("bob"), MyHost());