blob: 2b10bcde4378bc4f500ab5bb723d40cffac25133 [file] [log] [blame]
Julien Boeuf026a4172015-02-02 18:36:37 -08001/*
2 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003 * Copyright 2015 gRPC authors.
Julien Boeuf026a4172015-02-02 18:36:37 -08004 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
Julien Boeuf026a4172015-02-02 18:36:37 -08008 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009 * http://www.apache.org/licenses/LICENSE-2.0
Julien Boeuf026a4172015-02-02 18:36:37 -080010 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
Julien Boeuf026a4172015-02-02 18:36:37 -080016 *
17 */
18
19#include <grpc/support/port_platform.h>
20
Yuchen Zeng4594bd92016-05-31 14:06:01 -070021#ifdef GPR_WINDOWS_TMPFILE
Julien Boeuf026a4172015-02-02 18:36:37 -080022
Julien Boeuf026a4172015-02-02 18:36:37 -080023#include <io.h>
24#include <stdio.h>
25#include <string.h>
Nicolas "Pixel" Nobled6dcec52015-02-05 23:28:17 -080026#include <tchar.h>
Julien Boeuf026a4172015-02-02 18:36:37 -080027
Julien Boeuf05618962015-02-03 21:58:53 -080028#include <grpc/support/alloc.h>
Julien Boeuf026a4172015-02-02 18:36:37 -080029#include <grpc/support/log.h>
Masood Malekghassemi701af602015-06-03 15:01:17 -070030#include <grpc/support/string_util.h>
Julien Boeuf026a4172015-02-02 18:36:37 -080031
Yuchen Zeng12dfdc32016-04-26 22:05:41 -070032#include "src/core/lib/support/string_windows.h"
Craig Tiller9533d042016-03-25 17:11:06 -070033#include "src/core/lib/support/tmpfile.h"
Nicolas "Pixel" Nobled6dcec52015-02-05 23:28:17 -080034
Craig Tillerbaa14a92017-11-03 09:09:36 -070035FILE* gpr_tmpfile(const char* prefix, char** tmp_filename_out) {
36 FILE* result = NULL;
Nicolas "Pixel" Nobled6dcec52015-02-05 23:28:17 -080037 LPTSTR template_string = NULL;
38 TCHAR tmp_path[MAX_PATH];
39 TCHAR tmp_filename[MAX_PATH];
40 DWORD status;
41 UINT success;
Julien Boeuf05618962015-02-03 21:58:53 -080042
Nicolas "Pixel" Nobled6dcec52015-02-05 23:28:17 -080043 if (tmp_filename_out != NULL) *tmp_filename_out = NULL;
Julien Boeuf05618962015-02-03 21:58:53 -080044
Nicolas "Pixel" Nobled6dcec52015-02-05 23:28:17 -080045 /* Convert our prefix to TCHAR. */
46 template_string = gpr_char_to_tchar(prefix);
47 GPR_ASSERT(template_string);
Julien Boeuf026a4172015-02-02 18:36:37 -080048
Nicolas "Pixel" Nobled6dcec52015-02-05 23:28:17 -080049 /* Get the path to the best temporary folder available. */
50 status = GetTempPath(MAX_PATH, tmp_path);
51 if (status == 0 || status > MAX_PATH) goto end;
52
53 /* Generate a unique filename with our template + temporary path. */
54 success = GetTempFileName(tmp_path, template_string, 0, tmp_filename);
55 if (!success) goto end;
56
57 /* Open a file there. */
58 if (_tfopen_s(&result, tmp_filename, TEXT("wb+")) != 0) goto end;
Julien Boeuf05618962015-02-03 21:58:53 -080059
60end:
Nicolas Noblecfd60732015-03-18 16:27:43 -070061 if (result && tmp_filename_out) {
Nicolas "Pixel" Nobled6dcec52015-02-05 23:28:17 -080062 *tmp_filename_out = gpr_tchar_to_char(tmp_filename);
Julien Boeuf05618962015-02-03 21:58:53 -080063 }
Nicolas "Pixel" Nobled6dcec52015-02-05 23:28:17 -080064
Nicolas Nobledf80ba82015-02-11 16:05:29 -080065 gpr_free(template_string);
Julien Boeuf05618962015-02-03 21:58:53 -080066 return result;
Julien Boeuf026a4172015-02-02 18:36:37 -080067}
68
Yuchen Zeng4594bd92016-05-31 14:06:01 -070069#endif /* GPR_WINDOWS_TMPFILE */