basin
basin

Reputation: 4190

Allocate file on NTFS without zeroing

I want to make a tool similar to zerofree for linux. I want to do it by allocating a big file without zeroing it, look for nonzero blocks and rewrite them.

With admin privileges it is possible, uTorrent can do this: http://www.netcheif.com/Articles/uTorrent/html/AppendixA_02_12.html#diskio.no_zero , but it's closed source.

Upvotes: 3

Views: 1280

Answers (3)

ST3
ST3

Reputation: 8946

You should try SetFilePointerEx

Note that it is not an error to set the file pointer to a position beyond the end of the file.

So after you create file, call SetFilePointerEx and then SetEndOfFile or WriteFile or WriteFileEx and close the file, size should be increased.

EDIT

Raymonds suggested SetValidData is also good solution, but this requares privileges, so shouldn't be used often by anyone. My solution is the best on NTFS, because it supports feature known as initialized size it means that after using SetFilePointerEx data won't be initialized to zeros, but after attempt to read uninitialized data you will receive zeros.

To sum up, if NTFS use SetFilePointerEx, if FAT (not very likely) - use SetValidData

Upvotes: 0

basin
basin

Reputation: 4190

Wrote a tool https://github.com/basinilya/winzerofree . It uses SetFileValidData() as @RaymondChen suggested

Upvotes: 2

mox
mox

Reputation: 6324

I am not sure this answers your question (need), but such a tool already exists. You might have a look at fsutil.exe Fsutil command line tool. This tool has a huge potential to discover the internal structures of NTFS files and can also create file of any size (without the need to zeroing it manually). Hope that helps.

Upvotes: 2

Related Questions