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 "src/surf/HostImpl.hpp"
13 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_file,"S4U files");
18 File::File(const char* fullpath, void* userdata) : File(fullpath, Host::current(), userdata){};
20 File::File(const char* fullpath, sg_host_t host, void* userdata) : path_(fullpath), userdata_(userdata), host_(host)
22 // this cannot fail because we get a xbt_die if the mountpoint does not exist
23 Storage* st = nullptr;
24 size_t longest_prefix_length = 0;
26 XBT_DEBUG("Search for storage name for '%s' on '%s'", fullpath, host->cname());
28 for (auto mnt : host->mountedStorages()) {
29 XBT_DEBUG("See '%s'", mnt.first.c_str());
30 mount_point = std::string(fullpath).substr(0, mnt.first.size());
32 if (mount_point == mnt.first && mnt.first.length() > longest_prefix_length) {
33 /* The current mount name is found in the full path and is bigger than the previous*/
34 longest_prefix_length = mnt.first.length();
38 if (longest_prefix_length > 0) { /* Mount point found, split fullpath into mount_name and path+filename*/
39 mount_point = std::string(fullpath).substr(0, longest_prefix_length);
40 path = std::string(fullpath).substr(longest_prefix_length, strlen(fullpath));
42 xbt_die("Can't find mount point for '%s' on '%s'", fullpath, host->cname());
44 pimpl_ = simcall_file_open(mount_point.c_str(), path.c_str(), st);
45 storage_type = st->type();
46 storageId = st->name();
51 simcall_file_close(pimpl_, host_);
54 sg_size_t File::read(sg_size_t size)
56 return simcall_file_read(pimpl_, size, Host::current());
59 sg_size_t File::write(sg_size_t size)
61 return simcall_file_write(pimpl_,size, Host::current());
64 sg_size_t File::write(sg_size_t size, sg_host_t host)
66 return simcall_file_write(pimpl_, size, host);
69 sg_size_t File::size()
71 return simcall_file_get_size(pimpl_);
74 void File::seek(sg_size_t pos)
76 simcall_file_seek(pimpl_,pos,SEEK_SET);
79 sg_size_t File::tell()
81 return simcall_file_tell(pimpl_);
84 void File::move(const char* fullpath)
86 simcall_file_move(pimpl_,fullpath);
91 simcall_file_unlink(pimpl_, Host::current());
94 void File::unlink(sg_host_t host)
96 simcall_file_unlink(pimpl_, host);
99 }} // namespace simgrid::s4u