Home | History | Annotate | Download | only in windows
      1 /*
      2  * Copyright (c) 2015, Intel Corporation
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without modification,
      6  * are permitted provided that the following conditions are met:
      7  *
      8  * 1. Redistributions of source code must retain the above copyright notice, this
      9  * list of conditions and the following disclaimer.
     10  *
     11  * 2. Redistributions in binary form must reproduce the above copyright notice,
     12  * this list of conditions and the following disclaimer in the documentation and/or
     13  * other materials provided with the distribution.
     14  *
     15  * 3. Neither the name of the copyright holder nor the names of its contributors
     16  * may be used to endorse or promote products derived from this software without
     17  * specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     21  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     22  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
     23  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
     26  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 #include "TmpFile.hpp"
     32 
     33 #include <memory>
     34 
     35 #include <Windows.h>
     36 
     37 using std::to_string;
     38 
     39 namespace parameterFramework
     40 {
     41 namespace utility
     42 {
     43 
     44 /** Format an error code returned by GetLastError to a human readable string. */
     45 static std::string formatError(DWORD error)
     46 {
     47     LPTSTR formatedError = nullptr; // Pointer to the output buffer
     48 
     49     FormatMessage(
     50         FORMAT_MESSAGE_FROM_SYSTEM |         // use system message tables to retrieve error text
     51             FORMAT_MESSAGE_ALLOCATE_BUFFER | // allocate buffer on local heap for error text
     52             FORMAT_MESSAGE_IGNORE_INSERTS,   // no insertion parameters
     53         NULL,                                // unused with FORMAT_MESSAGE_FROM_SYSTEM
     54         error,                               // the error to format
     55         MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Use default language
     56         (LPTSTR)&formatedError,                    // output a pointer to the formated string
     57         0,                                         // minimum size for output buffer
     58         NULL);                                     // No arguments
     59 
     60     // Release memory allocated by FormatMessage() on exit
     61     // Ignore LocalFree failure
     62     auto localFree = [](char *ptr) { LocalFree(ptr); };
     63     std::unique_ptr<char, decltype(localFree)> guard{formatedError, localFree};
     64 
     65     if (formatedError == nullptr) {
     66         return "Could not format error " + to_string(error) + ": " + to_string(::GetLastError());
     67     }
     68     return formatedError;
     69 }
     70 
     71 std::string TmpFile::mktmp()
     72 {
     73     char directory[] = ".";
     74     char prefix[] = "pfw"; // GetTempFileName uses up to the first three characters
     75     char path[MAX_PATH + 1];
     76     if (::GetTempFileName(directory, prefix, 0, path) == 0) {
     77         auto error = ::GetLastError();
     78 
     79         auto message = std::string() + "Could not create a tmp file in \"" + directory +
     80                        "\", with prefix \"" + prefix + "\": (" + to_string(error) + ") " +
     81                        formatError(error);
     82         throw std::runtime_error(message);
     83     }
     84 
     85     return path;
     86 }
     87 
     88 } // utility
     89 } // parameterFramework
     90