temporary_filename.h
Go to the documentation of this file.
1 // Portions Copyright (c) 2020 Analog Devices, Inc.
2 //
3 // Copyright 2008, Google Inc.
4 // All rights reserved.
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 // * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 #include <cstdint>
32 #include <cstdio>
33 #ifdef USE_GLOG
34 #include <glog/logging.h>
35 #else
36 #include <aditof/log.h>
37 #endif
38 #include <string>
39 
40 #if _WIN32
41 #include <fcntl.h>
42 #include <io.h>
43 #include <windows.h>
44 #else
45 #include <unistd.h>
46 #endif // _WIN32
47 
49  int fhandle = -1;
50 #ifdef _WIN32
51  char temp_dir_path[MAX_PATH + 1] = {'\0'}; // NOLINT
52  char temp_file_path[MAX_PATH + 1] = {'\0'}; // NOLINT
53  ::GetTempPathA(sizeof(temp_dir_path), temp_dir_path);
54  const UINT success = ::GetTempFileNameA(temp_dir_path, prefix.c_str(),
55  0, // Generate unique file name.
56  temp_file_path);
57  if (success == 0) {
58  LOG(ERROR) << "Unable to create a temporary file in " << temp_dir_path;
59  }
60  errno_t err = _sopen_s(&fhandle, temp_file_path, _O_BINARY, _SH_DENYNO,
61  _S_IREAD | _S_IWRITE);
62  if (err == 0) {
63  _close(fhandle);
64  }
65 #else
66  const int max_path = 4096;
67  char temp_file_path[max_path] = {0};
68  if (prefix.length() >= 512) {
69  LOG(ERROR) << "Unable to create a temporary file with prefix "
70  << prefix;
71  return "";
72  }
73 
74  // There's no guarantee that a test has write access to the current
75  // directory, so we create the temporary file in the /tmp directory
76  // instead. We use /tmp on most systems, and /sdcard on Android.
77  // That's because Android doesn't have /tmp.
78 #if OS_LINUX_ANDROID
79  // Note: Android applications are expected to call the framework's
80  // Context.getExternalStorageDirectory() method through JNI to get
81  // the location of the world-writable SD Card directory. However,
82  // this requires a Context handle, which cannot be retrieved
83  // globally from native code. Doing so also precludes running the
84  // code as part of a regular standalone executable, which doesn't
85  // run in a Dalvik process (e.g. when running it through 'adb shell').
86  //
87  // The location /sdcard is directly accessible from native code
88  // and is the only location (unofficially) supported by the Android
89  // team. It's generally a symlink to the real SD Card mount point
90  // which can be /mnt/sdcard, /mnt/sdcard0, /system/media/sdcard, or
91  // other OEM-customized locations. Never rely on these, and always
92  // use /sdcard.
93  strcat(temp_file_path, "/sdcard/");
94 #else
95  strcat(temp_file_path, "/tmp/");
96  //char temp_file_path[] = "/tmp/adi_tofcam_.XXXXXX";
97 #endif // OS_LINUX_ANDROID
98  strcat(temp_file_path, prefix.c_str());
99  strcat(temp_file_path, ".XXXXXX");
100  fhandle = mkstemp(temp_file_path);
101  if (fhandle == -1) {
102  close(fhandle);
103  }
104 #endif
105 
106  std::string filename = "";
107  if (fhandle == -1) {
108  LOG(ERROR) << "Unable to open temporary file " << temp_file_path;
109  } else {
110  filename = temp_file_path;
111  }
112 
113  return filename;
114 }
ERROR
const int ERROR
Definition: log_severity.h:60
io.h
log.h
string
GLsizei const GLchar *const * string
Definition: glcorearb.h:3083
getTempFilename
std::string getTempFilename(std::string prefix)
Definition: temporary_filename.h:48
prefix
static const char prefix[]
Definition: test_pair_ipc.cpp:26
err
static UPB_NORETURN void err(tarjan *t)
Definition: ruby/ext/google/protobuf_c/upb.c:5856
LOG
#define LOG(x)
Definition: sdk/include/aditof/log.h:72


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:59