Home | History | Annotate | Download | only in testing
      1 // Copyright 2014 The Android Open Source Project
      2 //
      3 // This software is licensed under the terms of the GNU General Public
      4 // License version 2, as published by the Free Software Foundation, and
      5 // may be copied, distributed, and modified under those terms.
      6 //
      7 // This program is distributed in the hope that it will be useful,
      8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
      9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     10 // GNU General Public License for more details.
     11 
     12 #include "android/filesystems/testing/TestSupport.h"
     13 
     14 #include "android/base/EintrWrapper.h"
     15 #include "android/base/Log.h"
     16 
     17 #include <stdlib.h>
     18 
     19 #ifdef _WIN32
     20 #include <windows.h>
     21 #else
     22 #include <unistd.h>
     23 #endif
     24 
     25 namespace android {
     26 namespace testing {
     27 
     28 std::string CreateTempFilePath() {
     29 #ifdef _WIN32
     30     char tempDir[MAX_PATH];
     31     DWORD ret = GetTempPath(MAX_PATH, tempDir);
     32     CHECK(ret > 0 && ret < MAX_PATH)
     33             << "Could not get temporary directory path";
     34 
     35     std::string result;
     36     result.resize(MAX_PATH);
     37     UINT ret2 = GetTempFileName(tempDir, "emulator-test-", 0,  &result[0]);
     38     CHECK(ret2 != ERROR_BUFFER_OVERFLOW) << "Could not create temporary file name";
     39     result.resize(::strlen(result.c_str()));
     40     return result;
     41 #else
     42     std::string result("/tmp/emulator-test.XXXXXX");
     43     int ret = HANDLE_EINTR(mkstemp(&result[0]));
     44     PCHECK(ret >= 0) << "Could not create temporary filepath";
     45     ::close(ret);
     46     return result;
     47 #endif
     48 }
     49 
     50 }  // namespace testing
     51 }  // namespace android
     52