Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
ef572828043709f7d0b3bdc18165622f7f686deb
[simgrid.git] / examples / cpp / io-file-system / s4u-io-file-system.cpp
1 /* Copyright (c) 2006-2023. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 #include <string>
7 #include <vector>
8
9 #include "simgrid/plugins/file_system.h"
10 #include "simgrid/s4u.hpp"
11
12 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "a sample log category");
13 namespace sg4 = simgrid::s4u;
14
15 class MyHost {
16 public:
17   void show_info(std::vector<sg4::Disk*> const& disks) const
18   {
19     XBT_INFO("Storage info on %s:", sg4::Host::current()->get_cname());
20
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));
25     }
26   }
27
28   void operator()() const
29   {
30     std::vector<sg4::Disk*> const& disks = sg4::Host::current()->get_disks();
31
32     show_info(disks);
33
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);
37
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());
40
41     // check that sizes have changed
42     show_info(disks);
43
44     // Now retrieve the size of created file and read it completely
45     const sg_size_t file_size = file->size();
46     file->seek(0);
47     const sg_size_t read = file->read(file_size);
48     XBT_INFO("Read %llu bytes on %s", read, filename.c_str());
49
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());
53
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());
57     file->move(newpath);
58
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());
63     delete file_data;
64
65     // Close the file
66     file->close();
67
68     show_info(disks);
69
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());
73     file->unlink();
74     file->close(); // Unlinking the file on "disk" does not close the file and free the object
75
76     show_info(disks);
77   }
78 };
79
80 int main(int argc, char** argv)
81 {
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());
86   e.run();
87
88   return 0;
89 }