Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
af4a32d90792781efc4c5c6442282f100c38c5c5
[simgrid.git] / src / s4u / s4u_file.cpp
1 /* Copyright (c) 2015-2017. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 #include "xbt/log.h"
7
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"
14
15 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_file,"S4U files");
16
17 namespace simgrid {
18 namespace s4u {
19
20 File::File(std::string fullpath, void* userdata) : File(fullpath, Host::current(), userdata){};
21
22 File::File(std::string fullpath, sg_host_t host, void* userdata) : path_(fullpath), userdata_(userdata)
23 {
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;
27   std::string path;
28   XBT_DEBUG("Search for storage name for '%s' on '%s'", fullpath.c_str(), host->getCname());
29
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());
33
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();
37       st                    = mnt.second;
38     }
39   }
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());
43   } else
44     xbt_die("Can't find mount point for '%s' on '%s'", fullpath.c_str(), host->getCname());
45
46   pimpl_ =
47       simgrid::simix::kernelImmediate([this, st, path] { return new simgrid::surf::FileImpl(st, path, mount_point); });
48   localStorage = st;
49 }
50
51 File::~File()
52 {
53   simgrid::simix::kernelImmediate([this] { delete pimpl_; });
54 }
55
56 sg_size_t File::read(sg_size_t size)
57 {
58   XBT_DEBUG("READ %s on disk '%s'", getPath(), localStorage->getCname());
59   // if the current position is close to the end of the file, we may not be able to read the requested size
60   sg_size_t read_size = localStorage->read(std::min(size, this->size() - this->tell()));
61   pimpl_->incrPosition(read_size);
62   return read_size;
63 }
64
65 sg_size_t File::write(sg_size_t size)
66 {
67   XBT_DEBUG("WRITE %s on disk '%s'. size '%llu/%llu'", getPath(), localStorage->getCname(), size, this->size());
68   // If the storage is full before even starting to write
69   if (localStorage->getSizeUsed() >= localStorage->getSize())
70     return 0;
71   /* Substract the part of the file that might disappear from the used sized on the storage element */
72   localStorage->decrUsedSize(this->size() - this->tell());
73
74   sg_size_t write_size = localStorage->write(size);
75   pimpl_->incrPosition(write_size);
76   pimpl_->setSize(this->tell());
77
78   localStorage->getContent()->erase(pimpl_->getName());
79   localStorage->getContent()->insert({pimpl_->getName(), this->size()});
80
81   return write_size;
82 }
83
84 sg_size_t File::size()
85 {
86   return simgrid::simix::kernelImmediate([this] { return pimpl_->size(); });
87 }
88
89 void File::seek(sg_offset_t pos)
90 {
91   simgrid::simix::kernelImmediate([this, pos] { pimpl_->seek(pos, SEEK_SET); });
92 }
93
94 void File::seek(sg_offset_t pos, int origin)
95 {
96   simgrid::simix::kernelImmediate([this, pos, origin] { pimpl_->seek(pos, origin); });
97 }
98
99 sg_size_t File::tell()
100 {
101   return simgrid::simix::kernelImmediate([this] { return pimpl_->tell(); });
102 }
103
104 void File::move(std::string fullpath)
105 {
106   simgrid::simix::kernelImmediate([this, fullpath] { pimpl_->move(fullpath); });
107 }
108
109 int File::unlink()
110 {
111   return simgrid::simix::kernelImmediate([this] { return pimpl_->unlink(); });
112 }
113
114 }} // namespace simgrid::s4u