-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcvt2sparse.c
executable file
·185 lines (156 loc) · 4.34 KB
/
cvt2sparse.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include <windows.h>
#include <stdio.h>
#include <inttypes.h>
#include "utils.h"
/*#include "bitfields.h"
#include "oassert.h"
#include "memutils.h"
#include "porg_utils.h"
#include "files.h"*/
BOOL verbose=FALSE, dry_run= FALSE;
void die_GetLastError(char *fname)
{
DWORD err = GetLastError();
exit(-1);
}
void set_sparse_range(char *fname, HANDLE h, LONGLONG start, LONGLONG size)
{
FILE_ZERO_DATA_INFORMATION fzdi;
fzdi.FileOffset.QuadPart = start;
fzdi.BeyondFinalZero.QuadPart = start + size;
DWORD dw;
BOOL b=DeviceIoControl(h, FSCTL_SET_ZERO_DATA, &fzdi, sizeof(fzdi), NULL, 0, &dw, NULL);
if (b==FALSE)
{
printf ("Error while setting attributes for %s file:\n", fname);
die_GetLastError ("DeviceIoControl(...FSCTL_SET_ZERO_DATA...) failed");
};
}
void convert_file_to_sparse (char *fname)
{
BOOL b;
HANDLE f=CreateFile (fname, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, NULL);
if (f==INVALID_HANDLE_VALUE)
{
printf ("Error while opening %s file:\n", fname);
die_GetLastError ("CreateFile()");
};
LARGE_INTEGER file_size;
if (GetFileSizeEx (f, &file_size)==FALSE)
die_GetLastError ("GetFileSizeEx()");
printf ("%s...\n", fname);
if (dry_run == FALSE)
{
DWORD dwTemp;
b=DeviceIoControl(f, FSCTL_SET_SPARSE, NULL, 0, NULL, 0, &dwTemp, NULL);
if (b==FALSE)
{
printf ("Error while setting attributes for %s file:\n", fname);
die_GetLastError ("DeviceIoControl(...FSCTL_SET_SPARSE...) failed");
};
};
#define BLK_SIZE 0x100000
BYTE* buf=(BYTE*)malloc(BLK_SIZE);
//oassert(buf);
int state=0; // 0 - initial, 1 - first zero block was seen
LONGLONG zero_blk_begin=0;
LONGLONG cur_pos=0;
LONGLONG zero_bufs_size=0;
for (;;)
{
DWORD was_read = 0;
b = ReadFile (f, buf, BLK_SIZE, &was_read, NULL);
BOOL is_zero_buf = is_blk_zero (buf, was_read);
if (state == 0 && was_read > 0 && is_zero_buf)
{
zero_blk_begin=cur_pos;
if (verbose)
{
printf("zero block. begin=0x%" PRIx64, zero_blk_begin);
}
state=1;
}
else if (state == 1 && is_zero_buf == FALSE)
{
// got zero block
LONGLONG sz = cur_pos - zero_blk_begin;
if (verbose)
{
printf (" end=0x%I64x, size=%" PRId64 "\n", cur_pos, sz);
/*strbuf sb=STRBUF_INIT;
strbuf_fancy_size (&sb, sz);
strbuf_puts(&sb);
strbuf_deinit(&sb);*/
};
if (dry_run == FALSE)
set_sparse_range (fname, f, zero_blk_begin, sz);
state = 0;
zero_bufs_size += sz;
};
if (b == FALSE || was_read == 0)
{
break;
}
cur_pos += was_read;
};
free (buf);
CloseHandle (f);
printf ("Total size of zero blocks marked=%" PRId64 "\n", zero_bufs_size);
/*strbuf sb=STRBUF_INIT;
strbuf_fancy_size (&sb, zero_bufs_size);
strbuf_puts(&sb);
strbuf_deinit(&sb);*/
printf ("Now the file occupies %02f%% of its size.\n",
100-(double)(((double)zero_bufs_size/(double)file_size.QuadPart)*100));
};
void convert_file_or_dir_to_sparse (char *fname)
{
WIN32_FIND_DATA d;
HANDLE h=FindFirstFile (fname, &d);
if (h==INVALID_HANDLE_VALUE)
die_GetLastError ("FindFirstFile()");
do
{
if (IS_SET(d.dwFileAttributes, FILE_ATTRIBUTE_DIRECTORY))
continue;
/*strbuf sb=STRBUF_INIT;
full_path_and_filename_to_path_only (&sb, fname);
strbuf_addstr (&sb, d.cFileName);
convert_file_to_sparse (sb.buf);
strbuf_deinit (&sb);*/
convert_file_to_sparse(d.cFileName);
} while (FindNextFile (h, &d)!=FALSE);
};
int main()
{
int argc = 0;
PCHAR *argv = CommandLineToArgvA( GetCommandLineA(), &argc );
printf ("Convert to NTFS sparse file <dennis(a)yurichev.com>, <vaclav.lipert(a)live.com> (%s)\n\n", __DATE__);
if (argc==1)
{
printf ("Usage: cvt2parse [--verbose] [--dry-run] file_or_directory ...\n");
printf ("Examples:\n");
printf ("\tcvt2parse file1.vmdk file2.vmdk\n");
printf ("\tcvt2parse *.vmdk\n");
printf ("\tcvt2parse c:\\virtual_machine\\*.vmdk\n");
return 1;
};
for (int arg=1; arg<argc; arg++)
{
//oassert (argv[arg]);
if (stricmp (argv[arg], "--verbose")==0)
{
printf ("Setting verbose\n");
verbose = TRUE;
}
else if (stricmp (argv[arg], "--dry-run")==0)
{
printf ("Setting dry run\n");
dry_run = TRUE;
}
else
convert_file_or_dir_to_sparse (argv[arg]);
};
return 0;
};