Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Replace "XBT_PUBLIC_CLASS" with "class XBT_PUBLIC".
[simgrid.git] / src / plugins / file_system / FileSystem.hpp
1 /* Copyright (c) 2006-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_S4U_FILE_HPP
7 #define SIMGRID_S4U_FILE_HPP
8
9 #include "simgrid/plugins/file_system.h"
10 #include <xbt/Extendable.hpp>
11 #include <xbt/base.h>
12
13 #include <simgrid/simix.h>
14 #include <string>
15
16 namespace simgrid {
17 namespace s4u {
18
19 /** @brief A simulated file
20  *
21  * Used to simulate the time it takes to access to a file, but does not really store any information.
22  *
23  * They are located on @ref simgrid::s4u::Storage that are accessed from a given @ref simgrid::s4u::Host through
24  * mountpoints.
25  * For now, you cannot change the mountpoints programatically, and must declare them from your platform file.
26  */
27 class XBT_PUBLIC File {
28 public:
29   File(std::string fullpath, void* userdata);
30   File(std::string fullpath, sg_host_t host, void* userdata);
31   ~File();
32
33   /** Retrieves the path to the file */
34   const char* getPath() { return fullpath_.c_str(); }
35
36   /** Simulates a local read action. Returns the size of data actually read */
37   sg_size_t read(sg_size_t size);
38
39   /** Simulates a write action. Returns the size of data actually written. */
40   sg_size_t write(sg_size_t size);
41
42   /** Allows to store user data on that host */
43   void setUserdata(void* data) { userdata_ = data; }
44   /** Retrieves the previously stored data */
45   void* getUserdata() { return userdata_; }
46
47   sg_size_t size();
48   void seek(sg_offset_t pos);             /** Sets the file head to the given position. */
49   void seek(sg_offset_t pos, int origin); /** Sets the file head to the given position from a given origin. */
50   sg_size_t tell();                       /** Retrieves the current file position */
51
52   /** Rename a file. WARNING: It is forbidden to move the file to another mount point */
53   void move(std::string fullpath);
54   int remoteCopy(sg_host_t host, const char* fullpath);
55   int remoteMove(sg_host_t host, const char* fullpath);
56
57   int unlink(); /** Remove a file from the contents of a disk */
58   void dump();
59
60   int desc_id = 0;
61   Storage* localStorage;
62   std::string mount_point_;
63
64 private:
65   sg_size_t size_;
66   std::string path_;
67   std::string fullpath_;
68   sg_size_t current_position_ = SEEK_SET;
69   void* userdata_             = nullptr;
70 };
71
72 class XBT_PUBLIC FileSystemStorageExt {
73 public:
74   static simgrid::xbt::Extension<Storage, FileSystemStorageExt> EXTENSION_ID;
75   explicit FileSystemStorageExt(Storage* ptr);
76   ~FileSystemStorageExt();
77   std::map<std::string, sg_size_t>* parseContent(std::string filename);
78   std::map<std::string, sg_size_t>* getContent() { return content_; }
79   sg_size_t getSize() { return size_; }
80   sg_size_t getUsedSize() { return usedSize_; }
81   void decrUsedSize(sg_size_t size) { usedSize_ -= size; }
82   void incrUsedSize(sg_size_t size) { usedSize_ += size; }
83 private:
84   std::map<std::string, sg_size_t>* content_;
85   sg_size_t usedSize_ = 0;
86   sg_size_t size_     = 0;
87 };
88
89 class XBT_PUBLIC FileDescriptorHostExt {
90 public:
91   static simgrid::xbt::Extension<Host, FileDescriptorHostExt> EXTENSION_ID;
92   FileDescriptorHostExt() = default;
93   ~FileDescriptorHostExt() { delete file_descriptor_table; }
94   std::vector<int>* file_descriptor_table = nullptr; // Created lazily on need
95 };
96 }
97 } // namespace simgrid::s4u
98
99 #endif /* SIMGRID_S4U_HOST_HPP */