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

(fix) Impl moving file atomically on windows, #54 #104

Merged
merged 3 commits into from
Apr 16, 2019

Conversation

killme2008
Copy link
Contributor

Motivation:

Use Files.move(source,path,opts) to replace File#renameTo(file) for moving files atomically.

The code is from

https://github.com/jenkinsci/jenkins/blob/master/core/src/main/java/hudson/util/AtomicFileWriter.java#L187

Modification:

Modify the ProtoBufFile#save() implementation.

Result:

Fixes #54

Files.deleteIfExists(tmpPath);
} catch (IOException e2) {
e2.addSuppressed(e1);
LOG.warn("Unable to delete {}, good bye then!", tmpPath);
Copy link
Contributor

Choose a reason for hiding this comment

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

这行日志不错: good bye then!

Copy link
Contributor

Choose a reason for hiding this comment

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

已经到 good bye 地步了的话,日志 level 是不是应该为 error?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

没有必要,上层会 catch 打印

}

if (destFile.exists()) {
LOG.info("The target file { } was already existing", destPath);
Copy link
Contributor

@fengjiachun fengjiachun Apr 16, 2019

Choose a reason for hiding this comment

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

大括号多了一个空格,另外日志是一句话,应该有一个句号

Path destPath = destFile.toPath();
try {
return Files.move(tmpPath, destPath, StandardCopyOption.ATOMIC_MOVE) == destPath;
} catch (IOException e) {
Copy link
Contributor

Choose a reason for hiding this comment

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

final IOException e
按照 jraft 的统一代码风格,应该有个 final 修饰

@@ -96,6 +105,7 @@ private void readBytes(byte[] bs, InputStream input) throws IOException {
* @return true if save success
Copy link
Contributor

Choose a reason for hiding this comment

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

@param msg protobuf message
@param sync if sync flush data to disk
@return true if save success

这三行注释原来没有对整齐,要么顺便也改下?应该是

msg proto 之间两个空格
sync ifxxx 之间一个空格
return true 之间一个空格

Copy link
Contributor Author

Choose a reason for hiding this comment

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

这些东西应该工具来保证,如果工具无法做到,我觉的这个规范可以不要了。 javadoc 会处理。

Copy link
Contributor

Choose a reason for hiding this comment

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

格式化插件无法做到

Copy link
Contributor Author

Choose a reason for hiding this comment

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

恩,那这个规范就不要了,没有必要去手工调整。参数合理说明即可。生成的 javadoc 会格式化的。

File destFile = new File(this.path);
Path destPath = destFile.toPath();
try {
return Files.move(tmpPath, destPath, StandardCopyOption.ATOMIC_MOVE) == destPath;
Copy link
Contributor

Choose a reason for hiding this comment

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

Files.move() == destPath 是个恒等式,还不如新起一行 return true 清晰

@fengjiachun fengjiachun merged commit a4f45f4 into master Apr 16, 2019
@fengjiachun fengjiachun deleted the feature/atomic-file branch April 16, 2019 08:40
@fengjiachun fengjiachun mentioned this pull request Aug 15, 2019
4 tasks
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.

ProtoBufFile#save(Message msg, boolean sync) 返回false问题
2 participants