#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <openssl/md5.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/dirent.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include "distcc.h"
#include "bulk.h"
#include "indirect_client.h"
#include "indirect_util.h"
#include "rpc.h"
#include "trace.h"
static int dcc_handle_remote_indirection_pull(int ifd)
{
unsigned path_length;
char path[MAXPATHLEN];
int result = 0;
unsigned file_stat_info_flag, pull_response;
off_t file_size, send_size;
struct timespec mod_time;
struct stat st_pullfile;
if ((result = dcc_r_token_int(ifd, indirection_path_length_token, &path_length)) != 0) {
rs_log_error("unable to fetch indirection path length");
}
if (result == 0) {
if ((result = dcc_readx(ifd, path, path_length)) != 0)
rs_log_error("unable to fetch indirection path");
path[path_length] = 0;
}
if (result == 0) {
if ((result = dcc_r_token_int(ifd, indirection_file_stat_token, &file_stat_info_flag)) != 0) {
rs_log_error("unable to fetch indirection file stat token");
} else {
if (file_stat_info_flag == indirection_file_stat_info_present) {
if (result == 0) result = dcc_readx(ifd, &file_size, sizeof(file_size));
if (result == 0) result = dcc_readx(ifd, &mod_time, sizeof(mod_time));
if (result != 0) rs_log_error("unable to fetch indirection file stat info");
}
}
}
if (result == 0) {
if (stat(path, &st_pullfile) != 0) {
result = -1;
pull_response = indirection_pull_response_file_missing;
rs_log_warning("Unable to send pull file: %s - %s", path, strerror(errno));
} else {
if (file_stat_info_flag == indirection_file_stat_info_present && st_pullfile.st_size == file_size && st_pullfile.st_mtimespec.tv_sec == mod_time.tv_sec && st_pullfile.st_mtimespec.tv_nsec == mod_time.tv_nsec) {
pull_response = indirection_pull_response_file_ok;
rs_log_info("using cached pull file");
} else {
if (file_stat_info_flag == indirection_file_stat_info_present) {
rs_log_info("pull file changed, sending");
rs_log_info("my size = %d, my seconds = %d, my nsec = %d\nhis size = %d, his seconds = %d, his nsec = %d", (int)st_pullfile.st_size, st_pullfile.st_mtimespec.tv_sec, st_pullfile.st_mtimespec.tv_nsec, (int)file_size, mod_time.tv_sec, mod_time.tv_nsec);
} else {
rs_log_info("pull file missing, sending");
}
pull_response = indirection_pull_response_file_download;
}
}
if ((result = dcc_x_token_int(ifd, indirection_pull_response_token, pull_response)) != 0) {
rs_log_error("unable to send pull response code");
} else {
if (pull_response == indirection_pull_response_file_download) {
if (dcc_x_file(ifd, path, indirection_pull_file, DCC_COMPRESS_LZO1X, &send_size) ||
dcc_writex(ifd, &st_pullfile.st_mtimespec, sizeof(st_pullfile.st_mtimespec))) {
rs_log_error("failure sending pull file");
}
}
}
} else {
}
return result;
}
int dcc_handle_remote_indirection_request(int ifd, int operation)
{
int result;
if ( operation == indirection_request_pull ) {
result = dcc_handle_remote_indirection_pull(ifd);
} else {
rs_log_error("Unsupported indirection operation: %d", operation);
result = -1;
}
return result;
}