• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 //
3 // Copyright 2015 gRPC authors.
4 //
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
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16 //
17 //
18 
19 #include <grpc/support/port_platform.h>
20 
21 #ifdef GPR_WINDOWS_TMPFILE
22 
23 #include <grpc/support/alloc.h>
24 #include <grpc/support/string_util.h>
25 #include <io.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <tchar.h>
29 
30 #include "src/core/util/crash.h"
31 #include "src/core/util/tchar.h"
32 #include "src/core/util/tmpfile.h"
33 
gpr_tmpfile(const char * prefix,char ** tmp_filename_out)34 FILE* gpr_tmpfile(const char* prefix, char** tmp_filename_out) {
35   FILE* result = NULL;
36   TCHAR tmp_path[MAX_PATH];
37   TCHAR tmp_filename[MAX_PATH];
38   DWORD status;
39   UINT success;
40 
41   if (tmp_filename_out != NULL) *tmp_filename_out = NULL;
42 
43   // Convert our prefix to TCHAR.
44   grpc_core::TcharString template_string = grpc_core::CharToTchar(prefix);
45 
46   // Get the path to the best temporary folder available.
47   status = GetTempPath(MAX_PATH, tmp_path);
48   if (status == 0 || status > MAX_PATH) goto end;
49 
50   // Generate a unique filename with our template + temporary path.
51   success = GetTempFileName(tmp_path, template_string.c_str(), 0, tmp_filename);
52   if (!success) goto end;
53 
54   // Open a file there.
55   if (_tfopen_s(&result, tmp_filename, TEXT("wb+")) != 0) goto end;
56 
57 end:
58   if (result && tmp_filename_out) {
59     *tmp_filename_out =
60         gpr_strdup(grpc_core::TcharToChar(tmp_filename).c_str());
61   }
62 
63   return result;
64 }
65 
66 #endif  // GPR_WINDOWS_TMPFILE
67