|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of the License "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 // |
|
18 // T_BIGFILE_WRITER.CPP |
|
19 // |
|
20 |
|
21 |
|
22 |
|
23 #include <windows.h> |
|
24 |
|
25 #pragma warning (disable:4201) // warning C4201: nonstandard extension used : nameless struct/union |
|
26 #include <winioctl.h> |
|
27 |
|
28 #include <stdio.h> |
|
29 |
|
30 #define __WRITE_FILE_CONTENTS__ |
|
31 |
|
32 |
|
33 const DWORD K1Kb = 1 << 10; |
|
34 const DWORD K1Mb = 1 << 20; |
|
35 const DWORD K1Gb = 1 << 30; |
|
36 const DWORD K2Gb = 0x80000000; |
|
37 const DWORD K3Gb = 0xC0000000; |
|
38 const DWORD K4GbMinusOne = 0xFFFFFFFF; |
|
39 const DWORD KBufSize = 256 * K1Kb; |
|
40 |
|
41 BYTE gBuffer[KBufSize]; |
|
42 |
|
43 template <class T> |
|
44 T Min(T aLeft,T aRight) |
|
45 {return(aLeft<aRight ? aLeft : aRight);} |
|
46 |
|
47 |
|
48 BOOL WriteFile(char* aFileName, DWORD aSize) |
|
49 { |
|
50 BOOL success = TRUE; |
|
51 |
|
52 HANDLE deviceHandle = CreateFileA( |
|
53 aFileName, |
|
54 GENERIC_WRITE, FILE_SHARE_WRITE, |
|
55 NULL, CREATE_ALWAYS, 0, NULL); |
|
56 |
|
57 if (deviceHandle == INVALID_HANDLE_VALUE) |
|
58 { |
|
59 printf("Open file error %d", GetLastError()); |
|
60 return FALSE; |
|
61 } |
|
62 |
|
63 #ifdef __WRITE_FILE_CONTENTS__ |
|
64 //printf("size of DWORD is %d", sizeof(DWORD)); |
|
65 DWORD nNumberOfBytesToWrite; |
|
66 for (DWORD pos = 0; pos < aSize; pos+=nNumberOfBytesToWrite) |
|
67 { |
|
68 for (DWORD n=0; n<KBufSize; n+=4) |
|
69 { |
|
70 *((DWORD*) &gBuffer[n]) = pos + n; |
|
71 } |
|
72 |
|
73 nNumberOfBytesToWrite = Min(KBufSize, aSize - pos); |
|
74 DWORD nNumberOfBytesWritten; |
|
75 |
|
76 success = WriteFile (deviceHandle, gBuffer, nNumberOfBytesToWrite, &nNumberOfBytesWritten, NULL); |
|
77 if (!success) |
|
78 { |
|
79 printf("Write file error %d", GetLastError()); |
|
80 break; |
|
81 } |
|
82 printf("\rWriting %s %lu%% done...", aFileName, (LONG64(pos) + LONG64(nNumberOfBytesToWrite)) * LONG64(100) / LONG64(aSize)); |
|
83 } |
|
84 |
|
85 printf("\n"); |
|
86 #else |
|
87 printf("Setting file size for %s to %u\n", aFileName, aSize); |
|
88 LONG dwSeekLo = (LONG) aSize; |
|
89 LONG dwSeekHi = 0; |
|
90 DWORD dwPos = SetFilePointer(deviceHandle, dwSeekLo, &dwSeekHi, FILE_BEGIN); |
|
91 if (dwPos != aSize) |
|
92 { |
|
93 printf("SetFilePointer() error %d", GetLastError()); |
|
94 success = FALSE; |
|
95 } |
|
96 else |
|
97 { |
|
98 success = SetEndOfFile(deviceHandle); |
|
99 if (!success) |
|
100 { |
|
101 printf("SetFilePointer() error %d", GetLastError()); |
|
102 } |
|
103 } |
|
104 |
|
105 #endif |
|
106 |
|
107 CloseHandle(deviceHandle); |
|
108 return success; |
|
109 } |
|
110 |
|
111 |
|
112 |
|
113 int main(int argc,char *argv[]) |
|
114 { |
|
115 printf("BigFileWriter...\n"); |
|
116 |
|
117 if (argc != 2) |
|
118 { |
|
119 printf("Creates big (between 2 & 4GB) files on a Windows drive for use by T_BIGFILE.EXE\n"); |
|
120 printf("Syntax : BigFileWriter <drive letter>\n"); |
|
121 exit(0); |
|
122 } |
|
123 |
|
124 char filePath[] = "\\\\.\\?:\\F32-TST"; |
|
125 |
|
126 char driveLetter = argv[1][0]; |
|
127 filePath[4] = driveLetter; |
|
128 printf("Creating big files on %s\n", filePath); |
|
129 |
|
130 BOOL success; |
|
131 |
|
132 |
|
133 success = CreateDirectory(filePath, NULL); |
|
134 if (!success && GetLastError() != ERROR_ALREADY_EXISTS) |
|
135 { |
|
136 printf("Unable to create directory %d", GetLastError()); |
|
137 exit(4); |
|
138 } |
|
139 |
|
140 success = SetCurrentDirectory(filePath); |
|
141 if (!success) |
|
142 { |
|
143 printf("Unable to change to directory %d", GetLastError()); |
|
144 exit(4); |
|
145 } |
|
146 |
|
147 success = WriteFile("File2GBMinusOne.txt", K2Gb-1); |
|
148 if (!success) exit(4); |
|
149 |
|
150 success = WriteFile("File2GB.txt", K2Gb); |
|
151 if (!success) exit(4); |
|
152 |
|
153 success = WriteFile("File3GB.txt", K3Gb); |
|
154 if (!success) exit(4); |
|
155 |
|
156 // NB This won't fit on an 8GB drive |
|
157 success = WriteFile("File4GBMinusOne.txt", K4GbMinusOne); |
|
158 if (!success) |
|
159 { |
|
160 DeleteFile("File4GBMinusOne.txt"); |
|
161 exit(4); |
|
162 } |
|
163 |
|
164 |
|
165 return 0; |
|
166 } |