static void fr_command_tar_delete (FRCommand *comm, GList *file_list) { GList *scan; gchar *new_filename, *new_filename2; gboolean name_modified; new_filename = uncompress (comm, &name_modified); /* Delete files. */ fr_process_begin_command (comm->process, "tar"); fr_process_add_arg (comm->process, "--delete"); fr_process_add_arg (comm->process, "-f"); fr_process_add_arg (comm->process, new_filename); for (scan = file_list; scan; scan = scan->next) fr_process_add_arg (comm->process, scan->data); fr_process_end_command (comm->process); new_filename2 = recompress (comm, new_filename); /* Restore original name. */ if (name_modified) { fr_process_begin_command (comm->process, "mv"); fr_process_add_arg (comm->process, "-f"); fr_process_add_arg (comm->process, new_filename2); fr_process_add_arg (comm->process, comm->filename); fr_process_end_command (comm->process); } g_free(new_filename); g_free(new_filename2); }
static void fr_command_tar_add (FRCommand *comm, GList *file_list, gchar *base_dir, gboolean update) { GList *scan; gchar *new_filename, *new_filename2; gboolean name_modified; new_filename = uncompress (comm, &name_modified); /* Add files. */ fr_process_begin_command (comm->process, "tar"); if (base_dir != NULL) { fr_process_add_arg (comm->process, "-C"); fr_process_add_arg (comm->process, base_dir); } if (update) fr_process_add_arg (comm->process, "-uf"); else fr_process_add_arg (comm->process, "-rf"); fr_process_add_arg (comm->process, new_filename); for (scan = file_list; scan; scan = scan->next) fr_process_add_arg (comm->process, scan->data); fr_process_end_command (comm->process); new_filename2 = recompress (comm, new_filename); /* Restore original name. */ if (name_modified) { fr_process_begin_command (comm->process, "mv"); fr_process_add_arg (comm->process, "-f"); fr_process_add_arg (comm->process, new_filename2); fr_process_add_arg (comm->process, comm->filename); fr_process_end_command (comm->process); } g_free(new_filename); g_free(new_filename2); }
/* compress from stdin to fixed-size block on stdout */ int main (int argc, char **argv) { int ret; /* return code */ unsigned size; /* requested fixed output block size */ unsigned have; /* bytes written by deflate() call */ unsigned char *blk; /* intermediate and final stream */ unsigned char *tmp; /* close to desired size stream */ z_stream def, inf; /* zlib deflate and inflate states */ /* get requested output size */ if (argc != 2) quit ("need one argument: size of output block"); ret = strtol (argv[1], argv + 1, 10); if (argv[1][0] != 0) quit ("argument must be a number"); if (ret < 8) /* 8 is minimum zlib stream size */ quit ("need positive size of 8 or greater"); size = (unsigned) ret; /* allocate memory for buffers and compression engine */ blk = malloc (size + EXCESS); def.zalloc = Z_NULL; def.zfree = Z_NULL; def.opaque = Z_NULL; ret = deflateInit (&def, Z_DEFAULT_COMPRESSION); if (ret != Z_OK || blk == NULL) quit ("out of memory"); /* compress from stdin until output full, or no more input */ def.avail_out = size + EXCESS; def.next_out = blk; ret = partcompress (stdin, &def); if (ret == Z_ERRNO) quit ("error reading input"); /* if it all fit, then size was undersubscribed -- done! */ if (ret == Z_STREAM_END && def.avail_out >= EXCESS) { /* write block to stdout */ have = size + EXCESS - def.avail_out; if (fwrite (blk, 1, have, stdout) != have || ferror (stdout)) quit ("error writing output"); /* clean up and print results to stderr */ ret = deflateEnd (&def); assert (ret != Z_STREAM_ERROR); free (blk); fprintf (stderr, "%u bytes unused out of %u requested (all input)\n", size - have, size); return 0; } /* it didn't all fit -- set up for recompression */ inf.zalloc = Z_NULL; inf.zfree = Z_NULL; inf.opaque = Z_NULL; inf.avail_in = 0; inf.next_in = Z_NULL; ret = inflateInit (&inf); tmp = malloc (size + EXCESS); if (ret != Z_OK || tmp == NULL) quit ("out of memory"); ret = deflateReset (&def); assert (ret != Z_STREAM_ERROR); /* do first recompression close to the right amount */ inf.avail_in = size + EXCESS; inf.next_in = blk; def.avail_out = size + EXCESS; def.next_out = tmp; ret = recompress (&inf, &def); if (ret == Z_MEM_ERROR) quit ("out of memory"); /* set up for next reocmpression */ ret = inflateReset (&inf); assert (ret != Z_STREAM_ERROR); ret = deflateReset (&def); assert (ret != Z_STREAM_ERROR); /* do second and final recompression (third compression) */ inf.avail_in = size - MARGIN; /* assure stream will complete */ inf.next_in = tmp; def.avail_out = size; def.next_out = blk; ret = recompress (&inf, &def); if (ret == Z_MEM_ERROR) quit ("out of memory"); assert (ret == Z_STREAM_END); /* otherwise MARGIN too small */ /* done -- write block to stdout */ have = size - def.avail_out; if (fwrite (blk, 1, have, stdout) != have || ferror (stdout)) quit ("error writing output"); /* clean up and print results to stderr */ free (tmp); ret = inflateEnd (&inf); assert (ret != Z_STREAM_ERROR); ret = deflateEnd (&def); assert (ret != Z_STREAM_ERROR); free (blk); fprintf (stderr, "%u bytes unused out of %u requested (%lu input)\n", size - have, size, def.total_in); return 0; }