summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2009-04-29 18:13:17 +0300
committerLars Wirzenius <liw@liw.fi>2009-04-29 18:13:17 +0300
commit721028c921e56e3126cbf94b95a11d4f279c0fdb (patch)
tree8a779277c5cec683c8738b56b9cf6eddfdc274b8
parentfca0f1229335f599176866f64b03a2df442c773c (diff)
parent2811012b43d1d045358992add5af3b5c1b524e65 (diff)
downloadextrautils-721028c921e56e3126cbf94b95a11d4f279c0fdb.tar.gz
Merged implementation of create-file.
-rwxr-xr-xcreate-file89
-rw-r--r--create-file.145
2 files changed, 134 insertions, 0 deletions
diff --git a/create-file b/create-file
new file mode 100755
index 0000000..dd41073
--- /dev/null
+++ b/create-file
@@ -0,0 +1,89 @@
+#!/usr/bin/python
+#
+# create-file -- create a file full of zeroes
+# Copyright (C) 2009 Lars Wirzenius
+#
+# This program 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.
+#
+# This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
+
+
+import optparse
+import os
+
+
+def parse_size(size):
+ sizes = (
+ ("k", 1000),
+ ("m", 1000**2),
+ ("g", 1000**3),
+
+ ("kb", 1000),
+ ("mb", 1000**2),
+ ("gb", 1000**3),
+
+ ("kib", 1024),
+ ("mib", 1024**2),
+ ("gib", 1024**3),
+ )
+ size = size.lower()
+ for suffix, x in sizes:
+ if size.endswith(suffix):
+ amount = int(size[:-len(suffix)].strip())
+ return amount * x
+ return int(size.strip())
+
+
+def parse_args():
+ parser = optparse.OptionParser()
+
+ parser.epilog = ("SIZE is a size. Default is in bytes, use suffixes "
+ "kB/MB/GB for multiples of 1000, KiB/MiB/GiB for "
+ "multiples of 1024. Only integers supported.")
+
+ parser.add_option("--sparse", action="store_true",
+ help="create a sparse file")
+
+ parser.add_option("--size", metavar="SIZE", default="0",
+ help="create a file of size SIZE")
+
+ options, filenames = parser.parse_args()
+ options.size = parse_size(options.size)
+
+ return options, filenames
+
+
+def create_file(filename, options):
+ fd = os.open(filename, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0666)
+ if options.sparse:
+ if options.size > 0:
+ os.lseek(fd, options.size - 1, os.SEEK_SET)
+ os.write(fd, "\0")
+ else:
+ bytes = options.size
+ data = "\0" * 1024**2
+ while bytes >= len(data):
+ os.write(fd, data)
+ bytes -= len(data)
+ if bytes > 0:
+ os.write(fd, data[:bytes])
+ os.close(fd)
+
+
+def main():
+ options, filenames = parse_args()
+ for filename in filenames:
+ create_file(filename, options)
+
+
+if __name__ == "__main__":
+ main()
diff --git a/create-file.1 b/create-file.1
new file mode 100644
index 0000000..4bdfea7
--- /dev/null
+++ b/create-file.1
@@ -0,0 +1,45 @@
+.\" create-file.1 - manual page for the create-file command
+.\" Copyright (C) 2009 Lars Wirzenius
+.\"
+.\" This program 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.
+.\"
+.\" This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
+.\"
+.TH CREATE-FILE 1
+.SH NAME
+create-file \- create a file full of zero bytes
+.SH SYNOPSIS
+.B create-file
+.RB [ -h ]
+.RB [ --help ]
+.RB [ --sparse ]
+.RB [ --size =\fISIZE ]
+.RI [ filename ]...
+.SH DESCRIPTION
+.B create-file
+creates a file full of zero bytes (0x00, NUL).
+.SH OPTIONS
+.TP
+.BR -h ", " --help
+Show a short help message.
+.TP
+.B --sparse
+Create a sparse file, which does not allocate disk blocks.
+.TP
+.BR --size =\fISIZE
+Create a file of
+.I SIZE
+bytes.
+The size may be have a suffix to indicate kilobyte (K, KB), megabyte (M, MB),
+or gigabyte (G, GB), or power-of-two multipliers for kibibyte (KiB),
+mebibyte (MiB), or gibibyte (GiB).
+All suffixes are case-insensitive.