1 /* Copyright (c) 2015-2017. The SimGrid Team. All rights reserved. */
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. */
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"
15 #include <boost/algorithm/string.hpp>
16 #include <boost/algorithm/string/join.hpp>
17 #include <boost/algorithm/string/split.hpp>
20 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_file,"S4U files");
24 simgrid::xbt::Extension<s4u::Storage, FileSystemStorageExt> FileSystemStorageExt::EXTENSION_ID;
26 File::File(std::string fullpath, void* userdata) : File(fullpath, Host::current(), userdata){};
28 File::File(std::string fullpath, sg_host_t host, void* userdata) : fullpath_(fullpath), userdata_(userdata)
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());
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());
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();
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());
49 xbt_die("Can't find mount point for '%s' on '%s'", fullpath.c_str(), host->getCname());
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()) {
61 content->insert({path_, size_});
62 XBT_DEBUG("File '%s' was not found, file created.", path_.c_str());
66 sg_size_t File::read(sg_size_t size)
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;
75 sg_size_t File::write(sg_size_t size)
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))
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_);
84 sg_size_t write_size = localStorage->write(size);
85 localStorage->extension<FileSystemStorageExt>()->incrUsedSize(write_size);
87 current_position_ += write_size;
88 size_ = current_position_;
89 std::map<std::string, sg_size_t>* content = localStorage->extension<FileSystemStorageExt>()->getContent();
91 content->erase(path_);
92 content->insert({path_, size_});
97 sg_size_t File::size()
102 void File::seek(sg_offset_t offset)
104 current_position_ = offset;
107 void File::seek(sg_offset_t offset, int origin)
111 current_position_ = offset;
114 current_position_ += offset;
117 current_position_ = size_ + offset;
124 sg_size_t File::tell()
126 return current_position_;
129 void File::move(std::string fullpath)
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);
142 XBT_WARN("File %s doesn't exist", path_.c_str());
145 XBT_WARN("New full path %s is not on the same mount point: %s.", fullpath.c_str(), mount_point_.c_str());
151 /* Check if the file is on local storage */
152 std::map<std::string, sg_size_t>* content = localStorage->extension<FileSystemStorageExt>()->getContent();
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());
158 XBT_DEBUG("UNLINK %s on disk '%s'", path_.c_str(), localStorage->getCname());
159 localStorage->extension<FileSystemStorageExt>()->decrUsedSize(size_);
161 // Remove the file from storage
162 content->erase(fullpath_);
168 FileSystemStorageExt::FileSystemStorageExt(simgrid::s4u::Storage* ptr)
170 content_ = parseContent(ptr->getImpl()->content_name);
173 FileSystemStorageExt::~FileSystemStorageExt()
178 std::map<std::string, sg_size_t>* FileSystemStorageExt::parseContent(std::string filename)
180 if (filename.empty())
183 std::map<std::string, sg_size_t>* parse_content = new std::map<std::string, sg_size_t>();
185 std::ifstream* fs = surf_ifsopen(filename);
188 std::vector<std::string> tokens;
190 std::getline(*fs, 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));
198 parse_content->insert({tokens.front(), size});
200 } while (not fs->eof());
202 return parse_content;
207 using simgrid::s4u::FileSystemStorageExt;
209 static void onStorageCreation(simgrid::s4u::Storage& st)
211 st.extension_set(new FileSystemStorageExt(&st));
214 static void onStorageDestruction(simgrid::s4u::Storage& st)
216 delete st.extension<FileSystemStorageExt>();
219 /* **************************** Public interface *************************** */
222 void sg_storage_file_system_init()
225 if (FileSystemStorageExt::EXTENSION_ID.valid())
228 FileSystemStorageExt::EXTENSION_ID = simgrid::s4u::Storage::extension_create<FileSystemStorageExt>();
230 simgrid::s4u::Storage::onCreation.connect(&onStorageCreation);
231 simgrid::s4u::Storage::onDestruction.connect(&onStorageDestruction);
234 sg_size_t sg_storage_get_size_free(sg_storage_t st)
236 return st->getImpl()->getSize() - st->extension<FileSystemStorageExt>()->getUsedSize();
239 sg_size_t sg_storage_get_size_used(sg_storage_t st)
241 return st->extension<FileSystemStorageExt>()->getUsedSize();
244 sg_size_t sg_storage_get_size(sg_storage_t st)
246 return st->getImpl()->getSize();