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 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
|
/* $Id: shrinkfile.c 6135 2003-01-19 01:15:40Z rra $
**
** Shrink files on line boundaries.
**
** Written by Landon Curt Noll <chongo@toad.com>, and placed in the
** public domain. Rewritten for INN by Rich Salz.
**
** Usage:
** shrinkfile [-n] [-s size [-m maxsize]] [-v] file...
** -n No writes, exit 0 if any file is too large, 1 otherwise
** -s size Truncation size (0 default); suffix may be k, m,
** or g to scale. Must not be larger than 2^31 - 1.
** -m maxsize Maximum size allowed before truncation. If maxsize
** <= size, then it is reset to size. Default == size.
** -v Print status line.
**
** Files will be shrunk an end of line boundary. In no case will the
** file be longer than size bytes if it was longer than maxsize bytes.
** If the first line is longer than the absolute value of size, the file
** will be truncated to zero length.
**
** The -n flag may be used to determine of any file is too large. No
** files will be altered in this mode.
*/
#include "config.h"
#include "clibrary.h"
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
#include "inn/innconf.h"
#include "inn/messages.h"
#include "libinn.h"
#define MAX_SIZE 0x7fffffffUL
/*
** Open a safe unique temporary file that will go away when closed.
*/
static FILE *
OpenTemp(void)
{
FILE *F;
char *filename;
int fd;
filename = concatpath(innconf->pathtmp, "shrinkXXXXXX");
fd = mkstemp(filename);
if (fd < 0)
sysdie("cannot create temporary file");
F = fdopen(fd, "w+");
if (F == NULL)
sysdie("cannot fdopen %s", filename);
unlink(filename);
free(filename);
return F;
}
/*
** Does file end with \n? Assume it does on I/O error, to avoid doing I/O.
*/
static int
EndsWithNewline(FILE *F)
{
int c;
if (fseeko(F, 1, SEEK_END) < 0) {
syswarn("cannot seek to end of file");
return true;
}
/* return the actual character or EOF */
if ((c = fgetc(F)) == EOF) {
if (ferror(F))
syswarn("cannot read last byte");
return true;
}
return c == '\n';
}
/*
** Add a newline to location of a file.
*/
static bool
AppendNewline(char *name)
{
FILE *F;
if ((F = xfopena(name)) == NULL) {
syswarn("cannot add newline");
return false;
}
if (fputc('\n', F) == EOF
|| fflush(F) == EOF
|| ferror(F)
|| fclose(F) == EOF) {
syswarn("cannot add newline");
return false;
}
return true;
}
/*
** Just check if it is too big
*/
static bool
TooBig(FILE *F, off_t maxsize)
{
struct stat Sb;
/* Get the file's size. */
if (fstat((int)fileno(F), &Sb) < 0) {
syswarn("cannot fstat");
return false;
}
/* return true if too large */
return (maxsize > Sb.st_size ? false : true);
}
/*
** This routine does all the work.
*/
static bool
Process(FILE *F, char *name, off_t size, off_t maxsize, bool *Changedp)
{
off_t len;
FILE *tmp;
struct stat Sb;
char buff[BUFSIZ + 1];
int c;
size_t i;
bool err;
/* Get the file's size. */
if (fstat((int)fileno(F), &Sb) < 0) {
syswarn("cannot fstat");
return false;
}
len = Sb.st_size;
/* Process a zero size request. */
if (size == 0 && len > maxsize) {
if (len > 0) {
fclose(F);
if ((F = fopen(name, "w")) == NULL) {
syswarn("cannot overwrite");
return false;
}
fclose(F);
*Changedp = true;
}
return true;
}
/* See if already small enough. */
if (len <= maxsize) {
/* Newline already present? */
if (EndsWithNewline(F)) {
fclose(F);
return true;
}
/* No newline, add it if it fits. */
if (len < size - 1) {
fclose(F);
*Changedp = true;
return AppendNewline(name);
}
}
else if (!EndsWithNewline(F)) {
if (!AppendNewline(name)) {
fclose(F);
return false;
}
}
/* We now have a file that ends with a newline that is bigger than
* we want. Starting from {size} bytes from end, move forward
* until we get a newline. */
if (fseeko(F, -size, SEEK_END) < 0) {
syswarn("cannot fseeko");
fclose(F);
return false;
}
while ((c = getc(F)) != '\n')
if (c == EOF) {
syswarn("cannot read");
fclose(F);
return false;
}
/* Copy rest of file to temp. */
tmp = OpenTemp();
err = false;
while ((i = fread(buff, 1, sizeof buff, F)) > 0)
if (fwrite(buff, 1, i, tmp) != i) {
err = true;
break;
}
if (err) {
syswarn("cannot copy to temporary file");
fclose(F);
fclose(tmp);
return false;
}
/* Now copy temp back to original file. */
fclose(F);
if ((F = fopen(name, "w")) == NULL) {
syswarn("cannot overwrite file");
fclose(tmp);
return false;
}
fseeko(tmp, 0, SEEK_SET);
while ((i = fread(buff, 1, sizeof buff, tmp)) > 0)
if (fwrite(buff, 1, i, F) != i) {
err = true;
break;
}
if (err) {
syswarn("cannot overwrite file");
fclose(F);
fclose(tmp);
return false;
}
fclose(F);
fclose(tmp);
*Changedp = true;
return true;
}
/*
** Convert size argument to numeric value. Return -1 on error.
*/
static off_t
ParseSize(char *p)
{
off_t scale;
unsigned long str_num;
char *q;
/* Skip leading spaces */
while (ISWHITE(*p))
p++;
if (*p == '\0')
return -1;
/* determine the scaling factor */
q = &p[strlen(p) - 1];
switch (*q) {
default:
return -1;
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
scale = 1;
break;
case 'k': case 'K':
scale = 1024;
*q = '\0';
break;
case 'm': case 'M':
scale = 1024 * 1024;
*q = '\0';
break;
case 'g': case 'G':
scale = 1024 * 1024 * 1024;
*q = '\0';
break;
}
/* Convert string to number. */
if (sscanf(p, "%lud", &str_num) != 1)
return -1;
if (str_num > MAX_SIZE / scale)
die("size is too big");
return scale * str_num;
}
/*
** Print usage message and exit.
*/
static void
Usage(void)
{
fprintf(stderr,
"Usage: shrinkfile [-n] [ -m maxsize ] [-s size] [-v] file...");
exit(1);
}
int
main(int ac, char *av[])
{
bool Changed;
bool Verbose;
bool no_op;
FILE *F;
char *p;
int i;
off_t size = 0;
off_t maxsize = 0;
/* First thing, set up our identity. */
message_program_name = "shrinkfile";
/* Set defaults. */
Verbose = false;
no_op = false;
umask(NEWSUMASK);
if (!innconf_read(NULL))
exit(1);
/* Parse JCL. */
while ((i = getopt(ac, av, "m:s:vn")) != EOF)
switch (i) {
default:
Usage();
/* NOTREACHED */
case 'n':
no_op = true;
break;
case 'm':
if ((maxsize = ParseSize(optarg)) < 0)
Usage();
break;
case 's':
if ((size = ParseSize(optarg)) < 0)
Usage();
break;
case 'v':
Verbose = true;
break;
}
if (maxsize < size) {
maxsize = size;
}
ac -= optind;
av += optind;
if (ac == 0)
Usage();
while ((p = *av++) != NULL) {
if ((F = fopen(p, "r")) == NULL) {
syswarn("cannot open %s", p);
continue;
}
/* -n (no_op) or normal processing */
if (no_op) {
/* check if too big and exit zero if it is */
if (TooBig(F, maxsize)) {
if (Verbose)
notice("%s is too large", p);
exit(0);
/* NOTREACHED */
}
/* no -n, do some real work */
} else {
Changed = false;
if (!Process(F, p, size, maxsize, &Changed))
syswarn("cannot shrink %s", p);
else if (Verbose && Changed)
notice("shrunk %s", p);
}
}
if (no_op && Verbose) {
notice("did not find a file that was too large");
}
/* if -n, then exit non-zero to indicate no file too big */
exit(no_op ? 1 : 0);
/* NOTREACHED */
}
|