Skip to content

Commit

Permalink
Merge maint into master (using imerge)
Browse files Browse the repository at this point in the history
  • Loading branch information
jrosdahl committed Nov 13, 2014
2 parents c6bd92b + 7c6209c commit f49770a
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 49 deletions.
31 changes: 17 additions & 14 deletions ccache.c
Expand Up @@ -741,7 +741,9 @@ to_cache(struct args *args)
cached_obj, strerror(errno));
}
tmp_stdout = format("%s.tmp.stdout.%s", cached_obj, tmp_string());
create_empty_file(tmp_stdout);
tmp_stderr = format("%s.tmp.stderr.%s", cached_obj, tmp_string());
create_empty_file(tmp_stderr);

args_add(args, "-o");
args_add(args, output_obj);
Expand Down Expand Up @@ -801,7 +803,7 @@ to_cache(struct args *args)
int fd_result;
char *tmp_stderr2;

tmp_stderr2 = format("%s.tmp.stderr2.%s", cached_obj, tmp_string());
tmp_stderr2 = format("%s.2", tmp_stderr);
if (x_rename(tmp_stderr, tmp_stderr2)) {
cc_log("Failed to rename %s to %s: %s", tmp_stderr, tmp_stderr2,
strerror(errno));
Expand Down Expand Up @@ -972,22 +974,28 @@ get_object_name_from_cpp(struct args *args, struct mdfour *hash)
input_base[10] = 0;
}

path_stdout = format(
"%s/%s.tmp.%s.%s",
temp_dir(), input_base, tmp_string(), conf->cpp_extension);
path_stderr = format("%s/tmp.cpp_stderr.%s", temp_dir(), tmp_string());

if (create_parent_dirs(path_stdout) != 0) {
if (create_parent_dirs(path_stderr) != 0) {
fatal("Failed to create parent directory for %s: %s\n",
path_stdout, strerror(errno));
path_stderr, strerror(errno));
}

add_pending_tmp_file(path_stdout);
create_empty_file(path_stderr);
add_pending_tmp_file(path_stderr);

time_of_compilation = time(NULL);

if (!direct_i_file) {
/* The temporary file needs the proper i_extension for the compiler to do
* its thing. However, create_empty_file requires the tmp_string() part to
* be last, which is why the temporary file is created in two steps. */
char *path_stdout_tmp =
format("%s/%s.tmp.%s", temp_dir(), input_base, tmp_string());
create_empty_file(path_stdout_tmp);
path_stdout = format("%s.%s", path_stdout_tmp, conf->cpp_extension);
x_rename(path_stdout_tmp, path_stdout);
free(path_stdout_tmp);
add_pending_tmp_file(path_stdout);

/* run cpp on the input file to obtain the .i */
args_add(args, "-E");
args_add(args, input_file);
Expand All @@ -999,11 +1007,6 @@ get_object_name_from_cpp(struct args *args, struct mdfour *hash)
can skip the cpp stage and directly form the
correct i_tmpfile */
path_stdout = input_file;
if (create_empty_file(path_stderr) != 0) {
cc_log("Failed to create %s: %s", path_stderr, strerror(errno));
stats_update(STATS_ERROR);
failed();
}
status = 0;
}

Expand Down
3 changes: 1 addition & 2 deletions ccache.h
Expand Up @@ -142,7 +142,6 @@ char *dirname(const char *path);
const char *get_extension(const char *path);
char *remove_extension(const char *path);
size_t file_size(struct stat *st);
int safe_create_wronly(const char *fname);
char *format_human_readable_size(uint64_t size);
char *format_parsable_size_with_suffix(uint64_t size);
bool parse_size_with_suffix(const char *str, uint64_t *size);
Expand All @@ -151,7 +150,7 @@ char *gnu_getcwd(void);
#ifndef HAVE_STRTOK_R
char *strtok_r(char *str, const char *delim, char **saveptr);
#endif
int create_empty_file(const char *fname);
int create_empty_file(char *fname);
const char *get_home_directory(void);
char *get_cwd(void);
bool same_executable_name(const char *s1, const char *s2);
Expand Down
2 changes: 1 addition & 1 deletion lockfile.c
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2010-2012 Joel Rosdahl
* Copyright (C) 2010-2014 Joel Rosdahl
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
Expand Down
8 changes: 7 additions & 1 deletion manifest.c
Expand Up @@ -737,7 +737,13 @@ manifest_put(const char *manifest_path, struct file_hash *object_hash,
}

tmp_file = format("%s.tmp.%s", manifest_path, tmp_string());
fd2 = safe_create_wronly(tmp_file);
fd2 = mkstemp(tmp_file);
if (fd2 == -1 && errno == ENOENT) {
if (create_parent_dirs(tmp_file) == 0) {
reformat(&tmp_file, "%s.tmp.%s", manifest_path, tmp_string());
fd2 = mkstemp(tmp_file);
}
}
if (fd2 == -1) {
cc_log("Failed to open %s", tmp_file);
goto out;
Expand Down
24 changes: 19 additions & 5 deletions stats.c
Expand Up @@ -130,14 +130,22 @@ stats_write(const char *path, struct counters *counters)
size_t i;
char *tmp_file;
FILE *f;
int fd;

tmp_file = format("%s.tmp.%s", path, tmp_string());
f = fopen(tmp_file, "wb");
if (!f && errno == ENOENT) {
if (create_parent_dirs(path) == 0) {
f = fopen(tmp_file, "wb");
fd = mkstemp(tmp_file);
if (fd == -1 && errno == ENOENT) {
if (create_parent_dirs(tmp_file) == 0) {
reformat(&tmp_file, "%s.tmp.%s", path, tmp_string());
fd = mkstemp(tmp_file);
}
}
if (fd == -1) {
cc_log("Failed to open %s", tmp_file);
close(fd);
goto end;
}
f = fdopen(fd, "wb");
if (!f) {
cc_log("Failed to open %s", tmp_file);
goto end;
Expand All @@ -147,7 +155,7 @@ stats_write(const char *path, struct counters *counters)
fatal("Failed to write to %s", tmp_file);
}
}
fclose(f);
fclose(f); /* This also implicitly closes the fd. */
x_rename(tmp_file, path);

end:
Expand Down Expand Up @@ -360,7 +368,13 @@ stats_zero(void)

for (dir = 0; dir <= 0xF; dir++) {
struct counters *counters = counters_init(STATS_END);
struct stat st;
fname = format("%s/%1x/stats", conf->cache_dir, dir);
if (stat(fname, &st) != 0) {
/* No point in trying to reset the stats file if it doesn't exist. */
free(fname);
continue;
}
if (lockfile_acquire(fname, lock_staleness_limit)) {
stats_read(fname, counters);
for (i = 0; stats_info[i].message; i++) {
Expand Down
37 changes: 11 additions & 26 deletions util.c
Expand Up @@ -241,7 +241,7 @@ copy_file(const char *src, const char *dest, int compress_level)
struct stat st;
int errnum;

tmp_name = format("%s.%s.XXXXXX", dest, tmp_string());
tmp_name = format("%s.%s", dest, tmp_string());

/* open destination file */
fd_out = mkstemp(tmp_name);
Expand Down Expand Up @@ -505,16 +505,16 @@ get_hostname(void)
}

/*
* Return a string to be used to distinguish temporary files. Also tries to
* cope with NFS by adding the local hostname.
* Return a string to be passed to mkstemp to create a temporary file. Also
* tries to cope with NFS by adding the local hostname.
*/
const char *
tmp_string(void)
{
static char *ret;

if (!ret) {
ret = format("%s.%u", get_hostname(), (unsigned)getpid());
ret = format("%s.%u.XXXXXX", get_hostname(), (unsigned)getpid());
}

return ret;
Expand Down Expand Up @@ -854,25 +854,6 @@ file_size(struct stat *st)
#endif
}

/*
* Create a file for writing. Creates parent directories if they don't exist.
*/
int
safe_create_wronly(const char *fname)
{
int fd = open(fname, O_WRONLY | O_CREAT | O_EXCL | O_BINARY, 0666);
if (fd == -1 && errno == ENOENT) {
/*
* Only make sure parent directories exist when have failed to open the
* file -- this saves stat() calls.
*/
if (create_parent_dirs(fname) == 0) {
fd = open(fname, O_RDWR | O_CREAT | O_EXCL | O_BINARY, 0666);
}
}
return fd;
}

/* Format a size as a human-readable string. Caller frees. */
char *
format_human_readable_size(uint64_t v)
Expand Down Expand Up @@ -1043,12 +1024,13 @@ strtok_r(char *str, const char *delim, char **saveptr)

/* create an empty file */
int
create_empty_file(const char *fname)
create_empty_file(char *fname)
{
int fd;

fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC|O_EXCL|O_BINARY, 0666);
fd = mkstemp(fname);
if (fd == -1) {
cc_log("Failed to create %s: %s", fname, strerror(errno));
return -1;
}
close(fd);
Expand Down Expand Up @@ -1289,7 +1271,10 @@ x_unlink(const char *path)
goto out;
}
if (unlink(tmp_name) == -1) {
result = -1;
/* If it was released in a race, that's OK. */
if (errno != ENOENT) {
result = -1;
}
}
out:
free(tmp_name);
Expand Down

0 comments on commit f49770a

Please sign in to comment.