简体   繁体   English

Ruby-在代码中使用删除文件和目录命令时如何防止擦拭硬盘

[英]Ruby - How to prevent wiping your hard drive when using delete file and directory commands in your code

I'm writing some code that at run time may create or delete directories within the project path. 我正在编写一些代码,这些代码在运行时可能会在项目路径中创建或删除目录。 I haven't really used ruby for file processing so i'm really uneasy about having code that, with a few mistypes weeks down the line, could result in wiping other directories outside of my project path. 我并没有真正使用ruby进行文件处理,因此我对代码的使用感到非常不安,因为可能在几周后输入错误,可能会导致项目目录之外的其他目录被清除。

Is there anyway to make it impossible for the program to delete files outside of its own path regardless of whats typed in destructive calls? 无论如何,无论破坏性调用中键入了什么内容,程序都无法删除其自身路径之外的文件?

You might want to create a wrapper method around your favourite delete method (or, perhaps, around whole class, because not only deleting files is potentially destructive file operation), which would expand all the submitted paths and check whether they begin with your "sandbox" path). 您可能想围绕自己喜欢的delete方法创建一个包装方法(或者也许围绕整个类,因为不仅删除文件可能会破坏文件),这将扩展所有提交的路径并检查它们是否以“ sandbox”开头”)。 You can also try to redefine delete method, if you are willing to cripple it through whole application. 如果您愿意在整个应用程序中减少删除方法,也可以尝试重新定义删除方法。

And maybe the cleanest solution of them all would be to create a new user on your system and run your program as him. 也许所有这些方法中最干净的解决方案是在系统上创建一个新用户并以他的身份运行程序。

On a POSIX system, you can use Dir.chroot to change the root that your application sees. 在POSIX系统上,可以使用Dir.chroot更改应用程序可以看到的根目录。 Then ALL actions, not just delete ones, will be limited to the project directory. 然后,所有操作(不仅是删除操作)都将限制在项目目录中。 This does mean that external commands will be unavailable unless you make them part of your project directory as well. 这确实意味着外部命令将不可用,除非您也将它们也设置为项目目录的一部分。

This is the standard 'sandboxing' method used in Unix based systems. 这是在基于Unix的系统中使用的标准“沙盒”方法。 It can be difficult to setup (eliminating all external dependancies is sometimes hard), but affords significant protection when configured properly. 设置可能很困难(有时很难消除所有外部依赖关系),但如果配置正确,则可以提供重要的保护。

Pathname is a wrapper class for almost any file operations. 路径名是几乎所有文件操作的包装器类。

require "pathname"
path= Pathname.new("/home/johannes")
path.directory? # => true
path.children # => [#<Pathname:.bash_history>, #<Pathname:Documents>, #<Pathname:Desktop>]
path.children.each do |p|
  p.delete if p.file?
end

Pathname#children does not contain . Pathname#children不包含. or .. so you don't accidently walk up the tree instead of down. ..这样您就不会意外地走上树而不是走下树。 If you still don't trust in the code, you can even check if on path is contained in another 如果您仍然不信任该代码,甚至可以检查on路径是否包含在另一个路径中

Pathname.new("test") <=> Pathname.new("test/123") # => -1

You could generate an Array of filenames in your project directory using 您可以使用以下命令在项目目录中生成文件名数组

my_files = Dir["/bla/bla/your/directory/**/*"]

and then simply check if the filename passed to your "delete" function exist in your my_files array. 然后只需检查传递给“删除”函数的文件名是否存在于my_files数组中。
I'm sure there is a more elegant solution, but this could work ^_^ 我敢肯定有一个更优雅的解决方案,但这可以工作^ _ ^

You could use File.expand_path and File.dirname on the input, and check that against __FILE__ . 您可以在输入中使用File.expand_pathFile.dirname ,并根据__FILE__检查。 So something like this might work: 所以这样的事情可能会起作用:

File.delete(path) if File.dirname(File.expand_path(path)).include? File.dirname(File.expand_path(__FILE__))

I've got automated tests that routinely create and wipe out directories. 我有自动创建和清除目录的自动化测试。 I've taken two approaches: 我采取了两种方法:

  1. Use /tmp as much as possible. 尽可能使用/ tmp。 The 'tmpdir' standard library module will create temporary directories which will be destroyed when your program exits. “ tmpdir”标准库模块将创建临时目录,该目录将在程序退出时销毁。 Or, 要么,

  2. When the code creates a directory that it will later be deleting, it drops a marker file into the directory. 当代码创建一个以后将要删除的目录时,它将标记文件拖放到该目录中。 When it comes time to delete the directory, if the marker file is not found, the code refuses to delete the directory. 是时候删除目录了,如果找不到标记文件,则代码拒绝删除目录。 A marker file might be called ".ok_to_delete", for example. 例如,标记文件可能称为“ .ok_to_delete”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使您的Ruby代码模糊不清? - Obscure your Ruby code? 如何将CSV文件作为红宝石黄瓜数据存储库 - How to make your CSV file as your ruby cucumber data repository 如何查找使用您的gem的应用程序的目录? - How to find the directory of an app that is using your gem? 什么是ruby测试工具,它“破坏”了代码以查看测试的紧密程度? - What is the ruby test tool called that 'breaks' your code to see how tight your tests are? 如何使用Sublime Text在ROR中的应用程序根目录中创建文件example_user.rb - How to create a file example_user.rb in your application root directory in ROR using Sublime Text 在Ruby on Rails中为其他用户进行保存时如何保存参数 - How to save params when your saving for a different user in Ruby on Rails 如何修复“您的 Ruby 版本是 1.9.3,但您的 Gemfile 指定了 2.0.0” - How to fix "Your Ruby version is 1.9.3, but your Gemfile specified 2.0.0" 如何修复“您的 Ruby 版本为 2.3.3,但您的 Gemfile 指定为 2.5.2” - How to fix 'Your Ruby version is 2.3.3, but your Gemfile specified 2.5.2' 无法运行rails命令。您的Ruby版本是2.2.1,但您的Gemfile指定为2.1.4 - Cant run rails commands. Your Ruby version is 2.2.1, but your Gemfile specified 2.1.4 使用外部硬盘驱动器时,heroku 2.4.0命令不起作用 - Using external hard drive, heroku 2.4.0 commands are not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM