OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 |
| 6 #include <ctime> |
| 7 #include <iostream> |
| 8 #include <vector> |
| 9 |
| 10 #include "inode.h" |
| 11 #include "memfilesystem.h" |
| 12 |
| 13 namespace filesystem{ |
| 14 |
| 15 Inode::Inode( |
| 16 unsigned long long appointed_inode, int file_type, |
| 17 const std::string& inode_modified,const std::string& file_modified, |
| 18 const std::string& file_access) |
| 19 : inode_number_(appointed_inode), |
| 20 file_type_(file_type) { |
| 21 set_inode_modified(inode_modified); |
| 22 set_file_modified(file_modified); |
| 23 set_file_access(file_access); |
| 24 } |
| 25 |
| 26 Inode::~Inode(){ |
| 27 } |
| 28 |
| 29 void Inode::set_inode_number(unsigned long long appointed_inode) { |
| 30 inode_number_ = appointed_inode; |
| 31 } |
| 32 |
| 33 unsigned long long Inode::inode_number() const { |
| 34 return inode_number_; |
| 35 } |
| 36 |
| 37 void Inode::set_file_type(int file_type){ |
| 38 switch (file_type) { |
| 39 case FILE_TYPE_FILE: |
| 40 case FILE_TYPE_DIR: |
| 41 file_type_ = file_type; |
| 42 break; |
| 43 default: |
| 44 file_type_ = FILE_TYPE_UNKNOWN; |
| 45 break; |
| 46 } |
| 47 } |
| 48 |
| 49 int Inode::file_type() const { |
| 50 return file_type_; |
| 51 } |
| 52 |
| 53 void Inode::set_file_access(const std::string& file_access) { |
| 54 if (file_access.size()== 0) |
| 55 file_access_=Inode::CurentTime(); |
| 56 else |
| 57 file_access_=file_access; |
| 58 } |
| 59 |
| 60 std::string Inode::file_access() const { |
| 61 return file_access_; |
| 62 } |
| 63 |
| 64 void Inode::set_file_modified(const std::string& file_modified) { |
| 65 if (file_modified.size()== 0) |
| 66 file_modified_=Inode::CurentTime(); |
| 67 else |
| 68 file_modified_=file_modified; |
| 69 } |
| 70 |
| 71 std::string Inode::file_modified() const { |
| 72 return file_modified_; |
| 73 } |
| 74 |
| 75 void Inode::set_inode_modified(const std::string& inode_modified) { |
| 76 if (inode_modified.size() == 0) |
| 77 inode_modified_=Inode::CurentTime(); |
| 78 else |
| 79 inode_modified_=inode_modified; |
| 80 } |
| 81 |
| 82 std::string Inode::inode_modified() const { |
| 83 return inode_modified_; |
| 84 } |
| 85 |
| 86 std::string Inode::CurentTime(){ |
| 87 time_t rawtime; |
| 88 struct tm * timeinfo; |
| 89 char buffer[80]; |
| 90 |
| 91 time(&rawtime); |
| 92 timeinfo = localtime(&rawtime); |
| 93 |
| 94 strftime(buffer, 80, "%d-%m-%Y %I:%M:%S", timeinfo); |
| 95 std::string str(buffer); |
| 96 return str; |
| 97 } |
| 98 |
| 99 InodeFile::InodeFile( |
| 100 unsigned long long appointed_inode, |
| 101 std::string inode_modified, std::string file_modified, |
| 102 std::string file_access) |
| 103 :Inode( |
| 104 appointed_inode,Inode::FILE_TYPE_FILE, |
| 105 inode_modified, |
| 106 file_modified, |
| 107 file_access){ |
| 108 } |
| 109 |
| 110 InodeFile::~InodeFile(){ |
| 111 } |
| 112 |
| 113 bool InodeFile::Truncate(const size_t length) { |
| 114 if( data_.size()<length) |
| 115 return false; |
| 116 data_.resize( length ); |
| 117 return true; |
| 118 } |
| 119 |
| 120 bool InodeFile::Write(const char *data, uint64_t offset, uint64_t dataSize) { |
| 121 if(data==nullptr&& |
| 122 dataSize>0) |
| 123 return false; |
| 124 if(offset>data_.size()) |
| 125 return false; |
| 126 data_.insert( offset, data, dataSize ); |
| 127 return true; |
| 128 } |
| 129 |
| 130 bool InodeFile::Read( |
| 131 size_t offset, |
| 132 size_t* inOutLen, |
| 133 const char** outData) const { |
| 134 if(inOutLen==nullptr || |
| 135 offset>data_.size()) |
| 136 return false; |
| 137 *inOutLen = offset + *inOutLen < data_.size() ? |
| 138 *inOutLen : data_.size() - offset; |
| 139 *outData = data_.c_str()+ offset; |
| 140 return true; |
| 141 } |
| 142 |
| 143 unsigned long long InodeDirectory::GetInodeFromName(const std::string& name) { |
| 144 ListOfEntriesIterator it = entries_.find(name); |
| 145 if (it != entries_.end()) |
| 146 return it->second; |
| 147 return Inode::NO_INODE; |
| 148 } |
| 149 |
| 150 void InodeDirectory::AddEntry( |
| 151 const std::string& name, unsigned long long inode) { |
| 152 entries_.insert(std::pair<std::string, unsigned long long>(name, inode)); |
| 153 // Tell the BookKeeper to add a reference |
| 154 if (book_keeper_) |
| 155 book_keeper_->AddLink(inode); |
| 156 } |
| 157 |
| 158 void InodeDirectory::DeleteEntry( |
| 159 const std::string& name, unsigned long long inode) { |
| 160 ListOfEntriesIterator it = entries_.find(name); |
| 161 if (it != entries_.end() && |
| 162 it->second == inode) { |
| 163 entries_.erase(it); |
| 164 // Ask the BookKeeper to delete |
| 165 if (book_keeper_) |
| 166 book_keeper_->RemoveLink(inode); |
| 167 } |
| 168 } |
| 169 |
| 170 void InodeDirectory::GetAllInodes(std::vector<EntryPair>& inodes) { |
| 171 for (ListOfEntriesIterator it(entries_.begin()); |
| 172 it != entries_.end(); ++it) { |
| 173 inodes.push_back(EntryPair(it->first, it->second)); |
| 174 } |
| 175 } |
| 176 |
| 177 InodeDirectory::InodeDirectory( |
| 178 BookKeeper* bookKeeper, |
| 179 unsigned long long appointed_inode, |
| 180 std::string inode_modified, |
| 181 std::string file_modified, |
| 182 std::string file_access) |
| 183 :Inode( |
| 184 appointed_inode, |
| 185 Inode::FILE_TYPE_DIR, |
| 186 inode_modified, |
| 187 file_modified, |
| 188 file_access), |
| 189 book_keeper_(bookKeeper) { |
| 190 } |
| 191 |
| 192 |
| 193 InodeDirectory::~InodeDirectory(){ |
| 194 } |
| 195 |
| 196 }//namespace filesystem |
OLD | NEW |