Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
fix test (hate dict) and please sonar
[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 "src/surf/HostImpl.hpp"
12
13 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_file,"S4U files");
14
15 namespace simgrid {
16 namespace s4u {
17
18 File::File(const char* fullpath, void* userdata) : File(fullpath, Host::current(), userdata){};
19
20 File::File(const char* fullpath, sg_host_t host, void* userdata) : path_(fullpath), userdata_(userdata), host_(host)
21 {
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;
25   std::string path;
26   XBT_DEBUG("Search for storage name for '%s' on '%s'", fullpath, host->cname());
27
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());
31
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();
35       st                    = mnt.second;
36     }
37   }
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));
41   } else
42     xbt_die("Can't find mount point for '%s' on '%s'", fullpath, host->cname());
43
44   pimpl_       = simcall_file_open(mount_point.c_str(), path.c_str(), st);
45   storage_type = st->type();
46   storageId    = st->name();
47 }
48
49 File::~File()
50 {
51   simcall_file_close(pimpl_, host_);
52 }
53
54 sg_size_t File::read(sg_size_t size)
55 {
56   return simcall_file_read(pimpl_, size, Host::current());
57 }
58
59 sg_size_t File::write(sg_size_t size)
60 {
61   return simcall_file_write(pimpl_,size, Host::current());
62 }
63
64 sg_size_t File::write(sg_size_t size, sg_host_t host)
65 {
66   return simcall_file_write(pimpl_, size, host);
67 }
68
69 sg_size_t File::size()
70 {
71   return simcall_file_get_size(pimpl_);
72 }
73
74 void File::seek(sg_size_t pos)
75 {
76   simcall_file_seek(pimpl_,pos,SEEK_SET);
77 }
78
79 sg_size_t File::tell()
80 {
81   return simcall_file_tell(pimpl_);
82 }
83
84 void File::move(const char* fullpath)
85 {
86   simcall_file_move(pimpl_,fullpath);
87 }
88
89 void File::unlink()
90 {
91   simcall_file_unlink(pimpl_, Host::current());
92 }
93
94 void File::unlink(sg_host_t host)
95 {
96   simcall_file_unlink(pimpl_, host);
97 }
98
99 }} // namespace simgrid::s4u