From ed8aefcec6235b5c70bf014699ccc3c4c6a43d80 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Tue, 27 Jan 2009 14:39:41 +0200 Subject: Wrote first version of corrupt. --- Makefile | 4 ++- corrupt.1 | 25 +++++++++++++ corrupt.c | 118 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 146 insertions(+), 1 deletion(-) create mode 100644 corrupt.1 create mode 100644 corrupt.c diff --git a/Makefile b/Makefile index 20a7bc9..45087a0 100644 --- a/Makefile +++ b/Makefile @@ -4,9 +4,11 @@ sharedir = $(prefix)/share mandir = $(sharedir)/man man1dir = $(mandir)/man1 -progs = isascii +progs = isascii corrupt scripts = total +CFLAGS = -Wall -O2 --std=gnu99 + all: $(progs) install: all diff --git a/corrupt.1 b/corrupt.1 new file mode 100644 index 0000000..f9b1338 --- /dev/null +++ b/corrupt.1 @@ -0,0 +1,25 @@ +.\" corrupt.1 - manual page for the corrupt 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 . +.\" +.TH CORRUPT 1 +.SH NAME +corrupt \- modify files by randomly changing bits +.SH SYNOPSIS +.BR corrupt +.IR file ... +.SH DESCRIPTION +.B corrupt +modifies files by toggling a randomly chosen bit. diff --git a/corrupt.c b/corrupt.c new file mode 100644 index 0000000..f8c5cd2 --- /dev/null +++ b/corrupt.c @@ -0,0 +1,118 @@ +/* + * 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 . + */ + + +#define _LARGEFILE64_SOURCE + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +/* Choose a random integer in [0..n-1]. */ +static long choose(long n) +{ + if (n == 0) + return 0; + + long buckets = (RAND_MAX + 1L) / n; + long max = buckets * n; + long r; + + do { + r = random(); + } while (r >= max); + return r / buckets; +} + + +static int corrupt(const char *filename) +{ + int fd; + struct stat st; + + fd = open(filename, O_RDWR | O_LARGEFILE); + if (fd == -1) { + fprintf(stderr, "Can't open %s: %s\n", filename, strerror(errno)); + return -1; + } + + if (fstat(fd, &st) == -1) { + fprintf(stderr, "Can't find length of %s: %s", filename, + strerror(errno)); + return -1; + } + + long offset = choose(st.st_size); + long bit = choose(CHAR_BIT); + printf("%s: offset=%ld bit=%ld\n", filename, offset, bit); + + if (lseek(fd, offset, SEEK_SET) == (off_t) -1) { + fprintf(stderr, "Can't seek in %s: %s", filename, strerror(errno)); + return -1; + } + + unsigned char byte; + if (read(fd, &byte, 1) == -1) { + fprintf(stderr, "Can't read %s: %s", filename, strerror(errno)); + return -1; + } + + /* This is where we toggle the bit. */ + unsigned mask = 1 << bit; // All zeroes, except for the interesting bit. + unsigned interesting = byte & mask; // Only the interesting bit can be 1. + unsigned toggled = interesting ^ mask; // Interesting bit has been toggled. + unsigned others = byte & (~mask); // Interesting bit is 0. + byte = others | toggled; + + /* Now write it back. */ + if (lseek(fd, offset, SEEK_SET) == (off_t) -1) { + fprintf(stderr, "Can't seek in %s: %s", filename, strerror(errno)); + return -1; + } + if (write(fd, &byte, 1) != 1) { + fprintf(stderr, "Can't write %s: %s", filename, strerror(errno)); + return -1; + } + + if (close(fd) == -1) { + fprintf(stderr, "Can't close %s: %s", filename, strerror(errno)); + return -1; + } + + return 0; +} + + +int main(int argc, char **argv) +{ + int exit_code; + + srandom(time(NULL) + getpid()); + exit_code = 0; + for (int i = 1; i < argc; ++i) + if (corrupt(argv[i]) == -1) + exit_code = 1; + return exit_code; +} -- cgit v1.2.1 From 4accd8958f01d7aac837c2fff716ba14ea2aa4b7 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Tue, 27 Jan 2009 15:13:56 +0200 Subject: Added a --bits (-n) option to corrupt. --- corrupt.c | 109 ++++++++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 84 insertions(+), 25 deletions(-) diff --git a/corrupt.c b/corrupt.c index f8c5cd2..0d29453 100644 --- a/corrupt.c +++ b/corrupt.c @@ -17,6 +17,7 @@ #define _LARGEFILE64_SOURCE +#define _GNU_SOURCE #include #include @@ -28,6 +29,11 @@ #include #include #include +#include + + +/* Number of bits to corrupt. */ +static int num_bits = 1; /* Choose a random integer in [0..n-1]. */ @@ -47,33 +53,27 @@ static long choose(long n) } -static int corrupt(const char *filename) +static int seek(const char *filename, int fd, off_t offset) { - int fd; - struct stat st; - - fd = open(filename, O_RDWR | O_LARGEFILE); - if (fd == -1) { - fprintf(stderr, "Can't open %s: %s\n", filename, strerror(errno)); + if (lseek(fd, offset, SEEK_SET) == (off_t) -1) { + fprintf(stderr, "Can't seek in %s: %s", filename, strerror(errno)); return -1; } + return 0; +} - if (fstat(fd, &st) == -1) { - fprintf(stderr, "Can't find length of %s: %s", filename, - strerror(errno)); - return -1; - } - long offset = choose(st.st_size); - long bit = choose(CHAR_BIT); - printf("%s: offset=%ld bit=%ld\n", filename, offset, bit); +static int corrupt_one_bit(const char *filename, int fd, struct stat st) +{ + long offset; + long bit; + unsigned char byte; + + offset = choose(st.st_size); + bit = choose(CHAR_BIT); - if (lseek(fd, offset, SEEK_SET) == (off_t) -1) { - fprintf(stderr, "Can't seek in %s: %s", filename, strerror(errno)); + if (seek(filename, fd, offset) == -1) return -1; - } - - unsigned char byte; if (read(fd, &byte, 1) == -1) { fprintf(stderr, "Can't read %s: %s", filename, strerror(errno)); return -1; @@ -87,14 +87,37 @@ static int corrupt(const char *filename) byte = others | toggled; /* Now write it back. */ - if (lseek(fd, offset, SEEK_SET) == (off_t) -1) { - fprintf(stderr, "Can't seek in %s: %s", filename, strerror(errno)); + if (seek(filename, fd, offset) == -1) return -1; - } if (write(fd, &byte, 1) != 1) { fprintf(stderr, "Can't write %s: %s", filename, strerror(errno)); return -1; } + + return 0; +} + + +static int corrupt(const char *filename) +{ + int fd; + struct stat st; + + fd = open(filename, O_RDWR | O_LARGEFILE); + if (fd == -1) { + fprintf(stderr, "Can't open %s: %s\n", filename, strerror(errno)); + return -1; + } + + if (fstat(fd, &st) == -1) { + fprintf(stderr, "Can't find length of %s: %s", filename, + strerror(errno)); + return -1; + } + + for (int i = 0; i < num_bits; ++i) + if (corrupt_one_bit(filename, fd, st) == -1) + return -1; if (close(fd) == -1) { fprintf(stderr, "Can't close %s: %s", filename, strerror(errno)); @@ -105,13 +128,49 @@ static int corrupt(const char *filename) } +static int parse_args(int argc, char **argv) +{ + const char optstring[] = "n:"; + const struct option longopts[] = { + { "bits", required_argument, NULL, 'n' }, + { 0 }, + }; + char *endptr; + + for (;;) { + int opt = getopt_long(argc, argv, optstring, longopts, NULL); + switch (opt) { + case -1: + return optind; + + case 'n': + num_bits = strtol(optarg, &endptr, 10); + if (*endptr != '\0') { + fprintf(stderr, "Invalid number of bits: %s\n", optarg); + exit(1); + } + break; + + default: + case '?': + exit(1); + } + } +} + + int main(int argc, char **argv) { int exit_code; - + int i; + + i = parse_args(argc, argv); + if (i == -1) + return 1; + srandom(time(NULL) + getpid()); exit_code = 0; - for (int i = 1; i < argc; ++i) + for (; i < argc; ++i) if (corrupt(argv[i]) == -1) exit_code = 1; return exit_code; -- cgit v1.2.1 From fde4d951bc38301e94ad336c73835e7108704dbc Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Tue, 27 Jan 2009 15:19:09 +0200 Subject: Updated manual page for corrupt. --- corrupt.1 | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/corrupt.1 b/corrupt.1 index f9b1338..593a0e3 100644 --- a/corrupt.1 +++ b/corrupt.1 @@ -19,7 +19,14 @@ corrupt \- modify files by randomly changing bits .SH SYNOPSIS .BR corrupt +[\fB-n\fR \fIBITS\fR] +[\fB--bits\fR \fIBITS\fR] .IR file ... .SH DESCRIPTION .B corrupt modifies files by toggling a randomly chosen bit. +.SH OPTIONS +.TP +.BR -n " \fIBITS\fR, " --bits " \fIBITS\fR" +Set the number of bits to modify. +Default is one bit. -- cgit v1.2.1 From 7ddb4d64810f3e58b43798de7f2dc2bf67df59d5 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Tue, 3 Feb 2009 12:48:06 +0200 Subject: Added errno. --- errno | 16 ++++++++++++++++ errno.1 | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100755 errno create mode 100644 errno.1 diff --git a/errno b/errno new file mode 100755 index 0000000..2a39e2f --- /dev/null +++ b/errno @@ -0,0 +1,16 @@ +#!/usr/bin/python + +import errno +import os +import sys + +toname = dict((str(getattr(errno, x)), x) for x in dir(errno) if x.startswith("E")) +tocode = dict((x, getattr(errno, x)) for x in dir(errno) if x.startswith("E")) + +for arg in sys.argv[1:]: + if arg in tocode: + print arg, tocode[arg], os.strerror(tocode[arg]) + elif arg in toname: + print toname[arg], arg, os.strerror(int(arg)) + else: + print "Unknown:", arg diff --git a/errno.1 b/errno.1 new file mode 100644 index 0000000..aefb0ff --- /dev/null +++ b/errno.1 @@ -0,0 +1,51 @@ +.\" errno.1 - manual page for the errno 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 . +.\" +.TH ERRNO 1 +.SH NAME +errno \- look up errno codes or names +.SH SYNOPSIS +.B errno +.RI [ code | name ]... +.SH DESCRIPTION +.B errno +prints the description of +.BR errno (3) +values, from the command line. +It can look descriptions based on the numeric code or names like +.BR EEXIST . +.PP +For example, to look up what the code 2 means: +.sp 1 +.nf +.RS +$ errno 2 +ENOENT 2 No such file or directory +.RE +.fi +.sp 1 +Similarly, to look up what the code EEXIST means: +.sp 1 +.nf +.RS +$ errno EEXIST +EEXIST 17 File exists +.RE +.fi +.sp 1 +Note that the output always lists both the name and the code. +.SH "SEE ALSO" +.BR errno (3). -- cgit v1.2.1 From 5fef3f2ae3dd27eda5130a3adc9e68b32e828d48 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Tue, 3 Feb 2009 12:49:51 +0200 Subject: Reformat. --- errno | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/errno b/errno index 2a39e2f..d801faa 100755 --- a/errno +++ b/errno @@ -4,8 +4,12 @@ import errno import os import sys -toname = dict((str(getattr(errno, x)), x) for x in dir(errno) if x.startswith("E")) -tocode = dict((x, getattr(errno, x)) for x in dir(errno) if x.startswith("E")) +toname = dict((str(getattr(errno, x)), x) + for x in dir(errno) + if x.startswith("E")) +tocode = dict((x, getattr(errno, x)) + for x in dir(errno) + if x.startswith("E")) for arg in sys.argv[1:]: if arg in tocode: -- cgit v1.2.1 From a66aade1e9f2471aaa72b4c63b0ca2128e75d5ca Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Tue, 24 Feb 2009 13:50:05 +0200 Subject: Added file truncation script from Mikko Rauhala. --- trunc | 112 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 trunc diff --git a/trunc b/trunc new file mode 100644 index 0000000..6217614 --- /dev/null +++ b/trunc @@ -0,0 +1,112 @@ +#!/bin/sh + +# trunc v.1.0rc1 - Truncate a file to a given length +# +# Copyright Mikko Rauhala , 2009 +# +# This program is free software. It comes without any warranty, to +# the extent permitted by applicable law. You can redistribute it +# and/or modify it under the terms of the Do What The Fuck You Want +# To Public License, Version 2, as published by Sam Hocevar at +# http://sam.zoy.org/wtfpl/COPYING and reproduced here: +# +# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE +# Version 2, December 2004 +# +# Copyright (C) 2004 Sam Hocevar +# 14 rue de Plaisance, 75014 Paris, France +# Everyone is permitted to copy and distribute verbatim or modified +# copies of this license document, and changing it is allowed as long +# as the name is changed. +# +# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE +# TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION +# +# 0. You just DO WHAT THE FUCK YOU WANT TO. + +# README: + +usage() +{ +cat 1>&2 << EOF +Usage: trunc [-b blocksize] size file(s) + +Files will be truncated to the given size. Size is given in bytes +by default, but another block size may also be spesified. If a +file was originally larger than the given size, the rest of it +is lost. If it was smaller, the newly created part of the file +will be filled with zeroes (sparse if supported by the system). +If a file doesn't exist, an empty (sparse) file is created with +the given size and name. + +Size is given directly to dd as a count and the optional blocksize +as bs; refer to your system's dd documentation for possible special +formatting options. + +EOF +exit 0 +} + +# In practice, trunc is a small convenience wrapper for dd, which +# does the entire actual job. The dd recipe isn't all that complicated +# either, but hey, this is still more convenient and some of us find +# ourselves wanting to do this from time to time. + +# Technically, we rely on the fact that dd per default (without +# conv=notrunc) truncates the output file after completing its other +# tasks. We merely tell dd to seek the output file to the given +# position and write nothing. + +# History: + +# 1.0rc1 Initial release candidate. Will be named 1.0 later if +# no bugs are found. + + +# Make sure locale settings don't interfere. Might need to rethink and +# isolate this setting to smaller parts of the script if this ever gets +# localized, but for now, it's okay. (Probably unnecessary here anyway.) +LC_ALL=C +export LC_ALL + +RETVAL=0 + +ohnoes() +{ + echo "trunc: $1" 1>&2 + exit "$2" +} + +if ! which dd > /dev/null +then + ohnoes "dd not found (why, oh why?)" 3 +fi + +BS=1 +if [ "a$1" = "a-b" ]; then + if [ "a$2" = "a" ]; then + usage + else + BS="$2" + shift + shift + fi +fi + +if [ "a$1" = a -o "a$2" = a ]; then + usage +fi + +SIZE="$1" +shift +FILE="$1" + +while [ "a$FILE" != a ]; do + if ! ERROR="`dd if=/dev/null of="$FILE" bs="$BS" count=0 seek="$SIZE" 2>&1`"; then + ohnoes "$ERROR" 1 + fi + shift + FILE="$1" +done + +exit 0 -- cgit v1.2.1 From 8d95b516427eb23a89e7fea0da07dfaef22f7839 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Tue, 24 Feb 2009 13:52:03 +0200 Subject: Add execution bits to permissions. --- trunc | 0 1 file changed, 0 insertions(+), 0 deletions(-) mode change 100644 => 100755 trunc diff --git a/trunc b/trunc old mode 100644 new mode 100755 -- cgit v1.2.1 From fca0f1229335f599176866f64b03a2df442c773c Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Tue, 24 Feb 2009 14:49:36 +0200 Subject: Updated trunc version from Mikko Rauhala. --- trunc | 36 +++++++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 11 deletions(-) mode change 100755 => 100644 trunc diff --git a/trunc b/trunc old mode 100755 new mode 100644 index 6217614..51b62c4 --- a/trunc +++ b/trunc @@ -1,6 +1,6 @@ #!/bin/sh -# trunc v.1.0rc1 - Truncate a file to a given length +# trunc v.1.0 - Truncate a file to a given length # # Copyright Mikko Rauhala , 2009 # @@ -29,7 +29,8 @@ usage() { cat 1>&2 << EOF -Usage: trunc [-b blocksize] size file(s) +Usage: trunc [-b blocksize] size file(s) + trunc [--help|-h|--version|-v] Files will be truncated to the given size. Size is given in bytes by default, but another block size may also be spesified. If a @@ -57,31 +58,47 @@ exit 0 # tasks. We merely tell dd to seek the output file to the given # position and write nothing. +# There are some idiosyncracies in this script; most of them can be +# explained by me having started scripting first in the mid-90s and +# not bothering to properly find out which of the nicer constructs +# are bashims (which I want to avoid). Yeah, I'm a lazy bastard. + # History: +# 1.0 Initial release. Slightly cleaned up from rc1 though +# no actual bugs were found. While cleaning, added also +# -v and -h. # 1.0rc1 Initial release candidate. Will be named 1.0 later if # no bugs are found. # Make sure locale settings don't interfere. Might need to rethink and # isolate this setting to smaller parts of the script if this ever gets -# localized, but for now, it's okay. (Probably unnecessary here anyway.) +# localized, but for now, it's okay. (Probably unnecessary in this +# script anyway, I don't _think_ we're doing anything locale-spesific, +# but I've taken this habit to be sure. Doesn't hurt.) LC_ALL=C export LC_ALL -RETVAL=0 - ohnoes() { echo "trunc: $1" 1>&2 exit "$2" } -if ! which dd > /dev/null -then +if ! which dd > /dev/null; then ohnoes "dd not found (why, oh why?)" 3 fi +if [ "a$1" = "a-h" -o "a$1" = "a--help" ]; then + usage +fi + +if [ "a$1" = "a-v" -o "a$1" = "a--version" ]; then + echo "trunc 1.0 by Mikko Rauhala " 1>&2 + exit 0 +fi + BS=1 if [ "a$1" = "a-b" ]; then if [ "a$2" = "a" ]; then @@ -99,14 +116,11 @@ fi SIZE="$1" shift -FILE="$1" -while [ "a$FILE" != a ]; do +for FILE in "$@"; do if ! ERROR="`dd if=/dev/null of="$FILE" bs="$BS" count=0 seek="$SIZE" 2>&1`"; then ohnoes "$ERROR" 1 fi - shift - FILE="$1" done exit 0 -- cgit v1.2.1 From 3514265e115ea5b3bee143c614c46492842af4f2 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Thu, 23 Apr 2009 09:36:58 +0300 Subject: Wrote utility to create empty files. --- create-file | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100755 create-file 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 . + + +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() -- cgit v1.2.1 From 2811012b43d1d045358992add5af3b5c1b524e65 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Wed, 29 Apr 2009 18:13:01 +0300 Subject: Wrote manual page. --- create-file.1 | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 create-file.1 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 . +.\" +.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. -- cgit v1.2.1 From 25a34e1a16d0e8de5cdbc73bd5f0e57e9c1732bc Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Fri, 7 Aug 2009 22:12:22 +0300 Subject: Wrote manual page for minimify. --- minimify.1 | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 minimify.1 diff --git a/minimify.1 b/minimify.1 new file mode 100644 index 0000000..0e9a4b3 --- /dev/null +++ b/minimify.1 @@ -0,0 +1,33 @@ +.\" minimify.1 - manual page for the minimify 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 . +.\" +.TH MINIMIFY 1 +.SH NAME +minimify \- compress file to minimal size +.SH SYNOPSIS +.BR minimify +.IR file ... +.SH DESCRIPTION +.B minimify +compresses a file using different compression programs and picks the +smallest output. +Note that this can be extremely slow. +.PP +The following compression programs are supported: +.BR gzip , +.BR bzip2 , +and +.BR lzma . -- cgit v1.2.1 From 099a60a02d7c3f78282c74e2c09c567bbab06979 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Fri, 7 Aug 2009 22:32:55 +0300 Subject: Use multiprocessing to make use of all available processors. --- minimify | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100755 minimify diff --git a/minimify b/minimify new file mode 100755 index 0000000..1c05b47 --- /dev/null +++ b/minimify @@ -0,0 +1,67 @@ +#!/usr/bin/python +# +# minimify -- compress file to smallest size +# 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 . + + +import multiprocessing +import optparse +import os +import subprocess +import tempfile + + +COMPRESSORS = ( + ('gzip', '.gz'), + ('bzip2', '.bz2'), + ('lzma', '.lzma'), +) + + +def parse_args(): + parser = optparse.OptionParser() + options, filenames = parser.parse_args() + return options, filenames + + +def run_compressor(t): + compressor, filename, suffix, options = t + input_f = file(filename) + fd, name = tempfile.mkstemp(dir=os.path.dirname(filename)) + p = subprocess.Popen([compressor], stdin=input_f, stdout=fd) + p.communicate('') + os.close(fd) + if p.returncode: + raise Exception('Compression program %s failed' % p.returncode) + os.rename(name, filename + suffix) + + +def compress(filename, options): + pool = multiprocessing.Pool() + pool.map(run_compressor, + [(compressor, filename, suffix, options) + for compressor, suffix in COMPRESSORS]) + + +def main(): + options, filenames = parse_args() + for filename in filenames: + compress(filename, options) + + + +if __name__ == "__main__": + main() -- cgit v1.2.1 From 9085df7df610c9ecd7a6661d28a9551fd9c13131 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Fri, 7 Aug 2009 22:37:08 +0300 Subject: Keep only the smallest output file. --- minimify | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/minimify b/minimify index 1c05b47..e0a1f9c 100755 --- a/minimify +++ b/minimify @@ -50,17 +50,22 @@ def run_compressor(t): def compress(filename, options): + args = [(compressor, filename, suffix, options) + for compressor, suffix in COMPRESSORS] pool = multiprocessing.Pool() - pool.map(run_compressor, - [(compressor, filename, suffix, options) - for compressor, suffix in COMPRESSORS]) - + pool.map(run_compressor, args) + sizes = [(os.path.getsize(filename + suffix), filename + suffix) + for compressor, filename, suffix, options in args] + sizes.sort() + for size, pathname in sizes[1:]: + os.remove(pathname) + return sizes[0] def main(): options, filenames = parse_args() for filename in filenames: - compress(filename, options) - + size, name = compress(filename, options) + print size, name if __name__ == "__main__": -- cgit v1.2.1 From d3aad85411aa2835730719d4d70262218b787c02 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Fri, 7 Aug 2009 22:38:22 +0300 Subject: Simplify a bit. --- minimify | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/minimify b/minimify index e0a1f9c..e6830b9 100755 --- a/minimify +++ b/minimify @@ -47,16 +47,14 @@ def run_compressor(t): if p.returncode: raise Exception('Compression program %s failed' % p.returncode) os.rename(name, filename + suffix) + return os.path.getsize(filename + suffix), filename + suffix def compress(filename, options): args = [(compressor, filename, suffix, options) for compressor, suffix in COMPRESSORS] pool = multiprocessing.Pool() - pool.map(run_compressor, args) - sizes = [(os.path.getsize(filename + suffix), filename + suffix) - for compressor, filename, suffix, options in args] - sizes.sort() + sizes = sorted(pool.map(run_compressor, args)) for size, pathname in sizes[1:]: os.remove(pathname) return sizes[0] -- cgit v1.2.1 From fa7ad45397845730f6c623830f98196101d815f4 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Fri, 7 Aug 2009 22:39:27 +0300 Subject: Added missing empty line for formatting. --- minimify | 1 + 1 file changed, 1 insertion(+) diff --git a/minimify b/minimify index e6830b9..c4985cf 100755 --- a/minimify +++ b/minimify @@ -59,6 +59,7 @@ def compress(filename, options): os.remove(pathname) return sizes[0] + def main(): options, filenames = parse_args() for filename in filenames: -- cgit v1.2.1 From c9ecc35c1556cadc943d9a6482b692c697eaa505 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sat, 10 Oct 2009 11:48:09 +0300 Subject: Wrote a simplistic onerror script. --- onerror | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100755 onerror diff --git a/onerror b/onerror new file mode 100755 index 0000000..3e5a927 --- /dev/null +++ b/onerror @@ -0,0 +1,12 @@ +#!/bin/sh + +temp=$(mktemp) +"$@" > "$temp" 2>&1 +ret=$? +if [ $ret != 0 ] +then + cat "$temp" 1>&2 +fi +rm -f "$temp" +exit $ret + -- cgit v1.2.1