Working with sparse files in .NET

  • Thread starter Thread starter nickdu
  • Start date Start date
N

nickdu

I'm trying to create a sparse file and set the EOF to 4 TB. I can do this
easily enough in unmanaged code:

#include <windows.h>
#include <stdio.h>

void main(int argc, char *argv[])
{
// Cleanup
HANDLE hFile = (HANDLE) INVALID_HANDLE_VALUE;
// Cleanup
LARGE_INTEGER li = {0, 1024};
DWORD cb;
DWORD dwTick;
HRESULT hr = S_OK;

dwTick = GetTickCount();
if ((hFile = CreateFile(TEXT("c:\\mapped.map"), GENERIC_READ |
GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL |
FILE_FLAG_NO_BUFFERING, 0)) == (HANDLE) INVALID_HANDLE_VALUE)
{
hr = GetLastError();
hr = HRESULT_FROM_WIN32(hr);
goto cleanup;
}

printf("creating a sparse file.\n");
if (!DeviceIoControl(hFile, FSCTL_SET_SPARSE, NULL, 0, NULL, 0, &cb,
NULL))
{
hr = GetLastError();
hr = HRESULT_FROM_WIN32(hr);
goto cleanup;
}

if (!SetFilePointerEx(hFile, li, NULL, FILE_BEGIN))
{
hr = GetLastError();
hr = HRESULT_FROM_WIN32(hr);
printf("Failed SetFilePointerEx(), hr = 0x%08lX\n", hr);
goto cleanup;
}
if (!SetEndOfFile(hFile))
{
hr = GetLastError();
hr = HRESULT_FROM_WIN32(hr);
printf("Failed SetEndOfFile(), hr = 0x%08lX\n", hr);
goto cleanup;
}
cleanup:
if (hFile != (HANDLE) INVALID_HANDLE_VALUE)
CloseHandle(hFile);
dwTick = GetTickCount() - dwTick;
printf("Created file in %d milliseconds\n", dwTick);
return;
}

But I don't seem to be able to do the same thing using the .NET framework.
I tried:

using System;
using System.IO;

public class Application
{
public static void Main(string[] args)
{
if (args.Length != 2)
Console.WriteLine("Error, expecting file name and length.");
else
{
FileStream file = File.Create(args[0]);
file.Close();
File.SetAttributes(args[0], File.GetAttributes(args[0]) |
FileAttributes.SparseFile);
file = File.Open(args[0], FileMode.Open, FileAccess.ReadWrite,
FileShare.Read);
using(file)
{
file.SetLength(long.Parse(args[1]));
}
}
}
}

But it appears calling File.SetLength() is actually checking that I have
enough disk space and thus failing. Do I need to rely on interop?
--
Thanks,
Nick

(e-mail address removed)
remove "nospam" change community. to msn.com
 
Hello Nick

I tested your code. The problem is not with file.SetLength. The code

FileStream file = File.Create(args[0]);
file.Close();
File.SetAttributes(args[0], File.GetAttributes(args[0]) |
FileAttributes.SparseFile);

is not the right way to create a sparse file. (we can verify whether a file
is a sparse file by running the command 'fsutil sparse queryflag
<filename>'.

I find a good C# example of creating a sparse file by pinvoking the APIs
http://blogs.msdn.com/codedebate/archive/2007/12/18/6797175.aspx

As a test, you can change this line in the SparseFileTest!Program.cs file

fs.Seek(8192 * 1024, SeekOrigin.End);

to

fs.Seek(5000000000000, SeekOrigin.End);

and see if it successfully creates a file with 5TB size.

Please let me know if you have any other concerns, or need anything else.

Regards,
Jialiang Ge ([email protected], remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

MSDN Managed Newsgroup support offering is for non-urgent issues where an
initial response from the community or a Microsoft Support Engineer within
2 business day is acceptable. Please note that each follow up response may
take approximately 2 business days as the support professional working with
you may need further investigation to reach the most efficient resolution.
The offering is not appropriate for situations that require urgent,
real-time or phone-based interactions. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hello Nick

I am writing to check the status of the issue on your side. Would you mind
letting me know the result of the suggestions? If you need further
assistance, feel free to let me know. I will be more than happy to be of
assistance.

Happy New Year!

Regards,
Jialiang Ge ([email protected], remove 'online.')
Microsoft Online Community Support

=================================================
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no rights.
=================================================
 
Back
Top