Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
5c7cda05ea2d6480ce32eb676b6b8f11d5f278fc
[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/HostImpl.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   std::map<std::string, sg_size_t>* content = st->pimpl_->content_;
18   // if file does not exist create an empty file
19   if (content->find(path) != content->end())
20     size_ = content->at(path);
21   else {
22     size_ = 0;
23     content->insert({path, size_});
24     XBT_DEBUG("File '%s' was not found, file created.", path.c_str());
25   }
26 }
27
28 int FileImpl::seek(sg_offset_t offset, int origin)
29 {
30   switch (origin) {
31     case SEEK_SET:
32       current_position_ = offset;
33       return 0;
34     case SEEK_CUR:
35       current_position_ += offset;
36       return 0;
37     case SEEK_END:
38       current_position_ = size_ + offset;
39       return 0;
40     default:
41       return -1;
42   }
43 }
44
45 int FileImpl::unlink(sg_host_t host)
46 {
47   simgrid::surf::StorageImpl* st = host->pimpl_->findStorageOnMountList(mount_point_.c_str());
48   /* Check if the file is on this storage */
49   if (st->content_->find(path_) == st->content_->end()) {
50     XBT_WARN("File %s is not on disk %s. Impossible to unlink", cname(), st->cname());
51     return -1;
52   } else {
53     XBT_DEBUG("UNLINK %s on disk '%s'", cname(), st->cname());
54     st->usedSize_ -= size_;
55
56     // Remove the file from storage
57     st->content_->erase(path_);
58
59     return 0;
60   }
61 }
62
63 void FileImpl::move(sg_host_t host, const char* fullpath)
64 {
65   /* Check if the new full path is on the same mount point */
66   if (not strncmp(mount_point_.c_str(), fullpath, mount_point_.size())) {
67     std::map<std::string, sg_size_t>* content = host->pimpl_->findStorageOnMountList(mount_point_.c_str())->content_;
68     if (content->find(path_) != content->end()) { // src file exists
69       sg_size_t new_size = content->at(path_);
70       content->erase(path_);
71       std::string path = std::string(fullpath).substr(mount_point_.size(), strlen(fullpath));
72       content->insert({path.c_str(), new_size});
73       XBT_DEBUG("Move file from %s to %s, size '%llu'", path_.c_str(), fullpath, new_size);
74     } else {
75       XBT_WARN("File %s doesn't exist", path_.c_str());
76     }
77   } else {
78     XBT_WARN("New full path %s is not on the same mount point: %s.", fullpath, mount_point_.c_str());
79   }
80 }
81 }
82 }