Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
4c91e69c3c14e47031b8868c023769bc42c8f3cb
[simgrid.git] / examples / s4u / io-file-system / s4u-io-file-system.cpp
1 /* Copyright (c) 2006-2019. 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
14 class MyHost {
15 public:
16   void show_info(std::vector<simgrid::s4u::Disk*> const& disks)
17   {
18     XBT_INFO("Storage info on %s:", simgrid::s4u::Host::current()->get_cname());
19
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));
24     }
25   }
26
27   void operator()()
28   {
29     std::vector<simgrid::s4u::Disk*> const& disks = simgrid::s4u::Host::current()->get_disks();
30
31     show_info(disks);
32
33     // Open an non-existing file to create it
34     std::string filename     = "/scratch/tmp/data.txt";
35     simgrid::s4u::File* file = new simgrid::s4u::File(filename, nullptr);
36
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());
39
40     // check that sizes have changed
41     show_info(disks);
42
43     // Now retrieve the size of created file and read it completely
44     const sg_size_t file_size = file->size();
45     file->seek(0);
46     const sg_size_t read = file->read(file_size);
47     XBT_INFO("Read %llu bytes on %s", read, filename.c_str());
48
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());
52
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());
56     file->move(newpath);
57
58     // Test attaching some user data to the file
59     file->set_userdata(new std::string("777"));
60     std::string* file_data = static_cast<std::string*>(file->get_userdata());
61     XBT_INFO("User data attached to the file: %s", file_data->c_str());
62     delete file_data;
63
64     // Close the file
65     delete file;
66
67     show_info(disks);
68
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());
72     file->unlink();
73     delete file; // Unlinking the file on "disk" does not free the object
74
75     show_info(disks);
76   }
77 };
78
79 int main(int argc, char** argv)
80 {
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());
85   e.run();
86
87   return 0;
88 }