1 /* Copyright (c) 2017. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
7 #include "src/surf/FileImpl.hpp"
8 #include "src/surf/HostImpl.hpp"
10 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_file, surf, "Logging specific to the SURF file module");
14 FileImpl::FileImpl(sg_storage_t st, std::string path, std::string mount) : path_(path), mount_point_(mount)
16 XBT_DEBUG("\tOpen file '%s'", path.c_str());
17 std::map<std::string, sg_size_t>* content = st->pimpl_->content_;
18 // if file does not exist create an empty file
19 if (content->find(path) != content->end())
20 size_ = content->at(path);
23 content->insert({path, size_});
24 XBT_DEBUG("File '%s' was not found, file created.", path.c_str());
28 int FileImpl::seek(sg_offset_t offset, int origin)
32 current_position_ = offset;
35 current_position_ += offset;
38 current_position_ = size_ + offset;
45 int FileImpl::unlink(sg_host_t host)
47 simgrid::surf::StorageImpl* st = host->pimpl_->findStorageOnMountList(mount_point_.c_str());
48 /* Check if the file is on this storage */
49 if (st->content_->find(path_) == st->content_->end()) {
50 XBT_WARN("File %s is not on disk %s. Impossible to unlink", cname(), st->cname());
53 XBT_DEBUG("UNLINK %s on disk '%s'", cname(), st->cname());
54 st->usedSize_ -= size_;
56 // Remove the file from storage
57 st->content_->erase(path_);
63 void FileImpl::move(sg_host_t host, const char* fullpath)
65 /* Check if the new full path is on the same mount point */
66 if (not strncmp(mount_point_.c_str(), fullpath, mount_point_.size())) {
67 std::map<std::string, sg_size_t>* content = host->pimpl_->findStorageOnMountList(mount_point_.c_str())->content_;
68 if (content->find(path_) != content->end()) { // src file exists
69 sg_size_t new_size = content->at(path_);
70 content->erase(path_);
71 std::string path = std::string(fullpath).substr(mount_point_.size(), strlen(fullpath));
72 content->insert({path.c_str(), new_size});
73 XBT_DEBUG("Move file from %s to %s, size '%llu'", path_.c_str(), fullpath, new_size);
75 XBT_WARN("File %s doesn't exist", path_.c_str());
78 XBT_WARN("New full path %s is not on the same mount point: %s.", fullpath, mount_point_.c_str());