如何检查是否可以在MS XP/Vista上的给定目录内创建文件? [英] How to check if a file can be created inside given directory on MS XP/Vista?

查看:63
本文介绍了如何检查是否可以在MS XP/Vista上的给定目录内创建文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个代码可以在用户指定的目录中创建文件.用户可以指向无法创建文件的目录,但可以对其重命名.

I have a code that creates file(s) in user-specified directory. User can point to a directory in which he can't create files, but he can rename it.

我已经创建了用于测试目的的目录,我们将其称为C:\foo.

I have created directory for test purposes, let's call it C:\foo.

我对C:\foo具有以下权限:

  • 遍历目录/执行文件
  • 删除子文件夹和文件
  • 移除
  • 读取权限
  • 更改权限
  • 取得所有权

我没有C:\foo的以下任何权限:

I don't have any of the following permissions to C:\foo:

  • 完全控制
  • 文件创建
  • 文件夹创建

到目前为止,我已经尝试了以下方法:

I have tried following approaches, so far:


os.access('C:\foo', os.W_OK) == True


os.access('C:\foo', os.W_OK) == True

st = os.stat('C:\foo')
mode = st[stat.ST_MODE]
mode & stat.S_IWRITE == True

st = os.stat('C:\foo')
mode = st[stat.ST_MODE]
mode & stat.S_IWRITE == True

我相信这是由于我可以重命名文件夹,因此它对我来说是可变的.但这很满足-不.

I believe that this is caused by the fact that I can rename folder, so it is changeable for me. But it's content - not.

有谁知道如果当前用户有权在该目录中创建文件,我该如何编写代码来检查给定目录?

Does anyone know how can I write code that will check for a given directory if current user has permissions to create file in that directory?

简而言之-我想检查当前用户是否具有给定文件夹名称的文件创建文件夹创建权限.

In brief - I want to check if current user has File creation and Folder creation permissions for given folder name.

"Windows Vista认证"程序的第3个测试用例引起了对此类代码的需求,该状态指出:

The need for such code arisen from the Test case no 3 from 'Certified for Windows Vista' program, which states:

  1. 该应用程序必须不允许最低特权用户将任何文件保存到Windows系统目录中,以通过此测试用例.

应该理解为应​​用程序可以尝试将文件保存在Windows系统目录中,但是在出现故障时不应该崩溃吗?"还是应用程序必须在尝试保存文件之前执行安全性检查?"

Should this be understood as 'Application may try to save file in Windows System directory, but shouldn't crash on failure?' or rather 'Application has to perform security checks before trying to save file?'

我是否应该仅因为Windows Vista本身不允许最低权限的用户将任何文件保存在%WINDIR%中就停止打扰?

Should I stop bothering just because Windows Vista itself won't allow the Least-Privileged user to save any files in %WINDIR%?

推荐答案

我最近编写了一个应用程序,以通过一组测试来从Microsoft获得ISV状态,并且我还添加了该条件. 我的理解是,如果用户是Least Priveledge,那么他将没有权限在系统文件夹中进行写.因此,我按照Ishmaeel的描述解决了这个问题.我尝试创建文件并捕获异常,然后通知用户他无权将文件写入该目录.

I recently wrote a App to pass a set of test to obtain the ISV status from Microsoft and I also add that condition. The way I understood it was that if the user is Least Priveledge then he won't have permission to write in the system folders. So I approached the problem the the way Ishmaeel described. I try to create the file and catch the exception then inform the user that he doesn't have permission to write files to that directory.

据我了解,特权级别最低的用户将没有必要的权限来写入这些文件夹,如果他拥有,则他不是特权级别最低的用户.

In my understanding an Least-Priviledged user will not have the necessary permissions to write to those folders, if he has then he is not a Least-Priveledge user.

我是否应该仅因为Windows Vista本身不允许最低权限的用户将任何文件保存在%WINDIR%中就停止打扰?

Should I stop bothering just because Windows Vista itself won't allow the Least-Privileged user to save any files in %WINDIR%?

我认为?是的.

这篇关于如何检查是否可以在MS XP/Vista上的给定目录内创建文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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