Logo AND Algorithmique Numérique Distribuée

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