Julien Boeuf | 026a417 | 2015-02-02 18:36:37 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | * Copyright 2015 gRPC authors. |
Julien Boeuf | 026a417 | 2015-02-02 18:36:37 -0800 | [diff] [blame] | 4 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 5 | * 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 Boeuf | 026a417 | 2015-02-02 18:36:37 -0800 | [diff] [blame] | 8 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
Julien Boeuf | 026a417 | 2015-02-02 18:36:37 -0800 | [diff] [blame] | 10 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 11 | * 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 Boeuf | 026a417 | 2015-02-02 18:36:37 -0800 | [diff] [blame] | 16 | * |
| 17 | */ |
| 18 | |
| 19 | #include <grpc/support/port_platform.h> |
| 20 | |
Yuchen Zeng | 4594bd9 | 2016-05-31 14:06:01 -0700 | [diff] [blame] | 21 | #ifdef GPR_WINDOWS_TMPFILE |
Julien Boeuf | 026a417 | 2015-02-02 18:36:37 -0800 | [diff] [blame] | 22 | |
Julien Boeuf | 026a417 | 2015-02-02 18:36:37 -0800 | [diff] [blame] | 23 | #include <io.h> |
| 24 | #include <stdio.h> |
| 25 | #include <string.h> |
Nicolas "Pixel" Noble | d6dcec5 | 2015-02-05 23:28:17 -0800 | [diff] [blame] | 26 | #include <tchar.h> |
Julien Boeuf | 026a417 | 2015-02-02 18:36:37 -0800 | [diff] [blame] | 27 | |
Julien Boeuf | 0561896 | 2015-02-03 21:58:53 -0800 | [diff] [blame] | 28 | #include <grpc/support/alloc.h> |
Julien Boeuf | 026a417 | 2015-02-02 18:36:37 -0800 | [diff] [blame] | 29 | #include <grpc/support/log.h> |
Masood Malekghassemi | 701af60 | 2015-06-03 15:01:17 -0700 | [diff] [blame] | 30 | #include <grpc/support/string_util.h> |
Julien Boeuf | 026a417 | 2015-02-02 18:36:37 -0800 | [diff] [blame] | 31 | |
Yuchen Zeng | 12dfdc3 | 2016-04-26 22:05:41 -0700 | [diff] [blame] | 32 | #include "src/core/lib/support/string_windows.h" |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 33 | #include "src/core/lib/support/tmpfile.h" |
Nicolas "Pixel" Noble | d6dcec5 | 2015-02-05 23:28:17 -0800 | [diff] [blame] | 34 | |
| 35 | FILE *gpr_tmpfile(const char *prefix, char **tmp_filename_out) { |
Julien Boeuf | 026a417 | 2015-02-02 18:36:37 -0800 | [diff] [blame] | 36 | FILE *result = NULL; |
Nicolas "Pixel" Noble | d6dcec5 | 2015-02-05 23:28:17 -0800 | [diff] [blame] | 37 | LPTSTR template_string = NULL; |
| 38 | TCHAR tmp_path[MAX_PATH]; |
| 39 | TCHAR tmp_filename[MAX_PATH]; |
| 40 | DWORD status; |
| 41 | UINT success; |
Julien Boeuf | 0561896 | 2015-02-03 21:58:53 -0800 | [diff] [blame] | 42 | |
Nicolas "Pixel" Noble | d6dcec5 | 2015-02-05 23:28:17 -0800 | [diff] [blame] | 43 | if (tmp_filename_out != NULL) *tmp_filename_out = NULL; |
Julien Boeuf | 0561896 | 2015-02-03 21:58:53 -0800 | [diff] [blame] | 44 | |
Nicolas "Pixel" Noble | d6dcec5 | 2015-02-05 23:28:17 -0800 | [diff] [blame] | 45 | /* Convert our prefix to TCHAR. */ |
| 46 | template_string = gpr_char_to_tchar(prefix); |
| 47 | GPR_ASSERT(template_string); |
Julien Boeuf | 026a417 | 2015-02-02 18:36:37 -0800 | [diff] [blame] | 48 | |
Nicolas "Pixel" Noble | d6dcec5 | 2015-02-05 23:28:17 -0800 | [diff] [blame] | 49 | /* 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 Boeuf | 0561896 | 2015-02-03 21:58:53 -0800 | [diff] [blame] | 59 | |
| 60 | end: |
Nicolas Noble | cfd6073 | 2015-03-18 16:27:43 -0700 | [diff] [blame] | 61 | if (result && tmp_filename_out) { |
Nicolas "Pixel" Noble | d6dcec5 | 2015-02-05 23:28:17 -0800 | [diff] [blame] | 62 | *tmp_filename_out = gpr_tchar_to_char(tmp_filename); |
Julien Boeuf | 0561896 | 2015-02-03 21:58:53 -0800 | [diff] [blame] | 63 | } |
Nicolas "Pixel" Noble | d6dcec5 | 2015-02-05 23:28:17 -0800 | [diff] [blame] | 64 | |
Nicolas Noble | df80ba8 | 2015-02-11 16:05:29 -0800 | [diff] [blame] | 65 | gpr_free(template_string); |
Julien Boeuf | 0561896 | 2015-02-03 21:58:53 -0800 | [diff] [blame] | 66 | return result; |
Julien Boeuf | 026a417 | 2015-02-02 18:36:37 -0800 | [diff] [blame] | 67 | } |
| 68 | |
Yuchen Zeng | 4594bd9 | 2016-05-31 14:06:01 -0700 | [diff] [blame] | 69 | #endif /* GPR_WINDOWS_TMPFILE */ |