Skip to content

Commit

Permalink
Add readonly checks to copy_overwrite_checks in on-disk CytoFrame cla…
Browse files Browse the repository at this point in the history
…sses
  • Loading branch information
jacobpwagner committed Sep 4, 2020
1 parent b3931f3 commit 5afbec8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 20 deletions.
23 changes: 13 additions & 10 deletions inst/include/cytolib/H5CytoFrame.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ class H5CytoFrame:public CytoFrame{
string get_uri() const{
return filename_;
}
void check_write_permission(){
void check_write_permission() const{
if(readonly_)
throw(domain_error("Can't write to the read-only H5CytoFrame object!"));

Expand Down Expand Up @@ -308,16 +308,21 @@ class H5CytoFrame:public CytoFrame{
* protect the h5 from being overwritten accidentally
* which will make the original cf object invalid
*/
void copy_overwrite_check(const string & dest) const
void copy_overwrite_check(const string & dest, bool overwrite) const
{
if(fs::equivalent(fs::path(filename_), fs::path(dest)))
throw(domain_error("Copying H5CytoFrame to itself is not supported! "+ dest));
// Check if trying to write to same file
if(fs::equivalent(fs::path(filename_), fs::path(dest))){
// First check if that is even allowed by this CytoFrame
check_write_permission();
// Then make sure it has been explicitly approved by the caller
if(!overwrite)
throw(domain_error("Copying H5CytoFrame to itself is not supported! "+ dest));
}
}

CytoFramePtr copy(const string & h5_filename = "", bool overwrite = false) const
{
if(!overwrite)
copy_overwrite_check(h5_filename);
copy_overwrite_check(h5_filename, overwrite);
string new_filename = h5_filename;
if(new_filename == "")
{
Expand All @@ -334,9 +339,8 @@ class H5CytoFrame:public CytoFrame{
}
CytoFramePtr copy(uvec row_idx, uvec col_idx, const string & h5_filename = "", bool overwrite = false) const
{
copy_overwrite_check(h5_filename, overwrite);

if(!overwrite)
copy_overwrite_check(h5_filename);
string new_filename = h5_filename;
if(new_filename == "")
{
Expand All @@ -350,9 +354,8 @@ class H5CytoFrame:public CytoFrame{

CytoFramePtr copy(uvec idx, bool is_row_indexed, const string & h5_filename = "", bool overwrite = false) const
{
copy_overwrite_check(h5_filename, overwrite);

if(!overwrite)
copy_overwrite_check(h5_filename);
string new_filename = h5_filename;
if(new_filename == "")
{
Expand Down
24 changes: 14 additions & 10 deletions inst/include/cytolib/TileCytoFrame.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ class TileCytoFrame:public CytoFrame{
string get_uri() const{
return uri_;
}
void check_write_permission(){
void check_write_permission() const{
if(readonly_)
throw(domain_error("Can't write to the read-only TileCytoFrame object!"));

Expand Down Expand Up @@ -511,16 +511,22 @@ class TileCytoFrame:public CytoFrame{
* protect the h5 from being overwritten accidentally
* which will make the original cf object invalid
*/
void copy_overwrite_check(const string & dest) const
void copy_overwrite_check(const string & dest, bool overwrite) const
{
if(fs::equivalent(fs::path(uri_), fs::path(dest)))
throw(domain_error("Copying TileCytoFrame to itself is not supported! "+ dest));
// Check if trying to write to same file
if(fs::equivalent(fs::path(uri_), fs::path(dest))){
// First check if that is even allowed by this CytoFrame
check_write_permission();
// Then make sure it has been explicitly approved by the caller
if(!overwrite)
throw(domain_error("Copying TileCytoFrame to itself is not supported! "+ dest));
}
}

CytoFramePtr copy(const string & uri = "", bool overwrite = false) const
{
if(!overwrite)
copy_overwrite_check(uri);
copy_overwrite_check(uri, overwrite);

string new_uri = uri;
if(new_uri == "")
{
Expand All @@ -537,9 +543,8 @@ class TileCytoFrame:public CytoFrame{
}
CytoFramePtr copy(uvec row_idx, uvec col_idx, const string & uri = "", bool overwrite = false) const
{
copy_overwrite_check(uri, overwrite);

if(!overwrite)
copy_overwrite_check(uri);
string new_uri = uri;
if(new_uri == "")
{
Expand All @@ -553,9 +558,8 @@ class TileCytoFrame:public CytoFrame{

CytoFramePtr copy(uvec idx, bool is_row_indexed, const string & uri = "", bool overwrite = false) const
{
copy_overwrite_check(uri, overwrite);

if(!overwrite)
copy_overwrite_check(uri);
string new_uri = uri;
if(new_uri == "")
{
Expand Down

0 comments on commit 5afbec8

Please sign in to comment.