[Box Backup-commit] COMMIT r997 - box/trunk/test/backupdiff

boxbackup-dev at fluffy.co.uk boxbackup-dev at fluffy.co.uk
Thu Oct 12 23:19:23 BST 2006


Author: chris
Date: 2006-10-12 23:19:22 +0100 (Thu, 12 Oct 2006)
New Revision: 997

Modified:
   box/trunk/test/backupdiff/testbackupdiff.cpp
Log:
 * Use truncation/sparse files to quickly create files with 200MB of zeroes


Modified: box/trunk/test/backupdiff/testbackupdiff.cpp
===================================================================
--- box/trunk/test/backupdiff/testbackupdiff.cpp	2006-10-12 22:18:33 UTC (rev 996)
+++ box/trunk/test/backupdiff/testbackupdiff.cpp	2006-10-12 22:19:22 UTC (rev 997)
@@ -66,23 +66,21 @@
 
 void make_file_of_zeros(const char *filename, size_t size)
 {
-	static const size_t bs = 0x10000;
-	size_t remSize = size;
-	void *b = malloc(bs);
-	memset(b, 0, bs);
-	FILE *f = fopen(filename, "wb");
+	#ifdef WIN32
+	HANDLE handle = openfile(filename, O_WRONLY | O_CREAT | O_EXCL, 0);
+	TEST_THAT(handle != INVALID_HANDLE_VALUE);
+	SetFilePointer(handle, size, NULL, FILE_BEGIN);
+	TEST_THAT(GetLastError() == NO_ERROR);
+	TEST_THAT(SetEndOfFile(handle) == true);
+	TEST_THAT(CloseHandle(handle)  == true);
+	#else
+	int fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0600);
+	if (fd < 0) perror(filename);
+	TEST_THAT(fd >= 0);
+	TEST_THAT(ftruncate(fd, size) == 0);
+	TEST_THAT(close(fd) == 0);
+	#endif
 
-	// Using largish blocks like this is much faster, while not consuming too much RAM
-	while(remSize > bs)
-	{
-		fwrite(b, bs, 1, f);
-		remSize -= bs;
-	}
-	fwrite(b, remSize, 1, f);
-
-	fclose(f);
-	free(b);
-
 	TEST_THAT((size_t)TestGetFileSize(filename) == size);
 }
 




More information about the Boxbackup-commit mailing list