|  | @@ -35,43 +35,48 @@
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  #ifdef GPR_WIN32
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -#include "src/core/support/file.h"
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  |  #include <io.h>
 | 
	
		
			
				|  |  |  #include <stdio.h>
 | 
	
		
			
				|  |  |  #include <string.h>
 | 
	
		
			
				|  |  | +#include <tchar.h>
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  #include <grpc/support/alloc.h>
 | 
	
		
			
				|  |  |  #include <grpc/support/log.h>
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -FILE *gpr_tmpfile(const char *prefix, char **tmp_filename) {
 | 
	
		
			
				|  |  | +#include "src/core/support/file.h"
 | 
	
		
			
				|  |  | +#include "src/core/support/string_win32.h"
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +FILE *gpr_tmpfile(const char *prefix, char **tmp_filename_out) {
 | 
	
		
			
				|  |  |    FILE *result = NULL;
 | 
	
		
			
				|  |  | -  char *template;
 | 
	
		
			
				|  |  | +  LPTSTR template_string = NULL;
 | 
	
		
			
				|  |  | +  TCHAR tmp_path[MAX_PATH];
 | 
	
		
			
				|  |  | +  TCHAR tmp_filename[MAX_PATH];
 | 
	
		
			
				|  |  | +  DWORD status;
 | 
	
		
			
				|  |  | +  UINT success;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -  if (tmp_filename != NULL) *tmp_filename = NULL;
 | 
	
		
			
				|  |  | +  if (tmp_filename_out != NULL) *tmp_filename_out = NULL;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -  gpr_asprintf(&template, "%s_XXXXXX", prefix);
 | 
	
		
			
				|  |  | -  GPR_ASSERT(template != NULL);
 | 
	
		
			
				|  |  | +  /* Convert our prefix to TCHAR. */
 | 
	
		
			
				|  |  | +  template_string = gpr_char_to_tchar(prefix);
 | 
	
		
			
				|  |  | +  GPR_ASSERT(template_string);
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -  /* _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.");
 | 
	
		
			
				|  |  | -    goto end;
 | 
	
		
			
				|  |  | -  }
 | 
	
		
			
				|  |  | -  if (fopen_s(&result, template, "wb+") != 0) {
 | 
	
		
			
				|  |  | -    gpr_log(GPR_ERROR, "Could not open file %s", template);
 | 
	
		
			
				|  |  | -    result = NULL;
 | 
	
		
			
				|  |  | -    goto end;
 | 
	
		
			
				|  |  | -  }
 | 
	
		
			
				|  |  | +  /* Get the path to the best temporary folder available. */
 | 
	
		
			
				|  |  | +  status = GetTempPath(MAX_PATH, tmp_path);
 | 
	
		
			
				|  |  | +  if (status == 0 || status > MAX_PATH) goto end;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +  /* Generate a unique filename with our template + temporary path. */
 | 
	
		
			
				|  |  | +  success = GetTempFileName(tmp_path, template_string, 0, tmp_filename);
 | 
	
		
			
				|  |  | +  if (!success) goto end;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +  /* Open a file there. */
 | 
	
		
			
				|  |  | +  if (_tfopen_s(&result, tmp_filename, TEXT("wb+")) != 0) goto end;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  end:
 | 
	
		
			
				|  |  | -  if (result != NULL && tmp_filename != NULL) {
 | 
	
		
			
				|  |  | -    *tmp_filename = template;
 | 
	
		
			
				|  |  | -  } else {
 | 
	
		
			
				|  |  | -    gpr_free(template);
 | 
	
		
			
				|  |  | +  if (result && tmp_filename) {
 | 
	
		
			
				|  |  | +    *tmp_filename_out = gpr_tchar_to_char(tmp_filename);
 | 
	
		
			
				|  |  |    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +  gpr_free(tmp_filename);
 | 
	
		
			
				|  |  |    return result;
 | 
	
		
			
				|  |  |  }
 | 
	
		
			
				|  |  |  
 |