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->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 (localStorage->getSizeUsed() >= localStorage->getSize())
81 /* Substract the part of the file that might disappear from the used sized on the storage element */
82 localStorage->decrUsedSize(size_ - current_position_);
84 sg_size_t write_size = localStorage->write(size);
85 localStorage->incrUsedSize(write_size);
87 current_position_ += write_size;
88 size_ = current_position_;
90 localStorage->getContent()->erase(path_);
91 localStorage->getContent()->insert({path_, size_});
96 sg_size_t File::size()
101 void File::seek(sg_offset_t offset)
103 current_position_ = offset;
106 void File::seek(sg_offset_t offset, int origin)
110 current_position_ = offset;
113 current_position_ += offset;
116 current_position_ = size_ + offset;
123 sg_size_t File::tell()
125 return current_position_;
128 void File::move(std::string fullpath)
130 /* Check if the new full path is on the same mount point */
131 if (not strncmp(mount_point_.c_str(), fullpath.c_str(), mount_point_.length())) {
132 std::map<std::string, sg_size_t>* content = localStorage->getContent();
133 auto sz = content->find(path_);
134 if (sz != content->end()) { // src file exists
135 sg_size_t new_size = sz->second;
136 content->erase(path_);
137 std::string path = fullpath.substr(mount_point_.length(), fullpath.length());
138 content->insert({path.c_str(), new_size});
139 XBT_DEBUG("Move file from %s to %s, size '%llu'", path_.c_str(), fullpath.c_str(), new_size);
141 XBT_WARN("File %s doesn't exist", path_.c_str());
144 XBT_WARN("New full path %s is not on the same mount point: %s.", fullpath.c_str(), mount_point_.c_str());
150 /* Check if the file is on local storage */
151 if (localStorage->getContent()->find(path_) == localStorage->getContent()->end()) {
152 XBT_WARN("File %s is not on disk %s. Impossible to unlink", path_.c_str(), localStorage->getCname());
155 XBT_DEBUG("UNLINK %s on disk '%s'", path_.c_str(), localStorage->getCname());
156 localStorage->decrUsedSize(size_);
158 // Remove the file from storage
159 localStorage->getContent()->erase(fullpath_);
165 FileSystemStorageExt::FileSystemStorageExt(simgrid::s4u::Storage* ptr)
167 content_ = parseContent(ptr->getImpl()->content_name);
170 FileSystemStorageExt::~FileSystemStorageExt()
175 std::map<std::string, sg_size_t>* FileSystemStorageExt::parseContent(std::string filename)
177 if (filename.empty())
180 std::map<std::string, sg_size_t>* parse_content = new std::map<std::string, sg_size_t>();
182 std::ifstream* fs = surf_ifsopen(filename);
185 std::vector<std::string> tokens;
187 std::getline(*fs, line);
189 if (line.length() > 0) {
190 boost::split(tokens, line, boost::is_any_of(" \t"), boost::token_compress_on);
191 xbt_assert(tokens.size() == 2, "Parse error in %s: %s", filename.c_str(), line.c_str());
192 sg_size_t size = std::stoull(tokens.at(1));
195 parse_content->insert({tokens.front(), size});
197 } while (not fs->eof());
199 return parse_content;
204 using simgrid::s4u::FileSystemStorageExt;
206 static void onStorageCreation(simgrid::s4u::Storage& st)
208 st.extension_set(new FileSystemStorageExt(&st));
211 static void onStorageDestruction(simgrid::s4u::Storage& st)
213 delete st.extension<FileSystemStorageExt>();
216 /* **************************** Public interface *************************** */
219 void sg_storage_file_system_init()
222 if (FileSystemStorageExt::EXTENSION_ID.valid())
225 FileSystemStorageExt::EXTENSION_ID = simgrid::s4u::Storage::extension_create<FileSystemStorageExt>();
227 simgrid::s4u::Storage::onCreation.connect(&onStorageCreation);
228 simgrid::s4u::Storage::onDestruction.connect(&onStorageDestruction);