First public contribution.
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
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".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
18 // T_BIGFILE_WRITER.CPP
25 #pragma warning (disable:4201) // warning C4201: nonstandard extension used : nameless struct/union
30 #define __WRITE_FILE_CONTENTS__
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;
41 BYTE gBuffer[KBufSize];
44 T Min(T aLeft,T aRight)
45 {return(aLeft<aRight ? aLeft : aRight);}
48 BOOL WriteFile(char* aFileName, DWORD aSize)
52 HANDLE deviceHandle = CreateFileA(
54 GENERIC_WRITE, FILE_SHARE_WRITE,
55 NULL, CREATE_ALWAYS, 0, NULL);
57 if (deviceHandle == INVALID_HANDLE_VALUE)
59 printf("Open file error %d", GetLastError());
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)
68 for (DWORD n=0; n<KBufSize; n+=4)
70 *((DWORD*) &gBuffer[n]) = pos + n;
73 nNumberOfBytesToWrite = Min(KBufSize, aSize - pos);
74 DWORD nNumberOfBytesWritten;
76 success = WriteFile (deviceHandle, gBuffer, nNumberOfBytesToWrite, &nNumberOfBytesWritten, NULL);
79 printf("Write file error %d", GetLastError());
82 printf("\rWriting %s %lu%% done...", aFileName, (LONG64(pos) + LONG64(nNumberOfBytesToWrite)) * LONG64(100) / LONG64(aSize));
87 printf("Setting file size for %s to %u\n", aFileName, aSize);
88 LONG dwSeekLo = (LONG) aSize;
90 DWORD dwPos = SetFilePointer(deviceHandle, dwSeekLo, &dwSeekHi, FILE_BEGIN);
93 printf("SetFilePointer() error %d", GetLastError());
98 success = SetEndOfFile(deviceHandle);
101 printf("SetFilePointer() error %d", GetLastError());
107 CloseHandle(deviceHandle);
113 int main(int argc,char *argv[])
115 printf("BigFileWriter...\n");
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");
124 char filePath[] = "\\\\.\\?:\\F32-TST";
126 char driveLetter = argv[1][0];
127 filePath[4] = driveLetter;
128 printf("Creating big files on %s\n", filePath);
133 success = CreateDirectory(filePath, NULL);
134 if (!success && GetLastError() != ERROR_ALREADY_EXISTS)
136 printf("Unable to create directory %d", GetLastError());
140 success = SetCurrentDirectory(filePath);
143 printf("Unable to change to directory %d", GetLastError());
147 success = WriteFile("File2GBMinusOne.txt", K2Gb-1);
148 if (!success) exit(4);
150 success = WriteFile("File2GB.txt", K2Gb);
151 if (!success) exit(4);
153 success = WriteFile("File3GB.txt", K3Gb);
154 if (!success) exit(4);
156 // NB This won't fit on an 8GB drive
157 success = WriteFile("File4GBMinusOne.txt", K4GbMinusOne);
160 DeleteFile("File4GBMinusOne.txt");