Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
free the engine at the end of test
[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(const char* fullpath, void* userdata) : File(fullpath, Host::current(), userdata){};
21
22 File::File(const char* fullpath, sg_host_t host, void* userdata) : path_(fullpath), userdata_(userdata), host_(host)
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, host->cname());
29
30   for (auto mnt : host->mountedStorages()) {
31     XBT_DEBUG("See '%s'", mnt.first.c_str());
32     mount_point = std::string(fullpath).substr(0, mnt.first.size());
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 = std::string(fullpath).substr(0, longest_prefix_length);
42     path        = std::string(fullpath).substr(longest_prefix_length, strlen(fullpath));
43   } else
44     xbt_die("Can't find mount point for '%s' on '%s'", fullpath, host->cname());
45
46   pimpl_       = simcall_file_open(mount_point.c_str(), path.c_str(), st);
47   storage_type = st->type();
48   storageId    = st->name();
49 }
50
51 File::~File()
52 {
53   simcall_file_close(pimpl_, host_);
54 }
55
56 sg_size_t File::read(sg_size_t size)
57 {
58   return simcall_file_read(pimpl_, size, Host::current());
59 }
60
61 sg_size_t File::write(sg_size_t size)
62 {
63   return simcall_file_write(pimpl_,size, Host::current());
64 }
65
66 sg_size_t File::write(sg_size_t size, sg_host_t host)
67 {
68   return simcall_file_write(pimpl_, size, host);
69 }
70
71 sg_size_t File::size()
72 {
73   return simgrid::simix::kernelImmediate([this] { return pimpl_->size(); });
74 }
75
76 void File::seek(sg_offset_t pos)
77 {
78   simgrid::simix::kernelImmediate([this, pos] { pimpl_->seek(pos, SEEK_SET); });
79 }
80
81 void File::seek(sg_offset_t pos, int origin)
82 {
83   simgrid::simix::kernelImmediate([this, pos, origin] { pimpl_->seek(pos, origin); });
84 }
85
86 sg_size_t File::tell()
87 {
88   return simgrid::simix::kernelImmediate([this] { return pimpl_->tell(); });
89 }
90
91 void File::move(const char* fullpath)
92 {
93   simcall_file_move(pimpl_,fullpath);
94 }
95
96 void File::unlink()
97 {
98   simcall_file_unlink(pimpl_, Host::current());
99 }
100
101 void File::unlink(sg_host_t host)
102 {
103   simcall_file_unlink(pimpl_, host);
104 }
105
106 }} // namespace simgrid::s4u