Skip to content

Commit 654fddc

Browse files
committed
code cleanup
1 parent 824481e commit 654fddc

File tree

8 files changed

+64
-30
lines changed

8 files changed

+64
-30
lines changed

include/fsmod/FileSystem.hpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020

2121
namespace simgrid::fsmod {
2222

23-
/**
24-
* @brief A class that implements a file system abstraction
25-
*/
23+
/**
24+
* @brief A class that implements a file system abstraction
25+
*/
2626
class XBT_PUBLIC FileSystem {
2727
const std::string name_;
2828
int max_num_open_files_;
@@ -40,12 +40,15 @@ namespace simgrid::fsmod {
4040
void create_file(const std::string& full_path, const std::string& size) const;
4141

4242
[[nodiscard]] bool file_exists(const std::string& full_path) const;
43-
[[nodiscard]] bool directory_exists(const std::string& full_dir_path) const;
44-
[[nodiscard]] std::set<std::string, std::less<>> list_files_in_directory(const std::string& full_dir_path) const;
4543

4644
void move_file(const std::string& src_full_path, const std::string& dst_full_path) const;
4745
void unlink_file(const std::string& full_path) const;
46+
47+
48+
void create_directory(const std::string& full_dir_path) const;
49+
[[nodiscard]] bool directory_exists(const std::string& full_dir_path) const;
4850
void unlink_directory(const std::string& full_dir_path) const;
51+
[[nodiscard]] std::set<std::string, std::less<>> list_files_in_directory(const std::string& full_dir_path) const;
4952

5053
[[nodiscard]] sg_size_t file_size(const std::string& full_path) const;
5154

@@ -59,7 +62,7 @@ namespace simgrid::fsmod {
5962
[[nodiscard]] std::shared_ptr<Partition> get_partition_for_path_or_null(const std::string& full_path) const;
6063

6164

62-
private:
65+
private:
6366
[[nodiscard]] std::pair<std::shared_ptr<Partition>, std::string> find_path_at_mount_point(const std::string &full_path) const;
6467

6568
std::map<std::string, std::shared_ptr<Partition>, std::less<>> partitions_;

include/fsmod/FileSystemException.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
XBT_ATTRIB_NORETURN void rethrow_nested(simgrid::xbt::ThrowPoint&& throwpoint, \
2222
const std::string& message) const override \
2323
{ \
24-
std::string augmented_message = std::string("CRAP: ") + message; \
24+
std::string augmented_message = std::string(msg_prefix) + message; \
2525
std::throw_with_nested(AnyException(std::move(throwpoint), augmented_message)); \
2626
} \
2727
}
@@ -33,6 +33,7 @@ namespace simgrid::fsmod {
3333
DECLARE_FSMOD_EXCEPTION(FileNotFoundException, "File not found");
3434
DECLARE_FSMOD_EXCEPTION(NotEnoughSpaceException, "Not enough space");
3535
DECLARE_FSMOD_EXCEPTION(FileIsOpenException, "Operation not permitted on an opened file");
36+
DECLARE_FSMOD_EXCEPTION(DirectoryAlreadyExistsException, "Directory already exists");
3637
DECLARE_FSMOD_EXCEPTION(DirectoryDoesNotExistException, "Directory does not exist");
3738
DECLARE_FSMOD_EXCEPTION(TooManyOpenFilesException, "Too many open files");
3839
DECLARE_FSMOD_EXCEPTION(FileAlreadyExistsException, "File already exists");

include/fsmod/Partition.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ namespace simgrid::fsmod {
100100

101101
[[nodiscard]] std::shared_ptr<Storage> get_storage() const { return storage_; }
102102

103+
void create_new_directory(const std::string& dir_path);
103104
[[nodiscard]] bool directory_exists(const std::string& dir_path) const { return content_.find(dir_path) != content_.end(); }
104105
std::set<std::string, std::less<>> list_files_in_directory(const std::string &dir_path) const;
105106
void delete_directory(const std::string &dir_path);

src/File.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ namespace simgrid::fsmod {
178178
*/
179179
void File::update_current_position(sg_offset_t pos) {
180180
if (pos < 0) {
181-
throw simgrid::fsmod::InvalidSeekException(XBT_THROW_POINT, "Cannot seek before the first byte");
181+
throw InvalidSeekException(XBT_THROW_POINT, "Cannot seek before the first byte");
182182
}
183183
current_position_ = pos;
184184
}

src/FileSystem.cpp

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ namespace simgrid::fsmod {
3434
return PathUtil::is_at_mount_point(simplified_path, element.first);
3535
});
3636
if (it == this->partitions_.end()) {
37-
throw simgrid::fsmod::InvalidPathException(XBT_THROW_POINT, "No path prefix matches a partition's mount point (" + simplified_path + ")");
37+
throw InvalidPathException(XBT_THROW_POINT, "No path prefix matches a partition's mount point (" + simplified_path + ")");
3838
}
3939
auto path_at_mount_point = PathUtil::path_at_mount_point(simplified_path, it->first);
4040
auto partition = it->second;
@@ -173,7 +173,7 @@ namespace simgrid::fsmod {
173173
// TODO: This is weak, since if directory "a/b/c/d" exists, director "a/b" does not!
174174
// TODO: (we don't _really_ have directories, just prefixes before the file name)
175175
if (partition->directory_exists(path_at_mount_point)) {
176-
throw simgrid::fsmod::InvalidPathException(XBT_THROW_POINT, "Provided file path is that of an existing directory (" + path_at_mount_point + ")");
176+
throw InvalidPathException(XBT_THROW_POINT, "Provided file path is that of an existing directory (" + path_at_mount_point + ")");
177177
}
178178

179179
// Split the path
@@ -192,7 +192,7 @@ namespace simgrid::fsmod {
192192
std::shared_ptr<File> FileSystem::open(const std::string &full_path, const std::string& access_mode) {
193193
// "Get a file descriptor"
194194
if (this->num_open_files_ >= this->max_num_open_files_) {
195-
throw simgrid::fsmod::TooManyOpenFilesException(XBT_THROW_POINT);
195+
throw TooManyOpenFilesException(XBT_THROW_POINT);
196196
}
197197
if (access_mode != "r" && access_mode != "w" && access_mode != "a") {
198198
throw std::invalid_argument("Invalid access mode. Authorized values are: 'r', 'w', or 'a'");
@@ -209,7 +209,7 @@ namespace simgrid::fsmod {
209209
auto metadata = partition->get_file_metadata(dir, file_name);
210210
if (not metadata) {
211211
if (access_mode == "r")
212-
throw simgrid::fsmod::FileNotFoundException(XBT_THROW_POINT, full_path);
212+
throw FileNotFoundException(XBT_THROW_POINT, full_path);
213213
create_file(full_path, "0B");
214214
metadata = partition->get_file_metadata(dir, file_name);
215215
} else {
@@ -259,14 +259,14 @@ namespace simgrid::fsmod {
259259
// Check that the file exist
260260
auto file_metadata = partition->get_file_metadata(dir, file_name);
261261
if (not file_metadata) {
262-
throw simgrid::fsmod::FileNotFoundException(XBT_THROW_POINT, full_path);
262+
throw FileNotFoundException(XBT_THROW_POINT, full_path);
263263
}
264264

265265
return file_metadata->get_current_size();
266266
}
267267

268268
/**
269-
* @brief Unlike a file
269+
* @brief Unlink a file
270270
* @param full_path: the file path
271271
*/
272272
void FileSystem::unlink_file(const std::string &full_path) const {
@@ -292,7 +292,7 @@ namespace simgrid::fsmod {
292292

293293
// No mv across partitions (just like in the real world)
294294
if (src_partition != dst_partition) {
295-
throw simgrid::fsmod::InvalidMoveException(XBT_THROW_POINT, "Cannot move file across partitions");
295+
throw InvalidMoveException(XBT_THROW_POINT, "Cannot move file across partitions");
296296
}
297297

298298
auto [src_dir, src_file_name] = PathUtil::split_path(src_path_at_mount_point);
@@ -303,7 +303,7 @@ namespace simgrid::fsmod {
303303
}
304304

305305
/**
306-
* @brief Method to check that a file exists at a given path
306+
* @brief Check that a file exists at a given path
307307
* @param full_path: the file path
308308
* @return true if the file exists, false otherwise
309309
*/
@@ -319,7 +319,22 @@ namespace simgrid::fsmod {
319319
}
320320

321321
/**
322-
* @brief Method to check that a directory exists at a given path
322+
* @brief Create a directory
323+
* @param full_dir_path: the directory path
324+
*/
325+
void FileSystem::create_directory(const std::string& full_dir_path) const {
326+
std::string simplified_path = PathUtil::simplify_path_string(full_dir_path);
327+
// This check cannot be performed at the partition-level
328+
if (this->file_exists(simplified_path)) {
329+
throw InvalidPathException(XBT_THROW_POINT, "Path is that of an existing file: " + simplified_path);
330+
}
331+
auto [partition, path_at_mount_point] = this->find_path_at_mount_point(simplified_path);
332+
partition->create_new_directory(path_at_mount_point);
333+
}
334+
335+
336+
/**
337+
* @brief Check that a directory exists at a given path
323338
* @param full_path: the directory path
324339
* @return true if the directory exists, false otherwise
325340
*/
@@ -330,7 +345,7 @@ namespace simgrid::fsmod {
330345
}
331346

332347
/**
333-
* @brief Method to get the list of names of files in a directory
348+
* @brief Retrieve the list of names of files in a directory
334349
* @param full_dir_path: the path to the directory
335350
* @return
336351
*/
@@ -341,7 +356,7 @@ namespace simgrid::fsmod {
341356
}
342357

343358
/**
344-
* @brief Method to remove a directory and the files it contains
359+
* @brief Remove a directory and the files it contains
345360
* @param full_dir_path: the path to the directory
346361
* @return
347362
*/

src/Partition.cpp

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ namespace simgrid::fsmod {
4444

4545
// Check that the file doesn't already exit
4646
if (this->get_file_metadata(dir_path, file_name)) {
47-
throw simgrid::fsmod::FileAlreadyExistsException(XBT_THROW_POINT, dir_path + "/" + file_name);
47+
throw FileAlreadyExistsException(XBT_THROW_POINT, dir_path + "/" + file_name);
4848
}
4949

5050
content_[dir_path][file_name] = std::make_unique<FileMetadata>(size, this, dir_path, file_name);
@@ -60,11 +60,11 @@ namespace simgrid::fsmod {
6060
void Partition::delete_file(const std::string &dir_path, const std::string &file_name) {
6161
auto* metadata_ptr = this->get_file_metadata(dir_path, file_name);
6262
if (not metadata_ptr) {
63-
throw simgrid::fsmod::FileNotFoundException(XBT_THROW_POINT, dir_path + "/" + file_name);
63+
throw FileNotFoundException(XBT_THROW_POINT, dir_path + "/" + file_name);
6464
}
6565

6666
if (metadata_ptr->get_file_refcount() > 0) {
67-
throw simgrid::fsmod::FileIsOpenException(XBT_THROW_POINT, dir_path + "/" + file_name);
67+
throw FileIsOpenException(XBT_THROW_POINT, dir_path + "/" + file_name);
6868
}
6969

7070
this->new_file_deletion_event(metadata_ptr);
@@ -84,7 +84,7 @@ namespace simgrid::fsmod {
8484
// Get the src metadata, which must exist
8585
const FileMetadata* src_metadata = this->get_file_metadata(src_dir_path, src_file_name);
8686
if (not src_metadata) {
87-
throw simgrid::fsmod::FileNotFoundException(XBT_THROW_POINT, src_dir_path + "/" + src_file_name);
87+
throw FileNotFoundException(XBT_THROW_POINT, src_dir_path + "/" + src_file_name);
8888
}
8989

9090
// Get the dst metadata, if any
@@ -97,10 +97,10 @@ namespace simgrid::fsmod {
9797

9898
// Sanity checks
9999
if (src_metadata->get_file_refcount() > 0) {
100-
throw simgrid::fsmod::FileIsOpenException(XBT_THROW_POINT, src_dir_path + "/" + src_file_name);
100+
throw FileIsOpenException(XBT_THROW_POINT, src_dir_path + "/" + src_file_name);
101101
}
102102
if (dst_metadata && dst_metadata->get_file_refcount()) {
103-
throw simgrid::fsmod::FileIsOpenException(XBT_THROW_POINT, dst_dir_path + "/" + dst_file_name);
103+
throw FileIsOpenException(XBT_THROW_POINT, dst_dir_path + "/" + dst_file_name);
104104
}
105105

106106
// Update free space if needed
@@ -124,7 +124,7 @@ namespace simgrid::fsmod {
124124

125125
std::set<std::string, std::less<>> Partition::list_files_in_directory(const std::string &dir_path) const {
126126
if (content_.find(dir_path) == content_.end()) {
127-
throw simgrid::fsmod::DirectoryDoesNotExistException(XBT_THROW_POINT, dir_path);
127+
throw DirectoryDoesNotExistException(XBT_THROW_POINT, dir_path);
128128
}
129129
std::set<std::string, std::less<>> keys;
130130
for (auto const &[filename, metadata]: content_.at(dir_path)) {
@@ -133,14 +133,22 @@ namespace simgrid::fsmod {
133133
return keys;
134134
}
135135

136+
137+
void Partition::create_new_directory(const std::string &dir_path) {
138+
if (content_.find(dir_path) != content_.end()) {
139+
throw DirectoryAlreadyExistsException(XBT_THROW_POINT, dir_path);
140+
}
141+
this->content_[dir_path] = (std::unordered_map<std::string, std::unique_ptr<FileMetadata>>){};
142+
}
143+
136144
void Partition::delete_directory(const std::string &dir_path) {
137145
if (content_.find(dir_path) == content_.end()) {
138-
throw simgrid::fsmod::DirectoryDoesNotExistException(XBT_THROW_POINT, dir_path);
146+
throw DirectoryDoesNotExistException(XBT_THROW_POINT, dir_path);
139147
}
140148
// Check that no file is open
141149
for (const auto &[filename, metadata]: content_.at(dir_path)) {
142150
if (metadata->get_file_refcount() != 0) {
143-
throw simgrid::fsmod::FileIsOpenException(XBT_THROW_POINT, "No content deleted in directory before file " + filename + " is open");
151+
throw FileIsOpenException(XBT_THROW_POINT, "No content deleted in directory before file " + filename + " is open");
144152
}
145153
}
146154
// Wipe everything out
@@ -149,7 +157,7 @@ namespace simgrid::fsmod {
149157

150158

151159
void Partition::create_space(sg_size_t num_bytes) {
152-
throw simgrid::fsmod::NotEnoughSpaceException(XBT_THROW_POINT);
160+
throw NotEnoughSpaceException(XBT_THROW_POINT);
153161
}
154162

155163
void Partition::new_file_creation_event(FileMetadata *file_metadata) {

src/PartitionFIFOCaching.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ namespace simgrid::fsmod {
2727
}
2828
}
2929
if (space_that_can_be_created < num_bytes) {
30-
throw simgrid::fsmod::NotEnoughSpaceException(XBT_THROW_POINT, "Unable to evict files to create enough space");
30+
throw NotEnoughSpaceException(XBT_THROW_POINT, "Unable to evict files to create enough space");
3131
}
3232
for (auto const &victim: files_to_remove_to_create_space) {
3333
this->delete_file(this->priority_list_[victim]->dir_path_, this->priority_list_[victim]->file_name_);

test/file_system_test.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,12 @@ TEST_F(FileSystemTest, Directories) {
142142
ASSERT_THROW(fs_->unlink_directory("/dev/a/b/d"), sgfs::DirectoryDoesNotExistException);
143143
ASSERT_FALSE(fs_->directory_exists("/dev/a/b/d"));
144144

145+
ASSERT_THROW(fs_->create_directory("/whatever/bogus"), sgfs::InvalidPathException);
146+
ASSERT_THROW(fs_->create_directory("/dev/a/foo.txt"), sgfs::InvalidPathException);
147+
ASSERT_NO_THROW(fs_->create_directory("/dev/a/new_dir"));
148+
ASSERT_THROW(fs_->create_directory("/dev/a/new_dir"), sgfs::DirectoryAlreadyExistsException);
149+
ASSERT_NO_THROW(fs_->unlink_directory("/dev/a/new_dir"));
150+
145151

146152
XBT_INFO("Try to unlink a directory in which one file is opened. This shouldn't work");
147153
std::shared_ptr<sgfs::File> file;

0 commit comments

Comments
 (0)