Skip to content

Commit

Permalink
Entry: restore weak_ptr on Storage to avoid circular reference.
Browse files Browse the repository at this point in the history
  • Loading branch information
srouquette committed Sep 5, 2015
1 parent d477b15 commit bc03f4d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 16 deletions.
3 changes: 2 additions & 1 deletion include/filesystem/entry.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class Entry {
const std::string& url() const noexcept;

private:
storage_ptr_t storage() const;
void throws_if_nonexistent() const;
void throws_if_storage_null() const;
void safe_update_status() const;
Expand All @@ -69,7 +70,7 @@ class Entry {
const fs::path path_;
mutable boost::shared_mutex shared_mutex_;
mutable fs::file_status status_;
storage_ptr_t storage_;
std::weak_ptr<Storage> storage_; // weak_ptr to prevent circular ref
const std::string url_;
};

Expand Down
26 changes: 12 additions & 14 deletions src/filesystem/entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,15 @@ Entry::content_t Entry::content() const {
bool Entry::exists() const {
std::lock_guard<std::mutex> lock{ mutex_ };
try {
throws_if_storage_null();
return storage_->exists(*this);
return storage()->exists(*this);
} catch (const filesystem_error&) {
return false;
}
}

bool Entry::is_dir() const {
std::lock_guard<std::mutex> lock{ mutex_ };
throws_if_storage_null();
return storage_->is_dir(*this);
return storage()->is_dir(*this);
}

void Entry::invalidate() noexcept {
Expand All @@ -58,7 +56,7 @@ void Entry::invalidate() noexcept {

void Entry::ls() {
safe_update_status();
auto content = storage_->ls(*this);
auto content = storage()->ls(*this);
std::lock_guard<std::mutex> lock{ mutex_ };
std::swap(content_, content);
on_update_(*this);
Expand All @@ -84,16 +82,17 @@ const std::string& Entry::url() const noexcept {
return url_;
}

void Entry::throws_if_nonexistent() const {
update_status();
if (!storage_->exists(*this)) {
throw EXCEPTION(__FUNCTION__, url_, no_such_file_or_directory);
storage_ptr_t Entry::storage() const {
if (storage_.expired()) {
throw EXCEPTION(__FUNCTION__, url_, no_such_device);
}
return storage_.lock();
}

void Entry::throws_if_storage_null() const {
if (!storage_) {
throw EXCEPTION(__FUNCTION__, url_, no_such_device);
void Entry::throws_if_nonexistent() const {
update_status();
if (!storage()->exists(*this)) {
throw EXCEPTION(__FUNCTION__, url_, no_such_file_or_directory);
}
}

Expand All @@ -103,8 +102,7 @@ void Entry::safe_update_status() const {
}

void Entry::update_status() const {
throws_if_storage_null();
status_ = storage_->status(*this);
status_ = storage()->status(*this);
}

} // namespace filesystem
Expand Down
2 changes: 1 addition & 1 deletion test/filesystem/pattern/storage_helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ void StorageHelper::remove(const fs::path& path) const {
entries_.erase(path);
try {
fs::remove_all(path);
} catch (const std::exception& e) {}
} catch (...) {}
}

} // namespace filesystem
Expand Down

0 comments on commit bc03f4d

Please sign in to comment.