/*
- * Copyright (C) 2010-2014 Joel Rosdahl
+ * Copyright (C) 2010-2015 Joel Rosdahl
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
bool
lockfile_acquire(const char *path, unsigned staleness_limit)
{
+ int saved_errno = 0;
char *lockfile = format("%s.lock", path);
char *my_content = NULL, *content = NULL, *initial_content = NULL;
const char *hostname = get_hostname();
#ifdef _WIN32
fd = open(lockfile, O_WRONLY|O_CREAT|O_EXCL|O_BINARY, 0666);
if (fd == -1) {
+ saved_errno = errno;
cc_log("lockfile_acquire: open WRONLY %s: %s", lockfile, strerror(errno));
- if (errno == ENOENT) {
+ if (saved_errno == ENOENT) {
/* Directory doesn't exist? */
if (create_parent_dirs(lockfile) == 0) {
/* OK. Retry. */
continue;
}
}
- if (errno != EEXIST) {
+ if (saved_errno != EEXIST) {
/* Directory doesn't exist or isn't writable? */
goto out;
}
acquired = true;
goto out;
}
- cc_log("lockfile_acquire: symlink %s: %s", lockfile, strerror(errno));
- if (errno == ENOENT) {
+ saved_errno = errno;
+ cc_log("lockfile_acquire: symlink %s: %s", lockfile, strerror(saved_errno));
+ if (saved_errno == ENOENT) {
/* Directory doesn't exist? */
if (create_parent_dirs(lockfile) == 0) {
/* OK. Retry. */
continue;
}
}
- if (errno == EPERM) {
+ if (saved_errno == EPERM) {
/*
* The file system does not support symbolic links. We have no choice but
* to grant the lock anyway.
acquired = true;
goto out;
}
- if (errno != EEXIST) {
+ if (saved_errno != EEXIST) {
/* Directory doesn't exist or isn't writable? */
goto out;
}
/*
* Copy src to dest, decompressing src if needed. compress_level > 0 decides
- * whether dest will be compressed, and with which compression level.
+ * whether dest will be compressed, and with which compression level. Returns 0
+ * on success and -1 on failure. On failure, errno represents the error.
*/
int
copy_file(const char *src, const char *dest, int compress_level)
char *tmp_name;
struct stat st;
int errnum;
+ int saved_errno = 0;
/* open destination file */
tmp_name = x_strdup(dest);
/* open source file */
fd_in = open(src, O_RDONLY | O_BINARY);
if (fd_in == -1) {
- cc_log("open error: %s", strerror(errno));
+ saved_errno = errno;
+ cc_log("open error: %s", strerror(saved_errno));
goto error;
}
gz_in = gzdopen(fd_in, "rb");
if (!gz_in) {
- cc_log("gzdopen(src) error: %s", strerror(errno));
+ saved_errno = errno;
+ cc_log("gzdopen(src) error: %s", strerror(saved_errno));
close(fd_in);
goto error;
}
if (compress_level > 0) {
gz_out = gzdopen(dup(fd_out), "wb");
if (!gz_out) {
- cc_log("gzdopen(dest) error: %s", strerror(errno));
+ saved_errno = errno;
+ cc_log("gzdopen(dest) error: %s", strerror(saved_errno));
goto error;
}
gzsetparams(gz_out, compress_level, Z_DEFAULT_STRATEGY);
do {
count = write(fd_out, buf + written, n - written);
if (count == -1 && errno != EINTR) {
+ saved_errno = errno;
break;
}
written += count;
if (compress_level > 0) {
cc_log("gzwrite error: %s (errno: %s)",
gzerror(gz_in, &errnum),
- strerror(errno));
+ strerror(saved_errno));
} else {
- cc_log("write error: %s", strerror(errno));
+ cc_log("write error: %s", strerror(saved_errno));
}
goto error;
}
*/
gzerror(gz_in, &errnum);
if (!gzeof(gz_in) || (errnum != Z_OK && errnum != Z_STREAM_END)) {
+ saved_errno = errno;
cc_log("gzread error: %s (errno: %s)",
- gzerror(gz_in, &errnum), strerror(errno));
+ gzerror(gz_in, &errnum), strerror(saved_errno));
gzclose(gz_in);
if (gz_out) {
gzclose(gz_out);
/* the close can fail on NFS if out of space */
if (close(fd_out) == -1) {
- cc_log("close error: %s", strerror(errno));
+ saved_errno = errno;
+ cc_log("close error: %s", strerror(saved_errno));
goto error;
}
if (x_rename(tmp_name, dest) == -1) {
- cc_log("rename error: %s", strerror(errno));
+ saved_errno = errno;
+ cc_log("rename error: %s", strerror(saved_errno));
goto error;
}
}
tmp_unlink(tmp_name);
free(tmp_name);
+ errno = saved_errno;
return -1;
}
*/
char *tmp_name = format("%s.rm.%s", path, tmp_string());
int result = 0;
- int saved_errno;
+ int saved_errno = 0;
cc_log("Unlink %s via %s", path, tmp_name);
if (x_rename(path, tmp_name) == -1) {
result = -1;
if (result) {
cc_log("x_unlink failed: %s", strerror(saved_errno));
}
+ errno = saved_errno;
return result;
}