1 /* Copyright (c) 2008-2021. 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. */
6 #include "simgrid/actor.h"
7 #include "simgrid/disk.h"
8 #include "simgrid/engine.h"
9 #include "simgrid/host.h"
10 #include "simgrid/plugins/file_system.h"
14 #include "xbt/sysdep.h"
16 #include <stdio.h> /* SEEK_SET */
18 XBT_LOG_NEW_DEFAULT_CATEGORY(io_file_system, "Messages specific for this io example");
20 static void show_info(unsigned int disk_count, const sg_disk_t* disks)
22 XBT_INFO("Storage info on %s:", sg_host_self_get_name());
24 for (unsigned int i = 0; i < disk_count; i++) {
25 const_sg_disk_t d = disks[i];
26 // Retrieve disk's information
27 XBT_INFO(" %s (%s) Used: %llu; Free: %llu; Total: %llu.", sg_disk_get_name(d), sg_disk_get_mount_point(d),
28 sg_disk_get_size_used(d), sg_disk_get_size_free(d), sg_disk_get_size(d));
32 static void host(int argc, char* argv[])
34 unsigned int disk_count;
36 sg_host_get_disks(sg_host_self(), &disk_count, &disks);
38 show_info(disk_count, disks);
40 // Open a non-existing file to create it
41 const char* filename = "/scratch/tmp/data.txt";
42 sg_file_t file = sg_file_open(filename, NULL);
44 sg_size_t write = sg_file_write(file, 200000); // Write 200,000 bytes
45 XBT_INFO("Create a %llu bytes file named '%s' on /scratch", write, filename);
47 // check that sizes have changed
48 show_info(disk_count, disks);
50 // Now retrieve the size of created file and read it completely
51 const sg_size_t file_size = sg_file_get_size(file);
52 sg_file_seek(file, 0, SEEK_SET);
53 const sg_size_t read = sg_file_read(file, file_size);
54 XBT_INFO("Read %llu bytes on %s", read, filename);
56 // Now write 100,000 bytes in tmp/data.txt
57 write = sg_file_write(file, 100000); // Write 100,000 bytes
58 XBT_INFO("Write %llu bytes on %s", write, filename);
60 // Now rename file from ./tmp/data.txt to ./tmp/simgrid.readme
61 const char* newpath = "/scratch/tmp/simgrid.readme";
62 XBT_INFO("Move '%s' to '%s'", sg_file_get_name(file), newpath);
63 sg_file_move(file, newpath);
65 // Test attaching some user data to the file
66 sg_file_set_data(file, xbt_strdup("777"));
67 char* file_data = (char*)(sg_file_get_data(file));
68 XBT_INFO("User data attached to the file: %s", file_data);
74 show_info(disk_count, disks);
76 // Reopen the file and then unlink it
77 file = sg_file_open("/scratch/tmp/simgrid.readme", NULL);
78 XBT_INFO("Unlink file: '%s'", sg_file_get_name(file));
81 show_info(disk_count, disks);
85 int main(int argc, char** argv)
87 simgrid_init(&argc, argv);
88 sg_storage_file_system_init();
89 simgrid_load_platform(argv[1]);
90 sg_actor_create("host", sg_host_by_name("bob"), host, 0, NULL);