#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <time.h>
#include <sys/stat.h>
#include <sys/file.h>
#include "distcc.h"
#include "trace.h"
#include "util.h"
#include "exitcode.h"
#include "snprintf.h"
#include "lock.h"
#include "timefile.h"
int dcc_mark_timefile(const char *lockname,
const struct dcc_hostdef *host)
{
char *filename;
int fd;
int ret;
if ((ret = dcc_make_lock_filename(lockname, host, 0, &filename)))
return ret;
if ((ret = dcc_open_lockfile(filename, &fd))) {
free(filename);
return ret;
}
if (write(fd, "x", 1) != 1) {
rs_log_error("write to %s failed: %s", lockname, strerror(errno));
dcc_close(fd);
return EXIT_IO_ERROR;
}
dcc_close(fd);
rs_trace("mark %s", filename);
free(filename);
return 0;
}
int dcc_remove_timefile(const char *lockname,
const struct dcc_hostdef *host)
{
char *filename;
int ret = 0;
if ((ret = dcc_make_lock_filename(lockname, host, 0, &filename)))
return ret;
if (unlink(filename) == 0) {
rs_trace("remove %s", filename);
} else {
if (errno == ENOENT) {
} else {
rs_log_error("unlink %s failed: %s", filename, strerror(errno));
ret = EXIT_IO_ERROR;
}
}
free(filename);
return 0;
}
int dcc_check_timefile(const char *lockname,
const struct dcc_hostdef *host,
time_t *mtime)
{
char *filename;
struct stat sb;
int ret;
if ((ret = dcc_make_lock_filename(lockname, host, 0, &filename)))
return ret;
if (stat(filename, &sb) == -1) {
*mtime = (time_t) 0;
if (errno == ENOENT) {
free(filename);
return 0;
} else {
rs_log_error("stat %s failed: %s", filename, strerror(errno));
free(filename);
return EXIT_IO_ERROR;
}
}
*mtime = sb.st_mtime;
free(filename);
return 0;
}