Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
b5dba9050b2f53e3ebad9248aa130f19ad67248a
[simgrid.git] / src / s4u / s4u_file.cpp
1 /* Copyright (c) 2015-2017. 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 "xbt/log.h"
7
8 #include "simgrid/s4u/File.hpp"
9 #include "simgrid/s4u/Host.hpp"
10 #include "simgrid/s4u/Storage.hpp"
11 #include "simgrid/simix.hpp"
12 #include "src/surf/HostImpl.hpp"
13
14 #include <algorithm>
15 #include <boost/algorithm/string.hpp>
16 #include <boost/algorithm/string/join.hpp>
17 #include <boost/algorithm/string/split.hpp>
18 #include <fstream>
19
20 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_file,"S4U files");
21
22 namespace simgrid {
23 namespace s4u {
24 simgrid::xbt::Extension<s4u::Storage, FileSystemStorageExt> FileSystemStorageExt::EXTENSION_ID;
25
26 File::File(std::string fullpath, void* userdata) : File(fullpath, Host::current(), userdata){};
27
28 File::File(std::string fullpath, sg_host_t host, void* userdata) : fullpath_(fullpath), userdata_(userdata)
29 {
30   // this cannot fail because we get a xbt_die if the mountpoint does not exist
31   Storage* st                  = nullptr;
32   size_t longest_prefix_length = 0;
33   XBT_DEBUG("Search for storage name for '%s' on '%s'", fullpath.c_str(), host->getCname());
34
35   for (auto const& mnt : host->getMountedStorages()) {
36     XBT_DEBUG("See '%s'", mnt.first.c_str());
37     mount_point_ = fullpath.substr(0, mnt.first.length());
38
39     if (mount_point_ == mnt.first && mnt.first.length() > longest_prefix_length) {
40       /* The current mount name is found in the full path and is bigger than the previous*/
41       longest_prefix_length = mnt.first.length();
42       st                    = mnt.second;
43     }
44   }
45   if (longest_prefix_length > 0) { /* Mount point found, split fullpath into mount_name and path+filename*/
46     mount_point_ = fullpath.substr(0, longest_prefix_length);
47     path_        = fullpath.substr(longest_prefix_length, fullpath.length());
48   } else
49     xbt_die("Can't find mount point for '%s' on '%s'", fullpath.c_str(), host->getCname());
50
51   localStorage = st;
52
53   XBT_DEBUG("\tOpen file '%s'", path_.c_str());
54   std::map<std::string, sg_size_t>* content = localStorage->extension<FileSystemStorageExt>()->getContent();
55   // if file does not exist create an empty file
56   auto sz = content->find(path_);
57   if (sz != content->end()) {
58     size_ = sz->second;
59   } else {
60     size_ = 0;
61     content->insert({path_, size_});
62     XBT_DEBUG("File '%s' was not found, file created.", path_.c_str());
63   }
64 }
65
66 sg_size_t File::read(sg_size_t size)
67 {
68   XBT_DEBUG("READ %s on disk '%s'", getPath(), localStorage->getCname());
69   // if the current position is close to the end of the file, we may not be able to read the requested size
70   sg_size_t read_size = localStorage->read(std::min(size, size_ - current_position_));
71   current_position_ += read_size;
72   return read_size;
73 }
74
75 sg_size_t File::write(sg_size_t size)
76 {
77   XBT_DEBUG("WRITE %s on disk '%s'. size '%llu/%llu'", getPath(), localStorage->getCname(), size, size_);
78   // If the storage is full before even starting to write
79   if (sg_storage_get_size_used(localStorage) >= sg_storage_get_size(localStorage))
80     return 0;
81   /* Substract the part of the file that might disappear from the used sized on the storage element */
82   localStorage->extension<FileSystemStorageExt>()->decrUsedSize(size_ - current_position_);
83
84   sg_size_t write_size = localStorage->write(size);
85   localStorage->extension<FileSystemStorageExt>()->incrUsedSize(write_size);
86
87   current_position_ += write_size;
88   size_ = current_position_;
89   std::map<std::string, sg_size_t>* content = localStorage->extension<FileSystemStorageExt>()->getContent();
90
91   content->erase(path_);
92   content->insert({path_, size_});
93
94   return write_size;
95 }
96
97 sg_size_t File::size()
98 {
99   return size_;
100 }
101
102 void File::seek(sg_offset_t offset)
103 {
104   current_position_ = offset;
105 }
106
107 void File::seek(sg_offset_t offset, int origin)
108 {
109   switch (origin) {
110     case SEEK_SET:
111       current_position_ = offset;
112       break;
113     case SEEK_CUR:
114       current_position_ += offset;
115       break;
116     case SEEK_END:
117       current_position_ = size_ + offset;
118       break;
119     default:
120       break;
121   }
122 }
123
124 sg_size_t File::tell()
125 {
126   return current_position_;
127 }
128
129 void File::move(std::string fullpath)
130 {
131   /* Check if the new full path is on the same mount point */
132   if (not strncmp(mount_point_.c_str(), fullpath.c_str(), mount_point_.length())) {
133     std::map<std::string, sg_size_t>* content = localStorage->extension<FileSystemStorageExt>()->getContent();
134     auto sz = content->find(path_);
135     if (sz != content->end()) { // src file exists
136       sg_size_t new_size = sz->second;
137       content->erase(path_);
138       std::string path = fullpath.substr(mount_point_.length(), fullpath.length());
139       content->insert({path.c_str(), new_size});
140       XBT_DEBUG("Move file from %s to %s, size '%llu'", path_.c_str(), fullpath.c_str(), new_size);
141     } else {
142       XBT_WARN("File %s doesn't exist", path_.c_str());
143     }
144   } else {
145     XBT_WARN("New full path %s is not on the same mount point: %s.", fullpath.c_str(), mount_point_.c_str());
146   }
147 }
148
149 int File::unlink()
150 {
151   /* Check if the file is on local storage */
152   std::map<std::string, sg_size_t>* content = localStorage->extension<FileSystemStorageExt>()->getContent();
153
154   if (content->find(path_) == content->end()) {
155     XBT_WARN("File %s is not on disk %s. Impossible to unlink", path_.c_str(), localStorage->getCname());
156     return -1;
157   } else {
158     XBT_DEBUG("UNLINK %s on disk '%s'", path_.c_str(), localStorage->getCname());
159     localStorage->extension<FileSystemStorageExt>()->decrUsedSize(size_);
160
161     // Remove the file from storage
162     content->erase(fullpath_);
163
164     return 0;
165   }
166 }
167
168 FileSystemStorageExt::FileSystemStorageExt(simgrid::s4u::Storage* ptr)
169 {
170   content_ = parseContent(ptr->getImpl()->content_name);
171 }
172
173 FileSystemStorageExt::~FileSystemStorageExt()
174 {
175   delete content_;
176 }
177
178 std::map<std::string, sg_size_t>* FileSystemStorageExt::parseContent(std::string filename)
179 {
180   if (filename.empty())
181     return nullptr;
182
183   std::map<std::string, sg_size_t>* parse_content = new std::map<std::string, sg_size_t>();
184
185   std::ifstream* fs = surf_ifsopen(filename);
186
187   std::string line;
188   std::vector<std::string> tokens;
189   do {
190     std::getline(*fs, line);
191     boost::trim(line);
192     if (line.length() > 0) {
193       boost::split(tokens, line, boost::is_any_of(" \t"), boost::token_compress_on);
194       xbt_assert(tokens.size() == 2, "Parse error in %s: %s", filename.c_str(), line.c_str());
195       sg_size_t size = std::stoull(tokens.at(1));
196
197       usedSize_ += size;
198       parse_content->insert({tokens.front(), size});
199     }
200   } while (not fs->eof());
201   delete fs;
202   return parse_content;
203 }
204 }
205 }
206
207 using simgrid::s4u::FileSystemStorageExt;
208
209 static void onStorageCreation(simgrid::s4u::Storage& st)
210 {
211   st.extension_set(new FileSystemStorageExt(&st));
212 }
213
214 static void onStorageDestruction(simgrid::s4u::Storage& st)
215 {
216   delete st.extension<FileSystemStorageExt>();
217 }
218
219 /* **************************** Public interface *************************** */
220 SG_BEGIN_DECL()
221
222 void sg_storage_file_system_init()
223 {
224
225   if (FileSystemStorageExt::EXTENSION_ID.valid())
226     return;
227
228   FileSystemStorageExt::EXTENSION_ID = simgrid::s4u::Storage::extension_create<FileSystemStorageExt>();
229
230   simgrid::s4u::Storage::onCreation.connect(&onStorageCreation);
231   simgrid::s4u::Storage::onDestruction.connect(&onStorageDestruction);
232 }
233
234 sg_size_t sg_storage_get_size_free(sg_storage_t st)
235 {
236   return st->getImpl()->getSize() - st->extension<FileSystemStorageExt>()->getUsedSize();
237 }
238
239 sg_size_t sg_storage_get_size_used(sg_storage_t st)
240 {
241   return st->extension<FileSystemStorageExt>()->getUsedSize();
242 }
243
244 sg_size_t sg_storage_get_size(sg_storage_t st)
245 {
246   return st->getImpl()->getSize();
247 }
248
249 SG_END_DECL()