Skip to content

File fail on overwrite#2842

Merged
obiltschnig merged 13 commits intopocoproject:poco-1.10.0from
KevDi:feature/FileFailOnOverwrite
Dec 9, 2019
Merged

File fail on overwrite#2842
obiltschnig merged 13 commits intopocoproject:poco-1.10.0from
KevDi:feature/FileFailOnOverwrite

Conversation

@KevDi
Copy link
Copy Markdown
Contributor

@KevDi KevDi commented Nov 18, 2019

With the optional Parameter failOnOverwrite it's possible to get an exception if the destination File exists on copy/move/rename. In some cases it is usefull to get the Information if the Copy/Move/Rename fail because of an existing File.

@obiltschnig
Copy link
Copy Markdown
Member

How about, instead of using a bool parameter, using an options parameter with a FAIL_ON_OVERWRITE flag? Would make the code much more readable. I know we have used bool parameters in way too many places in Poco already, but better late then never to start making it right.

Something like:

enum Options
{
    OPT_FAIL_ON_OVERWRITE = 0x01
};

...

void copyTo(const std::string& path, int options = 0) const;

@obiltschnig obiltschnig added this to the Release 1.10.0 milestone Nov 25, 2019
@obiltschnig obiltschnig self-assigned this Nov 25, 2019
@KevDi
Copy link
Copy Markdown
Contributor Author

KevDi commented Nov 26, 2019

I could change it to an Option Parameter like you showed if you want.
But where should i place the enum? Inside the File.h? Or should it be placed in every File_* Header?

@obiltschnig
Copy link
Copy Markdown
Member

Just put the Options enum inside the File class definition in File.h:

class Foundation_API File: private FileImpl
	/// The File class provides methods for working with a file.
	/// ...
{
public:
	typedef FileSizeImpl FileSize;

	enum LinkType
		/// Type of link for linkTo().
	{
		LINK_HARD     = 0, /// hard link
		LINK_SYMBOLIC = 1  /// symbolic link
	};

	enum Options
	{
		OPT_FAIL_ON_OVERWRITE = 0x01
	};

...

@KevDi
Copy link
Copy Markdown
Contributor Author

KevDi commented Nov 26, 2019

So i have added the Options Advice. Pls take a look and say if it's okay so =)

Copy link
Copy Markdown
Member

@obiltschnig obiltschnig left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, but please fix the comments still mentioning the failOnOverwrite param.

Copy link
Copy Markdown
Member

@obiltschnig obiltschnig left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have some more comments/requests for change, unfortunately.

void setExecutableImpl(bool flag = true);
void copyToImpl(const std::string& path) const;
void renameToImpl(const std::string& path);
void copyToImpl(const std::string& path, bool failOnOverwrite = false) const;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs to be fixed.



void FileImpl::copyToImpl(const std::string& path) const
void FileImpl::copyToImpl(const std::string& path, bool failOnOverwrite) const
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs to be fixed.

void copyToImpl(const std::string& path) const;
void renameToImpl(const std::string& path);
void copyToImpl(const std::string& path, bool failOnOverwrite = false) const;
void renameToImpl(const std::string& path, bool failOnOverwrite = false);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also here


int dd = open(path.c_str(), O_CREAT | O_TRUNC | O_WRONLY, st.st_mode);
int dd;
if (options == 1) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

better check would be options & File::OPT_FAIL_ON_OVERWRITE (in case we add another option)

Copy link
Copy Markdown
Contributor Author

@KevDi KevDi Nov 26, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So i have to include the File.h Header inside the File_UNIX.cpp?
Which seems like a circle include because File_UNIX already included inside the File.h


int dd = open(path.c_str(), O_CREAT | O_TRUNC | O_WRONLY, st.st_mode & S_IRWXU);
int dd;
if (options == 1) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

options & File::OPT_FAIL_ON_OVERWRITE

poco_assert (!_path.empty());

if (CopyFileA(_path.c_str(), path.c_str(), FALSE) == 0)
if (CopyFileA(_path.c_str(), path.c_str(), options == 1) == 0)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(options & File::OPT_FAIL_ON_OVERWRITE) != 0


if (MoveFileExA(_path.c_str(), path.c_str(), MOVEFILE_REPLACE_EXISTING) == 0)
handleLastErrorImpl(_path);
if (options == 1) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

options & File::OPT_FAIL_ON_OVERWRITE

std::wstring upath;
convertPath(path, upath);
if (CopyFileW(_upath.c_str(), upath.c_str(), FALSE) == 0)
if (CopyFileW(_upath.c_str(), upath.c_str(), options == 1) == 0)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(options & File::OPT_FAIL_ON_OVERWRITE) != 0

convertPath(path, upath);
if (MoveFileExW(_upath.c_str(), upath.c_str(), MOVEFILE_REPLACE_EXISTING) == 0)
handleLastErrorImpl(_path);
if (options == 1) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

options & File::OPT_FAIL_ON_OVERWRITE

@KevDi
Copy link
Copy Markdown
Contributor Author

KevDi commented Nov 26, 2019

So i have to include the File.h Header inside the File_UNIX.cpp?
Which seems like a circle include because File_UNIX already included inside the File.h

@obiltschnig
Copy link
Copy Markdown
Member

Yeah, good point. In the other places we have actually defined the enum in the Impl classes (like FileSize). I see that for LinkType, this has also been implemented the "wrong" way, using a magic number in FileImpl, rather than the symbolic value. I'd say, leave it as is for now, I will eventually take care of both. And thank you for your contribution!

@obiltschnig
Copy link
Copy Markdown
Member

Btw, an example for how it's done properly is the Thread class.

@KevDi
Copy link
Copy Markdown
Contributor Author

KevDi commented Nov 27, 2019

I have added the remarks like it was implemented inside the Thread Class. Currently i done it for the Unix and Win32U File Implementation. Please take a look if it is ok so. If its ok i will made the changes for the missing ones.

@obiltschnig
Copy link
Copy Markdown
Member

Looks good, but please also fix the checks for options == 1.

@KevDi
Copy link
Copy Markdown
Contributor Author

KevDi commented Nov 27, 2019

Yes i will fix them i just want to ask if its okay the way i did it now

@KevDi
Copy link
Copy Markdown
Contributor Author

KevDi commented Nov 30, 2019

I have made the changes to the other File Impls

@obiltschnig obiltschnig merged commit 56fe4ea into pocoproject:poco-1.10.0 Dec 9, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants