static bool enable_direct = true;
/*
- * Whether to enable compression of files stored in the cache. (Manifest files
- * are always compressed.)
+ * If non-zero, enables compression of files stored in the cache, compressed
+ * at the given level. (Manifest files are always compressed.)
*/
-static bool enable_compression = false;
+static int enable_compression = 0;
/* number of levels (1 <= nlevels <= 8) */
static int nlevels = 2;
if (getenv("CCACHE_COMPRESS")) {
cc_log("Compression enabled");
- enable_compression = true;
+ if (getenv("CCACHE_COMPRESS_LEVEL")) {
+ enable_compression = atoi(getenv("CCACHE_COMPRESS_LEVEL"));
+ } else {
+ enable_compression = 6;
+ }
}
if ((env = getenv("CCACHE_NLEVELS"))) {
void cc_log_argv(const char *prefix, char **argv);
void fatal(const char *format, ...) ATTR_FORMAT(printf, 1, 2);
void copy_fd(int fd_in, int fd_out);
-int copy_file(const char *src, const char *dest, int compress_dest);
-int move_file(const char *src, const char *dest, int compress_dest);
+int copy_file(const char *src, const char *dest, int compress_level);
+int move_file(const char *src, const char *dest, int compress_level);
int move_uncompressed_file(const char *src, const char *dest,
- int compress_dest);
+ int compress_level);
bool file_is_compressed(const char *filename);
int create_dir(const char *dir);
int create_parent_dirs(const char *path);
#endif
/*
- * Copy src to dest, decompressing src if needed. compress_dest decides whether
- * dest will be compressed.
+ * Copy src to dest, decompressing src if needed. compress_level > 0 decides whether
+ * dest will be compressed, and with which compression level.
*/
int
-copy_file(const char *src, const char *dest, int compress_dest)
+copy_file(const char *src, const char *dest, int compress_level)
{
int fd_in = -1, fd_out = -1;
gzFile gz_in = NULL, gz_out = NULL;
tmp_name = format("%s.%s.XXXXXX", dest, tmp_string());
cc_log("Copying %s to %s via %s (%s)",
- src, dest, tmp_name, compress_dest ? "compressed": "uncompressed");
+ src, dest, tmp_name, compress_level ? "compressed": "uncompressed");
/* open source file */
fd_in = open(src, O_RDONLY | O_BINARY);
goto error;
}
- if (compress_dest) {
+ if (compress_level) {
/*
* A gzip file occupies at least 20 bytes, so it will always
* occupy an entire filesystem block, even for empty files.
goto error;
}
if (file_size(&st) == 0) {
- compress_dest = 0;
+ compress_level = 0;
}
}
- if (compress_dest) {
+ if (compress_level) {
gz_out = gzdopen(dup(fd_out), "wb");
if (!gz_out) {
cc_log("gzdopen(dest) error: %s", strerror(errno));
goto error;
}
+ gzsetparams(gz_out, compress_level, Z_DEFAULT_STRATEGY);
}
while ((n = gzread(gz_in, buf, sizeof(buf))) > 0) {
- if (compress_dest) {
+ if (compress_level) {
written = gzwrite(gz_out, buf, n);
} else {
ssize_t count;
} while (written < n);
}
if (written != n) {
- if (compress_dest) {
+ if (compress_level) {
cc_log("gzwrite error: %s (errno: %s)",
gzerror(gz_in, &errnum),
strerror(errno));
/* Run copy_file() and, if successful, delete the source file. */
int
-move_file(const char *src, const char *dest, int compress_dest)
+move_file(const char *src, const char *dest, int compress_level)
{
int ret;
- ret = copy_file(src, dest, compress_dest);
+ ret = copy_file(src, dest, compress_level);
if (ret != -1) {
x_unlink(src);
}
* are on the same file system.
*/
int
-move_uncompressed_file(const char *src, const char *dest, int compress_dest)
+move_uncompressed_file(const char *src, const char *dest, int compress_level)
{
- if (compress_dest) {
- return move_file(src, dest, compress_dest);
+ if (compress_level) {
+ return move_file(src, dest, compress_level);
} else {
return x_rename(src, dest);
}