Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
pluginify storage contents
[simgrid.git] / include / simgrid / s4u / File.hpp
1 /* Copyright (c) 2006-2015. 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 XBT_PUBLIC_CLASS File
28 {
29 public:
30   File(std::string fullpath, void* userdata);
31   File(std::string fullpath, sg_host_t host, void* userdata);
32   ~File() = default;
33
34   /** Retrieves the path to the file */
35   const char* getPath() { return fullpath_.c_str(); }
36
37   /** Simulates a local read action. Returns the size of data actually read */
38   sg_size_t read(sg_size_t size);
39
40   /** Simulates a write action. Returns the size of data actually written. */
41   sg_size_t write(sg_size_t size);
42
43   /** Allows to store user data on that host */
44   void setUserdata(void* data) { userdata_ = data; }
45   /** Retrieves the previously stored data */
46   void* getUserdata() { return userdata_; }
47
48   /** Retrieve the datasize */
49   sg_size_t size();
50
51   /** Sets the file head to the given position. */
52   void seek(sg_offset_t pos);
53   void seek(sg_offset_t pos, int origin);
54
55   /** Retrieves the current file position */
56   sg_size_t tell();
57
58   /** Rename a file. WARNING: It is forbidden to move the file to another mount point */
59   void move(std::string fullpath);
60
61   /** Remove a file from disk */
62   int unlink();
63
64   int desc_id = 0;
65   Storage* localStorage;
66   std::string mount_point_;
67
68 private:
69   sg_size_t size_;
70   std::string path_;
71   std::string fullpath_;
72   sg_size_t current_position_ = SEEK_SET;
73   void* userdata_             = nullptr;
74 };
75
76 class FileSystemStorageExt {
77 public:
78   static simgrid::xbt::Extension<simgrid::s4u::Storage, FileSystemStorageExt> EXTENSION_ID;
79   explicit FileSystemStorageExt(simgrid::s4u::Storage* ptr);
80   ~FileSystemStorageExt();
81   std::map<std::string, sg_size_t>* parseContent(std::string filename);
82   std::map<std::string, sg_size_t>* getContent() { return content_; }
83   sg_size_t getUsedSize() { return usedSize_; }
84   void decrUsedSize(sg_size_t size) { usedSize_ -= size; }
85   void incrUsedSize(sg_size_t size) { usedSize_ += size; }
86 private:
87   std::map<std::string, sg_size_t>* content_;
88   sg_size_t usedSize_ = 0;
89 };
90 }
91 } // namespace simgrid::s4u
92
93 #endif /* SIMGRID_S4U_HOST_HPP */