annotate lib/swig/swigwin-2.0.11/CCache/util.c @ 2553:48708da03b7f

Party Creation UI logic separated from MainMenu
author a.parshin
date Wed, 13 May 2015 02:20:05 +0200
parents b3009adc0e2f
children
rev   line source
1899
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
1 /*
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
2 Copyright (C) Andrew Tridgell 2002
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
3
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
4 This program is free software; you can redistribute it and/or modify
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
5 it under the terms of the GNU General Public License as published by
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
6 the Free Software Foundation; either version 2 of the License, or
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
7 (at your option) any later version.
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
8
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
9 This program is distributed in the hope that it will be useful,
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
12 GNU General Public License for more details.
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
13
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
14 You should have received a copy of the GNU General Public License
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
15 along with this program; if not, write to the Free Software
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
17 */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
18
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
19 #include "ccache.h"
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
20
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
21 static FILE *logfile;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
22
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
23 /* log a message to the CCACHE_LOGFILE location */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
24 void cc_log(const char *format, ...)
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
25 {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
26 va_list ap;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
27 extern char *cache_logfile;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
28
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
29 if (!cache_logfile) return;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
30
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
31 if (!logfile) logfile = fopen(cache_logfile, "a");
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
32 if (!logfile) return;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
33
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
34 va_start(ap, format);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
35 vfprintf(logfile, format, ap);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
36 va_end(ap);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
37 fflush(logfile);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
38 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
39
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
40 /* something went badly wrong! */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
41 void fatal(const char *msg)
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
42 {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
43 cc_log("FATAL: %s\n", msg);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
44 exit(1);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
45 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
46
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
47 int safe_rename(const char* oldpath, const char* newpath)
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
48 {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
49 /* safe_rename is for creating entries in the cache.
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
50
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
51 Works like rename(), but it never overwrites an existing
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
52 cache entry. This avoids corruption on NFS. */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
53 #ifndef _WIN32
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
54 int status = link(oldpath, newpath);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
55 if( status == 0 || errno == EEXIST )
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
56 #else
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
57 int status = CreateHardLinkA(newpath, oldpath, NULL) ? 0 : -1;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
58 if( status == 0 || GetLastError() == ERROR_ALREADY_EXISTS )
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
59 #endif
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
60 {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
61 return unlink( oldpath );
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
62 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
63 else
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
64 {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
65 return -1;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
66 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
67 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
68
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
69 #ifndef ENABLE_ZLIB
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
70 /* copy all data from one file descriptor to another */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
71 void copy_fd(int fd_in, int fd_out)
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
72 {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
73 char buf[10240];
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
74 int n;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
75
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
76 while ((n = read(fd_in, buf, sizeof(buf))) > 0) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
77 if (write(fd_out, buf, n) != n) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
78 fatal("Failed to copy fd");
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
79 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
80 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
81 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
82
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
83 #ifndef HAVE_MKSTEMP
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
84 /* cheap and nasty mkstemp replacement */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
85 int mkstemp(char *template)
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
86 {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
87 mktemp(template);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
88 return open(template, O_RDWR | O_CREAT | O_EXCL | O_BINARY, 0600);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
89 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
90 #endif
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
91
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
92 /* move a file using rename */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
93 int move_file(const char *src, const char *dest) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
94 return safe_rename(src, dest);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
95 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
96
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
97 /* copy a file - used when hard links don't work
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
98 the copy is done via a temporary file and atomic rename
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
99 */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
100 static int copy_file(const char *src, const char *dest)
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
101 {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
102 int fd1, fd2;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
103 char buf[10240];
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
104 int n;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
105 char *tmp_name;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
106 mode_t mask;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
107
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
108 x_asprintf(&tmp_name, "%s.XXXXXX", dest);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
109
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
110 fd1 = open(src, O_RDONLY|O_BINARY);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
111 if (fd1 == -1) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
112 free(tmp_name);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
113 return -1;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
114 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
115
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
116 fd2 = mkstemp(tmp_name);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
117 if (fd2 == -1) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
118 close(fd1);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
119 free(tmp_name);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
120 return -1;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
121 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
122
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
123 while ((n = read(fd1, buf, sizeof(buf))) > 0) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
124 if (write(fd2, buf, n) != n) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
125 close(fd2);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
126 close(fd1);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
127 unlink(tmp_name);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
128 free(tmp_name);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
129 return -1;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
130 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
131 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
132
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
133 close(fd1);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
134
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
135 /* get perms right on the tmp file */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
136 #ifndef _WIN32
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
137 mask = umask(0);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
138 fchmod(fd2, 0666 & ~mask);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
139 umask(mask);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
140 #else
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
141 (void)mask;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
142 #endif
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
143
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
144 /* the close can fail on NFS if out of space */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
145 if (close(fd2) == -1) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
146 unlink(tmp_name);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
147 free(tmp_name);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
148 return -1;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
149 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
150
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
151 unlink(dest);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
152
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
153 if (rename(tmp_name, dest) == -1) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
154 unlink(tmp_name);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
155 free(tmp_name);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
156 return -1;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
157 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
158
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
159 free(tmp_name);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
160
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
161 return 0;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
162 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
163
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
164 /* copy a file to the cache */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
165 static int copy_file_to_cache(const char *src, const char *dest) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
166 return copy_file(src, dest);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
167 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
168
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
169 /* copy a file from the cache */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
170 static int copy_file_from_cache(const char *src, const char *dest) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
171 return copy_file(src, dest);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
172 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
173
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
174 #else /* ENABLE_ZLIB */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
175
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
176 /* copy all data from one file descriptor to another
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
177 possibly decompressing it
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
178 */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
179 void copy_fd(int fd_in, int fd_out) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
180 char buf[10240];
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
181 int n;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
182 gzFile gz_in;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
183
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
184 gz_in = gzdopen(dup(fd_in), "rb");
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
185
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
186 if (!gz_in) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
187 fatal("Failed to copy fd");
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
188 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
189
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
190 while ((n = gzread(gz_in, buf, sizeof(buf))) > 0) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
191 if (write(fd_out, buf, n) != n) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
192 fatal("Failed to copy fd");
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
193 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
194 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
195 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
196
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
197 static int _copy_file(const char *src, const char *dest, int mode) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
198 int fd_in, fd_out;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
199 gzFile gz_in, gz_out = NULL;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
200 char buf[10240];
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
201 int n, ret;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
202 char *tmp_name;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
203 mode_t mask;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
204 struct stat st;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
205
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
206 x_asprintf(&tmp_name, "%s.XXXXXX", dest);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
207
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
208 if (getenv("CCACHE_NOCOMPRESS")) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
209 mode = COPY_UNCOMPRESSED;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
210 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
211
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
212 /* open source file */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
213 fd_in = open(src, O_RDONLY);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
214 if (fd_in == -1) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
215 return -1;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
216 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
217
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
218 gz_in = gzdopen(fd_in, "rb");
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
219 if (!gz_in) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
220 close(fd_in);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
221 return -1;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
222 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
223
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
224 /* open destination file */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
225 fd_out = mkstemp(tmp_name);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
226 if (fd_out == -1) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
227 gzclose(gz_in);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
228 free(tmp_name);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
229 return -1;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
230 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
231
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
232 if (mode == COPY_TO_CACHE) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
233 /* The gzip file format occupies at least 20 bytes. So
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
234 it will always occupy an entire filesystem block,
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
235 even for empty files.
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
236 Since most stderr files will be empty, we turn off
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
237 compression in this case to save space.
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
238 */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
239 if (fstat(fd_in, &st) != 0) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
240 gzclose(gz_in);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
241 close(fd_out);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
242 free(tmp_name);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
243 return -1;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
244 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
245 if (file_size(&st) == 0) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
246 mode = COPY_UNCOMPRESSED;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
247 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
248 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
249
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
250 if (mode == COPY_TO_CACHE) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
251 gz_out = gzdopen(dup(fd_out), "wb");
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
252 if (!gz_out) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
253 gzclose(gz_in);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
254 close(fd_out);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
255 free(tmp_name);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
256 return -1;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
257 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
258 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
259
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
260 while ((n = gzread(gz_in, buf, sizeof(buf))) > 0) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
261 if (mode == COPY_TO_CACHE) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
262 ret = gzwrite(gz_out, buf, n);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
263 } else {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
264 ret = write(fd_out, buf, n);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
265 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
266 if (ret != n) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
267 gzclose(gz_in);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
268 if (gz_out) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
269 gzclose(gz_out);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
270 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
271 close(fd_out);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
272 unlink(tmp_name);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
273 free(tmp_name);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
274 return -1;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
275 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
276 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
277
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
278 gzclose(gz_in);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
279 if (gz_out) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
280 gzclose(gz_out);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
281 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
282
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
283 /* get perms right on the tmp file */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
284 mask = umask(0);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
285 fchmod(fd_out, 0666 & ~mask);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
286 umask(mask);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
287
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
288 /* the close can fail on NFS if out of space */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
289 if (close(fd_out) == -1) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
290 unlink(tmp_name);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
291 free(tmp_name);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
292 return -1;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
293 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
294
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
295 unlink(dest);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
296
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
297 if (rename(tmp_name, dest) == -1) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
298 unlink(tmp_name);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
299 free(tmp_name);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
300 return -1;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
301 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
302
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
303 free(tmp_name);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
304
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
305 return 0;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
306 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
307
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
308 /* move a file to the cache, compressing it */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
309 int move_file(const char *src, const char *dest) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
310 int ret;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
311
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
312 ret = _copy_file(src, dest, COPY_TO_CACHE);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
313 if (ret != -1) unlink(src);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
314 return ret;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
315 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
316
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
317 /* copy a file to the cache, compressing it */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
318 static int copy_file_to_cache(const char *src, const char *dest) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
319 return _copy_file(src, dest, COPY_TO_CACHE);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
320 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
321
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
322 /* copy a file from the cache, decompressing it */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
323 static int copy_file_from_cache(const char *src, const char *dest) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
324 return _copy_file(src, dest, COPY_FROM_CACHE);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
325 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
326 #endif /* ENABLE_ZLIB */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
327
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
328 /* test if a file is zlib compressed */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
329 int test_if_compressed(const char *filename) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
330 FILE *f;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
331
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
332 f = fopen(filename, "rb");
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
333 if (!f) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
334 return 0;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
335 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
336
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
337 /* test if file starts with 1F8B, which is zlib's
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
338 * magic number */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
339 if ((fgetc(f) != 0x1f) || (fgetc(f) != 0x8b)) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
340 fclose(f);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
341 return 0;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
342 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
343
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
344 fclose(f);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
345 return 1;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
346 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
347
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
348 /* copy file to the cache with error checking taking into account compression and hard linking if desired */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
349 int commit_to_cache(const char *src, const char *dest, int hardlink)
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
350 {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
351 int ret = -1;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
352 struct stat st;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
353 if (stat(src, &st) == 0) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
354 unlink(dest);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
355 if (hardlink) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
356 #ifdef _WIN32
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
357 ret = CreateHardLinkA(dest, src, NULL) ? 0 : -1;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
358 #else
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
359 ret = link(src, dest);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
360 #endif
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
361 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
362 if (ret == -1) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
363 ret = copy_file_to_cache(src, dest);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
364 if (ret == -1) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
365 cc_log("failed to commit %s -> %s (%s)\n", src, dest, strerror(errno));
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
366 stats_update(STATS_ERROR);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
367 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
368 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
369 } else {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
370 cc_log("failed to put %s in the cache (%s)\n", src, strerror(errno));
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
371 stats_update(STATS_ERROR);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
372 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
373 return ret;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
374 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
375
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
376 /* copy file out of the cache with error checking taking into account compression and hard linking if desired */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
377 int retrieve_from_cache(const char *src, const char *dest, int hardlink)
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
378 {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
379 int ret = 0;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
380
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
381 x_utimes(src);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
382
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
383 if (strcmp(dest, "/dev/null") == 0) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
384 ret = 0;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
385 } else {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
386 unlink(dest);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
387 /* only make a hardlink if the cache file is uncompressed */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
388 if (hardlink && test_if_compressed(src) == 0) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
389 #ifdef _WIN32
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
390 ret = CreateHardLinkA(dest, src, NULL) ? 0 : -1;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
391 #else
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
392 ret = link(src, dest);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
393 #endif
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
394 } else {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
395 ret = copy_file_from_cache(src, dest);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
396 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
397 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
398
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
399 /* the cached file might have been deleted by some external process */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
400 if (ret == -1 && errno == ENOENT) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
401 cc_log("hashfile missing for %s\n", dest);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
402 stats_update(STATS_MISSING);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
403 return -1;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
404 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
405
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
406 if (ret == -1) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
407 ret = copy_file_from_cache(src, dest);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
408 if (ret == -1) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
409 cc_log("failed to retrieve %s -> %s (%s)\n", src, dest, strerror(errno));
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
410 stats_update(STATS_ERROR);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
411 return -1;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
412 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
413 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
414 return ret;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
415 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
416
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
417 /* make sure a directory exists */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
418 int create_dir(const char *dir)
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
419 {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
420 struct stat st;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
421 if (stat(dir, &st) == 0) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
422 if (S_ISDIR(st.st_mode)) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
423 return 0;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
424 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
425 errno = ENOTDIR;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
426 return 1;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
427 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
428 #ifdef _WIN32
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
429 if (mkdir(dir) != 0 && errno != EEXIST) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
430 return 1;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
431 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
432 #else
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
433 if (mkdir(dir, 0777) != 0 && errno != EEXIST) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
434 return 1;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
435 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
436 #endif
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
437 return 0;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
438 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
439
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
440 char const CACHEDIR_TAG[] =
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
441 "Signature: 8a477f597d28d172789f06886806bc55\n"
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
442 "# This file is a cache directory tag created by ccache.\n"
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
443 "# For information about cache directory tags, see:\n"
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
444 "# http://www.brynosaurus.com/cachedir/\n";
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
445
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
446 int create_cachedirtag(const char *dir)
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
447 {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
448 char *filename;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
449 struct stat st;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
450 FILE *f;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
451 x_asprintf(&filename, "%s/CACHEDIR.TAG", dir);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
452 if (stat(filename, &st) == 0) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
453 if (S_ISREG(st.st_mode)) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
454 goto success;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
455 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
456 errno = EEXIST;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
457 goto error;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
458 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
459 f = fopen(filename, "w");
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
460 if (!f) goto error;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
461 if (fwrite(CACHEDIR_TAG, sizeof(CACHEDIR_TAG)-1, 1, f) != 1) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
462 goto error;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
463 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
464 if (fclose(f)) goto error;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
465 success:
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
466 free(filename);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
467 return 0;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
468 error:
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
469 free(filename);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
470 return 1;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
471 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
472
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
473 /*
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
474 this is like asprintf() but dies if the malloc fails
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
475 note that we use vsnprintf in a rather poor way to make this more portable
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
476 */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
477 void x_asprintf(char **ptr, const char *format, ...)
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
478 {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
479 va_list ap;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
480
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
481 *ptr = NULL;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
482 va_start(ap, format);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
483 if (vasprintf(ptr, format, ap) == -1) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
484 fatal("out of memory in x_asprintf");
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
485 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
486 va_end(ap);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
487
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
488 if (!ptr) fatal("out of memory in x_asprintf");
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
489 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
490
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
491 /*
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
492 this is like strdup() but dies if the malloc fails
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
493 */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
494 char *x_strdup(const char *s)
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
495 {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
496 char *ret;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
497 ret = strdup(s);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
498 if (!ret) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
499 fatal("out of memory in strdup\n");
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
500 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
501 return ret;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
502 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
503
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
504 /*
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
505 this is like malloc() but dies if the malloc fails
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
506 */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
507 void *x_malloc(size_t size)
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
508 {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
509 void *ret;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
510 ret = malloc(size);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
511 if (!ret) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
512 fatal("out of memory in malloc\n");
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
513 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
514 return ret;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
515 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
516
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
517 /*
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
518 this is like realloc() but dies if the malloc fails
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
519 */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
520 void *x_realloc(void *ptr, size_t size)
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
521 {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
522 void *p2;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
523 #if 1
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
524 /* Avoid invalid read in memcpy below */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
525 p2 = realloc(ptr, size);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
526 if (!p2) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
527 fatal("out of memory in x_realloc");
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
528 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
529 #else
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
530 if (!ptr) return x_malloc(size);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
531 p2 = malloc(size);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
532 if (!p2) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
533 fatal("out of memory in x_realloc");
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
534 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
535 if (ptr) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
536 /* Note invalid read as the memcpy reads beyond the memory allocated by ptr */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
537 memcpy(p2, ptr, size);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
538 free(ptr);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
539 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
540 #endif
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
541 return p2;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
542 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
543
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
544
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
545 /*
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
546 revsusive directory traversal - used for cleanup
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
547 fn() is called on all files/dirs in the tree
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
548 */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
549 void traverse(const char *dir, void (*fn)(const char *, struct stat *))
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
550 {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
551 DIR *d;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
552 struct dirent *de;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
553
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
554 d = opendir(dir);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
555 if (!d) return;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
556
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
557 while ((de = readdir(d))) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
558 char *fname;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
559 struct stat st;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
560
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
561 if (strcmp(de->d_name,".") == 0) continue;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
562 if (strcmp(de->d_name,"..") == 0) continue;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
563
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
564 if (strlen(de->d_name) == 0) continue;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
565
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
566 x_asprintf(&fname, "%s/%s", dir, de->d_name);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
567 #ifdef _WIN32
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
568 if (stat(fname, &st))
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
569 #else
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
570 if (lstat(fname, &st))
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
571 #endif
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
572 {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
573 if (errno != ENOENT) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
574 perror(fname);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
575 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
576 free(fname);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
577 continue;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
578 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
579
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
580 if (S_ISDIR(st.st_mode)) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
581 traverse(fname, fn);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
582 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
583
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
584 fn(fname, &st);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
585 free(fname);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
586 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
587
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
588 closedir(d);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
589 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
590
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
591
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
592 /* return the base name of a file - caller frees */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
593 char *str_basename(const char *s)
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
594 {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
595 char *p = strrchr(s, '/');
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
596 if (p) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
597 s = (p+1);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
598 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
599
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
600 #ifdef _WIN32
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
601 p = strrchr(s, '\\');
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
602
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
603 if (p) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
604 s = (p+1);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
605 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
606 #endif
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
607
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
608 return x_strdup(s);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
609 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
610
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
611 /* return the dir name of a file - caller frees */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
612 char *dirname(char *s)
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
613 {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
614 char *p;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
615 s = x_strdup(s);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
616 p = strrchr(s, '/');
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
617 #ifdef _WIN32
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
618 p = strrchr(s, '\\');
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
619 #endif
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
620 if (p) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
621 *p = 0;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
622 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
623 return s;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
624 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
625
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
626 /*
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
627 http://www.ecst.csuchico.edu/~beej/guide/ipc/flock.html
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
628 http://cvs.php.net/viewvc.cgi/php-src/win32/flock.c?revision=1.2&view=markup
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
629 Should return 0 for success, >0 otherwise
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
630 */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
631 int lock_fd(int fd)
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
632 {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
633 #ifdef _WIN32
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
634 # if 1
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
635 return _locking(fd, _LK_NBLCK, 1);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
636 # else
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
637 HANDLE fl = (HANDLE)_get_osfhandle(fd);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
638 OVERLAPPED o;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
639 memset(&o, 0, sizeof(o));
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
640 return (LockFileEx(fl, LOCKFILE_EXCLUSIVE_LOCK, 0, 1, 0, &o))
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
641 ? 0 : GetLastError();
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
642 # endif
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
643 #else
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
644 struct flock fl;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
645 int ret;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
646
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
647 fl.l_type = F_WRLCK;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
648 fl.l_whence = SEEK_SET;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
649 fl.l_start = 0;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
650 fl.l_len = 1;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
651 fl.l_pid = 0;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
652
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
653 /* not sure why we would be getting a signal here,
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
654 but one user claimed it is possible */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
655 do {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
656 ret = fcntl(fd, F_SETLKW, &fl);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
657 } while (ret == -1 && errno == EINTR);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
658 return ret;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
659 #endif
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
660 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
661
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
662 /* return size on disk of a file */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
663 size_t file_size(struct stat *st)
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
664 {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
665 #ifdef _WIN32
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
666 return (st->st_size + 1023) & ~1023;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
667 #else
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
668 size_t size = st->st_blocks * 512;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
669 if ((size_t)st->st_size > size) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
670 /* probably a broken stat() call ... */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
671 size = (st->st_size + 1023) & ~1023;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
672 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
673 return size;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
674 #endif
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
675 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
676
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
677
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
678 /* a safe open/create for read-write */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
679 int safe_open(const char *fname)
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
680 {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
681 int fd = open(fname, O_RDWR|O_BINARY);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
682 if (fd == -1 && errno == ENOENT) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
683 fd = open(fname, O_RDWR|O_CREAT|O_EXCL|O_BINARY, 0666);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
684 if (fd == -1 && errno == EEXIST) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
685 fd = open(fname, O_RDWR|O_BINARY);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
686 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
687 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
688 return fd;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
689 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
690
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
691 /* display a kilobyte unsigned value in M, k or G */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
692 void display_size(unsigned v)
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
693 {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
694 if (v > 1024*1024) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
695 printf("%8.1f Gbytes", v/((double)(1024*1024)));
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
696 } else if (v > 1024) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
697 printf("%8.1f Mbytes", v/((double)(1024)));
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
698 } else {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
699 printf("%8u Kbytes", v);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
700 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
701 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
702
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
703 /* return a value in multiples of 1024 give a string that can end
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
704 in K, M or G
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
705 */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
706 size_t value_units(const char *s)
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
707 {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
708 char m;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
709 double v = atof(s);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
710 m = s[strlen(s)-1];
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
711 switch (m) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
712 case 'G':
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
713 case 'g':
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
714 default:
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
715 v *= 1024*1024;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
716 break;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
717 case 'M':
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
718 case 'm':
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
719 v *= 1024;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
720 break;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
721 case 'K':
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
722 case 'k':
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
723 v *= 1;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
724 break;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
725 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
726 return (size_t)v;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
727 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
728
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
729
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
730 /*
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
731 a sane realpath() function, trying to cope with stupid path limits and
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
732 a broken API
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
733 */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
734 char *x_realpath(const char *path)
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
735 {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
736 #ifdef _WIN32
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
737 char namebuf[MAX_PATH];
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
738 DWORD ret;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
739
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
740 ret = GetFullPathNameA(path, sizeof(namebuf), namebuf, NULL);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
741 if (ret == 0 || ret >= sizeof(namebuf)) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
742 return NULL;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
743 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
744
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
745 return x_strdup(namebuf);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
746 #else
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
747 int maxlen;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
748 char *ret, *p;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
749 #ifdef PATH_MAX
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
750 maxlen = PATH_MAX;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
751 #elif defined(MAXPATHLEN)
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
752 maxlen = MAXPATHLEN;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
753 #elif defined(_PC_PATH_MAX)
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
754 maxlen = pathconf(path, _PC_PATH_MAX);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
755 #endif
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
756 if (maxlen < 4096) maxlen = 4096;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
757
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
758 ret = x_malloc(maxlen);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
759
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
760 #if HAVE_REALPATH
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
761 p = realpath(path, ret);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
762 #else
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
763 /* yes, there are such systems. This replacement relies on
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
764 the fact that when we call x_realpath we only care about symlinks */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
765 {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
766 int len = readlink(path, ret, maxlen-1);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
767 if (len == -1) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
768 free(ret);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
769 return NULL;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
770 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
771 ret[len] = 0;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
772 p = ret;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
773 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
774 #endif
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
775 if (p) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
776 p = x_strdup(p);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
777 free(ret);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
778 return p;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
779 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
780 free(ret);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
781 return NULL;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
782 #endif
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
783 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
784
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
785 /* a getcwd that will returns an allocated buffer */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
786 char *gnu_getcwd(void)
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
787 {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
788 unsigned size = 128;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
789
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
790 while (1) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
791 char *buffer = (char *)x_malloc(size);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
792 if (getcwd(buffer, size) == buffer) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
793 return buffer;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
794 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
795 free(buffer);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
796 if (errno != ERANGE) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
797 return 0;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
798 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
799 size *= 2;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
800 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
801 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
802
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
803 /* create an empty file */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
804 int create_empty_file(const char *fname)
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
805 {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
806 int fd;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
807
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
808 fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC|O_EXCL|O_BINARY, 0666);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
809 if (fd == -1) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
810 return -1;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
811 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
812 close(fd);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
813 return 0;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
814 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
815
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
816 /*
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
817 return current users home directory or die
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
818 */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
819 const char *get_home_directory(void)
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
820 {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
821 #ifdef _WIN32
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
822 static char home_path[MAX_PATH] = {0};
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
823 HRESULT ret;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
824
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
825 /* we already have the path */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
826 if (home_path[0] != 0) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
827 return home_path;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
828 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
829
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
830 /* get the path to "Application Data" folder */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
831 ret = SHGetFolderPathA(NULL, CSIDL_APPDATA | CSIDL_FLAG_CREATE, NULL, 0, home_path);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
832 if (SUCCEEDED(ret)) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
833 return home_path;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
834 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
835
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
836 fprintf(stderr, "ccache: Unable to determine home directory\n");
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
837 return NULL;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
838 #else
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
839 const char *p = getenv("HOME");
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
840 if (p) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
841 return p;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
842 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
843 #ifdef HAVE_GETPWUID
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
844 {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
845 struct passwd *pwd = getpwuid(getuid());
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
846 if (pwd) {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
847 return pwd->pw_dir;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
848 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
849 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
850 #endif
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
851 fatal("Unable to determine home directory");
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
852 return NULL;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
853 #endif
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
854 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
855
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
856 int x_utimes(const char *filename)
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
857 {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
858 #ifdef HAVE_UTIMES
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
859 return utimes(filename, NULL);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
860 #else
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
861 return utime(filename, NULL);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
862 #endif
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
863 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
864
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
865 #ifdef _WIN32
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
866 /* perror for Win32 API calls, using GetLastError() instead of errno */
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
867 void perror_win32(LPTSTR pszFunction)
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
868 {
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
869 LPTSTR pszMessage;
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
870 DWORD dwLastError = GetLastError();
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
871
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
872 FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER |
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
873 FORMAT_MESSAGE_FROM_SYSTEM |
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
874 FORMAT_MESSAGE_IGNORE_INSERTS,
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
875 NULL,
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
876 dwLastError,
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
877 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
878 (LPTSTR)&pszMessage,
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
879 0, NULL );
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
880
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
881 fprintf(stderr, "%s: %s\n", pszFunction, pszMessage);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
882 LocalFree(pszMessage);
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
883 }
b3009adc0e2f Adding swig, gitignore, hgignore
Nomad
parents:
diff changeset
884 #endif