1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "sandbox/win/src/restricted_token.h"
6 #include "sandbox/win/src/restricted_token_utils.h"
7 #include "sandbox/win/tools/finder/finder.h"
9 DWORD Finder::ParseFileSystem(ATL::CString directory) {
10 WIN32_FIND_DATA find_data;
13 //Search for items in the directory.
14 ATL::CString name_to_search = directory + L"\\*";
15 find = ::FindFirstFile(name_to_search, &find_data);
16 if (INVALID_HANDLE_VALUE == find) {
17 DWORD error = ::GetLastError();
18 Output(FS_ERR, error, directory);
19 filesystem_stats_[BROKEN]++;
23 // parse all files or folders.
25 if (_tcscmp(find_data.cFileName, L".") == 0 ||
26 _tcscmp(find_data.cFileName, L"..") == 0)
29 ATL::CString complete_name = directory + L"\\" + find_data.cFileName;
30 TestFileAccess(complete_name);
32 // Call recursively the function if the path found is a directory.
33 if ((find_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0) {
34 ParseFileSystem(complete_name);
36 } while (::FindNextFile(find, &find_data) != 0);
38 DWORD err_code = ::GetLastError();
41 if (ERROR_NO_MORE_FILES != err_code) {
42 Output(FS_ERR, err_code, directory);
43 filesystem_stats_[BROKEN]++;
50 DWORD Finder::TestFileAccess(ATL::CString name) {
51 Impersonater impersonate(token_handle_);
53 filesystem_stats_[PARSE]++;
56 if (access_type_ & kTestForAll) {
57 file = ::CreateFile(name.GetBuffer(),
59 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
62 FILE_ATTRIBUTE_NORMAL,
65 if (file != INVALID_HANDLE_VALUE) {
66 filesystem_stats_[ALL]++;
67 Output(FS, L"R/W", name.GetBuffer());
70 } else if (::GetLastError() != ERROR_ACCESS_DENIED) {
71 Output(FS_ERR, GetLastError(), name);
72 filesystem_stats_[BROKEN]++;
76 if (access_type_ & kTestForWrite) {
77 file = ::CreateFile(name.GetBuffer(),
79 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
82 FILE_ATTRIBUTE_NORMAL,
85 if (file != INVALID_HANDLE_VALUE) {
86 filesystem_stats_[WRITE]++;
87 Output(FS, L"W", name);
90 } else if (::GetLastError() != ERROR_ACCESS_DENIED) {
91 Output(FS_ERR, ::GetLastError(), name);
92 filesystem_stats_[BROKEN]++;
96 if (access_type_ & kTestForRead) {
97 file = ::CreateFile(name.GetBuffer(),
99 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
102 FILE_ATTRIBUTE_NORMAL,
105 if (file != INVALID_HANDLE_VALUE) {
106 filesystem_stats_[READ]++;
107 Output(FS, L"R", name);
110 } else if (::GetLastError() != ERROR_ACCESS_DENIED) {
111 Output(FS_ERR, GetLastError(), name);
112 filesystem_stats_[BROKEN]++;