/[pcsx2_0.9.7]/branch/debug/0.X/0.9.X/0.9.7/ramdump-lateset/pcsx2/windows/WinCompressNTFS.cpp
ViewVC logotype

Contents of /branch/debug/0.X/0.9.X/0.9.7/ramdump-lateset/pcsx2/windows/WinCompressNTFS.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 415 - (show annotations) (download)
Sun Feb 27 08:25:06 2011 UTC (9 years, 7 months ago) by william
File size: 3738 byte(s)
fix issues from upstream merge of r4239-r4370
1 /* PCSX2 - PS2 Emulator for PCs
2 * Copyright (C) 2002-2010 PCSX2 Dev Team
3 *
4 * PCSX2 is free software: you can redistribute it and/or modify it under the terms
5 * of the GNU Lesser General Public License as published by the Free Software Found-
6 * ation, either version 3 of the License, or (at your option) any later version.
7 *
8 * PCSX2 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
10 * PURPOSE. See the GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License along with PCSX2.
13 * If not, see <http://www.gnu.org/licenses/>.
14 */
15
16 #include "PrecompiledHeader.h"
17 #include "Win32.h"
18
19 // Throws an exception based on the value returned from GetLastError.
20 // Performs an option return value success/fail check on hresult.
21 void StreamException_ThrowLastError( const wxString& streamname, HANDLE result )
22 {
23 if( result != INVALID_HANDLE_VALUE ) return;
24
25 int error = GetLastError();
26
27 switch( error )
28 {
29 case ERROR_FILE_NOT_FOUND:
30 throw Exception::FileNotFound( streamname );
31
32 case ERROR_PATH_NOT_FOUND:
33 throw Exception::FileNotFound( streamname );
34
35 case ERROR_TOO_MANY_OPEN_FILES:
36 throw Exception::CannotCreateStream( streamname ).SetDiagMsg(L"Too many open files");
37
38 case ERROR_ACCESS_DENIED:
39 throw Exception::AccessDenied( streamname );
40
41 case ERROR_INVALID_HANDLE:
42 throw Exception::BadStream( streamname ).SetDiagMsg(L"Stream object or handle is invalid");
43
44 case ERROR_SHARING_VIOLATION:
45 throw Exception::AccessDenied( streamname ).SetDiagMsg(L"Sharing violation");
46
47 default:
48 {
49 throw Exception::BadStream( streamname ).SetDiagMsg(pxsFmt( L"General Win32 File/stream error [GetLastError: %d]", error ));
50 }
51 }
52 }
53
54 // returns TRUE if an error occurred.
55 bool StreamException_LogLastError( const wxString& streamname, const wxChar* action, HANDLE result )
56 {
57 try
58 {
59 StreamException_ThrowLastError( streamname, result );
60 }
61 catch( Exception::BadStream& ex )
62 {
63 Console.WriteLn( Color_Yellow, L"%s: %s", action, ex.FormatDiagnosticMessage().c_str() );
64 return true;
65 }
66 return false;
67 }
68
69 // Sets the NTFS compression flag for a directory or file. This function does not operate
70 // recursively. Set compressStatus to false to decompress compressed files (and do nothing
71 // to already decompressed files).
72 //
73 // Exceptions thrown: None.
74 // (Errors are logged to console. Failures are considered non-critical)
75 //
76 void NTFS_CompressFile( const wxString& file, bool compressStatus )
77 {
78 bool isFile = !wxDirExists( file );
79
80 if( isFile && !wxFileExists( file ) ) return;
81 if( GetFileAttributes(file) & FILE_ATTRIBUTE_COMPRESSED ) return;
82 if( !wxIsWritable( file ) ) return;
83
84 const DWORD flags = isFile ? FILE_ATTRIBUTE_NORMAL : (FILE_FLAG_BACKUP_SEMANTICS | FILE_ATTRIBUTE_DIRECTORY);
85
86 HANDLE bloated_crap = CreateFile( file,
87 FILE_GENERIC_WRITE | FILE_GENERIC_READ,
88 FILE_SHARE_READ | FILE_SHARE_DELETE,
89 NULL,
90 OPEN_EXISTING,
91 flags,
92 NULL
93 );
94
95 // Fail silently -- non-compression of files and folders is not an errorable offense.
96
97 if( !StreamException_LogLastError( file, L"NTFS Compress Notice", bloated_crap ) )
98 {
99 DWORD bytesReturned = 0;
100 DWORD compressMode = compressStatus ? COMPRESSION_FORMAT_DEFAULT : COMPRESSION_FORMAT_NONE;
101
102 BOOL result = DeviceIoControl(
103 bloated_crap, FSCTL_SET_COMPRESSION,
104 &compressMode, 2, NULL, 0,
105 &bytesReturned, NULL
106 );
107
108 if( !result )
109 StreamException_LogLastError( file, L"NTFS Compress Notice" );
110
111 CloseHandle( bloated_crap );
112 }
113 }

  ViewVC Help
Powered by ViewVC 1.1.22