2 * Copyright (C) 2002 Andrew Tridgell
3 * Copyright (C) 2009-2014 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)
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, '\\');
702 return x_strdup(".");
707 * Return the file extension (including the dot) of a path as a pointer into
708 * path. If path has no file extension, the empty string and the end of path is
712 get_extension(const char *path)
714 size_t len = strlen(path);
717 for (p = &path[len - 1]; p >= path; --p) {
729 * Return a string containing the given path without the filename extension.
733 remove_extension(const char *path)
735 return x_strndup(path, strlen(path) - strlen(get_extension(path)));
738 /* return size on disk of a file */
740 file_size(struct stat *st)
743 return (st->st_size + 1023) & ~1023;
745 size_t size = st->st_blocks * 512;
746 if ((size_t)st->st_size > size) {
747 /* probably a broken stat() call ... */
748 size = (st->st_size + 1023) & ~1023;
754 /* a safe open/create for read-write */
756 safe_open(const char *fname)
758 int fd = open(fname, O_RDWR|O_BINARY);
759 if (fd == -1 && errno == ENOENT) {
760 fd = open(fname, O_RDWR|O_CREAT|O_EXCL|O_BINARY, 0666);
761 if (fd == -1 && errno == EEXIST) {
762 fd = open(fname, O_RDWR|O_BINARY);
768 /* Format a size (in KiB) as a human-readable string. Caller frees. */
770 format_size(size_t v)
773 if (v >= 1024*1024) {
774 s = format("%.1f Gbytes", v/((double)(1024*1024)));
775 } else if (v >= 1024) {
776 s = format("%.1f Mbytes", v/((double)(1024)));
778 s = format("%.0f Kbytes", (double)v);
783 /* return a value in multiples of 1024 give a string that can end
787 value_units(const char *s)
812 path_max(const char *path)
817 #elif defined(MAXPATHLEN)
820 #elif defined(_PC_PATH_MAX)
821 long maxlen = pathconf(path, _PC_PATH_MAX);
822 if (maxlen >= 4096) {
831 a sane realpath() function, trying to cope with stupid path limits and
835 x_realpath(const char *path)
837 long maxlen = path_max(path);
840 ret = x_malloc(maxlen);
843 p = realpath(path, ret);
845 /* yes, there are such systems. This replacement relies on
846 the fact that when we call x_realpath we only care about symlinks */
848 int len = readlink(path, ret, maxlen-1);
867 /* a getcwd that will returns an allocated buffer */
874 char *buffer = (char *)x_malloc(size);
875 if (getcwd(buffer, size) == buffer) {
879 if (errno != ERANGE) {
880 cc_log("getcwd error: %d (%s)", errno, strerror(errno));
887 /* create an empty file */
889 create_empty_file(const char *fname)
893 fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC|O_EXCL|O_BINARY, 0666);
902 * Return current user's home directory, or NULL if it can't be determined.
905 get_home_directory(void)
907 const char *p = getenv("HOME");
913 struct passwd *pwd = getpwuid(getuid());
923 * Get the current directory by reading $PWD. If $PWD isn't sane, gnu_getcwd()
924 * is used. Caller frees.
942 if (stat(pwd, &st_pwd) != 0) {
945 if (stat(cwd, &st_cwd) != 0) {
948 if (st_pwd.st_dev == st_cwd.st_dev && st_pwd.st_ino == st_cwd.st_ino) {
950 return x_strdup(pwd);
957 * Check whether s1 and s2 have the same executable name.
960 same_executable_name(const char *s1, const char *s2)
963 bool eq = strcasecmp(s1, s2) == 0;
965 char *tmp = format("%s.exe", s2);
966 eq = strcasecmp(s1, tmp) == 0;
971 return str_eq(s1, s2);
976 * Compute the length of the longest directory path that is common to two
977 * paths. s1 is assumed to be the path to a directory.
980 common_dir_prefix_length(const char *s1, const char *s2)
985 while (*p1 && *p2 && *p1 == *p2) {
989 while ((*p1 && *p1 != '/') || (*p2 && *p2 != '/')) {
993 if (!*p1 && !*p2 && p2 == s2 + 1) {
994 /* Special case for s1 and s2 both being "/". */
1001 * Compute a relative path from from (an absolute path to a directory) to to (a
1002 * path). Assumes that both from and to are well-formed and canonical. Caller
1006 get_relative_path(const char *from, const char *to)
1008 size_t common_prefix_len;
1013 assert(from && from[0] == '/');
1016 if (!*to || *to != '/') {
1017 return x_strdup(to);
1020 result = x_strdup("");
1021 common_prefix_len = common_dir_prefix_length(from, to);
1022 if (common_prefix_len > 0 || !str_eq(from, "/")) {
1023 for (p = from + common_prefix_len; *p; p++) {
1025 x_asprintf2(&result, "../%s", result);
1029 if (strlen(to) > common_prefix_len) {
1030 x_asprintf2(&result, "%s%s", result, to + common_prefix_len + 1);
1032 i = strlen(result) - 1;
1033 while (i >= 0 && result[i] == '/') {
1037 if (str_eq(result, "")) {
1039 result = x_strdup(".");
1045 * Return whether path is absolute.
1048 is_absolute_path(const char *path)
1051 return path[0] && path[1] == ':';
1053 return path[0] == '/';
1058 * Return whether the argument is a full path.
1061 is_full_path(const char *path)
1063 if (strchr(path, '/'))
1066 if (strchr(path, '\\'))
1073 * Update the modification time of a file in the cache to save it from LRU
1077 update_mtime(const char *path)
1087 * Rename oldpath to newpath (deleting newpath).
1090 x_rename(const char *oldpath, const char *newpath)
1093 /* Windows' rename() refuses to overwrite an existing file. */
1094 unlink(newpath); /* not x_unlink, as x_unlink calls x_rename */
1096 return rename(oldpath, newpath);
1100 * Remove path, NFS hazardous. Use only for temporary files that will not exist
1101 * on other systems. That is, the path should include tmp_string().
1104 tmp_unlink(const char *path)
1106 cc_log("Unlink %s (as-tmp)", path);
1107 return unlink(path);
1111 * Remove path, NFS safe.
1114 x_unlink(const char *path)
1117 * If path is on an NFS share, unlink isn't atomic, so we rename to a temp
1118 * file. We don't care if the temp file is trashed, so it's always safe to
1121 char *tmp_name = format("%s.tmp.rm.%s", path, tmp_string());
1123 cc_log("Unlink %s via %s", path, tmp_name);
1124 if (x_rename(path, tmp_name) == -1) {
1128 if (unlink(tmp_name) == -1) {
1137 /* Like readlink() but returns the string or NULL on failure. Caller frees. */
1139 x_readlink(const char *path)
1141 long maxlen = path_max(path);
1144 if (maxlen < 4096) maxlen = 4096;
1146 buf = x_malloc(maxlen);
1147 len = readlink(path, buf, maxlen-1);
1158 * Reads the content of a file. Size hint 0 means no hint. Returns true on
1159 * success, otherwise false.
1162 read_file(const char *path, size_t size_hint, char **data, size_t *size)
1165 size_t pos = 0, allocated;
1167 if (size_hint == 0) {
1169 if (stat(path, &st) == 0) {
1170 size_hint = st.st_size;
1173 size_hint = (size_hint < 1024) ? 1024 : size_hint;
1175 fd = open(path, O_RDONLY);
1179 allocated = size_hint;
1180 *data = x_malloc(allocated);
1182 if (pos > allocated / 2) {
1184 *data = x_realloc(*data, allocated);
1186 ret = read(fd, *data + pos, allocated - pos);
1187 if (ret == 0 || (ret == -1 && errno != EINTR)) {
1196 cc_log("Failed reading %s", path);
1207 * Return the content (with NUL termination) of a text file, or NULL on error.
1211 read_text_file(const char *path)
1216 if (read_file(path, 0, &data, &size)) {
1217 data = x_realloc(data, size + 1);