@matheusbsbs / matheus-bagliano / commits / 33f239365c
Fork 0

path helpers: move git_mkstemp* to wrapper.c

git_mkstemp_mode and related functions do not require access to specialized git machinery, unlike some other functions from path.c (like set_shared_perm()). Move them to wrapper.c where the wrapper xmkstemp_mode is defined. This eliminates a dependency of wrapper.o on environment.o via path.o. With typical linkers (e.g., gcc), that dependency makes programs that use functions from wrapper.o and not environment.o or path.o larger than they need to be. Signed-off-by: Jonathan Nieder <jrnieder@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jonathan Nieder committed Nov 6, 2010 at 06:46 UTC 33f239365cce2682a1faac0d5670d684aa1981ad
2 files changed +113 -113
path.c
-113
@@ -161,119 +161,6 @@ char *git_path_submodule(const char *path, const char *fmt, ...)
161 return cleanup_path(pathname);
162 }
163
164 -/* git_mkstemp() - create tmp file honoring TMPDIR variable */
165 -int git_mkstemp(char *path, size_t len, const char *template)
166 -{
167 - const char *tmp;
168 - size_t n;
169 -
170 - tmp = getenv("TMPDIR");
171 - if (!tmp)
172 - tmp = "/tmp";
173 - n = snprintf(path, len, "%s/%s", tmp, template);
174 - if (len <= n) {
175 - errno = ENAMETOOLONG;
176 - return -1;
177 - }
178 - return mkstemp(path);
179 -}
180 -
181 -/* git_mkstemps() - create tmp file with suffix honoring TMPDIR variable. */
182 -int git_mkstemps(char *path, size_t len, const char *template, int suffix_len)
183 -{
184 - const char *tmp;
185 - size_t n;
186 -
187 - tmp = getenv("TMPDIR");
188 - if (!tmp)
189 - tmp = "/tmp";
190 - n = snprintf(path, len, "%s/%s", tmp, template);
191 - if (len <= n) {
192 - errno = ENAMETOOLONG;
193 - return -1;
194 - }
195 - return mkstemps(path, suffix_len);
196 -}
197 -
198 -/* Adapted from libiberty's mkstemp.c. */
199 -
200 -#undef TMP_MAX
201 -#define TMP_MAX 16384
202 -
203 -int git_mkstemps_mode(char *pattern, int suffix_len, int mode)
204 -{
205 - static const char letters[] =
206 - "abcdefghijklmnopqrstuvwxyz"
207 - "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
208 - "0123456789";
209 - static const int num_letters = 62;
210 - uint64_t value;
211 - struct timeval tv;
212 - char *template;
213 - size_t len;
214 - int fd, count;
215 -
216 - len = strlen(pattern);
217 -
218 - if (len < 6 + suffix_len) {
219 - errno = EINVAL;
220 - return -1;
221 - }
222 -
223 - if (strncmp(&pattern[len - 6 - suffix_len], "XXXXXX", 6)) {
224 - errno = EINVAL;
225 - return -1;
226 - }
227 -
228 - /*
229 - * Replace pattern's XXXXXX characters with randomness.
230 - * Try TMP_MAX different filenames.
231 - */
232 - gettimeofday(&tv, NULL);
233 - value = ((size_t)(tv.tv_usec << 16)) ^ tv.tv_sec ^ getpid();
234 - template = &pattern[len - 6 - suffix_len];
235 - for (count = 0; count < TMP_MAX; ++count) {
236 - uint64_t v = value;
237 - /* Fill in the random bits. */
238 - template[0] = letters[v % num_letters]; v /= num_letters;
239 - template[1] = letters[v % num_letters]; v /= num_letters;
240 - template[2] = letters[v % num_letters]; v /= num_letters;
241 - template[3] = letters[v % num_letters]; v /= num_letters;
242 - template[4] = letters[v % num_letters]; v /= num_letters;
243 - template[5] = letters[v % num_letters]; v /= num_letters;
244 -
245 - fd = open(pattern, O_CREAT | O_EXCL | O_RDWR, mode);
246 - if (fd > 0)
247 - return fd;
248 - /*
249 - * Fatal error (EPERM, ENOSPC etc).
250 - * It doesn't make sense to loop.
251 - */
252 - if (errno != EEXIST)
253 - break;
254 - /*
255 - * This is a random value. It is only necessary that
256 - * the next TMP_MAX values generated by adding 7777 to
257 - * VALUE are different with (module 2^32).
258 - */
259 - value += 7777;
260 - }
261 - /* We return the null string if we can't find a unique file name. */
262 - pattern[0] = '\0';
263 - return -1;
264 -}
265 -
266 -int git_mkstemp_mode(char *pattern, int mode)
267 -{
268 - /* mkstemp is just mkstemps with no suffix */
269 - return git_mkstemps_mode(pattern, 0, mode);
270 -}
271 -
272 -int gitmkstemps(char *pattern, int suffix_len)
273 -{
274 - return git_mkstemps_mode(pattern, suffix_len, 0600);
275 -}
276 -
164 int validate_headref(const char *path)
165 {
166 struct stat st;
wrapper.c
+113
@@ -204,6 +204,119 @@ int xmkstemp(char *template)
204 return fd;
205 }
206
207 +/* git_mkstemp() - create tmp file honoring TMPDIR variable */
208 +int git_mkstemp(char *path, size_t len, const char *template)
209 +{
210 + const char *tmp;
211 + size_t n;
212 +
213 + tmp = getenv("TMPDIR");
214 + if (!tmp)
215 + tmp = "/tmp";
216 + n = snprintf(path, len, "%s/%s", tmp, template);
217 + if (len <= n) {
218 + errno = ENAMETOOLONG;
219 + return -1;
220 + }
221 + return mkstemp(path);
222 +}
223 +
224 +/* git_mkstemps() - create tmp file with suffix honoring TMPDIR variable. */
225 +int git_mkstemps(char *path, size_t len, const char *template, int suffix_len)
226 +{
227 + const char *tmp;
228 + size_t n;
229 +
230 + tmp = getenv("TMPDIR");
231 + if (!tmp)
232 + tmp = "/tmp";
233 + n = snprintf(path, len, "%s/%s", tmp, template);
234 + if (len <= n) {
235 + errno = ENAMETOOLONG;
236 + return -1;
237 + }
238 + return mkstemps(path, suffix_len);
239 +}
240 +
241 +/* Adapted from libiberty's mkstemp.c. */
242 +
243 +#undef TMP_MAX
244 +#define TMP_MAX 16384
245 +
246 +int git_mkstemps_mode(char *pattern, int suffix_len, int mode)
247 +{
248 + static const char letters[] =
249 + "abcdefghijklmnopqrstuvwxyz"
250 + "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
251 + "0123456789";
252 + static const int num_letters = 62;
253 + uint64_t value;
254 + struct timeval tv;
255 + char *template;
256 + size_t len;
257 + int fd, count;
258 +
259 + len = strlen(pattern);
260 +
261 + if (len < 6 + suffix_len) {
262 + errno = EINVAL;
263 + return -1;
264 + }
265 +
266 + if (strncmp(&pattern[len - 6 - suffix_len], "XXXXXX", 6)) {
267 + errno = EINVAL;
268 + return -1;
269 + }
270 +
271 + /*
272 + * Replace pattern's XXXXXX characters with randomness.
273 + * Try TMP_MAX different filenames.
274 + */
275 + gettimeofday(&tv, NULL);
276 + value = ((size_t)(tv.tv_usec << 16)) ^ tv.tv_sec ^ getpid();
277 + template = &pattern[len - 6 - suffix_len];
278 + for (count = 0; count < TMP_MAX; ++count) {
279 + uint64_t v = value;
280 + /* Fill in the random bits. */
281 + template[0] = letters[v % num_letters]; v /= num_letters;
282 + template[1] = letters[v % num_letters]; v /= num_letters;
283 + template[2] = letters[v % num_letters]; v /= num_letters;
284 + template[3] = letters[v % num_letters]; v /= num_letters;
285 + template[4] = letters[v % num_letters]; v /= num_letters;
286 + template[5] = letters[v % num_letters]; v /= num_letters;
287 +
288 + fd = open(pattern, O_CREAT | O_EXCL | O_RDWR, mode);
289 + if (fd > 0)
290 + return fd;
291 + /*
292 + * Fatal error (EPERM, ENOSPC etc).
293 + * It doesn't make sense to loop.
294 + */
295 + if (errno != EEXIST)
296 + break;
297 + /*
298 + * This is a random value. It is only necessary that
299 + * the next TMP_MAX values generated by adding 7777 to
300 + * VALUE are different with (module 2^32).
301 + */
302 + value += 7777;
303 + }
304 + /* We return the null string if we can't find a unique file name. */
305 + pattern[0] = '\0';
306 + return -1;
307 +}
308 +
309 +int git_mkstemp_mode(char *pattern, int mode)
310 +{
311 + /* mkstemp is just mkstemps with no suffix */
312 + return git_mkstemps_mode(pattern, 0, mode);
313 +}
314 +
315 +int gitmkstemps(char *pattern, int suffix_len)
316 +{
317 + return git_mkstemps_mode(pattern, suffix_len, 0600);
318 +}
319 +
320 int xmkstemp_mode(char *template, int mode)
321 {
322 int fd;