libfoedus-core
FOEDUS Core Library
filesystem.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014-2015, Hewlett-Packard Development Company, LP.
3  * This program is free software; you can redistribute it and/or modify it
4  * under the terms of the GNU General Public License as published by the Free
5  * Software Foundation; either version 2 of the License, or (at your option)
6  * any later version.
7  *
8  * This program is distributed in the hope that it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11  * more details. You should have received a copy of the GNU General Public
12  * License along with this program; if not, write to the Free Software
13  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14  *
15  * HP designates this particular file as subject to the "Classpath" exception
16  * as provided by HP in the LICENSE.txt file that accompanied this code.
17  */
18 #ifndef FOEDUS_FS_FILESYSTEM_HPP_
19 #define FOEDUS_FS_FILESYSTEM_HPP_
20 #include <iosfwd>
21 #include <string>
22 
23 #include "foedus/cxx11.hpp"
24 #include "foedus/error_code.hpp"
25 #include "foedus/fwd.hpp"
26 #include "foedus/initializable.hpp"
28 #include "foedus/fs/path.hpp"
29 
30 namespace foedus {
31 namespace fs {
32 
38 enum FileType {
44 };
45 
52  kNoPerms = 0, // kFileNotFound is kNoPerms rather than kPermsNotKnown
53 
54  // POSIX equivalent macros given in comments.
55  // Values are from POSIX and are given in octal per the POSIX standard.
56 
57  // permission bits
58 
59  kOwnerRead = 0400, // S_IRUSR, Read permission, owner
60  kOwnerWrite = 0200, // S_IWUSR, Write permission, owner
61  kOwnerExe = 0100, // S_IXUSR, Execute/search permission, owner
62  kOwnerAll = 0700, // S_IRWXU, Read, write, execute/search by owner
63 
64  kGroupRead = 040, // S_IRGRP, Read permission, group
65  kGroupWrite = 020, // S_IWGRP, Write permission, group
66  kGroupExe = 010, // S_IXGRP, Execute/search permission, group
67  kGroupAll = 070, // S_IRWXG, Read, write, execute/search by group
68 
69  kOthersRead = 04, // S_IROTH, Read permission, others
70  kOthersWrite = 02, // S_IWOTH, Write permission, others
71  kOthersExe = 01, // S_IXOTH, Execute/search permission, others
72  kOthersAll = 07, // S_IRWXO, Read, write, execute/search by others
73 
75 
76  kPermsNotKnown = 0xFFFF, // present when directory_entry cache not loaded
77 };
78 
84 
89 struct FileStatus {
91  explicit FileStatus(FileType type, FilePermission permissions = kPermsNotKnown)
92  : type_(type), permissions_(permissions) {}
93 
94  bool type_present() const { return type_ != kStatusError; }
95  bool permissions_present() const { return permissions_ != kPermsNotKnown;}
96  bool status_known() const { return type_present() && permissions_present(); }
97  bool exists() const { return type_ != kStatusError && type_ != kFileNotFound; }
98  bool is_regular_file() const { return type_ == kRegularFile; }
99  bool is_directory() const { return type_ == kDirectoryFile; }
100 
103 };
104 
110 struct SpaceInfo {
111  uint64_t capacity_;
113  uint64_t free_;
115  uint64_t available_;
116  friend std::ostream& operator<<(std::ostream& o, const SpaceInfo& v);
117 };
118 
123 FileStatus status(const Path& p);
128 inline bool exists(const Path& p) {return status(p).exists(); }
133 inline bool is_directory(const Path& p) {return status(p).is_directory(); }
138 inline bool is_regular_file(const Path& p) {return status(p).is_regular_file(); }
139 
144 Path current_path();
154 Path home_path();
159 Path absolute(const std::string& p);
160 
161 // so far not needed
162 // ErrorCode copy(const Path& from, const Path& to);
163 // ErrorCode copy_directory(const Path& from, const Path& to);
164 // ErrorCode copy_file(const Path& from, const Path& to);
165 
174 bool create_directories(const Path& p, bool sync = false);
183 bool create_directory(const Path& p, bool sync = false);
188 uint64_t file_size(const Path& p);
194 bool remove(const Path& p);
200 uint64_t remove_all(const Path& p);
205 SpaceInfo space(const Path& p);
210 std::string unique_name(uint64_t differentiator = 0);
222 std::string unique_name(const std::string& model, uint64_t differentiator = 0);
223 
238 bool fsync(const Path& path, bool sync_parent_directory = false);
239 
261 bool atomic_rename(const Path& old_path, const Path& new_path);
262 
280 bool durable_atomic_rename(const Path& old_path, const Path& new_path);
281 
286 inline bool rename(const Path& old_path, const Path& new_path) {
287  return atomic_rename(old_path, new_path);
288 }
289 
290 } // namespace fs
291 } // namespace foedus
292 #endif // FOEDUS_FS_FILESYSTEM_HPP_
bool is_directory() const
Definition: filesystem.hpp:99
FilePermission permissions_
Definition: filesystem.hpp:102
uint64_t available_
Less than free_.
Definition: filesystem.hpp:115
bool is_directory(const Path &p)
Returns if the file is a directory.
Definition: filesystem.hpp:133
Analogue of boost::filesystem::space_info.
Definition: filesystem.hpp:110
uint64_t remove_all(const Path &p)
Recursively deletes a directory.
Definition: filesystem.cpp:148
Root package of FOEDUS (Fast Optimistic Engine for Data Unification Services).
Definition: assert_nd.hpp:44
Path home_path()
Returns the absolute path of the home directory of the user running this process. ...
Definition: filesystem.cpp:80
Forward declarations of classes in root package.
FileStatus status(const Path &p)
Returns the status of the file.
Definition: filesystem.cpp:45
bool permissions_present() const
Definition: filesystem.hpp:95
Path current_path()
Returns the current working directory.
Definition: filesystem.cpp:66
bool create_directory(const Path &p, bool sync=false)
mkdir.
Definition: filesystem.cpp:108
FileStatus(FileType type, FilePermission permissions=kPermsNotKnown)
Definition: filesystem.hpp:91
Analogue of boost::filesystem::path.
Definition: path.hpp:37
bool durable_atomic_rename(const Path &old_path, const Path &new_path)
fsync() on source file before rename, then fsync() on the parent folder after rename.
Definition: filesystem.cpp:236
Path absolute(const std::string &p)
Returns the absolue path of the specified path.
Definition: filesystem.cpp:62
bool is_regular_file(const Path &p)
Returns if the file is a regular file.
Definition: filesystem.hpp:138
bool create_directories(const Path &p, bool sync=false)
Recursive mkdir (mkdirs).
Definition: filesystem.cpp:89
bool exists(const Path &p)
Returns if the file exists.
Definition: filesystem.hpp:128
FilePermission
Analogue of boost::filesystem::perm.
Definition: filesystem.hpp:51
SpaceInfo space(const Path &p)
Returns free space information for the device the file is on.
Definition: filesystem.cpp:158
bool is_regular_file() const
Definition: filesystem.hpp:98
bool rename(const Path &old_path, const Path &new_path)
Just a synonym of atomic_rename() to avoid confusion.
Definition: filesystem.hpp:286
uint64_t file_size(const Path &p)
Returns size of the file.
Definition: filesystem.cpp:120
bool exists() const
Definition: filesystem.hpp:97
friend std::ostream & operator<<(std::ostream &o, const SpaceInfo &v)
Definition: filesystem.cpp:193
FileType
Analogue of boost::filesystem::file_type.
Definition: filesystem.hpp:38
std::string unique_name(uint64_t differentiator=0)
Equivalent to unique_path("%%%%-%%%%-%%%%-%%%%").
Definition: filesystem.cpp:174
Analogue of boost::filesystem::file_status.
Definition: filesystem.hpp:89
bool status_known() const
Definition: filesystem.hpp:96
bool atomic_rename(const Path &old_path, const Path &new_path)
Renames the old file to the new file with the POSIX atomic-rename semantics.
Definition: filesystem.cpp:199
bool type_present() const
Definition: filesystem.hpp:94
assorted::FixedString< 508 > FixedPath
Represents a fixed (thus can be placed in shared memory) path string.
Definition: filesystem.hpp:83
bool fsync(const Path &path, bool sync_parent_directory=false)
Makes the content and metadata of the file durable all the way up to devices.
Definition: filesystem.cpp:203
uint64_t free_
Less than capacity_.
Definition: filesystem.hpp:113