File: compfile.c

package info (click to toggle)
afio 2.5.1.20160103+gitc8e4317-1
  • links: PTS, VCS
  • area: non-free
  • in suites: bullseye, buster, sid, stretch
  • size: 864 kB
  • ctags: 647
  • sloc: ansic: 4,903; sh: 139; makefile: 83; awk: 19
file content (403 lines) | stat: -rw-r--r-- 9,805 bytes parent folder | download
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
391
392
393
394
395
396
397
398
399
400
401
402
403
/* afio file compresssion code */

#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>

#include <limits.h>
#include <stdlib.h>
#include <sys/signal.h>
#include <sys/types.h>
#include <sys/stat.h>

#include "patchlevel.h"

#include "afio.h"

int matchcompext(char *);
void meminit(void);
void memwrite(char *buf, int count);
int setupgzip(char *name);

int zipfdfd;

/* default value vor gzip 1.2.3 */
int gzipfactor=6;

/* compress all files? */
int forceZflag = 0;

/* this can be set to use something else but gzip: */
char *compressprog = NULL;
int compressargs = 0;


/* max. (virtual) memory usage for -Z option */
off_t maxmem=250*1024*1024;

/* files whose length is below this length won't be compressed */
long compthreshold=0;


/* stuff to build argument list for compression/decompression : */

#define	MAX_ARGS  101

char 	* compress_arg_list[MAX_ARGS+1];
/* MAX_ARGS+1 because we need to leave room for a NULL to mark the end */
int	compress_arg_no = 1;

void	add_arg(char *arg)
{
    if((compress_arg_no == 1)&&(*arg=='\0'))
    {
	/* special case: -Q "" means no arguments. */
	return;
    }

    if(compress_arg_no < MAX_ARGS )
	compress_arg_list[compress_arg_no++] = arg;
    else
    {
	fprintf (stderr, "afio: Fatal: maximium number of -Q arguments exceeded.\n");
	exit(1);
    }

}

/*
 * meminit, memwrite, memreset, memread, memfree.
 */

int memerror;
char *membank=NULL;
size_t allocsize=0;
size_t memsize,membytesread,membytesleft;

void meminit(void)
{
 memerror=0;
 memsize=0;
 /* RWWH: Initialize only the first time */
 if (!allocsize) {
   allocsize=100000;
   membank=(char *)malloc(allocsize);
   if(membank==NULL)
   {
       warn_nocount("Memory","Low on virtual memory, zipping twice instead.");
       memerror=1;
       allocsize=0;
   }
 }
}

void memwrite(char *buf, int count)
{
 char *oldbank;

 if(memerror) return;

 oldbank=membank;
 /* RWWH: don't increase by linear amount! grow exponentially! */
 if (memsize+count>=allocsize) {
   allocsize=1.25*(memsize+count);
#if 0 
   fprintf(stderr,"%d allocsize=%u\n",sizeof(size_t), allocsize);
#endif
   if(allocsize/1024L/1024L > 2000) 
     {
       /* safety: to avoid possible errors due to use of 32 bit int
          and size_t types here and there, prevent membank going over 2GB if
	  compression programm actually expands filesize. 
	  Note that -M is capped at 1.5GB anyway.
       */
       membank=NULL; /* simulate realloc running out of memory */
     }
   else
     {
       membank=(char *)realloc(membank,allocsize);
     }
   if(membank==NULL)
    {
      warn_nocount("Memory","Low on virtual memory, zipping twice instead.");
      /* The free() both works and is necessary.  I can't recall what
         manpage documents this. */
      free(oldbank);
      memerror=1;
      allocsize=0;
      return;
    }
 }

 memcpy(membank+memsize,buf,(size_t)count);
 memsize+=count;
}

void memreset()
{
 membytesread=0;
 membytesleft=memsize;
}

int memread(char *buf,int count)
{
 if(membytesleft==0) return 0;

 if ((size_t)count > membytesleft) count=membytesleft;
 memcpy(buf,membank+membytesread,(size_t)count);
 membytesread+=count;
 membytesleft-=count;
 return count;
}

void memfree()
{
 /* We do not free anymore, this should speed things up. */
#if 0
 if(memerror==0) free(membank);
 allocsize=0;
#endif
}


/*******/

/*
 * Fork a gzip zipping the file name. The zipped version is output through a
 * pipe, the pipe handle is returned.
 * Returns -1 on failure.
 */
int comppid;

int setupgzip(char *name)
{
 int pipedes[2];
 char farg[3];

 farg[0]='-';
 farg[1]=gzipfactor+'0';
 farg[2]='\0';

 if(pipe(pipedes)==-1) { perror("pipe"); return -1; }

 mayberewind();

 if ((comppid = xfork ("out(), compressing", NODIE)) == 0)
    {
      if (arfd != STDIN && arfd != STDOUT) VOID close (arfd);

      dup2(pipedes[1],fileno(stdout));
      close(pipedes[1]);
      close(pipedes[0]);

      VOID close (fileno (stdin));

      if (open (name, O_RDONLY) >= 0)
      {
	  if(! compressargs)
	      execlp (compressprog, compressprog, "-c", farg, NULL);
	  else
	      execvp (compressprog, compress_arg_list);
      }
      exit (1);
   }

 close(pipedes[1]);

 /* fprintf(stderr,"pid %d init, pipedes=%d.\n",comppid,pipedes[0]);  */

 return pipedes[0];
}

#include <sys/wait.h>
#include <sys/types.h>

void waitforgzip()
{
 /* fprintf(stderr,"wait for gzip %d.\n",comppid); */

 xwait (comppid, "out(), wait for gzip child", FALSE);
}

/*
 * compress "name" if we can
 * If we compress we change the statbuf size, the file name, close
 * the old pointer to the file and return the pointer to the compressed
 * version;
 */

#if ( defined(sun) && defined(__svr4__) ) || defined(__CYGWIN32__)
#include <dirent.h>
#else
#include <sys/dir.h>
#endif

void
compressfile (int *fdp, char *name, reg Stat *asb, int *cratio) {
  int compout;
  char *tmpcomp;
  Stat asb2;
  int zipfd;
  ssize_t len;
  char buf[40960];
  off_t ziplen;
  int usemem;

  if (cratio)
    *cratio = 100;

  /* We need to mark the file as being suitable or not for decompression on
     reading the archive. This is done by adding a .z extension.

     There is a problem with this: a file that is not suitable for
     compression may already have such an extension.
     Since this is a regular file, we can use the
     device number to indicate something weird going on here.

     We look at the bit (asb->sb_rdev)&RDEV_NOTCOMPR:
     If it is 1, it means that the file wasn't suitable for compression,
     and thus that any .z present was there on the original name.
     If it is 0, this does NOT imply that the file was compressed by afio.

     Note that it being 0 doesn't mean `decompress me', it merely
     indicates that a .z extender, _if_ _present_, was added by afio.
     (However, as of version 2.3.6, false does mean that the file was
     compressed by afio and a .z extender added.  In older archives one may
     see filenames with the bit 0 and not ending in .z. Such files
     have a compressed version larger than the original.)

     In pre-2.4.4 versions, files with size 0 do not have the bit
     set to 1.
  */

  /* indicate unsuitable for compression, may change it later */
   asb->sb_rdev |= RDEV_NOTCOMPR;

  /* Hard link handling code will break if we do compression on the
     file, so try to compress only if no links or hard link handling.
     Note that if forceZflag==1 than also lflag==1 was set, so this
     test will never return false.

     Also only try to compress if not already compressed,
     or length is >= threshold, or <= maxsizetocompress,
     but always try if we force compression
  */

  if ( (lflag || (asb->sb_nlink == 1))
       &&
       ( ( !matchcompext(name)
           && (asb->sb_size >= compthreshold)
           && ((maxsizetocompress==0) || ((ulonglong)(asb->sb_size) <= maxsizetocompress))
	   )
	 || forceZflag
       )
     )
  {
    /* make sure compress could put on the .Z */
    if ((tmpcomp = strrchr (name, '/')) != NULL)
      tmpcomp++;
    else
      tmpcomp = name;
#ifdef NAME_MAX
    if (strlen (tmpcomp) + 2 > NAME_MAX)
#else
#ifdef MAXNAMLEN	   /* BSD otherwise should be sysV (FFS on sysV?) */
    if (strlen (tmpcomp) + 2 > MAXNAMLEN)
#else
    if (strlen (tmpcomp) + 2 > DIRSIZ)
#endif /* MAXNAMLEN */
#endif /* NAME_MAX */
      {
#ifndef LONGZFILE
	VOID warn (name, " is too long to tack on .z");
	return;
#endif
      }

  /*  fprintf(stderr,"---nam: %s, len: %d ziplen: ",name,asb->sb_size); */

    usemem=1;

    if((zipfd=setupgzip(name))!=-1)
      {
        ziplen=0;
        if(usemem) meminit();
        while((len=read(zipfd,buf,sizeof(buf)))!=0)
          {
           if(len<0) {  fprintf(stderr,
                          "Trouble zipping file, storing uncompressed\n");
                        /* read error on pipe, do not use gzip on this file */
                        ziplen=0; break;
                     }
           if(usemem) memwrite(buf,len);

           ziplen+=len;
           /* too much memory used? */
           if(usemem && ziplen>maxmem)
	     { /* prepare to zip twice and free memory */
               usemem=0;
               if (!memerror) memfree();
               /* fprintf(stderr,"zipping twice."); */
	     }
	  }
        close(zipfd);

        /* wait for child to exit */
        if(xwait (comppid, "out(), wait for gzip child", FALSE)!= 0)
            {  fprintf(stderr,
                    "Trouble zipping file, storing uncompressed\n");
               ziplen=0;
            }

        if(memerror) usemem=0;

    /*   fprintf(stderr,"%ld\n",ziplen); */
        asb2.sb_size=ziplen;

	if (ziplen>0)
	  {
	    /* if compressed file is smaller than original, of if forceZflag
	       is set, use compressed data */
	    if (asb2.sb_size < asb->sb_size || forceZflag )
	      {
                if(usemem)
                  {
                    close (*fdp);
                    strcat (name, ".z");
                    /* was suitable for compression */
                    asb->sb_rdev &= ~RDEV_NOTCOMPR;

		    if (cratio)
		      *cratio = (int)((asb2.sb_size * 100.0) / asb->sb_size);
		    asb->sb_size = asb2.sb_size;
		    *fdp = MEMFD;
		  }
                else
		  if ((compout = setupgzip(name)) >= 0)
                    {
                      zipfdfd=compout;
                      compout=ZIPFD;

                      close (*fdp);
                      strcat (name, ".z");
                      /* was suitable for compression */
		      asb->sb_rdev &= ~RDEV_NOTCOMPR;

		      if (cratio)
			*cratio = (int)((asb2.sb_size * 100.0) / asb->sb_size);
		      asb->sb_size = asb2.sb_size;
		      *fdp = compout;
		    }
	      }
            else
              {
                if(usemem) memfree();
	      }
	  }
      }

  }
}