Мгновенное резервирование места на диске

Пожелания по улучшению программы. Чего еще не хватает в DM.

Moderators: Korney San, Qwertiy, marcipan, igelizm

Post Reply
HMage
Posts: 1
Joined: 05 Jan 2008, 10:08 Sat

Мгновенное резервирование места на диске

Post by HMage »

Здравствуйте!

Очень долго ждать, пока зарезервируется место под гигабайтный файл и хотелось бы, чтобы резервирование места на диске было мгновенным.

Для этого есть функция SetEndOfFile().

Чтобы не быть голословным, прикрепляю пример:

Code: Select all

#include <stdio.h> // for fprintf() and atexit()
#include <conio.h> // for getch()
#include <windows.h> // for everything else

#pragma comment(lib, "winmm.lib") // for timeGetTime()

__int64 myFileSeek (HANDLE hf, __int64 distance, DWORD MoveMethod)
{
   LARGE_INTEGER li;
   li.QuadPart = distance;
   li.LowPart = SetFilePointer (hf, li.LowPart, &li.HighPart, MoveMethod);
   if (li.LowPart == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR) {
      li.QuadPart = INVALID_SET_FILE_POINTER;
   }
   return li.QuadPart;
}

void exithandler(void) {
	fprintf (stderr, "\n\nPress any key to close this application...");
	getch();
	fprintf (stderr, "\n");
}

int main (int argc, char* argv[]) {
	HANDLE file;
	__int64 res;
	BOOL res2;
	DWORD start;

	fprintf (stdout, "SetEndOfFile() example program.\n\n");

	fprintf (stdout, "This program will create a 1GB file named \"example.dat\"\n"
					 "in the root of C: disk using SetEndOfFile() call.\n");

	fprintf (stdout, "=========================================================\n\n");
	fprintf (stdout, "Creating file...\n");

	atexit(exithandler);
	start = timeGetTime();

	file = CreateFileA("C:\\example.dat", GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, 0, NULL);
	if (file == INVALID_HANDLE_VALUE) {
		fprintf (stderr, "ERROR: Couldn't create file.\n");
		return 1;
	}

	res = myFileSeek(file, 0x40000000, FILE_BEGIN);
	if (res == INVALID_SET_FILE_POINTER) {
		fprintf (stderr, "ERROR: Couldn't seek to 1GB.\n");
		return 1;
	}

	res2 = SetEndOfFile(file);
	if (res2 == FALSE) {
		fprintf (stderr, "ERROR: Couldn't set file size to 1GB.\n");
		return 1;
	}

	CloseHandle(file);

	fprintf (stdout, "Done creating 1GB file!\n");
	fprintf (stdout, "Creating new 1GB file took %.3f seconds.\n", (float)(timeGetTime() - start) * 0.001f);

	return 0;
}
Исполняемый файл для проверки:
http://www.hmage.ru/seteof.zip
Post Reply