aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core/support
diff options
context:
space:
mode:
authorGravatar Julien Boeuf <jboeuf@google.com>2015-02-03 21:58:53 -0800
committerGravatar Julien Boeuf <jboeuf@google.com>2015-02-03 23:17:12 -0800
commit05618967516ca807d4deadea05e90b439c3f2e7c (patch)
treef8ca8ce2ea0791c47beac372d103d36d937701b5 /src/core/support
parent026a417defcd13d0ae5e8a8ddb67c18ff02fa142 (diff)
Addressing comments.
The new gpr_tmpfile API is actually much nicer to use. Thanks Nico!
Diffstat (limited to 'src/core/support')
-rw-r--r--src/core/support/env_posix.c3
-rw-r--r--src/core/support/env_win32.c3
-rw-r--r--src/core/support/file.h10
-rw-r--r--src/core/support/file_posix.c25
-rw-r--r--src/core/support/file_win32.c26
5 files changed, 51 insertions, 16 deletions
diff --git a/src/core/support/env_posix.c b/src/core/support/env_posix.c
index 4af5aea9b0..79b8fcd7d7 100644
--- a/src/core/support/env_posix.c
+++ b/src/core/support/env_posix.c
@@ -54,7 +54,8 @@ char *gpr_getenv(const char *name) {
}
void gpr_setenv(const char *name, const char *value) {
- GPR_ASSERT(setenv(name, value, 1) == 0);
+ int res = setenv(name, value, 1);
+ GPR_ASSERT(res == 0);
}
#endif /* GPR_POSIX_ENV */
diff --git a/src/core/support/env_win32.c b/src/core/support/env_win32.c
index 7964409364..a31fa79d68 100644
--- a/src/core/support/env_win32.c
+++ b/src/core/support/env_win32.c
@@ -53,7 +53,8 @@ char *gpr_getenv(const char *name) {
}
void gpr_setenv(const char *name, const char *value) {
- GPR_ASSERT(_putenv_s(name, value) == 0);
+ errno_t res = _putenv_s(name, value);
+ GPR_ASSERT(res == 0);
}
#endif /* GPR_WIN32 */
diff --git a/src/core/support/file.h b/src/core/support/file.h
index a9d81498e5..92f420e7ce 100644
--- a/src/core/support/file.h
+++ b/src/core/support/file.h
@@ -48,11 +48,11 @@ extern "C" {
will be set to 1 in case of success and 0 in case of failure. */
gpr_slice gpr_load_file(const char *filename, int *success);
-/* Creates a temporary file from a template.
- The last six characters of template must be "XXXXXX" and these are replaced
- with a string that makes the filename unique. Since it will be modified,
- template must not be a string constant. */
-FILE *gpr_tmpfile(char *template);
+/* Creates a temporary file from a prefix.
+ If tmp_filename is not NULL, *tmp_filename is assigned the name of the
+ created file and it is the responsibility of the caller to gpr_free it
+ unless an error occurs in which case it will be set to NULL. */
+FILE *gpr_tmpfile(const char *prefix, char **tmp_filename);
#ifdef __cplusplus
}
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;
}
diff --git a/src/core/support/file_win32.c b/src/core/support/file_win32.c
index b85422c372..d415281e0d 100644
--- a/src/core/support/file_win32.c
+++ b/src/core/support/file_win32.c
@@ -41,24 +41,38 @@
#include <stdio.h>
#include <string.h>
+#include <grpc/support/alloc.h>
#include <grpc/support/log.h>
-FILE *gpr_tmpfile(char *template) {
+FILE *gpr_tmpfile(const char *prefix, char **tmp_filename) {
FILE *result = NULL;
+ char *template;
+
+ if (tmp_filename != NULL) *tmp_filename = NULL;
+
+ gpr_asprintf(&template, "%s_XXXXXX", prefix);
+ GPR_ASSERT(template != NULL);
/* _mktemp_s can only create a maximum of 26 file names for any combination of
base and template values which is kind of sad... We may revisit this
function later to have something better... */
if (_mktemp_s(template, strlen(template) + 1) != 0) {
gpr_log(LOG_ERROR, "Could not create tmp file.");
- return NULL;
+ goto end;
}
- if (fopen_s(&result, template, "wb+") == 0) {
- return result;
- } else {
+ if (fopen_s(&result, template, "wb+") != 0) {
gpr_log(GPR_ERROR, "Could not open file %s", template);
- return NULL;
+ result = NULL;
+ goto end;
+ }
+
+end:
+ if (result != NULL && tmp_filename != NULL) {
+ *tmp_filename = template;
+ } else {
+ gpr_free(template);
}
+ return result;
}
#endif /* GPR_WIN32 */