/** Remove a file from disk */
int unlink();
- int unlink(sg_host_t host);
/* FIXME: add these to the S4U API:
XBT_PUBLIC(const char *) MSG_file_get_name(msg_file_t file);
void resume() override;
void post() override;
- sg_host_t host = nullptr;
surf_action_t surf_io = nullptr;
};
*/
msg_error_t MSG_file_unlink(msg_file_t fd)
{
- /* Find the host where the file is physically located (remote or local)*/
- msg_storage_t storage_src = simgrid::s4u::Storage::byName(fd->storageId);
- msg_host_t attached_host = storage_src->getHost();
- fd->unlink(attached_host);
+ fd->unlink();
delete fd;
return MSG_OK;
}
void File::move(const char* fullpath)
{
- sg_host_t host = Host::current();
- simgrid::simix::kernelImmediate([this, host, fullpath] { pimpl_->move(host, fullpath); });
+ simgrid::simix::kernelImmediate([this, fullpath] { pimpl_->move(fullpath); });
}
int File::unlink()
{
- return unlink(Host::current());
-}
-
-int File::unlink(sg_host_t host)
-{
- return simgrid::simix::kernelImmediate([this, host] { return pimpl_->unlink(host); });
+ return simgrid::simix::kernelImmediate([this] { return pimpl_->unlink(); });
}
}} // namespace simgrid::s4u
THROWF(host_error, 0, "Host %s failed, you cannot call this function", host->getCname());
simgrid::kernel::activity::IoImpl* synchro = new simgrid::kernel::activity::IoImpl();
- synchro->host = host;
- synchro->surf_io = host->pimpl_->read(file, size);
+ synchro->surf_io = file->read(size);
synchro->surf_io->setData(synchro);
XBT_DEBUG("Create io synchro %p", synchro);
THROWF(host_error, 0, "Host %s failed, you cannot call this function", host->getCname());
simgrid::kernel::activity::IoImpl* synchro = new simgrid::kernel::activity::IoImpl();
- synchro->host = host;
- synchro->surf_io = host->pimpl_->write(file, size);
+ synchro->surf_io = file->write(size);
synchro->surf_io->setData(synchro);
XBT_DEBUG("Create io synchro %p", synchro);
FileImpl::FileImpl(sg_storage_t st, std::string path, std::string mount) : path_(path), mount_point_(mount)
{
XBT_DEBUG("\tOpen file '%s'", path.c_str());
- std::map<std::string, sg_size_t>* content = st->pimpl_->content_;
+ location_ = st->pimpl_;
+ std::map<std::string, sg_size_t>* content = location_->content_;
// if file does not exist create an empty file
if (content->find(path) != content->end())
size_ = content->at(path);
}
}
+Action* FileImpl::read(sg_size_t size)
+{
+ XBT_DEBUG("READ %s on disk '%s'", cname(), location_->cname());
+ if (current_position_ + size > size_) {
+ if (current_position_ > size_) {
+ size = 0;
+ } else {
+ size = size_ - current_position_;
+ }
+ current_position_ = size_;
+ } else
+ current_position_ += size;
+
+ return location_->read(size);
+}
+
+Action* FileImpl::write(sg_size_t size)
+{
+ XBT_DEBUG("WRITE %s on disk '%s'. size '%llu/%llu'", cname(), location_->cname(), size, size_);
+
+ StorageAction* action = location_->write(size);
+ action->file_ = this;
+ /* Substract the part of the file that might disappear from the used sized on the storage element */
+ location_->usedSize_ -= (size_ - current_position_);
+ // If the storage is full before even starting to write
+ if (location_->usedSize_ >= location_->size_) {
+ action->setState(Action::State::failed);
+ }
+ return action;
+}
+
int FileImpl::seek(sg_offset_t offset, int origin)
{
switch (origin) {
}
}
-int FileImpl::unlink(sg_host_t host)
+int FileImpl::unlink()
{
- simgrid::surf::StorageImpl* st = host->pimpl_->findStorageOnMountList(mount_point_.c_str());
/* Check if the file is on this storage */
- if (st->content_->find(path_) == st->content_->end()) {
- XBT_WARN("File %s is not on disk %s. Impossible to unlink", cname(), st->cname());
+ if (location_->content_->find(path_) == location_->content_->end()) {
+ XBT_WARN("File %s is not on disk %s. Impossible to unlink", cname(), location_->cname());
return -1;
} else {
- XBT_DEBUG("UNLINK %s on disk '%s'", cname(), st->cname());
- st->usedSize_ -= size_;
+ XBT_DEBUG("UNLINK %s on disk '%s'", cname(), location_->cname());
+ location_->usedSize_ -= size_;
// Remove the file from storage
- st->content_->erase(path_);
+ location_->content_->erase(path_);
return 0;
}
}
-void FileImpl::move(sg_host_t host, const char* fullpath)
+void FileImpl::move(const char* fullpath)
{
/* Check if the new full path is on the same mount point */
if (not strncmp(mount_point_.c_str(), fullpath, mount_point_.size())) {
- std::map<std::string, sg_size_t>* content = host->pimpl_->findStorageOnMountList(mount_point_.c_str())->content_;
+ std::map<std::string, sg_size_t>* content = location_->content_;
if (content->find(path_) != content->end()) { // src file exists
sg_size_t new_size = content->at(path_);
content->erase(path_);
void incrPosition(sg_size_t incr) { current_position_ += incr; }
sg_size_t tell() { return current_position_; }
int seek(sg_offset_t offset, int origin);
- int unlink(sg_host_t host);
- void move(sg_host_t host, const char* fullpath);
+ int unlink();
+ void move(const char* fullpath);
+ Action* read(sg_size_t size);
+ Action* write(sg_size_t size);
private:
+ StorageImpl* location_;
std::string path_;
std::string mount_point_;
sg_size_t size_;
piface_->pimpl_ = this;
}
-simgrid::surf::StorageImpl* HostImpl::findStorageOnMountList(const char* mount)
-{
- XBT_DEBUG("Search for storage name '%s' on '%s'", mount, piface_->getCname());
- if (storage_.find(mount) == storage_.end())
- xbt_die("Can't find mount '%s' for '%s'", mount, piface_->getCname());
-
- return storage_.at(mount);
-}
-
void HostImpl::getAttachedStorageList(std::vector<const char*>* storages)
{
for (auto s : storage_)
storages->push_back(s.second->piface_.getName());
}
-Action* HostImpl::read(surf_file_t fd, sg_size_t size)
-{
- simgrid::surf::StorageImpl* st = findStorageOnMountList(fd->mount());
- XBT_DEBUG("READ %s on disk '%s'", fd->cname(), st->cname());
- if (fd->tell() + size > fd->size()) {
- if (fd->tell() > fd->size()) {
- size = 0;
- } else {
- size = fd->size() - fd->tell();
- }
- fd->setPosition(fd->size());
- } else
- fd->incrPosition(size);
-
- return st->read(size);
-}
-
-Action* HostImpl::write(surf_file_t fd, sg_size_t size)
-{
- simgrid::surf::StorageImpl* st = findStorageOnMountList(fd->mount());
- XBT_DEBUG("WRITE %s on disk '%s'. size '%llu/%llu'", fd->cname(), st->cname(), size, fd->size());
-
- StorageAction* action = st->write(size);
- action->file_ = fd;
- /* Substract the part of the file that might disappear from the used sized on the storage element */
- st->usedSize_ -= (fd->size() - fd->tell());
- // If the storage is full before even starting to write
- if (st->usedSize_ >= st->size_) {
- action->setState(Action::State::failed);
- }
- return action;
-}
-
}
}
explicit HostImpl(s4u::Host* host);
virtual ~HostImpl() = default;
- /** @brief Return the storage of corresponding mount point */
- virtual simgrid::surf::StorageImpl* findStorageOnMountList(const char* storage);
-
- /** @brief Get the xbt_dynar_t of storages attached to the Host */
+ /** @brief Get the vector of storages (by names) attached to the Host */
virtual void getAttachedStorageList(std::vector<const char*>* storages);
- /**
- * @brief Read a file
- *
- * @param fd The file descriptor to read
- * @param size The size in bytes to read
- * @return The StorageAction corresponding to the reading
- */
- virtual Action* read(surf_file_t fd, sg_size_t size);
-
- /**
- * @brief Write a file
- *
- * @param fd The file descriptor to write
- * @param size The size in bytes to write
- * @return The StorageAction corresponding to the writing
- */
- virtual Action* write(surf_file_t fd, sg_size_t size);
-
std::map<std::string, simgrid::surf::StorageImpl*> storage_;
simgrid::s4u::Host* piface_ = nullptr;