ucommon  7.0.0
About: GNU uCommon C++ is a portable and optimized class framework for writing C++ applications that need to use threads and support concurrent synchronization, and that use sockets, XML parsing, object serialization, thread-optimized string and data structure classes, etc..
  Fossies Dox: ucommon-7.0.0.tar.gz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

zerofill.cpp
Go to the documentation of this file.
1 // Copyright (C) 2010-2014 David Sugar, Tycho Softworks.
2 // Copyright (C) 2015 Cherokees of Idaho.
3 //
4 // This file is part of GNU uCommon C++.
5 //
6 // GNU uCommon C++ is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU Lesser General Public License as published
8 // by the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // GNU uCommon C++ is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public License
17 // along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>.
18 
19 #include <ucommon/secure.h>
20 
21 using namespace ucommon;
22 
23 static shell::flagopt helpflag('h',"--help", _TEXT("display this list"));
24 static shell::flagopt althelp('?', NULL, NULL);
25 static shell::numericopt passes('r', "--random", _TEXT("optional passes with randomized data (0-x)"), "count", 0);
26 
27 static uint8_t buffer[65536];
28 static bool live = false;
29 static bool temp = false;
30 
31 static void cleanup(void)
32 {
33  if(temp)
34  fsys::erase("zerofill.tmp");
35  if(live)
36  shell::printf("\n");
37  live = false;
38 }
39 
40 static void zerofill(void)
41 {
42  fsys::offset_t pos = 0l;
43  ssize_t size = 65536;
44  unsigned long count = 0;
45  fsys_t fs;
46 
47  temp = true;
48 
49  fs.open("zerofill.tmp", 0666, fsys::STREAM);
50  if(!is(fs))
51  shell::errexit(1, "*** zerofill: %s\n",
52  _TEXT("cannot create temporary file"));
53 
54  live = true;
55  while(size == 65536 && live) {
56  unsigned pass = 0;
57 
58  while(pass < (unsigned)*passes) {
59  fs.seek(pos);
60  Random::fill(buffer, sizeof(buffer));
61  fs.write(buffer, sizeof(buffer));
62  ++pass;
63  }
64 
65  fs.seek(pos);
66  memset(buffer, 0, sizeof(buffer));
67  size = fs.write(buffer, sizeof(buffer));
68  pos += sizeof(buffer);
69  if(++count >= 256) {
70  shell::printf(".");
71  count = 0;
72  }
73  }
74 
75  cleanup();
76 
77  if(fs.err() != ENOSPC)
78  shell::errexit(3, "*** zerofill: %s\n",
79  _TEXT("failed before end of space"));
80 }
81 
82 static void zerofill(const char *devname)
83 {
84  fsys::fileinfo_t ino;
85  fsys::offset_t pos = 0l;
86  ssize_t size = 65536;
87  unsigned long count = 0;
88  fsys_t fs;
89 
90  if(fsys::info(devname, &ino))
91  shell::errexit(5, "*** zerofill: %s: %s\n",
92  devname, _TEXT("cannot access"));
93 
94  if(!fsys::is_disk(&ino))
95  shell::errexit(6, "*** zerofill: %s: %s\n",
96  devname, _TEXT("not block device"));
97 
98  fs.open(devname, fsys::WRONLY);
99  if(fs.err())
100  shell::errexit(5, "*** zerofill: %s: %s\n",
101  devname, _TEXT("cannot modify"));
102 
103  shell::printf("%s", devname);
104 
105  live = true;
106  while(size == 65536 && live) {
107  unsigned pass = 0;
108 
109  while(pass < (unsigned)*passes) {
110  fs.seek(pos);
111  Random::fill(buffer, sizeof(buffer));
112  fs.write(buffer, sizeof(buffer));
113  ++pass;
114  }
115 
116  fs.seek(pos);
117  memset(buffer, 0, sizeof(buffer));
118  size = fs.write(buffer, sizeof(buffer));
119  pos += sizeof(buffer);
120  if(++count >= 256) {
121  shell::printf(".");
122  count = 0;
123  }
124  }
125 
126  cleanup();
127 
128  if(fs.err() != ENOSPC)
129  shell::errexit(3, "*** zerofill: %s\n",
130  _TEXT("failed before end of space"));
131 }
132 
133 int main(int argc, char **argv)
134 {
135  shell::bind("zerofill");
136  shell args(argc, argv);
137  unsigned count = 0;
138 
139  if(*passes < 0)
140  shell::errexit(2, "*** zerofill: random: %ld: %s\n",
141  *passes, _TEXT("negative random passes invalid"));
142 
143  if(is(helpflag) || is(althelp)) {
144  printf("%s\n", _TEXT("Usage: zerofill [options] path..."));
145  printf("%s\n\n", _TEXT("Erase and fill unused space with zeros"));
146  printf("%s\n", _TEXT("Options:"));
147  shell::help();
148  printf("\n%s\n", _TEXT("Report bugs to dyfet@gnu.org"));
149  return 0;
150  }
151 
152  secure::init();
153 
155 
156  if(!args())
157  zerofill();
158 
159  while(count < args() && live)
160  zerofill(args[count++]);
161 
162  return 0;
163 }
164 
ucommon::shell::numericopt
Definition: shell.h:389
helpflag
static shell::flagopt helpflag('h',"--help", _TEXT("display this list"))
ucommon::fsys::write
ssize_t write(const void *buffer, size_t count)
Definition: fsys.cpp:769
ucommon::fsys::fileinfo_t
struct stat fileinfo_t
Definition: fsys.h:147
ucommon::shell::bind
static void bind(const char *name)
Definition: shell.cpp:2113
ucommon
Definition: access.cpp:23
ucommon::Random::fill
static size_t fill(uint8_t *memory, size_t size)
Definition: random.cpp:40
passes
static shell::numericopt passes('r', "--random", _TEXT("optional passes with randomized data (0-x)"), "count", 0)
ucommon::fsys::err
int err(void) const
Definition: fsys.h:557
ucommon::fsys::erase
static int erase(const char *path)
Definition: fsys.cpp:1341
ucommon::secure::init
static bool init(void)
Definition: secure.cpp:37
ucommon::fsys::offset_t
long offset_t
Definition: fsys.h:176
temp
static bool temp
Definition: zerofill.cpp:29
ucommon::shell::errexit
static void static void errexit(int exitcode, const char *format=NULL,...) __PRINTF(2
Definition: shell.cpp:828
ucommon::fsys::info
int info(fileinfo_t *buffer)
ucommon::shell
Definition: shell.h:59
cleanup
static void cleanup(void)
Definition: zerofill.cpp:31
ucommon::fsys::is_disk
static bool is_disk(struct stat *inode)
Definition: fsys.h:649
secure.h
buffer
static uint8_t buffer[65536]
Definition: zerofill.cpp:27
live
static bool live
Definition: zerofill.cpp:28
ucommon::fsys::STREAM
Definition: fsys.h:169
ucommon::fsys
Definition: fsys.h:125
main
int main(int argc, char **argv)
Definition: zerofill.cpp:133
ucommon::shell::exiting
static void exiting(exitproc_t)
Definition: shell.cpp:1771
ucommon::fsys::open
void open(const char *path, access_t access)
Definition: fsys.cpp:919
althelp
static shell::flagopt althelp('?', NULL, NULL)
ucommon::shell::flagopt
Definition: shell.h:234
ucommon::fsys::WRONLY
Definition: fsys.h:162
ucommon::shell::help
static void help(void)
Definition: shell.cpp:408
ucommon::shell::printf
static size_t printf(const char *format,...) __PRINTF(1
Definition: shell.cpp:874
ucommon::fsys::seek
int seek(offset_t offset)
Definition: fsys.cpp:1124
ucommon::zerofill
void zerofill(void *addr, size_t size)
Definition: secure.h:895
ucommon::is
bool is(T &object)
Definition: generics.h:292
ucommon::_TEXT
const char * _TEXT(const char *s)
Definition: shell.h:911