Ejemplo n.º 1
0
static HRESULT WINAPI InstallCallback_OnStartBinding(IBindStatusCallback *iface,
        DWORD dwReserved, IBinding *pib)
{
    WCHAR tmp_dir[MAX_PATH];

    set_status(IDS_DOWNLOADING);

    GetTempPathW(sizeof(tmp_dir)/sizeof(WCHAR), tmp_dir);

    tmp_file_name = heap_alloc(MAX_PATH*sizeof(WCHAR));
    GetTempFileNameW(tmp_dir, NULL, 0, tmp_file_name);

    TRACE("creating temp file %s\n", debugstr_w(tmp_file_name));

    tmp_file = CreateFileW(tmp_file_name, GENERIC_WRITE, 0, NULL, 
                           CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

    if(tmp_file == INVALID_HANDLE_VALUE) {
        ERR("Could not create file: %d\n", GetLastError());
        clean_up();
        return E_FAIL;
    }

    return S_OK;
}
bool safeCreateFile(const String& path, CFDataRef data)
{
    // Create a temporary file.
    WCHAR tempDirPath[MAX_PATH];
    if (!GetTempPathW(WTF_ARRAY_LENGTH(tempDirPath), tempDirPath))
        return false;

    WCHAR tempPath[MAX_PATH];
    if (!GetTempFileNameW(tempDirPath, L"WEBKIT", 0, tempPath))
        return false;

    HANDLE tempFileHandle = CreateFileW(tempPath, GENERIC_READ | GENERIC_WRITE, 0, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
    if (tempFileHandle == INVALID_HANDLE_VALUE)
        return false;

    // Write the data to this temp file.
    DWORD written;
    if (!WriteFile(tempFileHandle, CFDataGetBytePtr(data), static_cast<DWORD>(CFDataGetLength(data)), &written, 0))
        return false;

    CloseHandle(tempFileHandle);

    // Copy the temp file to the destination file.
    String destination = path;
    if (!MoveFileExW(tempPath, destination.charactersWithNullTermination(), MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED))
        return false;

    return true;
}
Ejemplo n.º 3
0
void GenerateTempFileName ( wchar_t alter * tempFileNameOut, wchar_t const * extension ) {
#ifdef _MSC_VER
	wchar_t temp_dir[_MAX_DIR];
	DWORD dir_len = 0;
	dir_len = GetTempPathW(_MAX_DIR,  temp_dir);
	assert(dir_len != 0);	
	assert(dir_len <= _MAX_DIR);
	UINT res = 0;
	res = GetTempFileNameW(temp_dir, L"HOOPS", 0, tempFileNameOut);
	assert(res != 0);
	// if extension is specified replace .tmp with user-specified value
	if (extension) {
		wchar_t *old_extension = wcsrchr(tempFileNameOut, L'.');
		if (extension[0] == L'.')
			old_extension[0] = 0;
		else
			old_extension[1] = 0;
		wcscat(tempFileNameOut, extension);
	}
#else
	char temp_template[TEMPFILE_UTILS_BUFFER_SIZE];
	
	if (extension)
		GenerateTempFileName(temp_template, reinterpret_cast<char const *>(H_UTF8(extension).encodedText()));
	else
		GenerateTempFileName(temp_template);
	
	if (temp_template[0] == 0)
		tempFileNameOut[0] = 0;
	else
		wcscpy(tempFileNameOut, H_WCS(temp_template).encodedText());
#endif
}
/* good1() uses if(staticFalse) instead of if(staticTrue) */
static void good1()
{
    if(staticFalse)
    {
        /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
        printLine("Benign, fixed string");
    }
    else
    {
        {
            wchar_t filename[MAX_PATH] = L"";
            int fileDesc;
            /* FIX: Passing a non-zero value in for uUnique prevents GetTempFileName from creating
             * and then closing the file, at the cost of no longer guaranteeing the name is unique. */
            /* INCIDENTAL CWE338 Weak PRNG - use of rand() as a PRNG */
            if (GetTempFileNameW(L".", L"good", rand() + 1, filename) == 0)
            {
                exit(1);
            }
            printWLine(filename);
            /* FIX: Open a temporary file using open() and the O_CREAT and O_EXCL flags
            * NOTE: This is not a perfect solution, but it is the base case scenario */
            fileDesc = OPEN(filename, O_RDWR|O_CREAT|O_EXCL, S_IREAD|S_IWRITE);
            if (fileDesc != -1)
            {
                printLine("Temporary file was opened...now closing file");
                CLOSE(fileDesc);
            }
        }
    }
}
void CWE377_Insecure_Temporary_File__wchar_t_w32GetTempFileName_15_bad()
{
    switch(6)
    {
    case 6:
    {
        wchar_t filename[MAX_PATH] = L"";
        int fileDesc;
        /* FLAW: Passing 0 in for uUnique tells GetTempFileName to create and then close the file,
        * leading to an inescapable race condition when we try to open it again. */
        if (GetTempFileNameW(L".", L"bad", 0, filename) == 0)
        {
            exit(1);
        }
        printWLine(filename);
        /* FLAW: Open a temporary file using open() and flags that do not prevent a race condition */
        fileDesc = OPEN(filename, O_RDWR|O_CREAT, S_IREAD|S_IWRITE);
        if (fileDesc != -1)
        {
            printLine("Temporary file was opened...now closing file");
            CLOSE(fileDesc);
        }
    }
    break;
    default:
        /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
        printLine("Benign, fixed string");
        break;
    }
}
Ejemplo n.º 6
0
int _vscprintf(const char *format, va_list argptr)
{
  FILE* f = NULL;
  int result = 0; 
  WCHAR path [MAX_PATH];
  WCHAR wtmpfile [MAX_PATH];
  char atmpfile [MAX_PATH];

  GetTempPath(MAX_PATH,path);
  if (GetTempFileNameW(path,L"tmp",0,wtmpfile) == 0)
    return NULL;
  wcstombs(atmpfile,wtmpfile,MAX_PATH);
  f = fopen (atmpfile,"w+b");
  if (!f)
  {
    errno = EINVAL;
    return -1;
  }
  vfprintf(f, format, argptr);
  fflush(f);
  fseek(f, 0, SEEK_END);
  result = ftell(f);
  fclose(f);
  DeleteFileW(wtmpfile);
  return result;
}
Ejemplo n.º 7
0
TempStream::TempStream(const std::string &prefix, bool deleteOnClose,
                       IOManager *ioManager, Scheduler *scheduler)
{
    std::string tempdir;
    bool absolutePath =
#ifdef WINDOWS
        (prefix.size() >= 2 && (prefix[1] == ':' || prefix[1] == '\\')) ||
        (!prefix.empty() && prefix[0] == '\\');
#else
        !prefix.empty() && prefix[0] == '/';
#endif
    if (!absolutePath)
        tempdir = g_tempDir->val();
#ifdef WINDOWS
    std::wstring wtempdir = toUtf16(tempdir);
    if (!absolutePath && wtempdir.empty()) {
        wtempdir.resize(MAX_PATH);
        DWORD len = GetTempPathW(MAX_PATH, &wtempdir[0]);
        if (len == 0)
            wtempdir = L".";
        else
            wtempdir.resize(len);
    }
    std::wstring prefixW = toUtf16(prefix);
    size_t backslash = prefixW.rfind(L'\\');
    if (backslash != std::wstring::npos) {
        wtempdir += prefixW.substr(0, backslash);
        prefixW = prefixW.substr(backslash + 1);
    }
    std::wstring tempfile;
    tempfile.resize(MAX_PATH);
    UINT len = GetTempFileNameW(wtempdir.c_str(),
        prefixW.c_str(),
        0,
        &tempfile[0]);
    if (len == 0)
        MORDOR_THROW_EXCEPTION_FROM_LAST_ERROR_API("GetTempFileNameW");
    init(tempfile, FileStream::READWRITE,
        (FileStream::CreateFlags)(FileStream::OPEN |
            (deleteOnClose ? FileStream::DELETE_ON_CLOSE : 0)),
        ioManager, scheduler);
#else
    if (!absolutePath && tempdir.empty())
        tempdir = "/tmp/" + prefix + "XXXXXX";
    else if (!absolutePath)
        tempdir += prefix + "XXXXXX";
    else
        tempdir = prefix + "XXXXXX";
    int fd = mkstemp(&tempdir[0]);
    if (fd < 0)
        MORDOR_THROW_EXCEPTION_FROM_LAST_ERROR_API("mkstemp");
    init(fd, ioManager, scheduler);
    if (deleteOnClose) {
        int rc = unlink(tempdir.c_str());
        if (rc != 0)
            MORDOR_THROW_EXCEPTION_FROM_LAST_ERROR_API("unlink");
    }
    m_path = tempdir;
#endif
}
Ejemplo n.º 8
0
static void test_invalid_callbackW(void)
{
    BOOL ret;
    WCHAR source[MAX_PATH], temp[MAX_PATH];

    ret = SetupIterateCabinetW(NULL, 0, NULL, NULL);
    if (!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
    {
        win_skip("SetupIterateCabinetW is not available\n");
        return;
    }

    GetTempPathW(sizeof(temp)/sizeof(WCHAR), temp);
    GetTempFileNameW(temp, docW, 0, source);

    create_source_fileW(source, comp_cab_zip_multi, sizeof(comp_cab_zip_multi));

    SetLastError(0xdeadbeef);
    ret = SetupIterateCabinetW(source, 0, NULL, NULL);
    ok(!ret, "Expected SetupIterateCabinetW to return 0, got %d\n", ret);
    ok(GetLastError() == ERROR_INVALID_DATA,
       "Expected GetLastError() to return ERROR_INVALID_DATA, got %u\n",
       GetLastError());

    SetLastError(0xdeadbeef);
    ret = SetupIterateCabinetW(source, 0, crash_callbackW, NULL);
    ok(!ret, "Expected SetupIterateCabinetW to return 0, got %d\n", ret);
    ok(GetLastError() == ERROR_INVALID_DATA,
       "Expected GetLastError() to return ERROR_INVALID_DATA, got %u\n",
       GetLastError());

    DeleteFileW(source);
}
Ejemplo n.º 9
0
std::wstring DVLib::GetTemporaryFileNameW()
{	
	wchar_t tf[MAX_PATH];
    CHECK_WIN32_BOOL(GetTempFileNameW(GetTemporaryDirectoryW().c_str(), L"DV", 0, tf),
		L"GetTempFileNameW");
	return tf;
}
Ejemplo n.º 10
0
static void test_simple_enumerationW(void)
{
    BOOL ret;
    WCHAR source[MAX_PATH], temp[MAX_PATH];
    int enum_count = 0;

    ret = SetupIterateCabinetW(NULL, 0, NULL, NULL);
    if (!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
    {
        win_skip("SetupIterateCabinetW is not available\n");
        return;
    }

    GetTempPathW(sizeof(temp)/sizeof(WCHAR), temp);
    GetTempFileNameW(temp, docW, 0, source);

    create_source_fileW(source, comp_cab_zip_multi, sizeof(comp_cab_zip_multi));

    ret = SetupIterateCabinetW(source, 0, simple_callbackW, &enum_count);
    ok(ret == 1, "Expected SetupIterateCabinetW to return 1, got %d\n", ret);
    ok(enum_count == sizeof(expected_files)/sizeof(WCHAR *),
       "Unexpectedly enumerated %d files\n", enum_count);

    DeleteFileW(source);
}
Ejemplo n.º 11
0
//============================================================================
static void FileSrvIpAddressCallback (
    ENetError       result,
    void *          param,
    const wchar_t     addr[]
) {
    NetCliGateKeeperDisconnect();

    if (IS_NET_ERROR(result)) {
        plString msg = plString::Format("FileSrvIpAddressRequest failed: %S", NetErrorToString(result));
        plStatusLog::AddLineS("patcher.log", msg.c_str());

        s_patchResult = result;
        s_downloadComplete = true;
    }
    
    // Start connecting to the server
    const char* caddr = hsWStringToString(addr);
    NetCliFileStartConnect(&caddr, 1, true);
    delete[] caddr;

    PathGetProgramDirectory(s_newPatcherFile, arrsize(s_newPatcherFile));
    GetTempFileNameW(s_newPatcherFile, kPatcherExeFilename, 0, s_newPatcherFile);
    plFileUtils::RemoveFile(s_newPatcherFile);

    NetCliFileManifestRequest(ManifestCallback, nil, s_manifest);
}
Ejemplo n.º 12
0
static BOOL create_file(WCHAR *filename, const DWORD *data, DWORD data_size)
{
    static WCHAR temp_dir[MAX_PATH];
    DWORD written;
    HANDLE file;

    if (!temp_dir[0])
        GetTempPathW(ARRAY_SIZE(temp_dir), temp_dir);
    GetTempFileNameW(temp_dir, NULL, 0, filename);
    file = CreateFileW(filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0);
    if (file == INVALID_HANDLE_VALUE)
        return FALSE;

    if (data)
    {
        WriteFile(file, data, data_size, &written, NULL);
        if (written != data_size)
        {
            CloseHandle(file);
            DeleteFileW(filename);
            return FALSE;
        }
    }
    CloseHandle(file);
    return TRUE;
}
Ejemplo n.º 13
0
static DWORD WINAPI download_proc(PVOID arg)
{
    WCHAR tmp_dir[MAX_PATH], tmp_file[MAX_PATH];
    HRESULT hres;

    GetTempPathW(sizeof(tmp_dir)/sizeof(WCHAR), tmp_dir);
    GetTempFileNameW(tmp_dir, NULL, 0, tmp_file);

    TRACE("using temp file %s\n", debugstr_w(tmp_file));

    hres = URLDownloadToFileW(NULL, GeckoUrl, tmp_file, 0, &InstallCallback);
    if(FAILED(hres)) {
        ERR("URLDownloadToFile failed: %08x\n", hres);
        return 0;
    }

    if(sha_check(tmp_file)) {
        install_file(tmp_file);
    }else {
        WCHAR message[256];

        if(LoadStringW(hApplet, IDS_INVALID_SHA, message, sizeof(message)/sizeof(WCHAR))) {
            MessageBoxW(NULL, message, NULL, MB_ICONERROR);
        }
    }

    DeleteFileW(tmp_file);
    EndDialog(install_dialog, 0);
    return 0;
}
Ejemplo n.º 14
0
/***********************************************************************
 *           start_dosbox
 */
static void start_dosbox( const char *appname, const char *args )
{
    static const WCHAR cfgW[] = {'c','f','g',0};
    const char *config_dir = wine_get_config_dir();
    WCHAR path[MAX_PATH], config[MAX_PATH];
    HANDLE file;
    char *p, *buffer, app[MAX_PATH];
    int i;
    int ret = 1;
    DWORD written, drives = GetLogicalDrives();
    char *dosbox = find_dosbox();

    if (!dosbox) return;
    if (!GetTempPathW( MAX_PATH, path )) return;
    if (!GetTempFileNameW( path, cfgW, 0, config )) return;
    if (!GetCurrentDirectoryW( MAX_PATH, path )) return;
    if (!GetShortPathNameA( appname, app, MAX_PATH )) return;
    GetShortPathNameW( path, path, MAX_PATH );
    file = CreateFileW( config, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0 );
    if (file == INVALID_HANDLE_VALUE) return;

    buffer = HeapAlloc( GetProcessHeap(), 0, sizeof("[autoexec]") +
                        sizeof("mount -z c") + sizeof("config -securemode") +
                        25 * (strlen(config_dir) + sizeof("mount c /dosdevices/c:")) +
                        4 * strlenW( path ) +
                        6 + strlen( app ) + strlen( args ) + 20 );
    p = buffer;
    p += sprintf( p, "[autoexec]\n" );
    for (i = 25; i >= 0; i--)
        if (!(drives & (1 << i)))
        {
            p += sprintf( p, "mount -z %c\n", 'a' + i );
            break;
        }
    for (i = 0; i <= 25; i++)
        if (drives & (1 << i))
            p += sprintf( p, "mount %c %s/dosdevices/%c:\n", 'a' + i, config_dir, 'a' + i );
    p += sprintf( p, "%c:\ncd ", path[0] );
    p += WideCharToMultiByte( CP_UNIXCP, 0, path + 2, -1, p, 4 * strlenW(path), NULL, NULL ) - 1;
    p += sprintf( p, "\nconfig -securemode\n" );
    p += sprintf( p, "%s %s\n", app, args );
    p += sprintf( p, "exit\n" );
    if (WriteFile( file, buffer, strlen(buffer), &written, NULL ) && written == strlen(buffer))
    {
        const char *args[5];
        char *config_file = wine_get_unix_file_name( config );
        args[0] = dosbox;
        args[1] = "-userconf";
        args[2] = "-conf";
        args[3] = config_file;
        args[4] = NULL;
        ret = _spawnvp( _P_WAIT, args[0], args );
    }
    CloseHandle( file );
    DeleteFileW( config );
    HeapFree( GetProcessHeap(), 0, buffer );
    ExitProcess( ret );
}
Ejemplo n.º 15
0
TempFile::TempFile() {
  Buffer<wchar_t> buf(MAX_PATH);
  DWORD len = GetTempPathW(static_cast<DWORD>(buf.size()), buf.data());
  CHECK(len <= buf.size());
  CHECK_SYS(len);
  wstring temp_path = wstring(buf.data(), len);
  CHECK_SYS(GetTempFileNameW(temp_path.c_str(), L"", 0, buf.data()));
  path.assign(buf.data());
}
Ejemplo n.º 16
0
UnicodeString make_temp_file() {
  UnicodeString temp_path;
  CHECK_API(GetTempPathW(MAX_PATH, temp_path.buf(MAX_PATH)) != 0);
  temp_path.set_size();
  UnicodeString temp_file_name;
  CHECK_API(GetTempFileNameW(temp_path.data(), L"wme", 0, temp_file_name.buf(MAX_PATH)) != 0);
  temp_file_name.set_size();
  return temp_file_name;
}
Ejemplo n.º 17
0
cvs::string CFileAccess::tempfilename(const char *prefix)
{
	cvs::wstring tempfile;
	tempfile.resize(MAX_PATH);
	GetTempFileNameW(wtempdir().c_str(),Win32Wide(prefix),0,(wchar_t*)tempfile.data());
	tempfile.resize(wcslen((wchar_t*)tempfile.data()));
	DeleteFileW(tempfile.c_str());
	return (const char *)Win32Narrow(tempfile.c_str());
}
Ejemplo n.º 18
0
 TestFile() {
   wchar_t temp_dir[MAX_PATH];
   DWORD res = GetTempPathW(MAX_PATH, temp_dir);
   assert(res > 0 && res < MAX_PATH);
   wchar_t temp_file_path[MAX_PATH];
   UINT res2 = GetTempFileNameW(temp_dir, L"", 0, temp_file_path);
   assert(res2 != 0);
   file_path_ = temp_file_path;
   Write("");
 }
Ejemplo n.º 19
0
/***********************************************************************
 *           GetTempFileNameA   (KERNEL32.@)
 */
UINT WINAPI
GetTempFileNameA(IN LPCSTR lpPathName,
                 IN LPCSTR lpPrefixString,
                 IN UINT uUnique,
                 OUT LPSTR lpTempFileName)
{
    UINT ID;
    NTSTATUS Status;
    LPWSTR lpTempFileNameW;
    PUNICODE_STRING lpPathNameW;
    ANSI_STRING TempFileNameStringA;
    UNICODE_STRING lpPrefixStringW, TempFileNameStringW;

    /* Convert strings */
    lpPathNameW = Basep8BitStringToStaticUnicodeString(lpPathName);
    if (!lpPathNameW)
    {
        return 0;
    }

    if (!Basep8BitStringToDynamicUnicodeString(&lpPrefixStringW, lpPrefixString))
    {
        return 0;
    }

    lpTempFileNameW = RtlAllocateHeap(RtlGetProcessHeap(), 0, MAX_PATH * sizeof(WCHAR));
    if (!lpTempFileNameW)
    {
        SetLastError(ERROR_NOT_ENOUGH_MEMORY);
        RtlFreeUnicodeString(&lpPrefixStringW);
        return 0;
    }

    /* Call Unicode */
    ID = GetTempFileNameW(lpPathNameW->Buffer, lpPrefixStringW.Buffer, uUnique, lpTempFileNameW);
    if (ID)
    {
        RtlInitUnicodeString(&TempFileNameStringW, lpTempFileNameW);
        TempFileNameStringA.Buffer = lpTempFileName;
        TempFileNameStringA.MaximumLength = MAX_PATH;
 
        Status = BasepUnicodeStringTo8BitString(&TempFileNameStringA, &TempFileNameStringW, FALSE);
        if (!NT_SUCCESS(Status))
        {
            BaseSetLastNTError(Status);
            ID = 0;
        }
    }

    /* Cleanup */
    RtlFreeUnicodeString(&lpPrefixStringW);
    RtlFreeHeap(RtlGetProcessHeap(), 0, lpTempFileNameW);
    return ID;
 }
Ejemplo n.º 20
0
/***********************************************************************
 *           start_dosbox
 */
static void start_dosbox( const char *appname, const char *args )
{
    static const WCHAR cfgW[] = {'c','f','g',0};
    const char *config_dir = wine_get_config_dir();
    WCHAR path[MAX_PATH], config[MAX_PATH];
    HANDLE file;
    char *p, *buffer;
    int i;
    int ret = 1;
    DWORD written, drives = GetLogicalDrives();
    char *dosbox = find_dosbox();

    if (!dosbox) return;
    if (tolower(appname[0]) == 'z')
    {
        WINE_MESSAGE( "winevdm: Cannot start DOS application %s\n", appname );
        WINE_MESSAGE( "         because DOSBox doesn't support running from the Z: drive.\n" );
        ExitProcess(1);
    }
    if (!GetTempPathW( MAX_PATH, path )) return;
    if (!GetTempFileNameW( path, cfgW, 0, config )) return;
    if (!GetCurrentDirectoryW( MAX_PATH, path )) return;
    file = CreateFileW( config, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0 );
    if (file == INVALID_HANDLE_VALUE) return;

    buffer = HeapAlloc( GetProcessHeap(), 0, sizeof("[autoexec]") +
                        25 * (strlen(config_dir) + sizeof("mount c /dosdevices/c:")) +
                        4 * strlenW( path ) +
                        6 + strlen( appname ) + strlen( args ) + 20 );
    p = buffer;
    p += sprintf( p, "[autoexec]\n" );
    for (i = 0; i < 25; i++)
        if (drives & (1 << i))
            p += sprintf( p, "mount %c %s/dosdevices/%c:\n", 'a' + i, config_dir, 'a' + i );
    p += sprintf( p, "%c:\ncd ", path[0] );
    p += WideCharToMultiByte( CP_UNIXCP, 0, path + 2, -1, p, 4 * strlenW(path), NULL, NULL ) - 1;
    p += sprintf( p, "\n%s %s\n", appname, args );
    p += sprintf( p, "exit\n" );
    if (WriteFile( file, buffer, strlen(buffer), &written, NULL ) && written == strlen(buffer))
    {
        const char *args[4];
        char *config_file = wine_get_unix_file_name( config );
        args[0] = dosbox;
        args[1] = "-conf";
        args[2] = config_file;
        args[3] = NULL;
        ret = spawnvp( _P_WAIT, args[0], args );
    }
    CloseHandle( file );
    DeleteFileW( config );
    HeapFree( GetProcessHeap(), 0, buffer );
    ExitProcess( ret );
}
Ejemplo n.º 21
0
char *oscap_acquire_temp_dir()
{
	WCHAR temp_path[PATH_MAX];
	WCHAR temp_dir[PATH_MAX];

	DWORD ret = GetTempPathW(PATH_MAX, temp_path);
	if (ret > PATH_MAX || ret == 0) {
		oscap_seterr(OSCAP_EFAMILY_WINDOWS, "Could not retrieve the path of the directory for temporary files.");
		return NULL;
	}

	unsigned int unique;
	rand_s(&unique);
	ret = GetTempFileNameW(temp_path, L"oscap", unique, temp_dir);
	if (ret == 0) {
		oscap_seterr(OSCAP_EFAMILY_WINDOWS, "Could not get a name for new temporary directory.");
		return NULL;
	}
	char *temp_dir_str = oscap_windows_wstr_to_str(temp_dir);

	WCHAR *path_prefix = temp_dir;
	do {
		/* Use wide characters with L modifier because we work with a wide string. */
		WCHAR *delimiter = wcschr(path_prefix, L'\\');
		if (delimiter == NULL) {
			break;
		}
		*delimiter = L'\0';
		if (!CreateDirectoryW(temp_dir, NULL)) {
			ret = GetLastError();
			if (ret != ERROR_ALREADY_EXISTS) {
				char *error_message = oscap_windows_error_message(ret);
				oscap_seterr(OSCAP_EFAMILY_WINDOWS, "Could not create temp directory '%s': %s.", temp_dir_str, error_message);
				free(error_message);
				free(temp_dir_str);
				return NULL;
			}
		}
		*delimiter = L'\\';
		path_prefix = ++delimiter;
	} while (*path_prefix != L'\0');

	if (!CreateDirectoryW(temp_dir, NULL)) {
		ret = GetLastError();
		char *error_message = oscap_windows_error_message(ret);
		oscap_seterr(OSCAP_EFAMILY_WINDOWS, "Could not create temp directory '%s': %s.", temp_dir_str, error_message);
		free(error_message);
		free(temp_dir_str);
		return NULL;
	}

	return temp_dir_str;
}
Ejemplo n.º 22
0
Archivo: reg.c Proyecto: GYGit/reactos
static BOOL get_temp_ini_path(LPWSTR name)
{
    WCHAR tmp_dir[MAX_PATH];
    WCHAR prefix[] = {'a','v','p',0};

    if(!GetTempPathW(sizeof(tmp_dir)/sizeof(WCHAR), tmp_dir))
       return FALSE;

    if(!GetTempFileNameW(tmp_dir, prefix, 0, name))
        return FALSE;
    return TRUE;
}
Ejemplo n.º 23
0
static HANDLE temp_file(LPWSTR name) {
    UINT res;
    HANDLE h;

    res = GetTempFileNameW(L".", L"portable_data", 0, name);

    if (res == 0) { show_last_error(L"Failed to create temporary file to decompress portable data"); return INVALID_HANDLE_VALUE; }

    h = CreateFile(name, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
    if (h == INVALID_HANDLE_VALUE) { show_last_error(L"Failed to open temp file to decompress portable data"); }
    return h;

}
Ejemplo n.º 24
0
/*--------------------------------------------------------------------------*/
wchar_t *createtempfilenameW(const wchar_t *wcprefix, BOOL bShortFormat)
{
    wchar_t *wcReturnedTempFilename = NULL;

#ifdef _MSC_VER
    wchar_t *wcTmpDir = getTMPDIRW();
    if (wcTmpDir)
    {
        unsigned int uRetVal = 0;
        wchar_t wcTempFileName[MAX_PATH];
        uRetVal = GetTempFileNameW(wcTmpDir, wcprefix, 0, wcTempFileName);
        if (uRetVal != 0)
        {
            size_t len = wcslen(wcTempFileName) + 1;
            wchar_t* shortTempFilename = (wchar_t *)MALLOC(len * sizeof(wchar_t));
            if (shortTempFilename)
            {
                if (bShortFormat)
                {
                    GetShortPathNameW(wcTempFileName, shortTempFilename, (DWORD)len);
                }
                wcReturnedTempFilename = shortTempFilename;
            }
        }

        FREE(wcTmpDir);
    }
#else
    char *prefix = wide_string_to_UTF8(wcprefix);
    char *result = createtempfilename(prefix, bShortFormat);

    wcReturnedTempFilename = to_wide_string(result);

    if (result)
    {
        FREE(result);
        result = NULL;
    }
    if (prefix)
    {
        FREE(prefix);
        prefix = NULL;
    }
    if (result)
    {
        FREE(result);
        result = NULL;
    }
#endif
    return wcReturnedTempFilename;
}
Ejemplo n.º 25
0
QString QxtTemporaryDirPrivate::create()
{
    QString res;
    wchar_t buffer[MAX_PATH];
    QFileInfo fileInfo(dirTemplate);
    UINT uUnique = GetTempFileNameW((wchar_t*)fileInfo.path().utf16(), (wchar_t*)fileInfo.baseName().utf16(), 0, buffer);
    if (uUnique != 0)
    {
        res = QString::fromUtf16((const ushort*)buffer);
        QFile::remove(res);
        QDir().mkpath(res);
    }
    return res;
}
Ejemplo n.º 26
0
BOOL WINAPI GetTempAppletFileName(LPWSTR Buffer, DWORD BufferCch)
{
	if (BufferCch < MAX_PATH)
		return 0;

	WCHAR TempPath[MAX_PATH];
	DWORD TempPathLength = GetTempPathW(ARRAYSIZE(TempPath), TempPath);

	if (!TempPathLength)
		return 0;

	if (TempPathLength >= ARRAYSIZE(TempPath))
		return 0;

	return GetTempFileNameW(TempPath, L"hco", 0, Buffer);
}
Ejemplo n.º 27
0
static HRESULT install_cab_file(install_ctx_t *ctx)
{
    WCHAR tmp_path[MAX_PATH], tmp_dir[MAX_PATH];
    BOOL res = FALSE, leave_temp = FALSE;
    DWORD i;
    HRESULT hres;

    GetTempPathW(sizeof(tmp_path)/sizeof(WCHAR), tmp_path);

    for(i=0; !res && i < 100; i++) {
        GetTempFileNameW(tmp_path, NULL, GetTickCount() + i*17037, tmp_dir);
        res = CreateDirectoryW(tmp_dir, NULL);
    }
    if(!res)
        return E_FAIL;

    ctx->tmp_dir = tmp_dir;

    TRACE("Using temporary directory %s\n", debugstr_w(tmp_dir));

    hres = extract_cab_file(ctx);
    if(SUCCEEDED(hres)) {
        if(ctx->callback)
            IBindStatusCallback_OnProgress(ctx->callback, 0, 0, BINDSTATUS_INSTALLINGCOMPONENTS, ctx->install_file);

        switch(ctx->install_type) {
        case INSTALL_INF:
            hres = RunSetupCommandW(ctx->hwnd, ctx->install_file, NULL, ctx->tmp_dir, NULL, NULL, RSC_FLAG_INF, NULL);
            if(FAILED(hres))
                WARN("RunSetupCommandW failed: %08x\n", hres);
            break;
        case INSTALL_DLL:
            FIXME("Installing DLL, registering in temporary location\n");
            hres = setup_dll(ctx);
            if(SUCCEEDED(hres))
                leave_temp = TRUE;
            break;
        default:
            assert(0);
        }
    }

    if(!leave_temp)
        RemoveDirectoryW(ctx->tmp_dir);
    return hres;
}
static WCHAR *save_profile( BYTE *buffer, UINT size )
{
    static const WCHAR tstW[] = {'t','s','t',0};
    WCHAR path[MAX_PATH], filename[MAX_PATH], *ret;
    HANDLE handle;
    DWORD count;

    GetTempPathW(MAX_PATH, path);
    GetTempFileNameW(path, tstW, 0, filename);

    handle = CreateFileW(filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
    if (handle == INVALID_HANDLE_VALUE) return NULL;

    WriteFile(handle, buffer, size, &count, NULL);
    CloseHandle( handle );

    ret = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(filename) + 1) * sizeof(WCHAR));
    lstrcpyW(ret, filename);
    return ret;
}
Ejemplo n.º 29
0
char*
mktempdir(void)
{
	WinRune buf[1024];
	WinRune tmp[MAX_PATH];
	WinRune golink[] = {'g', 'o', 'l', 'i', 'n', 'k', '\0'};
	int n;
	
	n = GetTempPathW(nelem(buf), buf);
	if(n <= 0)
		return nil;
	buf[n] = '\0';
	
	if(GetTempFileNameW(buf, golink, 0, tmp) == 0)
		return nil;
	DeleteFileW(tmp);
	if(!CreateDirectoryW(tmp, nil))
		return nil;
	
	return toutf(tmp);
}
Ejemplo n.º 30
0
static DWORD WINAPI download_proc(PVOID arg)
{
    WCHAR tmp_dir[MAX_PATH], tmp_file[MAX_PATH];
    HRESULT hres;

    GetTempPathW(sizeof(tmp_dir)/sizeof(WCHAR), tmp_dir);
    GetTempFileNameW(tmp_dir, NULL, 0, tmp_file);

    TRACE("using temp file %s\n", debugstr_w(tmp_file));

    hres = URLDownloadToFileW(NULL, url, tmp_file, 0, &InstallCallback);
    if(FAILED(hres)) {
        ERR("URLDownloadToFile failed: %08x\n", hres);
        return 0;
    }

    if(sha_check(tmp_file))
        install_file(tmp_file);
    DeleteFileW(tmp_file);
    EndDialog(install_dialog, 0);
    return 0;
}