Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
remove codacy.
[simgrid.git] / include / simgrid / plugins / file_system.h
1 /* Copyright (c) 2017-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 #ifndef SIMGRID_PLUGINS_FILE_SYSTEM_H_
7 #define SIMGRID_PLUGINS_FILE_SYSTEM_H_
8
9 #include <simgrid/forward.h>
10 #include <xbt/base.h>
11 #include <xbt/dict.h>
12
13 #ifdef __cplusplus
14 #include <xbt/Extendable.hpp>
15
16 #include <map>
17 #include <memory>
18 #include <string>
19 #endif
20
21 // C interface
22 ////////////////
23 typedef sg_file_t msg_file_t; // MSG backwards compatibility
24
25 SG_BEGIN_DECL()
26 XBT_PUBLIC void sg_storage_file_system_init();
27 XBT_PUBLIC sg_file_t sg_file_open(const char* fullpath, void* data);
28 XBT_PUBLIC sg_size_t sg_file_read(sg_file_t fd, sg_size_t size);
29 XBT_PUBLIC sg_size_t sg_file_write(sg_file_t fd, sg_size_t size);
30 XBT_PUBLIC void sg_file_close(sg_file_t fd);
31
32 XBT_PUBLIC const char* sg_file_get_name(sg_file_t fd);
33 XBT_PUBLIC sg_size_t sg_file_get_size(sg_file_t fd);
34 XBT_PUBLIC void sg_file_dump(sg_file_t fd);
35 XBT_PUBLIC void* sg_file_get_data(sg_file_t fd);
36 XBT_PUBLIC void sg_file_set_data(sg_file_t fd, void* data);
37 XBT_PUBLIC void sg_file_seek(sg_file_t fd, sg_offset_t offset, int origin);
38 XBT_PUBLIC sg_size_t sg_file_tell(sg_file_t fd);
39 XBT_PUBLIC void sg_file_move(sg_file_t fd, const char* fullpath);
40 XBT_PUBLIC void sg_file_unlink(sg_file_t fd);
41 XBT_PUBLIC int sg_file_rcopy(sg_file_t file, sg_host_t host, const char* fullpath);
42 XBT_PUBLIC int sg_file_rmove(sg_file_t file, sg_host_t host, const char* fullpath);
43
44 XBT_PUBLIC sg_size_t sg_storage_get_size_free(sg_storage_t st);
45 XBT_PUBLIC sg_size_t sg_storage_get_size_used(sg_storage_t st);
46 XBT_PUBLIC sg_size_t sg_storage_get_size(sg_storage_t st);
47 XBT_PUBLIC xbt_dict_t sg_storage_get_content(sg_storage_t storage);
48
49 XBT_PUBLIC xbt_dict_t sg_host_get_storage_content(sg_host_t host);
50
51 #define MSG_file_open(fullpath, data) sg_file_open(fullpath, data)
52 #define MSG_file_read(fd, size) sg_file_read(fd, size)
53 #define MSG_file_write(fd, size) sg_file_write(fd, size)
54 #define MSG_file_close(fd) sg_file_close(fd)
55 #define MSG_file_get_name(fd) sg_file_get_name(fd)
56 #define MSG_file_get_size(fd) sg_file_get_size(fd)
57 #define MSG_file_dump(fd) sg_file_dump(fd)
58 #define MSG_file_get_data(fd) sg_file_get_data(fd)
59 #define MSG_file_set_data(fd, data) sg_file_set_data(fd, data)
60 #define MSG_file_seek(fd, offset, origin) sg_file_seek(fd, offset, origin)
61 #define MSG_file_tell(fd) sg_file_tell(fd)
62 #define MSG_file_move(fd, fullpath) sg_file_get_size(fd, fullpath)
63 #define MSG_file_unlink(fd) sg_file_unlink(fd)
64 #define MSG_file_rcopy(file, host, fullpath) sg_file_rcopy(file, host, fullpath)
65 #define MSG_file_rmove(file, host, fullpath) sg_file_rmove(file, host, fullpath)
66
67 #define MSG_storage_file_system_init() sg_storage_file_system_init()
68 #define MSG_storage_get_free_size(st) sg_storage_get_size_free(st)
69 #define MSG_storage_get_used_size(st) sg_storage_get_size_used(st)
70 #define MSG_storage_get_size(st) sg_storage_get_size(st)
71 #define MSG_storage_get_content(st) sg_storage_get_content(st)
72
73 #define MSG_host_get_storage_content(st) sg_host_get_storage_content(st)
74
75 SG_END_DECL()
76
77 // C++ interface
78 //////////////////
79
80 #ifdef __cplusplus
81
82 namespace simgrid {
83 namespace s4u {
84
85 /** @brief A simulated file
86  *
87  * Used to simulate the time it takes to access to a file, but does not really store any information.
88  *
89  * They are located on @ref simgrid::s4u::Storage that are accessed from a given @ref simgrid::s4u::Host through
90  * mountpoints.
91  * For now, you cannot change the mountpoints programatically, and must declare them from your platform file.
92  */
93 class XBT_PUBLIC File {
94 public:
95   File(const std::string& fullpath, void* userdata);
96   File(const std::string& fullpath, sg_host_t host, void* userdata);
97   File(const File&) = delete;
98   File& operator=(const File&) = delete;
99   ~File();
100
101   /** Retrieves the path to the file */
102   const char* get_path() { return fullpath_.c_str(); }
103
104   /** Simulates a local read action. Returns the size of data actually read */
105   sg_size_t read(sg_size_t size);
106
107   /** Simulates a write action. Returns the size of data actually written. */
108   sg_size_t write(sg_size_t size, int write_inside=0);
109
110   /** Allows to store user data on that host */
111   void set_userdata(void* data) { userdata_ = data; }
112   /** Retrieves the previously stored data */
113   void* get_userdata() { return userdata_; }
114
115   sg_size_t size();
116   void seek(sg_offset_t pos);             /** Sets the file head to the given position. */
117   void seek(sg_offset_t pos, int origin); /** Sets the file head to the given position from a given origin. */
118   sg_size_t tell();                       /** Retrieves the current file position */
119
120   /** Rename a file. WARNING: It is forbidden to move the file to another mount point */
121   void move(const std::string& fullpath);
122   int remote_copy(sg_host_t host, const char* fullpath);
123   int remote_move(sg_host_t host, const char* fullpath);
124
125   int unlink(); /** Remove a file from the contents of a disk */
126   void dump();
127
128   int desc_id = 0;
129   Storage* local_storage_;
130   std::string mount_point_;
131
132 private:
133   sg_size_t size_;
134   std::string path_;
135   std::string fullpath_;
136   sg_size_t current_position_ = SEEK_SET;
137   void* userdata_             = nullptr;
138 };
139
140 class XBT_PUBLIC FileSystemStorageExt {
141 public:
142   static simgrid::xbt::Extension<Storage, FileSystemStorageExt> EXTENSION_ID;
143   explicit FileSystemStorageExt(Storage* ptr);
144   FileSystemStorageExt(const FileSystemStorageExt&) = delete;
145   FileSystemStorageExt& operator=(const FileSystemStorageExt&) = delete;
146   std::map<std::string, sg_size_t>* parse_content(const std::string& filename);
147   std::map<std::string, sg_size_t>* get_content() { return content_.get(); }
148   sg_size_t get_size() { return size_; }
149   sg_size_t get_used_size() { return used_size_; }
150   void decr_used_size(sg_size_t size) { used_size_ -= size; }
151   void incr_used_size(sg_size_t size) { used_size_ += size; }
152
153 private:
154   std::unique_ptr<std::map<std::string, sg_size_t>> content_;
155   sg_size_t used_size_ = 0;
156   sg_size_t size_     = 0;
157 };
158
159 class XBT_PUBLIC FileDescriptorHostExt {
160 public:
161   static simgrid::xbt::Extension<Host, FileDescriptorHostExt> EXTENSION_ID;
162   FileDescriptorHostExt() = default;
163   FileDescriptorHostExt(const FileDescriptorHostExt&) = delete;
164   FileDescriptorHostExt& operator=(const FileDescriptorHostExt&) = delete;
165   std::unique_ptr<std::vector<int>> file_descriptor_table        = nullptr; // Created lazily on need
166 };
167 } // namespace s4u
168 } // namespace simgrid
169 #endif
170 #endif