Ruby 中删除的文件未从 HDD 中删除

发布于 2024-09-06 12:04:22 字数 706 浏览 3 评论 0原文

我在 Ruby 中删除文件时遇到了奇怪的情况,代码似乎报告正确,但物理文件并未从我的硬盘驱动器中删除。我可以在命令行上执行 rm path/to/file - 这有效。我什至可以打开 Rails 控制台和 File.safe_unlink 文件,这也有效,只是在我的 Rails 应用程序中,它无法删除实际文件:

def destroy
  Rails.logger.debug local_path #=> /Users/ryan/.../public/system/.../file.jpg
  Rails.logger.debug File.exist?(local_path) #=> true
  File.safe_unlink(local_path)
  Rails.logger.debug File.exist?(local_path) #=> false
  # yet the physical file actually STILL exists!
end

物理文件位于 Git 存储库中(存储库存储在 /public/ 中)系统/)有什么问题吗?我尝试使用 ruby​​-git gem 使用它提供的 rm 命令来删除文件,但这也不会删除文件。

我在测试期间打开了文件的所有权限,但仍然没有任何作用。我还用 File.writable?(local_path) 确认了这一点,它返回 true。

有什么想法可以阻止文件被删除吗?

I'm experiencing a weird situation with deleting files in Ruby, the code seems to report correctly, but the physical file isn't removed from my hard drive. I can do rm path/to/file on the command line - that works. I can even open up the Rails console and File.safe_unlink the file and that also works, it's just within my Rails app it fails to delete the actual file:

def destroy
  Rails.logger.debug local_path #=> /Users/ryan/.../public/system/.../file.jpg
  Rails.logger.debug File.exist?(local_path) #=> true
  File.safe_unlink(local_path)
  Rails.logger.debug File.exist?(local_path) #=> false
  # yet the physical file actually STILL exists!
end

The physical file is within a Git repo (the repo is stored within /public/system/) any gotchas with that? I've tried using the ruby-git gem to delete the file using the rm command it provides, but that doesn't delete the file either.

I've opened up all the permissions on the files during testing and still nothing works. I've also confirmed this with File.writable?(local_path) which returned true.

Any thoughts why it could be preventing the file from being removed?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

守护在此方 2024-09-13 12:04:22

您检查过目录的权限吗?删除是目录写操作,不是文件写操作。 (出于礼貌,rm 还会检查文件权限,并询问您是否真的想要这样做,如果该文件是写保护的;但如果该目录不可写,它会直接拒绝。 )

Have you checked the permissions on the directory? Deletion is a directory write operation, not file write operation. (rm will also check file perms and ask if you really want to do it, as a courtesy, if the file is write-protected; but if the directory isn't writable, it flat out refuses.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文