-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjunkfile.cpp
More file actions
151 lines (125 loc) · 3.48 KB
/
junkfile.cpp
File metadata and controls
151 lines (125 loc) · 3.48 KB
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
/*
* Copyright (C) 2014-2023, Brian Brice. All rights reserved.
*
* This file is part of junkfile.
*
* junkfile is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* junkfile is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with junkfile. If not, see <https://www.gnu.org/licenses/>.
*/
#include "targetver.h"
#include <cinttypes>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
#include <windows.h>
const std::string g_junk_filename = "junkfile";
const std::string g_junk_filename_extension = "bin";
int64_t get_free_space(void)
{
ULARGE_INTEGER free_bytes_avail;
if (GetDiskFreeSpaceEx(nullptr, &free_bytes_avail, nullptr, nullptr)) {
return static_cast<int64_t>(free_bytes_avail.QuadPart);
}
return 0;
}
bool file_exists(const std::string& filename)
{
return GetFileAttributes(filename.c_str()) != INVALID_FILE_ATTRIBUTES;
}
void get_next_filename(
const std::string& filename_prefix,
const std::string& extension,
std::string& filename
)
{
std::ostringstream os;
os << filename_prefix << '.' << extension;
for (size_t i = 1; file_exists(os.str()); i++) {
os.str("");
os << filename_prefix << std::setw(3) << std::setfill('0') << i << '.' << extension;
}
filename = os.str();
}
bool create_junk_file(const std::string& filename, int64_t size)
{
bool succ = false;
HANDLE h_file = CreateFile(
filename.c_str(), GENERIC_WRITE, 0, nullptr,
CREATE_NEW, FILE_FLAG_NO_BUFFERING, nullptr
);
if (h_file != INVALID_HANDLE_VALUE) {
LARGE_INTEGER file_pos;
file_pos.QuadPart = size;
if (SetFilePointerEx(h_file, file_pos, nullptr, FILE_BEGIN)) {
succ = (SetEndOfFile(h_file) != FALSE);
}
CloseHandle(h_file);
h_file = INVALID_HANDLE_VALUE;
if (!succ) {
DeleteFile(filename.c_str());
}
}
return succ;
}
void exit_msg(const std::string& msg)
{
std::cerr << "error: " << msg << std::endl;
exit(EXIT_FAILURE);
}
int main(int argc, char* argv[])
{
if (argc != 2) {
exit_msg("expected 1 argument, file size");
}
char* units;
int64_t size = strtoll(argv[1], &units, 10), free_space = get_free_space();
if (size == 0) {
exit_msg("could not parse argument, file size");
}
if (units && *units != '\0') {
switch (*units) {
case 't':
case 'T':
size *= 1024;
case 'g':
case 'G':
size *= 1024;
case 'm':
case 'M':
size *= 1024;
case 'k':
case 'K':
size *= 1024;
break;
default:
exit_msg("unknown unit");
}
}
if (size < 0) {
size = free_space + size;
}
if (size < 0 || free_space < size) {
exit_msg("file size is out of range");
}
std::string filename;
get_next_filename(g_junk_filename, g_junk_filename_extension, filename);
if (!create_junk_file(filename, size)) {
exit_msg("could not create junk file of requested size");
}
std::cout << "created file: " << filename << ", " << size << " bytes" << std::endl;
}