2 * Copyright (C) 2002 Andrew Tridgell
3 * Copyright (C) 2009-2013 Joel Rosdahl
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the Free
7 * Software Foundation; either version 3 of the License, or (at your option)
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 #ifdef HAVE_SYS_TIME_H
33 #include <sys/locking.h>
41 extern char *cache_logfile;
49 logfile = fopen(cache_logfile, "a");
51 int fd = fileno(logfile);
52 int flags = fcntl(fd, F_GETFD, 0);
54 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
65 #ifdef HAVE_GETTIMEOFDAY
70 gettimeofday(&tv, NULL);
71 #ifdef __MINGW64_VERSION_MAJOR
72 tm = _localtime32(&tv.tv_sec);
74 tm = localtime(&tv.tv_sec);
76 strftime(timestamp, sizeof(timestamp), "%Y-%m-%dT%H:%M:%S", tm);
77 fprintf(logfile, "[%s.%06d %-5d] ", timestamp, (int)tv.tv_usec,
80 fprintf(logfile, "[%-5d] ", (int)getpid());
85 * Write a message to the CCACHE_LOGFILE location (adding a newline).
88 cc_log(const char *format, ...)
98 vfprintf(logfile, format, ap);
100 fprintf(logfile, "\n");
105 * Log an executed command to the CCACHE_LOGFILE location.
108 cc_log_argv(const char *prefix, char **argv)
115 fputs(prefix, logfile);
116 print_command(logfile, argv);
120 /* something went badly wrong! */
122 fatal(const char *format, ...)
127 va_start(ap, format);
128 vsnprintf(msg, sizeof(msg), format, ap);
131 cc_log("FATAL: %s", msg);
132 fprintf(stderr, "ccache: FATAL: %s\n", msg);
138 * Copy all data from fd_in to fd_out, decompressing data from fd_in if needed.
141 copy_fd(int fd_in, int fd_out)
147 gz_in = gzdopen(dup(fd_in), "rb");
150 fatal("Failed to copy fd");
153 while ((n = gzread(gz_in, buf, sizeof(buf))) > 0) {
154 ssize_t count, written = 0;
156 count = write(fd_out, buf + written, n - written);
158 if (errno != EAGAIN && errno != EINTR) {
159 fatal("Failed to copy fd");
164 } while (written < n);
171 /* cheap and nasty mkstemp replacement */
173 mkstemp(char *template)
176 return open(template, O_RDWR | O_CREAT | O_EXCL | O_BINARY, 0600);
181 * Copy src to dest, decompressing src if needed. compress_dest decides whether
182 * dest will be compressed.
185 copy_file(const char *src, const char *dest, int compress_dest)
187 int fd_in = -1, fd_out = -1;
188 gzFile gz_in = NULL, gz_out = NULL;
198 tmp_name = format("%s.%s.XXXXXX", dest, tmp_string());
199 cc_log("Copying %s to %s via %s (%s)",
200 src, dest, tmp_name, compress_dest ? "compressed": "uncompressed");
202 /* open source file */
203 fd_in = open(src, O_RDONLY | O_BINARY);
205 cc_log("open error: %s", strerror(errno));
209 gz_in = gzdopen(fd_in, "rb");
211 cc_log("gzdopen(src) error: %s", strerror(errno));
216 /* open destination file */
217 fd_out = mkstemp(tmp_name);
219 cc_log("mkstemp error: %s", strerror(errno));
225 * A gzip file occupies at least 20 bytes, so it will always
226 * occupy an entire filesystem block, even for empty files.
227 * Turn off compression for empty files to save some space.
229 if (fstat(fd_in, &st) != 0) {
230 cc_log("fstat error: %s", strerror(errno));
233 if (file_size(&st) == 0) {
239 gz_out = gzdopen(dup(fd_out), "wb");
241 cc_log("gzdopen(dest) error: %s", strerror(errno));
246 while ((n = gzread(gz_in, buf, sizeof(buf))) > 0) {
248 written = gzwrite(gz_out, buf, n);
253 count = write(fd_out, buf + written, n - written);
254 if (count == -1 && errno != EINTR) {
258 } while (written < n);
262 cc_log("gzwrite error: %s (errno: %s)",
263 gzerror(gz_in, &errnum),
266 cc_log("write error: %s", strerror(errno));
273 * gzeof won't tell if there's an error in the trailing CRC, so we must check
274 * gzerror before considering everything OK.
276 gzerror(gz_in, &errnum);
277 if (!gzeof(gz_in) || (errnum != Z_OK && errnum != Z_STREAM_END)) {
278 cc_log("gzread error: %s (errno: %s)",
279 gzerror(gz_in, &errnum), strerror(errno));
285 tmp_unlink(tmp_name);
298 /* get perms right on the tmp file */
300 fchmod(fd_out, 0666 & ~mask);
304 /* the close can fail on NFS if out of space */
305 if (close(fd_out) == -1) {
306 cc_log("close error: %s", strerror(errno));
310 if (x_rename(tmp_name, dest) == -1) {
311 cc_log("rename error: %s", strerror(errno));
329 tmp_unlink(tmp_name);
334 /* Run copy_file() and, if successful, delete the source file. */
336 move_file(const char *src, const char *dest, int compress_dest)
340 ret = copy_file(src, dest, compress_dest);
348 * Like move_file(), but assumes that src is uncompressed and that src and dest
349 * are on the same file system.
352 move_uncompressed_file(const char *src, const char *dest, int compress_dest)
355 return move_file(src, dest, compress_dest);
357 return x_rename(src, dest);
361 /* test if a file is zlib compressed */
363 file_is_compressed(const char *filename)
367 f = fopen(filename, "rb");
372 /* test if file starts with 1F8B, which is zlib's
374 if ((fgetc(f) != 0x1f) || (fgetc(f) != 0x8b)) {
383 /* make sure a directory exists */
385 create_dir(const char *dir)
388 if (stat(dir, &st) == 0) {
389 if (S_ISDIR(st.st_mode)) {
395 if (mkdir(dir, 0777) != 0 && errno != EEXIST) {
402 * Return a static string with the current hostname.
407 static char hostname[200] = "";
410 strcpy(hostname, "unknown");
412 gethostname(hostname, sizeof(hostname)-1);
414 hostname[sizeof(hostname)-1] = 0;
421 * Return a string to be used to distinguish temporary files. Also tries to
422 * cope with NFS by adding the local hostname.
430 ret = format("%s.%u", get_hostname(), (unsigned)getpid());
436 /* Return the hash result as a hex string. Caller frees. */
438 format_hash_as_string(const unsigned char *hash, unsigned size)
444 for (i = 0; i < 16; i++) {
445 sprintf(&ret[i*2], "%02x", (unsigned) hash[i]);
447 sprintf(&ret[i*2], "-%u", size);
452 char const CACHEDIR_TAG[] =
453 "Signature: 8a477f597d28d172789f06886806bc55\n"
454 "# This file is a cache directory tag created by ccache.\n"
455 "# For information about cache directory tags, see:\n"
456 "# http://www.brynosaurus.com/cachedir/\n";
459 create_cachedirtag(const char *dir)
463 char *filename = format("%s/CACHEDIR.TAG", dir);
464 if (stat(filename, &st) == 0) {
465 if (S_ISREG(st.st_mode)) {
471 f = fopen(filename, "w");
473 if (fwrite(CACHEDIR_TAG, sizeof(CACHEDIR_TAG)-1, 1, f) != 1) {
477 if (fclose(f)) goto error;
486 /* Construct a string according to a format. Caller frees. */
488 format(const char *format, ...)
493 va_start(ap, format);
494 if (vasprintf(&ptr, format, ap) == -1) {
495 fatal("Out of memory in format");
499 if (!*ptr) fatal("Internal error in format");
504 this is like strdup() but dies if the malloc fails
507 x_strdup(const char *s)
512 fatal("Out of memory in x_strdup");
518 this is like strndup() but dies if the malloc fails
521 x_strndup(const char *s, size_t n)
530 while (m < n && s[m]) {
542 fatal("x_strndup: Could not allocate %lu bytes", (unsigned long)n);
548 this is like malloc() but dies if the malloc fails
551 x_malloc(size_t size)
556 * malloc() may return NULL if size is zero, so always do this to make sure
557 * that the code handles it regardless of platform.
563 fatal("x_malloc: Could not allocate %lu bytes", (unsigned long)size);
568 /* This is like calloc() but dies if the allocation fails. */
570 x_calloc(size_t nmemb, size_t size)
573 if (nmemb * size == 0) {
575 * calloc() may return NULL if nmemb or size is 0, so always do this to
576 * make sure that the code handles it regardless of platform.
580 ret = calloc(nmemb, size);
582 fatal("x_calloc: Could not allocate %lu bytes", (unsigned long)size);
588 this is like realloc() but dies if the malloc fails
591 x_realloc(void *ptr, size_t size)
594 if (!ptr) return x_malloc(size);
595 p2 = realloc(ptr, size);
597 fatal("x_realloc: Could not allocate %lu bytes", (unsigned long)size);
604 * This is like x_asprintf() but frees *ptr if *ptr != NULL.
607 x_asprintf2(char **ptr, const char *format, ...)
613 va_start(ap, format);
614 if (vasprintf(ptr, format, ap) == -1) {
615 fatal("Out of memory in x_asprintf2");
619 if (!ptr) fatal("Out of memory in x_asprintf2");
626 * Recursive directory traversal. fn() is called on all entries in the tree.
629 traverse(const char *dir, void (*fn)(const char *, struct stat *))
637 while ((de = readdir(d))) {
641 if (str_eq(de->d_name, ".")) continue;
642 if (str_eq(de->d_name, "..")) continue;
644 if (strlen(de->d_name) == 0) continue;
646 fname = format("%s/%s", dir, de->d_name);
647 if (lstat(fname, &st)) {
648 if (errno != ENOENT) {
655 if (S_ISDIR(st.st_mode)) {
667 /* return the base name of a file - caller frees */
669 basename(const char *s)
675 p = strrchr(s, '\\');
682 /* return the dir name of a file - caller frees */
691 p2 = strrchr(s, '\\');
700 return x_strdup(".");
705 * Return the file extension (including the dot) of a path as a pointer into
706 * path. If path has no file extension, the empty string and the end of path is
710 get_extension(const char *path)
712 size_t len = strlen(path);
715 for (p = &path[len - 1]; p >= path; --p) {
727 * Return a string containing the given path without the filename extension.
731 remove_extension(const char *path)
733 return x_strndup(path, strlen(path) - strlen(get_extension(path)));
736 /* return size on disk of a file */
738 file_size(struct stat *st)
741 return (st->st_size + 1023) & ~1023;
743 size_t size = st->st_blocks * 512;
744 if ((size_t)st->st_size > size) {
745 /* probably a broken stat() call ... */
746 size = (st->st_size + 1023) & ~1023;
752 /* a safe open/create for read-write */
754 safe_open(const char *fname)
756 int fd = open(fname, O_RDWR|O_BINARY);
757 if (fd == -1 && errno == ENOENT) {
758 fd = open(fname, O_RDWR|O_CREAT|O_EXCL|O_BINARY, 0666);
759 if (fd == -1 && errno == EEXIST) {
760 fd = open(fname, O_RDWR|O_BINARY);
766 /* Format a size (in KiB) as a human-readable string. Caller frees. */
768 format_size(size_t v)
771 if (v >= 1024*1024) {
772 s = format("%.1f Gbytes", v/((double)(1024*1024)));
773 } else if (v >= 1024) {
774 s = format("%.1f Mbytes", v/((double)(1024)));
776 s = format("%.0f Kbytes", (double)v);
781 /* return a value in multiples of 1024 give a string that can end
785 value_units(const char *s)
810 path_max(const char *path)
815 #elif defined(MAXPATHLEN)
818 #elif defined(_PC_PATH_MAX)
819 long maxlen = pathconf(path, _PC_PATH_MAX);
820 if (maxlen >= 4096) {
829 a sane realpath() function, trying to cope with stupid path limits and
833 x_realpath(const char *path)
835 long maxlen = path_max(path);
838 ret = x_malloc(maxlen);
841 p = realpath(path, ret);
843 /* yes, there are such systems. This replacement relies on
844 the fact that when we call x_realpath we only care about symlinks */
846 int len = readlink(path, ret, maxlen-1);
865 /* a getcwd that will returns an allocated buffer */
872 char *buffer = (char *)x_malloc(size);
873 if (getcwd(buffer, size) == buffer) {
877 if (errno != ERANGE) {
878 cc_log("getcwd error: %d (%s)", errno, strerror(errno));
885 /* create an empty file */
887 create_empty_file(const char *fname)
891 fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC|O_EXCL|O_BINARY, 0666);
900 * Return current user's home directory, or NULL if it can't be determined.
903 get_home_directory(void)
905 const char *p = getenv("HOME");
911 struct passwd *pwd = getpwuid(getuid());
921 * Get the current directory by reading $PWD. If $PWD isn't sane, gnu_getcwd()
922 * is used. Caller frees.
940 if (stat(pwd, &st_pwd) != 0) {
943 if (stat(cwd, &st_cwd) != 0) {
946 if (st_pwd.st_dev == st_cwd.st_dev && st_pwd.st_ino == st_cwd.st_ino) {
948 return x_strdup(pwd);
955 * Check whether s1 and s2 have the same executable name.
958 same_executable_name(const char *s1, const char *s2)
961 bool eq = strcasecmp(s1, s2) == 0;
963 char *tmp = format("%s.exe", s2);
964 eq = strcasecmp(s1, tmp) == 0;
969 return str_eq(s1, s2);
974 * Compute the length of the longest directory path that is common to two
975 * paths. s1 is assumed to be the path to a directory.
978 common_dir_prefix_length(const char *s1, const char *s2)
983 while (*p1 && *p2 && *p1 == *p2) {
988 /* s2 starts with "s1/". */
992 /* s2 is equal to s1. */
994 /* Special case for s1 and s2 both being "/". */
1000 /* Compute the common directory prefix */
1001 while (p1 > s1 && *p1 != '/') {
1009 * Compute a relative path from from (an absolute path to a directory) to to (a
1010 * path). Assumes that both from and to are well-formed and canonical. Caller
1014 get_relative_path(const char *from, const char *to)
1016 size_t common_prefix_len;
1021 assert(from && from[0] == '/');
1024 if (!*to || *to != '/') {
1025 return x_strdup(to);
1028 result = x_strdup("");
1029 common_prefix_len = common_dir_prefix_length(from, to);
1030 if (common_prefix_len > 0 || !str_eq(from, "/")) {
1031 for (p = from + common_prefix_len; *p; p++) {
1033 x_asprintf2(&result, "../%s", result);
1037 if (strlen(to) > common_prefix_len) {
1038 x_asprintf2(&result, "%s%s", result, to + common_prefix_len + 1);
1040 i = strlen(result) - 1;
1041 while (i >= 0 && result[i] == '/') {
1045 if (str_eq(result, "")) {
1047 result = x_strdup(".");
1053 * Return whether path is absolute.
1056 is_absolute_path(const char *path)
1059 return path[0] && path[1] == ':';
1061 return path[0] == '/';
1066 * Return whether the argument is a full path.
1069 is_full_path(const char *path)
1071 if (strchr(path, '/'))
1074 if (strchr(path, '\\'))
1081 * Update the modification time of a file in the cache to save it from LRU
1085 update_mtime(const char *path)
1095 * Rename oldpath to newpath (deleting newpath).
1098 x_rename(const char *oldpath, const char *newpath)
1101 /* Windows' rename() refuses to overwrite an existing file. */
1102 unlink(newpath); /* not x_unlink, as x_unlink calls x_rename */
1104 return rename(oldpath, newpath);
1108 * Remove path, NFS hazardous. Use only for temporary files that will not exist
1109 * on other systems. That is, the path should include tmp_string().
1112 tmp_unlink(const char *path)
1114 cc_log("Unlink %s (as-tmp)", path);
1115 return unlink(path);
1119 * Remove path, NFS safe.
1122 x_unlink(const char *path)
1125 * If path is on an NFS share, unlink isn't atomic, so we rename to a temp
1126 * file. We don't care if the temp file is trashed, so it's always safe to
1129 char *tmp_name = format("%s.tmp.rm.%s", path, tmp_string());
1131 cc_log("Unlink %s via %s", path, tmp_name);
1132 if (x_rename(path, tmp_name) == -1) {
1136 if (unlink(tmp_name) == -1) {
1145 /* Like readlink() but returns the string or NULL on failure. Caller frees. */
1147 x_readlink(const char *path)
1149 long maxlen = path_max(path);
1154 #elif defined(MAXPATHLEN)
1155 maxlen = MAXPATHLEN;
1156 #elif defined(_PC_PATH_MAX)
1157 maxlen = pathconf(path, _PC_PATH_MAX);
1159 if (maxlen < 4096) maxlen = 4096;
1161 buf = x_malloc(maxlen);
1162 len = readlink(path, buf, maxlen-1);
1173 * Reads the content of a file. Size hint 0 means no hint. Returns true on
1174 * success, otherwise false.
1177 read_file(const char *path, size_t size_hint, char **data, size_t *size)
1180 size_t pos = 0, allocated;
1182 if (size_hint == 0) {
1184 if (stat(path, &st) == 0) {
1185 size_hint = st.st_size;
1188 size_hint = (size_hint < 1024) ? 1024 : size_hint;
1190 fd = open(path, O_RDONLY);
1194 allocated = size_hint;
1195 *data = x_malloc(allocated);
1198 if (pos > allocated / 2) {
1200 *data = x_realloc(*data, allocated);
1202 ret = read(fd, *data + pos, allocated - pos);
1203 if (ret == 0 || (ret == -1 && errno != EINTR)) {
1212 cc_log("Failed reading %s", path);
1223 * Return the content (with NUL termination) of a text file, or NULL on error.
1227 read_text_file(const char *path)
1232 if (read_file(path, 0, &data, &size)) {
1233 data = x_realloc(data, size + 1);