如何prevent或拦截Directory.Delete通话(路径,真实) [英] How to prevent or intercept a call to Directory.Delete(path, true)

查看:127
本文介绍了如何prevent或拦截Directory.Delete通话(路径,真实)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我调试噗 - 所有客户的数据是 - 走的问题。它并没有花太多的时间来弄清楚,一个错误的分支导致code的 Directory.Delete(customerRoot,真)行。灾难性线被写了一个普通的GUI开发。不会有太多的线条,可以导致这样的灾难。所以我的问题是如何prevent这个特殊的电话。 ( DirectoryInfo.Delete()是第二个)。

Recently I was debugging a "poof-all-customer-data-is-gone" problem. It did not take much time to figure out that a wrong branch led to a Directory.Delete(customerRoot, true) line of code. The catastrophic line was written by a regular GUI developer. There are not many lines that can lead to such a disaster. So my question is how to prevent this particular call. (DirectoryInfo.Delete() is the second one).

下面是我优先考虑的可能的解决方案列表

Here is my prioritized list of possible solutions


  • 与任何第三方改变构建过程编译错误

  • 运行时拦截,没有第三方

  • 运行时拦截与第三方(不改变构建过程)

  • 与第三方(我猜PostSharp将做到这一点)
  • 编译错误
  • 有关客户如何爱自己的数据,图形用户界面开发人员教育研讨会

任何其他想法?

我会提到,我们的系统有文件/文件夹删除专用服务(验证和记录)。

I will mention that our system has a dedicated service (validated and logged) for file/folder delete.

推荐答案

要拦截在运行时调用,您可以使用的 FileSystemWatcher的类,但它实际上不会prevent删除,只是让你知道它的发生。 preventing实际删除很多麻烦。 Windows不提供的Linux的ptrace的功能,所以你不能拦截系统调用本身(有关于文章的此处但可用性相当从我可以告诉限制)。别人建议 - 设置访问权限和信任的水平 - 可能会奏效,但是当你真正需要的东西删除,你的删除服务就在.NET方法的包装,你会prevent,从工作,太。

To "intercept" the call at runtime, you can use the FileSystemWatcher class, but it wont actually prevent the deletion, just let you know it's happened. Preventing the actual deletion is a lot trickier. Windows does not offer Linux's ptrace functionality, so you cannot intercept the system call itself (there is an article about that here but the usability is fairly limited from what I can tell). What others suggest - setting access rights and trust levels - might work, but when you do actually need to delete something and your "deletion service" is just a wrapper around the .NET methods, you will prevent that from working, too.

另一种选择是去第三方工具和检查这在编译时 - 的这个问题刚刚您所需要的是,其中的例子,还有很多更多的工具可以做到这一点(我只用ReSharper的,但我想它可以完成这个任务为好)

The other option is to go for third party tools and check this at compile time - this question has just what you need for that, including examples, and there's a lot more tools that can do this (I only use Resharper, but I would imagine it can accomplish the task as well).

不过,我不认为那点这里。你的开发犯了一个错误,那疲惫不堪的一些客户资料,没有人是高兴。它不意味着你需要做它的一个全系统的规则和执行规则,这意味着开发应该已经测试了他或她的code更好的和/或应该使用删除服务你提到(无论是) 。不管你怎么努力,你不能prevent人做愚蠢的事情。你不想花费时间和金钱做防御工事周围的Directory.Delete()方法才发现,别人做出了不同的愚蠢的错误,这特别的事情永远不会再成为问题。除非这样的问题(滥用Directory.Delete())很容易出现比平时更在您的系统,让它去,专注于其他事情。

However, I dont think thats the point here. Your dev made a mistake, it whacked some customer data and nobody's happy about that. It doesnt mean you need to make a system-wide rule of it and enforce that rule, it means the dev should have tested his or her code better and/or should have used the deletion service you mention (whatever that is). No matter how hard you try, you cant prevent people from doing stupid things. You dont want to spend time and money making fortifications around the Directory.Delete() method only to find out that someone else made a different silly mistake and this particular thing will never be an issue again. Unless problems like this (misusing Directory.Delete() ) are prone to happen more than usual in your system, let it go, concentrate on other things.

这篇关于如何prevent或拦截Directory.Delete通话(路径,真实)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆