Skip to content
New issue

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

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

Already on GitHub? # to your account

File.renameTo及File.setReadOnly的问题 #90

Open
JiaRG opened this issue Mar 29, 2023 · 6 comments · May be fixed by #91
Open

File.renameTo及File.setReadOnly的问题 #90

JiaRG opened this issue Mar 29, 2023 · 6 comments · May be fixed by #91
Labels
bug Something isn't working

Comments

@JiaRG
Copy link
Contributor

JiaRG commented Mar 29, 2023

Bug 描述

请清晰和简洁的描述你遇见的 bug,方便快速的定位和解决问题

运行环境

  • MyPerf4J 版本号,最新
  • JDK 版本号,JDK8
  • 运行容器和框架,Tomcat 8.5
  • 操作系统,Windows

final File destFile = new File(filePath);
final boolean rename = tempFile.renameTo(destFile) && destFile.setReadOnly();

renameTo 在 window 上一直都是返回 false ,所以一直只创建了 tmp 文件,没生成最终的文件

并且假如第一次生成了最终的文件的话,并且给设置成了 readonly , 那下一轮生成的时候会报错的吧

@JiaRG JiaRG added the bug Something isn't working label Mar 29, 2023
@LinShunKang
Copy link
Owner

感谢指出,MyPerf4J 的开发与使用基本在 MacOS 和 Linux 上,所以对 Windows 系统的测试不够充分;
欢迎提交 PR,我周末会抽时间统一处理:)

@LinShunKang
Copy link
Owner

不过,每一次更新文件都是生成临时文件然后覆盖旧文件,设置 readonly 不会有问题才对

@JiaRG
Copy link
Contributor Author

JiaRG commented Mar 29, 2023

定位到原因了,是 fileWriter 还没关闭呢,就要重命名,所以一直导致 renameTo 返回 false

但是重命名和设置只读属性的逻辑挪到 try-with-resource 外面来,只有第一次能 renameTo 成功,后续就一直失败了,看下这个方法的 doc

我换成了 Files.move(tempPath, destFile.toPath(), StandardCopyOption.REPLACE_EXISTING); 这个 api 底层我看了下有一步删除的操作 , 就会报错

@LinShunKang
Copy link
Owner

欢迎提交 PR : )

@JiaRG
Copy link
Contributor Author

JiaRG commented Apr 2, 2023

windows 下 Files.move 这个 api 在 juint 里能重命名只读文件,在 tomcat 就会报错。。。
image
image

我手动去掉只读,它就又好了😑

@JiaRG JiaRG linked a pull request Apr 2, 2023 that will close this issue
5 tasks
@JiaRG
Copy link
Contributor Author

JiaRG commented Apr 3, 2023

还有个小问题一起在这个 PR 里修了
image

这俩处打印了 ConfigKey 对象的的内存地址了,没显示对应的 key,重写下 toString 就好了

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants