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/FileImpl.hpp"
13 #include "src/surf/HostImpl.hpp"
15 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_file,"S4U files");
20 File::File(std::string fullpath, void* userdata) : File(fullpath, Host::current(), userdata){};
22 File::File(std::string fullpath, sg_host_t host, void* userdata) : path_(fullpath), userdata_(userdata)
24 // this cannot fail because we get a xbt_die if the mountpoint does not exist
25 Storage* st = nullptr;
26 size_t longest_prefix_length = 0;
28 XBT_DEBUG("Search for storage name for '%s' on '%s'", fullpath.c_str(), host->getCname());
30 for (auto const& mnt : host->getMountedStorages()) {
31 XBT_DEBUG("See '%s'", mnt.first.c_str());
32 mount_point = fullpath.substr(0, mnt.first.length());
34 if (mount_point == mnt.first && mnt.first.length() > longest_prefix_length) {
35 /* The current mount name is found in the full path and is bigger than the previous*/
36 longest_prefix_length = mnt.first.length();
40 if (longest_prefix_length > 0) { /* Mount point found, split fullpath into mount_name and path+filename*/
41 mount_point = fullpath.substr(0, longest_prefix_length);
42 path = fullpath.substr(longest_prefix_length, fullpath.length());
44 xbt_die("Can't find mount point for '%s' on '%s'", fullpath.c_str(), host->getCname());
47 simgrid::simix::kernelImmediate([this, st, path] { return new simgrid::surf::FileImpl(st, path, mount_point); });
48 storage_type = st->getType();
49 storageId = st->getName();
54 simgrid::simix::kernelImmediate([this] { delete pimpl_; });
57 sg_size_t File::read(sg_size_t size)
59 return simcall_file_read(pimpl_, size);
62 sg_size_t File::write(sg_size_t size)
64 return simcall_file_write(pimpl_, size);
67 sg_size_t File::size()
69 return simgrid::simix::kernelImmediate([this] { return pimpl_->size(); });
72 void File::seek(sg_offset_t pos)
74 simgrid::simix::kernelImmediate([this, pos] { pimpl_->seek(pos, SEEK_SET); });
77 void File::seek(sg_offset_t pos, int origin)
79 simgrid::simix::kernelImmediate([this, pos, origin] { pimpl_->seek(pos, origin); });
82 sg_size_t File::tell()
84 return simgrid::simix::kernelImmediate([this] { return pimpl_->tell(); });
87 void File::move(std::string fullpath)
89 simgrid::simix::kernelImmediate([this, fullpath] { pimpl_->move(fullpath); });
94 return simgrid::simix::kernelImmediate([this] { return pimpl_->unlink(); });
97 }} // namespace simgrid::s4u