Logo Search packages:      
Sourcecode: tardy version File versions  Download package

void tar_output_filter_gzip::write_header ( const tar_header &   )  [virtual]

The write_header method is used to write file meta-data to the output stream. Must be called exactly once for each file. The size in the header shall exactly match the amount of data written with the write_data methods which follow.

Reimplemented from tar_output_filter.

Definition at line 73 of file gzip.cc.

References crc, drop_dead(), tar_output_filter::filename(), hdr, tar_output::nfatal(), outbuf, pass_through, stream, temp_filename, temp_fp, and tar_output_filter::write_header().

{
    if (arg.type != tar_header::type_normal)
    {
      pass_through = true;
      tar_output_filter::write_header(arg);
      return;
    }
    if (arg.size <= 1024)
    {
      // Don't bother with files which are too small to compress usefully.
      pass_through = true;
      tar_output_filter::write_header(arg);
      return;
    }
    pass_through = false;

    //
    // Remember the header for later use.
    //
    hdr = arg;

    //
    // Open a temporary file to hold the compressed file.
    //
    static char filename_template[] = "/tmp/tardy-XXXXXX";
    char filename[sizeof(filename_template)];
    strcpy(filename, filename_template);
    int fd = mkstemp(filename);
    if (fd < 0)
      nfatal("mkstemp \"%s\"", filename);
    temp_filename = filename;
    FILE *fp = fdopen(fd, "w+b");
    temp_fp = fp;

    //
    // Initialize the compression engine.
    //
    crc = crc32(0L, Z_NULL, 0);
    stream.avail_in = 0;
    stream.avail_out = 0;
    stream.next_in = NULL;
    stream.next_out = NULL;
    stream.opaque = (voidpf)0;
    stream.zalloc = (alloc_func)0;
    stream.zfree = (free_func)0;

    /*
     * Set the parameters for the compression.
     * Note: windowBits is passed < 0 to suppress zlib header.
     */
    int err =
      deflateInit2
      (
          &stream,
          Z_BEST_COMPRESSION, // level
          Z_DEFLATED,         // method
          -MAX_WBITS,           // windowBits
          DEF_MEM_LEVEL,      // memLevel
          Z_DEFAULT_STRATEGY  // strategy
      );
    if (err != Z_OK)
      drop_dead(err);

    if (!outbuf)
      outbuf = new Byte[Z_BUFSIZE];
    stream.next_out = outbuf;
    stream.avail_out = Z_BUFSIZE;

    /*
     * Write a very simple .gz header:
     */
    static unsigned char ghdr[] =
    {
      0x1F, 0x8B,  // gzip magic number
        Z_DEFLATED,  // method
      Z_FLAG_EXTRA_FIELD, // flags
      0, 0, 0, 0,  // time
      0,           // xflags
      3,           // always use unix OS_CODE
      4, 0,        // extra length
    };
    fwrite(ghdr, 1, sizeof(ghdr), fp);
    put_long(fp, hdr.size);
}


Generated by  Doxygen 1.6.0   Back to index