2 * Copyright (C) 2002 Andrew Tridgell
3 * Copyright (C) 2009-2011 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");
60 #ifdef HAVE_GETTIMEOFDAY
65 gettimeofday(&tv, NULL);
66 #ifdef __MINGW64_VERSION_MAJOR
67 tm = _localtime32(&tv.tv_sec);
69 tm = localtime(&tv.tv_sec);
71 strftime(timestamp, sizeof(timestamp), "%Y-%m-%dT%H:%M:%S", tm);
72 fprintf(logfile, "[%s.%06d %-5d] ", timestamp, (int)tv.tv_usec,
75 fprintf(logfile, "[%-5d] ", (int)getpid());
80 * Write a message to the CCACHE_LOGFILE location (adding a newline).
83 cc_log(const char *format, ...)
93 vfprintf(logfile, format, ap);
95 fprintf(logfile, "\n");
100 * Log an executed command to the CCACHE_LOGFILE location.
103 cc_log_argv(const char *prefix, char **argv)
110 fputs(prefix, logfile);
111 print_command(logfile, argv);
115 /* something went badly wrong! */
117 fatal(const char *format, ...)
122 va_start(ap, format);
123 vsnprintf(msg, sizeof(msg), format, ap);
126 cc_log("FATAL: %s", msg);
127 fprintf(stderr, "ccache: FATAL: %s\n", msg);
133 * Copy all data from fd_in to fd_out, decompressing data from fd_in if needed.
136 copy_fd(int fd_in, int fd_out)
142 gz_in = gzdopen(dup(fd_in), "rb");
145 fatal("Failed to copy fd");
148 while ((n = gzread(gz_in, buf, sizeof(buf))) > 0) {
149 ssize_t count, written = 0;
151 count = write(fd_out, buf + written, n - written);
152 if (count == -1 && errno != EINTR) {
153 fatal("Failed to copy fd");
156 } while (written < n);
163 /* cheap and nasty mkstemp replacement */
165 mkstemp(char *template)
168 return open(template, O_RDWR | O_CREAT | O_EXCL | O_BINARY, 0600);
173 * Copy src to dest, decompressing src if needed. compress_dest decides whether
174 * dest will be compressed.
177 copy_file(const char *src, const char *dest, int compress_dest)
179 int fd_in = -1, fd_out = -1;
180 gzFile gz_in = NULL, gz_out = NULL;
190 tmp_name = format("%s.%s.XXXXXX", dest, tmp_string());
191 cc_log("Copying %s to %s via %s (%s)",
192 src, dest, tmp_name, compress_dest ? "compressed": "uncompressed");
194 /* open source file */
195 fd_in = open(src, O_RDONLY | O_BINARY);
197 cc_log("open error: %s", strerror(errno));
201 gz_in = gzdopen(fd_in, "rb");
203 cc_log("gzdopen(src) error: %s", strerror(errno));
208 /* open destination file */
209 fd_out = mkstemp(tmp_name);
211 cc_log("mkstemp error: %s", strerror(errno));
217 * A gzip file occupies at least 20 bytes, so it will always
218 * occupy an entire filesystem block, even for empty files.
219 * Turn off compression for empty files to save some space.
221 if (fstat(fd_in, &st) != 0) {
222 cc_log("fstat error: %s", strerror(errno));
225 if (file_size(&st) == 0) {
231 gz_out = gzdopen(dup(fd_out), "wb");
233 cc_log("gzdopen(dest) error: %s", strerror(errno));
238 while ((n = gzread(gz_in, buf, sizeof(buf))) > 0) {
240 written = gzwrite(gz_out, buf, n);
245 count = write(fd_out, buf + written, n - written);
246 if (count == -1 && errno != EINTR) {
250 } while (written < n);
254 cc_log("gzwrite error: %s (errno: %s)",
255 gzerror(gz_in, &errnum),
258 cc_log("write error: %s", strerror(errno));
265 * gzeof won't tell if there's an error in the trailing CRC, so we must check
266 * gzerror before considering everything OK.
268 gzerror(gz_in, &errnum);
269 if (!gzeof(gz_in) || (errnum != Z_OK && errnum != Z_STREAM_END)) {
270 cc_log("gzread error: %s (errno: %s)",
271 gzerror(gz_in, &errnum), strerror(errno));
277 tmp_unlink(tmp_name);
290 /* get perms right on the tmp file */
292 fchmod(fd_out, 0666 & ~mask);
296 /* the close can fail on NFS if out of space */
297 if (close(fd_out) == -1) {
298 cc_log("close error: %s", strerror(errno));
302 if (x_rename(tmp_name, dest) == -1) {
303 cc_log("rename error: %s", strerror(errno));
321 tmp_unlink(tmp_name);
326 /* Run copy_file() and, if successful, delete the source file. */
328 move_file(const char *src, const char *dest, int compress_dest)
332 ret = copy_file(src, dest, compress_dest);
340 * Like move_file(), but assumes that src is uncompressed and that src and dest
341 * are on the same file system.
344 move_uncompressed_file(const char *src, const char *dest, int compress_dest)
347 return move_file(src, dest, compress_dest);
349 return x_rename(src, dest);
353 /* test if a file is zlib compressed */
355 file_is_compressed(const char *filename)
359 f = fopen(filename, "rb");
364 /* test if file starts with 1F8B, which is zlib's
366 if ((fgetc(f) != 0x1f) || (fgetc(f) != 0x8b)) {
375 /* make sure a directory exists */
377 create_dir(const char *dir)
380 if (stat(dir, &st) == 0) {
381 if (S_ISDIR(st.st_mode)) {
387 if (mkdir(dir, 0777) != 0 && errno != EEXIST) {
394 * Return a static string with the current hostname.
399 static char hostname[200] = "";
402 strcpy(hostname, "unknown");
404 gethostname(hostname, sizeof(hostname)-1);
406 hostname[sizeof(hostname)-1] = 0;
413 * Return a string to be used to distinguish temporary files. Also tries to
414 * cope with NFS by adding the local hostname.
422 ret = format("%s.%u", get_hostname(), (unsigned)getpid());
428 /* Return the hash result as a hex string. Caller frees. */
430 format_hash_as_string(const unsigned char *hash, unsigned size)
436 for (i = 0; i < 16; i++) {
437 sprintf(&ret[i*2], "%02x", (unsigned) hash[i]);
439 sprintf(&ret[i*2], "-%u", size);
444 char const CACHEDIR_TAG[] =
445 "Signature: 8a477f597d28d172789f06886806bc55\n"
446 "# This file is a cache directory tag created by ccache.\n"
447 "# For information about cache directory tags, see:\n"
448 "# http://www.brynosaurus.com/cachedir/\n";
451 create_cachedirtag(const char *dir)
455 char *filename = format("%s/CACHEDIR.TAG", dir);
456 if (stat(filename, &st) == 0) {
457 if (S_ISREG(st.st_mode)) {
463 f = fopen(filename, "w");
465 if (fwrite(CACHEDIR_TAG, sizeof(CACHEDIR_TAG)-1, 1, f) != 1) {
469 if (fclose(f)) goto error;
478 /* Construct a string according to a format. Caller frees. */
480 format(const char *format, ...)
485 va_start(ap, format);
486 if (vasprintf(&ptr, format, ap) == -1) {
487 fatal("Out of memory in format");
491 if (!*ptr) fatal("Internal error in format");
496 this is like strdup() but dies if the malloc fails
499 x_strdup(const char *s)
504 fatal("Out of memory in x_strdup");
510 this is like strndup() but dies if the malloc fails
513 x_strndup(const char *s, size_t n)
522 while (m < n && s[m]) {
534 fatal("x_strndup: Could not allocate %lu bytes", (unsigned long)n);
540 this is like malloc() but dies if the malloc fails
543 x_malloc(size_t size)
548 * malloc() may return NULL if size is zero, so always do this to make sure
549 * that the code handles it regardless of platform.
555 fatal("x_malloc: Could not allocate %lu bytes", (unsigned long)size);
560 /* This is like calloc() but dies if the allocation fails. */
562 x_calloc(size_t nmemb, size_t size)
565 if (nmemb * size == 0) {
567 * calloc() may return NULL if nmemb or size is 0, so always do this to
568 * make sure that the code handles it regardless of platform.
572 ret = calloc(nmemb, size);
574 fatal("x_calloc: Could not allocate %lu bytes", (unsigned long)size);
580 this is like realloc() but dies if the malloc fails
583 x_realloc(void *ptr, size_t size)
586 if (!ptr) return x_malloc(size);
587 p2 = realloc(ptr, size);
589 fatal("x_realloc: Could not allocate %lu bytes", (unsigned long)size);
596 * This is like x_asprintf() but frees *ptr if *ptr != NULL.
599 x_asprintf2(char **ptr, const char *format, ...)
605 va_start(ap, format);
606 if (vasprintf(ptr, format, ap) == -1) {
607 fatal("Out of memory in x_asprintf2");
611 if (!ptr) fatal("Out of memory in x_asprintf2");
618 * Recursive directory traversal. fn() is called on all entries in the tree.
621 traverse(const char *dir, void (*fn)(const char *, struct stat *))
629 while ((de = readdir(d))) {
633 if (str_eq(de->d_name, ".")) continue;
634 if (str_eq(de->d_name, "..")) continue;
636 if (strlen(de->d_name) == 0) continue;
638 fname = format("%s/%s", dir, de->d_name);
639 if (lstat(fname, &st)) {
640 if (errno != ENOENT) {
647 if (S_ISDIR(st.st_mode)) {
659 /* return the base name of a file - caller frees */
661 basename(const char *s)
667 p = strrchr(s, '\\');
674 /* return the dir name of a file - caller frees */
683 p2 = strrchr(s, '\\');
692 return x_strdup(".");
697 * Return the file extension (including the dot) of a path as a pointer into
698 * path. If path has no file extension, the empty string and the end of path is
702 get_extension(const char *path)
704 size_t len = strlen(path);
707 for (p = &path[len - 1]; p >= path; --p) {
719 * Return a string containing the given path without the filename extension.
723 remove_extension(const char *path)
725 return x_strndup(path, strlen(path) - strlen(get_extension(path)));
728 /* return size on disk of a file */
730 file_size(struct stat *st)
733 return (st->st_size + 1023) & ~1023;
735 size_t size = st->st_blocks * 512;
736 if ((size_t)st->st_size > size) {
737 /* probably a broken stat() call ... */
738 size = (st->st_size + 1023) & ~1023;
744 /* a safe open/create for read-write */
746 safe_open(const char *fname)
748 int fd = open(fname, O_RDWR|O_BINARY);
749 if (fd == -1 && errno == ENOENT) {
750 fd = open(fname, O_RDWR|O_CREAT|O_EXCL|O_BINARY, 0666);
751 if (fd == -1 && errno == EEXIST) {
752 fd = open(fname, O_RDWR|O_BINARY);
758 /* Format a size (in KiB) as a human-readable string. Caller frees. */
760 format_size(size_t v)
763 if (v >= 1024*1024) {
764 s = format("%.1f Gbytes", v/((double)(1024*1024)));
765 } else if (v >= 1024) {
766 s = format("%.1f Mbytes", v/((double)(1024)));
768 s = format("%.0f Kbytes", (double)v);
773 /* return a value in multiples of 1024 give a string that can end
777 value_units(const char *s)
802 path_max(const char *path)
807 #elif defined(MAXPATHLEN)
810 #elif defined(_PC_PATH_MAX)
811 long maxlen = pathconf(path, _PC_PATH_MAX);
812 if (maxlen >= 4096) {
821 a sane realpath() function, trying to cope with stupid path limits and
825 x_realpath(const char *path)
827 long maxlen = path_max(path);
830 ret = x_malloc(maxlen);
833 p = realpath(path, ret);
835 /* yes, there are such systems. This replacement relies on
836 the fact that when we call x_realpath we only care about symlinks */
838 int len = readlink(path, ret, maxlen-1);
857 /* a getcwd that will returns an allocated buffer */
864 char *buffer = (char *)x_malloc(size);
865 if (getcwd(buffer, size) == buffer) {
869 if (errno != ERANGE) {
870 cc_log("getcwd error: %d (%s)", errno, strerror(errno));
877 /* create an empty file */
879 create_empty_file(const char *fname)
883 fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC|O_EXCL|O_BINARY, 0666);
892 * Return current user's home directory, or NULL if it can't be determined.
895 get_home_directory(void)
897 const char *p = getenv("HOME");
903 struct passwd *pwd = getpwuid(getuid());
913 * Get the current directory by reading $PWD. If $PWD isn't sane, gnu_getcwd()
914 * is used. Caller frees.
932 if (stat(pwd, &st_pwd) != 0) {
935 if (stat(cwd, &st_cwd) != 0) {
938 if (st_pwd.st_dev == st_cwd.st_dev && st_pwd.st_ino == st_cwd.st_ino) {
940 return x_strdup(pwd);
947 * Check whether s1 and s2 have the same executable name.
950 same_executable_name(const char *s1, const char *s2)
953 bool eq = strcasecmp(s1, s2) == 0;
955 char *tmp = format("%s.exe", s2);
956 eq = strcasecmp(s1, tmp) == 0;
961 return str_eq(s1, s2);
966 * Compute the length of the longest directory path that is common to two
970 common_dir_prefix_length(const char *s1, const char *s2)
975 while (*p1 && *p2 && *p1 == *p2) {
979 while (p1 > s1 && ((*p1 && *p1 != '/' ) || (*p2 && *p2 != '/'))) {
987 * Compute a relative path from from to to. Caller frees.
990 get_relative_path(const char *from, const char *to)
992 size_t common_prefix_len;
997 if (!*to || *to != '/') {
1001 result = x_strdup("");
1002 common_prefix_len = common_dir_prefix_length(from, to);
1003 for (p = from + common_prefix_len; *p; p++) {
1005 x_asprintf2(&result, "../%s", result);
1008 if (strlen(to) > common_prefix_len) {
1009 p = to + common_prefix_len + 1;
1013 x_asprintf2(&result, "%s%s", result, p);
1015 i = strlen(result) - 1;
1016 while (i >= 0 && result[i] == '/') {
1020 if (str_eq(result, "")) {
1022 result = x_strdup(".");
1028 * Return whether path is absolute.
1031 is_absolute_path(const char *path)
1034 return path[0] && path[1] == ':';
1036 return path[0] == '/';
1041 * Return whether the argument is a full path.
1044 is_full_path(const char *path)
1046 if (strchr(path, '/'))
1049 if (strchr(path, '\\'))
1056 * Update the modification time of a file in the cache to save it from LRU
1060 update_mtime(const char *path)
1070 * Rename oldpath to newpath (deleting newpath).
1073 x_rename(const char *oldpath, const char *newpath)
1076 /* Windows' rename() refuses to overwrite an existing file. */
1077 unlink(newpath); /* not x_unlink, as x_unlink calls x_rename */
1079 return rename(oldpath, newpath);
1083 * Remove path, NFS hazardous. Use only for temporary files that will not exist
1084 * on other systems. That is, the path should include tmp_string().
1087 tmp_unlink(const char *path)
1089 cc_log("Unlink %s (as-tmp)", path);
1090 return unlink(path);
1094 * Remove path, NFS safe.
1097 x_unlink(const char *path)
1100 * If path is on an NFS share, unlink isn't atomic, so we rename to a temp
1101 * file. We don't care if the temp file is trashed, so it's always safe to
1104 const char* tmp_name = format("%s.%s.rmXXXXXX", path, tmp_string());
1105 cc_log("Unlink %s via %s", path, tmp_name);
1106 if (x_rename(path, tmp_name) == -1) {
1109 if (unlink(tmp_name) == -1) {
1116 /* Like readlink() but returns the string or NULL on failure. Caller frees. */
1118 x_readlink(const char *path)
1120 long maxlen = path_max(path);
1125 #elif defined(MAXPATHLEN)
1126 maxlen = MAXPATHLEN;
1127 #elif defined(_PC_PATH_MAX)
1128 maxlen = pathconf(path, _PC_PATH_MAX);
1130 if (maxlen < 4096) maxlen = 4096;
1132 buf = x_malloc(maxlen);
1133 len = readlink(path, buf, maxlen-1);
1144 * Reads the content of a file. Size hint 0 means no hint. Returns true on
1145 * success, otherwise false.
1148 read_file(const char *path, size_t size_hint, char **data, size_t *size)
1151 size_t pos = 0, allocated;
1153 if (size_hint == 0) {
1155 if (stat(path, &st) == 0) {
1156 size_hint = st.st_size;
1159 size_hint = (size_hint < 1024) ? 1024 : size_hint;
1161 fd = open(path, O_RDONLY);
1165 allocated = size_hint;
1166 *data = x_malloc(allocated);
1169 if (pos > allocated / 2) {
1171 *data = x_realloc(*data, allocated);
1173 ret = read(fd, *data + pos, allocated - pos);
1174 if (ret == 0 || (ret == -1 && errno != EINTR)) {
1183 cc_log("Failed reading %s", path);
1194 * Return the content (with NUL termination) of a text file, or NULL on error.
1198 read_text_file(const char *path)
1203 if (read_file(path, 0, &data, &size)) {
1204 data = x_realloc(data, size + 1);