Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use File.list and File.walk within a try with resource #5718

Merged
merged 5 commits into from Nov 24, 2020

Conversation

gregw
Copy link
Contributor

@gregw gregw commented Nov 24, 2020

The API contract of File.list and File.walk requires them to be closed after use.

The API contract of File.list and File.walk requires them to be closed after use.
@gregw gregw requested a review from janbartel November 24, 2020 12:31
Left out filter
Factored out deleteFile with better debug
try
{
sweepFile(now, p);
if (!Files.deleteIfExists(p))
Copy link
Contributor

Choose a reason for hiding this comment

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

Bzzzt. Do not pass go, do not collect $100. YOu can only delete it if it has expired!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oooops fixed

LOG.warn(e);
if (!Files.deleteIfExists(p))
LOG.warn("Could not delete {}", p.getFileName());
if (LOG.isDebugEnabled())
Copy link
Contributor

Choose a reason for hiding this comment

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

So the debug output looking like the delete happened will be done even if the file wasn't actually deleted.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

factored out to a better common deleteFile method

try
{
sweepFile(now, p);
if (!Files.deleteIfExists(p))
LOG.warn("Could not delete {}", p.getFileName());
Copy link
Contributor

Choose a reason for hiding this comment

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

In general, I'm not sure we need to do the delete outside of the stream: there are plenty of examples if you do a google search where the recommended way to do a recursive directory delete is to use Files.walk/list and delete each file. I can't find any counter examples that says that is a bad idea.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK I have switched back.

Can delete files whilst walking
Copy link
Contributor

@joakime joakime left a comment

Choose a reason for hiding this comment

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

I'm good with this.
The session specific behaviors that @janbartel pointed out seem important though.

}
catch (Exception e)
{
LOG.warn(e);
}
}

public boolean isExpired(long now, Path p)
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't see the benefit of splitting sweepFile() into isExpired() and then deleteFile() - it's more efficient just to use sweepFile().

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah now that we no longer collect before delete, there is none....

Restored sweepFile
fixed minor code suggestions
@gregw gregw requested a review from janbartel November 24, 2020 14:06
Copy link
Contributor

@janbartel janbartel left a comment

Choose a reason for hiding this comment

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

LGTM

@gregw gregw merged commit 9f82ca0 into jetty-9.4.x Nov 24, 2020
@gregw gregw deleted the jetty-9.4.x-auto-close-streams branch November 24, 2020 17:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants