Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines.
[simgrid.git] / examples / c / io-file-system / io-file-system.c
1 /* Copyright (c) 2008-2021. 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 "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"
11
12 #include "xbt/log.h"
13 #include "xbt/str.h"
14 #include "xbt/sysdep.h"
15
16 #include <stdio.h> /* SEEK_SET */
17
18 XBT_LOG_NEW_DEFAULT_CATEGORY(io_file_system, "Messages specific for this io example");
19
20 static void show_info(unsigned int disk_count, const sg_disk_t* disks)
21 {
22   XBT_INFO("Storage info on %s:", sg_host_self_get_name());
23
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));
29   }
30 }
31
32 static void host(int argc, char* argv[])
33 {
34   unsigned int disk_count;
35   sg_disk_t* disks;
36   sg_host_get_disks(sg_host_self(), &disk_count, &disks);
37
38   show_info(disk_count, disks);
39
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);
43
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);
46
47   // check that sizes have changed
48   show_info(disk_count, disks);
49
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);
55
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);
59
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);
64
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);
69   xbt_free(file_data);
70
71   // Close the file
72   sg_file_close(file);
73
74   show_info(disk_count, disks);
75
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));
79   sg_file_unlink(file);
80
81   show_info(disk_count, disks);
82   xbt_free(disks);
83 }
84
85 int main(int argc, char** argv)
86 {
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);
91   simgrid_run();
92
93   return 0;
94 }