Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
rename a field for clarity: that's not a signal
[simgrid.git] / src / surf / FileImpl.cpp
1 /* Copyright (c) 2017. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "src/surf/FileImpl.hpp"
8 #include "src/surf/StorageImpl.hpp"
9
10 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_file, surf, "Logging specific to the SURF file module");
11 namespace simgrid {
12 namespace surf {
13
14 FileImpl::FileImpl(sg_storage_t st, std::string path, std::string mount) : path_(path), mount_point_(mount)
15 {
16   XBT_DEBUG("\tOpen file '%s'", path.c_str());
17   location_ = st->getImpl();
18   std::map<std::string, sg_size_t>* content = location_->getContent();
19   // if file does not exist create an empty file
20   auto sz = content->find(path);
21   if (sz != content->end()) {
22     size_ = sz->second;
23   } else {
24     size_ = 0;
25     content->insert({path, size_});
26     XBT_DEBUG("File '%s' was not found, file created.", path.c_str());
27   }
28 }
29
30 int FileImpl::seek(sg_offset_t offset, int origin)
31 {
32   switch (origin) {
33     case SEEK_SET:
34       current_position_ = offset;
35       return 0;
36     case SEEK_CUR:
37       current_position_ += offset;
38       return 0;
39     case SEEK_END:
40       current_position_ = size_ + offset;
41       return 0;
42     default:
43       return -1;
44   }
45 }
46
47 int FileImpl::unlink()
48 {
49   /* Check if the file is on this storage */
50   if (location_->getContent()->find(path_) == location_->getContent()->end()) {
51     XBT_WARN("File %s is not on disk %s. Impossible to unlink", getCname(), location_->getCname());
52     return -1;
53   } else {
54     XBT_DEBUG("UNLINK %s on disk '%s'", getCname(), location_->getCname());
55     location_->usedSize_ -= size_;
56
57     // Remove the file from storage
58     location_->getContent()->erase(path_);
59
60     return 0;
61   }
62 }
63
64 void FileImpl::move(std::string fullpath)
65 {
66   /* Check if the new full path is on the same mount point */
67   if (not strncmp(mount_point_.c_str(), fullpath.c_str(), mount_point_.size())) {
68     std::map<std::string, sg_size_t>* content = location_->getContent();
69     auto sz = content->find(path_);
70     if (sz != content->end()) { // src file exists
71       sg_size_t new_size = sz->second;
72       content->erase(path_);
73       std::string path = fullpath.substr(mount_point_.length(), fullpath.length());
74       content->insert({path.c_str(), new_size});
75       XBT_DEBUG("Move file from %s to %s, size '%llu'", path_.c_str(), fullpath.c_str(), new_size);
76     } else {
77       XBT_WARN("File %s doesn't exist", path_.c_str());
78     }
79   } else {
80     XBT_WARN("New full path %s is not on the same mount point: %s.", fullpath.c_str(), mount_point_.c_str());
81   }
82 }
83 }
84 }