diff options
author | 2015-02-03 21:58:53 -0800 | |
---|---|---|
committer | 2015-02-03 23:17:12 -0800 | |
commit | 05618967516ca807d4deadea05e90b439c3f2e7c (patch) | |
tree | f8ca8ce2ea0791c47beac372d103d36d937701b5 /src/core/support/file_posix.c | |
parent | 026a417defcd13d0ae5e8a8ddb67c18ff02fa142 (diff) |
Addressing comments.
The new gpr_tmpfile API is actually much nicer to use. Thanks Nico!
Diffstat (limited to 'src/core/support/file_posix.c')
-rw-r--r-- | src/core/support/file_posix.c | 25 |
1 files changed, 22 insertions, 3 deletions
diff --git a/src/core/support/file_posix.c b/src/core/support/file_posix.c index a156e7703c..a763fbcda2 100644 --- a/src/core/support/file_posix.c +++ b/src/core/support/file_posix.c @@ -54,15 +54,26 @@ #include <string.h> #include <unistd.h> +#include <grpc/support/alloc.h> #include <grpc/support/log.h> -FILE *gpr_tmpfile(char *template) { +#include "src/core/support/string.h" + +FILE *gpr_tmpfile(const char *prefix, char **tmp_filename) { FILE *result = NULL; - int fd = mkstemp(template); + char *template; + int fd; + + if (tmp_filename != NULL) *tmp_filename = NULL; + + gpr_asprintf(&template, "%s_XXXXXX", prefix); + GPR_ASSERT(template != NULL); + + fd = mkstemp(template); if (fd == -1) { gpr_log(GPR_ERROR, "mkstemp failed for template %s with error %s.", template, strerror(errno)); - return NULL; + goto end; } result = fdopen(fd, "w+"); if (result == NULL) { @@ -70,6 +81,14 @@ FILE *gpr_tmpfile(char *template) { template, fd, strerror(errno)); unlink(template); close(fd); + goto end; + } + +end: + if (result != NULL && tmp_filename != NULL) { + *tmp_filename = template; + } else { + gpr_free(template); } return result; } |